﻿var FormValidator = function()
{
	this._errors = new Array();
	this._controls = new Array();
	this._isEmpty = function(data)
	{
		return (data == null || data == '');
	};
	this._isEmail = function(data)
	{
		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		
		return filter.test(data);
	};
	this._isNumber = function(data)
	{
		return (!isNaN(data));
	};
	this._isDigitOnly = function(data)
	{
		if(this._isNumber(data))
		{
			if(data.indexOf(".") != -1)
			{
				return false;
			}
			return true;
		}
		return false;
	};
	this._isEqual = function(a, b)
	{
		return (a == b);
	};
	this._addError = function(controlId, message)
	{
		this._errors[this._errors.length] = message;
		this._controls[this._controls.length] = controlId;
	};
	this.checkEmpty = function(arg)
	{
		for(var controlId in arg)
		{
			var data = $U.text(controlId);
			if(this._isEmpty(data))
			{
				this._addError(controlId, arg[controlId]);
			}
		}
	};
	this.checkId = function(arg)
	{
		for(var controlId in arg)
		{
			var data = $U.text(controlId);
			if(this._isEmpty(data) || parseInt(data) == 0)
			{
				this._addError(controlId, arg[controlId]);
			}
		}
	};
	this.checkEmail = function(arg)
	{
		for(var controlId in arg)
		{
			var data = $U.text(controlId);
			if(!this._isEmpty(data) && !this._isEmail(data))
			{
				this._addError(controlId, arg[controlId]);
			}
		}
	};
	this.checkEqual = function(ctlToVal, ctlToComp, msg)
	{
		if(this._isEmpty(ctlToVal)) return;
		if($U.text(ctlToVal) != $U.text(ctlToComp))
		{
			this._addError(ctlToComp, msg);
		}
	};
	this.checkRange = function(ctlToVal, minAmt, maxAmt, msg)
	{
		if(this._isEmpty(ctlToVal)) return;
		
		var val = parseInt($U.text(ctlToVal));
		
		if(val < minAmt || val > maxAmt)
		{
			this._addError(ctlToVal, msg);
		}
	};
	this.checkDigit = function(arg)
	{
		for(var controlId in arg)
		{
			var data = $U.text(controlId);
			if(!this._isEmpty(data) && !this._isDigitOnly(data))
			{
				this._addError(controlId, arg[controlId]);
			}
		}
	};
	this.checkNumber = function(arg)
	{
		for(var controlId in arg)
		{
			var data = $U.text(controlId);
			if(!this._isEmpty(data) && !this._isNumber(data))
			{
				this._addError(controlId, arg[controlId]);
			}
		}
	};
	this.checkPosNumber = function(arg)
	{
	    for(var controlId in arg)
		{
			var data = $U.text(controlId);
			if(!this._isEmpty(data) && (!this._isNumber(data) || data <= 0))
			{
				this._addError(controlId, arg[controlId]);
			}
		}
	};
	this.checkPosDigit = function(arg)
	{
	    for(var controlId in arg)
		{
			var data = $U.text(controlId);
			if(!this._isEmpty(data) && (!this._isDigitOnly(data) || data <= 0))
			{
				this._addError(controlId, arg[controlId]);
			}
		}
	};
	this.checkRadio = function(arg)
	{
		for(var controlId in arg)
		{
			var data = $U.radioVal(controlId);
			
			if(data == null)
			{
				var ctl = document.getElementsByName(controlId);
				this._addError(ctl[0].id, arg[controlId]);
			}
		}
	};
	
	this.checkMinLength = function(ctl, ln, msg)
	{
	    var val = $('#'+ ctl).val();
	    
	    if(val.length > 0 && val.length < ln)
	    {
	        this._addError(ctl, msg);
	    }
	    
	    return false;
	};
	
	this.checked = function(arg)
	{
		for(var controlId in arg)
		{
			if(!$U.checked(controlId))
			{
				this._addError(controlId, arg[controlId]);
			}
		}
	};
	this.isValid = function()
	{
		return (this._errors.length == 0);
	};
	this.focus = function()
	{
		if(this._controls.length > 0)
		{
			$U.focus(this._controls[0]);
		}
	};
	this.html = function()
	{
		var total = this._errors.length;
	    	
		if(total == 0) return '';
		
		var html = '<b>Please check the following error(s):</b>';
		
		html += '<ul>';
		
		for(var i = 0; i < total; i++)
		{
			html += '<li>' + this._errors[i] + '</li>';
		}
		
		html += '</ul>';
		
		return html;
	};
	this.text = function()
	{
		var total = this._errors.length;
		
		if(total == 0) return '';
		
		var txt = 'Please check the following error(s):' + "\n";
		
		for(var i = 0; i < total; i++)
		{
			txt += '* ' + this._errors[i];
			
			if(i < (total -1))
			{
				txt += "\n";
			}
		}
		
		return txt;
	}
}
