//On load Event Handler Functions
//Enables a dev to just register functions to run once the window has finished loading
var OnLoadEvents = new Array();

//Browser Checks
var browser = navigator.userAgent.toLowerCase();
isGecko = (browser .indexOf("gecko") != -1);
isSafari = (browser .indexOf("safari") != -1);
isKonqueror = (browser.indexOf("konqueror") != -1);

//Main two checks used
var isIE	= (navigator.appName == "Microsoft Internet Explorer" && window.print);
var isBrowserDOM = (document.getElementById && document.getElementsByTagName);
//    alert('Register called with '+func);
function RegisterOnLoadEvent(func){
	OnLoadEvents[OnLoadEvents.length] = func;
}

//    alert('Onload called ('+OnLoadEvents.length+' items)');
window.onload = function(){
	if (OnLoadEvents.length == 0)
		return;
	
	for (i=0; i < OnLoadEvents.length; i++)
	{
		OnLoadEvents[i]();
	}
}; 

//helper function to make sure document.getElementById exists
//be nice and give a fallback to older IEs
function GetElementById (id){
	if (document.getElementById)
		return document.getElementById(id);
	else if (document.all)
		return document.all[id];
	else
		return null;
}

//Nice function to hide Select input boxes when in IE
//Thank you Sean Inman
function SI_toggleSelects(state) 
{
	if (isIE || isSafari) 
	{
	   // alert('Toggling');
		for (var i = 0; (sel = document.getElementsByTagName('select')[i]); i++)
		{
			sel.style.visibility = state;
			if(state == 'hidden')
			    alert('hidden');
//			    sel.style.display = 'none';
//			else
//			    sel.style.display = 'inline';
		}
	}
}

function OnEnter(sender, evt, targetId, targetEvent){
	//get event object
		
	evt = (evt) ? evt : ((event) ? event : null);
	if (!evt) return;	

	//make sure the enter key was pressed
	if (evt.keyCode != 13) return;

	//execute target function
	setTimeout("var t = GetElementById('" + targetId + "'); if (t && t." + targetEvent + ") t." + targetEvent + "();", 10);
	//setTimeout(new function(){eval("var t = GetElementById('" + targetId + "'); if (t) t." + targetEvent + "();");}, 100);
	
	return false;
}

//Helper function to dtermine if the browser supports Ajax
function SupportsAjax(){
	//lame, but check if IE and older than 6
	var ua = navigator.appVersion;
	var IEOffset = ua.indexOf("MSIE");
	if (IEOffset >= 0)
		if (parseFloat(ua.substring(IEOffset + 5, ua.indexOf(";", IEOffset))) < 6)
			return false;
			
	if (!document.getElementById) return false;
	if (typeof(XMLHttpRequest) != "undefined") return true;
	if (typeof new ActiveXObject('Microsoft.XMLHTTP') != "undefined") return true;
	if (typeof new ActiveXObject('Msxml2.XMLHTTP') != "undefined") return true;
	
	return false;
}


//Simple helper function to hide or show a specified element
function SetDisplay(elementId, show){
	var element = GetElementById(elementId);	
	SetClasses(show ? 'remove' : 'add', element, 'hidden');		
}

function SetClasses(action, element,class1,class2)
{
  switch (action){
    case 'swap':
      element.className=!SetClasses('check',o,class1)?element.className.replace(class2,class1) : element.className.replace(class1,class2);
    break;
    case 'add':
      if(!SetClasses('check',element,class1)){element.className+=element.className?' '+class1:class1;}
    break;
    case 'remove':
      var rep=element.className.match(' '+class1)?' '+class1:class1;
      element.className=element.className.replace(rep,'');
    break;
    case 'check':
      return new RegExp('\\b'+class1+'\\b').test(element.className)
    break;
  }
}

function SetValue(ctrlId, value){
	var ctrl = GetElementById(ctrlId);
	if (!ctrl) return;
	
	ctrl.value = value;
}


//Object that manages the search control state on most pages
State = {
	IsModernBrowser: false,
	SearchMode: "Destination",
	
	CheckModernBrowser: function(){
		State.IsModernBrowser = SupportsAjax();
		if (typeof SaveState == "function") SaveState();
	},
	
	SwitchMode: function (){
		State.SearchMode = (State.SearchMode == "Destination") ? "PropertyName" : "Destination";
		if (typeof SaveState == "function") SaveState();
	},
	
	Set: function(elementId, value){
		var element = GetElementById(elementId);
		if (element) element.value = value;
	}
}


function ShowElement(elementName){
	var ele = GetElementById(elementName);
	if (!ele) return;
	ele.style.display = "";
}

function HideElement(elementName){
	var ele = GetElementById(elementName);
	if (!ele) return;
	ele.style.display = "none";
}

/*Event Util*/

function Util_FormatDate_YYYYMMDD(date) {
    return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
   }

   function Util_FormatDate_DDMMYYYY(date) {
   	return date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear();
   }

function Util_FormatDate_DDMMMMYYYY(date) {
    var month = new Array(12);
    month[0] = "January";
    month[1] = "February";
    month[2] = "March";
    month[3] = "April";
    month[4] = "May";
    month[5] = "June";
    month[6] = "July";
    month[7] = "August";
    month[8] = "September";
    month[9] = "October";
    month[10] = "November";
    month[11] = "December";


    return date.getDate() + ' ' + month[date.getMonth()] + ' ' + date.getFullYear();
}


function Util_ParseDate(datetime) {
    if (datetime.toLowerCase() == 'today') {
        return Util_Today();
    }

    if (datetime.toLowerCase() == 'tomorrow') {
        var tomorrow = Util_Today();
        tomorrow.setDate(tomorrow.getDate() + 1);
        return tomorrow;
    }

    var DDMMYY = datetime.split('/');
    if (DDMMYY.length == 3) {
        if (DDMMYY[2] < 100)
            DDMMYY[2] = "20" + DDMMYY[2];

        datetime = DDMMYY[1] + '/' + DDMMYY[0] + '/' + DDMMYY[2];
    }

    var DDMMMMYY = datetime.split(' ');
    if (DDMMMMYY.length == 3) {
        if (DDMMMMYY[2] < 100)
            DDMMMMYY[2] = "20" + DDMMMMYY[2];

        datetime = DDMMMMYY[0] + ' ' + DDMMMMYY[1] + ' ' + DDMMMMYY[2];
    }

    return Date.parse(datetime);
}

function Util_Today() {
    var today = new Date();
    today.setTime(Util_ParseDate(Util_FormatDate_DDMMMMYYYY(today)));
    return today;
}

function Util_DateIsValid(value) {
    var date = new Date();
    date.setTime(Util_ParseDate(value));

    var today = Util_Today();
    if (isNaN(date) || date.getTime() - today.getTime() < 0) {
        return false;
    }

    return true;
}

function Util_UpdateDateOnBlur(sender) {
    var date = new Date();
    date.setTime(Util_ParseDate($get(sender).value));
    if (!isNaN(date)) {
        $get(sender).value = Util_FormatDate_DDMMMMYYYY(date);
    }
}


function AmenityFilter_ValidateDate(sender, args) {
    sender.errormessage = "An invalid date was entered. Please make sure the date is in the correct format and that the date is not in the past";
    args.IsValid = Util_DateIsValid(args.Value);
}


function AmenityFilter_CompareDate(val) {
    if (ValidatorGetValue(val.controltovalidate) == "")
        return true;

    return Util_ParseDate(ValidatorGetValue(val.controltovalidate)) >= Util_ParseDate(ValidatorGetValue(val.controltocompare));
}


