/*
   Validator Class
   
   Class        : TRequiredFieldValidator
   Description  : 
   Author       : Estanislao Contreras Chávez
                  Copyrigth (c) 2002
   Date			: Dec-10-2002
   Last Updated : 
*/

function TBaseValidator(id,controlToValidate,errorMessage,text,label){
 // Atributes
  this.id = id;
  this.controlToValidate = controlToValidate;
  this.errorMessage = errorMessage;
  this.text = text;  
  this.label = label;
  this.isValid = false;        
 // Methods
  this.initialize = TBaseValidator_initialize;
  this.validate = TBaseValidator_validate; 
  this.onValidate = function(sender) { return; };
  this.initialize();
  return this;
}

function TBaseValidator_initialize(){
  var warningMsg = "";
  if (this.controlToValidate == null)
    warningMsg+= "\t - 'controlToValidate' property can not be null \n"
  if (this.label==null) 
    warningMsg+= "\t - 'label' property can not be null \n";
  if (warningMsg!="")
  {
    alert("TBaseValidator Exception.\n\n "+this.id+" object has the next errors\n"+warningMsg);
	return false;
  }
  return true;
}


function TBaseValidator_validate() {
  this.label.innerText = "";
  this.onValidate(this);
  if (!this.isValid)
    this.label.innerText = this.text;  
}

function TRequiredFieldValidator(id,controlToValidate,errorMessage,text,label){
  this.base = TBaseValidator;
  this.base(id,controlToValidate,errorMessage,text,label)
  this.onValidate = TRequiredFieldValidator_onValidate;
}

function TRequiredFieldValidator_onValidate(sender){
  sender.isValid = (sender.controlToValidate.value != "");  
}

function TMinLengthFieldValidator(id,controlToValidate,errorMessage,text,label,minLength){
  this.base = TBaseValidator;
  this.base(id,controlToValidate,errorMessage,text,label)
  this.minLength = minLength;
  this.onValidate = TMinLengthFieldValidator_onValidate;
}

function TMinLengthFieldValidator_onValidate(sender){
 sender.isValid = (sender.controlToValidate.value.length >= sender.minLength)
}



function TValidationSummary(showMessageBox,showSummary,messageBox){
  this.showMessageBox = showMessageBox;
  this.showSummary = showSummary;
  this.messageBox = messageBox;
  this.isValid  = false;
  this.validators = new Array();
  this.addValidator = TValidationSummary_addValidator;
  this.validate = TValidationSummary_validate;
}

function TValidationSummary_addValidator(validator){
  this.validators.push(validator);
}

function TValidationSummary_validate(){
  if (this.messageBox != null) this.messageBox.innerHTML = "";
  this.isValid = false;
  var controlsAreValid = true;
  var messages = "";
  for (var i=0; i<this.validators.length; i++){
    this.validators[i].validate();
	if (! this.validators[i].isValid) {	
	  messages += this.validators[i].errorMessage + '<br>';
	}
	controlsAreValid = controlsAreValid && this.validators[i].isValid;
  }  
  this.isValid = controlsAreValid;
  if (this.messageBox != null && this.showMessageBox)
     this.messageBox.innerHTML = messages;  
  if (this.showSummary && messages != "")
  alert(messages.replace(/<br>/gi,'\n'));  
}
