function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
   var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
   if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function validateLogin() {
	var username = document.login_form.username.value;
	var password = document.login_form.password.value;
	
	if (username == "") {
		alert("You must enter a username.");
		document.login_form.username.focus();
		return false;
	}
	if (password == "") {
		alert("You must enter a password.");
		document.login_form.password.focus();
		return false;
	}
	
	encodePassword(password);
}

function validateEmail(email) {
	// E-mail Validation by Henrik Petersen / NetKontoret
	// Explained at www.echoecho.com/jsforms.htm
	// Please do not remove this line and the two lines above.
	apos=email.indexOf("@"); 
	dotpos=email.lastIndexOf(".");
	lastpos=email.length-1;
	if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2) {
		return false;
	} else {
		return true;
	}
}

function encodePassword(password) {
    var hash = hex_md5(password);
	document.login_form.password.value = hash;
	return true;
}

function cancelAdmin(section) {
	document.forms[0].action = baseUrl+"/"+section;
	document.forms[0].submit();	
}

function replaceChars(entry) {
	out = "\\"; // replace this
	add = "/";  // with this
	temp = "" + entry; // temporary holder

	while (temp.indexOf(out)>-1) {
		pos= temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add + 
		temp.substring((pos + out.length), temp.length));
	}
	return temp;
}

function isNumeric(strString) {
	var strValidChars = "0123456789.";
	var strChar;
	var blnResult = true;

	if (strString.length == 0) return false;
	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++) {
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1) {
			blnResult = false;
		}
	}
	return blnResult;
}

function isPhone(strString) {
	var strValidChars = "0123456789()+- ";
	var strChar;
	var blnResult = true;

	if (strString.length == 0) return false;
	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++) {
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1) {
			blnResult = false;
		}
	}
	return blnResult;
}

function confirmDelete(path) {
	if (confirm("Are you sure want to delete this entry from the database?")) {
		window.location = baseUrl+path;
	}
}

function hideBlock(divId) {
	document.getElementById(divId).style.display = "none";
}

function showBlock(divId) {
	document.getElementById(divId).style.display = "block";
}

function valButton(btn) {
    var cnt = -1;
    for (var i=btn.length-1; i > -1; i--) {
        if (btn[i].checked) {cnt = i; i = -1;}
    }
    if (cnt > -1) {
		return btn[cnt].value;
	}  else {
		return null;
	}
}

function validateContactForm() {
	var validateFlag = false;
	
	var fullName = document.getElementById('feedbackName');
	var email = document.getElementById('feedbackEmail');
	var comments = document.getElementById('feedbackComments');
	
	if (fullName.value == "") {
		alert("You must enter your full name.");
		fullName.focus();
		validateFlag = false;
		return false;
	}
	if (email.value == "") {
		alert("You must enter your email address.");
		email.focus();
		validateFlag = false;
		return false;
	}
	if (email.value != "") {
		if (!validateEmail(email.value)) {
    		alert("You must enter a valid email address");
    	    email.focus();
			validateFlag = false;
    		return false;
    	}
	}
	if (comments.value == "") {
		alert("You must enter your comments.");
		comments.focus();
		validateFlag = false;
		return false;
	}
	
	document.mainbodyform.submit();
}

function toggleDiv(divId) {
    styleObj=document.getElementById(divId).style;
    if (styleObj.display==='none') {
        styleObj.display='';
    } else {
        styleObj.display='none';
    }
}

function stripCharacter(words,character) {
	var spaces = words.length;
	for(var x = 1; x<spaces; ++x) {
		words = words.replace(character, "");   
	}
	return words;
}

function validateReservation() {
	with (window.document.reservationform) {
		if (numNights.value== "" || isNaN(numNights.value)) {
			numNights.focus();
			alert("Please enter the number of nights in the space provided.");
			return false;
		}
		if ((doubleRooms.value == 0) && (twinRooms.value == 0) && (singleRooms.value == 0)) {
			singleRooms.focus();
			alert("Please select at least 1 room from the menus provided.");
			return false;
		}
		submit();	
	}
}

function validateReservationProcess() {
	with (window.document.mainbodyform) {
		if (enquiryName.value.length == 0) {
			enquiryName.focus();
			alert("Please enter your name in the space provided.");
			return false;
		}
		if (enquiryTel.value.length == 0) {
			enquiryTel.focus();
			alert("Please enter your telephone number in the space provided.");
			return false;
		}
		if (enquiryEmail.value.length == 0) {
			enquiryEmail.focus();
			alert("Please enter your email address in the space provided.");
			return false;
		}
		if (enquiryEmail.value.length != 0){
		  if (!validateEmail(enquiryEmail.value)){
			return false;}
		}
		submit();	
	}
}
