/**
 * 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.min!="") && (e.max!=null && e.max!="")){
      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.min!="") && (e.max!=null && e.max!="")){
      // 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 && e.min!="") && (v<e.min)) ||
        ((e.max!=null && e.max!="") && (v>e.max))){        
        i
        errors += "\n- The field " + e.title + " must be a number";
        if (e.min!=null && e.min!="") {
          errors += " that is greater than or equal to " + e.min;
        }
        if (e.max!=null && e.max!="" && e.min!=null && e.min!="") {
          errors += " and equal to or less than " + e.max;
        } else if(e.max!=null && e.max!="") {
          errors += " that is equal to or less than " + e.max;
        }  
        errors += ".\n"
      }
    } 
  }
  // build message
  if (!empty_fields && !errors) {
    // save to cookie valid for 1 mins to re-populate if code validation fails
    var f = document.getElementById('registerForm');
    if (f) {
      setCookie("soscontcol318007", f.col318007.value);
      setCookie("soscontcol318006", f.col318006.value);
      setCookie("soscontcol318027", f.col318027.value);
      setCookie("soscontcol318029", f.col318029.value);
      setCookie("soscontcol318023", f.col318023.value);
      setCookie("soscontcol318028", f.col318028.value);
      setCookie("soscontcol318021", f.col318021.value);
      //setCookie("soscontcol318033", f.col318033.value);
      setCookie("soscontcol318014", f.col318014.value);
      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;
}

function createLoad() {
  var f = document.getElementById('registerForm');
  if (f) {
    if (getCookie("soscontcol318007") != "") { f.col318007.value = getCookie("soscontcol318007"); }
    if (getCookie("soscontcol318006") != "") { f.col318006.value = getCookie("soscontcol318006"); }
    if (getCookie("soscontcol318027") != "") { f.col318027.value = getCookie("soscontcol318027"); }
    if (getCookie("soscontcol318029") != "") { f.col318029.value = getCookie("soscontcol318029"); }
    if (getCookie("soscontcol318023") != "") { f.col318023.value = getCookie("soscontcol318023"); }
    if (getCookie("soscontcol318028") != "") { f.col318028.value = getCookie("soscontcol318028"); }
    if (getCookie("soscontcol318021") != "") { f.col318021.value = getCookie("soscontcol318021"); }
    //if (getCookie("soscontcol318033") != "") { f.col318033.value = getCookie("soscontcol318033"); }
    if (getCookie("soscontcol318014") != "") { f.col318014.value = getCookie("soscontcol318014"); }
  }
}

function setCookie(c_name,value)
{
  var exdate=new Date();
  exdate.setTime(exdate.getTime() + 1*60*1000);
  var c_value=escape(value) + "; expires="+exdate.toUTCString() + ";";
  document.cookie=c_name + "=" + c_value;
}

function getCookie(c_name)
{
  var i,x,y,ARRcookies=document.cookie.split(";");
  for (i=0;i<ARRcookies.length;i++)
  {
    x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
    y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
    x=x.replace(/^\s+|\s+$/g,"");
    if (x==c_name)
    {
      return unescape(y);
    }
  }
  return "";
}

/**
 * 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,"?");
      }
    }
  }
  
}


