//*********************************************************************************
// Form Validation
// TYPES: 1 = EMAIL ; 2 = NAMES ; 3 = TITLES
//*********************************************************************************

var contactForm_Fields =			new Array('name','email','company','phone','message');
var contactForm_Errors =			new Array('Name','Email','Company','Phone','Message');
var contactForm_IsTypes =			new Array(0,1,0,0);


function SetWhichForm(num){
	document.vForm.whichForm.value = num;
}

var RegExNames = /^([a-zA-Z0-9_-]+)$/;
var RegExTitles = /^([a-zA-Z0-9_-]+)$/;


function MainForm() {
	var pass = true;
	var ErrorMessage = "The following fields are required:\n";
	var theArray = null;
	var theErrors = null;
	var theTypes = null;
	
	switch(document.vForm.whichForm.value){
		case "0":
			theArray = contactForm_Fields;
			theErrors = contactForm_Errors;
			theTypes = contactForm_IsTypes;
			break;
	}
	
	for (i = 0;i < theArray.length;i++) {
		var tempobj = eval("document.vForm." + theArray[i]);
		if(tempobj.value == ""){
			ErrorMessage += " - " + theErrors[i] + "\n";
			pass = false;
		}else{
			var TypeID = theTypes[i];
			if(TypeID == 1){
				var strEmail = emailCheck(tempobj.value);
				if(strEmail.length != 0){
					ErrorMessage += " - " + strEmail + "\n";
					pass = false;
				}			
			}else if(TypeID == 2){
				if(RegExNames.test(tempobj.value)==false)
				{
					ErrorMessage += " - Please enter a valid " + tempobj.name + "\n";
					pass = false;
				} 
			}else if(TypeID == 3){
				if(RegExTitles.test(tempobj.value)==false)
				{
					ErrorMessage += " - Please enter a valid " + tempobj.name + "\n";
					pass = false;
				} 
			}
		}
	}
	if (!pass) {
		alert(ErrorMessage)
		return false;
	}
	else {
		return true;
	}
}


function emailCheck(emailStr){
	var checkTLD = 1;
	var knownDomsPat = /^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat = /^(.+)@(.+)$/;
	var specialChars = "\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars = "\[^\\s" + specialChars + "\]";
	var quotedUser = "(\"[^\"]*\")";
	var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom = validChars + '+';
	var word = "(" + atom + "|" + quotedUser + ")";
	var userPat = new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat = new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray = emailStr.match(emailPat);

	if (matchArray == null) {
		return "Email address seems incorrect (check @ and .'s)\n";
	}
	var user=matchArray[1];
	var domain=matchArray[2];

	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			return "Ths username contains invalid characters\n";
		}
	}
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			return "Ths domain name contains invalid characters\n";
		}
	}

	if (user.match(userPat)==null) {
		return "The username doesn't seem to be valid\n";
	}

	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				return "Destination IP address is invalid!\n";
			}
		}
	}

	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat)==-1) {
			return "The domain name does not seem to be valid\n";
		}
	}
	if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) {
		return "The address must end in a well-known domain or two letter " + "country\n";
	}
	if (len<2) {
		return "This address is missing a hostname\n";
	}
	return "";
}