// JavaScript Document

// create object
function formValidator()
{
// set up array to hold error messages
this.errorList = new Array;
// set up object methods
this.isEmpty = isEmpty; 
this.isNumber = isNumber; 
this.isAlphabetic = isAlphabetic; 
this.isAlphaNumeric = isAlphaNumeric; 
this.isWithinRange = isWithinRange; 
this.isEmailAddress = isEmailAddress; 
this.isChecked = isChecked; 
this.raiseError = raiseError; 
this.numErrors = numErrors; 
this.displayErrors = displayErrors; 
this.CheckPhoneNumber = CheckPhoneNumber;
}
// check to see if input is whitespace only or empty
function isEmpty(val) {
	if (val.match(/^s+$/) || val == "") {
		return true;
	} else {
		return false;
	} 
}
// check to see if input is number
function isNumber(val) {
	if (isNaN(val)) {
		return false;
	} else {
		return true;
	} 
}
// check to see if input is alphabetic
function isAlphabetic(val) {
	if (val.match(/^[a-zA-Z]+$/)) {
		return true;
	}
	else {
		return false;
	} 
}
// check to see if input is alphanumeric
function isAlphaNumeric(val) {
	if (val.match(/^[a-zA-Z0-9]+$/)) {
		return true;
	} else {
		return false;
	} 
}
// check to see if value is within range
function isWithinRange(val, min, max) {
	if (val >= min && val <= max) {
		return true;
	} else {
		return false;
	} 
}
// check to see if input is a valid email address
function isEmailAddress(val) {
	if (val.match(/^([a-zA-Z0-9])+([.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-]+)+/)) {
		return true;
	} else {
		return false;
	} 
}
// check to see if form value is checked
function isChecked(obj) {
	if (obj.checked) {
		return true;
	} else {
	return false;
	} 
}
// display all errors
// iterate through error array and print each item
function displayErrors() {
	for (x=0; x<this.errorList.length; x++) {
		alert("Error: " + this.errorList[x]);
	}
}
// add an error to error list
function raiseError(msg) {
	this.errorList[this.errorList.length] = msg;
}
// return number of errors in error array
function numErrors() {
	return this.errorList.length;
}
function CheckPhoneNumber(TheNumber) {
	var valid = 1
	var GoodChars = "0123456789()-+. "
	var i = 0
	if (TheNumber=="") {
		// Return false if number is empty
		valid = 0
	}
	for (i =0; i <= TheNumber.length -1; i++) {
		if (GoodChars.indexOf(TheNumber.charAt(i)) == -1) {
			valid = 0
		} // End if statement
	} // End for loop
	return valid;
}

// end object
function writit(text,id)
{
	if (document.getElementById)
	{
		x = document.getElementById(id);
		x.innerHTML = '';
		x.innerHTML = text;
	}
	else if (document.all)
	{
		x = document.all[id];
		x.innerHTML = text;
	}
	else if (document.layers)
	{
		x = document.layers[id];
		text2 = '<P CLASS="testclass">' + text + '</P>';
		x.document.open();
		x.document.write(text2);
		x.document.close();
	}
}

function checkForm() {
	var error = 0;
	fv = new formValidator();
	if (fv.isEmpty(document.getElementById('firstname').value)) {
		fv.raiseError('error');
		writit("Please enter a first name.","fname");
		error = 1;
	} else {writit("","fname") };
	if (fv.isEmpty(document.getElementById('lastname').value)) {
		writit("Please enter a last name.","lname");
		error = 1;
		fv.raiseError('error');
	} else {writit("","lname") };
	
	if (fv.isEmpty(document.getElementById('companyName').value)) {
		writit("Please enter a company name.","cname");
		error = 1;
		fv.raiseError('error');
	} else {writit("","cname") };
	//Check the phoen number and then check to see if it is valid
	if (fv.isEmpty(document.getElementById('phne').value)) {
		writit("Please enter a phone number.","cph");
		fv.raiseError('error');
	} else {writit("","cph") };
 	
	if (!fv.CheckPhoneNumber(document.getElementById('phne').value)) {
		writit("Please enter a valid phone number.","cph");
	} 
	
	//Check the email Address and then check to see if it is valid
	if(fv.isEmpty(document.getElementById('email').value)) {
		writit("Please enter an email address.","em");
		fv.raiseError('error');
	} else if(!checkMail(document.getElementById('email').value)) {
		writit("Please enter a valid email address.","em");
		fv.raiseError('error');		
	} else {
		writit("","em");
	}
	
	if (fv.isEmpty(document.getElementById('confirmEmail').value)) {
		writit("Please confirm your email address.","cem");
		fv.raiseError('error');
	} else {writit("","cem") };
	
	if (fv.isEmpty(document.getElementById('address').value)) {
		writit("Please enter an address.","ad");
		fv.raiseError('error');
	} else {writit("","ad") };
		
	if (fv.isEmpty(document.getElementById('comment').value)) {
		writit("Please enter your comment or question.","cmt");
		fv.raiseError('error');
	} else {writit("","cmt") };
	
	if(!compare(document.getElementById('confirmEmail').value,document.getElementById('email').value)){ 
		writit("Email addresses do not match","cem");
	} 
	
	if (fv.numErrors() > 0) {
		return false;
	} else	{
		document.getElementById('VerticalForm').submit();
		return true;
	}	
}
function checkMail(x)
{
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(x)) {  return true; }
	else  { return false;}
}
function compare(v1,v2) {
	if(v1==v2) {
		return true;
	} else {
		return false;
	}
}


