﻿var yEvent = YAHOO.util.Event;
var yDom = YAHOO.util.Dom;

function LMSValidator()
{
	this.Vlds = new Array();
}

LMSValidator.prototype.AddVld = function(VldObj)
{
	if(VldObj.Type == "DDLVld")//If DropDownList Validator cannot asign keyup event
	{
		yEvent.addListener(VldObj.cID , "change", Validate, this);
	}
	else
	{
		yEvent.addListener(VldObj.cID, "keyup", Validate, this);
		if(VldObj.Type == "CompVld")//Attach a handler to fire when the control to compare changes
		{
			yEvent.addListener(VldObj.c2cID, "keyup", LinkedValidate, this);
		}
	}
	this.Vlds.push(VldObj);
};

Validate = function(e, obj)
{
	var i = new Number();
	for(i = 0; i < obj.Vlds.length; i++)
	{
		if(obj.Vlds[i].cID == yEvent.getTarget(e).id)
		{
			obj.Vlds[i].Validate();
			break;
		}
	}
};

LinkedValidate = function(e, obj)
{
	var i = new Number();
	for(i = 0; i < obj.Vlds.length; i++)
	{
		if((obj.Vlds[i].c2cID != undefined) && (obj.Vlds[i].c2cID == GetTarget(e)))
		{
			obj.Vlds[i].Validate();
			break;
		}
	}
};

LMSValidator.prototype.IsValid = function()
{
	var vld = new Boolean(true);
	for(i = 0; i < this.Vlds.length; i++)
	{
		if(this.Vlds[i].Sts == false)
		{
			vld = false;
			break;
		}
	}
	return vld;
};

LMSValidator.prototype.GetErrorMsgs = function()
{
	var errMsgs = new Array();
	for(i = 0; i < this.Vlds.length; i++)
	{
		if(this.Vlds[i].Sts == false)
		{
			errMsgs.push(this.Vlds[i].Msg);
		}
	}
	return errMsgs;
};

//************************************* Validator Types *************************************
//VldBase
function VldBase(vID, vMsg)
{
	this.ID = vID;
	this.Sts = new Boolean(false);
	this.Msg = vMsg;
}

VldBase.prototype.HideVld = function()
{
	document.getElementById(this.ID).className = "vldOK";
};

VldBase.prototype.ShowVld = function()
{
	document.getElementById(this.ID).className = "vld";
}

//Required Field Validator
function ReqFieldVld(vID, CtrlID, InitialVal, vMsg)
{
	VldBase.call(this, vID, vMsg);
	this.cID = CtrlID;
	this.iVal = InitialVal;
	this.Type = "ReqFieldVld";
}

ReqFieldVld.prototype = new VldBase();

ReqFieldVld.prototype.Validate = function()
{
	var tSts = new Boolean(false);
	if(document.getElementById(this.cID).value == this.iVal)
	{
		this.Sts = false;
		this.ShowVld();
	}
	else
	{
		this.Sts = true;
		this.HideVld();
	}
};

//Comparison Validator
function CompVld(vID, CtrlID, CtrlToCompID, vMsg)
{
	VldBase.call(this, vID, vMsg);
	this.cID = CtrlID;
	this.c2cID = CtrlToCompID;
	this.Type = "CompVld";
}

CompVld.prototype = new VldBase();

CompVld.prototype.Validate = function()
{
	if((document.getElementById(this.cID).value == "") || (document.getElementById(this.cID).value != document.getElementById(this.c2cID).value))
	{
		this.Sts = false;
		this.ShowVld();
	}
	else
	{
		this.Sts = true;
		this.HideVld();
	}
};

//Regular Expression Validator
function RegExpVld(vID, CtrlID, RegExpression, vMsg)
{
	VldBase.call(this, vID, vMsg);
	this.cID = CtrlID;
	this.Regex = RegExpression;
	this.Type = "RegExpVld";
}

RegExpVld.prototype = new VldBase();

RegExpVld.prototype.Validate = function()
{
	if(!this.Regex.test(document.getElementById(this.cID).value))
	{
		this.Sts = false;
		this.ShowVld();
	}
	else
	{
		this.Sts = true;
		this.HideVld();
	}
};

//DropdownList Validator
function DDLVld(vID, CtrlID, vMsg)
{
	VldBase.call(this, vID, vMsg);
	this.cID = CtrlID;
	this.Type = "DDLVld";
}

DDLVld.prototype = new VldBase();

DDLVld.prototype.Validate = function()
{
	//Perhaps Eval with this.condition can make the Validate function go into the Base Vld
	if(document.getElementById(this.cID).selectedIndex == 0)
	{
		this.Sts = false;
		this.ShowVld();
	}
	else
	{
		this.Sts = true;
		this.HideVld();
	}
};