//Based on http://developer.apple.com/internet/webcontent/validation.html

// Check Form

function checkForm(form) {
	var errormessage = "";
	var temperrormessage = "";
	
	/*Check the name field*/
	temperrormessage = checkIsEmpty(document.forms.contact.name.value, 'Name');
	if (temperrormessage.length > 0) {
		errormessage += temperrormessage;
	}

	/*Check the company field*/
	temperrormessage = checkIsEmpty(document.forms.contact.company.value, 'Company');
	if (temperrormessage.length > 0) {
		errormessage += temperrormessage;
	}

	/*Check the phone field*/
	temperrormessage = checkPhone(document.forms.contact.phone.value);
	if (temperrormessage.length > 0) {
		errormessage += temperrormessage;
	}

	/*Check the fax field*/
	temperrormessage = checkFax(document.forms.contact.fax.value);
	if (temperrormessage.length > 0) {
		errormessage += temperrormessage;
	}

	/*Check the email field*/
	temperrormessage = checkEmail(document.forms.contact.email.value);
	if (temperrormessage.length > 0) {
		errormessage += temperrormessage;
	}
	
	/*Check the contact via DDL*/
	temperrormessage = checkContactVia(document.forms.contact.contactvia.value);
	if (temperrormessage.length > 0) {
		errormessage += temperrormessage;
	}
				
	if (errormessage.length) {
		/*If is compatible with the InnerHTML function*/
		if (innerhtml_compatible) {
			alert("Errors have occured.");
			/*Replace /n with <br> in errors string*/
			replaceRE = /\n/gi;
			errormessageformatted = errormessage.replace(replaceRE, '<br>');
			document.getElementById('errorsarea').innerHTML	= "<div id=\"errors\"><img src=\"images/form_divclose.gif\" alt=\"Close\" onClick=\"changeDiv(\'errors\',\'none\');\"><strong>Error:</strong><br>"+errormessageformatted+"</div>";
		} 
		else {
			alert("Errors have occured:\n"+errormessage);
		}
		return false;
		
	}
	else {
		return true;
	}	
}

function checkEmail (strng) {
	var error="";
    var emailFilter=/^.+@.+\..{2,3}$/;
	
    if ((strng.length)&&(!(emailFilter.test(strng)))) { 
       error += "Please enter a valid email address.\n";
    }
    else {
		//test email for illegal characters
		var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
		if (strng.match(illegalChars)) {
        	error += "The email address contains illegal characters.\n";
		}
	}
	return error;    
}


// phone number - strip out delimiters and check for 10 digits

function checkPhone (strng) {
	var error = "";
	var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    	if ((strng.length)&&((isNaN(parseInt(stripped))))) {
	       error += "The phone number contains illegal characters.\n";
	    }
	    if ((strng.length)&&(!(stripped.length == 11))) {
			error += "The phone number is the wrong length. Make sure you included an area code.\n";
	    } 
	return error;
}

// fax number - strip out delimiters and check for 10 digits

function checkFax (strng) {
	var error = "";
	var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    	if ((strng.length)&&((isNaN(parseInt(stripped))))) {
	       error += "The fax number contains illegal characters.\n";
	    }
	    if ((strng.length)&&(!(stripped.length == 11))) {
			error += "The fax number is the wrong length. Make sure you included an area code.\n";
	    } 
	return error;
}

// contact via - 

function checkContactVia (strng) {
	var error = "";
	if (strng == "Phone") {
		if (!(document.forms.contact.phone.value.length)) {
			error += "You have asked to to contact you via Phone, but you have not entered a phone number.\n";
		}
	}
	else if (strng == "Fax") {
		if (!(document.forms.contact.fax.value.length)) {
			error += "You have asked to to contact you via Fax, but you have not entered a fax number.\n";
		}
	}
	else if (strng == "E-Mail") {
		if (!(document.forms.contact.email.value.length)) {
			error += "You have asked to to contact you via E-Mail, but you have not entered an E-Mail address.\n";
		}
	}
	return error;
}

// non-empty textbox

function checkIsEmpty(strng, fldname) {
var error = "";
  if (strng.length == 0) {
     error = "The required text \'" +fldname+ "\' has not been entered.\n"
  }
return error;	  
}

