//classes
var ANJ_Variables, ANJ_Elements, ANJ_AC, ANJ_Date, ANJ_F, ANJ_Airport, ANJ_LcCity, ANJ_Cal, ANJ_AutoMenu;

//holds airninja global functions
ANJ_F = function() {};

//holds airninja elements
var ANJ_e;
//hold airninja global variables
var ANJ_v;

//the active autocomplete
var ANJ_a;

//autocompletes
var ANJ_acfrom, ANJ_acto;

ANJ_Variables = function()
{
	this.today = null;
	this.lastBooking = null;
	this.activeDate = -1;
	this.calDate = null;
	
	// 0 = not locked
	// 1 = locked on from city
	// 2 = locked on to country
	this.lockStatus = 0;
	
	this.fromCountries = null;
	this.fromCities = null;
	this.toCities = null;
	
	// 1 = all cities
	// 2 = low cost only
	this.lcType = 1;

	this.lcValid = false;
	this.skyScannerKeys = [];
	this.lcRequestNum = 0;

	//cached two letter airport lists;
	this.airportLists = [];
};

ANJ_Elements = function()
{
	this.w1 = document.getElementById("ANJ_w1");
	this.w2 = document.getElementById("ANJ_w2");
	this.all = document.getElementById("ANJ_all");
	this.lc = document.getElementById("ANJ_lc");
	this.lc3 = document.getElementById("ANJ_lc3");
	this.lc4 = document.getElementById("ANJ_lc4");
	this.lc5 = document.getElementById("ANJ_lc5");
	this.lc6 = document.getElementById("ANJ_lc6");

	this.txtloc = [ null, document.getElementById("ANJ_txtfrom"), document.getElementById("ANJ_txtto") ];
	this.loading = [ null, document.getElementById("ANJ_fromload"), document.getElementById("ANJ_toload") ];
	this.selectors = [ null, document.getElementById("ANJ_selectorfrom"), document.getElementById("ANJ_selectorto") ];
	this.didja = [ null, document.getElementById("ANJ_didjafrom"), document.getElementById("ANJ_didjato") ];
	this.didja1 = [ null, document.getElementById("ANJ_didjaf1"), document.getElementById("ANJ_didjat1") ];
	this.didja2 = [ null, document.getElementById("ANJ_didjaf2"), document.getElementById("ANJ_didjat2") ];
	this.seldidja = [ null, document.getElementById("ANJ_seldidjafrom"), document.getElementById("ANJ_seldidjato") ];
	this.searching = [ null, document.getElementById("ANJ_fromsearching"), document.getElementById("ANJ_tosearching") ];

	this.ret = document.getElementById("ANJ_ret");
	this.nearby = document.getElementById("ANJ_nearby");
	this.tradbody = document.getElementById("ANJ_tradbody");
	this.lcnone = document.getElementById("ANJ_lcnone");
	this.lcloading = document.getElementById("ANJ_lcloading");
	this.lcbody = document.getElementById("ANJ_lcbody");
	this.cal = document.getElementById("ANJ_cal");
	this.cal_month1 = document.getElementById("ANJ_cal_month1");
	this.cal_month2 = document.getElementById("ANJ_cal_month2");
	this.cal_prev = document.getElementById("ANJ_cal_prev");
	this.cal_next = document.getElementById("ANJ_cal_next");
	this.fromcountries = document.getElementById("ANJ_fromcountries");
	this.fromcities = document.getElementById("ANJ_fromcities");
	this.tocountries = document.getElementById("ANJ_tocountries");
	this.tocities = document.getElementById("ANJ_tocities");
	this.depmonth = document.getElementById("ANJ_depmonth");
	this.depday = document.getElementById("ANJ_depday");
	this.retmonth = document.getElementById("ANJ_retmonth");
	this.retday = document.getElementById("ANJ_retday");
	this.ft2 = document.getElementById("ANJ_ft2");
	this.travelers = document.getElementById("ANJ_travelers");
	this.chknearby = document.getElementById("ANJ_chknearby");

};

ANJ_F.init = function(f,t)
{
	ANJ_a = null;
	ANJ_e = new ANJ_Elements();
	ANJ_v = new ANJ_Variables();
	ANJ_acfrom = new ANJ_AC(1);
	ANJ_acto = new ANJ_AC(2);
	ANJ_F.preloadImages();

	ANJ_v.today = new ANJ_Date();
	ANJ_v.today.setFromToday();

	ANJ_v.lastBooking = new ANJ_Date();
	ANJ_v.lastBooking.setFromToday();
	ANJ_v.lastBooking.addDays(330);

	ANJ_F.fillMonths( ANJ_e.depmonth );
	ANJ_F.fillMonths( ANJ_e.retmonth );

	ANJ_F.fillDays( ANJ_e.depmonth, ANJ_e.depday );
	ANJ_F.fillDays( ANJ_e.retmonth, ANJ_e.retday );

	var date1 = new ANJ_Date();
	date1.setFromToday();
	date1.addDays(7); 
	ANJ_F.setElementsToDate( ANJ_e.depmonth, ANJ_e.depday, date1 );

	var date2 = new ANJ_Date();
	date2.setFromToday();
	date2.addDays(14);
	ANJ_F.setElementsToDate( ANJ_e.retmonth, ANJ_e.retday, date2 );

	if ( f !== "" ) {
		ANJ_acfrom.setAirport( new ANJ_Airport(f), false );
	}

	if ( t !== "" ) {
		ANJ_acto.setAirport( new ANJ_Airport(t), false );
	}

	ANJ_v.calDate = date1;
	ANJ_F.loadTradProviders();
	setTimeout(ANJ_F.init2, 100);	
};

ANJ_F.init2 = function()
{
	ANJ_e.txtloc[1].focus();
};

ANJ_F.createRequestObject = function()
{
	var ro;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer"){
		ro = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		ro = new XMLHttpRequest();
	}
	return ro;
};

ANJ_F.getMatchString = function(s)
{
	var ret = s.toUpperCase();
	ret = ret.replace(/[^A-Z]/g, "");
	return ret;
};


ANJ_F.preloadImages = function()
{
	var i1, i2, i3, i4, i5;
	i1 = new Image();
	i1.src = "travelsearch/images/bg-cal.png";
	i2 = new Image();
	i2.src = "travelsearch/images/prev.png";
	i3 = new Image();
	i3.src = "travelsearch/images/next.png";
	i4 = new Image();
	i4.src = "travelsearch/images/x.png";
	i5 = new Image();
	i5.src = "travelsearch/images/loading.gif";
};

ANJ_F.fillMonths = function(sel)
{
	var d = new ANJ_Date();
	d.setFromToday();
	d.day = 1;

	var count = 0;
	sel.length = 13;
	while ( ANJ_Date.isLater(d, ANJ_v.lastBooking) < 0 )
	{
		sel.options[ count++ ] =  new Option( ANJ_Date.getMonthString(d.month) + " " + d.year, d.year + "-" + d.month );
		d.addMonths(1);
	}
	sel.length = count;
};

ANJ_F.setElementsToDate = function( selm, seld, date )
{
	var ms = date.year + "-" + date.month;
	for ( var i=0; i<selm.options.length; i++ )
	{
		if ( selm.options[i].value == ms )
		{
			selm.selectedIndex = i;
			break;
		}
	}
	ANJ_F.fillDays(selm,seld);
	seld.selectedIndex = date.day-1;
};

ANJ_F.fillDays = function(selm,seld)
{
	var idx = seld.selectedIndex;
	var d = new ANJ_Date();
	d.setFromMonthString( selm.value );
	var count = ANJ_Date.getDaysInMonth( d.month, d.year );
	seld.length = count;
	for ( var i=1; i<=count; i++ )
	{
		seld.options[i-1] = new Option(i,i);
	}
	if ( idx >= -1 ) {
		if ( idx >= seld.options.length ) {
			idx = seld.options.length - 1;
		}
		seld.selectedIndex = idx;
	}
};

ANJ_F.changeMonth = function(which)
{
	if ( which == 1 ) {
		ANJ_F.fillDays(ANJ_e.depmonth, ANJ_e.depday);
	}
	else if ( which == 2 ) {
		ANJ_F.fillDays(ANJ_e.retmonth, ANJ_e.retday);
	}
};

ANJ_F.changeType = function(which)
{
	var a,b,c,d,e;
	ANJ_v.lcType = which;

	a = ANJ_e.w1.style;
	b = ANJ_e.w2.style;
	c = ANJ_e.all.style;
	d = ANJ_e.lc.style;
	e = ANJ_e.nearby.style;

	if ( ANJ_v.lcType == 1 ) {
		a.display = "block";
		b.display = "none";
		c.display = "block";
		d.display = "none";
		e.display = "none";
		if ( ANJ_acfrom.airport !== null && ANJ_acto.airport !== null ) {
			ANJ_F.showTsResults();
		}
	}
	if ( ANJ_v.lcType == 2 ) {
		a.display = "none";
		b.display = "block";
		c.display = "none";
		d.display = "block";
		e.display = "block";
		ANJ_F.loadLowCost();
	}
};

ANJ_F.toggleReturnDay = function(on)
{
	if ( on !== 0 ) {
		ANJ_e.ret.style.visibility = "visible";
	}
	else {
		ANJ_e.ret.style.visibility = "hidden";	
	}
	if ( ANJ_acfrom.airport !== null && ANJ_acto.airport !== null ) {
		ANJ_F.loadTradProviders();
	}
};

ANJ_F.loadLowCost = function()
{
	ANJ_e.lc3.style.display = "none";
	ANJ_e.lc4.style.display = "none";
	ANJ_e.lc5.style.display = "none";
	ANJ_e.lc6.style.display = "none";
	ANJ_v.lcValid = false;
	ANJ_F.hideTsResults();
	ANJ_F.loadFromCountries();
};

ANJ_F.loadFromCountries = function()
{
	var sel = ANJ_e.fromcountries;
	sel.length = 1;
	sel.options[0] = new Option("loading...");
	sel.disabled = true;

	if ( ANJ_v.fromCountries === null )
	{
		var http = ANJ_F.createRequestObject();
		http.open("get", "/script/get-lc-data.php?action=fromcountries", true);
		http.onreadystatechange = function()
		{
			var response;
			if ( http.readyState == 4 ) {
				response = http.responseText;
			}
			else { return; }
			ANJ_v.fromCountries = ANJ_F.loadCountries(response);
			ANJ_F.drawCountries(ANJ_v.fromCountries, sel);
		};
		http.send(null);
	}
	else {
		ANJ_F.drawCountries(ANJ_v.fromCountries, sel);
	}
};

ANJ_F.loadCountries = function(response)
{
	var countries = [];
	var lines = response.split("\n");
	for (var i=0; i<lines.length-1; i++) 
	{
		countries.push( lines[i] );
	}
	return countries;
};

ANJ_F.drawCountries = function(countries, sel)
{
	sel.length = countries.length + 1;
	sel.options[0] = new Option("select a country", -1);
	for (var i=0; i<countries.length; i++)
	{
		sel.options[i+1] = new Option(countries[i], countries[i]);
	}
	sel.disabled = false;
};

ANJ_F.changeFromCountry = function()
{
	ANJ_v.lcValid = false;
	ANJ_F.hideTsResults();
	ANJ_e.lc6.style.display = "none";
	if ( ANJ_e.fromcountries.selectedIndex === 0 ) {
		ANJ_e.lc3.style.display = "none";
		ANJ_e.lc4.style.display = "none";
		ANJ_e.lc5.style.display = "none";
	}
	else {
		ANJ_e.lc3.style.display = "block";
		ANJ_e.lc4.style.display = "block";
		ANJ_e.lc5.style.display = "block";
		ANJ_v.lockStatus = 0;
		ANJ_F.loadFromCities();
		ANJ_F.loadToCountries();
	}
};

ANJ_F.loadToCountries = function()
{
	var sel = ANJ_e.tocountries;
	sel.length = 1;
	sel.options[0] = new Option("loading...");
	sel.disabled = true;

	ANJ_e.lc6.style.display = "none";

	var nb = "&nearby=";
	if ( ANJ_e.chknearby.checked ) {
		nb += "1";
	}
	else { 
		nb += "0"; 
	}

	var http = ANJ_F.createRequestObject();
	if ( ANJ_v.lockStatus === 0 ) {
		var fromcountry = ANJ_e.fromcountries.value;
		http.open("get", "/script/get-lc-data.php?action=tocountries&fromcountry=" + escape(fromcountry) + nb, true);
	}
	else {
		var fromcity = ANJ_v.fromCities[ ANJ_e.fromcities.value ];
		var params;
		if ( fromcity.usespecific && !ANJ_e.chknearby.checked ) {
			params = "fromairport=" + escape(fromcity.code);
		}
		else {
			params = "fromcity=" + escape(fromcity.id) + nb;
		}
		http.open("get", "/script/get-lc-data.php?action=tocountries&" + params, true);
	}

	http.onreadystatechange = function()
	{
		var response;
		if ( http.readyState == 4 ) {
			response = http.responseText;
		}
		else { return; }

		var c = ANJ_F.loadCountries(response);
		ANJ_F.drawCountries(c, sel);
	};
	http.send(null);
};

ANJ_F.changeToCountry = function()
{
	ANJ_v.lcValid = false;
	ANJ_F.hideTsResults();
	var sel = ANJ_e.tocountries;
	if ( sel.selectedIndex === 0  )
	{
		if ( ANJ_v.lockStatus == 2 ) {
			ANJ_v.lockStatus = 0;
			ANJ_F.loadFromCities();
		}
	}
	else
	{
		switch ( ANJ_v.lockStatus )
		{
			case 0:
				ANJ_v.lockStatus = 2;
				ANJ_F.loadFromCities();
				break;
			case 1:
				ANJ_F.loadToCities();
				break;
			case 2:
				ANJ_F.loadFromCities();
				break;
		}
	}
};

ANJ_F.loadFromCities = function()
{
	var sel = ANJ_e.fromcities;
	sel.length = 1;
	sel.options[0] = new Option("loading...");
	sel.disabled = true;

	ANJ_e.lc6.style.display = "none";

	var fromcountry = ANJ_e.fromcountries.value;
	var http = ANJ_F.createRequestObject();

	if ( ANJ_v.lockStatus == 2 )
	{
		var tocountry = ANJ_e.tocountries.value;	
		var params = "&fromcountry=" + escape(fromcountry) + "&tocountry=" + escape(tocountry);
		if ( ANJ_e.chknearby.checked ) {
			params += "&nearby=1";
		}
		http.open("get", "/script/get-lc-data.php?action=fromcities" + params, true);
	}
	else
	{
		http.open("get", "/script/get-lc-data.php?action=fromcities&fromcountry=" + escape(fromcountry), true);
	}

	http.onreadystatechange = function()
	{
		var response;
		if ( http.readyState == 4 ) {
			response = http.responseText;
		}
		else { return; }
		var lines = response.split("\n");
		ANJ_v.fromCities = [];
		ANJ_F.loadCities(lines, ANJ_v.fromCities);
		ANJ_F.drawCities(ANJ_v.fromCities, sel);
	};
	http.send(null);
};

ANJ_F.changeFromCity = function()
{
	ANJ_v.lcValid = false;
	ANJ_F.hideTsResults();
	var sel = ANJ_e.fromcities;
	if ( sel.value == -1  )
	{
		if ( ANJ_v.lockStatus == 1 )
		{
			ANJ_v.lockStatus = 0;
			ANJ_F.loadToCountries();
		}
	}
	else
	{
		var fromcity = ANJ_v.fromCities[ sel.value ];

		ANJ_acfrom.setAirport ( new ANJ_Airport( fromcity.code + "\t" + fromcity.place + "\t" + fromcity.country + "\t" + fromcity.makeToken() + "\t0\t\t" + fromcity.id ), false );

		if ( ANJ_v.lockStatus === 0 ) {
			ANJ_v.lockStatus = 1;
			ANJ_F.loadToCountries();
		}
		else if ( ANJ_v.lockStatus == 1 ) {
			ANJ_F.loadToCountries();
		}
		else if ( ANJ_v.lockStatus == 2)  {
			ANJ_F.loadToCities();
		}
	}
};

ANJ_F.loadToCities = function()
{
	ANJ_e.lc6.style.display = "block";

	var sel = ANJ_e.tocities;
	sel.length = 1;
	sel.options[0] = new Option("loading...");
	sel.disabled = true;

	var tocountry = ANJ_e.tocountries.value;	
	var fromcity = ANJ_v.fromCities[ ANJ_e.fromcities.value ];

	var params;
	if ( fromcity.usespecific && !ANJ_e.chknearby.checked ) {
		params = "&fromairport=" + escape(fromcity.code);
	}
	else
	{
		params = "&fromcity=" + escape(fromcity.id) + "&nearby=";
		if ( ANJ_e.chknearby.checked ) {
			params += "1";
		}
		else { 
			params += "0";
		}
	}

	var http = ANJ_F.createRequestObject();	
	http.open("get", "/script/get-lc-data.php?action=tocities&tocountry=" + escape(tocountry) + params, true);
	http.onreadystatechange = function()
	{
		var response;
		if ( http.readyState == 4 ) {
			response = http.responseText;
		}
		else { return; }
		var lines = response.split("\n");
		ANJ_v.toCities = [];
		ANJ_F.loadCities(lines, ANJ_v.toCities);
		ANJ_F.drawCities(ANJ_v.toCities, sel);
	};
	http.send(null);
};

ANJ_F.changeToCity = function()
{
	var sel = ANJ_e.tocities;
	if ( sel.value == -1 )
	{
		ANJ_v.lcValid = false;
		ANJ_F.hideTsResults();
	}
	else
	{
		var tocity = ANJ_v.toCities[ sel.value ];
		ANJ_acto.setAirport ( new ANJ_Airport( tocity.code + "\t" + tocity.place + "\t" + tocity.country + "\t" + tocity.makeToken() + "\t0\t\t" + tocity.id ), false );
		ANJ_v.lcValid = true;
		ANJ_F.showTsResults();
	}
};

ANJ_F.loadCities = function(lines, cities)
{
	var citycount = [];
	var added = [];
	var i;
	var c, k;
	for (i=0; i<lines.length; i++)
	{
		var x = lines[i];
		if ( x.ANJ_trim() === "" ) {
			continue;
		}
		c = new ANJ_LcCity(lines[i]);
		cities.push( c );

		k = "x" + c.id;
		if ( citycount[k] ) {
			citycount[k]++;
		}
		else {
			citycount[k] = 1;
		}
		if ( c.allairportscode == c.code )  {
			added[k] = true;
		}
	}
	// Add city - all airports
	for (i=0; i<cities.length; i++)
	{
		c = cities[i];
		k = "x" + c.id;
		if ( citycount[k] > 1 && !added[k] )
		{
			c = new ANJ_LcCity( c.id + "\t" + c.defaultcode + "\t" + c.place + "\t" + c.region + "\t" + c.regionabbv + "\t" + c.country + "\tAll Airports\t" + c.allairportscode + "\t" + c.defaultcode);
			cities.push( c );
			added[k] = true;
		}
	}
	// Give tokens to cities
	for (i=0; i<cities.length; i++)
	{
		c = cities[i];
		var num = citycount["x" + c.id];
		if ( num == 1 ) {
			c.token = c.place;
		}
		else 
		{
			c.token = c.place + " - " + c.airport;
			if ( c.airport != "All Airports" || c.code == c.allairportscode ) {
				c.token += " (" + c.code + ")";
			}
		}
		if ( num > 1 && c.airport != "All Airports" ) {
			c.usespecific = true;
		}
	}
	cities.sort( ANJ_LcCity.cmp );
};

ANJ_F.drawCities = function(cities, sel)
{
	var i, r = "", ops = [];
	for (i=0; i<cities.length; i++)
	{
		var c = cities[i];
		if ( ( c.country == "United States" || c.country == "Canada" || c.country == "Australia" ) && c.region != r )
		{
			r = c.region;
			ops.push( new Option(r,-1) );
		}
		ops.push( new Option(c.token, i) );
	}

	sel.length = ops.length+1;
	sel.options[0] = new Option("select a city...", -1);
	for (i=0; i<ops.length; i++)
	{
		var o = ops[i];
		sel.options[i+1] = o;
		if ( o.value == -1 ) {
			sel.options[i+1].className = "ANJ_opRegion";
		}
	}
	sel.disabled = false;
};

/*************************
*
* AUTOCOMPLETE
*
**************************/

ANJ_AutoMenu = function(parent)
{
	this.parent = parent;
	this.div = ANJ_e.selectors[parent.which];
	this.subset = null;
	this.maxshown = 8;
	this.selidx = -1;

	this.hide = function()
	{
		this.div.style.display = "none";
		this.selidx = -1;
	};

	this.moveUp = function()
	{
		if ( this.selidx > 0  )
		{
			this.erase();
			this.selidx--;
			this.highlight();
		}
	};

	this.moveDown = function()
	{
		var idx = this.selidx;
		if ( idx < this.subset.length-1 && idx <this.maxshown-1 ) 
		{
			this.erase();
			this.selidx++;
			this.highlight();
		}
	};

	this.selRow = function(r)
	{
		if ( r != this.selidx ) 
		{
			this.erase();
			this.selidx = r;
			this.highlight();
		}
	};

	this.clickRow = function(r)
	{
		this.selidx = r;
		parent.close();
	};

	this.erase = function() { 
		document.getElementById("ANJ_row" + this.parent.which + "_" + this.selidx ).className = "row"; 
	};
	
	this.highlight = function() {
		document.getElementById("ANJ_row" + this.parent.which + "_" + this.selidx ).className = "selrow"; 
	};

	this.draw = function()
	{
		var airports = ANJ_v.airportLists[ parent.firsttwo ];

		this.subset = [];
		if ( airports.length === 0 ) { 
			this.hide();
			return;
		}

		var w = parent.txt;
		var i;
		for (i=0; i<airports.length; i++)
		{
			var a = airports[i];
			if ( w == a.code ) {
				this.subset.push( a );
				continue;
			}
			if ( a.matchString.length < w.length ) {
				continue;
			}
			if ( a.matchString.substr(0,w.length) == w ) {
				this.subset.push( a );
			}
		}
		this.subset.sort ( ANJ_Airport.cmp );

		if ( this.subset.length === 0 ) {
			this.hide();
			return;
		}

		var html = [];
		for (i=0; i<this.subset.length && i<this.maxshown; i++) {
			html.push ("<div class=\"row\" id=\"ANJ_row" + this.parent.which + "_" + i + "\" onclick=\"ANJ_AC.clickRow(" + i + ")\" onmouseover=\"ANJ_AC.selectRow(" + i + ")\">" + this.subset[i].token + "</div>");
		}
		this.selidx = 0;
		this.div.innerHTML = html.join('');
		this.div.style.display = "block";
		this.highlight();
		parent.setstate(ANJ_AC.MENU);
	};
};

ANJ_AC = function(which)
{
	this.which = which;
	this.airport = null;
	this.state = ANJ_AC.NOTHING;
	this.menu = new ANJ_AutoMenu(this);
	this.input = ANJ_e.txtloc[this.which];
	this.txt = "";
	this.usertyped = false;
	this.firsttwo = "";
	this.reqnum = 0;
	this.suggested = null;
	
	this.setstate = function(newstate) {
		
		if ( this.state == newstate) {
			return;
		}

		var ldiv = ANJ_e.loading[this.which];

		if ( this.state == ANJ_AC.DIDJA ) {
			ANJ_e.didja[this.which].style.display = "none";
		}

		if ( this.state == ANJ_AC.MENU ) {
			this.menu.hide();
		}

		if ( this.state == ANJ_AC.LOADAIRPORTS || this.state == ANJ_AC.LOADSUGGESTED ) {
			ldiv.style.display = "none";
		}

		if ( newstate == ANJ_AC.DIDJA ) {
			ANJ_e.didja[this.which].style.display = "block";
		}

		if ( newstate == ANJ_AC.LOADAIRPORTS ) {
			ldiv.innerHTML = "loading airports...";
			ldiv.style.display = "block";
		}

		if ( newstate == ANJ_AC.LOADSUGGESTED ) {
			ldiv.innerHTML = "searching for airport...";
			ldiv.style.display = "block";
		}
		
		this.state = newstate;
	};

	this.usertyping = function()
	{
		var x = ANJ_F.getMatchString ( this.input.value );
		if ( x == this.txt ) {
			return;
		}
		this.txt = x;
		this.usertyped = true;

		if ( this.state != ANJ_AC.LOADAIRPORTS ) {
			this.setstate( ANJ_AC.NOTHING );
		}

		this.airport = null;
		ANJ_F.hideTsResults();

		if ( x.length < 2 ) {
			this.setstate( ANJ_AC.NOTHING );
			return;
		}
		
		var ft = x.substring(0,2);

		if ( ft != this.firsttwo )
		{
			this.firsttwo = ft; 
			if ( ANJ_v.airportLists[ft] === undefined )
			{
				var http = ANJ_F.createRequestObject();
				http.open("get", "/travelsearch/script/airports/" + ft + ".txt");
				this.setstate( ANJ_AC.LOADAIRPORTS );
				var ac = this;
				http.onreadystatechange = function()
				{
					var response;
					if( http.readyState == 4 ) {
						response = http.responseText;
					} 
					else { return; }
					if ( http.status == 404 ) {
						response = "";
					}
		
					var airports = [];
					var lines = response.split("\n");
					for (var i=0; i<lines.length; i++)
					{
						if ( lines[i].length <= 0 ) {
							continue;
						}
						airports.push( new ANJ_Airport(lines[i]) );
					}
					ANJ_v.airportLists[ft] = airports;

					if ( ac.state != ANJ_AC.LOADAIRPORTS )
					{
						return;
					}
					if ( ac.firsttwo != ft )
					{
						return;
					}
					ac.setstate( ANJ_AC.NOTHING );
					ac.menu.draw();
				};
				http.send(null);
			}
			else {
				this.menu.draw();
			}
		} 
		else {
			if ( ANJ_v.airportLists[ft] !== undefined ) {
				this.menu.draw();
			}
		}
	};

	this.close = function()
	{
		if ( this.menu.selidx != -1 )
		{
			var a = this.menu.subset[ this.menu.selidx ];
			this.setAirport(a, true);
			this.setstate( ANJ_AC.NOTHING );
		}
		else {
			if ( this.usertyped && this.airport === null && this.txt !== "" ) {
				this.setstate( ANJ_AC.NOTHING );
				this.starterror();
			}
		}
		this.usertyped = false;
		ANJ_a = null;
	};

	this.setAirport = function(a, ss)
	{
		this.airport = a;
		this.input.value = a.token;
		this.txt = ANJ_F.getMatchString ( this.input.value );
		if ( ss && ANJ_acfrom.airport !== null && ANJ_acto.airport !== null ) {
			ANJ_F.showTsResults();
		}
	};

	this.starterror = function()
	{
		var http = ANJ_F.createRequestObject();
		http.open("get", "/travelsearch/script/get-suggested.php?input=" + escape( this.input.value ) );

		var sel = ANJ_e.seldidja[ this.which ];
		this.setstate( ANJ_AC.LOADSUGGESTED );

		var rnum = ++this.reqnum;
		var ac = this;

		http.onreadystatechange = function()
		{
			var response;
			if( http.readyState == 4 ) {
				response = http.responseText;
			} 
			else { return; }
			if ( http.status == 404 ) {
				response = "";
			}

			if ( ac.state != ANJ_AC.LOADSUGGESTED )
			{
				return;
			}
			if ( rnum != ac.reqnum )
			{
				return;
			}

			ac.suggested = [];

			var i;
			var lines = response.split("\n");
			for (i=0; i<lines.length; i++)
			{
				if ( lines[i].length <= 0 ) {
					continue;
				}
				ac.suggested.push( new ANJ_Airport(lines[i]) );
			}

			var airport = ac.findsugmatch();
			if ( airport !== null )
			{
				ac.setAirport(airport, true);
				ac.setstate( ANJ_AC.NOTHING );
			}
			else
			{
				var d1 = ANJ_e.didja1[ac.which], d2 = ANJ_e.didja2[ac.which];
				if ( ac.suggested.length === 0 ) {
					d1.innerHTML = "No matches found.";
					d2.style.display = "none";
				}
				else {
					if ( ac.suggested.length == 1 ) {
						d1.innerHTML = "No exact match found.  Did you mean?";
					} 
					else {
						d1.innerHTML = "Multiple possible matches.  Did you mean?";
					}
					sel.length = ac.suggested.length + 1;
					sel.options[0] = new Option("Choose an airport", -1);
					for (i=0; i<ac.suggested.length; i++) {
						sel.options[i+1] = new Option( ac.suggested[i].token, i );
					}
					d2.style.display = "block";
				}
				ac.setstate( ANJ_AC.DIDJA );
			}
		};
		http.send(null);
	};

	this.choosesug = function()
	{
		var idx = ANJ_e.seldidja[ this.which ].value;
		if ( idx == -1 ) {
			return;
		}
		this.setAirport ( this.suggested[idx], true );
		this.setstate( ANJ_AC.NOTHING );
	};

	this.findsugmatch = function()
	{
		var a;
		if ( this.suggested.length == 1 )
		{
			a = this.suggested[0];
			if ( a.code == this.txt ) {
				return a;
			}
			if ( ANJ_F.getMatchString(a.token) == this.txt ) {
				return a;
			}
		}
		var m = null;
		for (var i=0; i<this.suggested.length; i++ )
		{
			a = this.suggested[i];
			if ( ANJ_F.getMatchString(a.place) == this.txt ) {
				if ( m === null ) {
					m = a;
				} 
				else {
					m = null;
					break;
				}
			}
		}
		return m;
	};
};

ANJ_AC.NOTHING = 0;
ANJ_AC.MENU = 1;
ANJ_AC.DIDJA = 2;
ANJ_AC.LOADAIRPORTS = 3;
ANJ_AC.LOADSUGGESTED = 4;

ANJ_AC.focus = function(which)
{
	if ( which == 1 ) {
		ANJ_a = ANJ_acfrom;
	}
	else {
		ANJ_a = ANJ_acto;
	}
};

ANJ_AC.keyDown = function(e) 
{
	if ( ANJ_a === null ) {
		return;
	}

	var code = ANJ_AC.getKeyCode(e);
	switch (code)
	{
		case 9:
		case 13:
			ANJ_a.close(); 
			break;
		case 27:
			ANJ_a.menu.hide();
			break;
		case 38:
			ANJ_a.menu.moveUp();
			break;
		case 40:
			ANJ_a.menu.moveDown(); 
			break;
	}
};

ANJ_AC.typing = function(which, event)
{
	if ( ANJ_a === null ) {
		ANJ_AC.focus(which);
	}
	ANJ_a.usertyping();
};

ANJ_AC.getKeyCode = function(e)
{
	if (document.all) {
		return window.event.keyCode;
	}
	else { 
		return e.which;
	}
};

ANJ_AC.selectRow = function(r)
{
	ANJ_a.menu.selRow(r);
};

ANJ_AC.clickRow = function(r) 
{
	ANJ_a.menu.clickRow(r);
};

ANJ_AC.chooseSuggested = function(which)
{
	if ( which == 1 ) {
		ANJ_acfrom.choosesug();
	}
	else {
		ANJ_acto.choosesug();
	}
};

ANJ_AC.close = function()
{
	if ( ANJ_a !== null ) {
		ANJ_a.close();
	}
};

/*************************
*
* TRAVEL SEARCH RESULTS
*
*************************/

ANJ_F.makeLcRoutesRequest = function()
{
	ANJ_F.lcSetLoading(true);
	ANJ_v.lcRequestNum++;
	var http = ANJ_F.createRequestObject();

	var params = "";
	if ( ANJ_v.lcType == 2 )
	{
		var fromcity = ANJ_v.fromCities[ ANJ_e.fromcities.value ];
		var tocity = ANJ_v.toCities[ ANJ_e.tocities.value ];
		var nearby =  ANJ_e.chknearby.checked;
		if ( fromcity.usespecific ) {
			params = "&depairport=" + fromcity.code;
		}
		params += "&depcity=" + fromcity.id;
		if ( tocity.usespecific  ) {
			params += "&arrairport=" + tocity.code;
		}
		params += "&arrcity=" + tocity.id;
		if ( nearby ) {
			params += "&nearby=1";
		}
		else {
			params += "&nearby=0";
		}
	}
	else
	{
		if ( ANJ_acfrom.airport.token.indexOf("All Airports") < 0  ) {
			params = "&depairport=" + ANJ_acfrom.airport.code;
		}
		params += "&depcity=" + ANJ_acfrom.airport.cityid;
		if ( ANJ_acto.airport.token.indexOf("All Airports") < 0  ) {
			params += "&arrairport=" + ANJ_acto.airport.code;
		}
		params += "&arrcity=" + ANJ_acto.airport.cityid;
		params += "&nearby=1";
	}
	
	http.open("get", "/travelsearch/script/get-lc-routes.php?num=" + ANJ_v.lcRequestNum + params);
	http.onreadystatechange = function()
	{
		var response;
		if ( http.readyState == 4 ) {
			response = http.responseText;
		}
		else { return; }
		ANJ_e.lcbody.innerHTML = response;
		ANJ_e.lcbody.style.display = "block";
		ANJ_e.lcnone.style.display = "none";
		ANJ_F.lcSetLoading(false);
	};
	http.send(null);
};

ANJ_F.lcSetLoading = function(show)
{
	if ( show ) {
		ANJ_e.lcloading.style.display = "block";
		ANJ_e.lcnone.style.display = "none";
		ANJ_e.lcbody.style.display = "none";
	}
	else {
		ANJ_e.lcloading.style.display = "none";
	}
};

ANJ_F.goLcRoute = function(airlineid, depcode, arrcode)
{
	var d1 = new ANJ_Date();
	var d2 = new ANJ_Date();
	d1.setFromElements( ANJ_e.depmonth, ANJ_e.depday );
	d2.setFromElements( ANJ_e.retmonth, ANJ_e.retday );
	if ( !ANJ_F.checkDates(d1, d2) ) {
		return;
	}
	var d1s = d1.month + "/" + d1.day + "/" + d1.year;
	var d2s = d2.month + "/" + d2.day + "/" + d2.year;
	var url = "/travelsearch/go-airline.php?airline=" + airlineid + "&depcode=" + depcode + "&arrcode=" + arrcode + "&depart=" + escape( d1s ) + "&arrive=" + escape( d2s ) + "&travelers=" + ANJ_e.travelers.value;
	if ( ANJ_e.ft2.checked ) {
		url += "&flighttype=2";
	}
	else { 
		url += "&flighttype=1"; 
	}
	window.open(url);
};

ANJ_F.checkDates = function(d1, d2)
{
	if ( ANJ_Date.isLater(ANJ_v.today, d1) == 1 )
	{
		alert( "Departure date: Please enter a date that is today or later.");  
		return false;
	}

	if ( ANJ_e.ft2.checked )
	{
		if ( ANJ_Date.isLater(d1, d2) > 0  )
		{
			alert("Your return date must be on or after your departure date.");
			return false;
		}
		if ( ANJ_Date.isLater(d2, ANJ_v.lastBooking) > 0 )
		{
			alert("Return Date: Sorry, reservations are not accepted past " + ANJ_v.lastBooking.month + "/" + ANJ_v.lastBooking.day + "/" + ANJ_v.lastBooking.year );
			return false;
		}
	}
	else
	{
		if ( ANJ_Date.isLater(d1, ANJ_v.lastBooking) > 0)
		{
			alert("Departure Date: Sorry, reservations are not accepted past " + ANJ_v.lastBooking.month + "/" + ANJ_v.lastBooking.day + "/" + ANJ_v.lastBooking.year );
			return false;
		}
	}
	return true;
};

ANJ_F.hideTsResults = function()
{
	ANJ_e.lcbody.style.display = "none";
	ANJ_e.lcnone.style.display = "block";
	ANJ_F.lcSetLoading(false);
	ANJ_F.loadTradProviders();
};

ANJ_F.showTsResults = function()
{
	ANJ_F.makeLcRoutesRequest();
	ANJ_F.loadTradProviders();
};

ANJ_F.doTradSearch = function(engine)
{
	var from;
	var fromCountry;

	var to;
	var toCountry;

	if ( ANJ_v.lcType == 1 )
	{
		if ( ANJ_e.txtloc[1].value === "" )
		{
			alert("Please enter the city or airport code you are leaving from.");
			return;
		}
		
		if ( ANJ_e.txtloc[2].value === "" )
		{
			alert("Please enter the city or airport code you are going to.");
			return;
		}

		if ( ANJ_acfrom.airport === null )
		{
			from = ANJ_e.txtloc[1].value;
			fromCountry = "";
		}
		else
		{
			from = ANJ_acfrom.airport.code;
			fromCountry = ANJ_acfrom.airport.country;
		}

		if ( ANJ_acto.airport === null )
		{
			to = ANJ_e.txtloc[2].value;
			toCountry = "";
		}
		else
		{
			to = ANJ_acto.airport.code;
			toCountry = ANJ_acto.airport.country;
		}
	}
	else
	{
		if ( !ANJ_v.lcValid )
		{
			alert("Please select your departure and arrival cities");
			return;
		}

		var x = ANJ_v.fromCities[ ANJ_e.fromcities.value ];
		var y = ANJ_v.toCities[ ANJ_e.tocities.value ];

		from = x.code;
		fromCountry = x.country;

		to = y.code;
		toCountry = y.country;
	}

	if ( from == to && fromCountry == toCountry )
	{
		alert("Error:  FROM and TO locations can't be the same.");
		return;
	}

	var d1 = new ANJ_Date();
	var d2 = new ANJ_Date();
	d1.setFromElements( ANJ_e.depmonth, ANJ_e.depday );
	d2.setFromElements( ANJ_e.retmonth, ANJ_e.retday );
	if ( !ANJ_F.checkDates(d1, d2) ) {
		return;
	}
	
	var d1s = d1.month + "/" + d1.day + "/" + d1.year;
	var d2s = d2.month + "/" + d2.day + "/" + d2.year;
	var url = "/travelsearch/trad-search.php?engine=" + engine + "&depcode=" + escape(from) + "&arrcode=" + escape(to) + "&depcountry=" + escape(fromCountry) + "&arrcountry=" + escape(toCountry) + "&depart=" + escape( d1s ) + "&arrive=" + escape( d2s ) + "&travelers=" + ANJ_e.travelers.value;
	if ( ANJ_e.ft2.checked ) {
		url += "&flighttype=rt";
	}
	else {
		url += "&flighttype=ow";
	}
	window.open(url);
};

ANJ_F.loadTradProviders = function()
{
	var html = [];
	if ( ANJ_acfrom.airport === null || ANJ_acto.airport === null ) {
		ANJ_F.loadTradDefault(html);
	}
	else
	{
		/*if ( ANJ_acfrom.airport.country == "Australia" || ANJ_acfrom.airport.country == "New Zealand" || ANJ_acfrom.airport.country == "Malaysia" ) {
			ANJ_F.loadBestFlights(html);
		}*/
	
		ANJ_F.loadTradDefault(html);

		if ( ANJ_acfrom.airport.country == "United States" || ANJ_acfrom.airport.country == "Canada" )
		{
			html.push("<div class=\"provider\" onclick=\"ANJ_F.doTradSearch('travelocity')\"><img src=\"/travelsearch/images/providers/travelocity.png\" alt=\"Travelocity\"></div>");
			html.push("<div class=\"provider\" onclick=\"ANJ_F.doTradSearch('priceline')\"><img src=\"/travelsearch/images/providers/priceline.png\" alt=\"Priceline\"></div>");
		}

		if ( ANJ_acfrom.airport.country == "United Kingdom"  ) {
			html.push("<div class=\"provider\" onclick=\"ANJ_F.doTradSearch('skyscanner')\"><img src=\"/travelsearch/images/providers/skyscanner.png\" alt=\"Skyscanner\"></div>");
		}

		/*if ( ANJ_acfrom.airport.country == "United Kingdom" || ANJ_acfrom.airport.country == "Canada" ) {
			ANJ_F.loadBestFlights(html);
		}*/	

		/*var skey = ANJ_acfrom.airport.code + "-" + ANJ_acto.airport.code;
		if ( ANJ_v.skyScannerKeys[skey] === undefined ) {
			ANJ_F.checkSkyScanner(skey);
		}
		else if ( ANJ_v.skyScannerKeys[skey] == 1 ) {
			html.push("<div class=\"provider\" onclick=\"ANJ_F.doTradSearch('skyscanner')\"><img src=\"/travelsearch/images/providers/skyscanner.png\" alt=\"Skyscanner\"></div>");
		}*/
	}
	html.push("<div style=\"clear:both;\"></div>");
	ANJ_e.tradbody.style.display = "block";
	ANJ_e.tradbody.innerHTML = html.join('');
};

ANJ_F.loadTradDefault = function(html)
{
	html.push("<div class=\"provider\" onclick=\"ANJ_F.doTradSearch('expedia')\"><img src=\"/travelsearch/images/providers/expedia.png\" alt=\"Expedia\"></div>");
	html.push("<div class=\"provider\" onclick=\"ANJ_F.doTradSearch('kayak')\"><img src=\"/travelsearch/images/providers/kayak.png\" alt=\"Kayak\"></div>");
	html.push("<div class=\"provider\" onclick=\"ANJ_F.doTradSearch('orbitz')\"><img src=\"/travelsearch/images/providers/orbitz.png\" alt=\"Orbitz\"></div>");
};

ANJ_F.loadBestFlights = function(html)
{
	html.push("<div class=\"provider\" onclick=\"ANJ_F.doTradSearch('bestflights')\"><img src=\"/travelsearch/images/providers/bestflights.png\" alt=\"Best Flights\"></div>");
};

ANJ_F.checkSkyScanner = function(key)
{
	var ssHttp = ANJ_F.createRequestObject();
	ssHttp.open("get", "/travelsearch/script/checkskyscanner.php?key=" + key);
	ssHttp.onreadystatechange = function()
	{
		var response;
		if ( ssHttp.readyState == 4 ) {
			response = ssHttp.responseText;
		}
		else { return; }

		var result = parseInt( response.ANJ_trim(), 10 );
		ANJ_v.skyScannerKeys[key] = result;
		if ( result == 1 ) {
			ANJ_F.loadTradProviders();
		}
	};
	ssHttp.send(null);
};

/***************************
*
* ANJ_Date
*
***************************/

ANJ_Date = function()
{
	this.addDays = function(num)
	{
		for (var i=0; i<num; i++)
		{
			this.day++;
			if ( this.day > ANJ_Date.getDaysInMonth(this.month, this.year) )
			{
				this.day = 1;
				this.month++;
				if ( this.month > 12 )
				{
					this.month=1;
					this.year++;
				}
			}
		}
	};

	this.addMonths = function (num)
	{
		var i;
		if ( num > 0 ) {
			for (i=0; i<num; i++)
			{
				this.month++;
				if ( this.month > 12 )
				{
					this.month = 1;
					this.year++;
				}
			}
		}
		else {
			for (i=0; i>num; i--)
			{
				this.month--;
				if ( this.month < 1 )
				{
					this.month = 12;
					this.year--;
				}
			}
		}
	};

	this.setFromToday = function()
	{
		var x = new Date();
		this.year = x.getFullYear();
		this.month = x.getMonth() + 1;
		this.day = x.getDate();
	};

	this.setFromDate = function(month, day, year)
	{
		this.year = year;
		this.month = month;
		this.day = day;
	};

	this.setFromMonthString = function(ms)
	{
		var x = ms.split("-");
		this.year = parseInt(x[0], 10);
		this.month = parseInt(x[1], 10);
	};

	this.setFromElements = function(selM, selD)
	{
		var ms = selM.value;
		this.setFromMonthString(ms);
		this.day = parseInt(selD.value, 10);
	};
};

ANJ_Date.getStartDay = function(m,y)
{
	var ret;

	switch (m)
	{
		case 1:
			ret = 6;
			break;
		case 2:
			ret = 2;
			break;
		case 3:
			ret = 2;
			break;
		case 4:
			ret = 5;
			break;
		case 5:
			ret = 0;
			break;
		case 6:
			ret = 3;
			break;
		case 7:
			ret = 5;
			break;
		case 8:
			ret = 1;
			break;
		case 9:
			ret = 4;
			break;
		case 10:
			ret = 6;
			break;
		case 11:
			ret = 2;
			break;
		case 12:
			ret = 4;
			break;
	}

	var advance = y - 2005;
	var lp = Math.floor ( advance / 4 );
	if ( y % 4 === 0 && m > 2 ) {
		lp++;
	}
	ret += advance + lp;
	ret = ret % 7;

	return ret; 
};

//  returns
//  1	d1 is later than d2
// -1	d2 is later than d1
//  0	the dates are the same
ANJ_Date.isLater = function(d1, d2)
{
	 if ( d1.year > d2.year ) {
		  return 1;
	 }
	 if ( d1.year < d2.year ) {
		  return -1;
	 }
	 if ( d1.month > d2.month ) {
		  return 1;
	 }
	 if ( d1.month < d2.month ) {
		  return -1;
	 }
	 if ( d1.day > d2.day ) {
		  return 1;
	 }
	 if ( d1.day < d2.day ) {
		  return -1;
	 }
	 return 0;
};


ANJ_Date.getDaysInMonth = function(m, y)
{
	switch (m)
		{
	case 1:
		return 31;
	case 2:
		if ( y % 4 === 0 ) {
			return 29;
		}
		else {
			return 28;
		}
		break;
	case 3:
		return 31;
	case 4:
		return 30;
	case 5:
		return 31;
	case 6:
		return 30;
	case 7:
		return 31;
	case 8:
		return 31;
	case 9:
		return 30;
	case 10:
		return 31;
	case 11:
		return 30;
	case 12:
		return 31;
	}
};

ANJ_Date.getMonthString = function(m)
{
	switch (m)
	{
		case 1:
			return "Jan";
		case 2:
			return "Feb";
		case 3:
			return "Mar";
		case 4:
			return "Apr";
		case 5:
			return "May";
		case 6:
			return "Jun";
		case 7:
			return "Jul";
		case 8:
			return "Aug";
		case 9:
			return "Sep";
		case 10:
			return "Oct";
		case 11:
			return "Nov";
		case 12:
			return "Dec";
	}
};

/***************************
*
* ANJ_Cal, static calendar functions
*
***************************/

ANJ_Cal = function()
{
};

ANJ_Cal.showCal = function(event, which)
{
	ANJ_v.activeDate = which;
	ANJ_Cal.makeCal();
	var agt=navigator.userAgent.toLowerCase();
	var x, y;
	if ( agt.indexOf("msie") != -1 ) {
		x = event.clientX;
		y = event.clientY;
	}
	else {
		x = event.pageX;
		y = event.pageY;
	}
	ANJ_e.cal.style.top = (y ) + "px";
	ANJ_e.cal.style.left = (x-150) + "px";
	ANJ_e.cal.style.display = "block";
};

ANJ_Cal.makeCal = function()
{
	var d1 = new ANJ_Date();
	d1.setFromDate(ANJ_v.calDate.month, 1, ANJ_v.calDate.year); 

	ANJ_e.cal_month1.innerHTML = ANJ_Cal.getCalMonth(d1.month, d1.year);

	var d2 = new ANJ_Date();
	d2.setFromToday();
	d2.day = 1;

	if ( ANJ_Date.isLater(d1, d2) != 1 ) {
		ANJ_e.cal_prev.style.display = "none";
	}
	else {
		ANJ_e.cal_prev.style.display = "block";
	}

	d1.addMonths(1);
	
	ANJ_e.cal_month2.innerHTML = ANJ_Cal.getCalMonth(d1.month, d1.year);

	d1.addMonths(1);

	if ( ANJ_Date.isLater(d1, ANJ_v.lastBooking) != -1 ) {
		ANJ_e.cal_next.style.display = "none";
	}
	else {
		ANJ_e.cal_next.display = "block"; 
	}
};


ANJ_Cal.getCalMonth = function(month, year)
{
	var i, table = [];
	
	table.push( "<table cellpadding=\"0\" cellspacing=\"0\">" );
	table.push( "<tr><td colspan=\"7\" class=\"a\">" + ANJ_Date.getMonthString(month) + " " + year );
	table.push( "<tr><td class=\"b\">Su<td class=\"b\">M<td class=\"b\">Tu<td class=\"b\">W<td class=\"b\">Th<td class=\"b\">F<td class=\"b\">Sa" );

	var dow = ANJ_Date.getStartDay(month, year);
	table.push("<tr>");
	for (i=0; i<dow; i++) {
		table.push("<td>&nbsp;");
	}

	var dim = ANJ_Date.getDaysInMonth(month, year);
	for (i=1; i<=dim; i++)
	{
		var d = new ANJ_Date();
		d.setFromDate(month, i, year);
	 
		if ( ANJ_Date.isLater(d, ANJ_v.today) != -1 && ANJ_Date.isLater(ANJ_v.lastBooking, d) != -1 ) {
			table.push("<td><a href='javascript: void(0)' onclick='ANJ_Cal.selDate(" + month + "," + i + "," + year + ")'>" + i + "</a>");
		}
		else {
			table.push("<td>" + i);
		}
		dow++;
		if ( dow == 7 ) 
		{
			dow = 0;
			if ( i != dim ) {
				table.push("<tr>");
			}
		}
	}
	if ( dow !== 0 )
	{
		for (i=dow; i<7; i++) {
			table.push("<td>&nbsp;");
		}
	}
	table.push("</table>");
	return table.join('');
};

ANJ_Cal.doNextMonth = function()
{
	ANJ_v.calDate.addMonths(1);
	ANJ_Cal.makeCal();
};

ANJ_Cal.doPrevMonth = function()
{
	ANJ_v.calDate.addMonths(-1);
	ANJ_Cal.makeCal();
};

ANJ_Cal.selDate = function(m,d,y)
{
	var date = new ANJ_Date();
	date.setFromDate(m,d,y);
	if ( ANJ_v.activeDate == 1 ) {
		ANJ_F.setElementsToDate ( ANJ_e.depmonth, ANJ_e.depday, date );	
	}
	else {
		ANJ_F.setElementsToDate ( ANJ_e.retmonth, ANJ_e.retday, date );
	}
	ANJ_Cal.hideCal();
};

ANJ_Cal.hideCal = function()
{
	ANJ_e.cal.style.display = "none";
};

/***************************
*
* ANJ_LcCity
*
***************************/

ANJ_LcCity = function(line)
{
	var tabs = line.split("\t");
	this.id = tabs[0];
	this.code = tabs[1];
	this.place = tabs[2];
	this.region = tabs[3];
	this.regionabbv = tabs[4];
	this.country = tabs[5];
	this.airport = tabs[6];
	this.allairportscode = tabs[7];
	this.defaultcode = tabs[8];
	this.usespecific = false;

	this.makeToken = function()
	{
		var token = this.place;
		if ( this.country == "United States" || this.country == "Canada" || this.country == "Australia" ) {
			token += ", " + this.regionabbv;
		}
		if ( this.country != "United States" ) {
			token += ", " + this.country;
		}
		return token + " (" + this.code + ")";
	};
};

ANJ_LcCity.cmp = function(a, b)
{
	if ( a.country == "United States" || a.country == "Canada" || a.country == "Australia" )
	{
		if ( a.region < b.region ) {
			return -1;
		}
		if ( b.region < a.region ) {
			return 1;
		}
	}

	if ( a.place < b.place ) {
		return -1;
	}
	if ( b.place < a.place ) {
		return 1;
	}

	if ( a.airport == "All Airports" )
	{
		if ( b.airport == "All Airports" ) {
			return 0;
		} 
		else {
			return -1;
		}
	} 
	else {
		if ( b.airport == "All Airports") { 
			return 1;
		}
	}

	if ( a.airport < b.airport ) {
		return -1;
	}
	if ( b.airport < a.airport ) {
		return 1;
	}
	return 0;
};

/***************************
*
* ANJ_Airport
*
***************************/

ANJ_Airport = function(line)
{
	var tabs = line.split("\t");
	this.code = tabs[0];
	this.place = tabs[1];
	this.country = tabs[2];
	this.token = tabs[3];
	this.priority = parseInt(tabs[4],10);
	/* Match string might be the token, or maybe something else like "Mallorca" for "Palma de Mallorca" */
	this.matchString = ANJ_F.getMatchString( tabs[5] );
	this.cityid = tabs[6];
};


ANJ_Airport.cmp = function(a, b)
{
	if ( a.code == ANJ_a.input.value.toUpperCase() ) {
		return -1;
	}
	if ( b.code == ANJ_a.input.value.toUpperCase() ) {
		return 1;
	}
	if ( a.priority > b.priority ) {
		return -1;
	}
	if ( b.priority > a.priority ) {
		return 1;
	}
	if ( a.place < b.place ) {
		return -1;
	}
	if ( b.place < a.place ) {
		return 1;
	}
	if ( a.token < b.token ) {
		return -1;
	}
	if ( b.token < a.token ) {
		return 1;
	}
	return 0;
};

String.prototype.ANJ_trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
};

