
// Copyright (C) 2009 Aaron Merriman. All rights reserved.

function validate_contact() {
 var con_val = new Array(0,document.forms['contact_us'].name_first.value,
                           document.forms['contact_us'].name_last.value,
                           document.forms['contact_us'].email.value,
                           document.forms['contact_us'].phone.value,
                           document.forms['contact_us'].message.value),
     chk_pos = 0,
     chk_nam = /[^A-Za-z0-9 ]/,
     err_val = new Array();
 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('cn_01').className = "error";
  err_val[1] = 1;
 } else {
  document.getElementById('cn_01').className = "";
 }
 if (con_val[2].length < 1 || chk_nam.test(con_val[2])) {
  document.getElementById('cn_02').className = "error";
  err_val[2] = 1;
 } else {
  document.getElementById('cn_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('.') || con_val[3].lastIndexOf('_') > con_val[3].lastIndexOf('.')) {
  document.getElementById('cn_03').className = "error";
  err_val[3] = 1;
 } else {
  document.getElementById('cn_03').className = "";
 }
 if (con_val[5].length < 1) {
  document.getElementById('cn_05').className = "error";
  err_val[5] = 1;
 } else {
  document.getElementById('cn_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] == 1) {
   document.forms['contact_us'].name_first.focus();
  } else
   if (err_val[2] == 1) {
    document.forms['contact_us'].name_lst.focus();
   } else
    if (err_val[3] == 1) {
     document.forms['contact_us'].email.focus();
    } else
     if (err_val[5] == 1) {
      document.forms['contact_us'].message.focus();
     }
  } else {
   document.forms['contact_us'].submit();
  }
}

function confirm_save() {
 var temp_value = document.forms['manage_data'].managed_data.value;
 if (temp_value.length < 1) {
  if (confirm('You are about to save an empty file. This operation cannot be undone. Proceed?')) {
   document.forms['manage_data'].submit();
  }
 } else {
  document.forms['manage_data'].submit();
 }
}
