
// Copyright (C) 2008 Aaron Merriman, AaronMerriman.com. All rights reserved.

function blurred(tmp_obj) {
 if (tmp_obj.value == "") {
  tmp_obj.value = tmp_obj.defaultValue;
 }
}

function focused(tmp_obj) {
 if (tmp_obj.value == tmp_obj.defaultValue) {
  tmp_obj.value = "";
 }
}

function eb_validate(eb_val) {
 var chk_pos = 0,
     chk_nam = /[^A-Za-z0-9 ]/,
     err_val = new Array();

 switch(eb_val) {
  case 1:
   con_val = new Array(0,document.forms['eb_consult'].name_first.value,
                         document.forms['eb_consult'].name_last.value,
                         document.forms['eb_consult'].email.value,
                         document.forms['eb_consult'].phone.value);

   for (var tmp_val = 1; tmp_val < con_val.length; tmp_val++) {
    con_val[tmp_val] = con_val[tmp_val].replace(/^\s+|\s+$/g,'');
   }
   for (var tmp_val = 0; tmp_val < con_val[3].length; tmp_val++) {
    if (con_val[3].charAt(tmp_val) == "@") {
     chk_pos++;
    }
   }
   if (con_val[1].length < 1 || chk_nam.test(con_val[1]) || con_val[1].toLowerCase() == "first name") {
    document.getElementById('ebu_01').className = "field_consult fl_01 input_error_consult";
    err_val[1] = 1;
   } else {
    document.getElementById('ebu_01').className = "field_consult fl_01";
   }
   if (con_val[2].length < 1 || chk_nam.test(con_val[2]) || con_val[2].toLowerCase() == "last name") {
    document.getElementById('ebu_02').className = "field_consult fl_01 input_error_consult";
    err_val[2] = 1;
   } else {
    document.getElementById('ebu_02').className = "field_consult fl_01";
   }
   if (con_val[3].length < 5 || /[^A-Za-z0-9\-\.@_]|\.@|@\.|\.\.|^\.|\.$|!\./.test(con_val[3]) || chk_pos != 1 || con_val[3].lastIndexOf('@') > con_val[3].lastIndexOf('.') || con_val[3].lastIndexOf('-') > con_val[3].lastIndexOf('.')) {
    document.getElementById('ebu_03').className = "field_consult fl_02 input_error_consult";
    err_val[3] = 1;
   } else {
    document.getElementById('ebu_03').className = "field_consult fl_02";
   }
   if (con_val[4].length < 1 || con_val[4].toLowerCase() == "phone") {
    document.getElementById('ebu_04').className = "field_consult fl_01 input_error_consult";
    err_val[4] = 1;
   } else {
    document.getElementById('ebu_04').className = "field_consult fl_01";
   }
   if (err_val.length > 0) {
    alert('Oops! One or more required fields are invalid or incomplete (noted in red).\nPlease revise and re-click the \'Go!\' button.');
    if (err_val[1] > 0) {
     document.forms['eb_consult'].name_first.focus();
    } else {
     if (err_val[2] > 0) {
      document.forms['eb_consult'].name_last.focus();
     } else {
      if (err_val[3] > 0) {
       document.forms['eb_consult'].email.focus();
      } else {
       if (err_val[4] > 0) {
        document.forms['eb_consult'].phone.focus();
       }
      }
     }
    }
   } else {
    document.forms['eb_consult'].submit();
   }
   break;
  case 2:
   con_val = new Array(0,document.forms['eb_contact'].name_first.value,
                         document.forms['eb_contact'].name_last.value,
                         document.forms['eb_contact'].email.value,
                         document.forms['eb_contact'].phone.value,
                         document.forms['eb_contact'].message.value);

   for (var tmp_val = 1; tmp_val < con_val.length; tmp_val++) {
    con_val[tmp_val] = con_val[tmp_val].replace(/^\s+|\s+$/g,'');
   }
   for (var tmp_val = 0; tmp_val < con_val[3].length; tmp_val++) {
    if (con_val[3].charAt(tmp_val) == "@") {
     chk_pos++;
    }
   }
   if (con_val[1].length < 1 || chk_nam.test(con_val[1])) {
    document.getElementById('ebc_01').className = "input_error_contact";
    err_val[1] = 1;
   } else {
    document.getElementById('ebc_01').className = "";
   }
   if (con_val[2].length < 1 || chk_nam.test(con_val[2])) {
    document.getElementById('ebc_02').className = "input_error_contact";
    err_val[2] = 1;
   } else {
    document.getElementById('ebc_02').className = "";
   }
   if (con_val[3].length < 5 || /[^A-Za-z0-9\-\.@_]|\.@|@\.|\.\.|^\.|\.$|!\./.test(con_val[3]) || chk_pos != 1 || con_val[3].lastIndexOf('@') > con_val[3].lastIndexOf('.') || con_val[3].lastIndexOf('-') > con_val[3].lastIndexOf('.')) {
    document.getElementById('ebc_03').className = "input_error_contact";
    err_val[3] = 1;
   } else {
    document.getElementById('ebc_03').className = "";
   }
   if (con_val[4].length < 1) {
    document.getElementById('ebc_04').className = "input_error_contact";
    err_val[4] = 1;
   } else {
    document.getElementById('ebc_04').className = "";
   }
   if (con_val[5].length < 1) {
    document.getElementById('ebc_05').className = "input_error_contact";
    err_val[5] = 1;
   } else {
    document.getElementById('ebc_05').className = "";
   }
   if (err_val.length > 0) {
    alert('Oops! One or more required fields are invalid or incomplete (noted in red).\nPlease revise and re-click the \'Send Now\' button.');
    if (err_val[1] > 0) {
     document.forms['eb_contact'].name_first.focus();
    } else {
     if (err_val[2] > 0) {
      document.forms['eb_contact'].name_last.focus();
     } else {
      if (err_val[3] > 0) {
       document.forms['eb_contact'].email.focus();
      } else {
       if (err_val[4] > 0) {
        document.forms['eb_contact'].phone.focus();
       } else {
        if (err_val[5] > 0) {
         document.forms['eb_contact'].message.focus();
        }
       }
      }
     }
    }
   } else {
    document.forms['eb_contact'].submit();
   }
   break;
 }
}
