var alertmsg = "";


//
// called from onSubmit of MyAccount
//
function checkAllFields(formobj) {
	if (checkRequiredFields(formobj)) {
		if (checkPasswords(formobj)) {
			if (isEmailValid(formobj.email)) {
				return true
			}
			else {
				alert("The email address is not valid.  It must contain the characters '@' and '.'.");
				return false;
			}
		}
		else {
			alert(alertmsg)
			return false
		}	
	}
	else {
		alert(alertmsg)
		return false
	}	
}

//	
//	returns a TRUE if all required fields are filled in.
//
function checkRequiredFields(formobj) {
	alertmsg = "";
	var tempmsg = "";
	
	// Check these fields only if the box is not checked.
	if (!formobj.noemailcheckbox.checked) {
		if(isEmpty(formobj.email)) {
			tempmsg = "\nEmail Address"
		}
		if(isEmpty(formobj.password1)) {
			tempmsg += "\nPassword"
		}
		if(isEmpty(formobj.password2)) {
			tempmsg += "\nVerify Password"
		}
	}
	
	if(isEmpty(formobj.billname)) {
		tempmsg += "\nName"
	}
	if(isEmpty(formobj.billaddr1)) {
		tempmsg += "\nAddress1"
	}
	if(isEmpty(formobj.billcity)) {
		tempmsg += "\nCity"
	}
	if(isEmpty(formobj.billzip)) {
		tempmsg += "\nPostal Code"
	}
	if(isEmpty(formobj.billphone1)) {
		tempmsg += "\nDaytime Phone"
	}
	
	
	// Check these fields only if the box is not marked.
	if (!formobj.samebilling.checked) {
		
		if(isEmpty(formobj.shipname)) {
			tempmsg += "\nShipping Name"
		}
		if(isEmpty(formobj.shipaddr1)) {
			tempmsg += "\nShipping Address1"
		}
		if(isEmpty(formobj.shipcity)) {
			tempmsg += "\nShipping City"
		}
		if(isEmpty(formobj.shipzip)) {
			tempmsg += "\nShipping Postal Code"
		}
	}
	
	
	
	if (tempmsg == "") {
		return true
	}
	else {
		alertmsg = "The following required field(s) are blank:" +tempmsg 
		return false
	}
}


//
//	returns a TRUE if both passwords match
//
function checkPasswords(formobj) {
	alertmsg = "";
	password = formobj.password1.value
	verify = formobj.password2.value
	if (password == verify) {
		return true
	}
	else {
		alertmsg = "Passwords do not match.\nPlease ensure both password entries match."
		return false
	}
}

//
// Returns a TRUE if field is empty
//
function isEmpty(textObj){
	var status = true;
	if (textObj.value.length == 0)
		status = true;
	else {
		for (var i=0; i<textObj.value.length; ++i){
	    	var ch = textObj.value.charAt(i);
	    	if (ch != ' ' && ch != '\t'){
	 			status = false;
	        }
		}
	}
	return (status)
}


//
// Validate email address
// Ensures there is an '@' sign and a '.' that follows the '@' character.
//
function isEmailValid(textObj) {
	if (isEmpty(textObj)) {
		return true;
	}
	var status = true;
	var at = textObj.value.indexOf('@', 0);
	if (at == -1 || at == 0) {
		status = false;
	}
	else {
		if (textObj.value.indexOf('.', at) == -1){
			status = false;
		}
	}
		
	return (status);
	
}

