function initForms()
{
	for (var i = 0; i < document.forms.length; i++)
	{
		document.forms.item(i).validator = new WebForm(document.forms.item(i));
	}	
	var a = document.getElementsByTagName("input");
	for (var i = 0; i < a.length; i++)
	{
		if (a.item(i).id == "enableme") a.item(i).disabled = false;
	}
}


/************************************
*	WebForm Object
************************************/

function WebForm(form)
{
	this.form = form;
	this.version = "2.0.1.0";
	this.valid = false;
	this.validators = [];
	this.addValidators = wbfr_addValidators;
	this.validationSummary = new validSummary(this)
	this.disable = function(){this.validationSummary.disable()};
	this.enable = function(){this.validationSummary.enable()};
	this.addRequiredFieldValidator = wbfr_addRequiredFieldValidator;
	this.addRequiredPairValidator = wbfr_addRequiredPairValidator;
	this.addRegularExpressionValidator = wbfr_addRegularExpressionValidator;
	this.addCompareValidator = wbfr_addCompareValidator;
	this.addCustomValidator = wbfr_addCustomValidator;
	this.addRangeValidator = wbfr_addRangeValidator;
	this.reset = wbfr_reset;
	this.submit = wbfr_submit;
	this.validate = wbfr_validate;
	this.hide = function(){this.validationSummary.hide()};

	this.addValidators();
	for (var i = 0; i < this.validators.length; i++) this.validators[i].mark();
	//for (var i = 0; i < this.validators.length; i++) alert(this.validators[i].errormessage);
}


function wbfr_addValidators()
{
	var list = this.form.getElementsByTagName("DIV");
	for (var i = 0; i < list.length; i++)
	{
		switch (list.item(i).className)
		{
			case "RequiredFieldValidator": this.addRequiredFieldValidator(list.item(i).getAttribute("ControlToValidate"), list.item(i).getAttribute("ErrorMessage"));break;
			case "RegularExpressionValidator": this.addRegularExpressionValidator(list.item(i).getAttribute("ControlToValidate"), list.item(i).getAttribute("ErrorMessage"), list.item(i).getAttribute("validationexpression"));break;
			case "CompareValidator": this.addCompareValidator(list.item(i).getAttribute("ControlToValidate"), list.item(i).getAttribute("ErrorMessage"), list.item(i).getAttribute("controltocompare"));break;
			case "CustomValidator": this.addCustomValidator(list.item(i).getAttribute("ControlToValidate"), list.item(i).getAttribute("ErrorMessage"), list.item(i).getAttribute("callbackFunc"));break;
			case "RangeValidator": this.addRangeValidator(list.item(i).getAttribute("ControlToValidate"), list.item(i).getAttribute("ErrorMessage"), list.item(i).getAttribute("minRange"), list.item(i).getAttribute("maxRange"));break;
			case "RequiredPairValidator": this.addRequiredPairValidator(list.item(i).getAttribute("ControlToValidate"), list.item(i).getAttribute("ControlToValidate2"), list.item(i).getAttribute("ErrorMessage"));break;
		}
	}
}

function wbfr_addRequiredFieldValidator(controltovalidate, errormessage)
{
	var o = new Object();
	o.form = this.form;
	o.controltovalidate = controltovalidate;
	o.errormessage = errormessage;
	o.validate = function (summary)
	{
		var control = this.form.elements[this.controltovalidate]; 
		if ("select-one" == control.type)
		{
			if(control && control.selectedIndex >= 0 ) return true;
		}
		if(control && control.value && control.value.trim()) return true;
		if (control.readOnly) return true; 
		summary.add(control, errormessage);
		return false;
	}
	o.mark = function()
	{
		var control = this.form.elements[this.controltovalidate]; 
		if (control.length) return;
		if (control.style.direction == "rtl")
		{
			control.style.backgroundImage = "url(/images/he/red-mark.gif)";
			control.style.backgroundPosition = "left bottom";
		}
		else
		{
			control.style.backgroundImage = "url(/images/en/red-mark.gif)";
			control.style.backgroundPosition = "right bottom";
		}
		control.style.backgroundRepeat = "no-repeat";
	}
	this.validators[this.validators.length] = o;
}

function wbfr_addRequiredPairValidator(controltovalidate, controltovalidate2, errormessage)
{
	var o = new Object();
	o.form = this.form;
	o.controltovalidate = controltovalidate;
	o.controltovalidate2 = controltovalidate2;
	o.errormessage = errormessage;

	o.validate = function (summary)
	{
		var control = [this.form.elements[this.controltovalidate], this.form.elements[this.controltovalidate2]]; 
		for (var i = 0; i < control.length; i++)
		{
			if ("select-one" == control[i].type)
			{
				if(control[i] && control[i].selectedIndex >= 0 ) return true;
			}
			if(control[i] && control[i].value && control[i].value.trim()) return true;
			if (control[i].readOnly) return true; 
		}		
		summary.add(control[0], errormessage);
		return false;
	}
	o.mark = function()
	{
		var aControls = [this.controltovalidate, o.controltovalidate2];
		for (var i = 0; i < 2; i++)
		{
			var control = this.form.elements[aControls[i]]; 
			if (control.length) return;
			if (control.style.direction == "ltr")
			{
				control.style.backgroundImage = "url(/images/en/red-mark.gif)";
				control.style.backgroundPosition = "right bottom";
			}
			else
			{
				control.style.backgroundImage = "url(/images/he/red-mark.gif)";
				control.style.backgroundPosition = "left bottom";
			}
			control.style.backgroundRepeat = "no-repeat";
		}	
	}
	this.validators[this.validators.length] = o;
}

function wbfr_addRegularExpressionValidator(controltovalidate, errormessage, validationexpression)
{
	var o = new Object();
	o.form = this.form;
	o.controltovalidate = controltovalidate;
	o.errormessage = errormessage;
	o.validationexpression = validationexpression;
	o.validate = function (summary)
	{
		var control = this.form.elements[this.controltovalidate]; 
		if (!control || !control.value) return true;
		if (control.disabled) return true; 
		if (control.readOnly) return true; 
		var re = new RegExp(validationexpression, "i");
		if(re.test(control.value.trim())) return true;
		summary.add(control, errormessage);
		return false;
	}
	o.mark = function(){}
	this.validators[this.validators.length] = o;
}

function wbfr_addCompareValidator(controltovalidate, errormessage, controltocompare)
{
	var o = new Object();
	o.form = this.form;
	o.controltovalidate = controltovalidate;
	o.errormessage = errormessage;
	o.controltocompare = controltocompare;
	o.validate = function (summary)
	{
		var control1 = this.form.elements[this.controltovalidate]; 
		if (control1.readOnly) return true; 
		if (!control1 || !control1.value) return true;
		
		var control2 = this.form.elements[this.controltocompare]; 
		if (control2 && control2.value)
		{
			if(control1.value == control2.value) return true;
		}	
		summary.add(control1, this.errormessage);
		return false;
	}
	o.mark = function(){}
	this.validators[this.validators.length] = o;
}

function wbfr_addCustomValidator(controltovalidate, errormessage, callbackFunc)
{
	var o = new Object();
	o.form = this.form;
	o.controltovalidate = controltovalidate;
	o.errormessage = errormessage;
	o.callbackFunc = callbackFunc;
	o.validate = function (summary)
	{
		var control = this.form.elements[this.controltocompare]; 
		if (control.readOnly) return true; 
		document.smartForm  = this.form;
		if (control && eval(this.callbackFunc + "(document.smartForm, control, summary)")) return true;
		summary.add(control, this.errormessage);
		return false;
	}
	o.mark = function(){}
	this.validators[this.validators.length] = o;
} 

function wbfr_addRangeValidator(controltovalidate, errormessage, minRange, maxRange)
{
	var o = new Object();
	o.controltovalidate = controltovalidate;
	o.errormessage = errormessage;
	o.minRange = parseInt(minRange);
	o.maxRange = parseInt(maxRange);
	o.validate = function (summary)
	{
		var control = this.form.elements[this.controltocompare]; 
		if (!control || !control.value) return true;
		if (control.readOnly) return true; 
		var val = parseInt(control.value);
		if (!isNaN(val) && val >= this.minRange && val <= this.maxRange) return true;
		summary.add(control, this.errormessage);
		return false;
	}
	o.mark = function(){}
	this.validators[this.validators.length] = o;
}

function wbfr_submit()
{
	if (!this.validationSummary)
	{
		return false;
	}
	this.reset();
	if (!this.validate())
	{
		this.validationSummary.show();
		return false;
	}
	return true;	
}

function wbfr_reset()
{
	this.validationSummary.hide();
	this.validationSummary.clear();
}

function wbfr_validate()
{
	for (var i = 0; i < this.validators.length; i++){this.validators[i].validate(this.validationSummary);}
	return this.validationSummary.isValid();
}

/************************************
*	validationSummary Object
************************************/
function validSummary(owner)
{
	this.owner = owner;
	this.objects = [];
	this.warnings = [];
	this.messages = [];
	this.disabled = false;
	this.element = this.owner.form.getElementsByTagName("DIV")._validationSummary; 
	//document.getElementById("_validationSummary");
	if (!this.element)
	{
		this.element = document.createElement("DIV");
		this.element.className = "ValidationSummary";
		this.owner.form.insertBefore( this.element, this.owner.form.firstChild);
		this.element.style.display = "none";
	}	
	
	this.disable = vlds_disable;
	this.enable = vlds_enable;
	this.add = vlds_add;
	this.clear = vlds_clear;
	this.resetInvalidStyle = vlds_resetInvalidStyle;
	this.hide = vlds_hide;
	this.show = vlds_show;
	this.isValid = vlds_isValid;
}

function vlds_disable(){this.disabled = true;}

function vlds_enable(){this.disabled = false;}

function vlds_hide()
{
	this.resetInvalidStyle();
	this.element.style.display = "none";
}

function vlds_show(isSucsses)
{
	var a = isSucsses ? this.messages : this.warnings;
	var text = isSucsses ? "Results:" :"<div class=vsCap>Error:</div>"
	+ (a.length > 1 ? "<ol style='margin-top:4px;margin-bottom:4px;'><li>" : "") + a.join("<li>") + (a.length > 1 ? "</ol>" : "&nbsp;&nbsp;")
	+ "&nbsp;<a href='javascript:document." + this.owner.form.name + ".validator.hide()'></a><br>";
	this.element.style.display = "block";
	this.element.innerHTML = text;
	this.element.style.display = "block";
	if (!isSucsses) this.resetInvalidStyle("clsSmartInputInvalid");
}

function vlds_isValid()
{
	if (this.disabled) return true;
	return (this.warnings.length == 0);
}

function vlds_add(object, warning)
{
	this.objects[this.objects.length] = object;
	this.warnings[this.warnings.length] = warning;
}

function vlds_clear()
{	
	this.resetInvalidStyle();
	this.element.innerHTML = "";
	this.objects = [];
	this.warnings = [];
}


function vlds_resetInvalidStyle(newClass)
{
	if (!newClass) newClass = "clsSmartInput";
	for (var i = 0; i < this.objects.length; i++)
	{
		if ("object" == typeof(this.objects[i]))
		{
			this.objects[i].className = newClass;
			//alert("resetInvalidStyle " + this.objects[i].className);
		}
	}
}

