// JavaScript Document

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

function validateComma(fld) {
	var error = "";
	
	if (fld.value == ",") {
		fld.style.background = '#FFFF00';
		error = "This field is required.\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 = '#FFFF00';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = '#FFFF00';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#FFFF00';
        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 = '#FFFF00';
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = '#FFFF00';
    } else if (!(stripped.length == 10)) {
        error = "The phone number is the wrong length. Make sure you included an area code.\n";
        fld.style.background = '#FFFF00';
    } 
    return error;
}

//Validate 60-Day Free Trial Form
function validateFreeTrial(theForm) {
var reason = "";

  reason += validateEmpty(theForm.name);
  reason += validateEmail(theForm.email);
  reason += validatePhone(theForm.phone);
      
  if (reason != "") {
    alert("Some fields need correction:\n\n" + reason);
    return false;
  }

  return true;
}

//Validate Referral Form
function validateReferral(theForm) {
var reason = "";

  reason += validateEmail(theForm.email);
  reason += validatePhone(theForm.telephone);
  reason += validateEmpty(theForm.firstName);
  reason += validateEmpty(theForm.lastName);
  reason += validateEmpty(theForm.dealer);
  reason += validateEmpty(theForm.product);
  reason += validateComma(theForm.email);
  reason += validateComma(theForm.telephone);
  reason += validateComma(theForm.firstName);
  reason += validateComma(theForm.lastName);
  reason += validateComma(theForm.dealer);
  reason += validateComma(theForm.product);
  
  
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

//validate video form
function validateVideo(theForm) {
var reason = "";

  reason += validateEmail(theForm.email);
  reason += validatePhone(theForm.phone);
  reason += validateEmpty(theForm.first_name);
  reason += validateEmpty(theForm.last_name);
  reason += validateEmpty(theForm.dealer);
  reason += validateEmpty(theForm.website);
  reason += validateComma(theForm.email);
  reason += validateComma(theForm.phone);
  reason += validateComma(theForm.first_name);
  reason += validateComma(theForm.last_name);
  reason += validateComma(theForm.dealer);
  reason += validateComma(theForm.website);
  
  
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

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

  reason += validateEmail(theForm.email);
  reason += validateEmpty(theForm.name);
  reason += validateEmpty(theForm.dealer);
  reason += validatePhone(theForm.phone);
  reason += validateComma(theForm.email);
  reason += validateComma(theForm.name);
  reason += validateComma(theForm.dealer);
  reason += validateComma(theForm.phone);
  
  
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}


//Validate Newzletter Form
function validateFormOnSubmitNewz(theForm) {
var reason = "";

  reason += validateEmail(theForm.emailaddress);
  reason += validateEmpty(theForm.fullname);
  reason += validateComma(theForm.emailaddress);
  reason += validateComma(theForm.fullname);
  
  
  
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

//Validate Sample Form
function validateFormOnSubmitSamp(theForm) {
var reason = "";

  reason += validateEmail(theForm.youremail);
  reason += validateEmpty(theForm.firstname);
  reason += validateEmpty(theForm.lastname);
  reason += validateEmpty(theForm.dealership);
  reason += validateComma(theForm.youremail);
  reason += validateComma(theForm.firstname);
  reason += validateComma(theForm.lastname);
  reason += validateComma(theForm.dealership);
  
  
  
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

//Validate Virtual Demo Form
function validateFormOnSubmitVD(theForm) {
var reason = "";

  reason += validateEmail(theForm.email);
  reason += validateEmpty(theForm.firstname);
  reason += validateEmpty(theForm.lastname);
  reason += validateEmpty(theForm.dealer);
  reason += validatePhone(theForm.phone);
  reason += validateComma(theForm.firstname);
  reason += validateComma(theForm.email);
  reason += validateComma(theForm.lastname);
  reason += validateComma(theForm.dealer);
  reason += validateComma(theForm.phone);
  
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

//Validate Apples to Apples Form
function validateFormApples(theForm) {
var reason = "";

  reason += validateEmail(theForm.email);
  reason += validatePhone(theForm.telephone);
  reason += validateEmpty(theForm.firstName);
  reason += validateEmpty(theForm.lastName);
  reason += validateEmpty(theForm.dealer);
  reason += validateEmpty(theForm.vendor);
  reason += validateEmpty(theForm.product);
  reason += validateEmpty(theForm.price);
  reason += validateComma(theForm.email);
  reason += validateComma(theForm.telephone);
  reason += validateComma(theForm.firstName);
  reason += validateComma(theForm.lastName);
  reason += validateComma(theForm.dealer);
  reason += validateComma(theForm.vendor);
  reason += validateComma(theForm.product);
  reason += validateComma(theForm.price);
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

//Validate Reseller Form
function validateFormReseller(theForm) {
var reason = "";

  reason += validateEmail(theForm.email);
  reason += validatePhone(theForm.telephone);
  reason += validateEmpty(theForm.firstname);
  reason += validateEmpty(theForm.lastname);
  reason += validateComma(theForm.email);
  reason += validateComma(theForm.telephone);
  reason += validateComma(theForm.firstname);
  reason += validateComma(theForm.lastname);
  
  
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

//Validate Contact Us Form
function validateFormContactUs(theForm) {
var reason = "";

  reason += validateEmail(theForm.youremail);
  reason += validateEmpty(theForm.firstname);
  reason += validateEmpty(theForm.lastname);
  reason += validateEmpty(theForm.comments);
  reason += validateComma(theForm.youremail);
  reason += validateComma(theForm.firstname);
  reason += validateComma(theForm.lastname);
  reason += validateComma(theForm.comments);
  
  
  
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

//Validate Free Site Mock
function validateFormWebsite(theForm) {
var reason = "";

  reason += validateEmail(theForm.email);
  reason += validateEmpty(theForm.dealer);
  reason += validateEmpty(theForm.website);
  reason += validateEmpty(theForm.comments);
  reason += validateEmpty(theForm.firstname);
  reason += validateEmpty(theForm.lastname);
  reason += validateComma(theForm.email);
  reason += validateComma(theForm.dealer);
  reason += validateComma(theForm.website);
  reason += validateComma(theForm.comments);
  reason += validateComma(theForm.firstname);
  reason += validateComma(theForm.lastname);
  
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

//Validate Free Rank Report
function validateFormRankReport(theForm) {
var reason = "";

  reason += validateEmail(theForm.email);
  reason += validateEmpty(theForm.lastname);
  reason += validateEmpty(theForm.firstname);
  reason += validateEmpty(theForm.website); 
  reason += validateComma(theForm.email);
  reason += validateComma(theForm.lastname);
  reason += validateComma(theForm.firstname);
  reason += validateComma(theForm.website);
  
  
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

//Validate Test Drive Form
function validateFormTestDrive(theForm) {
var reason = "";

  reason += validateEmail(theForm.youremail);
  reason += validateEmpty(theForm.firstname);
  reason += validateEmpty(theForm.lastname);
  reason += validateEmpty(theForm.dealership);
  reason += validateComma(theForm.youremail);
  reason += validateComma(theForm.firstname);
  reason += validateComma(theForm.lastname);
  reason += validateComma(theForm.dealership);
  
  
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

