		
addDOMLoadEvent = (function(){
    // create event function stack
    var load_events = [],
        load_timer,
        script,
        done,
        exec,
        old_onload,
        init = function () {
            done = true;

            // kill the timer
            clearInterval(load_timer);

            // execute each function in the stack in the order they were added
            while (exec = load_events.shift())
                exec();

            if (script) script.onreadystatechange = '';
        };

    return function (func) {
        // if the init function was already ran, just run this function now and stop
        if (done) return func();

        if (!load_events[0]) {
            // for Mozilla/Opera9
            if (document.addEventListener)
                document.addEventListener("DOMContentLoaded", init, false);

            // for Internet Explorer
            /*@cc_on @*/
            /*@if (@_win32)
                document.write("<script id=__ie_onload defer src=//0><\/scr"+"ipt>");
                script = document.getElementById("__ie_onload");
                script.onreadystatechange = function() {
                    if (this.readyState == "complete")
                        init(); // call the onload handler
                };
            @end @*/

            // for Safari
            if (/WebKit/i.test(navigator.userAgent)) { // sniff
                load_timer = setInterval(function() {
                    if (/loaded|complete/.test(document.readyState))
                        init(); // call the onload handler
                }, 10);
            }

            // for other browsers set the window.onload, but also execute the old window.onload
            old_onload = window.onload;
            window.onload = function() {
                init();
                if (old_onload) old_onload();
            };
        }

        load_events.push(func);
    }
})();


function showForm() {
	if (document.getElementById('showform_button') && document.getElementById('showform_button').className != 'none') {
		var button = document.getElementById('showform_button');
		var button2 = document.getElementById('showform_button2');
		button.onclick = function(){
			if (button.className == 'hidden') {
				button.className = '';
				if (document.getElementById('showform_button2')) button2.className = '';
				button.firstChild.src = 'images/button-ShowForm.png';
				document.getElementById('findhotelform').className = '';
			} else {
				button.className = 'hidden';
				if (document.getElementById('showform_button2')) button2.className = 'hidden';
				button.firstChild.src = 'images/button-HideForm.png';
				document.getElementById('findhotelform').className = 'visible';
			}
		}
		if (document.getElementById('showform_button2')) button2.onclick = button.onclick;
	}
}

function hideDateFormat() {
	if(document.getElementById('checkInDate')) {
		hideFormat(document.getElementById('checkInDate'));
	}
	if(document.getElementById('checkOutDate')) {
		hideFormat(document.getElementById('checkOutDate'));
	}
}

function hideFormat(id) {
	id.onfocus = function() {
		if(id.value=='mm/dd/yyyy') id.value='';
		displayCalendar(id,'mm/dd/yyyy',this);
	}
	id.onblur = function() {
		if(id.value=='') id.value='mm/dd/yyyy';
	}
}

function showStates(load,default_state,strState,strProvince) {
	if(document.getElementById('country') && document.getElementById('state') && !load) {
		var country = document.getElementById('country');
		var state = document.getElementById('state');
		country.onchange = function() {
				state.options.length = '0';
			if(country.value ==  '0001') {
				for(var i=0; i < US_States.length; i++) {
					state.options[i] = new Option(US_States[i],US_States[i]);
					document.getElementById('stateitem').style.visibility = 'visible';
					document.getElementById('statelabel').innerHTML = 'State:';
				}
			} else if(country.value == '0260') {
				for(var i=0; i < CA_States.length; i++) {
					state.options[i] = new Option(CA_States[i],CA_States[i]);
					document.getElementById('stateitem').style.visibility = 'visible';
					document.getElementById('statelabel').innerHTML = 'Province:';
				}
			}
		}
	} else {
		var country = document.getElementById('country');
		var state = document.getElementById('state');
		var selected = false;
		if(country.value ==  '0001') {
			for(var i=0; i < US_States.length; i++) {
				selected=false;
				if (default_state == US_States[i]) selected = true;
				state.options[i] = new Option(US_States[i],US_States[i],selected);
				document.getElementById('stateitem').style.visibility = 'visible';
				document.getElementById('statelabel').innerHTML = 'State:';
			}
		} else if(country.value == '0260') {
			for(var i=0; i < CA_States.length; i++) {
				selected=false;
				if (default_state == CA_States[i]) selected = true;
				state.options[i] = new Option(CA_States[i],CA_States[i],selected);
				document.getElementById('stateitem').style.visibility = 'visible';
				document.getElementById('statelabel').innerHTML = 'Province:';
			}
		}
	}
}

function doPCRCheck() {
	var field = document.getElementById('yourpcr');
	if (field) {
		field.onkeypress = function(e){
			if (!e) e = window.event;
			var unicode=e.charCode? e.charCode : e.keyCode;
			
			if (field.value.length >= 9) {
				if ((unicode!=8) && (unicode != 9) && (unicode != 46) && (unicode != 37) && (unicode != 38) && (unicode != 39) && (unicode != 40)) { //if not a number		
					return false; //disable key press
				}
			}/* else {
				if (unicode != 8 && unicode != 46 && (unicode < 37 || unicode > 57)) {
					return false;
				}
			}*/
		}
	}
}

function buildWordCount(){
	
	if (document.getElementById('telluswhy')) {
		var field = document.getElementById('telluswhy');
		var display = document.getElementById('wordsDisplay');
		field.onkeypress = function(e){
			if (!e) e = window.event;
			var unicode=e.charCode? e.charCode : e.keyCode;	
			
			if (field.value != '') {
				var words = field.value.replace(/\s+/g, " ").replace(/^\s*/, "").replace(/\s*$/, "").split(" ").length;
				display.innerHTML = 200 - words;
				
				/*if (words >= 200) {
					if (unicode == 32 || unicode == 13) {
						display.innerHTML+= ' <span class="warning"><b>Warning:</b> You have reached 200 words.</span>';
						return false; //disable key press
					}
				}*/
			}
			else {
				display.innerHTML = 200;
			}			
		}
		
		if (field.value != '') {
			var words = field.value.replace(/\s+/g, " ").replace(/^\s*/, "").replace(/\s*$/, "").split(" ").length;
			display.innerHTML = 200 - words;
		}
	}
	
}

function getWordCount(value) {
    return value.replace(/\s+/g, " ")
               .replace(/^\s*/, "")
               .replace(/\s*$/, "")
               .split(" ").length;
}


function clearInput(e) {
	e = document.getElementById(e);
	e.value = '';
}

addDOMLoadEvent(showForm);
addDOMLoadEvent(hideDateFormat);
//addDOMLoadEvent(showStates);
addDOMLoadEvent(buildWordCount);
//addDOMLoadEvent(doPCRCheck);

var US_States = new Array('','AL','AK','AZ','AR','CA','CO','CT','DE','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','RI','SC','SD','TN','TX','UT','VT','VA','WA','WV','WI','WY');
var CA_States = new Array('','AB','BC','MB','NL','NS','ON','PE','QC','SK','YT');
	
