/**
 * validation.js
 * A file which contains validation routines which
 * will be called by JS in the onsubmit attribute of a
 * form.
 */
 
function verify(f) {

  var msg;
  var empty_fields="";
  var errors="";
  
  var verifyBilling = false;

  // return if there has been a cancel
  if(f.novalidation) return true
  
  // loop through items in form
  for(var i = 0; i<f.length; i++){
    var e = f.elements[i]
    // planID > 1 then verify billing elements
    if (e.name == "planID" && e.checked && (e.value > 1 && e.value < 5)) {
      verifyBilling = true;
    }

    // required billing address fields - these should not be
    // set required on the account create form.
    if (verifyBilling && e.name.substring(0,7) == "billing") {
      if (e.value=="" && e.name != "billingAddress2") {
        empty_fields += "\n- " + e.title;
      }
      continue;
    }

    // check is required
    if (e.required){
      if(e.type=="text" || 
        e.type=="textarea" || 
        e.type=="password") {
        if((e.value==null) || (e.value=="")){
          empty_fields +="\n- " + e.title;
          continue;
        }
      } else if (e.type=="select-one"){
        if (e.options[e.selectedIndex].value=="") {
          continue;        
        }
      } else if (e.type="checkbox") {
        if (e.checked == false) {
          empty_fields +="\n- " + e.title;
	  continue;
	}
        continue;
      }
    }
    // check string and email
    if ((e.wwtype=="string" || e.wwtype=="email") && (e.min!=null) && (e.max!=null)){
      if ((e.value.length<e.min) || (e.value.length>e.max)){
        errors += "\n- " + e.title + " must be between " + 
          e.min + " and " + e.max + " characters in length";
      }
    }
    
    // check email
    // http://www.codetoad.com/javascript/is_valid_email.asp
    if (e.wwtype=="email" &&
        e.value.length>0 &&
        e.value.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1) {
      errors += "\n- " + e.title + " is not a properly formed email address";

    }    
    // check numeric
    if ((e.wwtype=="numeric") && (e.min!=null) && (e.max!=null)){
      // nulls are passed through
      if((e.type=="text" || 
        e.type=="textarea" || 
        e.type=="password") &&
        e.value==null || e.value==""){
        continue;
      } else if (e.type=="select-one" && e.options[e.selectedIndex].value==""){
        continue;
      }
      // numeric and in range?
      var v = parseFloat(e.value)
      if ((isNaN(v)) || 
        ((e.min!=null) && (v<e.min)) ||
        ((e.max!=null) && (v>e.max))){
        
        errors += "\n- The field " + e.title + " must be a number";
        if (e.min!=null) {
          errors += " that is greater than or equal to " + e.min;
        }
        if (e.max!=null && e.min!=null) {
          errors += " and equal to or less than " + e.max;
        } else if(e.max!=null) {
          errors += " that is equal to or less than " + e.max;
        }  
        errors += ".\n"
      }
    } 
  }
  // build message
  if (!empty_fields && !errors) return true;
  msg = "The form was not submitted because of the following errors.\n";
  if (empty_fields){
    msg += "\nThe following required fields are empty:" + 
      empty_fields + "\n";
    if (errors) msg+= "\n";
  }
  if (errors) {
    msg += "The following errors occurred on the form: " +
      errors;
  }
  alert(msg)
  return false;
}

/**
 * FileUp fails to read the form when IE 6 sends a multipart
 * form with ms extended chars in a text with a checkbox. This script
 * is an unsophisticated way to stop the curly quotes being 
 * sent along with the form.
 */
function cleanMSExtendedChars(f){
  for(var i = 0; i<f.length; i++){
    var e = f.elements[i]
    if(e.type=="text" || e.type=="textarea" || e.type=="password") {
      if(!((e.value==null) || (e.value==""))){
        e.value=e.value.replace(/€/g,"Euro");
        e.value=e.value.replace(/‚/g,"'");
        e.value=e.value.replace(/ƒ/g,"f");
        e.value=e.value.replace(/„/g,"\"");
        e.value=e.value.replace(/…/g,"...");
        e.value=e.value.replace(/†/g,"*");
        e.value=e.value.replace(/‡/g,"**");
        e.value=e.value.replace(/ˆ/g,"^");
        e.value=e.value.replace(/‰/g," ");
        e.value=e.value.replace(/Š/g,"S");
        e.value=e.value.replace(/‹/g,"'");
        e.value=e.value.replace(/Œ/g,"OE");
        e.value=e.value.replace(/Ž/g,"Z");
        e.value=e.value.replace(/‘/g,"'");
        e.value=e.value.replace(/’/g,"'");
        e.value=e.value.replace(/“/g, "\"");
        e.value=e.value.replace(/”/g, "\"");
        e.value=e.value.replace(/•/g,"*");
        e.value=e.value.replace(/–/g,"-");
        e.value=e.value.replace(/—/g,"--");
        e.value=e.value.replace(/˜/g,"~");
        e.value=e.value.replace(/™/g,"TM");
        e.value=e.value.replace(/š/g,"s");
        e.value=e.value.replace(/›/g,"'");
        e.value=e.value.replace(/œ/g,"oe");
        e.value=e.value.replace(/ž/g,"z");
        e.value=e.value.replace(/Ÿ/g,"Y");
        // All other unicode values >256
        e.value=e.value.replace(/[\u00FF-\uFFFF]/g,"?");
      }
    }
  }
  
}

