// Error and Standard field border colors
var fieldErrorColor = "red";
var fieldStdColor = "#7F9DB9";
var fieldErrorWidth = "2px";
var fieldStdWidth = "1px";
var firstErrorField = null;

// ===============================================================================
// FUNCTION............	valFrmSignup( form ) -> boolean
// RETURNS.............	FALSE if validation failed, otherwise TRUE
// DESCRIPTION.........	Validates fields on the ATG signup form
// AUTHOR..............	K. Anderson, Superlative Solutions, Inc.
// NOTES...............	
// ===============================================================================
valFrmSignup = function(frm) {
	var rc = false;
	var eMsg = "";
	firstErrorField = null;
	
	// Check required fields
	eMsg += checkRequiredField("mbrfirstname","Your First name");
	eMsg += checkRequiredField("mbrlastname","Your Last name");
	eMsg += checkRequiredField("mbraddr1","Your address");
	eMsg += checkRequiredField("mbrcity","Your city");
	eMsg += checkRequiredField("sttid","Your state or province");
	eMsg += checkRequiredField("mbrzip","Your zip/postal code");
	eMsg += checkRequiredField("mbremail","Your email address");
	eMsg += checkRequiredField("mbrphone","Your daytime phone number");
	eMsg += checkRequiredField("mbrusername","Your desired username");
	eMsg += checkRequiredField("mbrpwd","Your desired password");
	eMsg += checkRequiredField("mbrpwd_2","Password confirmation field");
	eMsg += checkRequiredField("mbrsecquest","A selected security question");
	eMsg += checkRequiredField("mbrsecanswer","Your answer to the security question");
	
	if( eMsg != "" ) {
		if( firstErrorField != null ) firstErrorField.focus();
		alert("Unable to process your entry because one or more required fields were not entered.\r\n" + 
				"Fields that need must be entered are indicated with an asterisk and now have a red border.\r\n" + 
				"The following required fields are missing:\r\n\r\n" +
				eMsg + "\r\n" +
				"Please correct the error(s) and resubmit your entry.");
		return false;
	}
	
	// Now check for valid entries
	// Start with the email address
	if( frm.mbremail.value.indexOf("@") < 1 ) {
		eMsg += setFieldError("mbremail","The email address entered is not valid");
	} else {
		var emaildom = frm.mbremail.value.substr(frm.mbremail.value.indexOf("@")+1);
		if( emaildom.indexOf(".") < 1 ) {
			eMsg += setFieldError("mbremail","The email address entered is not valid");
		}
	}
	
	// Make sure passwords match
	if( frm.mbrpwd.value != frm.mbrpwd_2.value ) {
		eMsg += setFieldError("mbrpwd","Passwords entered do not match");
		eMsg += setFieldError("mbrpwd_2","");
	}
	
	if( eMsg != "" ) {
		if( firstErrorField != null ) firstErrorField.focus();
		alert("Unable to process your entry because of the following error(s):\r\n\r\n" + 
				eMsg + "\r\n" +
				"Fields that are invalid now have a red border. Please correct the error(s)\r\n" + 
				"and resubmit your entry.");
		return false;
	}
	
	// If we made it to here, it was successful, so return true
	return true;
}

// ===============================================================================
// FUNCTION............	valFrmContact( form ) -> boolean
// RETURNS.............	FALSE if validation failed, otherwise TRUE
// DESCRIPTION.........	Validates fields on the ATG contact us form
// AUTHOR..............	K. Anderson, Superlative Solutions, Inc.
// NOTES...............	
// ===============================================================================
valFrmContact = function(frm) {
	var rc = false;
	var eMsg = "";
	firstErrorField = null;
	
	// Check required fields
	eMsg += checkRequiredField("txtName","Your name");
	eMsg += checkRequiredField("txtEmail","Your email address");
	eMsg += checkRequiredField("txtCity","Your city of residence");
	eMsg += checkRequiredField("cboState","Your state");
	eMsg += checkRequiredField("cboReason","The reason you are contacting us");
	eMsg += checkRequiredField("txtComment","Your comment or question");
	eMsg += checkRequiredField("captcha","Security code");
	
	if( eMsg != "" ) {
		if( firstErrorField != null ) firstErrorField.focus();
		alert("Unable to process your entry because one or more required fields were not entered.\r\n" + 
				"Fields that need must be entered are indicated with an asterisk and now have a red border.\r\n" + 
				"The following required fields are missing:\r\n\r\n" +
				eMsg + "\r\n" +
				"Please correct the error(s) and resubmit your entry.");
		return false;
	}
	
	// Now check for valid entries
	// Start with the email address
	if( frm.txtEmail.value.indexOf("@") < 1 ) {
		eMsg += setFieldError("txtEmail","The email address entered is not valid");
	} else {
		var emaildom = frm.txtEmail.value.substr(frm.txtEmail.value.indexOf("@")+1);
		if( emaildom.indexOf(".") < 1 ) {
			eMsg += setFieldError("txtEmail","The email address entered is not valid");
		}
	}
	
	if( eMsg != "" ) {
		if( firstErrorField != null ) firstErrorField.focus();
		alert("Unable to process your entry because of the following error(s):\r\n\r\n" + 
				eMsg + "\r\n" +
				"Fields that are invalid now have a red border. Please correct the error(s)\r\n" + 
				"and resubmit your entry.");
		return false;
	}
	
	// If we made it to here, it was successful, so return true
	return true;
}

// ===============================================================================
// FUNCTION............	setFieldError( fieldName, errText ) -> string
// RETURNS.............	String with error text
// DESCRIPTION.........	Specified field will have it's border changed to a red
//						color and error message will be returned
// AUTHOR..............	K. Anderson, Superlative Solutions, Inc.
// NOTES...............	
// ===============================================================================
setFieldError = function( fn, msg ) {
	var e = document.getElementById(fn);
	var rc = "";
	if( e != null ) {
	
		if( msg != "" ) rc = "- " + msg + "\r\n";
		e.style.borderColor = fieldErrorColor;
		e.style.borderWidth = fieldErrorWidth;
		if( firstErrorField == null ) firstErrorField = e;

	}
	
	return rc;
}

// ===============================================================================
// FUNCTION............	checkRequiredField( fieldName, errText ) -> string
// RETURNS.............	String with error message text
// DESCRIPTION.........	Checks specified field to see if it has a value. If not,
// 						it sets the field border to red and return error text
// AUTHOR..............	K. Anderson, Superlative Solutions, Inc.
// NOTES...............	
// ===============================================================================
checkRequiredField = function( fn, msg ) {
	var e = document.getElementById(fn);
	var rc = "";
	if( e != null ) {
	
		// Display field type
		//if( e.type != "text" && e.type != "select-one" && e.type != "password" ) alert(fn + " type = " + e.type);
		
		// Process based on type of field
		switch( e.type ) {
			case "text":
				if( e.value == "" ) rc = "- " + msg + "\r\n";
				break;
			case "password":
				if( e.value == "" ) rc = "- " + msg + "\r\n";
				break;
			case "select-one":
				if( e[e.selectedIndex].value == "" ) rc = "- " + msg + "\r\n";
				break;
		}

		// Last thing to do. Set the field border color
		if( rc != "" ) {
			e.style.borderColor = fieldErrorColor;
			e.style.borderWidth = fieldErrorWidth;
			if( firstErrorField == null ) firstErrorField = e;
		} else {
			e.style.borderColor = fieldStdColor;
			e.style.borderWidth = fieldStdWidth;
		}
	
	}
	
	return rc;
}
