var browser_IE;
var browser_NS4;
var browser_W3C;
var platForm;
var isMac;
var defaultNumOfAdults=1;
var defaultNumOfRooms=1;
var defaultNumOfKids=0;


browser_IE  = (document.all && navigator.appName == 'Microsoft Internet Explorer') ? true : false;
browser_NS4 = (document.layers) ? true : false;
browser_W3C = (!document.all && document.getElementById) ? true : false;

platForm=navigator.platform;
isMac=platForm.indexOf("Mac");


// Trims a string
function trim(str) {
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

function validateSearchForm(theForm)
{
    var today = formatDate(new Date(),"M/d/yyyy")
    var checkInDate = trim(theForm.CheckInDate.value);
    var checkOutDate = trim(theForm.CheckOutDate.value);
    

	if(trim(theForm.CityName.value)=="")
	{
		alert("Please enter the city name");
		return false;
	}

	if(checkInDate != "")
	{

		if( isDate(checkInDate,"M/d/yyyy") == false)
		{
			alert("Please enter valid check-in date ")
            theForm.CheckInDate.value = "";
			return false;
		}
		if (checkOutDate=="")
		{
			alert("Please enter check-out date");
			return false;
		}
	}
	if(checkOutDate != "")
	{
		if( isDate(checkOutDate,"M/d/yyyy") == false)
		{
			theForm.CheckOutDate.value = "";
            alert("Please enter valid check-out date ")
			return false;
		}
		if (trim(checkInDate)=="")
		{
			alert("Please enter check-In date");
			return false;
		}
	}
    

	if(checkInDate != "" || checkOutDate != "")
	{
		theForm.CheckAvailability.value="Y"
        if( 0!=(compareDates(today,"M/d/yyyy",checkInDate  ,"M/d/yyyy")))
        {
            alert("Check-in date you have chosen has already passed")
            theForm.CheckInDate.value = "";
            return false;
        }
        if( 0==(compareDates(checkOutDate,"M/d/yyyy",checkInDate  ,"M/d/yyyy")))
        {
            alert("Please choose a check-out date which is after your check-in date")
            theForm.CheckOutDate.value = "";
            return false;
        }

	}
	else{
		theForm.CheckAvailability.value="N"
	}
    if(theForm.NumOfRooms)
    {
        var rooms;
        if(theForm.NumOfRooms.type=="select-one")
        {
            rooms = theForm.NumOfRooms.options[theForm.NumOfRooms.selectedIndex].value

        }
        else if(theForm.NumOfRooms.type=="hidden")
        {

            rooms=theForm.NumOfRooms.value

        }
        SetCookie ("NumOfRooms",rooms)

    }
    if(theForm.NumOfAdults)
    {
        if(theForm.NumOfAdults.type=="select-one")
        {
            adults=theForm.NumOfAdults.options[theForm.NumOfAdults.selectedIndex].value
        }
        else if(theForm.NumOfAdults.type=="hidden")
        {
            adults=theForm.NumOfAdults.value
        }
        
        SetCookie ("NumOfAdults", adults)
    }
    
    if(theForm.NumOfKids)
    {
        var kids;
        if(theForm.NumOfKids.type=="select-one")
        {
            kids = theForm.NumOfKids.options[theForm.NumOfKids.selectedIndex].value

        }
        else if(theForm.NumOfKids.type=="hidden")
        {

            kids=theForm.NumOfKids.value

        }
        SetCookie ("NumOfKids",kids)

    }
	if(theForm.StateCode)
	{
	    var state;
        if(theForm.StateCode.type=="select-one")
        {
            state = theForm.StateCode.options[theForm.StateCode.selectedIndex].value

        }
        else if(theForm.StateCode.type=="hidden")
        {
            state=theForm.StateCode.value
        }
		
        SetCookie ("StateCode",state)
	}
	if(theForm.CountryCode)
	{
	        var country;
        if(theForm.CountryCode.type=="select-one")
        {
            country = theForm.CountryCode.options[theForm.CountryCode.selectedIndex].value

        }
        else if(theForm.CountryCode.type=="hidden")
        {
            country=theForm.CountryCode.value
        }
		
		SetCookie ("CountryCode",country)
	}	

	theForm.CheckInDate.value = checkInDate;
	theForm.CheckOutDate.value = checkOutDate;
    SetCookie ("CheckInDate",checkInDate)
    SetCookie ("CheckOutDate",checkOutDate)
	theForm.CityName.value =trim(theForm.CityName.value);
	return true;

}

function populateFormElement(element, theValue, theDefaultValue)
{

    if (theValue == null)
    {
        theValue=theDefaultValue
    }
    elementType= element.type;
    if (element.type=="select-one")
    {

        len = element.length
        for (i=0;i<len ;i++ )
         {
             if(element.options[i].value == theValue)
             {
                element.options[i].selected = true;
             }
         }
    }
    if(element.type=="hidden")
    {
        element.value=theValue
    
    }
}


function validateAvailForm(theForm)
{
	var today = formatDate(new Date(),"M/d/yyyy")
    var checkInDate = trim(theForm.CheckInDate.value);
    var checkOutDate = trim(theForm.CheckOutDate.value);

    if(checkInDate != "")
	{

		if( isDate(checkInDate,"M/d/yyyy") == false)
		{
			alert("Please enter valid check-in date ")
			return false;
		}
		if (checkOutDate=="")
		{
			alert("Please enter check-out date");
			return false;
		}
	}
	else
	{
		alert("Please enter the check-in date")
		return false;
	}

	if(checkOutDate != "")
	{
		if( isDate(checkOutDate,"M/d/yyyy") == false)
		{
			alert("Please enter valid check-out date ")
			return false;
		}
		if (checkInDate=="")
		{
			alert("Please enter check-In date");
			return false;
		}
	}
	else
	{
		alert("Please enter the check-out date");
		return false;
		
	}
	
	if(checkInDate != "" || checkOutDate != "")
	{
        if( 0!=(compareDates(today,"M/d/yyyy",checkInDate  ,"M/d/yyyy")))
        {
            alert("Check-in date you have chosen has already passed")
            theForm.CheckInDate.value = "";
            return false;
        }
        if( 0==(compareDates(checkOutDate,"M/d/yyyy",checkInDate  ,"M/d/yyyy")))
        {
            alert("Please choose a check-out date which is after your check-in date")
            theForm.CheckOutDate.value = "";
            return false;
        }

	}
    if(theForm.NumOfRooms)
    {
        var rooms;
        if(theForm.NumOfRooms.type=="select-one")
        {
            rooms = theForm.NumOfRooms.options[theForm.NumOfRooms.selectedIndex].value

        }
        else if(theForm.NumOfRooms.type=="hidden")
        {

            rooms=theForm.NumOfRooms.value

        }
        SetCookie ("NumOfRooms",rooms)

    }

    if(theForm.NumOfAdults)
    {
        if(theForm.NumOfAdults.type=="select-one")
        {
            adults=theForm.NumOfAdults.options[theForm.NumOfAdults.selectedIndex].value
        }
        else if(theForm.NumOfAdults.type=="hidden")
        {
            adults=theForm.NumOfAdults.value
        }
        
        SetCookie ("NumOfAdults", adults)
    }
    if(theForm.NumOfKids)
    {
        var kids;
        if(theForm.NumOfKids.type=="select-one")
        {
            kids = theForm.NumOfKids.options[theForm.NumOfKids.selectedIndex].value

        }
        else if(theForm.NumOfKids.type=="hidden")
        {

            kids=theForm.NumOfKids.value

        }
        SetCookie ("NumOfKids",kids)

    }

	theForm.CheckInDate.value = checkInDate;
	theForm.CheckOutDate.value = checkOutDate
    SetCookie ("CheckInDate",checkInDate)
    SetCookie ("CheckOutDate",checkOutDate)
	return true;

}

function onError(form_object, input_object, object_value, error_message)
{
	alert(error_message);
	return false;	
}

function requiredField(obj, obj_type)
{
	if (obj_type == "TEXT" || obj_type == "PASSWORD")
	{
        
		if (trim(obj.value).length == 0) 
	  		return false;
		else 
	  		return true;
	}
	else if (obj_type == "SELECT")
	{
		for (i=0; i < obj.length; i++)
		{
			if (obj.options[i].selected)
				return true;
		}
	   	return false;	
	}
	else if (obj_type == "SINGLE_VALUE_RADIO" || obj_type == "SINGLE_VALUE_CHECKBOX")
	{
		if (obj.checked)
			return true;
		else
	   		return false;	
	}
	else if (obj_type == "RADIO" || obj_type == "CHECKBOX")
	{
		for (i=0; i < obj.length; i++)
		{
			if (obj[i].checked)
				return true;
		}
	   	return false;	
	}
}



function checkStrLength(object_value, max_value)
{
	len = object_value.length
	
	if(len >= max_value)
		return false
	else
		return true

}


function checkregex(object_value, regex)
{
	return regex.test(object_value);
}

function getRadioButtonValue(theElement)
{ 
    var j = theElement.length;


    for(var i=0; i<j; i++)
    {
           if(theElement[i].checked)
        {
            theValue = theElement[i].value;
            return theValue;
        }
    }
    if (!theElement.length)
    {
	theValue=theElement.value
	return theValue;
    }
    return null;

}

function clearRadioButtons(theElement)
{
    var j = theElement.length;
    for(var i=0; i<j; i++)
    {
         theElement[i].checked = false
        
    }
    if (!theElement.length)
     {
		theElement.checked=false;
     }
}

function populateSearchFields(theForm)
{
    if(GetCookie("CheckInDate") != null)
    {
    theForm.CheckInDate.value = GetCookie("CheckInDate")
    }
    if(GetCookie("CheckOutDate") != null)
    {
    theForm.CheckOutDate.value = GetCookie("CheckOutDate")
    }
    if(theForm.NumOfAdults) 
    {
       populateFormElement(theForm.NumOfAdults, GetCookie("NumOfAdults"), defaultNumOfAdults)
    }
    if (theForm.NumOfRooms)
    {
        populateFormElement(theForm.NumOfRooms, GetCookie("NumOfRooms"), defaultNumOfRooms)
    }
    if (theForm.NumOfKids)
    {
        populateFormElement(theForm.NumOfKids, GetCookie("NumOfKids"), defaultNumOfKids)
    }
	if (theForm.StateCode)
    {
		if(GetCookie("StateCode") !=null)
		{
			populateFormElement(theForm.StateCode, GetCookie("StateCode"))
		}
    }
	if (theForm.CountryCode)
    {
        if(GetCookie("CountryCode") !=null)
		{
			populateFormElement(theForm.CountryCode, GetCookie("CountryCode"))
		}
    }
}

function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; 
  }
  return null;
}

function SetCookie (name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    //((path) ? "; path=" + path : "") +
     "; path=/" +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}


function DeleteCookie (name,path,domain) {
  if (GetCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

function bookmark(url, description)
{
    netscape="Netscape User's hit CTRL+D to add a bookmark to this site."
    if (browser_IE)
    {
      window.external.AddFavorite(url, description);
    }
    else if (browser_NS4)
    {
        alert(netscape);
    }
}
//this function used by the home page, should be removed later
function populateDatesFields(theForm)
{
    if(GetCookie("CheckInDate") != null)
    {
    theForm.CheckInDate.value = GetCookie("CheckInDate")
    }
    if(GetCookie("CheckOutDate") != null)
    {
    theForm.CheckOutDate.value = GetCookie("CheckOutDate")
    }
}

