function js360FormValidator(oForm)
{
	this.form=oForm;
	this.controls=[];
	this.compared_controls=[];
	this.validate=js360Validate;
	this.addControlToValidate=js360AddControlToValidate;
	this.addControlToCompare=js360AddControlToCompare;
}

function js360AddControlToCompare(sgroup, control1, control2, label, msg)
{
	icompared_controls=this.compared_controls.length;
	this.compared_controls[icompared_controls]=new js360ComparedControls(sgroup, control1, control2, label, msg);
}

function js360ComparedControls(group, control1, control2, label, msg)
{
	this.group=group;
	this.control_1=control1;
	this.control_2=control2;
	this.label=label;
	this.message=msg;
}

function js360Validate(sGroup)
{
	var ctl;
	var oControl;
	var oLabel;
	var ret;
	var err = "";
	var controlValue;
	var fieldErr;
	var d;
	var ValidatedControls=this.controls;
	
	ret = true;
	for(iControl=0;iControl<ValidatedControls.length;iControl++)
	{
		fieldErr=true;ctl=ValidatedControls[iControl];
		if(ctl.Group==sGroup)
		{
			fieldErr=false;
			if(ctl.Label!="")oLabel = document.getElementById(ctl.Label);
			oControl = document.getElementById(ctl.Name);
			
			//alert(ctl.Name);
			if(oControl)
			{
				switch(ctl.ControlType)
				{
					case "entry":
						controlValue = oControl.value;
						//alert(controlValue);
						break;
						
					case "list":
						controlValue = oControl.options[oControl.selectedIndex].value;
						break;
				}
				
				if((controlValue=="") && (ctl.Mandatory==true))
				{
					fieldErr=true;
				}
				
				if((ctl.DataType=="numeric") && isNaN(controlValue))
				{
					fieldErr=true;
				}
				
				if((ctl.DataType=="date"))
				{   
					d = new Date(controlValue);
					if(!isNaN(d))fieldErr=true;
				}
				
				if(ctl.DataType=="email")
				{
					//isValidEmail=controlValue.search(/^[a-zA-Z]+([_\.-]?[a-zA-Z0-9]+)*@[a-zA-Z0-9]+([\.-]?[a-zA-Z0-9]+)*(\.[a-zA-Z]{2,4})+$/);
					//if(isValidEmail==-1) fieldErr=true;
				}
				
				if(fieldErr==true)
				{
					if(oLabel)
					{
						oLabel.innerHTML=ctl.Message;
					}
					else
					{
						err += ctl.Message + "\n";
					}
					ret = false;
				}
				else
				{
					if(oLabel)
					{
						oLabel.innerHTML="";
					}
				}
			}
		}
	}

	for(iCompared=0;iCompared<this.compared_controls.length;iCompared++)
	{
		cc=this.compared_controls[iCompared];
		if(cc.group==sGroup)
		{
			c1=document.getElementById(cc.control_1);
			c2=document.getElementById(cc.control_2);
			l=document.getElementById(cc.label);
			msg=cc.message;
			if(c1.value!=c2.value)
			{
				l.innerHTML=msg;
				ret=false;
				//err += msg + "\n";
			}
			else
			{
				l.innerHTML="";
			}
		}
	}
	
	if(err!="") alert(err);
	return ret;
}

function js360AddControlToValidate(sGroup, sName, sControlType, sDataType, bMandatory, sMsg, sLabel)
{
	var c=document.getElementById(sName);
	var controlCount=this.controls.length;
	this.controls[controlCount]=new js360ValidatedControl(sGroup, sName, sControlType, sDataType, bMandatory, sMsg, sLabel);
}	
	
function js360ValidatedControl(sGroup, sName, sControlType, sDataType, bMandatory, sMsg, sLabel)
{
	this.Group=sGroup;
	this.Name=sName;
	this.ControlType=sControlType;
	this.DataType=sDataType;
	this.Label=sLabel;
	this.Message = sMsg;
	this.Mandatory = bMandatory; 
}

