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 showFeaturePhoto(type) {
    newWindow = window.open(baseUrl+'/home/showFeaturePhoto?type='+type+'', 'newWindow', 'toolbar=no, location=no, scrollbars=yes, resizable=yes, width=490, height=387, left=100, top=100');
}

function showLocationPhoto(type) {
    newWindow = window.open(baseUrl+'/home/showLocationPhoto?type='+type+'', 'newWindow', 'toolbar=no, location=no, scrollbars=yes, resizable=yes, width=490, height=387, left=100, top=100');
}

function isPhoneNumber(sText) {
    var ValidChars = "0123456789.+() ";
    var IsNumber=true;
    var Char;

    if (sText.length > 0) {
        Char = sText.charAt(0);
        if (ValidChars.indexOf(Char) == -1 && Char !="-") {
            IsNumber = false;
        }
    }

    for (i = 1; i < sText.length && IsNumber == true; i++) {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
        }
    }
    return IsNumber;

}

function validateContactForm() {
    var fullName = document.contact_form.full_name.value;
	var email = document.contact_form.email.value;
	var phone = document.contact_form.phone.value;
	var query = document.contact_form.query.value;
	
	if (fullName == "") {
		alert("You must enter your name.");
		document.contact_form.full_name.focus();
		return false;
	}
    if (email == "") {
		alert("You must enter your email address.");
		document.contact_form.email.focus();
		return false;
	}
	if (phone == "") {
		alert("You must provide a phone number");
		document.contact_form.phone.focus();
		return false;
	}
	if (!isPhoneNumber(phone)) {
		alert("You must provide a valid phone number");
		document.contact_form.phone.focus();
		return false;
	}
	if (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) {
			alert("You must enter a valid email address");
				document.contact_form.email.focus();
			return false;
		}
	}
	
	if (query == "") {
		alert("Please enter a query or some comments.");
		document.contact_form.query.focus();
		return false;
	}
	return true;
}

function validateLoginForm() {
	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 previousMonth() {
    document.booking_form.action = baseUrl+"/admin";
    document.booking_form.nav_action.value = "-1";
    document.booking_form.submit();
}

function nextMonth() {
    document.booking_form.action = baseUrl+"/admin";
    document.booking_form.nav_action.value = "+1";
    document.booking_form.submit();
}

function reloadCalendar() {
    document.booking_form.action = baseUrl+"/admin";
    document.booking_form.submit();
}

function showBooking(date) {
    document.booking_form.booking_date.value = date;
    document.booking_form.submit();
}

function addBooking() {
    document.booking_form.action = baseUrl+"/admin/addBooking";
    document.booking_form.submit();
}

function confirmBooking() {
    document.booking_form.action = baseUrl+"/admin/confirmBooking";
    document.booking_form.submit();
}

function previousClientMonth() {
    document.booking_form.action = baseUrl+"/home/availability";
    document.booking_form.nav_action.value = "-1";
    document.booking_form.submit();
}

function nextClientMonth() {
    document.booking_form.action = baseUrl+"/home/availability";
    document.booking_form.nav_action.value = "+1";
    document.booking_form.submit();
}

function reloadClientCalendar() {
    document.booking_form.action = baseUrl+"/home/availability";
    document.booking_form.submit();
}

function encodePassword(password) {
    var hash = hex_md5(password);
	document.login_form.password.value = hash;
	return true;
}


