// Validate Email Address
// (modified from http://www.sitepoint.com/article/862/2)
// used in conjunction with fncSubmit (below)
function validateEmail(email, msg, optional) {
	if (!email.value && optional) {
		return true;
	}
	var emailPat = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z])+$/;
	var matchArray=email.match(emailPat); 
	if (matchArray==null) {
		return false; 
		email.focus();
		email.select();
	} 
	return true;
}

// Ensure Form is Properly Filled Out
// (modified from http://www.js-examples.com/)
// call using below
// <input type="hidden" name="req_fields" value="name,Name|phone,Phone Number">
// <input name="Submit" type="button" value="Submit" onClick="fncSubmit(this.form);">
// NOTE: type="button" if type="submit" then no matter what, form will be submitted!!

function fncSubmit(obj) {
	var req = obj.req_fields;
	var submitflag = true;
	var notice = "There was an error with your form:\n\n";
	
	// [1] If certain fields are required, check if any are missing
	if (req) { 
		// Determine if any fields are required; if so, build error message
		var arrFields = obj.req_fields.value.split("|")
		for(var x=0; x<=arrFields.length-1; x++) {
			var arrName = arrFields[x].split(",");
			// if req fields are omitted: output the specific errors and set submitflag to false 
			var str = "if (obj."+arrName[0]+".value == '') { notice += '> "+arrName[1]+"\\n'; submitflag = false;}";
			eval(str);
		}
		// If no fields were missing, check email
		if (submitflag) { 

		// below used if there is a newsletter sign-up form 
		/* 
			if (obj.nletter[0].checked && email_check.length<2){
			  	notice += "> To sign up for the email newsletter, you must enter a valid email address.\n";
				submitflag = false;
			} 
		*/
		// Now, if there is an email address, validate it
			if (obj.email != null && obj.email.value.length>0 && !validateEmail(obj.email.value)){
				notice += "> The email address you entered is invalid.\n";
				submitflag = false;
			}
		}
		notice += "\nPlease check these errors and resubmit.";
		
	} else {
	// [1] Means there isn't a required fields parameter; keep going
		submitflag = true;
	}
	
	if (submitflag) {
		obj.submit();
	} else {
		alert(notice);
	}
}
//
