// res_property0011.js
// version 2.0

function colorMissingFields(fdName)
	{
	  var path = eval("document.forms['formState']." +fdName+ ".style");
	  path.backgroundColor = '#FFFFCC';
	}

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'].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()));
}
	
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;
	
}
	
function validateState(isHomePage) 
	{
	if (isHomePage){
	 if(!isAlphabetic(document.forms['formState'].state.options[document.forms['formState'].state.selectedIndex].value) && document.forms['formState'].state.selectedIndex != 0)
	 {
	   alert("Please select a valid state");
	   colorMissingFields("state");
	   return false;
	   }
	if(document.forms['formState'].country.options[document.forms['formState'].country.selectedIndex].value == "United States" )
	 {  if(document.forms['formState'].state.selectedIndex == 0)
	      {
	      alert("Please select a state when selecting US as country");
	      colorMissingFields("state");
	      return false;
	 	   } 
		}
	}

	// 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('home_resv_arrival', 'home_resv_departure') && isValidDeptDate('home_resv_arrival', 'home_resv_departure'))
		{
			if(Date.parse(arrivalDate) >= Date.parse(departureDate))
				{  
					submitted = true;
					alert("Please enter a departure date that is after the arrival date.");
						colorMissingFields("home_resv_departure");
						return false;
				}else{
					submitState(isHomePage);
				}
		}else{
			return false;	
		}
	}

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)){
			 return true;
		 	//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")) )
}