/*

Form Validation script by Tony Kodis
e-mail: tony@celestialgraphix.com

To set-up, copy this script file to the web server and add the following tag to the HTML page's head:  <script language="JavaScript" src="PATH/formvalidate.js"></script>
--"PATH" is the file path pointing to the formvalidate.js file on the web server.
Example: <script type="text/javascript" src="../my_scripts/formvalidate.js"></script>

To call the script, add the following attribute to the HTML page's Form tag:  onsubmit="return validate('FORM NAME','REQUIRED FIELDS','E-MAIL FIELDS');"
--"FORM NAME" is the ID attribute attached to the form tag
--"REQUIRED FIELDS" are the Name attributes (separated by commas) of the Input fields you require to be filled
  (the required length is 7 characters, which can be changed... look for the only number "7" in the script below)
--"E-MAIL FIELDS" are the Name attributes (again, separated by commas) of the Input fields you require to be in e-mail address format (ie. tony@celestialgraphix.com)
Example: <form action="emailform.php" method="post" name="form1" onsubmit="return validate('Name,E-mail Address,Phone','E-mail Address');"> 

*/

function validate (formname,rfields,efields) {
	req_fields = rfields.split(',');
	email_fields = efields.split(',');
	errors = 0;
	missing_rfields = "";
	missing_efields = "";
	error_message = "";
	if (rfields != "") {
		for (i in req_fields) {
			if (document.getElementById(formname)[req_fields[i]].value.length < 7) {
				missing_rfields += " - " + req_fields[i] + "\n";
				errors=1;
			}
		}
	}
	if (efields != "") {
		for (i in email_fields) {
			if (document.getElementById(formname)[email_fields[i]].value.indexOf('@')<0 && document.getElementById(formname)[email_fields[i]].value!="") {
				missing_efields += " - " + email_fields[i] + "\n";
				errors=1;
			}
		}
	}
	if (errors == 1) {
		if (missing_rfields.length > 0) {
			error_message = "Please enter full values for the following fields:\n" + missing_rfields;
		}
		if (missing_efields.length > 0) {
			error_message = error_message + "Please enter valid e-mail address for the following fields:\n" + missing_efields;
		}
		alert (error_message);
		return false;
	}
	else {
		return true;
	}
}