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 validateMenuLogin() {
	var username = document.getElementById('menu_login_form').username.value;
	var password = document.getElementById('menu_login_form').password.value;

	if (username == "") {
		alert("You must enter a username.");
		document.getElementById('menu_login_form').username.focus();
		return false;
	}
	if (password == "") {
		alert("You must enter a password.");
		document.getElementById('menu_login_form').password.focus();
		return false;
	}

	encodeMemberPassword(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 encodeMemberPassword(password) {
	var hash = hex_md5(password);
	document.menu_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 confirmProcess(path) {
	if (confirm("Are you sure want to process this order now?")) {
		window.location = baseUrl+path;
	}
}

function confirmUpdateBlend(path) {
	if (confirm("Are you sure want to update this blend?\n\nThis will update the currrent batch.\n\nTo create a new batch, click the Create Batch icon.")) {
		window.location = baseUrl+path;
	}
}

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

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

function inlineBlock(divId) {
	document.getElementById(divId).style.display = "inline";
}

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 fullName = document.getElementById('full_name');
	var email = document.getElementById('email');
	var phone = document.getElementById('phone');

	if (fullName.value == "") {
		alert("You must enter your full name.");
		fullName.focus();
		return false;
	}
	if (email.value == "" && phone.value == "") {
		alert("You must enter either a phone number or email address.");
		email.focus();
		return false;
	}
	if (email.value != "") {
		if (!validateEmail(email.value)) {
			alert("You must enter a valid email address");
			email.focus();
			return false;
		}
	}
	if (phone.value != "" && !isPhone(phone.value)) {
		alert("The phone number contains invalid characters.");
		email.focus();
		return false;
	}
	return true;
}


function toggleResults(divId) {
	toggleDiv = document.getElementById(divId);
	new Effect.toggle(toggleDiv, 'slide');
}

function cancelMemberRegistration(path){
	if (confirm("Are you sure want to cancel the registration process?  All data entered to date will be lost.")) {
		window.location = baseUrl+'/'+path;
	}	
}

function validateRegisterStep1(){
	var username = document.register_form.email.value;
	var password = document.register_form.password.value;
	var confirmPassword = document.register_form.confirm_password.value;
	
	if (username == "") {
		alert("You must enter an email address.");
		document.register_form.email.focus();
		return false;
	}
	if (password == "") {
		alert("You must enter a password.");
		document.register_form.password.focus();
		return false;
	}
	if (password != confirmPassword) {
		alert("Your passwords do not match.");
		document.register_form.password.focus();
		return false;
	}
	encodeMemberSignupPassword(password, confirmPassword);	
}

function encodeMemberSignupPassword(password, confirmPassword) {
	var hash1 = hex_md5(password);
	var hash2 = hex_md5(confirmPassword);
	document.register_form.password.value = hash1;
	document.register_form.confirm_password.value = hash2;
	return true;
}
