//
// 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;
}

/* check for a valid (well-formed) email entry */
function isValidEmail(email) {
  rePattern = /^[A-Z0-9._%'-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/ig;
  isValid = rePattern.exec (email);
  if (isValid != null) {
    return true;
  } else {
    return false;
  }
}

//
// validate3() used on ajs_reprints.asp
//
function validate3(actFlag) {
  var valid1 = valid2 = valid3 = valid4 = valid5 = valid6 = true;
  var err = errtmp = "";

  // check text fields
  errtmp = MM_validateForm('name','','R', 'mail','','RisEmail', 'phone','','R', 'publications','','R', 'purpose','','R');
  if (errtmp != "") {
    err += errtmp;
    valid2 = false;
  }
	// extra checks
	// phone number well formed
  valid3 = validNumerals(document.permform.phone, 10, true);
  if (!valid3) {
    err += "\nPhone number must be ten digits.";
  }
	// email well-formed
  valid1 = isValidEmail(document.permform.email.value);
  if (!valid1) {
    err += "\nPlease enter a valid e-mail address.";
  }

  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.permform.method = "POST";
			document.permform.action = "ajs_reprints.asp";
			document.permform.submitted.value = "valid";
      document.permform.submit();
      return true;
    } else if (actFlag=="print") {
      print();
      window.close();
    } else {
      displayElements();   // DTE action
    }
  }
}

//
// validate2() used on BCD_Pamphlet.asp
//
function validate2(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.subscribe.fm_phone, 10, true);
  if (!valid3) {
    err += "\nPhone number must be ten digits";
  }

  valid4 = validNumerals(document.subscribe.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.subscribe.submit();
      return true;
    } else if (actFlag=="print") {
      print();
      window.close();
    } else {
      displayElements();   // DTE action
    }
  }
}

//
// validate() used on periodical_subs.asp
//
function validate(actFlag) {
  var valid1 = valid2 = valid3 = valid4 = valid5 = valid6 = true;
  var err = errtmp = "";

  // check cc if needed
  if (document.subscribe.fm_method.value == "x") {
    err += "\nPlease indicate a Payment Method";
    valid5 = false;
  } else if (document.subscribe.fm_method.value == "card") {
    if (document.subscribe.fm_cc_type.value == "x") {
      err += "\nPlease indicate a Credit Card type";
      valid5 = false;
    }
    errtmp = MM_validateForm('fm_cc_name','','R','fm_cc_number','','R','fm_cc_expires','','R');
    if (errtmp != "") {
      err += errtmp;
      valid1 = false;
    }
  }

  // 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_country','','R', 'fm_zip','','RisNum');
  if (errtmp != "") {
    err += errtmp;
    valid2 = false;
  }

  // extra checks
  valid3 = validNumerals(document.subscribe.fm_phone, 10, true);
  if (!valid3) {
    err += "\nPhone number must be ten digits";
  }

  valid4 = validNumerals(document.subscribe.fm_zip, 5, false);
  if (!valid4) {
    err += "\nZIP Code must be five digits";
  }

  if ((isNaN(document.subscribe.fm_dj_qty.value)) || (isNaN(document.subscribe.fm_fj_qty.value)) || (isNaN(document.subscribe.fm_djc_qty.value)) || (isNaN(document.subscribe.fm_fjc_qty.value))) {
    err += "\nSubscription quantities must be numbers.";
    valid6 = false;
  } else if ((document.subscribe.fm_dj_qty.value<=0) && (document.subscribe.fm_fj_qty.value<=0) && (document.subscribe.fm_djc_qty.value<=0) && (document.subscribe.fm_fjc_qty.value<=0)) {
    err += "\nNothing has been ordered!";
    valid6 = false;
  }

  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.subscribe.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);
}

function toStrMoney( numMoney ) {
  var t1 = ""+(Math.ceil(numMoney*100))/100;
  var t2 = t1.indexOf(".");
  if (t2==-1) {
    t1 = t1+".00";
  } else if ((t1.length-t2) != 3) {
    t1 = t1+"0";
  }
  return t1;
}

function updateAmount(source, unit_price) {
  var elemBaseName = source.name.substring(0,source.name.lastIndexOf("_"));
  var elemAmtName = elemBaseName+"_amt";
  var t1 = source.value;
  if ( isNaN(t1) || t1<0 ) {
    t1 = "0.00";
  } else {
    t1 = t1 * unit_price;
    t1 = toStrMoney(t1);
  }
  eval("document.subscribe."+elemAmtName).value = t1;
  updateSubTotal();
}

function updateSubTotal() {
  var sum = 0;
  sum += document.subscribe.fm_dj_amt.value/1;
  sum += document.subscribe.fm_fj_amt.value/1;
  sum += document.subscribe.fm_djc_amt.value/1;
  sum += document.subscribe.fm_fjc_amt.value/1;
  document.subscribe.fm_total.value = toStrMoney(sum);
}

// *********************************
// 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);
  }
}
// *********************************
