//(c) 2005 Great Lakes Higher Education Corporation
//Created: 19 October 2005 Michael Westphal
//
//SUMMARY:
// The purpose of this JavaScript library is the validation
// of Web Supply Order forms with the added functionality of
// excluding any unordered forms from a submitted order.
//
//
//DEPENDANCIES:
// This JavaScript library must have RegExpValidation.js 
// and also requires that the following HTML
// specifications are adhered to:
//
// ---------------------------------------
// HTML Form 			| Form ID
// ---------------------------------------
// - Order Supply form	| "orderSupplies"
//
//
// ---------------------------------------
// HTML Input field 	| Input field ID
// ---------------------------------------
// - Contact First Name	| "first_name"
// - Contact Last Name 	| "last_name"
// - Organization 		| "organization"
// - Street Address 	| "street_address"
// - City 				| "city"
// - State				| "state"
// - Zip 				| "zip"
// - Phone				| "phone"
// - Email Address 		| "email_address"
//
//
//FUNCTIONS:
// validateForm()
// validateShippingAddress()
// filterOrderForms()
// formatErrorMessage(str)
//


/**
 * NAME: validateForm
 *
 * PURPOSE: Joins together the form validation
 *	    and form filtering functions into
 *	    one. This method is called by the
 *	    the page's HTML Form onSubmit method.
 *	    If form validation fails, the page is
 *	    not submitted.
 *
 * PARAMETERS: None
 *
 * RETURNS: Boolean
 */
function validateForm() {
	result = false;

	if(validateShippingAddress()){
		filterOrderForms();
		validatePackets();
		result = true;
	}

	return result;
}


/**
 * NAME: validateShippingAddress
 *
 * PURPOSE: Validates each required shipping address field.
 *	    If any validation fails, populate an error message
 *	    dialog, display it to the user, and return false such
 *	    that, the form is not submitted. Else, submit the
 *	    form.
 *
 * PARAMETERS: None
 *
 * RETURNS: boolean
 */
function validateShippingAddress() {
	var result = true;
	var errorMsg = "";

	//Get the values of all important Shipping Address fields
	var firstName 	= document.getElementById("first_name").value;
	var lastName 	= document.getElementById("last_name").value;
	var org 	= document.getElementById("organization").value;
	var address 	= document.getElementById("street_address").value;
	var city 	= document.getElementById("city").value;
	var state	= document.getElementById("state").value;
	var zip 	= document.getElementById("zip").value;
	var phone 	= document.getElementById("phone").value;
	var email 	= document.getElementById("email_address").value;

	//Validate each field, get their associated error message if failed, and format the message
	errorMsg += formatErrorMessage( validateStr(firstName, alphaRegExp, "Contact First Name") );
	errorMsg += formatErrorMessage( validateStr(lastName, alphaRegExp, "Contact Last Name") );
	errorMsg += formatErrorMessage( validateStr(org, alphaNumRegExp, "Organization") );
	errorMsg += formatErrorMessage( validateStr(address, alphaNumRegExp, "Street Address") );
	errorMsg += formatErrorMessage( validateStr(city, cityRegExp, "City") );
	errorMsg += formatErrorMessage( validateStr(state, stateRegExp, "State") );
	errorMsg += formatErrorMessage( validateStr(zip, zipRegExp, "Zip") );
	errorMsg += formatErrorMessage( validateStr(phone, phoneRegExp, "Phone") );
	errorMsg += formatErrorMessage( validateStr(email, emailRegExp, "Email Address") );

	//If error messages exist, display error dialog & do not submit form
	if(errorMsg != ""){
		var title = "Missing Shipping Address Information:\n";
		alert(title + errorMsg);	       
		result = false;
	}

	return result;
}


/**
 * NAME: filterOrderForms
 *
 * PURPOSE: Filters out any elements within the HTML
 *	    Form which empty by disabling them. By
 *	    filter, the disabled elements are not
 *	    included with the form on Submit.
 *
 * PARAMETERS: None
 *
 * RETURNS: void
 */
function filterOrderForms() {
	var form = document.getElementById("orderSupplies");

	for(i = 0; i < form.elements.length; i++) {
		if(form.elements[i].value == null ||
		   form.elements[i].value == "") {
			form.elements[i].disabled = true;
		}
	}
}

/**
 * NAME: formateErrorMessage
 *
 * PURPOSE: Formats the input string into the desired
 *	    error message format. 
 *
 * PARAMETERS: str - String
 *
 * RETURNS: String
 */
 function formatErrorMessage(str){
 	var msg = "";
 	
 	if(str != null && str != ""){
 		msg += "-> " + str;
 	}
 	
 	return msg; 
 }
 /**
 * NAME: filterOrderForms
 *
 * PURPOSE: Filters out any elements within the HTML
 *	    Form which empty by disabling them. By
 *	    filter, the disabled elements are not
 *	    included with the form on Submit.
 *
 * PARAMETERS: None
 *
 * RETURNS: void
 */
 
function validatePackets() {
	var form = document.getElementById("orderSupplies");
	var orderedECP = false;
	var orderedEXCP = false;
	
	for(i = 0; i < form.elements.length; i++) {
		if(form.elements[i].name != null && form.elements[i].name != "") {
			var name = form.elements[i].id;
			var value = null;
			value = form.elements[i].value;
			if(name == "CUSTOM - Entrance Counseling Packet" && value != null && value != "") {
				orderedECP = true;
			}
			else if(name == "CUSTOM - Exit Counseling Packet" && value != null && value != "") {
				orderedEXCP = true;
			}
			else if(name.substring(0,3) == "ecp" && !orderedECP) {
				form.elements[i].disabled = true;
			}
			else if(name.substring(0,4) == "excp" && !orderedEXCP) {
				form.elements[i].disabled = true;
			}
		}
	}
}