
// nls-sensitive strings go here
var msg_header= "_____________________________________________________\n\n" +
	        "This form has not been submitted because of the following error(s).\n" +
	        "Please correct the error(s) and re-submit.\n" +
	        "_____________________________________________________\n\n";
var msg_baduid= "Your userid must contain 1-20 characters. Only a-z,A-Z,_, @, -, _, ., and 0-9 are allowed.  There must be no blanks." ;
var msg_badpwd= "Your password must contain 6-20 characters. Only a-z,A-Z, @, -, _, ., and 0-9 are allowed.  There must be no blanks. " 
var msg_uid1= "Your password must not match your userid.";
var msg_uid2= "Your password and verification must be the same.";
var msg_uid3= "Your userid must contain 1-20 characters. Only a-z,A-Z,_, @, -, ., and 0-9 are allowed.  There must be no blanks." ;
var msg_email1= "Your e-mail address must be of the form name@server.domain.";
var msg_noans= "The following required questions were unanswered:"; 
// end nls-sensitive strings

function SelectOther(option_name, textvalue) {
// Find the radio button, checkbox or selection which has the 'Other value and
// select it, but only if the passed text value is blank.
var i
var typeobj=eval("document.mailform." + option_name + ".type")

var justblanks=/^\s*$/
var othertest=/^\s*(.\:){0,1}\s*other\s*$/i

var option = eval ("document.mailform." + option_name) 
var typeobj=option.type

if (justblanks.test(textvalue)) return;

// selection lists
if (typeobj == "select-one" || typeobj == "select-multiple"){
  for (i=0;i<option.options.length;i++){
    if (othertest.test(option.options[i].value)){
      option.options[i].selected = true;
      return;
    }
  }
}

//radio buttons and checkboxes
if (typeobj != "text"){
  for (i=0;i<option.length;i++){
    if (othertest.test(option[i].value)){
      if (!(option[i].checked)){
         option[i].click();
      }
      return;
    }
  }
}
}

function isblank(s)
{ if((s!= '') && (s!= ' ') && (s!= '\n') && (s != '\t')) 
     return false;

return true;
}

function isEmail(s) {
                if ((s.indexOf('@', 0) == -1) || s.indexOf('.') == -1) 
                        return false;
return true;
}

function wuichars(s) {
// Weak userid rules allow 1-20 chars, no blanks and only alpha,_ @ - . & digits
// for userid
var weakTest=/^[a-z,\_,A-Z,0-9,\@,\-,\.]{1,20}$/
var found=weakTest.test(s)

if (found) return true;
return false;
}

function wpwchars(s) {
// Weak pw rules allow 1-20 chars, no blanks and only alpha _ @ - . & digits
// for userid and pw
var weakTest=/^[a-z,A-Z,0-9,\@,\-,\_,\.]{1,20}$/
var found=weakTest.test(s)

if (found) return true;
return false;
}


function spwchars(s) {
// Strong userid/pw rules require 6-20 alpha and digits and _ @ - .
var strTest1=/^[a-z,A-Z,0-9,\@,\-,\_,\.]{6,20}$/
var found1=strTest1.test(s)

if (!found1) return false;
return true;

}

function getRealValue(realObject)
{
lenobj=realObject.length // this will be undefined for empty text objects
                         // for IE 5; 0 for Netscape 4.
                         // IE 5 will continue, though.  If we try to
                         // reference some undefined properties, IE 5 stops

typeobj=realObject.type

// check  text entry, text area, anything other than selection list
if (typeobj == "text" || typeobj == "textarea" || typeobj == "password"){
    if (realObject.value.length != 0){
      if (isblank(realObject.value)) return false
       else return true
    }
    else return false
}
else{
// check selection list
 if (typeobj == "select-one" || typeobj == "select-multiple"){
   if (realObject.selectedIndex != -1) {
     if (realObject.options[realObject.selectedIndex].value != ""){
           return true
     }
   }
  return false
 }
 else {
//check radio object or checkbox
//with only one checkbox or button we have to handle specially
 isarray=typeof(realObject[1]);
 if (isarray == "undefined"){
  if (realObject.checked){
    return true
  }
  else{
    return false
  }
 }
//more than one item to pick from:

     for (x=0;x<realObject.length; x++)
      {
	if (realObject[x].checked)
	{
	    return true
	}
     }
   return false
 }
}
// Looks like it's empty!
return false;  

}


function FillIt(fname){
// For a given field, determine the ship field name and demoq field name,
// then assign ship to demoq value if ship value is null.
// Both ship and demoq will either exist as R_REGxxx or REGxxx,
// depending on if they are required fields or not.
var shipname;
var demorname;
var demoname; 
var exists;
eval("shipname=document.mailform.R_REG_S"+fname);
if ((exists=typeof(shipname)) == "undefined"){
  eval("shipname=document.mailform.REG_S"+fname);
}
eval("demoname=document.mailform.REG_"+fname);
eval("demorname=document.mailform.R_REG_"+fname);
  if (isblank(shipname.value)){
    if ((exists=typeof(demorname)) != "undefined"){
      shipname.value=demorname.value;
    }
    else if ((exists=typeof(demoname)) != "undefined"){
      shipname.value=demoname.value;
    }
  }
return true;  
}

function FillAddr(){
// Fill in address
var exists;
  if (isblank(document.mailform.R_REG_SADDRESS1.value) &&
      isblank(document.mailform.REG_SADDRESS2.value)){
    // get address line 1
    if ((exists=typeof(document.mailform.R_REG_ADDRESS1)) != "undefined"){
      document.mailform.R_REG_SADDRESS1.value=document.mailform.R_REG_ADDRESS1.value;
    }
    else if ((exists=typeof(document.mailform.REG_ADDRESS1)) != "undefined"){
      document.mailform.REG_SADDRESS1.value=document.mailform.REG_ADDRESS1.value;
    } 
    // get address line 2
    if ((exists=typeof(document.mailform.R_REG_ADDRESS2)) != "undefined"){
      document.mailform.R_REG_SADDRESS2.value=document.mailform.R_REG_ADDRESS2.value;
    }
    else if ((exists=typeof(document.mailform.REG_ADDRESS2)) != "undefined"){
      document.mailform.REG_SADDRESS2.value=document.mailform.REG_ADDRESS2.value;
    }
  }
return true;  
}

function FillCountry(){
//If shipping country is not filled in, use the one selected in the demogs
var exists;
var selectobj;
var cobj;
var countryfull;
var i;

if ((exists=typeof(document.mailform.R_REG_COUNTRY)) != "undefined") {
  selectobj=document.mailform.R_REG_COUNTRY;
}
else { 
  selectobj=document.mailform.REG_COUNTRY;
}
if ((exists=typeof(document.mailform.R_REG_COUNTRY)) != "undefined"){
  cobj=document.mailform.R_REG_SCOUNTRY;
}
else {
selectobj=document.mailform.REG_COUNTRY;
}
if ((selectobj.selectedIndex == -1) ||  // if no demog country selected
    (selectobj.selectedIndex == 0)) {
 return true;
}
countryfull=selectobj.options[selectobj.selectedIndex].value;
cparts=countryfull.split(":");  // split into x:y:z:country
// if shipping country is blank, fill it
if ((cobj.selectedIndex == -1 || cobj.selectedIndex == 0) 
    && (selectobj.selectedIndex != -1)){
  for (i=0;i<cobj.options.length;i++){
    if (cobj.options[i].value == cparts[3]){
      cobj.options[i].selected = true;
      return (true);
    }
  }
}
                      
return true;
}

function FinishShip(){
// If ship-to is on the page, then blank ship-to fields
// are assigned to the corresponding fields in demographics
var isShip=typeof(document.mailform.R_REG_SLASTNAME); // any shipping data?

if (isShip != "undefined"){
 FillIt("FIRSTNAME");
 FillIt("LASTNAME");
 FillAddr();
 FillIt("CITY");
 //Only allow JavaScript to fill in Ship State if REG_COUNTRY and REG_SCOUNTRY are the same
 if ((document.mailform.R_REG_COUNTRY.value.indexOf(document.mailform.R_REG_SCOUNTRY.value)) >= 0){
  FillIt("STATE");
 }
 FillIt("ZIP");
 FillIt("PHONE");
 FillCountry();  //please leave this last for devtool pwd update profile page..it doesn't work for them, and causes the rest of the FillIt functions to not get executed if it's in the middle.
}
return true;  
}

function checkInput(){
      
      var count=document.mailform.elements.length
      var i=0
      var endpos 
      var isQuestion
      var realname
      var unans_quest 
      var empty_fields 
      var msg 
      var IValue 
      var PWtest= typeof(document.mailform.S_PASSV)
      var PWtestR= typeof(document.mailform.R_S_PASSV)
      var UIDtestR= typeof(document.mailform.R_S_USER)
      var rqremail=typeof (document.mailform.R_REG_EMAIL)
      var item=""
      var focusobj=null 
      var pmsg


      focusobj=null
      empty_fields=""
      unans_quest = ""
      msg = ""
      pmsg = ""
      IValue = ""

      //Check the userid and password values are good
      // If we have a verify password on the page, then we
      // have all three fields for input
      if (!(PWtest == "undefined" && PWtestR == "undefined")){

       // Now, do we have the required ones or the optional?
       if (PWtestR != "undefined"){
          passfield=document.mailform.R_S_PASS;
          passvfield=document.mailform.R_S_PASSV;
       }
       else {
          userfield=document.mailform.S_USER;
          passfield=document.mailform.S_PASS;
          passvfield=document.mailform.S_PASSV;
       }
       if (UIDtestR != "undefined"){
          userfield=document.mailform.R_S_USER;
       }
       else {
          userfield=document.mailform.S_USER;
       }
         
         // userid can be only certain characters
         if (userfield.value != ""){
            if (!wuichars(userfield.value)){
              pmsg += msg_baduid + "\n";
             if (focusobj == null ){
                focusobj=userfield;
             }
            }
         }

         // password must be right characters and can't match userid
         if (passfield.value != ""){
            if (!spwchars(passfield.value)){
              pmsg += msg_badpwd + "\n";
             if (focusobj == null ){
                focusobj=passfield;
             }
            }
            if (userfield.value == passfield.value){
              pmsg += msg_uid1 + "\n";
             if (focusobj == null ){
                focusobj=passfield;
             }
            }
         }

         // verify password must match password
	 if (passfield.value != passvfield.value)
	{
       	     pmsg += msg_uid2 + "\n";
            if (focusobj == null){
                    focusobj=passfield;
            }
	    passvfield.value = ""     	
	}
       }

       //If e-mail was required, check its validity too
       if ((rqremail != "undefined") && (document.mailform.R_REG_EMAIL.value != "")){
          if (!isEmail(document.mailform.R_REG_EMAIL.value)){
       	     pmsg +=  msg_email1 + "\n";
            if (focusobj == null){
                    focusobj=document.mailform.R_REG_EMAIL
            }
          }
       }

       //If shipping address is on the form, default values 
       FinishShip();
      //checking for required naming convention and then checking that the required fields are filled in or questions answered	
      lastone="";
      for (i=0;i<count;i++)
      {
        if (document.mailform.elements[i].name != lastone){
          // REG_ radio buttons will come in one for each item, so skip extras
          
	  if ((document.mailform.elements[i].name.charAt(0)=="R") &&
	      (document.mailform.elements[i].name.charAt(1)=="_")) 
          {
	      endpos=(document.mailform.elements[i].name.length);
              isQuestion=false;
	
              if ((document.mailform.elements[i].name.charAt(2)=="Q")  &&
		  (document.mailform.elements[i].name.charAt(3)=="_"))
              {		
                  realname=(document.mailform.elements[i].name.substring(4,endpos));
                  // get the answer field name and value
		  eval("A_realname=document.mailform.A_"+realname);
                  isQuestion=true;  
              } 
              if ((document.mailform.elements[i].name.charAt(2)=="P")  &&
		  (document.mailform.elements[i].name.charAt(3)=="Q")  &&
		  (document.mailform.elements[i].name.charAt(4)=="_"))
              {		
                  realname=(document.mailform.elements[i].name.substring(5,endpos));
                  // get the answer field name and value
		  eval("A_realname=document.mailform.PA_"+realname);
                  isQuestion=true;  
              } 
              if  (isQuestion)
              {
                  // if  an I_ variable exists, use it. Otherwise, use the
                  // question name
                  IValue=(document.mailform.elements[i].name.substring(2,endpos));
		  Iname="document.mailform.I_"+realname
		  eval("if (typeof("+Iname+ ") != \"undefined\"){ IValue="+Iname+".value;}");
                  if (!getRealValue(A_realname)){	
                      unans_quest += ("\n" + IValue);
                      if (focusobj == null && A_realname.type == "text"){
                         focusobj=A_realname
                      }
                  }
              }


              else if ((document.mailform.elements[i].name.charAt(2)=="S") &&
		       (document.mailform.elements[i].name.charAt(3)=="_"))
              {	 	
		  realname=(document.mailform.elements[i].name.substring(4,endpos));
                  // if  an I_ variable exists, use it. Otherwise, use the
                  // question name
                  IValue=(document.mailform.elements[i].name.substring(2,endpos));
		  Iname="document.mailform.I_"+realname
		  eval("if (typeof("+Iname+ ") != \"undefined\"){ IValue=document.mailform.I_"+realname+".value;}");
                  // get the answer field name and value
                  //If this is an array (radio buttons), we want to pass the
                  // array, not the element in the array, so get that.  
		  realname=document.mailform.elements[i].name;
		  eval("A_realname=document.mailform."+realname);
                  if (!getRealValue(A_realname)){	 
                      unans_quest += ("\n" + IValue);
                      if (focusobj == null && (A_realname.type == "text" || A_realname.type == "password")){
                         focusobj=A_realname
                      }
                  }
              }
              else
              {	 	
		  realname=(document.mailform.elements[i].name.substring(2,endpos));
                  // if  an I_ variable exists, use it. Otherwise, use the
                  // question name
                  IValue=(document.mailform.elements[i].name.substring(2,endpos));
		  Iname="document.mailform.I_"+realname
		  eval("if (typeof("+Iname+ ") != \"undefined\"){ IValue=document.mailform.I_"+realname+".value;}");
                  // get the answer field name and value
                  //If this is an array (radio buttons), we want to pass the
                  // array, not the element in the array, so get that.  
		  realname=document.mailform.elements[i].name;
		  eval("A_realname=document.mailform."+realname);
                  if (!getRealValue(A_realname)){	 
                      unans_quest += ("\n" + IValue);
                      if (focusobj == null && A_realname.type == "text"){
                         focusobj=A_realname
                      }
                  }
              }


          }
        lastone= document.mailform.elements[i].name;
        }

      }


   if (unans_quest || pmsg){
          msg = msg_header;
          msg += pmsg;
          if (unans_quest){
            msg +="\n" + msg_noans + "\n" + unans_quest + "\n";
          }


          if (focusobj != null){
            focusobj.select();
            focusobj.focus();
          }
  	  alert(msg);
	  return false;
    }
return true;
}
	
