var browser_IE;
var browser_NS4;
var browser_W3C;
var platForm;
var isMac;
var defaultNumOfAdults=1;
var defaultNumOfRooms=1;
var defaultNumOfKids=0;
var defaultAge="N/A"


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");

rnd.today=new Date();
rnd.seed=rnd.today.getTime();

function rnd() {
	rnd.seed = (rnd.seed*9301+49297) % 233280;
	return rnd.seed/(233280.0);
}

function rand(number) {
	return Math.ceil(rnd()*number);
}


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

// translate date format from yyyy-mm-dd to Date
function splitDate(inputdate,separator) {
  arrayOfStrings = inputdate.split(separator);
  outputdate= new Date(arrayOfStrings[0],arrayOfStrings[1],arrayOfStrings[2]);
  return outputdate;
}

function setHomeFunction(){
	if (navigator.appName == "Netscape") {
		alert("To set ctv.ca as your home page, you must:\n1. Click OK to close this box\n2. Click the 'Edit' menu\n3. Click the 'Preferences' menu item\n4. Click the 'Use Current Page' button.");
	} else {
		if (document.all) {
			homeLink.style.behavior='url(#default#homepage)';
			homeLink.setHomePage('http://www.ctv.ca');
		}
	}
}

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(!validateKidsAge(theForm, kids)){
			alert("Please enter the age of each child")
			return false;
		}


    }

	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)
		if(!validateKidsAge(theForm, kids)){
			alert("Please enter the age of each child")
			return false;
		}


    }

	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 checkday(checkYear, checkMonth, checkDay)
{
	maxDay = 31;

	if (checkMonth == 4 || checkMonth == 6 ||
		checkMonth == 9 || checkMonth == 11)
		maxDay = 30;
	else if (checkMonth == 2)
	{
		if (checkYear % 4 > 0)
			maxDay =28;
		else if (checkYear % 100 == 0 && checkYear % 400 > 0)
			maxDay = 28;
		else
			maxDay = 29;
	}

	return checkrange(checkDay, 1, maxDay);
}




function numberrange(object_value, min_value, max_value)
{
	if (min_value != null)
	{
		if (object_value < min_value)
			return false;
	}

	if (max_value != null)
	{
		if (object_value > max_value)
			return false;
	}

	return true;
}


function checknumber(object_value)
{
	if (object_value.length == 0)
		return true;

	var start_format = " .+-0123456789";
	var number_format = " .0123456789";
	var check_char;
	var decimal = false;
	var trailing_blank = false;
	var digits = false;

	check_char = start_format.indexOf(object_value.charAt(0));

	if (check_char == 1)
		decimal = true;
	else if (check_char < 1)
		return false;

	for (var i = 1; i < object_value.length; i++)
	{
		check_char = number_format.indexOf(object_value.charAt(i));
		if (check_char < 0)
			return false;
		else if (check_char == 1)
		{
			if (decimal)
				return false;
			else
				decimal = true;
		}
		else if (check_char == 0)
		{
			if (decimal || digits)	
				trailing_blank = true;
		}
		else if (trailing_blank)
			return false;
		else
			digits = true;
	}	

	return true
}


function checkrange(object_value, min_value, max_value)
{
	if (object_value.length == 0)
		return true;

	if (!checknumber(object_value))
		return false;
	else
		return (numberrange((eval(object_value)), min_value, max_value));

	return true;
}

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

}

function setFormParam( strFormName, strParamName, strParamValue )
{
	var strObjName = "document." + strFormName + "." + strParamName;
	var obj = eval( strObjName );
	obj.value = strParamValue;
	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;
        }
    }
    return null;

}

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

    for(var i=0; i<j; i++)
    {
         theElement[i].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)
		populateFormElement(theForm.KidAge_0, GetCookie("KidAge_0"), defaultAge)
		populateFormElement(theForm.KidAge_1, GetCookie("KidAge_1"), defaultAge)
		populateFormElement(theForm.KidAge_2, GetCookie("KidAge_2"), defaultAge)
		populateFormElement(theForm.KidAge_3, GetCookie("KidAge_3"), defaultAge)

    }
}

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

function FixCookieDate (date) {
  var base = new Date(0);
  var skew = base.getTime(); // dawn of (Unix) time - should be 0
  if (skew > 0)  // Except on the Mac - ahead of its time
    date.setTime (date.getTime() - skew);
}

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);
    }
}
function validateEmail(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){ return false }

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)	{  return false	}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){ return false }

		 if (str.indexOf(at,(lat+1))!=-1){ return false }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){ return false }

		 if (str.indexOf(dot,(lat+2))==-1){ return false }
		
		 if (str.indexOf(" ")!=-1){ return false }

 		 return true					
	}

//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")
    }
}

function showLayer(lay) {
	if (browser_IE) {		
		document.all[lay].style.visibility = "visible";
		document.all[lay].style.display = "block";
	} 
	if (browser_NS4) {
		document.layers[lay].visibility = "show";
	}
	if (browser_W3C) {
		document.getElementById([lay]).style.visibility = "visible";
		document.getElementById([lay]).style.display = "block";			
	}
}

function hideLayer(lay) {
	if (browser_IE) {			
		document.all[lay].style.visibility = "hidden";
		document.all[lay].style.display = "none";
	}
	if (browser_NS4) {
		document.layers[lay].visibility = "hide";
	}
	if (browser_W3C) {
		document.getElementById([lay]).style.visibility = "hidden";
		document.getElementById([lay]).style.display = "none";			
	}
}

function showKidsAgeLayer(frm, layerPrefix){
	 num = frm.NumOfKids.options[frm.NumOfKids.selectedIndex].value

    for(n=0;n<4;n++){
		hideLayer(layerPrefix+n)
	}

	for(i=0;i<num;i++){
		
		showLayer(layerPrefix+i);
	}

}
function validateKidsAge(frm, kids){

	theForm=frm
	var age0;
		
		if(kids>0){
			if(theForm.KidAge_0.type=="select-one")
			{

				age0 = theForm.KidAge_0.options[theForm.KidAge_0.selectedIndex].value
								

			}
			else if(theForm.KidAge_0.type=="hidden")
			{
				age0=theForm.KidAge_0.value
			}
			if(age0=="N/A"){
				return false;
			}
				SetCookie("KidAge_0", age0);
		}
		var age1;
		if(kids>1){
			if(theForm.KidAge_1.type=="select-one")
			{
				age1 = theForm.KidAge_1.options[theForm.KidAge_1.selectedIndex].value

			}
			else if(theForm.KidAge_1.type=="hidden")
			{
				age1=theForm.KidAge_1.value
			}
			if(age1=="N/A"){
				return false;
			}
				SetCookie("KidAge_1", age1);
		}
		var age2;
		if(kids>2){
			if(theForm.KidAge_2.type=="select-one")
			{
				age2 = theForm.KidAge_2.options[theForm.KidAge_2.selectedIndex].value

			}
			else if(theForm.KidAge_2.type=="hidden")
			{
				age2=theForm.KidAge_2.value
			}
			if(age2=="N/A"){
				return false;
			}
				SetCookie("KidAge_2", age2);
		}

		var age3;
		if(kids>3){
			if(theForm.KidAge_3.type=="select-one")
			{
				age3 = theForm.KidAge_3.options[theForm.KidAge_3.selectedIndex].value

			}
			else if(theForm.KidAge_3.type=="hidden")
			{
				age3=theForm.KidAge_3.value
			}
			if(age3=="N/A"){
				return false;
			}
				SetCookie("KidAge_3", age3);

		}
		return true;

}
function makeAgeList(){
	document.write("<option value='N/A'>?</option>");
	for(i=0;i<18;i++){
		document.write("<option value='" + i +"'>"+ i + "</option>");
	}
}