function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}

var preloadFlag = false;
function preloadImages() {
	if (document.images) {
		navHome_o = newImage("images/btn-home2.gif");
		navRooms_o = newImage("images/btn-rooms2.gif");
		navFacilities_o = newImage("images/btn-facilities2.gif");
		navLocation_o = newImage("images/btn-location2.gif");
		navReservations_o = newImage("images/btn-reservations2.gif");
		preloadFlag = true;
	}
}

// control user input

String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

String.prototype.padL = function (nLength, sChar) {
  var sreturn = this;
  while (sreturn.length < nLength) {
    sreturn = String(sChar) + sreturn;
  }
  return sreturn;
}
              
function date_onkeydown() {
  if (window.event.srcElement.readOnly) return;
  var key_code = window.event.keyCode;
  var oElement = window.event.srcElement;
  if (window.event.shiftKey && String.fromCharCode(key_code) == "T") {
        var d = new Date();
        oElement.value = String(d.getMonth() + 1).padL(2, "0") + "/" +
                         String(d.getDate()).padL(2, "0") + "/" +
                         d.getFullYear();
        window.event.returnValue = 0;
    }
    if (!window.event.shiftKey && !window.event.ctrlKey && !window.event.altKey) {
        if ((key_code > 47 && key_code < 58) ||
          (key_code > 95 && key_code < 106)) {
            if (key_code > 95) key_code -= (95-47);
            oElement.value =
                oElement.value.replace(/[mdy]/, String.fromCharCode(key_code));
        }
        if (key_code == 8) {
            if (!oElement.value.match(/^[mdy0-9]{2}\/[mdy0-9]{2}\/[mdy0-9]{4}$/))
                oElement.value = "mm/dd/yyyy";
            oElement.value = oElement.value.replace(/([mdy\/]*)[0-9]([mdy\/]*)$/,
                function ($0, $1, $2) {
                    var idx = oElement.value.search(/([mdy\/]*)[0-9]([mdy\/]*)$/);
                    if (idx >= 5) {
                        return $1 + "y" + $2;
                    } else if (idx >= 2) {
                        return $1 + "d" + $2;
                    } else {
                        return $1 + "m" + $2;
                    }
                } );
            window.event.returnValue = 0;
        }
    }
    if (key_code != 9) {
        event.returnValue = false;
    }
}

//validate reservation form
function validate(){
	missinginfo = "";
	if (document.forms[1].Name.value == "") {
		missinginfo += "\n   -  Name";
	}
	if (document.forms[1].Address.value == "") {
		missinginfo += "\n   -  Address";
	}
	if (document.forms[1].City.value == "") {
		missinginfo += "\n   -  City";
	}
	if (document.forms[1].Province.value == "") {
		missinginfo += "\n   -  State/Province";
	}
	if (document.forms[1].Country.value == "") {
		missinginfo += "\n   -  Country";
	}
	if (document.forms[1].Arrival.value == "") {
		missinginfo += "\n   -  Arrival Date";
	}
	if (document.forms[1].Duration.value == "") {
		missinginfo += "\n   -  Duration";
	}
	if (document.forms[1].Guests.value == "") {
		missinginfo += "\n   -  Guests";
	}
	
	if ((document.forms[1].email.value == "") ||
		(document.forms[1].email.value.indexOf('@') == -1) ||
		(document.forms[1].email.value.indexOf('.') == -1)) {
			missinginfo += "\n   -  Email Address";
	}
	if (missinginfo != "") {
		missinginfo ="_____________________________\n" +
		"You failed to correctly fill in your:\n" +
		missinginfo + "\n_____________________________" +
		"\nPlease re-enter and submit again!";
		alert(missinginfo);
		return false;
	}
	else return true;
}

// validate subscription mailing list

function validate_form(form) {
  validity = true; // assume valid

  EmailField = form.email.value;
  FirstNameField = form.first_name.value;

  // Check for a valid email address
  if (validity && !check_email(EmailField)) {
    validity = false;
    alert('Error: Email address is invalid!');
  }

  // Check first name field entered
  if (validity && !check_empty(FirstNameField)) {
    validity = false;
    alert('Error: You have not given us your first name');
  }

  return validity;
}

function check_empty(text) {
  return (text.length > 0); // returns false if empty
}

function check_email(address) {
  if ((address == "")
    || (address.indexOf ('@') == -1)
    || (address.indexOf ('.') == -1))
      return false;
  return true;
}

// pop up script
var newWin = null;
function popUp(strURL, strType, strHeight, strWidth) {
	if (newWin != null && !newWin.closed)
		newWin.close();
	var strOptions="";
	if (strType=="console")
		strOptions="resizable,height="+strHeight+",width="+strWidth;
	if (strType=="fixed")
		strOptions="status,height="+strHeight+",width="+strWidth;
	if (strType=="elastic")
		strOptions="scrollbars,"+"resizable,height="+strHeight+",width="+strWidth;
	newWin = window.open(strURL, 'newWin', strOptions);
	newWin.focus();
}