/**
*
* validator
*
* @author Martin Gottlieb, Idev inc.
* @version 1.1
*
*/

function validator()
{
   this.inputName               = new Array();
   this.validationType          = new Array();
   this.formName                = new Array();
   this.errorMessage            = new Array();
   this.formSubmitObject        = "";
   this.length                  = 0;
   this.emptyErrorMessage       = "The required field is missing: ";
   this.showError               = true;

   this.validate        = validate;
   this.validateAndSubmit = validateAndSubmit;
   this.required        = required;
   this.unrequired      = unrequired;
   this.isInputValid    = isInputValid;
   this.displayError    = displayError;
   this.isEmailValid    = isEmailValid;
   this.isIntegerValid  = isIntegerValid;
   this.isPassWordValid = isPassWordValid;
   this.isPhoneValid    = isPhoneValid;
   this.isZipValid      = isZipValid;
   this.getIndex        = getIndex;
}

function setFormSubmitObject(formName)
{
   this.formSubmitObject= eval("document."+formName);
}

function getFormObject(formNameTemp)
{
   if(typeof inputNameTemp == 'string')
   {
      formName = eval("document."+formNameTemp);
   }else{
      formName = formNameTemp;
   }
   return formName;
}
function getInputObject(formNameTemp,inputNameTemp)
{
   if((typeof formNameTemp == 'string') && (typeof inputNameTemp == 'string') )
   {
      inputName = eval("document."+formNameTemp+"."+inputNameTemp);
   }else if(typeof inputNameTemp == 'string'){
      formName = getFormObject(formNameTemp);
      inputName = formName[inputNameTemp];
   }else{
      inputName = inputNameTemp;
   }
   return inputName;
}

function validateAndSubmit(formObject)
{
  if ( this.validate(formObject) ) {
     formObject.submit();
  }
}
function validate(formObject)
{
   for(i=0;i<this.length;i++)
   {

      if(this.validationType[i] == null || this.validationType[i] == "REGULAR" )
      {
         if(this.isInputValid(this.formName[i],this.inputName[i])==false)
         {
            return false;
         }
      }
      else if(this.validationType[i] == "INTEGER")
      {
         field = this.inputName[i];
         if(this.isIntegerValid(this.formName[i],field)==false)
         {
            this.displayError(this.formName[i],field,"EMPTY");
            return false;

         }
      }
      else if(this.validationType[i] == "PASSWORD")
      {
         pwds = this.inputName[i].replace(/ /g,"").split("&");
         if(this.isPassWordValid(this.formName[i],pwds[0],pwds[1])==false)
         {
           return false;
         }
      }
      else if(this.validationType[i] == "EMAIL")
      {
         email = this.inputName[i];
         if(this.isEmailValid(this.formName[i],email)==false)
         {
            this.displayError(this.formName[i],email,"EMPTY");
            return false;

         }
      }
      else if(this.validationType[i] == "ZIP")
      {
         zip = this.inputName[i];
         if(this.isZipValid(this.formName[i],zip)==false)
         {
            return false;
         }
      }
      else if(this.validationType[i] == "PHONE")
      {
         phone = this.inputName[i];
         if(this.isPhoneValid(this.formName[i],phone)==false)
         {
            return false;
         }
      }

   }

   if(formObject!=null)
   {
      formSubmitObject = formObject;
   }
   return true;
}

function getIndex( formName, inputName )
{

   for ( i = 0; i < this.length; ++i ) {

  if ( this.formName[i] == formName && this.inputName[i] == inputName ) {

    return i;
  }
   }
   return -1;
}

function required(formName,inputName,message,validationType)
{

  index = this.getIndex( formName, inputName );

  if ( index < 0 ) {
    this.formName[this.length]      = formName;
    this.inputName[this.length]     = inputName;
    this.validationType[this.length]  = validationType;
  this.errorMessage[this.length]    = message;
    this.length++;
  }
  else {

  this.validationType[ index ]    = validationType;
  }
}

function unrequired(formName,inputName)
{

  index = this.getIndex( formName, inputName );

  if ( index >= 0 ) {
  this.validationType[ index ] = "NONE";
  }
}


function displayError(formName,inputName,errorType)
{
    if(this.showError==false) return;

  index = this.getIndex( formName,inputName );

  if ( index >= 0 ) {
    if ( this.errorMessage[ index ] != null ) {
      alert( this.errorMessage[index] );
      return;
    }
  }

   if(errorType=="EMPTY")
   {
      alert(this.emptyErrorMessage+' '+inputName);
   }
}

function isInputValid(formNameTmp,inputName,validationType)
{
   formName     = getFormObject(formNameTmp);
   elementName  = getInputObject(formNameTmp,inputName);

   if ( (elementName.type == "text") || (elementName.type == "textarea") || (elementName.type == "hidden") )
   {
      if(elementName.value=='')
      {
         this.displayError(formNameTmp,elementName.name,"EMPTY");
         elementName.focus();
         return false;
      }
   }
   else if(elementName.type=='select-one'|| elementName.type=='select')
   {
      index=elementName.selectedIndex;
      if(index==-1 || elementName.options[index].value == "")
      {
         this.displayError(formNameTmp,elementName.name,"EMPTY");
         elementName.focus();
         return false;
      }
   }
   else if(elementName.type=='checkbox')
   {
      if(elementName.checked==false)
      {
         this.displayError(formNameTmp,inputName,"EMPTY");
         return false;
      }
   }
   else if( elementName[0].type=='radio')
   {
  var r;
  for ( r = 0; r < elementName.length; ++r ) {
            if(elementName[r].checked == true ) {
    return true;
      }
  }

        this.displayError(formNameTmp,inputName,"EMPTY");
        return false;
   }
   else if(elementName.type==null)
   {
      radioLength=elementName.length;
      if(!(radioLength>0))
      {
        return false;
      }
      for(j=0;j<radioLength;j++)
      {
         if(elementName[j].checked==true)
         {
            return true;
         }
      }
      this.displayError(formNameTmp,inputName,"EMPTY");

      return false;
   }
   else
   {
      if(elementName.value=='')
      {
         this.displayError(formNameTmp,elementName.name,"EMPTY");
         elementName.focus();
         return false;
      }
   }
   return true;
}

function isIntegerValid(formName,integerTemp)
{
   integerName = getInputObject(formName,integerTemp);

   if ( integerName.value.match(/^[1-9]+[0-9]*$/) == null ) {
  return false;
   }

  return true;
}

function isEmailValid(formName,emailTemp)
{
   emailName=getInputObject(formName,emailTemp);
   if(emailName.value.match(/^[A-Za-z0-9\']+[A-Za-z0-9\_\-\.\']*\@([A-Za-z0-9\-]+\.)+[A-Za-z]{2,5}$/) == null)
   {
      emailName.focus();
      //alert("The email address you provided is invalid. Please try again!");
      return false;
   }
   return true;
}
function isPhoneValid(formName,phoneTemp)
{
   phoneNumber=getInputObject(formName,phoneTemp);
   if(phoneNumber.value.match(/^1{0,1} *(-| ){0,1} *[\(]*[0-9]{0,3}[\)]* *(-| ){0,1} *[0-9]{3} *(-| ){0,1} *[0-9]{4}$/)
==null)
   {
      phoneNumber.focus();
      alert("The phone number you entered is invalid. Please try again!");
      return false;
   }
   return true;
}
function isZipValid(formName,zipTemp)
{
   zipCode=getInputObject(formName,zipTemp);
   if(zipCode.value.match(/^[0-9A-Za-z]*[\-]*[0-9A-Za-z]+$/)==null)
   {
      zipCode.focus();
      alert("The zipcode you entered is invalid. Please try again!");
      return false;
   }
   return true;
}

function isPassWordValid(formName,pwd1Temp,pwd2Temp)
{
   pwd1=getInputObject(formName,pwd1Temp).value;
   pwd2=getInputObject(formName,pwd2Temp).value;

   //the modify_user form does not require passwords unless they are being changed
   if(formName=='user_modify' && pwd1 == "" && pwd2=="") return true;

   if(!(pwd1==pwd2))
   {
      alert("The password you provided does not match the confirmation password. Please try again!");
      return false;
   }

   if(pwd1.length < 6 || pwd2.length < 6)
   {
      alert("Passwords must be at least 6 characters long. Please try again!");
      return false;
   }

   if(pwd1.length > 20 || pwd2.length > 20)
   {
      alert("Passwords must be 4 to 8 characters long. Please try again!");
      return false;
   }
   return true;
}


