function colorIt(x){
  if (x=="e"){
    document.getElementById('subcategory').setAttribute("style","color:");}
  else {
    document.getElementById('subcategory').setAttribute("style","color: #999999");}
}
function SubCategorize() {
  var elSel = document.getElementById('subcategory');
  var el = document.getElementById('category');
  if (elSel.length>0)  {
    for (x=0;x=elSel.length-1;x++)
    {elSel.remove(x);}
    elSel.remove(0);}
  if (el.value=="Conferences")
  {elSel.disabled=false; colorIt("e"); var elValues=new Array("--Select--","Christian Womanhood Spectacular","Pastors' School","Power Club Kids' Conference","Youth Conference")}
  else if (el.value=="Media")
  {elSel.disabled=false; colorIt("e"); var elValues=new Array("--Select--","Radio","Television","Website","Other")}
  else if (el.value=="Ministries")
  {elSel.disabled=false; colorIt("e"); var elValues=new Array("--Select--","Bus Ministry","Chapel Ministry","Christian Womanhood","Deaf Ministry","F.B.M.I.","Hyles Publications","Hyles Publications","Memory Land Cemetery","Phoster Club","Reformers Unanimous","Sailor Ministry","Spanish Department","Youth Baseball League","Youth Department","Other")}
  else if (el.value=="Schools")
  {elSel.disabled=false; colorIt("e"); var elValues=new Array("--Select--","City Baptist Schools","Hammond Baptist Schools","Hyles-Anderson College")}
  else if (el.value=="Schools")
  {elSel.disabled=false; colorIt("e"); var elValues=new Array("--Select--","City Baptist Schools","Hammond Baptist Schools","Hyles-Anderson College")}
  else
  {elSel.disabled=true; colorIt("d"); var elValues=new Array("No Subcategory")}
  if (elValues.length>0){
    for (i=0;i<elValues.length;i++)  {
      var elOptNew = document.createElement('option');
      elOptNew.text = elValues[i]; elOptNew.value = elValues[i];;
      try {elSel.add(elOptNew, null);}
      catch(ex) {elSel.add(elOptNew);}}
  }
}

function validateForm(theForm) {
  var why = "";
  why += checkName(theForm.first_name.value,"first");
  why += checkName(theForm.last_name.value,"last");
  why += checkEmail(theForm.email.value);
  why += checkDropDown(theForm.category.value,theForm.subcategory.value);
  why += checkPhone(theForm.phone.value);
  why += checkMessage(theForm.message.value);
  if (why != "") {
    document.getElementById('errorDetails').innerHTML=why;
    return false;
  } else {
    document.getElementById('Submit').style.display="none";
    return true;}
}

function checkName(strng,name) {
  var error = "";
  if (strng.length == 0) {
    error = "<li>You did not enter a "+name+" name.</li>"}
  if (strng.length != 0 && strng.length < 2) {
    error = "<li>Your "+name+" name should be at least 2 letters long.</li>"}
  //var illegalChars = /\W/; //allow only letters, numbers, and underscores
  var illegalChars = /[^A-Z a-z]/;
  if (illegalChars.test(strng)) {
    error = "<li>Your "+name+" name should only contain alphabetical letters.</li>";
  } 
  return error;    
}

function checkDropDown (strng,strng2) {
  var error=""
  if (strng == "--Select--"||strng == "") {
    error = "<li>You did not select a category.</li>";}
  else if (strng2 == "--Select--"||strng2 == "") {
    error = "<li>You did not select a sub category.</li>";
  }
  return error;
}

function checkEmail (strng) {
  var error="";
  if (strng == "") {
    error = "<li>You did not enter an email address.</li>";}
  var emailFilter=/^.+@.+\..{2,4}$/;
  if (!(emailFilter.test(strng))) { 
    error = "<li>Please enter a valid email address.</li>";}
  else {
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
    if (strng.match(illegalChars)) {
      error = "<li>Your email address contains illegal characters.</li>";}
  }
  return error;    
}

function checkMessage(strng) {
  var error = "";
  if (strng.length == 0) {
    error = "<li>You did not enter a message.</li>"}
  if (strng.length != 0 && strng.length < 15) {
    error = "<li>Your message is too short.</li>"}
  return error;    
}

function checkPhone (strng) {
  //test function for in domestic numbers and international numbers (length)
  var error=""
  if (strng != ""||strng.length>0) {
    var phoneFilter=/[^0-9\(\)\-\ \+]/;
    if ((phoneFilter.test(strng))) {
      error = "<li>Your phone number must only contain numbers, dashes, parentheses, and a plus for international numbers.</li>";}
    else {
      if (strng.length<10) {
        error = "<li>Your phone number is too short. Don't forget to include your area code.</li>";}
    }
  }
  return error;
}