function validateAgent(theForm) {
var reason = "";
  
  reason += validateEmpty(theForm.Compname,"company name");
  reason += validateEmpty(theForm.name,"full name");
  reason += validatePhone(theForm.phone);
  reason += validateFax(theForm.fax);
  reason += validateEmail(theForm.email);
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}


function validateFormOnSubmit(theForm) {
var reason = "";

  reason += validateEmpty(theForm.Compname,"company name");
  reason += validateEmpty(theForm.name,"full name");
  reason += validatePhone(theForm.phone);
  reason += validateFax(theForm.fax);
  reason += validateEmail(theForm.email);
  reason += validateEmpty(theForm.businessstart,"business start date");
  reason += validateEmpty(theForm.businesstype,"business type");
  reason += validateVMC(theForm.vmc);
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

function validateFormOnSubmitPremium(theForm) {
var reason = "";

  reason += validateEmpty(theForm.Compname,"company name");
  reason += validateEmpty(theForm.name,"full name");
  reason += validatePhone(theForm.phone);
  reason += validateFax(theForm.fax);
  reason += validateEmail(theForm.email);
  reason += validateEmpty(theForm.businessstart,"business start date");
  reason += validateEmpty(theForm.businesstype,"business type");
  reason += validateVMC(theForm.vmc);
  reason += validatePremiumRequirements(theForm.current_owner,"2 years of ownership are Required","current_owner","#d9f4ff");
  reason += validatePremiumRequirements(theForm.fico_score, "A Fico score of 600 is Required","fico_score","#ffffff");
  reason += validatePremiumRequirements(theForm.loan_amount, "You must qualify for at least $50,000","loan_amount","#d9f4ff");
  reason += validatePremiumRequirements(theForm.seasonal, "Your business cannot be sesonal","seasonal","#ffffff");
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

function validatePremiumRequirements(fld,msg,id,color) {
    var error = "";
	var tr = document.getElementById(id);
	
    if (fld.checked == false) {
         tr.style.backgroundColor = '#d9f4ff'; 
        error = msg + ".\n";
    } else {
        tr.style.backgroundColor = color;
    }
    return error;  
}

function validateEmpty(fld,msg) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = '#d9f4ff'; 
        error = "Please enter your " + msg + ".\n";
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateVMC(fld) {
    var error = "";
 
    if (fld.value == "Select") {
        fld.style.background = '#d9f4ff'; 
        error = "Please select your monthly credit card sales amount range .\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 = '#d9f4ff';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = '#d9f4ff';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#d9f4ff';
        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 = '#d9f4ff';
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = '#d9f4ff';
    } else if (!(stripped.length == 10)) {
        error = "The phone number is the wrong length. Make sure you included an area code.\n";
        fld.style.background = '#d9f4ff';
    }else{
		fld.style.background = 'White';
	}
    return error;
}
function validateFax(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

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

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

   if (fld.value == "") {
        error = "You need to enter a " + msg +" number.\n";
        fld.style.background = '#7dc9ff';
    } else if (isNaN(parseInt(stripped))) {
        error = "The" + msg + " contains illegal characters.\n";
        fld.style.background = '#7dc9ff';
    }
    return error;
}

function validateCalcOnSubmit(theForm) {
var reason = "";
  
  reason += validateCalc(theForm.cc_volume," credit cards volume");
  reason += validateCalc(theForm.gross_volume," gross sales volume");
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}
