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 encodeDetailsPassword(password) {
    var hash = hex_md5(password);
	document.details_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 validateLoginForm() {
	var email = document.login_form.email.value;
	var password = document.login_form.password.value;

	if (email == "") {
		alert("You must supply your username.");
		document.login_form.email.focus();
		return false;
	}
	// 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.
    if (email != "") {
        apos=email.indexOf("@");
        dotpos=email.lastIndexOf(".");
        lastpos=email.length-1;
        if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2) {
            alert("The supplied email address is invalid.");
            document.login_form.email.focus();
            return false;
        }
    }

	if (password == "") {
		alert("You must enter a password.");
		document.login_form.password.focus();
		return false;
	}

	encodePassword(password);
}

function validateUserForm() {
	var username = document.getElementById('username');
	var password = document.getElementById('password');
	var verifyPassword = document.getElementById('verify_password');
	
	if (username.value == "") {
		alert("You must enter either a username.");
		username.focus();
		return false;
	}
	if (password.value == "") {
	    alert("You must provide a password.");
	    password.focus();
		return false;
	}
	if (verifyPassword.value == "") {
	    alert("You must retype the password.");
	    verifyPassword.focus();
		return false;
	}
	if (verifyPassword.value != password.value) {
	    alert("Your passwords do not match - please retype.");
	    verifyPassword.select();
		return false;
	}
	
	return true;
}

function validateUpdateUserForm() {
	var password = document.getElementById('password');
	var verifyPassword = document.getElementById('verify_password');
	
	if (password.value != "") {
    	if (verifyPassword.value == "") {
    	    alert("You must retype the password.");
    	    verifyPassword.focus();
    		return false;
    	}
    	if (verifyPassword.value != password.value) {
    	    alert("Your passwords do not match - please retype.");
    	    verifyPassword.select();
    		return false;
    	}
	}
	return true;
}

function adminForm(type) {
    document.filter_form.action = baseUrl+"/broker/add"+type;
	document.filter_form.submit();	
}

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 name = document.getElementById("contact_name").value;
	var location = document.getElementById("contact_location").value;
	var contactNo = document.getElementById("contact_number").value;
	var email = document.getElementById("contact_email").value;
	var query = document.getElementById("contact_query").value;

	var result = false;
	
	if (name == ""){
		alert("You must provide your name!");
		result = false;
	}
	else if (contactNo == "" && email == ""){
		alert("You must provide a contact number or email adddress!");
		result = false;
	}
	else if (query == ""){
		alert("You did not enter a query!");
		result = false;
	}
	else {
		result= true;
	}
	return result;
}


function validateDetailsForm() {
	var email = document.details_form.email.value;
	var firstName = document.details_form.first_name.value;
	var surname = document.details_form.surname.value;
	var password = document.details_form.password.value;
	var verifyPassword = document.details_form.verify_password.value;
	
	if (firstName == "") {
		alert("You must enter your first name.");
		document.details_form.first_name.focus();
		return false;
	}
    if (surname == "") {
		alert("You must enter your surname.");
		document.details_form.surname.focus();
		return false;
	}

	if (email == "") {
		alert("You must supply an email address.");
		document.details_form.email.focus();
		return false;
	}

	// 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.
    if (email != "") {
        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.details_form.email.focus();
            return false;
        }
    }
	if (password == "") {
		alert("You must enter a password.");
		document.details_form.password.focus();
		return false;
	}
	if (verifyPassword == "") {
		alert("You must verify your password.");
		document.details_form.verify_password.focus();
		return false;
	}
	if (password != verifyPassword) {
		alert("Password and verification must match.");
		document.details_form.password.focus();
		return false;
	}

	encodeDetailsPassword(password);
}

function validateUpdateDetailsForm() {
	var firstName = document.details_form.first_name.value;
	var surname = document.details_form.surname.value;
	var password = document.details_form.password.value;
	var verifyPassword = document.details_form.verify_password.value;

    if (firstName == "") {
		alert("You must enter a first name.");
		document.details_form.first_name.focus();
		return false;
	}

    if (surname == "") {
		alert("You must enter a surname.");
		document.details_form.surname.focus();
		return false;
	}

	if (password != verifyPassword) {
	    alert("Password and verification must match.");
	    document.details_form.password.focus();
	    return false;
	}

	if (password.length > 0) {
	    encodeDetailsPassword(password);
	}
}

function validatePasswordForm() {
	var email = document.password_form.email.value;
   
	if (email == "") {
		alert("You must supply your email address.");
		document.password_form.email.focus();
		return false;
	}
	// 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.
    if (email != "") {
        apos=email.indexOf("@");
        dotpos=email.lastIndexOf(".");
        lastpos=email.length-1;
        if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2) {
            alert("The supplied email address is invalid.");
            document.password_form.email.focus();
            return false;
        }
    }
}

function validateChangePasswordForm() {
	var password = document.password_form.password.value;
	var verifyPassword = document.password_form.verify_password.value;
	
	if (password == "") {
		alert("You must enter a password.");
		document.password_form.password.focus();
		return false;
	}
	if (verifyPassword == "") {
		alert("You must verify your password.");
		document.password_form.verify_password.focus();
		return false;
	}
	if (password != verifyPassword) {
		alert("Password and verification must match.");
		document.password_form.password.focus();
		return false;
	}

	encodeChangePassword(password);
}

function encodeChangePassword(password) {
    var hash = hex_md5(password);
	document.password_form.password.value = hash;
	return true;
}