/*
	Validator class
	Jay McKinney
	11/13/2008
*/

function ControlValidator(strName, msgFailed, bUseRegEx, expression, expectedValue){
	this.Name = strName;
	this.MsgFailed = msgFailed;
	this.UseRegEx = bUseRegEx;
	this.Expression = expression;
	this.ExpectedValue = expectedValue;
}

ControlValidator.prototype.IsValid = function(){
	var isValid = true;
	var control = document.getElementById(this.Name);
	
	//Use Regular Expression?
	if(this.UseRegEx){
		var regex = new	RegExp(this.Expression);
		isValid = regex.test(control.value);
	}
	else{
		if(this.ExpectedValue !== ''){
			if(control.type.toLowerCase() === 'checkbox'){
				isValid=(control.checked === this.ExpectedValue);
			}
			else{				
				isValid=(control.value === this.ExpectedValue);			
			}						
		}
		else{
			isValid=(control.value !== '');
		}						
	}
	return isValid;
};

ControlValidator.prototype.Focus = function(){
	var control = document.getElementById(this.Name);
	control.focus();
};


function Validator(){
	this.ControlValidatorArray = new Array(0);
	this._count = this.ControlValidatorArray.length;
};

Validator.prototype.Count = function(){
	return this._count;
};

//Add an object to the list
Validator.prototype.Add = function(o){
	if(this._count > this.ControlValidatorArray.length){
		this.Grow(1);
	}	
	this.ControlValidatorArray[this._count] = o;
	this._count++;	
};

//Add a ControlValidator object to the list
Validator.prototype.Add = function(strName, msgFailed, bUseRegEx, expression, expectedValue){
	if(this._count > this.ControlValidatorArray.length){
		this.Grow(1);
	}	
	var c = new ControlValidator(strName, msgFailed, bUseRegEx, expression, expectedValue);
	this.ControlValidatorArray[this._count] = c;
	this._count++;	
};

//Insert an object at index
Validator.prototype.Insert = function(index, ControlValidator){
	if(index < 0 || index > this._count){
		alert('Index must be greater than 0 and less than count!');
		return;
	}
	else{
		var tmpArray = new Array(_count + 1);
		var oldIndex=0;				
		for(var i=0;i<tmpArray.length;i++){
			if(i !== index){
				tmpArray[i] = this.ControlValidatorArray[oldIndex];
				oldIndex++;
			}
			else
			{
				tmpArray[i] = ControlValidator;
			}
		}		
	}
	this.ControlValidatorArray = tmpArray;
	this._count++;
};

//Remove an object from the list
Validator.prototype.Remove = function(ControlValidator){	
	for (var i=0; i < this._count; i++){
		if(this.ControlValidatorArray[i] === o){
			this.ControlValidatorArray[i] = null;
			this.Trim();
			break;
		}
	}
};

//Remove an object from the list at index
Validator.prototype.RemoveAt = function(index){	
	if(index < 0 || index > _count){
		alert('Index must be greater than 0 and less than count!');
		return;
	}
	else{
		this.ControlValidatorArray[index] = null;
		this.Trim();
	}	
};

//Get an object from the list at index
Validator.prototype.GetItemAt = function(index){
	return this.ControlValidatorArray[index];
};

//Get a total count of all non null objects within the list
Validator.prototype.NonNullCount = function(){
	var counter = 0;
	for(var i=0;i < this.ControlValidatorArray.length;i++){
		if(this.ControlValidatorArray[i] !== null){
			counter++;
		}
	}
	return counter;
};

//Increase the array size by count
Validator.prototype.Grow = function(count){
	var tmpArray = new Array(this._count+count);
	for(var i=0;i < this._count;i++){
			tmpArray[i] = this.ControlValidatorArray[i];
		}	
	this.ControlValidatorArray = tmpArray;
	this._count += count;
};

//Trim the array of all null objects
Validator.prototype.Trim = function(){
	var tmpArray = new Array(this.NonNullCount());
	var index = 0;
	for(var i=0;i < this._count;i++){
		if(this.ControlValidatorArray[i] !== null){
			tmpArray[index] = this.ControlValidatorArray[i];
			index++;		
		}
	}	
	this.ControlValidatorArray = tmpArray;
	this._count = this.ControlValidatorArray.length;
};

Validator.prototype.ToString = function(){
	for(var i=0;i<this._count;i++){
		document.write("Item:" + i.toString() + " = " + this.ControlValidatorArray[i] + "<br>");
	}
};

Validator.prototype.Validate = function(){
	var isValid = true;
	for(var i=0;i < this.ControlValidatorArray.length;i++){
		if(!this.ControlValidatorArray[i].IsValid()){
			alert(this.ControlValidatorArray[i].MsgFailed);	
			this.ControlValidatorArray[i].Focus();
			isValid=false;
			break;
		}
	}	
	return isValid;
};