// res_property0011.js
// version 2.0

function colorMissingFields(fdName)
	{
	  var path = eval("document.forms['formState']." +fdName+ ".style");
	  path.backgroundColor = '#FFFFCC';
	  path.borderColor = '#D90019';
	}

function validateByRequestNumber()
	{
	
	if(document.forms['formState'].ByReqNum.value.length <= 0 && document.forms['formState'].LastName.value.length > 0)
	 {
	    alert("The following information was missing or incorrect:\n" +"* Wyndham ByRequest Member Number");
        colorMissingFields("ByReqNum");
        return false;
	  }
	  return true;
}

function validateLastName()
	{
	
	if(document.forms['formState'].ByReqNum.value.length > 0 && document.forms['formState'].LastName.value.length <= 0)
	 {
	    alert("The following information was missing or incorrect:\n" +"* Last Name");
        colorMissingFields("LastName");
        return false;
	  }
	  return true;
}


function changeCountry() 
	{
	var canadaIndex;
	var mexicoIndex;
	var usIndex;
	var puertoIndex;
	var caymanIndex;
	for (var i=0; i < document.forms['formState'].country.length; i++) 
		{
		if (document.forms['formState'].country.options[i].value == "Canada")
			{
			canadaIndex = i;
			continue;
            }
		if(document.forms['formState'].country.options[i].value == "Mexico")
            {
			mexicoIndex = i;
			continue;
            }
		if(document.forms['formState'].country.options[i].value == "United States")
            {
			usIndex = i;
			continue;
            }
		if(document.forms['formState'].country.options[i].value == "Puerto Rico")
			{
			puertoIndex = i;
			continue;
            }
		if(document.forms['formState'].country.options[i].value == "Cayman Islands")
            {
			caymanIndex = i;
			continue;
            }
        }
	if((document.forms['formState'].state.options[document.forms['formState'].state.selectedIndex].value =="Ontario") || (document.forms['formState'].state.options[document.forms['formState'].state.selectedIndex].value =="Quebec")|| (document.forms['formState'].state.options[document.forms['formState'].state.selectedIndex].value =="Alberta")||(document.forms['formState'].state.options[document.forms['formState'].state.selectedIndex].value =="Saskatchewan")) 
		{
		document.forms['formState'].country.selectedIndex = canadaIndex ;
		} 
	else if( (document.forms['formState'].state.options[document.forms['formState'].state.selectedIndex].value =="Colima") || (document.forms['formState'].state.options[document.forms['formState'].state.selectedIndex].value =="Nayarit") || (document.forms['formState'].state.options[document.forms['formState'].state.selectedIndex].value =="Quintana Roo") )
		{
		document.forms['formState'].country.selectedIndex = mexicoIndex ;
		}
	else if(document.forms['formState'].state.options[document.forms['formState'].state.selectedIndex].value =="Puerto Rico") 
		{
		document.forms['formState'].country.selectedIndex = puertoIndex ;
		}
	else if(document.forms['formState'].state.options[document.forms['formState'].state.selectedIndex].value =="Cayman Islands-British West Indies") 
		{
		document.forms['formState'].country.selectedIndex = caymanIndex ;
		}
	else 
		{
		document.forms['formState'].country.selectedIndex = usIndex ;
		}
    }

function reskinAdapter(){
	
	var arrivalDate 	    = document.formState.home_resv_arrival.value;
	var departureDate    = document.formState.home_resv_departure.value;
	var arrivalDateSec   = Date.parse(arrivalDate);
	var departureDateSec = Date.parse(departureDate);
	var arrDateFmt = new Date(arrivalDateSec);
	var depDateFmt = new Date(departureDateSec);
	if(isDate(arrDateFmt)){
		document.formState.lstMonth.value   = arrDateFmt.getMonth() + 1;
		document.formState.lstDay.value     = arrDateFmt.getDate();
		document.formState.lstYear.value    = arrDateFmt.getFullYear(); 
		if(isDate(depDateFmt)){
			document.formState.yDepMonth.value  = depDateFmt.getMonth() + 1;
			document.formState.yDepDay.value    = depDateFmt.getDate();
			document.formState.yDepYear.value   = depDateFmt.getFullYear();
			validateState();
		} else{
			alert('Please select a Departure Date');
		}
	} else{
		alert('Please select an Arrival Date');
	}
	
}
	
function isDate(currDate){
	var dateFields = String(currDate).split("/");
	if(dateFields.length != 3) return false;
	return ( !isNaN(dateFields[0]) && !isNaN(dateFields[1]) && !isNaN(dateFields[2]));
	//return ( !isNaN(currDate.getDate()) && !isNaN(currDate.getMonth()) && !isNaN(currDate.getFullYear()));
}
/*Lami Changes for Trec Widget : Start */
function isDateForLami(currDate){
	var dateFields = String(currDate).split("/");
	if(dateFields.length != 2) return false;
	return ( !isNaN(dateFields[0]) && !isNaN(dateFields[1]));
	//return ( !isNaN(currDate.getDate()) && !isNaN(currDate.getMonth()) && !isNaN(currDate.getFullYear()));
}
/*Lami Changes for Trec Widget : End */	
function isValidDate(currDate, isArrival){
	
	var today		= new Date();
	var thisYear		= today.getFullYear();
	var thisDate     = today.getDate();
	var thisMonth    = today.getMonth();
	var validDate    = true;
	var currDateFlds = currDate.split("/");
	var currDay 	   	= parseInt(currDateFlds[1],10);
	var currMonth   	= parseInt(currDateFlds[0],10); 
	var currYear 	= parseInt(currDateFlds[2],10);
	if(currYear < 1000 || currYear > 9999){
		validDate = false;
	}/*else if(currYear > (thisYear + 1)){
		 if(currMonth == (thisMonth + 1)){
			if (isArrival){
			   if(currDay >= thisDate){
		 		validDate=false;
		 	   }
		 	}else{
		 		if(currDay > thisDate){
		 			validDate=false;
		 		}
		 	}
		 } else if(currMonth > (thisMonth + 1)){
			validDate = false;		 	
		 }
	} */else{
		var firstDay  = 1;
		var lastDay   = 31; 
		switch(currMonth){
			case 4: 
			case 6:
			case 9:
			case 11: lastDay = 30;
					 break;		 
			case 2:  lastDay = 28; 
					 if(isLeapYear(currYear)){
						lastDay = 29;
				 	 }
				  	 break;
			default: lastDay = 31;
					 break;
		}
		if(currDay < firstDay || currDay > lastDay || currMonth > 12 || currMonth < 1){
			validDate = false;
		}
		//alert(currDateFlds+'\n'+'currDay: '+currDay+'currMonth: '+currMonth+' firstDay: '+firstDay+' lastDay: '+lastDay+'\ntoday: '+today+'\n'+validDate);
	}	
	return validDate;
	
}
/*Lami Changes for Trec Widget : Start */
function isValidDateForLami(currDate, isArrival){
	
	var today		= new Date();	
	var thisDate     = today.getDate();
	var thisMonth    = today.getMonth();
	var validDate    = true;
	var currDateFlds = currDate.split("/");
	var currDay 	   	= parseInt(currDateFlds[1],10);
	var currMonth   	= parseInt(currDateFlds[0],10); 	
	/*}else if(currYear > (thisYear + 1)){
		 if(currMonth == (thisMonth + 1)){
			if (isArrival){
			   if(currDay >= thisDate){
		 		validDate=false;
		 	   }
		 	}else{
		 		if(currDay > thisDate){
		 			validDate=false;
		 		}
		 	}
		 } else if(currMonth > (thisMonth + 1)){
			validDate = false;		 	
		 }
	} */
		var firstDay  = 1;
		var lastDay   = 31; 
		switch(currMonth){
			case 4: 
			case 6:
			case 9:
			case 11: lastDay = 30;
					 break;		 
			case 2:  lastDay = 28; 
					 if(isLeapYear(currYear)){
						lastDay = 29;
				 	 }
				  	 break;
			default: lastDay = 31;
					 break;		
		if(currDay < firstDay || currDay > lastDay || currMonth > 12 || currMonth < 1){
			validDate = false;
		}
		//alert(currDateFlds+'\n'+'currDay: '+currDay+'currMonth: '+currMonth+' firstDay: '+firstDay+' lastDay: '+lastDay+'\ntoday: '+today+'\n'+validDate);
	}	
	return validDate;
	
}
/*Lami Changes for Trec Widget : End */

//modified for WBER @author Cognizant	
function validateState(isHomePage) 
	{
   
	//COMMENTED FOR POI-POR CHANGE AUTHOR::COGNIZANT DATE::23/03/2011*********** START ************************************
	/*if (isHomePage){
		if(document.forms['formState'].country.options[document.forms['formState'].country.selectedIndex].value != "US" && 
			document.forms['formState'].country.options[document.forms['formState'].country.selectedIndex].value != "CA")
		{
			if(!isEmpty(document.forms['formState'].state.options[document.forms['formState'].state.selectedIndex].value) && 
				document.forms['formState'].state.options[document.forms['formState'].state.selectedIndex].value != "NA"){
			   colorMissingFields("state");
			   showErrorMessage("The state/province and country you selected do not match.");
			   return;
			}
		}

		if(document.forms['formState'].country.options[document.forms['formState'].country.selectedIndex].value == "US" || 
			document.forms['formState'].country.options[document.forms['formState'].country.selectedIndex].value == "CA")
		{  
			if(isEmpty(document.forms['formState'].state.options[document.forms['formState'].state.selectedIndex].value))
			{
			  colorMissingFields("state");
			  showErrorMessage("Please select a state when selecting US or Canada as country.");
			  return;
	 	   } 
		}
		
		if(mismatchUSCanadaStateSelection(document.forms['formState'].country.options[document.forms['formState'].country.selectedIndex].value, 
				document.forms['formState'].state.options[document.forms['formState'].state.selectedIndex].value)){
			colorMissingFields("state");
			showErrorMessage("The state/province and country you selected do not match.");
			return;
		}
	}*/
	//COMMENTED FOR POI-POR CHANGE AUTHOR::COGNIZANT DATE::23/03/2011*********** END ************************************

	// 02/01/07 - Modified by Hemanth for reskin project
	var now = new Date();
	var today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
	var arrivalDate = document.forms['formState'].home_resv_arrival.value;
	var departureDate = document.forms['formState'].home_resv_departure.value;
	
	var arrivalDateSec   = Date.parse(arrivalDate);
	var departureDateSec = Date.parse(departureDate);
	var arrDateFmt = new Date(arrivalDateSec);
	var depDateFmt = new Date(departureDateSec);
	if(isValidArrDate() && isValidDeptDate()){
   		if(Date.parse(arrivalDate) >= Date.parse(departureDate))
   		{  
   			submitted = true;
    		colorMissingFields("home_resv_departure");
			showErrorMessage("Please enter a departure date that is after the arrival date.");
    		return;
   		}else{
			document.forms['formState'].submit();
   		}
   	}
}
//End of chnage

/*Lami Changes for Trec Widget : Start */
function validateStateLami(isHomePage, variant) 
	{
	/* Start : Lami post prod change in Branch : SPHINX07222010*/
	if (isHomePage){
		/*if(document.getElementById("useWRPointsYes").checked == true && 
		  (!(document.getElementById("country").value == "US")||
		  (document.getElementById("country").value == "CA"))) {
			showErrorMessageForLami('Properties outside the U.S. and Canada cannot be reserved online using Wyndham Rewards points. Please adjust your search or <a href="https://www.wyndhamrewards.com/trec/consumer/redeemHotelWyn.action" target="_blank" title="International Free Night Rewards - opens in new window">click here for International Free Night Rewards</a>.');
			return;
		}*/
	/* End : Lami post prod change in Branch : SPHINX07222010*/
		/* Comented for POI-POR, 4/12/2011 start*/
//		if(document.forms['formState'].country.options[document.forms['formState'].country.selectedIndex].value != "US" && 
//			document.forms['formState'].country.options[document.forms['formState'].country.selectedIndex].value != "CA")
//		{
//			if(!isEmpty(document.forms['formState'].state.options[document.forms['formState'].state.selectedIndex].value) && 
//				document.forms['formState'].state.options[document.forms['formState'].state.selectedIndex].value != "NA"){
//			   colorMissingFields("state");
//			   showErrorMessageForLami("The state/province and country you selected do not match.");
//			   return;
//			}
//		}
		/* Comented for POI-POR, 4/12/2011 end*/

		/*if(document.forms['formState'].country.options[document.forms['formState'].country.selectedIndex].value == "US" || 
			document.forms['formState'].country.options[document.forms['formState'].country.selectedIndex].value == "CA")
		{  
			if(isEmpty(document.forms['formState'].state.options[document.forms['formState'].state.selectedIndex].value))
			{
			  colorMissingFields("state");
			  if(variant == "de") {
				  showErrorMessageForLami("Der Staat / Provinz und von Ihnen ausgewählte Land stimmen nicht überein.");
			  } else {
				showErrorMessageForLami("Please select a state when selecting US or Canada as country.");
			  }			  
			  return;
	 	   } 
		}
		
		if(mismatchUSCanadaStateSelection(document.forms['formState'].country.options[document.forms['formState'].country.selectedIndex].value, 
				document.forms['formState'].state.options[document.forms['formState'].state.selectedIndex].value)){
			colorMissingFields("state");
			showErrorMessageForLami("The state/province and country you selected do not match.");
			return;
		}*/
	}

	// 02/01/07 - Modified by Hemanth for reskin project
	/* Start - Changed for Lami : Removing Date validation*/
	/*var now = new Date();
	var today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
	var arrivalDate = document.forms['formState'].checkInDate.value;
	var departureDate = document.forms['formState'].checkOutDate.value;
	
	var arrivalDateSec   = Date.parse(arrivalDate);
	var departureDateSec = Date.parse(departureDate);
	var arrDateFmt = new Date(arrivalDateSec);
	var depDateFmt = new Date(departureDateSec);

	if(isValidArrDateForLami(variant) && isValidDeptDateForLami(variant)){
   		if(Date.parse(arrivalDate+"/"+now.getFullYear()) >= Date.parse(departureDate+"/"+now.getFullYear()))
   		{  
   			submitted = true;
    		colorMissingFields("home_resv_departure");
			showErrorMessageForLami("Please enter a departure date that is after the arrival date.");
    		return;
   		}else{*/
			document.forms['formState'].submit();
   		//}
   	//}
	/* End - Changed for Lami : Removing Date validation*/
}
/*Lami Changes for Trec Widget : End */
function getNights(enableXorY)
{
	var arrivalDate 	    = document.forms['formState'].home_resv_arrival.value;
	var departureDate    = document.forms['formState'].home_resv_departure.value;
	var arrivalDateSec   = Date.parse(arrivalDate);
	var departureDateSec = Date.parse(departureDate);
	var milliSecInADay   = 1000 * 60 * 60 * 24;
	var difference =  Math.abs(departureDateSec - arrivalDateSec);
	var arrDateFmt =  new Date(arrivalDateSec);
	var depDateFmt =  new Date(departureDateSec);
	var arrDateDD  =  arrDateFmt.getDate();
	var arrDateMM  =  arrDateFmt.getMonth();
	var arrDateYY  =  arrDateFmt.getFullYear();
	var deptDateYY =  depDateFmt.getFullYear();
	var today    = new Date();
	var thisYear = today.getFullYear();
	
	if(deptDateYY > (thisYear + 1)){
		alert("The date you have chosen is beyond the booking range.");
		return false;
	}
	var nights     =  Math.round(difference/milliSecInADay);
	if(nights > 99){
		alert("You can only book stays for 99 days or less.");
		return false;
	}
	
	var arrMonth = arrDateFmt.getMonth() + 1;
    var depMonth = depDateFmt.getMonth() + 1;	
	if (enableXorY){
	
	document.forms['formState'].lstNights.value  = Math.round(difference/milliSecInADay);
	document.forms['formState'].lstMonth.value   = makeTwoChars(arrMonth);
	document.forms['formState'].lstDay.value     = arrDateFmt.getDate();
	document.forms['formState'].lstYear.value    = arrDateFmt.getFullYear(); 
	document.forms['formState'].yDepMonth.value  = makeTwoChars(depMonth);
	document.forms['formState'].yDepDay.value    = depDateFmt.getDate();
	document.forms['formState'].yDepYear.value   = depDateFmt.getFullYear();
	return true;
	}else{
	document.forms['formState'].lstNights.value  = Math.round(difference/milliSecInADay);
	document.forms['formState'].lstMonth.value   = makeTwoChars(arrMonth);
	document.forms['formState'].lstDay.value     = arrDateFmt.getDate();
	document.forms['formState'].lstYear.value    = arrDateFmt.getFullYear(); 
	document.forms['formState'].xDepMonth.value  = makeTwoChars(depMonth);
	document.forms['formState'].xDepDay.value    = depDateFmt.getDate();
	document.forms['formState'].xDepYear.value   = depDateFmt.getFullYear();	
	return true;
	}
}
function submitState(isHomePage)
{
	if (document.forms['formState'].LastName.value == "Last Name")
	{
		document.forms['formState'].LastName.value = "";
	}
	if (document.forms['formState'].ByReqNum.value == "Number")
	{
		document.forms['formState'].ByReqNum.value = "";
	}
	if (validateByRequestNumber() && validateLastName())
	{
		 if(getNights(isHomePage)){
		 	document.forms['formState'].submit();	
	 	}
	 }
}  	

function isAlphabetic (s)

{  
    var i;
    var str = s +"";
        // Check if first character is letter.
     
        var c = str.charAt(0);

        if (!isLetter(c))
        return false;

    
    return true;
}


function isLetter (c)
{   return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}

//Added for WBER @author Cognizant
function isEmpty (inputStr)
{	
	if ( null == inputStr || "" == inputStr) { 
		return true; 
	}
	return false;
}

function mismatchUSCanadaStateSelection(countryName, stateName){
	if(isEmpty(countryName) || isEmpty(stateName)){
		return false;
	}
	
	var usaStates = new Array("AL","AK","AZ","AR","CA","CO","CT","DE","DC","FL","GA","HI","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","NT","NS","ON","PE","QC","SK","NF","NU","YT");
	
	var mismatchState = false;
	
	if(countryName == "US"){
		for (i = 0; i < canadaStates.length; i++) {
			if (stateName.toUpperCase() == canadaStates[i].toUpperCase()) {
				mismatchState = true;
				break;
			}
		}
	} else if(countryName == "CA"){
		for (i = 0; i < usaStates.length; i++) {
			if (stateName.toUpperCase() == usaStates[i].toUpperCase()) {
				mismatchState = true;
				break;
			}
		}
	}
	
	return mismatchState;
}

function showErrorMessage(errMsg){
	document.getElementById('home_resv_errmsg').innerHTML=errMsg;
	if(errMsg==''){
	document.getElementById('home_resv_err').className = "home_resv_error_hidden";
	}
		else{
	document.getElementById('home_resv_err').className = "home_resv_error_visible";
		}
}
/*Lami Changes for Trec Widget : Start */
function showErrorMessageForLami(errMsg){
	document.getElementById('home_resv_errmsg').innerHTML=errMsg;
	document.getElementById('home_resv_err').className = "home_resv_error_visible";
	document.getElementById('home_resv_errmsg_icon').innerHTML = "<img src=\"../images/homecontent/iconerror.gif\"></img>";
}
/*Lami Changes for Trec Widget : End */
//End of chnage
