blurList = Array();
focusedList = Array();
requiredButEmptyList = Array();
errors = Array();

function addToBlurList(obj) {
  blurList[obj.id] = obj;
}

function blurCheck(obj) {
  errorDiv = document.getElementById("errorMessages");
  errorDiv.innerHTML = "";
  errors = null;
  errors = Array();
  requiredButEmptyList = null;
  requiredButEmptyList = Array();
  
  focusedList[obj.id] = obj;
  
  x = 0;
  
  for(y in focusedList) {
    if(focusedList[y].RBECheck() != "") {
      errors[x] = focusedList[y].RBECheck();
      requiredButEmptyList[focusedList[y].id] = focusedList[y];
      x++;
    }
  }
  
  for(y in blurList) {
    if(blurList[y].validateMe() != "") {
      errors[x] = blurList[y].validateMe();
      x++;
    }
  }
  
  
  for(y in requiredButEmptyList) {
    inputObject = document.getElementById(requiredButEmptyList[y].id);
    inputObject.className = requiredButEmptyList[y].hClass + "invalid";
  }
  
  errorHTML = "";
  x = 0;
  for(y in errors) {
    if(x > 5) {
      break;
    }
    x++;
    errorHTML += errors[y];
  }
  
  if(errors.length > 0){
    errorDiv.className = "errordivinvalid";
    errorDiv.innerHTML = errorHTML;
  } else {
    errorDiv.className = "errordiv";
  }
}

function TextInput(newId,newHClass,newRequired,newMinLen,newMaxLen,newPattern,
  newSwitches,newLabelText,newPossibleValues) {
  this.id=newId;
  this.hClass=newHClass;
  this.required=newRequired;
  this.minLen=newMinLen;
  this.maxLen=newMaxLen;
  this.pattern= new RegExp(newPattern,newSwitches);
  this.labelText = newLabelText;
  this.possibleValues = newPossibleValues;
  
	this.getValue = function() {
	  inputObject = document.getElementById(this.id);
	  return inputObject.value;
	}
	
	this.setValue = function(newValue) {
	  inputObject = document.getElementById(this.id);
	  inputObject.value = newValue;
	}
	
	this.checkMaxLength = function() {
	  retVal = true;
	 	tmp = this.getValue();
	  if(tmp.length > this.maxLen) {
	  	retVal = false;
	  }
	  return retVal;
	}
	
	this.checkMinLength = function() {
	  retVal = true;
	  if(this.minLen != null) {
	  	tmp = this.getValue();
	  	
		  if(tmp.length < this.minLen) {
		  	retVal = false;
		  }
	  }
	  return retVal;
	}
	
	this.isEmpty = function() {
		retVal = false;
		tmp = this.getValue();
	
		if ((tmp.length==0) || (tmp==null)) {
		  retVal = true;
		}
		return retVal;
	}
	
	this.matchPattern = function() {
		retVal = false;
		
		tmp = this.getValue();
		
		if(tmp.match(this.pattern)) {
			retVal = true;
		}
		
		return retVal;
	}
  
  this.RBECheck = function() {
    errorMessage = "";
    if(this.requiredButEmpty()) {
      errorMessage = "&lt;"+this.labelText+"&gt; ";
      errorMessage += "is required but empty.<br>";
    }
    
    if(errorMessage!="") {
      if(this.required) {
        anchor = '<a class="required" href="#'+this.id+'">';
      } else {
        anchor = '<a href="#'+this.id+'">';
      }
      errorMessage = anchor+errorMessage+"</a>";
    }
    
	  inputObject = document.getElementById(this.id);
    
    return errorMessage;
  }
	
	this.requiredButEmpty = function() {
		retVal = false;
		
		if(this.required) {
			if(this.isEmpty()) {
				retVal = true;
			}
		}
		
		return retVal;
	}
	
	this.validateMe = function() {
	  valid = true;
	
    errorMessage="";
    
	  if(this.matchPattern() == false && !this.isEmpty()) {
	  	valid = false;
      errorMessage+="&lt;"+this.labelText+"&gt; ";
      errorMessage+="Failed pattern match (is not valid input)<br>";
	  }
	  
	  if(this.checkMaxLength()==false) {
	  	valid = false;
      errorMessage+="&lt;"+this.labelText+"&gt; ";
      errorMessage+="has too many characters.<br>";
	  }
	  
	  if(!this.isEmpty()) {
	  	if(this.checkMinLength()==false) {
		  	valid = false;
        errorMessage+="&lt;"+this.labelText+"&gt; ";
        errorMessage+="has too few characters. Minimum is: "
          +this.minLen+"<br>";
	  	}
	  }
    
    if(errorMessage!="") {
      if(this.required) {
        anchor = '<a class="required" href="#'+this.id+'">';
      } else {
        anchor = '<a href="#'+this.id+'">';
      }
      errorMessage = anchor+errorMessage+"</a>";
    }
    
	  inputObject = document.getElementById(this.id);
	  errorDiv = document.getElementById("errorMessages");
    
	  if(valid == false){
	  	inputObject.className = this.hClass + "invalid";
	  } else {
	  	inputObject.className = this.hClass;
	  }
    
    return errorMessage;
	}
}

function SelectInput(newId,newHClass,newRequired,newLabelText) {
  this.id=newId;
  this.hClass=newHClass;
  this.required=newRequired;
  this.labelText=newLabelText;
  this.possibleValues = Array();
  
	this.getValue = function() {
	  inputObject = document.getElementById(this.id);
	  return inputObject.value;
	}
	
	this.setValue = function(newValue) {
	  inputObject = document.getElementById(this.id);
	  inputObject.value = newValue;
	}
  
  this.addPossibleValue = function(newValue) {
    this.possibleValues[this.possibleValues.length] = newValue;
  }
	
	this.isEmpty = function() {
		retVal = false;
		tmp = this.getValue();
	
		if(tmp=="-") {
		  retVal = true;
		}
    
		return retVal;
	}

  this.RBECheck = function() {
    errorMessage = "";
    if(this.requiredButEmpty()) {
      errorMessage = "&lt;"+this.labelText+"&gt; ";
      errorMessage += "is required but empty.<br>";
      
      if(this.required) {
        anchor = '<a class="required" href="#'+this.id+'">';
      } else {
        anchor = '<a href="#'+this.id+'">';
      }
      errorMessage = anchor+errorMessage+"</a>";
      
      inputObject = document.getElementById(this.id);
    }
    
    return errorMessage;
  }
	
	this.requiredButEmpty = function() {
		retVal = false;
		
		if(this.required) {
			if(this.isEmpty()) {
				retVal = true;
			}
		}
		
		return retVal;
	}
  
	this.checkValue = function() {
		retVal = false;
    
    for(y in this.possibleValues) {
      if(this.getValue() == this.possibleValues[y]) {
        retVal = true;
      }
    }
    
		return retVal;
	}
  
	this.validateMe = function() {
	  valid = true;
	
    errorMessage="";
    
	  if(this.checkValue() == false) {
	  	valid = false;
      errorMessage+="&lt;"+this.labelText+"&gt; ";
      errorMessage+="was not a given option.<br>";
	  }
    
    if(errorMessage!="") {
      if(this.required) {
        anchor = '<a class="required" href="#'+this.id+'">';
      } else {
        anchor = '<a href="#'+this.id+'">';
      }
      errorMessage = anchor+errorMessage+"</a>";
    }
    
	  inputObject = document.getElementById(this.id);
    
	  if(valid == false){
	  	inputObject.className = this.hClass + "invalid";
	  } else {
	  	inputObject.className = this.hClass;
	  }
    
    return errorMessage;
	}
}

