// res_property0011.js
// version 1.0
// 22Jan 2004
// bharris@wyndham.com

function submitState()
	{
	if (document.formState.LastName.value == "Last Name")
	{
		document.formState.LastName.value = "";
	}
	if (document.formState.ByReqNum.value == "Number")
	{
		document.formState.ByReqNum.value = "";
	}
	if (validateByRequestNumber() && validateLastName())
	{
	 getNights()
	 document.formState.submit();
	 }
	}  	
function colorMissingFields(fdName)
	{
	  var path = eval("document.formState." +fdName+ ".style");
		path.backgroundColor = '#FFFFCC';
	}
function validateByRequestNumber()
	{
	
	if(document.formState.ByReqNum.value.length <= 0 && document.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.formState.ByReqNum.value.length > 0 && document.formState.LastName.value.length <= 0)
	 {
	    alert("The following information was missing or incorrect:\n" +"* Last Name");
        colorMissingFields("LastName");
        return false;
	  }
	  return true;
}

function getNights()
	{
	var aDate = new Date();
	var bDate = new Date();
	var xMonth = parseInt(document.formState.lstMonth.value);
	xMonth -= 1;
	var xDay = document.formState.lstDay.value;
	var xYear = document.formState.lstYear.value;
	aDate.setMonth(xMonth);
	aDate.setDate(xDay);
	aDate.setYear(xYear);

	var xMonth = parseInt(document.formState.yDepMonth.value);
	xMonth -= 1;
	var xDay = document.formState.yDepDay.value;
	var xYear = document.formState.yDepYear.value;
	bDate.setMonth(xMonth);
	bDate.setDate(xDay);
	bDate.setYear(xYear);	
	

	var xDay = 1000 * 60 * 60 * 24;
	var xDate = aDate.getTime();
	var yDate = bDate.getTime();
	var xDiff = Math.abs(xDate - yDate);
	var yDay = Math.round(xDiff/xDay);
	document.formState.lstNights.value = yDay;
	}

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

	
function validateState() 
	{
     
	 if(!isAlphabetic(document.formState.state.options[document.formState.state.selectedIndex].value) && document.formState.state.selectedIndex != 0)
	 {
	   alert("Please select a valid state");
	   colorMissingFields("state");
	   return;
	   }
	if(document.formState.country.options[document.formState.country.selectedIndex].value == "United States" )
	 {  if(document.formState.state.selectedIndex == 0)
	      {
	      alert("Please select a state when selecting US as country");
	      colorMissingFields("state");
	      return;
	      }

	}
	if (!checkDate(document.formState.lstDay.options[document.formState.lstDay.selectedIndex].value,document.formState.lstMonth.options[document.formState.lstMonth.selectedIndex].value,document.formState.lstYear.options[document.formState.lstYear.selectedIndex].value,false)) 
	{   			
   			alert("The date you have selected is in the past. Please select another date.");
   			colorMissingFields("lstDay");
   			colorMissingFields("lstMonth");
   			colorMissingFields("lstYear");
   			return;
   	}
   	if (!checkDate(document.formState.yDepDay.options[document.formState.yDepDay.selectedIndex].value,document.formState.yDepMonth.options[document.formState.yDepMonth.selectedIndex].value,document.formState.yDepYear.options[document.formState.yDepYear.selectedIndex].value,false)) 
	{   			
   			alert("The date you have selected is in the past. Please select another date.");
   			   			colorMissingFields("yDepDay");
   			   			colorMissingFields("yDepMonth");
   			   			colorMissingFields("yDepYear");
   			
   			return;
   	}
   	if(!compareDate(document.formState.lstDay.options[document.formState.lstDay.selectedIndex].value,document.formState.lstMonth.options[document.formState.lstMonth.selectedIndex].value,document.formState.lstYear.options[document.formState.lstYear.selectedIndex].value,document.formState.yDepDay.options[document.formState.yDepDay.selectedIndex].value,document.formState.yDepMonth.options[document.formState.yDepMonth.selectedIndex].value,document.formState.yDepYear.options[document.formState.yDepYear.selectedIndex].value))
    	{  alert("Please enter a departure date that is after the arrival date.");
    	   colorMissingFields("yDepDay");
   		   colorMissingFields("yDepMonth");
   		   colorMissingFields("yDepYear");
    	   return;
    	}
	submitState();
	}
	
	
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")) )
}
