/*JavaScript source file, Administrator */
function Validator(){
  this.requiredFields = new Array();
  this.alphanumericFields = new Array();
  this.digitsFields = new Array();
  this.phoneFields = new Array();
  this.emailFields = new Array();
  this.dateFields = new Array();
  this.error = null;
  
	this.addRequiredField = function(element,message){
	  try{
      ele = document.getElementById(element);
      if(ele.type=="radio"){
        ele = document.getElementsByName(ele.name);
        ele.type = "radio";
        //alert(ele.type);
        //alert(ele.length);
      }
      this.requiredFields[this.requiredFields.length] = {"field": ele , "message": message};
      //alert("field:"+ ele +", message:"+ message);
    }catch(err){
      txt="There was an error on this page.\n\n";
      txt+="Error description: " + err.description + "\n\n";
      txt+="Click OK to continue.\n\n";
      alert(txt);
    }	
	};
	
	this.addPhoneField = function(element,message){
	  try{
      ele = document.getElementById(element);
      if(ele.type=="radio"){
        ele = document.getElementsByName(ele.name);
        ele.type = "radio";
        //alert(ele.type);
        //alert(ele.length);
      }
      this.phoneFields[this.phoneFields.length] = {"field": ele , "message": message};
      //alert("field:"+ ele +", message:"+ message);
    }catch(err){
      txt="There was an error on this page.\n\n";
      txt+="Error description: " + err.description + "\n\n";
      txt+="Click OK to continue.\n\n";
      alert(txt);
    }	
	};
	
	this.addAlphanumericField = function(element,message){
	  try{
      ele = document.getElementById(element);
      if(ele.type=="radio"){
        ele = document.getElementsByName(ele.name);
        ele.type = "radio";
        //alert(ele.type);
        //alert(ele.length);
      }
      this.alphanumericFields[this.alphanumericFields.length] = {"field": ele , "message": message};
      //alert("field:"+ ele +", message:"+ message);
    }catch(err){
      txt="There was an error on this page.\n\n";
      txt+="Error description: " + err.description + "\n\n";
      txt+="Click OK to continue.\n\n";
      alert(txt);
    }	
	};
	
	this.addDigitsField = function(element,message){
	  try{
      ele = document.getElementById(element);
      if(ele.type=="radio"){
        ele = document.getElementsByName(ele.name);
        ele.type = "radio";
        //alert(ele.type);
        //alert(ele.length);
      }
      this.digitsFields[this.digitsFields.length] = {"field": ele , "message": message};
      //alert("field:"+ ele +", message:"+ message);
    }catch(err){
      txt="There was an error on this page.\n\n";
      txt+="Error description: " + err.description + "\n\n";
      txt+="Click OK to continue.\n\n";
      alert(txt);
    }	
	};
	
	this.addEmailField = function(element,message){
	  try{
      ele = document.getElementById(element);
      if(ele.type=="radio"){
        ele = document.getElementsByName(ele.name);
        ele.type = "radio";
        //alert(ele.type);
        //alert(ele.length);
      }
      this.emailFields[this.emailFields.length] = {"field": ele , "message": message};
      //alert("field:"+ ele +", message:"+ message);
    }catch(err){
      txt="There was an error on this page.\n\n";
      txt+="Error description: " + err.description + "\n\n";
      txt+="Click OK to continue.\n\n";
      alert(txt);
    }	
	};
	
	
	this.addDateField = function(element,message){
	  try{
      ele = document.getElementById(element);
      if(ele.type=="radio"){
        ele = document.getElementsByName(ele.name);
        ele.type = "radio";
        //alert(ele.type);
        //alert(ele.length);
      }
      this.dateFields[this.dateFields.length] = {"field": ele , "message": message};
      //alert("field:"+ ele +", message:"+ message);
    }catch(err){
      txt="There was an error on this page.\n\n";
      txt+="Error description: " + err.description + "\n\n";
      txt+="Click OK to continue.\n\n";
      alert(txt);
    }	
	};
	
	this.validate = function(){
	   this.error = null;
	   // Check for required fields
	   if(this.requiredFields.length>0){
	     for(i=0;i<this.requiredFields.length;i++){
          switch(this.requiredFields[i].field.type){
            case "text":
            case "select-one":
              if(this.requiredFields[i].field.value==""){
                this.error = this.requiredFields[i].message;
                this.requiredFields[i].field.style.background = 'Yellow';
              }else{
                this.requiredFields[i].field.style.background = 'White';
              }
              break;
            case "checkbox":
              if(this.requiredFields[i].field.checked == false){
                  this.error = this.requiredFields[i].message;
                  this.requiredFields[i].field.style.background = 'Yellow';
              }else{
                this.requiredFields[i].field.style.background = 'White';
              }
              break;
            case "radio":
              isValid = false;
              for(j=0;j< this.requiredFields[i].field.length;j++ ){
                if(this.requiredFields[i].field[j].checked == true){
                  isValid = true;
                  //this.requiredFields[i].field[j].style.background = 'White';
                  break;
                }else{
                  //this.requiredFields[i].field[j].style.background = 'Yellow';
                }
                //alert(this.requiredFields[i].field[j] + " type:" + this.requiredFields[i].field[j].type + " name:" + this.requiredFields[i].field[j].name + " value:" + this.requiredFields[i].field[j].value  );
              }
              if(!isValid){
                this.error = this.requiredFields[i].message;
              }
              break;
            default:
              this.requiredFields[i].field.style.background = 'White';
          }
          // if error alert error message and return           
          if(this.error!=null){
            alert(this.error);
            return false;
          }
       }
     }
     //check for alphanumeric
     if(this.alphanumericFields.length>0){
	     for(i=0;i<this.alphanumericFields.length;i++){
          switch(this.alphanumericFields[i].field.type){
            case "text":
            case "select-one":
              if(this.alphanumericFields[i].field.match(/\.*/)){
                this.error = this.alphanumericFields[i].message;
                this.alphanumericFields[i].field.style.background = 'Yellow';
              }else{
                this.alphanumericFields[i].field.style.background = 'White';
              }
              break;
            case "checkbox":
              if(this.alphanumericFields[i].field.checked == false){
                  this.error = this.alphanumericFields[i].message;
                  this.alphanumericFields[i].field.style.background = 'Yellow';
              }else{
                this.alphanumericFields[i].field.style.background = 'White';
              }
              break;
            case "radio":
              isValid = false;
              for(j=0;j< this.alphanumericFields[i].field.length;j++ ){
                if(this.alphanumericFields[i].field[j].checked == true){
                  isValid = true;
                  //this.alphanumericFields[i].field[j].style.background = 'White';
                  break;
                }else{
                  //this.alphanumericFields[i].field[j].style.background = 'Yellow';
                }
                //alert(this.alphanumericFields[i].field[j] + " type:" + this.alphanumericFields[i].field[j].type + " name:" + this.alphanumericFields[i].field[j].name + " value:" + this.alphanumericFields[i].field[j].value  );
              }
              if(!isValid){
                this.error = this.alphanumericFields[i].message;
              }
              break;
            default:
              this.alphanumericFields[i].field.style.background = 'White';
          }
          // if error alert error message and return           
          if(this.error!=null){
            alert(this.error);
            return false;
          }
       }
     } 
     //check for digits
     if(this.digitsFields.length>0){
	     for(i=0;i<this.digitsFields.length;i++){
          switch(this.digitsFields[i].field.type){
            case "text":
            case "select-one":
            case "checkbox":
			  if(this.digitsFields[i].field.value!=""){
	              if(!this.digitsFields[i].field.value.match(/^\d+\.?\d+$/)){
	                this.error = this.digitsFields[i].message;
	                this.digitsFields[i].field.style.background = 'Yellow';
	              }else{
	                this.digitsFields[i].field.style.background = 'White';
	              }
			  }
              break;
            case "radio":
              for(j=0;j< this.digitsFields[i].field.length;j++ ){
                if(this.digitsFields[i].field[j].checked == true){
                  if(!this.digitsFields[i].field[j].value.match(/^\d+\.?\d+$/)){
                    this.error = this.digitsFields[i].message;
					this.emailFields[i].field[j].style.background = 'Yellow';
                  }else{
					this.emailFields[i].field[j].style.background = 'White';
				  }
                  break;
                }
                //alert(this.digitsFields[i].field[j] + " type:" + this.digitsFields[i].field[j].type + " name:" + this.digitsFields[i].field[j].name + " value:" + this.digitsFields[i].field[j].value  );
              }
              break;
            default:
              this.digitsFields[i].field.style.background = 'White';
          }
          // if error alert error message and return           
          if(this.error!=null){
            alert(this.error);
            return false;
          }
       }
     }  
     //check for phone
     if(this.phoneFields.length>0){
	     for(i=0;i<this.phoneFields.length;i++){
          switch(this.phoneFields[i].field.type){
            case "text":
            case "select-one":
            case "checkbox":
			  if(this.phoneFields[i].field.value!=""){
	              var stripped = this.phoneFields[i].field.value.replace(/[\(\)\.\-\ \+]/g, '');    
	              if(isNaN(parseInt(stripped))){
	                this.error = this.phoneFields[i].message;
	                this.phoneFields[i].field.style.background = 'Yellow';
	              }else if(!(stripped.length > 8)){
	                this.error = "The phone number is the wrong length. Make sure you included an area code.";
	                this.phoneFields[i].field.style.background = 'Yellow';
	              }else{
	                this.phoneFields[i].field.style.background = 'White';
	              }
			  }
              break;
            case "radio":
              for(j=0;j< this.phoneFields[i].field.length;j++ ){
                if(this.phoneFields[i].field[j].checked == true){
                  var stripped = this.phoneFields[i].field[j].value.replace(/[\(\)\.\-\ \+]/g, '');    
                  if(isNaN(parseInt(stripped))){
                    this.error = this.phoneFields[i].message;
					this.emailFields[i].field[j].style.background = 'Yellow';
                  }else{
					this.emailFields[i].field[j].style.background = 'White';
				  }
                  if(!(stripped.length < 8)){
                    this.error = "The phone number is the wrong length. Make sure you included an area code.";
					this.emailFields[i].field[j].style.background = 'Yellow';
                  }else{
					this.emailFields[i].field[j].style.background = 'White';
				  }
                  break;
                }
                //alert(this.phoneFields[i].field[j] + " type:" + this.phoneFields[i].field[j].type + " name:" + this.phoneFields[i].field[j].name + " value:" + this.phoneFields[i].field[j].value  );
              }
              break;
            default:
              this.phoneFields[i].field.style.background = 'White';
          }
          // if error alert error message and return           
          if(this.error!=null){
            alert(this.error);
            return false;
          }
       }
     } 
     //check for email
     if(this.emailFields.length>0){
		var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
		var illegalChars = /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
	    for(i=0;i<this.emailFields.length;i++){
          switch(this.emailFields[i].field.type){
            case "text":
            case "select-one":
			case "checkbox":
			  if(this.emailFields[i].field.value!=""){	
	              if(!emailFilter.test(this.emailFields[i].field.value)){
						this.error = this.emailFields[i].message;
						this.emailFields[i].field.style.background = 'Yellow';
	              }else if (!emailFilter.test(this.emailFields[i].field.value)) {
						this.error = this.emailFields[i].message;
						this.emailFields[i].field.style.background = 'Yellow';
				  }else{
						this.emailFields[i].field.style.background = 'White';
	              }
			  }
              break;
            case "radio":
              for(j=0;j< this.emailFields[i].field.length;j++ ){
                if(this.emailFields[i].field[j].checked == true){
					if(!emailFilter.test(this.emailFields[i].field[j].value)){
						this.error = this.emailFields[i].message;
						this.emailFields[i].field[j].style.background = 'Yellow';
					}else if (!emailFilter.test(this.emailFields[i].field.value)) {
						this.error = this.emailFields[i].message;
						this.emailFields[i].field[j].style.background = 'Yellow';
					}else{
						this.emailFields[i].field[j].style.background = 'White';
					}
                  break;
                }
                //alert(this.emailFields[i].field[j] + " type:" + this.emailFields[i].field[j].type + " name:" + this.emailFields[i].field[j].name + " value:" + this.emailFields[i].field[j].value  );
              }
              break;
            default:
              this.emailFields[i].field.style.background = 'White';
          }
          // if error alert error message and return           
          if(this.error!=null){
            alert(this.error);
            return false;
          }
       }
     } 
     //check for date
     if(this.dateFields.length>0){
	     for(i=0;i<this.dateFields.length;i++){
          switch(this.dateFields[i].field.type){
            case "text":
            case "select-one":
			case "checkbox":
			  if(this.dateFields[i].field.value!=""){
	              if(this.dateFields[i].field.value==""){
	                this.error = this.dateFields[i].message;
	                this.dateFields[i].field.style.background = 'Yellow';
	              }else{
	                this.dateFields[i].field.style.background = 'White';
	              }
			  }
              break;
            case "radio":
              isValid = false;
              for(j=0;j< this.dateFields[i].field.length;j++ ){
                if(this.dateFields[i].field[j].checked == true){
                  isValid = true;
                  //this.dateFields[i].field[j].style.background = 'White';
                  break;
                }else{
                  //this.dateFields[i].field[j].style.background = 'Yellow';
                }
                //alert(this.dateFields[i].field[j] + " type:" + this.dateFields[i].field[j].type + " name:" + this.dateFields[i].field[j].name + " value:" + this.dateFields[i].field[j].value  );
              }
              if(!isValid){
                this.error = this.dateFields[i].message;
              }
              break;
            default:
              this.dateFields[i].field.style.background = 'White';
          }
          // if error alert error message and return           
          if(this.error!=null){
            alert(this.error);
            return false;
          }
       }
     } 
     return true;
  };
}