function setdate(arrivalDate, departureDate, thisForm){
	if(arguments.length == 2) 
	{
		thisForm = document.hotelSearchCriteriaForm;
	}

	if ( thisForm != null ) {
		if(arrivalDate != '' && arrivalDate != null && departureDate != '' && departureDate != null)
		{
			if(arrivalDate.indexOf("%") > 0) arrivalDate = unescape(arrivalDate);
			if(departureDate.indexOf("%") > 0) departureDate = unescape(departureDate);
			myArray = arrivalDate.split("/");
			var checkinDate = new Date((myArray[2]), myArray[0] - 1, myArray[1]);
			myArray = departureDate.split("/");
			var checkoutDate = new Date((myArray[2]), myArray[0] -1, myArray[1]);
		}
		else
		{
			var now = new Date();
			var checkinDate = new Date(now.getTime() + (14 * 24*60*60*1000));
			var checkoutDate = new Date(now.getTime() + (15 * 24*60*60*1000));
		}

		thisForm.arrivalMonth.options[checkinDate.getMonth()].selected = true;
		thisForm.departureMonth.options[checkoutDate.getMonth()].selected = true;
		thisForm.arrivalDay.options[checkinDate.getDate()-1].selected = true;
		thisForm.departureDay.options[checkoutDate.getDate()-1].selected = true;
		updateSelect(thisForm.arrivalMonth, thisForm.arrivalDay);	
		updateSelect(thisForm.departureMonth, thisForm.departureDay);
	}
}

function isBrowserSupp() {
    version =  parseFloat( navigator.appVersion );
	if ( ( version >= 2.0 ) && ( version < 2.1 ) && ( navigator.appName.indexOf( "Netscape" ) != -1 ) ) {
        return false;
    }
    else {
    	return true;
    }
	return true;
}

function isLeapYear(yrStr) {
	var leapYear=false;
	var year = parseInt(yrStr, 10);
	if (year%4 == 0) {
	    leapYear=true;
    	if (year%100 == 0) {
	        leapYear=false;
        	if (year%400 == 0) {
	            leapYear=true;
            }
        }
    }
	return leapYear;

}

function getDaysInMonth(mthIdx, YrStr) {
	var maxDays=31
	if (mthIdx==1) {
	    if (isLeapYear(YrStr)) {
	        maxDays=29;
        }
    	else {
	        maxDays=28;
        }
    }
	if (mthIdx==3 || mthIdx==5 || mthIdx==8 || mthIdx==10) {
	    maxDays=30;
    }
	return maxDays;
}

function adjustDate(mthIdx, Dt) {
	var value=0;
	var today = new Date()
	var theYear = parseInt(today.getYear(),10)
	if (mthIdx < today.getMonth()) {
    	theYear = (parseInt(today.getYear(), 10) + 1)
	}
	if(theYear<100){
    	theYear = "19" + theYear
	}
	else {
    	if((theYear-100) < 10) {
        	theYear = "0" + (theYear-100)
    	}
    	else{
        	theYear = (theYear-100)+""
    	}
    	theYear = "20" + theYear
	}
	var numDays=getDaysInMonth(mthIdx, theYear);
	if (mthIdx==1) {
	    if (Dt.options.selectedIndex + 1 < numDays) {
	        return 0;
        }
    	else {
        	Dt.options.selectedIndex=numDays - 1;
        	if (numDays==29) {
	            return 99;
            }
        	else {
            	return 1;
            }
        }
    }
	if (Dt.options.selectedIndex + 1 < numDays) {
    	value=0;
    }
	else {
    	if (Dt.options.selectedIndex + 1 > numDays) {
	        Dt.options.selectedIndex ;
       	 	value=3;
        }
    	else {
        	value=2;
        }
    }
	return value;
}

function getYear(mthIdx) {
	var today = new Date();
	var theYear = parseInt(today.getYear(),10)
	if (mthIdx < today.getMonth()) {
    	theYear = (parseInt(today.getYear(), 10) + 1)
	}
	if(theYear<100){
    	theYear = "19" + theYear
	}
	else {
    	if((theYear-100) < 10) {
        	theYear = "0" + (theYear-100)
    	}
    	else{
        	theYear = (theYear-100)+""
    	}
    	theYear = "20" + theYear
	}
	return theYear;
}

/*function dateChange(inM,inD,outM,outD) {
    if (!isBrowserSupp()) {
    	return;
    }
	var res = adjustDate(inM.options.selectedIndex, inD);
	if (res != 0) {
		outD.options.selectedIndex=0;
        if (outM.options.selectedIndex==11) {
        	outM.options.selectedIndex=0
		} else {
			outM.options.selectedIndex=inM.options.selectedIndex + 1;
        }
    } else {
    	outM.options.selectedIndex = inM.options.selectedIndex;
    	outD.options.selectedIndex = inD.options.selectedIndex+2;
    }
	return ;
}*/

function updateSelect(month, day) {
	var inDay = day.options.selectedIndex;
	var days = getDaysInMonth(month.options.selectedIndex, getYear(month.options.selectedIndex));
	day.options.length = 0;
	for (var i = 0; i < days; i++) {
		day.options[i] = new Option(i+1, i+1);
	}
	day.options[inDay].selected = true;
}

function dateChange(inM, inD, outM, outD) {
	if (!isBrowserSupp())
		return;
		
	updateSelect(inM, inD);
		
	var theYear = getYear(inM.options.selectedIndex);
	var daysInMonth = getDaysInMonth(inM.options.selectedIndex, theYear);
	var difference = daysInMonth - (inD.options.selectedIndex + 2)

	outM.options.selectedIndex = inM.options.selectedIndex;
	if (difference <= 0) {
		if (inM.options.selectedIndex == 11)	
			outM.options.selectedIndex = 0;
		else
			outM.options.selectedIndex = inM.options.selectedIndex + 1;
		outD.options.selectedIndex = Math.abs(difference);
	} else {
		outD.options.selectedIndex = inD.options.selectedIndex + 2;
	}
	updateSelect(outM, outD);
	
	return;
}

function dmddChange(outM,outD) {
    if (!isBrowserSupp()) {
	    return;
    }
	adjustDate(outM.options.selectedIndex,outD);
	return;
}


//*******************************************************************************'
// remove any leading or trailing spaces

function trim(strValue) // as String
{
	// if a valid string was passed....
	if (strValue.length > 0)
	{
		// remove leading spaces
		strValue = removeLeadingSpaces(strValue);
	}
	
	// if a valid string was passed...
	if (strValue.length > 0)
	{
		// remove trailing spaces
		strValue = removeTrailingSpaces(strValue);
	}
	
	// return the trimmed string
	return strValue;
}

//-  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -'
// syntax version
	
function Trim(string) // as String
{
	return trim(string)
}

//-  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -'
// example

/*
<script type="text/javascript" language="javascript">
function validateForm1(formObj)
{
	if (Trim(formObj.FirstName.value) == "")
	{
		alert("Please enter your first name.");
		formObj.FirstName.focus();
		return false;
	}
	else
	{
		return true;
	}
}
</script>
*/

//*******************************************************************************'
// remove any leading spaces from a string

function removeLeadingSpaces(strValue) // as String
{
	// for each space at the beginning of a string
	while (strValue.substring(0, 1) == " ")
	{
		// the string = the string less 1 character at the beginning
		strValue = strValue.substring(1, strValue.length);
	}
	
	// return the new string
	return strValue;
}

//-  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -'
// syntax version

function removeleadingspaces(strValue) // as String
{
	return removeLeadingSpaces(strValue)
}

//-  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -'
// syntax version

function RemoveLeadingSpaces(strValue) // as String
{
	return removeLeadingSpaces(strValue)
}

//*******************************************************************************'
// remove any trailing spaces from a string

function removeTrailingSpaces(strValue)
{
	// for each space at the end of a string
	while (strValue.substring((strValue.length - 1), strValue.length) == " ")
	{
		// the string = the string less 1 character at the end
		strValue = strValue.substring(0, (strValue.length - 1));
	}
	
	// return the new string
	return strValue;
}

//-  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -'
// syntax version

function removetrailingspaces(strValue) // as String
{
	return removeTrailingSpaces(strValue)
}

//-  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -'
// syntax version

function RemoveTrailingSpaces(strValue) // as String
{
	return removeTrailingSpaces(strValue)
}


//*******************************************************************************'
// de-select all children of the element
// select optSelected if specified
// requires ../format/trim.js

function clearDDLSelected(elementObj, optSelected)
{
	// make sure the element is of the right object type
	if (elementObj.type == "select-one")
	{
		// clear all selected options
		for (var i = 0; i < elementObj.length; i++)
		{
			// if this child of the element is selected, de-select it
			elementObj[i].selected = false;
		}
	
		// select the option specified
		if (optSelected.length > 0)
		{
			// for each child of the element...
			for (var i = 0; i < elementObj.length; i++)
			{
				// if this child's value matches the passed value to select...
				if (Trim(elementObj[i].value) == Trim(optSelected))
				{
					// check the object
					elementObj[i].selected = true;
					
					// break out of the for loop
					break;
				}
			}
		}
		
	}
}

//*******************************************************************************'
// get a value at position X delimited by character X

function getToken(str, pos, del) // as String
{
	// define the default return value
	var strReturnValue = "" // as String
	
	// split the str into an array based on the delimeter
	var myArray = str.split(del)
	
	// if the pos value is greater than the number of array elements return ""
	if (pos > myArray.length)
	{
		strReturnValue = "";
	}
	else
	{
		// array positions start at 0, subtract 1 from cnt
		pos = pos -1;
		strReturnValue = myArray[pos];
	}
	
	// return the return value
	return strReturnValue;
}

//-  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -'
// syntax version

function gettoken(str, pos, del) // as String
{
	return getToken(str, pos, del)
}

//-  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -'
// syntax version

function GetToken(str, pos, del) // as String
{
	return getToken(str, pos, del)
}

//*******************************************************************************'
// access key=value pairs in the QueryString
// Call function by x = queryString("key") returns key's value

function queryString(key, defaultValue) // as String
{ 
	var keyValue = null; 

	// for each key/value pair...
	for (var i = 0; i < queryString.keys.length; i++) 
	{ 
		// if this key matches the one being requested...
		if (queryString.keys[i].toLowerCase() == key.toLowerCase()) 
		{ 
			// save the key value
			keyValue = queryString.values[i]; 
			
			// break from the for loop
			break; 
		} 
	}

	//if passing default value
	if(arguments.length == 2)
	{
		if(keyValue == null)
		{
			keyValue = defaultValue;
		}
	}
	
	// return the key value
	return keyValue; 
} 

//-  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -'
// syntax version

function querystring(key) // as String
{
	return queryString(key)
}

//-  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -'
// syntax version

function QueryString(key) // as String
{
	return queryString(key)
}

//*******************************************************************************'

// create the array holding the Key names
queryString.keys = new Array();

// create the array holding the Key values
queryString.values = new Array(); 

// extract the key=value pairs from the QueryString
function queryString_Parse() 
{ 
	// get the QueryString as is
	var query = window.location.search.substring(1); 
	
	// split the QueryString into it key=value pairs
	var pairs = query.split("&"); 

	// for each key=value pair...
	for (var i=0;i<pairs.length;i++) 
	{ 
		// split the value from the key based on the = character
		var pos = pairs[i].indexOf('='); 
		
		if (pos >= 0) 
		{ 
			// the name is everything before the =
			var argName = pairs[i].substring(0,pos); 
			
			// assign it to the key array
			queryString.keys[queryString.keys.length] = argName; 
			
			// the value is everything after the =
			var value = pairs[i].substring(pos+1); 
			
			// assign it to the value array in the same index as the key name
			queryString.values[queryString.values.length] = value; 
		} 
	} 
} 

// call the function to gather and split the key=value pairs
queryString_Parse(); 

/*
<script type="text/javascript" language="javascript">
x = queryString("FieldName")
if (x == "" || x == null)
{
	// do something
}
</script>
*/

//*******************************************************************************'
function adjustHref()
{
	var txtTmp = ''; // as String
	var txtURL = ''; // as String
	
	// for each link on this page...
	for (var i = 0; i <= document.links.length-1; i++)
	{
		// grab this link
		txtTmp = document.links[i];
		
		// convert to string
		txtTmp = txtTmp.toString();
		
		// if the script we are looking for is within this links href...
		if (
			txtTmp.indexOf("/country/us.html") >= 0 ||
			txtTmp.indexOf("/state/us-") >= 0 ||
			txtTmp.indexOf("/hotels/us-") >= 0 ||
			txtTmp.indexOf("/hotels/us-") >= 0 ||
			txtTmp.indexOf("/hotelinfo/") >= 0 ||
			txtTmp.indexOf("/chain/us-") >= 0 ||
			txtTmp.indexOf("/poi/") >= 0
			)
		{
			txtURL = document.links[i].href.toString();
			txtURL += "?arrivalMonth=" + queryString("arrivalMonth", "");
			txtURL += "&arrivalDay=" + queryString("arrivalDay", "");
			txtURL += "&departureMonth=" + queryString("departureMonth", "");
			txtURL += "&departureDay=" + queryString("departureDay", "");
			txtURL += "&city=" + queryString("city", "");
			txtURL += "&state=" + queryString("state", "");
			txtURL += "&arrivalDate=" + queryString("arrivalDate", "");
			txtURL += "&departureDate=" + queryString("departureDate", "");
			txtURL += "&adults=" + queryString("adults", "");
			txtURL += "&children=" + queryString("children", "");
			
			// replace the href with the new version
			document.links[i].href = txtURL;
		}
	}
}

