// contact bar validation

function submitcontactbar()
{
  // collect data from form
  name = document.contactemailbar.name.value;
  p1 = document.contactemailbar.p1.value;
  p2 = document.contactemailbar.p2.value;
  p3 = document.contactemailbar.p3.value;
  project = document.contactemailbar.project.value;
  email = document.contactemailbar.email.value;
  // -- validation of items --
  var errs = new Array();
  // Validate Name
  if (name=='' || name.length<3) errs.push('* Please enter a valid name');
  if ( (p1.length<3 || p2.length<3 || p3.length<4) ||
       (isNaN(p1) || isNaN(p2) || isNaN(p3)) )
       errs.push('* Please provide valid 10 digits contact number');
  if (project=='') errs.push('* Please select project type');
  if (!checkEmail(email)) errs.push('* Please enter a valid email');
  // check & send 
  if (errs.length>0)
  {
    d = errs.join('\n');
    d += "\n\nDid you know we are available 24/7 ?\nCall (972)200-9841 NOW !";
    alert(d);
  }
  else
  {
    //alert('submit');
    document.contactemailbar.contactbar.value = name;
    document.contactemailbar.submit();
  }
}

// contact page validation 

function submitcontactform()
{
  // reset counters checking more than 1 field
  havecontact = 0;
  // collect data from form
  name = document.contactemailform.name.value;
  city = document.contactemailform.city.value;
  zip = document.contactemailform.zip.value;
  p11 = document.contactemailform.p11.value;
  p12 = document.contactemailform.p12.value;
  p13 = document.contactemailform.p13.value;
  p21 = document.contactemailform.p21.value;
  p22 = document.contactemailform.p22.value;
  p23 = document.contactemailform.p23.value;
  email = document.contactemailform.email.value;
  project = document.contactemailform.project.value;
  // -- validation of items --
  var errs = new Array();
  // Validate Name
  if (name=='' || name.length<3) errs.push('* Please enter a valid name');
  // Validate Name
  if (city=='' || city.length<3) errs.push('* Please provide valid city name');
  // Validate Zip if submitted
  if (zip!='') {
    if (zip.length<5 || isNaN(zip)) errs.push('* Please provide a valid 5 digit zip code');
  }
  // Validate Daytime Phone
  if (p11!='' || p12!='' || p13!='') {
   if ( (isNaN(p11) || isNaN(p12) || isNaN(p13)) ||
       (p11.length<3 || p12.length<3 || p13.length<4) ) 
       errs.push('* Daytime phone number not valid, provide 10 digits please');
   else havecontact++;
  }
  // Validate Other Phone
  if (p21!='' || p22!='' || p23!='') {
   if ( (isNaN(p21) || isNaN(p22) || isNaN(p23)) ||
       (p21.length<3 || p22.length<3 || p23.length<4) ) 
       errs.push('* Other phone number not valid, provide 10 digits please');
   else havecontact++;
  }
  // Validate Email
  if (email!='') {
   if (!checkEmail(email)) errs.push('* Email address not valid');
  }
  // -- validation of minimal requirements  --
  // have Contact
  if (havecontact==0) errs.push('* Please provide at least one valid phone number');
  // have project
  if (project=='') errs.push('* Please select project type');
  // check & send 
  if (errs.length>0)
  {
    alert(errs.join('\n'));
  }
  else
  {
    document.contactemailform.contactform.value = zip;
    document.contactemailform.submit();
  }
}

// check email helper

function checkEmail(email) {
  var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  var r = filter.test(email);
//  alert(r);
  if (!r) 
    return false;
  else 
    return true;
}
