/***   functions for Tech Support form ***/
var supportFlds = new Array('name', 'email', 'school', 'userType', 'subject', 'emailText');

var reqdFlds = new Array('name', 'email', 'school', 'subject', 'emailText');

var attrLabels = new Array(labels['name'], labels['email'], labels['schoolinst'], labels['ineedhelp'], labels['message']);

/**********************************************************************/

/** check required fields   **/
function validate(theForm) {
  var valid = true;
  var errmsg = "";

  // check for required fields
  var missingFlds = new Array();

  // check that there is a value for each required field 
  for (var i = 0; i < reqdFlds.length; i++) {
      var fldName = reqdFlds[i];
      var fld = theForm[fldName];
      var value = "";

      if (fld.type.indexOf("select") != -1) {
        var theChoices = fld.options;
        for (var j=0; j < theChoices.length; j++) {
          if (fld.options[j].selected) {
            value = fld.options[j].value;
            break;
          }
        }
        if (value.indexOf("Select ") != -1) {
          value = "";
        }
      }
      else if (fld.type.indexOf("checkbox") != -1) {
        if (fld.checked) {
          value = fld.value;
        } 
      } 
      else if (fld.type.indexOf("radio") != -1) {
        for (var k = 0; k < fld.length; k++) {
          if (fld[k].checked) {
            value = fld[k].value;
          }
        }
      } 
      else {
        value = fld.value;
      }

      if (!value || value == 0) {
        var fieldLabel = attrLabels[i];
        if (fieldLabel) {
          missingFlds[missingFlds.length] = fieldLabel;
        }
      }
  }

  if (missingFlds.length != 0) {
      // construct error message
      if (errmsg) {
        errmsg += "\n\n";
      }
      var valueStr = labels['thesevals'];
      if (missingFlds.length == 1) {
        errmsg += errors['missingdata']; 
        valueStr = labels['this value'];
      }
      else {
        errmsg += errors['missingdatas']; 
      }
      errmsg += "\n\n" + missingFlds.join("\n") + "\n\n" +
                errors['pleaseenter'] + " " + valueStr + " " + 
                errors['andtryagain']
      valid = false;
  }

  if (!valid) {
    alert(errmsg);
  }
  else {
    // check for valid email address
    if (theForm.email.value) {
      valid = validEmail(theForm.email.value);
    }
  }
  return valid;
}

/** email tech support **/
function emailSupport(theForm) {
  var valid = true;
  if (!theForm) {
    theForm = document.forms[0];
  }
  valid = validate(theForm);
  if (valid) {
    theForm.rm.value = "support_save";
    theForm.section.value = "support";
  }
  return valid;
}

/** check for valid email **/
function checkEmail(emailfld) {
  var emailaddr = emailfld.value;
  var valid = true;
  if (emailaddr) {
    valid = validEmail(emailaddr);
    if (!valid) {
     alert(errors['invalemailfmt'] + "\n" + errors['reenteremail']);
     emailfld.value = "";
     document.forms[0].email.focus();
    }
  }
  return valid;
}

function sendTechSupp(theForm) {
  isValid = validate(theForm);
  if (isValid) {
    document.forms[0].rm.value = "tsform_save";
    document.forms[0].section.value = "tsform"
    document.forms[0].submit();
  }
  return isValid;
}



