function isValidEmail(str) {
	// DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
	// This function is used to verify if the given value is a possible valid email address. This function makes 
	// sure the email address has one (@) and at least one (.). It also makes sure that there are no spaces, 
	// extra '@'s or a (.) just before or after the @. It also makes sure that there is atleast one (.) after the @.

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/

	if (str.match(illegalChars)) {
	   return false;
	}	

	if (str.indexOf(at)==-1){
		 return false;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		 return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false;
	}

	 if (str.indexOf(at,(lat+1))!=-1){
		return false;
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false;
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
		return false;
	 }
	
	 if (str.indexOf(" ")!=-1){
		return false;
	 }

	 return true					
}

function validateEmail(field) {
	if (field.value.length == 0) {
		alert("Email can not be empty.");
		return false;
	}
	if (isValidEmail(field.value) == false) {
		alert("Email address is not valid.");
		return false;
	} else {
		return true;
	}
}

function formSubmit() {
	if (document.Request.FirstName.value.length == 0) {
		alert("First name can not be empty.");
		return;
	}
	if (document.Request.LastName.value.length == 0) {
		alert("Last name can not be empty.");
		return;
	}
	if (!(checkPhone(document.Request.PhoneNum.value))) {
		return;
	}
	if (!(validateEmail(document.Request.EmailAddr))) {
		return;
	}
	document.Request.submit();
}

function checkPhone(str) {
	if (str.length == 0) {
		alert("Phone number can not be empty.");
		return false;
	}

	var stripped = str.replace(/[\(\)\.\-\ ]/g, '');
	//strip out acceptable non-numeric characters
	if (isNaN(parseInt(stripped))) {
	   alert("The phone number contains illegal characters.");
	   return false;
	}
	if (stripped.length < 10) {
		alert("The phone number is the wrong length.\nMake sure you included an area code.");
	   return false;
	}
	return true;
}

function validateActCodeReq(state) {
	if (state) {
		if (document.Request.Company.value.length == 0) {
			alert("Company name is required to obtain an activation code.");
			document.Request.NeedCode.checked = false;
			return false;
		}
	}
	return true;
}
