//*************************************************************************************************
//	FILE NAME:				validate_form.js
//	PURPOSE:                Client Side form validation
//  FUNCTION(s):			validateFormOnSubmit, validateEmpty, trim, validateEmail
//  PARAMETERS:             email, f_name, l_name, phone_number, brief, comments (all given on 
//							form submission in webform_php.
//	COMMENTS:				This javascript file does form validation on the webform before being
//							sent to process_form.php for email processing. It checks all the fields
//							to make sure that no fields are empty and that valid entries are made
//	AUTHOR:					Shaun Velasquez						
//  LAST MODIFIED:			April 9, 2009
//*************************************************************************************************



//*************************************************************************************************
//	FUNCTION:				validateContact
//	PURPOSE:				The 'main' function. Passes all the form input through all other 
//							functions to check for validation. If any errors are detected. An alert
//							to the user will be made informing him/her of the fields that needs to
//							be corrected.
//	PARAMETERS:				'theForm': information recieved from the form
//*************************************************************************************************

function validateForm(theForm) {
	var reason = "";

	var first_name_field = "First Name: ";
	var last_name_field = "Last Name: ";
	var email_field = "Email: ";
	var fi_field = "Financial Institution Name: ";
	var comments_field = "Comments: ";

	reason += validateEmpty(theForm.f_name, first_name_field);
	reason += validateEmpty(theForm.l_name, last_name_field);
	reason += validateEmail(theForm.email, email_field);
	reason += validateEmpty(theForm.comments, comments_field);
	reason += validateEmpty(theForm.fi, fi_field);
	  
	if (reason != "") {
		alert("Some fields need correction:\n" + reason);
		return false;
	}

}
//************************ End of Function: validateContact ********************************

function validateProduct(theForm) {
	var reason = "";

	var product_name_field = "Product Name: ";


	reason += validateEmpty(theForm.product_name, product_name_field);
      
	if (reason != "") {
		alert("Some fields need correction:\n" + reason);
		return false;
	}

}


//*************************************************************************************************
//	FUNCTION:				validateEmpty
//	PURPOSE:				Checks the given field to see if it is empty
//	PARAMETERS:				fld: contents of the form field, fld_name: the name of the field
//	COMMENTS:				If the field is empty. Add the name of the field that is empty to the
//							list of problems that needs to be fixed. This function also
//							changes the color of the field to Yellow to help the user see which
//							field needs to be corrected
//*************************************************************************************************


function validateEmpty(fld, fld_name) {
    var error = "";
 
    if (fld.value.length == 0) {							//if the field is empty
        fld.style.background = 'Yellow';					//change that text field to yellow
		error = fld_name;									//tell the user which field which needs
        error += "This field has not been filled in.\n";	//to be fixed and tell them what needs
    }														//to be done
	else {
        fld.style.background = 'White';
	}
    return error;  
}
//************************ End of Function: validateEmpty ********************************


//*************************************************************************************************
//	FUNCTION:				validateCategory
//	PURPOSE:				Checks the given field to see if a category has been selected
//	PARAMETERS:				fld: contents of the form field, fld_name: the name of the field
//*************************************************************************************************


function validateCategory(fld, fld_name) {
    var error = "";
 
    if (fld.selectedIndex == 0 ){
        fld.style.background = 'Yellow';					
		error = fld_name;									
        error += "Please select the appropriate category.\n";	
    }														
	else {
        fld.style.background = 'White';
	}
    return error;  
}

//************************ End of Function: validateCategory **************************************



//*************************************************************************************************
//	FUNCTION:				trim
//	PURPOSE:				Takes out any trailing white spaces
//	PARAMETERS:				contents of form field
//	COMMENTS:				takes out any trailing white spaces. To be used with the validateEmail
//							funtion
//*************************************************************************************************
function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}


//*************************************************************************************************
//	FUNCTION:				validateEmail
//	PURPOSE:				validates the email address give to make sure:
//							- It is not empty
//							- It is not using any illegal characters
//							- It is in the correct email format (e.g. me@csusm.edu)
//	PARAMETERS:				fld: contents of the form field, fld_name: the name of the field
//*************************************************************************************************
function validateEmail(fld, fld_name) {
    var error="";
    var tfld = trim(fld.value);						// value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;	// Checks for valid email format
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;	// List of illegal characters
	validateEmpty(fld, fld_name);					// Check to see if email field is empty
	if (!emailFilter.test(tfld) || fld.value.match(illegalChars)) { //checking email validness
        fld.style.background = 'Yellow';
        error = fld_name;
		error += "Please enter a valid email address.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
//************************ End of Function: validateEmail *********************************/

