
function preselect(selectObject, value) {
	for (i = 0; i < selectObject.options.length; i++) {
		if (selectObject.options[i].value.toUpperCase() == value.toUpperCase()) {
			selectObject.selectedIndex = i;
			break;
		}
	}
}

function resetSelect(selectObject) {
	selectObject.selectedIndex = 0;
}

function swapCountry(countrySelectObject, selectedState) {
	var usaStates = new Array("AL","AK","AZ","AR","CA","CO","CT","DE","DC","FL","GA","ID","IL","IN",
					"IA","KS","KY","LA","ME","MD","MA","MI","MN","MS","MO","MT","NE","NV",
					"NH","NJ","NM","NY","NC","ND","OH","OK","OR","PA","PR","RI","SC","SD","TN",
					"TX","UT","VT","VA","WA","WV","WI","WY");
	var canadaStates = new Array("AB","BC","MB","NB","NF","NT","NS","ON","PE","QC","SK");

	var usa = false;
	var canada = false;

	for (i = 0; i < usaStates.length; i++) {
		if (selectedState.toUpperCase() == usaStates[i].toUpperCase()) {
			usa = true;
			break;
		}
	}
	for (i = 0; i < canadaStates.length; i++) {
		if (selectedState.toUpperCase() == canadaStates[i].toUpperCase()) {
			canada = true;
			break;
		}
	}

	if (usa) {
		preselect(countrySelectObject, "US");
	}
	if (canada) {
		preselect(countrySelectObject, "CA");
	}
}


function swapState(stateSelectObject, selectedCountry) {
	if (selectedCountry.toUpperCase() != "US" && selectedCountry.toUpperCase() != "CA") {
		preselect(stateSelectObject, "NA");
	} else {
		resetSelect(stateSelectObject);
        }
}

	function onS() {
		formState.countryName.value=formState.country.options[formState.country.selectedIndex].text;
		if (formState.state.selectedIndex != 0) {
			formState.stateName.value=formState.state.options[formState.state.selectedIndex].text;
		}
		else {
			formState.stateName.value = "";
		}
		formState.checkInDate.value = formState.checkInDate.value; //formState.month1.value + "/" + formState.day1.value;
		formState.checkOutDate.value = formState.checkOutDate.value; //formState.month2.value + "/" + formState.day2.value;
	}

function gotoAdvancedSearch()
{
  document.forms['theSecondForm'].areaType.value=document.forms['formState'].areaType.value;
  document.forms['theSecondForm'].rateName.value=document.forms['formState'].rateName.value;
  document.forms['theSecondForm'].destination.value=document.forms['formState'].destination.value;
  document.forms['theSecondForm'].stateName.value=document.forms['formState'].stateName.value;
  document.forms['theSecondForm'].countryName.value=document.forms['formState'].countryName.value;
 // document.forms['theSecondForm'].city.value=document.forms['formState'].city.value;
  document.forms['theSecondForm'].checkInDate.value=document.forms['formState'].checkInDate.value;
  document.forms['theSecondForm'].checkOutDate.value=document.forms['formState'].checkOutDate.value;
  document.forms['theSecondForm'].numberAdults.value=document.forms['formState'].numberAdults.value;
  document.forms['theSecondForm'].numberChildren.value=document.forms['formState'].numberChildren.value;
  document.forms['theSecondForm'].numberRooms.value=document.forms['formState'].numberRooms.value;
//make sure dates are in ls format
  document.theSecondForm.submit();
}

function inputFocus(that,txt) {
	if(that.value==txt){
		that.value='';
	}
}
function inputBlur(that,txt) {
	if(that.value.length==0){
		that.value=txt;
	}
}

/* Search Form Date Functions */
	/*This function passes the date forward to the check out from the check in*/
	function dateForward(formName,inField,outField,lang)
		{
		/*make sure all the vars exist*/
		if(!lang)
			{lang = 'en_us';}
		else
			{
			if(lang == "")
				{lang = 'en_us';}
			else
				{lang = lang.toLowerCase();}
			}

		/*setup the vars*/
		var pushDate			=	new Date();
		var checkInObj			=	eval("document." + formName + "." + inField);
		var checkOutObj			=	eval("document." + formName + "." + outField);
		/*load the base dates as strings*/
		checkInDate_s			=	checkInObj.value;
		checkOutDate_s			=	checkOutObj.value;
		
		/*split the dates into arrays*/
		/*if (lang=="de_de")
			{
			inSplit 				=	checkInDate_s.split(".");
			outSplit				=	checkOutDate_s.split(".");
			}
		else if (lang=="zh_cn") 
			{
			inSplit 				=	checkInDate_s.split("-");
			outSplit				=	checkOutDate_s.split("-");
			}
		else*/ 
			/*{*/
			inSplit 				=	checkInDate_s.split("/");
			outSplit				=	checkOutDate_s.split("/");
			/*}*/
		/*Turn the string dates into objects*/
		checkInDate_o			=	createLSdateObj(inSplit,lang);
		checkOutDate_o			=	createLSdateObj(outSplit,lang);
		/*calculate the forward date (add 1 day)*/
		pushDate = checkInDate_o;
		pushDate.setDate(pushDate.getDate()+(1*1));

		//new - handle wrong date formats by not creating checkout date
		theInputDate 		= 	createLSdateObj(inSplit,lang);
		theInputDate 		= 	createLSdateString(theInputDate,lang);
		//if the form input (corrected with 0s if need be) matches the processed date, write checkout date
/*
		//requires a date object
		var showMonth	=	"";
		var showDay		=	"";
		showMonth = (dateObj.getMonth() + (1*1)).toString();
		showDay = dateObj.getDate().toString();

*/
		if (theInputDate == checkInDate_s){
			/*send the new date to the check out field*/
			checkOutObj.value 		= 	createLSdateString(pushDate,lang);
			}
		}
		
	/*This function overwrites given form fields to insure a US dateformat is passed to ALG	*/
	// this needs to be altered to change any dateFormat into dd/mm/yy for non-us sites. 
	//call it like this: usDateFormat('formState','checkInDate','EN_US'); usDateFormat('formState','checkOutDate','EN_US'); 
	function usDateFormat(formName,inField,lang)
		{
		/*make sure all the vars exist*/
		if(!lang)
			{lang = 'en_us';}
		else
			{
			if(lang == "")
				{lang = 'en_us';}
			else
				{lang = lang.toLowerCase();}
			}
		/*setup the vars*/
		var pushDate			=	new Date();
		var checkInObj			=	eval("document." + formName + "." + inField);
		//var checkOutObj			=	eval("document." + formName + "." + outField);
		/*load the base dates as strings*/
		checkInDate_s			=	checkInObj.value;
		//checkOutDate_s			=	checkOutObj.value;
		/*split the dates into arrays*/
		inSplit 				=	checkInDate_s.split("/");
		//outSplit				=	checkOutDate_s.split("/");
		/*Turn the string dates into objects*/
		checkInDate_o			=	createLSdateObj(inSplit,lang);
		//checkOutDate_o			=	createLSdateObj(outSplit,lang);
		/*send the new date to the check out field*/
		pushInDate = checkInDate_o;
		//pushOutDate = checkOutDate_o;
		pushInDate.setDate(pushInDate.getDate());
		//pushOutDate.setDate(pushOutDate.getDate());
		checkInObj.value 		= 	createLSdateString(pushInDate,'en_us');
		//checkOutObj.value 		= 	createLSdateString(pushOutDate,'en_us');
		}

	/*This function creates the date object based on lang*/
	function createLSdateObj(inSplit,lang)
		{
		/* new Date(year, month, day, hours, minutes, seconds, milliseconds) */
		var dateObj = new Date();
		if(lang == "en_us")// m/d/yy
			{
			if(inSplit.length == 3)
				{
				dateObj = new Date(20 + inSplit[2],inSplit[0] - (1*1),inSplit[1]);
				}
			else if (inSplit.length == 2)
				{
				thisYear = new Date().getFullYear();
				dateObj = new Date(thisYear,inSplit[0] - (1*1),inSplit[1]);
				}
			}
		else // dd/mm/yy
			{
			if(inSplit.length == 3)
				{
				dateObj = new Date(20 + inSplit[2],inSplit[1] - (1*1),inSplit[0]);
				}
			else if (inSplit.length == 2)
				{
				thisYear = new Date().getFullYear();
				dateObj = new Date(thisYear,inSplit[1] - (1*1),inSplit[0]);
				}
			}	
		/*else if(lang == "en_gb")// dd/mm/yy
			{
			if(inSplit.length == 3)
				{
				dateObj = new Date(20 + inSplit[2],inSplit[1] - (1*1),inSplit[0]);
				}
			else if (inSplit.length == 2)
				{
				thisYear = new Date().getFullYear();
				dateObj = new Date(thisYear,inSplit[1] - (1*1),inSplit[0]);
				}
			}
		else if(lang == "de_de")// dd.mm.yy
			{
			if(inSplit.length == 3)
				{
				dateObj = new Date(20 + inSplit[2],inSplit[1] - (1*1),inSplit[0]);
				}
			else if (inSplit.length == 2)
				{
				thisYear = new Date().getFullYear();
				dateObj = new Date(thisYear,inSplit[1] - (1*1),inSplit[0]);
				}
			}
		else if(lang == "zh_cn")// yy-m-d
			{
			if(inSplit.length == 3)
				{
				dateObj = new Date(20 + inSplit[0],inSplit[1] - (1*1),inSplit[2]);
				}
			else if (inSplit.length == 2)
				{
				thisYear = new Date().getFullYear();
				dateObj = new Date(thisYear,inSplit[0] - (1*1),inSplit[1]);
				}
			}*/
		return dateObj;
		}
		
/*
if(lang == "en_us")//mm.dd.yy
	{
	if(inSplit.length == 3)
		{
		dateObj = new Date(20 + inSplit[2],inSplit[0] - (1*1),inSplit[1]);
		}
	else if (inSplit.length == 2)
		{
		thisYear = new Date().getFullYear();
		dateObj = new Date(thisYear,inSplit[0] - (1*1),inSplit[1]);
		}
	}
else if(lang == "en_gb")//dd.mm.yy
	{
	if(inSplit.length == 3)
		{
		dateObj = new Date(20 + inSplit[2],inSplit[1] - (1*1),inSplit[0]);
		}
	else if (inSplit.length == 2)
		{
		thisYear = new Date().getFullYear();
		dateObj = new Date(thisYear,inSplit[1] - (1*1),inSplit[0]);
		}
	}
*/		
		
	/*This function creates the formatted string based on lang*/
	function createLSdateString(dateObj,lang)
		{
		var dateString 	= 	"";
		var shortYear	=	"";
		var showMonth	=	"";
		var showDay		=	"";
		/*Parse the object down to strings*/
		shortYear = dateObj.getFullYear().toString().substring(2,4);
		showMonth = (dateObj.getMonth() + (1*1)).toString();
		showDay = dateObj.getDate().toString();
		
		/*check which lang and run the appropriate string function*/
		if(lang == "en_us")// m/d/yy
			{dateString	= 	showMonth + "/" + showDay + "/"  + shortYear;}
		else // dd/mm/yy
			{
			if(showMonth < 10)
				{showMonth = 0 + showMonth;}
			if(showDay < 10)
				{showDay = 0 + showDay;}
			dateString	= 	showDay + "/" + showMonth + "/"  + shortYear;
			}
		/*else if (lang == "de_de")// dd.mm.yy
		{
			if(showMonth < 10)
				{showMonth = 0 + showMonth;}
			if(showDay < 10)
				{showDay = 0 + showDay;}
			dateString	= 	showDay + "." + showMonth + "."  + shortYear;
		}
		else if (lang == "zh_cn")// yy-m-d
			{dateString	= 	shortYear + "-" + showMonth + "-" + showDay;}
			*/
		return dateString;
		}

/*
if(lang == "en_us")
	{dateString	= 	showMonth + "/" + showDay + "/"  + shortYear;}
else if(lang == "en_gb")
	{
	if(showMonth < 10)
		{showMonth = 0 + showMonth;}
	if(showDay < 10)
		{showDay = 0 + showDay;}
	dateString	= 	showDay + "/" + showMonth + "/"  + shortYear;}
return dateString;
}
*/		