// JavaScript Document
function validateFormOnSubmit(theForm) {
var reason = "";

  reason += validateBusinessName(theForm.business_name);
  reason += validateBusinessType(theForm.business_type);
reason += validateMajorityTransactions(theForm.majority_transactions);
////  reason += validateLastname(theForm.last_name);
reason += validateAverageTicket(theForm.average_ticket);
reason += validateMonthlyVolume(theForm.monthly_volume);
reason += validateTimeFrame(theForm.time_frame);
reason += validateContactPerson(theForm.contact_person);
////reason += validateCompany(theForm.company);
reason += validateEmail(theForm.email);
reason += validatePhone(theForm.phone);
reason += validateCallTime(theForm.call_time);
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

//alert("We will get back to you shortly. Thanks!");
//theForm.submit();
}
function validateEmpty(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = '#FF4B4B'; 
        error = "The required field has not been filled in.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}
function validateBusinessType(fld) {
    var error = "";
   
 
    if (fld.value == "") {
        fld.style.background = '#FF4B4B'; 
        error = "You didn't select a Business type.\n";
    }   else {
        fld.style.background = 'White';
    }
    return error;
}

function validateMajorityTransactions(fld) {
    var error = "";
    
 
    if (fld.value == "") {
        fld.style.background = '#FF4B4B'; 
        error = "You didn't select Majority transaction.\n";
    }  else {
        fld.style.background = 'White';
    }
    return error;
}

//function validateLastname(fld) {
//    var error = "";
//    var illegalChars = /^[a-z] [-a-z0-9_ ]{0,49}$/i; // allow letters, numbers, and underscores
// 
//    if (fld.value == "") {
//        fld.style.background = '#FF4B4B'; 
//        error = "You didn't enter a Last Name.\n";
//    }  else if (illegalChars.test(fld.value)) {
//        fld.style.background = '#FF4B4B'; 
//        error = "Last name contains illegal characters.\n";
//    } else {
//        fld.style.background = 'White';
//    }
//    return error;
//}

function validateAverageTicket(fld) {
    var error = "";
 
    if (fld.value == "") {
        fld.style.background = '#FF4B4B'; 
        error = "You didn't select Average ticket.\n";
    }  else {
        fld.style.background = 'White';
    }
    return error;
}

function validateMonthlyVolume(fld) {
    var error = "";
 
    if (fld.value == "") {
        fld.style.background = '#FF4B4B'; 
        error = "You didn't select Monthly volume.\n";
    }  else {
        fld.style.background = 'White';
    }
    return error;
}

function validateTimeFrame(fld) {
    var error = "";
 
    if (fld.value == "") {
        fld.style.background = '#FF4B4B'; 
        error = "You didn't select a Time frame.\n";
    }  else {
        fld.style.background = 'White';
    }
    return error;
}

function validateCallTime(fld) {
    var error = "";
 
    if (fld.value == "") {
        fld.style.background = '#FF4B4B'; 
        error = "You didn't select the best time to call you.\n";
    }  else {
        fld.style.background = 'White';
    }
    return error;
}

function validateContactPerson(fld) {
    var error = "";
 
    if (fld.value == "") {
        fld.style.background = '#FF4B4B'; 
        error = "You didn't enter your name.\n";
    }  else {
        fld.style.background = 'White';
    }
    return error;
}


function validateBusinessName(fld) {
    var error = "";
    var illegalChars = /^[a-z] [-a-z0-9_ ]{0,49}$/i; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = '#FF4B4B'; 
        error = "You didn't enter a Comapny Name.\n";
    }  else if (illegalChars.test(fld.value)) {
        fld.style.background = '#FF4B4B'; 
        error = "Company name contains illegal characters.\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 = '#FF4B4B';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = '#FF4B4B';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#FF4B4B';
        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 = '#FF4B4B';
    } 
	else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = '#FF4B4B';
    } else if (!(stripped.length == 10)) {
        error = "The phone number is the wrong length. Make sure you included an area code.\n";
        fld.style.background = '#FF4B4B';
    }
		else {
        fld.style.background = 'White';
    }
    return error;
}