// JavaScript Document

// Close the specials div
function toggleLayer(whichLayer) {
  var elem, vis;
  if(document.getElementById)
    elem = document.getElementById(whichLayer);
  else if(document.all)
      elem = document.all[whichLayer];
  else if(document.layers)
    elem = document.layers[whichLayer];
  vis = elem.style;
  if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
    vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
  vis.display = (vis.display==''||vis.display=='block')?'none':'block';
}

// Form Validation
function validateFormStart(theForm) {
var reason = "";

  reason += validateEmpty(theForm.certto);
  reason += validateEmpty(theForm.certamount);
  reason += validateEmpty(theForm.certto_first);
  reason += validateEmpty(theForm.certto_last);
  reason += validateEmail(theForm.certto_email);
  reason += validateEmpty(theForm.certto_address1);
  reason += validateEmpty(theForm.certto_city);
  reason += validateEmpty(theForm.certto_state);
  reason += validateEmpty(theForm.certto_zip);
  reason += validateEmpty(theForm.certto_shipping);
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

function validateFormComplete(theForm) {
var reasons = "";

  reasons += validateEmpty(theForm.bname);
  reasons += validateEmpty(theForm.baddr1);
  reasons += validateEmpty(theForm.bcity);
  reasons += validateEmpty(theForm.bstate);
  reasons += validateEmpty(theForm.bzip);
  reasons += validateEmpty(theForm.cctype);
  reasons += validateEmpty(theForm.cardnumber);
  reasons += validateEmpty(theForm.expmonth);
  reasons += validateEmpty(theForm.expyear);
  reasons += validateEmpty(theForm.cvm);
      
  if (reasons != "") {
    alert("Some fields need correction:\n" + reasons);
    return false;
  }

  return true;
}

function validateEmpty(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "The required field has not been filled in.\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);
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {
        fld.style.background = 'Yellow';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
