// res_property0012.js
// version 2.0
// 25Jan 2004
// bharris@wyndham.com
function $(elementID) {
	if (document.getElementById) {
		return document.getElementById(elementID);
	} else if (document.all) {
		return document.all[elementID];
	} else if (document.layers) {
		document.layers[elementID];
	} else {
		window[elementID];
	}
}

// basic AJAX connector
function getHttpObject()
{
	var xmlHttpRequest;
  /*@cc_on
  @if (@_jscript_version >= 5)
  try {
    xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (exception1) {
    try {
      xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (exception2) {
      xmlHttpRequest = false;
    }
  }
  @else
    xmlhttpRequest = false;
  @end @*/
 
  if (!xmlHttpRequest && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlHttpRequest = new XMLHttpRequest();
    } catch (exception) {
      xmlHttpRequest = false;
    }
  }
  return xmlHttpRequest;
}


var HttpObjState = getHttpObject();
var HttpObjCity = getHttpObject();

function clearSelect(selectObj, newText) {
 for (var i=selectObj.options.length;i>=0;i--)
 selectObj.options[i] = null;
 selectObj.options[0] = new Option(newText,"", false, false );
}

function getStates(country,selectedState,selectedCity) {
 clearSelect(document.getElementById('state'), "Loading...");
 clearSelect(document.getElementById('destination'), "-----------------------");
  HttpObjState.open("GET", "../AjaxServlet?task=getStateOptions&countryNameForCity="+country +"&selectedState="+selectedState+"&selectedCity="+selectedCity, true);
//  HttpObjState.onreadystatechange= stateResponse;
  HttpObjState.onreadystatechange= function (){

		if(HttpObjState.readyState == 4) {

		try {
			var stateSelect = $('state');
			var ops = HttpObjState.responseXML.getElementsByTagName('option');
			var StateSelected='';
			for(var i=0;i<ops.length;i++) {
				if(ops[i].getAttribute("selected") == "true") {
					stateSelect.options[i] = new Option(ops[i].firstChild.data, ops[i].attributes[0].value, true, true);
					StateSelected = ops[i].attributes[0].value;
					stateSelect.options[i].selected=true;
				} else {
					stateSelect.options[i] = new Option(ops[i].firstChild.data, ops[i].attributes[0].value, false, false);
				}
			}
			if(ops.length<=1){
				getCities($('country').value,'NA',selectedCity)
			}
			else{
				getCities($('country').value,selectedState,selectedCity)
			}
		} catch (ex) {
//					alert("exception");
		}

    }

	}


  HttpObjState.send(null);
//	getCities($('country').value,$('stateProvince').value);
//	getCities(document.getElementById('country').value,document.getElementById('stateProvince').value);
}


function getCities(country,state,city) {
 clearSelect(document.getElementById('destination'), "Loading...");
  HttpObjCity.open("GET", "../AjaxServlet?task=getCityOptions&stateNameForCity="+state+"&countryNameForCity="+country+"&selectedCity="+city, true);
  HttpObjCity.onreadystatechange=function() {
	   if(HttpObjCity.readyState == 4) {
				try {
				var citySelect = $('destination');
			var CitySelected='';

				var ops = HttpObjCity.responseXML.getElementsByTagName("option");
				for(var i=0;i<ops.length;i++) {
				if(ops[i].getAttribute("selected") == "true") {
						citySelect.options[i] = new Option(ops[i].firstChild.data, ops[i].attributes[0].value, true, true);
					CitySelected = ops[i].attributes[0].value;
					citySelect.options[i].selected=true;
				} else 
						citySelect.options[i] = new Option(ops[i].firstChild.data, ops[i].attributes[0].value, false, false);
				}

			} catch (ex) {
//						alert("exception");
			}
    }
  }
  HttpObjCity.send(null);
}



function new_win(url,name,options)
{
   var new_window = window.open(url, name, options)

   //4889 - Sujay - 20041125
   new_window.focus();
}


function preselect(selectObject, value) {
	for (i = 0; i < selectObject.options.length; i++) {
		if (selectObject.options[i].value.toUpperCase() == value.toUpperCase()) {
			selectObject.selectedIndex = i;
			return value;
		}
	}
	return "";
}

function preselectTextbox(textboxObject, value) {
	textboxObject.value = value;
}

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","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 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" && selectedCountry.toUpperCase() != "") {
		preselect(stateSelectObject, "NA");
	} else {
		if (stateSelectObject.value == "NA" || selectedCountry=="") {
			preselect(stateSelectObject, ""); // "Select A State/Province"
		}
        }
}


function incDate(dateElement, currentDate, isReverse) {
	
	// consider currentDate is in 'mm/dd' format
	var splits = currentDate.split("/");
	var valid = false;
	if (splits.length == 2) {
		if (!isNaN(trim(splits[0])) && !isNaN(trim(splits[1]))) {
			valid = true;
		}
	}
	if (!valid)	{
		return;
	}
	if(isReverse=='false') {
		var m = parseInt(trim(splits[0]) , 10);
		var d = parseInt(trim(splits[1]) , 10);
	}
	else {
		var m = parseInt(trim(splits[1]) , 10);
		var d = parseInt(trim(splits[0]) , 10);
	}

	var daysInMonth = new Array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

	// Set 29 days in Feb if this is a leap year
	if ("undefined" != typeof(isLeapYear) && isLeapYear == false) {
		daysInMonth[1] = 28;
	}

	d++;
	if (d < 1 || d > daysInMonth[m - 1]) {
		d = 1;
		m++;
	}
	if (m < 1 || m > 12) {
		m = 1;
	}

	// convert m/d to mm/dd
	if (m < 10) {
		m = "0" + m;
	}
	if (d < 10) {
		d = "0" + d;
	}

	if(isReverse=='false') var result = m + "/" + d;
	else if(isReverse=='true') var result = d + "/" + m;
	dateElement.value = result;
}


//being used in Use Case 6 Exception 2
function decrDate(dateElement, currentDate) {
	// consider currentDate and dateElement.value is in 'mm/dd' format
	var splits1 = dateElement.value.split("/");
	var splits2 = currentDate.split("/");
	var valid = false;
	if (splits2.length == 2) {
		if (!isNaN(trim(splits2[0])) && !isNaN(trim(splits2[1])) && !isNaN(trim(splits1[0])) && !isNaN(trim(splits1[1]))) {
			valid = true;
		}
	}
	if (!valid)	{
		return;
	}

	var m1 = parseInt(trim(splits1[0]) , 10);
	var d1 = parseInt(trim(splits1[1]) , 10);
	var m2 = parseInt(trim(splits2[0]) , 10);
	var d2 = parseInt(trim(splits2[1]) , 10);

	var daysInMonth = new Array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

	// Set 29 days in Feb if this is a leap year
	if ("undefined" != typeof(isLeapYear) && isLeapYear == false) {
		daysInMonth[1] = 28;
	}

	if ((m1 == m2) && (d1 > d2)) {
		d2--;
		if (d2 < 1 || d2 > daysInMonth[m2 - 1]) {
			m2--;
			d2 = daysInMonth[m2 - 1];
		}
		if (m2 < 1 || m2 > 12) {
			m2 = 12;
			d2 = daysInMonth[m2 - 1];
		}
		// convert m/d to mm/dd
		if (m2 < 10) {
			m2 = "0" + m2;
		}
		if (d2 < 10) {
			d2 = "0" + d2;
		}
		var result = m2 + "/" + d2;
		dateElement.value = result;
	} else {
		// convert m/d to mm/dd
		if (m1 < 10) {
			m1 = "0" + m1;
		}
		if (d1 < 10) {
			d1 = "0" + d1;
		}
		var result = m1 + "/" + d1;
		dateElement.value = result;
	}

}

function trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}



// takes the original URL and removes any entry_src out of there
function getEntrySourceUrl(original) {
	var startIndex = original.indexOf("entry_source");
	if (startIndex == -1) {
		return original;
	}
	var endIndex = original.indexOf("&", startIndex);
	var ret;
	if (endIndex != -1)	{
		ret = original.substring(0, startIndex) + original.substring(endIndex + 1);
	}
	else {
		ret = original.substring(0, startIndex);
	}
	return ret;
}

//TED 7753
var txtCityState = new Array();
var checkedValues;
var txtCity;
var txtState;
var txtCountry;
function populateCityState(r,f){
	for(var i=0;i<f.elements[r].length;i++){
			if (f.elements[r][i].checked){
				checkedValues = f.elements[r][i].value;
				txtCityState = checkedValues.split(',');
				f.destination.value=txtCityState[0];
				f.state.value=txtCityState[1];
				f.country.value=txtCityState[2];
			}
	}
}


function showProperties()
	{
	//var propWin = window.open("lib/properties_select0010.htm","prop","height=256,width=320,menubar=0,directories=0,status=0,toolbar=0,scrollbars=1,left=128,top=128,alwaysRaised=1")
	propWin = window.open("/rates/PropertyList.jsp","prop","height=256,width=320,menubar=0,directories=0,status=0,toolbar=0,scrollbars=1,left=128,top=128,alwaysRaised=1");
	//propWin.document.write(xProp);
	//propWin.document.close();
	propWin.focus();
	}

function validatebrnum()
{
	
	if (document.formProperty.LastName.value.length <=0  && document.formProperty.ByReqNum.value.length > 0)
	{
	   alert("The following information is missing or incorrect:\n  * Last Name" );
	   colorMissingFields2("LastName");
		return false;
	}
	return true;
}

function validatebrname()
{
	
	if (document.formProperty.LastName.value.length > 0 && document.formProperty.ByReqNum.value.length <= 0 )
	{
	   alert("The following information is missing or incorrect:\n  * Wyndham ByRequest Member Number" );
       		colorMissingFields2("ByReqNum");
		return false;
	}
      return true;
}
	
function submitProp()
	{
	
	document.formProperty.submit();
	
	}
	function colorMissingFields2(fdName)
	{
	  var path = eval("document.formProperty." +fdName+ ".style");
		path.backgroundColor = '#FFFFCC';
	}
	
function validateProp() 
	{
	if (document.formProperty.proLabel.value=="Click to select" )
		{
		alert("Please select a property.");
		colorMissingFields2("proLabel");
		}
	else
		{
		 	if (!checkDate(document.formProperty.lstDay.options[document.formProperty.lstDay.selectedIndex].value,document.formProperty.lstMonth.options[document.formProperty.lstMonth.selectedIndex].value,document.formProperty.lstYear.options[document.formProperty.lstYear.selectedIndex].value,false)) 
          	{   			
   			alert("The date you have selected is in the past. Please select another date.");
   			colorMissingFields2("lstDay");
   			colorMissingFields2("lstMonth");
   			colorMissingFields2("lstYear");
   			return;
   	        }
   	        if (!checkDate(document.formProperty.xDepDay.options[document.formProperty.xDepDay.selectedIndex].value,document.formProperty.xDepMonth.options[document.formProperty.xDepMonth.selectedIndex].value,document.formProperty.xDepYear.options[document.formProperty.xDepYear.selectedIndex].value,false)) 
	        {    			
   			alert("The date you have selected is in the past. Please select another date.");
   			colorMissingFields2("xDepDay");
   			colorMissingFields2("xDepMonth");
   			colorMissingFields2("xDepYear");
   			return;
        	}
           if(!compareDate(document.formProperty.lstDay.options[document.formProperty.lstDay.selectedIndex].value,document.formProperty.lstMonth.options[document.formProperty.lstMonth.selectedIndex].value,document.formProperty.lstYear.options[document.formProperty.lstYear.selectedIndex].value,document.formProperty.xDepDay.options[document.formProperty.xDepDay.selectedIndex].value,document.formProperty.xDepMonth.options[document.formProperty.xDepMonth.selectedIndex].value,document.formProperty.xDepYear.options[document.formProperty.xDepYear.selectedIndex].value))
    	  {  
    	    alert("Please enter a departure date that is after the arrival date.");
    	    colorMissingFields2("xDepDay");
   			colorMissingFields2("xDepMonth");
   			colorMissingFields2("xDepYear");    	    
    	    return;
          	}
		 if (document.formProperty.ByReqNum.value == "Number")
	    {
		   document.formProperty.ByReqNum.value = "";
	    }
	    if (document.formProperty.LastName.value == "Last Name")
	    {
		  document.formProperty.LastName.value = "";
	    }
		 if (validatebrnum() && validatebrname())
		  submitProp();
		} 
	}