function checkFieldLen(myItem,maxlength){
	if(myItem.value.length>maxlength){
		myItem.className = "formError";
		alert("NOTICE: Field maximum size has been exceeded (Max "+maxlength+" characters).");
		myItem.value=myItem.value.substr(0,maxlength);
	}
	else myItem.className = "";
}
		
function isValidString(str){
	var isValid=true;
	if(str=='')	isValid=false;
	return isValid;
}
	
function isValidEmail(email){
	var isValid = true;
	var SymbolLocation = email.indexOf('@');
	var LastDotLocation = email.lastIndexOf('.');
	var SpaceLocation = email.indexOf(' ');
	var EmailLength = email.length;
	if (SymbolLocation < 1 ) isValid = false;	// At least one @ must be present and not before position 2
	if (LastDotLocation < SymbolLocation) isValid = false;	// at least one . (dot) afer the @ is required
	if (EmailLength - LastDotLocation <= 2) isValid = false;	// at least two characters [com, uk, fr, ...] must occur after the last . (dot)
	if (SpaceLocation != -1) isValid = false;	// no empty space " " is permitted (one may trim the email)
	return isValid;
}
		
function checkTextField(myField,err){
	if(!isValidString(myField.value)){
		myField.className="formError";
		err=true;
	}
	else myField.className="";
	return err;
}
	
function checkEmailField(myField,err){
	if(!isValidEmail(myField.value)){
		myField.className="formError";
		err=true;
	}
	else myField.className="";
	return err;
}
	
function checkSelect(field,err) {
	if(field.options[field.selectedIndex].value=='.') {
		field.className="formError";
		err=true;
	} else field.className="";
	return err;
}

function checkEnter(e, myForm){
	var characterCode;
	if(e && e.which){
		e = e;
		characterCode = e.which;
	} else{
		e = event;
		characterCode = e.keyCode;
	}
	if(characterCode == 13){
		myForm.submit();
		return false;
	} else return true;
}

function validateForm(){
  var reason = "";

  reason += validateEmpty(document.getElementById('firstName'));
  reason += validateEmpty(document.getElementById('lastName'));
  reason += validateEmail(document.getElementById('email'));
  reason += validatePhone(document.getElementById('phone'));
  
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

function validateEmpty(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "The required field has not been filled in.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "You didn't enter a phone number.\n";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!(stripped.length == 10)) {
        error = "The phone number is the wrong length. Make sure you included an area code.\n";
        fld.style.background = 'Yellow';
    }
    return error;
}

