/*
-------------------------------------------------------------------------------------------
				 WATSON BUSINESS RULES FUNCTIONS
-------------------------------------------------------------------------------------------
*/

//------------------------------------
// default the checkin date to today's date
function setDateToToday(isDefaultDates,defaultValue)
{
	if (startCIDate == '' && isDefaultDates == false){
		f.ciDate.value = 'MM/DD/YYYY';
		f.coDate.value = 'MM/DD/YYYY';
		f.arrivalDate.value = '';
		f.departureDate.value = '';
	} else if (startCIDate == '' && isDefaultDates == true) {
		function lZero(nr) {if (nr < 10) nr = "0" + nr;return nr;}
	
		var today = new Date();
		var month = today.getMonth() + 1;
		var day = today.getDate();
		var year = today.getYear() + "";

		f.ciDate.value = lZero(month) + "/" +  lZero(day) + "/200" + year.charAt(year.length-1);
		f.coDate.value = lZero(month) + "/" +  lZero(day + 1) + "/200" + year.charAt(year.length-1);
		
		var checkIn = new validDate(f.ciDate);
		
		if ( checkIn.valid ) {
			checkIn.setField();
			f.arrivalDate.value = f.ciDate.value;
			
			var checkInAlt = new validDate(f.arrivalDate);
			checkInAlt.setAltField(checkInAlt.d);
	
			var checkOut = validDate(f.coDate);
			
			if ( (! (checkOut.valid)) || (checkOut.d.getTime() < checkIn.d.getTime())) {
				checkOut.setD(checkIn.d);
				checkOut.nextDay();
				checkOut.setField();
				
				f.departureDate.value = f.coDate.value;
				
				var checkOutAlt = validDate(f.departureDate);
				checkOutAlt.setAltField(checkOutAlt.d);
			}
		}		
		
	} else {
		f.ciDate.value = startCIDate;
		f.coDate.value = startCODate;
		f.arrivalDate.value = startCIDateBE;
		f.departureDate.value = startCODateBE;
	}
}
//-------------------------------------
// methods for changing state/country
function setOnChange(f, stateField ){
	if( f.country == null || stateField == null )
		return;
	stateField.onchange = function() { setCountry(f,stateField); }
	f.country.onchange = function() { clearStateProv( stateField,f); }
}

function isTopCity(f){
	var tempCity = f.city.value.toLowerCase();
	if (isIn(tempCity,topCities,true)){
		var tempArray = topCities[arrayIndex].split(":");
		var tempState = tempArray[1];
		var tempCountry = tempArray[2];
		setStateValue(tempState,f);
		setCountryValue(tempCountry,f);
		if(tempCountry == 'US'){
			f.rateRange.disabled = false;
		} else {
			f.rateRange.disabled = true;
		}
	}
}

function setCountry( f, stateProv ){
	if( stateProv == null || f.country == null)
		return;
	var state = stateProv.options[stateProv.selectedIndex].value;
	if(  isIn(state, us_states) || isIn(state, misc) ){
		setCountryValue('US',f);
		f.rateRange.disabled = false;
	}else if(  isIn(state, ca_prov) ){
		setCountryValue('CA',f);
		f.rateRange.selectedIndex = '';
		f.rateRange.disabled = true;
	}else{
		setCountryValue('',f);
		f.rateRange.selectedIndex = '';
		f.rateRange.disabled = true;
	}
}

function setCountryToUS(f,flag){
	if (f.postalCode.value !='' && f.postalCode.value != null){
		if ((f.address.value != '' && f.address.value != null)){
			setCountryValue('US',f);
			f.rateRange.disabled = false;
		}
	}
}

function setCountryValue (val,f){
	if(f.country == null)
		return;
	if( f.country.options == null){
		f.country.value = val;
	}else{
		for( var n =0; n < f.country.options.length; n++)
			if( f.country.options[n].value == val ){
				f.country.selectedIndex = n;
			break;
		}
	}
}

function setStateValue (val,f){
	if(f.stateProvince == null)
		return;
	if( f.stateProvince.options == null){
		f.stateProvince.value = val;
	}else{
		for( var n =0; n < f.stateProvince.options.length; n++)
			if( f.stateProvince.options[n].value == val ){
				f.stateProvince.selectedIndex = n;
			break;
		}
	}
}

function clearStateProv(stateField,f) {
	if(f.country == null || f.country.options == null || stateField == null)
		return;
	var cntry = f.country.options[ f.country.selectedIndex ].value ;
	if(cntry != 'US' && cntry != 'CA'){
		f.rateRange.selectedIndex = '';
		f.rateRange.disabled = true;
		stateField.selectedIndex = 0;
	} else if(cntry == 'US'){
		f.rateRange.disabled = false;
	} else if(cntry == 'CA'){
		f.rateRange.selectedIndex = '';
		f.rateRange.disabled = true;
	}
}

function switchSearchType(elementValue) {
	if (elementValue == 'address'){
		hideShowDiv('divAddressContainer','show');
		hideShowDiv('divZipContainer','show');
		f.city.style.width = '65px';
		isCitySearch =false;
	} else {
		hideShowDiv('divAddressContainer','hide');
		hideShowDiv('divZipContainer','hide');
		f.city.style.width = '150px';
		isCitySearch =true;
	}
}

function setVariable(type,switchType) {
	if (type == 'search' && switchType == true) {
		isSearch = true;
	} else {
		isSearch = false;
	}
}

function zeroOutState(){
	f.stateProvince.selectedIndex = 0;
}

function zeroOutCountry(){
	f.country.selectedIndex = 0;
}

function pause() {
	return okForm(f,successPath);
}

function ciDate_onBlur(defaultValue) {
	var checkIn = new validDate(f.ciDate);
	
	if ( checkIn.valid ) {
		checkIn.setField();
		f.arrivalDate.value = f.ciDate.value;
		
		var checkInAlt = new validDate(f.arrivalDate);
		checkInAlt.setAltField(checkInAlt.d);

		var checkOut = new validDate(f.coDate);
		if ( !(checkOut.valid) || (checkOut.d.getTime() < checkIn.d.getTime())) {
			
			checkOut.setD(checkIn.d);
			checkOut.nextDay();
			checkOut.setField();
			
			f.departureDate.value = f.coDate.value;
			
			var checkOutAlt = validDate(f.departureDate);
			checkOutAlt.setAltField(checkOutAlt.d);
		}
	} else {
		f.ciDate.value = defaultValue;
		f.arrivalDate.value = '';
	}
}

function coDate_onBlur(defaultValue) {
	var checkOut = new validDate(f.coDate);
	if ( checkOut.valid ) {
		checkOut.setField();
		
		f.departureDate.value = f.coDate.value;
		
		var checkOutAlt = validDate(f.departureDate);
		checkOutAlt.setAltField(checkOutAlt.d);
	} else {
		f.coDate.value = defaultValue;
		f.departureDate.value = '';
	}
}