//
// form element input validation routines
//

/* for input.text controls taking numeric values
  specify control instance
  specify number of valid characters required
  specify whether any other chars allowed
  function returns true when conditions met
*/  
function validNumerals(c,q,a) {
  var flgRtn = false;
  var validCnt = 0;
  var inVal, inChar;
  if (c != undefined) {
    inVal = c.value;
    for (var n=0; n<inVal.length; n++) {
      inChar = parseInt(inVal.charAt(n));
      if (!isNaN(inChar)) { 
        validCnt++;
      }
    }
    if (validCnt==q) {
       flgRtn = true;
    }
    if ((!a) && (validCnt < inVal.length)) {
       flgRtn = false;
    }
  }
  return flgRtn;
}

function validate(actFlag) {
  var valid1 = valid2 = valid3 = valid4 = valid5 = valid6 = true;
  var err = errtmp = "";

  // check text fields
  errtmp = MM_validateForm('fm_name','','R', 'fm_email','','RisEmail', 'fm_phone','','R', 'fm_address','','R', 'fm_city','','R', 'fm_state','','R', 'fm_zip','','RisNum');
  if (errtmp != "") {
    err += errtmp;
    valid2 = false;
  }

  // extra checks
  valid3 = validNumerals(document.changeAddress.fm_phone, 10, true);
  if (!valid3) {
    err += "\nPhone number must be ten digits";
  }

  valid4 = validNumerals(document.changeAddress.fm_zip, 5, false);
  if (!valid4) {
    err += "\nZIP Code must be five digits";
  }

  if (!valid1 || !valid2 || !valid3 || !valid4 || !valid5 || !valid6) {
    alert("There seems to be a problem with the following input:"+err);
  }

  if (valid1 && valid2 && valid3 && valid4 && valid5 && valid6) { 
    if (actFlag=="send") {
      document.changeAddress.submit();
      return true;
    } else if (actFlag=="print") {
      print();
      window.close();
    } else {
      displayElements();   // DTE action
    }
  }
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='\n'+nm+' must contain an e-mail address.';
      } else if (test!='R') {
        if (isNaN(val)) errors+='\n'+nm+' must contain a number.';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (val<min || max<val) errors+='\n'+nm+' must contain a number between '+min+' and '+max+'.';
    } } } else if (test.charAt(0) == 'R') errors += '\n'+nm+' is required.'; }
  }
  return (errors);
}

// *********************************
// DT&E FUNCTION ONLY - remove from final version
function displayElements() {
  var response = "";
  var len = document.forms[0].length;
  if (len > 50 ) { len = 50; }
  for ( var n=0; n<len; n++) {
    if (document.forms[0].elements[n].type != "button") {
      response = response + document.forms[0].elements[n].name + ":" + document.forms[0].elements[n].value + "\n";
    }
  }
  alert(response);
  response = "..CONTINUED..\n";
  if (document.forms[0].length >= 50) {
    for ( var n=50; n<document.forms[0].length; n++) {
      if (document.forms[0].elements[n].type != "button") {
        response = response + document.forms[0].elements[n].name + ":" + document.forms[0].elements[n].value + "\n";
      }
    }
    alert(response);
  }
}
// *********************************
