﻿
var red = 'red requird';
var required = 'requird';
var firstName;
var lblFirstName;
var lastName;
var lblLastName;
var email;
var lblEmail;
var firstNameCheck;
var lastNameCheck;
var emailCheck;
var howDid;
var checkHowDid;
var phone;
var home;
var cell;
var work;
var phoneCheck = false;


function ValidateForm() {

   var returnval = false;
   var errorMsg = document.getElementById('errormessage');

   Process()
   ShowMessages();


   if (phoneCheck && checkHowDid && lastNameCheck && emailCheck) {
      returnval = true
   }
   else {
      errorMsg.className = 'visible';
   }

   return returnval;
}

function Process() {

   firstName = document.getElementById('firstName');
   lblFirstName = document.getElementById('lblfirstname');
   firstName.value = firstName.value.trim();


   lastName = document.getElementById('lastName');
   lblLastName = document.getElementById('lbllastname');
   lastName.value = lastName.value.trim();

   email = document.getElementById('email');
   lblEmail = document.getElementById('lblemail');
   email.value = email.value.trim();

   firstNameCheck = ValidateFieldsId(firstName);
   lastNameCheck = ValidateFieldsId(lastName);
   emailCheck = ValidateEmail(email);

   howDid = document.getElementById('howdid');
   checkHowDid = ValidateCheckBox();

   phone = document.getElementById('phone');

   home = document.getElementById('home');
   home.value = home.value.trim();

   cell = document.getElementById('cell');
   cell.value = cell.value.trim();

   work = document.getElementById('work');
   work.value = work.value.trim();

   phoneCheck = ValidatePhone(home, cell, work)

}

function ValidatePhone(home, cell, work) {

   var homeCheck = ValidateFieldsId(home);
   var cellCheck = ValidateFieldsId(cell);
   var workCheck = ValidateFieldsId(work);
   var phoneExpression = /^(\d{3}-\d{3}-\d{4})/;
   var count = 0;

   // All 3 fields are blank
   if (!homeCheck && !cellCheck && !workCheck) {
      return false;
   }
   else {

      // check for valid phone numbers

      if (homeCheck) {
         var homeNumber = home.value;
       //  if (!homeNumber.match(phoneExpression)) {
        //    return false;
       //  }
       //  else {
       //     count++;
      //   }
	  count++;
      }

      if (cellCheck) {
         var cellNumber = cell.value;
       //  if (!cellNumber.match(phoneExpression)) {
       //     return false;
      //   }
        // else {
       //     count++;
       //  }
	   count++;
      }

      if (workCheck) {
         var workNumber = work.value;
       //  if (!workNumber.match(phoneExpression)) {
       //     return false;
       //  }
      //   else {
      //      count++;
      //   }
	  count++;
      }
   }

   if (count > 0) {
      return true
   }
}

function ShowMessages() {

   if (!checkHowDid) {
      howDid.className = red;
   }
   else {
      howDid.className = required;
   }

   if (!firstNameCheck) {
      lblFirstName.className = red;
   }
   else {
      lblFirstName.className = required
   }

   if (!lastNameCheck) {
      lblLastName.className = red;
   }
   else {
      lblLastName.className = required
   }

   if (!emailCheck) {
      lblEmail.className = red;
   }
   else {
      lblEmail.className = required;
   }


   if (!phoneCheck) {
      phone.className = red;
   }
   else {
      phone.className = required;
   }
}

function ValidateFieldsId(field) {
   with (field) {

      if (value == null || value == "") {
         return false;
      }
      else {
         return true;
      }
   }
}

function ValidateEmail(field) {
   with (field) {
      apos = value.indexOf("@");
      dotpos = value.lastIndexOf(".");
      if (apos < 1 || dotpos - apos < 2) {
         return false;
      }
      else {
         return true;
      }
   }
}

function ValidateCheckBox() {
   var heard = 'heard-';
   var myArray = ['website', 'referral', 'community', 'radio', 'family', 'truck',
                   'internet', 'television', 'news', 'used', 'newspaper', 'yellowPages', 'other'];

   for (var x = 0; x < myArray.length; x++) {
      var checkBox = document.getElementById(heard + myArray[x]);
      if (checkBox.type == "checkbox") {
         if (checkBox.checked == 1) {
            return true;
         }

      }
   }

   return false;
}
