// JavaScript Document
function validatePhone(phoneAC, phonePre, phonePost)
{
   var error = "";
   var phone = phoneAC.value + "-" + phonePre.value + "-" + phonePost.value;
   var stripped = phone.replace(/[\(\)\.\-\ ]/g, '');     

   if (phone == "") {
        error = "You didn't enter a phone number.\n";
        phoneAC.style.background = 'white';
        phonePre.style.background = 'white';
        phonePost.style.background = 'white';
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number has illegal characters.\n";
        phoneAC.style.background = 'white';
        phonePre.style.background = 'white';
        phonePost.style.background = 'white';
    } else if (!(stripped.length == 10)) {
        error = "The phone number wrong length.\n";
        phoneAC.style.background = 'white';
        phonePre.style.background = 'white';
        phonePost.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 = 'White';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'White';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'White';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateFName(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off

  
    
    if (fld.value == "") {
        fld.style.background = 'White';
        error = "You didn't enter your first name.\n";
    } 
    return error;
}

function validateLName(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off

  
    
    if (fld.value == "") {
        fld.style.background = 'White';
        error = "You didn't enter your last name.\n";
    } 
    return error;
}


function validateFormOnSubmit(theForm) {
var reason = "";

  reason += validateFName(theForm.fName);
  reason += validateLName(theForm.lName);
  reason += validateEmail(theForm.email);
  reason += validatePhone(theForm.txtPhoneAC, theForm.txtPhonePre, theForm.txtPhonePost);

  
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

function validateFormOnSubmit_SF(theForm) {
var reason = "";

  reason += validateFName(theForm.first_name);
  reason += validateLName(theForm.last_name);
  reason += validateEmail(theForm.email);
  reason += validatePhone(theForm.txtPhoneAC, theForm.txtPhonePre, theForm.txtPhonePost);
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  var phone = theForm.txtPhoneAC.value + "-" + theForm.txtPhonePre.value + "-" + theForm.txtPhonePost.value;
  theForm.phone.value = phone;
  
  return true;
}

function checkLength(xField, xLength, xFocus){

	if (xField.value.length == xLength){
		xFocus.focus();
	}
	
	return;
}

function checkRecordType(obj){

	var form = obj.form;
	if (obj.options[obj.selectedIndex].value == "Lottery"){
		form.recordType.value = "012500000009EhL";
	}else{
		form.recordType.value = "012500000009EhQ";
	}

	return;
}

