var lastBooking;
var calDate;
var activeDate;
var today;
var searchNum = 0;

function BookmarkThis() 
{
	var url = document.location.href;
	var title = document.title;

	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	} else if( window.external ) { // IE Favorite
		window.external.AddFavorite( url, title); }
	else if(window.opera && window.print) { // Opera Hotlist
		return true; }
}

function bmHome()
{
	var url = "http://www.airninja.com";
	var title = "AirNinja.com - Fly Discount Airlines and Save!";

	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	} else if( window.external ) { // IE Favorite
		window.external.AddFavorite( url, title); }
	else if(window.opera && window.print) { // Opera Hotlist
		return true; }
}

function Delicious() 
{
	document.location.href = "http://del.icio.us/post?url=" + document.location.href + "&title=" + document.title;
}

function loadPage()
{
	preloadImages();
	
	today = new NinjaDate();
	today.setFromToday();

	lastBooking = new NinjaDate();
	lastBooking.setFromToday();
	lastBooking.addDays(330);

	fillMonths( document.getElementById("departingMonth") );
	fillMonths( document.getElementById("returningMonth") );

	fillDays(1);
	fillDays(2);


	var date1 = new NinjaDate();
	date1.setFromToday();
	date1.addDays(7); 
	date1.fillElements( 1 );

	var date2 = new NinjaDate();
	date2.setFromToday();
	date2.addDays(14);
	date2.fillElements( 2 );


	if ( document.getElementById("checkinMonth") != null )
	{
		fillMonths( document.getElementById("checkinMonth") );
		fillMonths( document.getElementById("checkoutMonth") );
		fillDays(3);
		fillDays(4);
		date1.fillElements( 3 );
		date2.fillElements( 4 );
 	}


	calDate = date1;

	if ( setDefaults == 2 || setDefaults == 3 )
		acFrom = new Airport(xCode + "\t\t\t" + xCountry + "\t\t0");
	if ( setDefaults == 1 || setDefaults == 3 )
		acTo = new Airport(yCode + "\t\t\t" + yCountry + "\t\t0");
}

function preloadImages()
{
	var i1 = new Image();
	i1.src = toRoot + "images_v3/bg-cal.png";
	var i2 = new Image();
	i2.src = toRoot + "images_v3/prev.png";
	var i3 = new Image();
	i3.src = toRoot + "images_v3/next.png";
	var i4 = new Image();
	i4.src = toRoot + "images_v3/x.png";
}

function fillMonths(sel)
{
	var t = new NinjaDate();
	t.setFromToday();
	t.day = 1;

	var count = 0;
	sel.length = 13;
	while ( isLater(t, lastBooking) < 0 )
	{
		sel.options[ count++ ] =  new Option( getMonthString(t.month) + " " + t.year, t.year + "-" + t.month );
		t.addMonths(1);
	}
	sel.length = count;
}

function fillDays(which)
{
	var x, y;
	if ( which == 1 )
	{
		x = document.getElementById("departingMonth");
		y = document.getElementById("departingDay");
	}
	else if ( which == 2)
	{
		x = document.getElementById("returningMonth");
		y = document.getElementById("returningDay");
	}
	else if ( which == 3)
	{
		x = document.getElementById("checkinMonth");
		y = document.getElementById("checkinDay");
	}
	else if ( which == 4)
	{
		x = document.getElementById("checkoutMonth");
		y = document.getElementById("checkoutDay");
	}


	var idx = y.selectedIndex;

	var d = new NinjaDate();
	d.setFromMonthString( x.options[ x.selectedIndex ].value );
	var count = getDaysInMonth( d.month, d.year );
	y.length = count;
	for ( var i=1; i<=count; i++ )
	{
		y.options[i-1] = new Option(i,i);
	}
	if ( idx >= -1 )
	{
		if ( idx >= y.options.length )
			idx = y.options.length - 1;
		y.selectedIndex = idx;
	}
	hideProviders();
}


function toggleReturnDay(on)
{
	var s = document.getElementById("returnTD").style;
	if ( on != 0 )
	{
		s.visibility = 'visible';
	}
	else
	{
		s.visibility = 'hidden';	
	}
	hideProviders();
}

function hideProviders()
{
	document.getElementById("frameHolder").style.display = "none";
	document.getElementById("providers").style.visibility = "hidden";
}

function changeTsType()
{
	var x = document.getElementById("flightSearch").style;
	var y = document.getElementById("hotelSearch").style;

	if ( document.getElementById("tsFlights").checked )
	{
		x.display = "block";
		y.display = "none";
	}
	else 
	{
		y.display = "block";
		x.display = "none";
	}
	hideProviders();
}

// Ninja Date Class 
function NinjaDate()
{
	this.makeLater = makeLater;
	this.addDays = addDays;
	this.setFromToday = setFromToday;
	this.setFromDate = setFromDate;
	this.setFromMonthString = setFromMonthString;
	this.setFromElements = setFromElements;
	this.addMonths = addMonths;
	this.fillElements = fillElements;
}

function fillElements( which )
{
	if ( which == 1 ) {
		sel1 = document.getElementById("departingMonth");
		sel2 = document.getElementById("departingDay");
	}
	else if ( which == 2 )
	{
		sel1 = document.getElementById("returningMonth");
		sel2 = document.getElementById("returningDay");
	}
	else if ( which == 3 )
	{
		sel1 = document.getElementById("checkinMonth");
		sel2 = document.getElementById("checkinDay");
	}
	else if ( which == 4 )
	{
		sel1 = document.getElementById("checkoutMonth");
		sel2 = document.getElementById("checkoutDay");
	}

	var ms = this.year + "-" + this.month;
	for ( var i=0; i<sel1.options.length; i++ )
	{
		if ( sel1.options[i].value == ms )
		{
			sel1.selectedIndex = i;
			break;
		}
	}
	fillDays ( which );
	sel2.selectedIndex = this.day-1;
}

function setFromToday()
{
	var today = new Date();
	this.year = today.getFullYear();
	this.month = today.getMonth() + 1;
	this.day = today.getDate();
}

function setFromDate(month, day, year)
{
	this.year = year;
	this.month = month;
	this.day = day;
}

function setFromMonthString(ms)
{
	var x = ms.split("-");
	this.year = parseInt( x[0] );
	this.month = parseInt( x[1] );
}

function setFromElements(selM, selD)
{
	var ms = selM.options[ selM.selectedIndex ].value;
	this.setFromMonthString(ms);
	this.day = parseInt(selD.options[ selD.selectedIndex ].value);
}

function addDays(num)
{
	var i;
	for (i=0; i<num; i++)
	{
		this.day++;
		if ( this.day > getDaysInMonth(this.month, this.year) )
		{
			this.day = 1;
			this.month++;
			if ( this.month > 12 )
			{
				this.month=1;
				this.year++;
			}
		}
	}
}

function addMonths(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--;
			}
		}
	}
}

function makeLater(date)
{
	if ( isLater(this, date) == 1)
	{
		date.year = this.year;
		date.month = this.month;
		date.day = this.day;
	}
}



// utility functions
function getStartDay(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
function isLater(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;
}


function getDaysInMonth(m, y)
{
	switch (m)
		{
	case 1:
		return 31;
	case 2:
		if ( y % 4 == 0 )
			return 29;
		else
			return 28;
	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;
	}
}

function getDisplayNum(n)
{
	if ( n < 10 )
		return "0" + n;
	else
		return "" + n;
}

function getMonthString(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";
	}
}


/***************************
*
* DRAWING THE CALENDAR
*
***************************/

function showCal(event, which)
{
	activeDate = which;
	makeCal();
	var dcal = document.getElementById("dcal");

	var agt=navigator.userAgent.toLowerCase();
	var y;
	var x;
	if ( agt.indexOf("msie") != -1 )
	{
		y = event.clientY;
		x = event.clientX;
	}
	else
	{
		y = event.pageY;
		x = event.pageX;
	}
	if ( tsStyle == 1 )
	{
		dcal.style.top = (y - 250) + "px";
		if ( which == 1 )
			dcal.style.left = (x-150) + "px";
		else
			dcal.style.left = (x-250) + "px";
	}
	else
	{
		dcal.style.top = (y - 200) + "px";
		dcal.style.left = (x-150) + "px";
	}
	dcal.style.display = "block";
}

function hideCal()
{
	var dcal = document.getElementById("dcal");
	dcal.style.display = "none";
}

function getCalMonth(month, year)
{
	var table = new Array();
	
	table.push( "<table cellpadding=\"0\" cellspacing=\"0\">" );
	table.push( "<tr><td colspan=\"7\" class=\"a\">" + 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 = getStartDay(month, year);
	table.push("<tr>");
	for (i=0; i<dow; i++)
		table.push("<td>&nbsp;");

	var dim = getDaysInMonth(month, year);
	for (i=1; i<=dim; i++)
	{
		var thisDate = new NinjaDate();
		thisDate.setFromDate(month, i, year);
	 
		if ( isLater(thisDate, today) != -1 && isLater(lastBooking, thisDate) != -1 )
			table.push("<td><a href='javascript: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('');
}

function makeCal()
{
	var month = calDate.month;
	var year = calDate.year;

	var d1 = new NinjaDate();
	d1.setFromDate(month, 1, year); 

	var table = getCalMonth(d1.month, d1.year);
	var calm1 = document.getElementById("month1");
	calm1.innerHTML = table;

	var d2 = new NinjaDate();
	d2.setFromToday();
	d2.day = 1;

	var oPrevMonth = document.getElementById("prev");
	if ( isLater(d1, d2) != 1 )
		oPrevMonth.style.display = "none";
	else
		oPrevMonth.style.display = "block";

	d1.addMonths(1);
	
	var table = getCalMonth(d1.month, d1.year);
	var calm2 = document.getElementById("month2");
	calm2.innerHTML = table;

	d1.addMonths(1);

	var oNextMonth = document.getElementById("next");
	if ( isLater(d1, lastBooking) != -1 )
		oNextMonth.style.display = "none";
	else
		oNextMonth.style.display = "block"; 
}

function doNextMonth()
{
	calDate.addMonths(1);
	makeCal();
}

function doPrevMonth()
{
	calDate.addMonths(-1);
	makeCal();
}

function selDate(m,d,y)
{
	var date = new NinjaDate();
	date.setFromDate(m,d,y);
	date.fillElements( activeDate );
	hideProviders();
	hideCal();
}

/*************************

AUTOCOMPLETE

**************************/

// Airport class 
function Airport(line)
{
	var tabs = line.split("\t");
	this.code = tabs[0];
	this.place = tabs[1];
	this.state = tabs[2];
	this.country = tabs[3];
	this.name = tabs[4];
	this.priority = parseInt(tabs[5]);
	
	if ( this.country == "USA" || this.country == "Canada" )
		this.token = this.place + ", " + this.state + " - " + this.name + " (" + this.code + ")";
	else
		this.token = this.place + ", " + this.country + " - " + this.name + " (" + this.code + ")";

	this.matchString = getMatchString( this.token );
}

function sortAirports(a, b)
{
	if ( a.code == acInput.value.toUpperCase() )
		return -1;
	if ( b.code == acInput.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;
}

var acInput = null;
var acSelector = null;
var acFirstTwo = "";
var acSet = null;
var acSubset = null;
var acSelection = -1;
var acWhich = -1;

var acFrom = null;
var acTo = null;

var acHttp;

function keyDown(e)
{
	if ( ccSelector != null )
	{
		if ( ccSelector.style.display == "block" )
		{
			ccKeyDown(e);
			return;
		}
	}

	if ( acSelector == null )
		return;

	if ( acSelector.style.display != "block" )
		return;

	var code = getKeyCode(e);

	switch (code)
	{
		case 9:
		case 13:
			confirmSelection(); 
			break;
		case 27:
			removeAutocomplete();
			break;
		case 38:
			moveSelectionUp();
			break;
		case 40:
			moveSelectionDown(); 
			break;
	}
}

function createRequestObject() {
	var ro;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer"){
		ro = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		ro = new XMLHttpRequest();
	}
	return ro;
}

function removeAutocomplete()
{
	if ( acSelector != null )
	{
		acSelector.innerHTML = "";
		acSelector.style.display = "none";
	}
	acFirstTwo = "";
	acSet = null;
	acSubset = null;
	acSelection = -1;
	acSelector = null;
	acInput = null;
	acWhich = -1;
}

function typing(which, event)
{
	var code = getKeyCode(event);

	if ( code == 40 || code == 38 || code == 9 || code == 13 || code == 27 )
		return;

	hideProviders();

	if ( which == 1 )
	{
		acInput = document.getElementById("searchinputFrom");
		acSelector = document.getElementById("selectorFrom");
		acFrom = null;
	}
	else
	{
		acInput = document.getElementById("searchinputTo");
		acSelector = document.getElementById("selectorTo");
		acTo = null;
	}
	acWhich = which;

	var w = acInput.value;
	if ( w.length < 2 )
	{
		acSelector.style.display = "none";
	}
	else
	{
		var ft = w.substr(0,2).toUpperCase();

		if ( ft != acFirstTwo )
		{
			acFirstTwo = ft;
			acHttp = createRequestObject();
			acHttp.open("get", toRoot + "script/airports/" + acFirstTwo + ".txt");
			acHttp.onreadystatechange = readAirports;
			acHttp.send(null);
		}
		else
			drawSelector();
	}
}

function getKeyCode(e)
{
	if (document.all)
		return window.event.keyCode;
	else return e.which;
}

function readAirports() {
	var response;

	if(acHttp.readyState == 4){
		response = acHttp.responseText;
	}
	else return;

	if ( acHttp.status == 404 )
	{
		response = "";
	}

	acMatchset = new Array();
	acSet = new Array();

	var lines = response.split("\n");
	for (i=0; i<lines.length; i++)
	{
		if ( lines[i].length <= 0 )
			continue;
		var a = new Airport(lines[i]);
		acSet.push( a );
	}

	drawSelector();
}

function getMatchString(s)
{
	var ret = s.toUpperCase();
	ret = ret.replace(/[^A-Z]/g, "");
	  return ret;
}

function drawSelector()
{
	if ( acSet == undefined )
		return;

	if ( acSet.length == 0 )
	{
		//document.getElementById("nocities").style.display = "block";
		return;
	}

	acSubset = new Array();
	w = getMatchString( acInput.value );

	for (i=0; i<acSet.length; i++)
	{
		var a = acSet[i];
		if ( acInput.value.toUpperCase() == a.code )
		{
			acSubset.push( a );
			continue;
		}
		if ( a.matchString.length < w.length )
			continue;
		if ( a.matchString.substr(0,w.length) == w )
		{
			acSubset.push( a );
		}
	}
	acSubset.sort ( sortAirports );

	/*if ( acSubset.length > 20  )
	{
		acSelector.style.display = "none";
		return;
	}*/

	if ( acSubset.length == 0 )
	{
		acSelector.style.display = "none";
		if ( w.length >= 3 )
		{ 
			//document.getElementById("nocities").style.display = "block";
		}
		return;
	}

	//document.getElementById("nocities").style.display = "none";
	
	var html = new Array();
	var i;
	for (i=0; i<acSubset.length && i<12; i++)
	{
		html.push ("<div class=\"row\" id=\"row" + i + "\" onclick=clickRow(" + i + ") onmouseover=selectRow(" + i + ")>" + acSubset[i].token + "</div>");
	}
	acSelector.innerHTML = html.join('');
	acSelector.style.display = "block";
	acSelection = 0;
	highlightCurrentSelection();
}

function selectRow(r)
{
	if ( r != acSelection )
	{
		eraseCurrentSelection();
		acSelection = r;
		highlightCurrentSelection();
	}
}

function clickRow(r)
{
	acSelection = r;
	confirmSelection();
}

function confirmSelection()
{
	if ( acSelection != -1 )
	{
		var a = acSubset[ acSelection ]
		acInput.value = a.token;
		if ( acWhich == 1 )
			acFrom = a;
		else
			acTo = a;
	}
	removeAutocomplete();
}


function eraseCurrentSelection()
{
	document.getElementById("row" + acSelection ).style.backgroundColor = "#fff";
	document.getElementById("row" + acSelection ).style.color = "#808080";
}

function highlightCurrentSelection()
{
	document.getElementById("row" + acSelection ).style.backgroundColor = "#0077ff";
	document.getElementById("row" + acSelection ).style.color = "#fff";
}

function moveSelectionDown()
{
	if ( acSelection < acSubset.length-1 && acSelection < 11 )
	{
		eraseCurrentSelection();
		acSelection++;
		highlightCurrentSelection();
	}
}

function moveSelectionUp()
{
	if ( acSelection > 0  )
	{
		eraseCurrentSelection();
		acSelection--;
		highlightCurrentSelection();
	}
}

/*************************

CITY COMPLETE
(for hotels, cars ... )

**************************/

// City class 
function City(line)
{
	var tabs = line.split("\t");
	this.city = tabs[0];
	this.region = tabs[1];
	this.country = tabs[2];
	this.regionCode = tabs[3];
	this.countryCode = tabs[4];
	this.priority = parseInt(tabs[5]);
	
	if ( this.country == "United States" || this.country == "Canada" )
		this.token = this.city + ", " + this.regionCode + ", " + this.country;
	else
		this.token = this.city + ", " + this.country;

	this.matchString = getMatchString( this.token );
}

function sortCities(a, b)
{
	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;
}


var ccInput = null;
var ccSelector = null;
var ccFirstTwo = "";
var ccSet = null;
var ccSubset = null;
var ccSelection = -1;
var ccHotel = null;
var ccHttp;

function ccKeyDown(e)
{
	var code = getKeyCode(e);

	switch (code)
	{
		case 9:
		case 13:
			ccConfirm(); 
			break;
		case 27:
			ccRemove();
			break;
		case 38:
			ccUp();
			break;
		case 40:
			ccDown();
			break;
	}
}

function ccRemove()
{
	if ( ccSelector != null )
	{
		ccSelector.innerHTML = "";
		ccSelector.style.display = "none";
	}
	ccFirstTwo = "";
	ccSet = null;
	ccSubset = null;
	ccSelection = -1;
	ccSelector = null;
	ccInput = null;
}

function ccTyping(event)
{
	var code = getKeyCode(event);

	if ( code == 40 || code == 38 || code == 9 || code == 13 || code == 27 )
		return;

	hideProviders();

	ccInput = document.getElementById("searchinputHotel");
	ccSelector = document.getElementById("selectorHotel");
	ccHotel = null;

	var w = ccInput.value;
	if ( w.length < 2 )
	{
		ccSelector.style.display = "none";
	}
	else
	{
		var ft = w.substr(0,2).toUpperCase();

		if ( ft != ccFirstTwo )
		{
			ccFirstTwo = ft;
			ccHttp = createRequestObject();
			ccHttp.open("get", toRoot + "script/cities/" + ccFirstTwo + ".txt");
			ccHttp.onreadystatechange = readCities;
			ccHttp.send(null);
		}
		else
			ccDrawSelector();
	}
}


function readCities() {
	var response;

	if(ccHttp.readyState == 4){
		response = ccHttp.responseText;
	}
	else return;

	if ( ccHttp.status == 404 )
	{
		response = "";
	}

	ccMatchset = new Array();
	ccSet = new Array();

	var lines = response.split("\n");
	for (i=0; i<lines.length; i++)
	{
		if ( lines[i].length <= 0 )
			continue;
		var c = new City(lines[i]);
		ccSet.push( c );
	}

	ccDrawSelector();
}

function ccDrawSelector()
{
	if ( ccSet == undefined )
		return;

	if ( ccSet.length == 0 )
		return;

	ccSubset = new Array();
	w = getMatchString( ccInput.value );

	for (i=0; i<ccSet.length; i++)
	{
		var c = ccSet[i];
		if ( c.matchString.length < w.length )
			continue;
		if ( c.matchString.substr(0,w.length) == w )
		{
			ccSubset.push( c );
		}
	}
	ccSubset.sort ( sortCities );

	if ( ccSubset.length == 0 )
	{
		ccSelector.style.display = "none";
		return;
	}

	var html = new Array();
	var i;
	for (i=0; i<ccSubset.length && i<12; i++)
	{
		html.push ("<div class=\"row\" id=\"ccRow" + i + "\" onclick=ccClick(" + i + ") onmouseover=ccSelect(" + i + ")>" + ccSubset[i].token + "</div>");
	}
	ccSelector.innerHTML = html.join('');
	ccSelector.style.display = "block";
	ccSelection = 0;
	ccHighlight();
}

function ccSelect(r)
{
	if ( r != ccSelection )
	{
		ccErase();
		ccSelection = r;
		ccHighlight();
	}
}

function ccClick(r)
{
	ccSelection = r;
	ccConfirm();
}

function ccConfirm()
{
	if ( ccSelection != -1 )
	{
		var c = ccSubset[ ccSelection ];
		ccInput.value = c.token;
		ccHotel = c;
	}
	ccRemove();
}


function ccErase()
{
	document.getElementById("ccRow" + ccSelection ).style.backgroundColor = "#fff";
	document.getElementById("ccRow" + ccSelection ).style.color = "#808080";
}

function ccHighlight()
{
	document.getElementById("ccRow" + ccSelection ).style.backgroundColor = "#426FD9";
	document.getElementById("ccRow" + ccSelection ).style.color = "#fff";
}

function ccDown()
{
	if ( ccSelection < ccSubset.length-1 && ccSelection < 11 )
	{
		ccErase();
		ccSelection++;
		ccHighlight();
	}
}

function ccUp()
{
	if ( ccSelection > 0  )
	{
		ccErase();
		ccSelection--;
		ccHighlight();
	}
}



/**************************
*
* SEARCH FLIGHTS 
*
**************************/

function getProviders()
{
	var d1 = new NinjaDate();
	var d2 = new NinjaDate();

	d1.setFromElements( document.getElementById("departingMonth"), document.getElementById("departingDay") );
	d2.setFromElements( document.getElementById("returningMonth"), document.getElementById("returningDay") );

	if ( isLater(today, d1) == 1 )
	{
		alert( "Departure date: Please enter a date that is today or later.");  
		return;
	}

	if ( document.getElementById("searchinputFrom").value == "" )
	{
		alert("Please enter the city or airport code you are leaving from.");
		return;
	}
		
	if ( document.getElementById("searchinputTo").value == "" )
	{
		alert("Please enter the city or airport code you are going to.");
		return;
	}

	var from;
	var fromCountry;
	if ( acFrom == null )
	{
		from = document.getElementById("searchinputFrom").value;
		fromCountry = "";
	}
	else
	{
		from = acFrom.code;
		fromCountry = acFrom.country;
	}

	var to;
	var toCountry;
	if ( acTo == null )
	{
		to = document.getElementById("searchinputTo").value;
		toCountry = "";
	}
	else
	{
		to = acTo.code;
		toCountry = acTo.country;
	}

	if ( document.getElementById("ft2").checked )
	{
		if ( isLater(d1, d2) > 0  )
		{
			alert("Your return date must be on or after your departure date.");
			return;
		}
		if ( isLater(d2, lastBooking) > 0 )
		{
			alert("Return Date: Sorry, reservations are not accepted past " + lastBooking.month + "/" + lastBooking.day + "/" + lastBooking.year );
			return;
		}
	}
	else
	{
		if ( isLater(d1, lastBooking) > 0)
		{
			alert("Departure Date: Sorry, reservations are not accepted past " + lastBooking.month + "/" + lastBooking.day + "/" + lastBooking.year );
			return;
		}
	}

	var d1s = d1.month + "/" + d1.day + "/" + d1.year;
	var d2s = d2.month + "/" + d2.day + "/" + d2.year;

	document.getElementById("providers").style.visibility = "visible";
	document.getElementById("loading").style.display = "block";
	document.getElementById("frameHolder").style.display = "none";

	var url = toRoot + "providers_v3.php?depcode=" + escape(from) + "&depcountry=" + escape(fromCountry) + "&arrcode=" + escape(to) + "&arrcountry=" + escape(toCountry) + "&depart=" + escape( d1s ) + "&arrive=" + escape( d2s ) + "&travelers=" + document.getElementById("travelers").value;
	if ( document.getElementById("ft1").checked )
		url += "&flighttype=1";
	else
		url += "&flighttype=2";

	document.getElementById("providersFrame").src = url;
}

function getHotelProviders()
{
	var d1 = new NinjaDate();
	var d2 = new NinjaDate();

	d1.setFromElements( document.getElementById("checkinMonth"), document.getElementById("checkinDay") );
	d2.setFromElements( document.getElementById("checkoutMonth"), document.getElementById("checkoutDay") );

	if ( isLater(today, d1) == 1 )
	{
		alert( "Check in date: Please enter a date that is today or later.");  
		return;
	}

	if ( document.getElementById("searchinputHotel").value == "" )
	{
		alert("Please enter the city where you would like to stay.");
		return;
	}
	
	var city, region, country, regionCode, countryCode;
	if ( ccHotel != null )
	{
		city = ccHotel.city;
		region = ccHotel.region;
		country = ccHotel.country;
		regionCode = ccHotel.regionCode;
		countryCode = ccHotel.countryCode;
	}
	else
	{
		city = document.getElementById("searchinputHotel").value;
		region = "";
		country = "";
		regionCode = "";
		countryCode = "";
	}

	if ( isLater(d1, d2) > 0  )
	{
		alert("Your check in date must be on or after your check out date.");
		return;
	}
	if ( isLater(d2, lastBooking) > 0 )
	{
		alert("Sorry, reservations are not accepted past " + lastBooking.month + "/" + lastBooking.day + "/" + lastBooking.year );
		return;
	}

	var nR = document.getElementById("numRooms").value;
	var nG = document.getElementById("numGuests").value;

	if ( nR > nG )
	{
		alert("You must have at least one guest per room.");
		return;
	}

	var d1s = d1.month + "/" + d1.day + "/" + d1.year;
	var d2s = d2.month + "/" + d2.day + "/" + d2.year;

	document.getElementById("providers").style.visibility = "visible";
	document.getElementById("loading").style.display = "block";
	document.getElementById("frameHolder").style.display = "none";

	var url = toRoot + "hotels_v3.php?city=" + escape(city) + "&region=" + escape(region) + "&country=" + escape(country) + "&regionCode=" + escape(regionCode) + "&countryCode=" + escape(countryCode) + "&checkin=" + escape( d1s ) + "&checkout=" + escape( d2s ) + "&rooms=" + nR + "&guests=" + nG;

	document.getElementById("providersFrame").src = url;
}

function providersFrameLoaded()
{
	document.getElementById("loading").style.display = "none";
	document.getElementById("frameHolder").style.display = "block";
	document.getElementById("providersFrame").height = document.getElementById("providersFrame").contentWindow.document.body.scrollHeight + 20;
}

function anSearch(engine)
{
	var f = document.getElementById("anForm");
	f.engine.value = engine;
	f.submit();
}

function hotelSearch(engine)
{
	var f = document.getElementById("hnForm");
	f.engine.value = engine;

	if ( engine == "hotelsdotcom" && f.numRooms > 1 )
	{
		alert("Note: You may need to adjust the number of people in each room when you reach Hotels.com.");
	}

	if ( engine == "expedia" )
	{
		if ( f.numRooms > 3)
		{
			alert("Expedia supports a maximum of 3 rooms.");
			return;
		}
		if ( f.numRooms > 1)
		{
			alert("Note: You may need to adjust the number of people in each room when you reach Expedia.");
		}
	}
	if ( engine == "hotwire" )
	{
		if ( f.numRooms > 4)
		{
			alert("Hotwire supports a maximum of 4 rooms.");
			return;
		}
	}
	if ( engine == "travelocity" )
	{
		if ( f.numRooms > 4)
		{
			alert("Travelocity supports a maximum of 4 rooms.");
			return;
		}
		if ( f.numRooms > 1)
		{
			alert("Note: You may need to adjust the number of people in each room when you reach Travelocity.");
		}
	}

	f.submit();
}

function trim(str)
{
	return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

function mlJoin()
{
	var em = document.getElementById("mlEmail").value;
	var ct = document.getElementById("mlCity").value;

	if ( trim(em) == "" )
	{
		alert("Please enter an e-mail address.");
		return;
	}
	if ( em.length > 255 )
	{
		alert("We have a 255 character limit for e-mail addresses.");
		return;
	}
	if ( ct.lenth > 255 )
	{
		alert("We have a 255 character limit for home cities.");
		return;
	}

	mlHttp = createRequestObject();
	mlHttp.open("post", "/join_v3.php", true);
	var params = "em=" + escape(em) + "&ct=" + escape(ct);
	mlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	mlHttp.setRequestHeader("Content-length", params.length);
	mlHttp.setRequestHeader("Connection", "close");
	mlHttp.onreadystatechange = function() {
			var response;
			if( mlHttp.readyState == 4){
				response = mlHttp.responseText;
			}
			else return;
			alert( response );		
	}
	mlHttp.send(params);
}

function submitComment()
{
	var x = document.getElementById("commentText").value;
	x = trim(x);
	if ( x == "" )
	{
		alert("Please enter a comment.");
		return false;
	}
	var url = document.location.href;
	url = url.replace("http://localhost:82", "");
	url = url.replace("http://www.airninja.com", "");
	url = url.replace("http://airninja.com", "");
	url = url.replace("#comments", "");
	document.getElementById("commentUrl").value = url;
	return true;
}