function FieldsList()
{
	this.instanceOf= function()
	{
		var self=new Array();
		self.Add = function (obj)
		{
			if(obj)
			{
				this[this.length]=obj;
			}
		}
		return self;
	}
}


function Field(FieldName,DefaultValue,FieldType,ValidationType,Message,EmptyMessage,OptionalArgs,IsOptional)
{
	this.FieldName=FieldName;
	this.FieldType=FieldType;
	this.ValidationType=ValidationType;
	this.OptionalArgs=OptionalArgs;
	this.Message=Message;
	this.ErrorDiv="Err"+FieldName;
	this.IsOptional=IsOptional;
	if(this.IsOptional==null)
		this.IsOptional=false;
	if(EmptyMessage==null)
	{
		this.EmptyMessage=Message;
	}	
	else
	{
		this.EmptyMessage=EmptyMessage;
	}

	this.DefaultValue=DefaultValue;

	if(DefaultValue==null)
	{
		switch(ValidationType)
		{
			case "string":
			this.DefaultValue="";
			break;
			case "numeric":
			this.DefaultValue=0;
			break;
			case "range":
			this.DefaultValue=0;
			break;
		}
	}
	
}

function Range(MinVal,MaxVal)
{
	this.Type="Range";
	this.MinVal=MinVal;
	this.MaxVal=MaxVal;
}

function Comparer(FieldName)
{
	this.Type="Comparer";
	this.FieldName=FieldName;
}

function ConditionalCheck(FieldName,FieldType,CompareValue)
{
	this.Type="ConditionalCheck";
	this.FieldName=FieldName;
	this.FieldType=FieldType;
	this.CompareValue=CompareValue;
}

function DisplayError(Target,Message)
{
	if(document.getElementById(Target)==null && Message!="")
		alert(Message);
	if(document.getElementById(Target)!=null)
	{
		document.getElementById(Target).style.display='block';
		document.getElementById(Target).innerHTML=Message;
	}

}

function ValidateForm(FormName,ObjFields)
{
	var Excluded;
	var Valid=true;

	for(x=0;x<ObjFields.length;x++)
	{

		var Value="";
		switch(ObjFields[x].FieldType)
		{
			case "text":
			Value=document.getElementById(ObjFields[x].FieldName).value;
			break;
			case "select":
			Value=document.getElementById(ObjFields[x].FieldName).options[document.getElementById(ObjFields[x].FieldName).selectedIndex].value;
			break;	
		}

		
		if(ObjFields[x].OptionalArgs!=null && ObjFields[x].OptionalArgs.Type=="ConditionalCheck")
		{
			var ConditionValue="";
			switch(ObjFields[x].OptionalArgs.FieldType)
			{
				case "text":
				ConditionValue=document.getElementById(ObjFields[x].OptionalArgs.FieldName).value;
				break;
				case "select":
				ConditionValue=document.getElementById(ObjFields[x].OptionalArgs.FieldName).options[document.getElementById(ObjFields[x].OptionalArgs.FieldName).selectedIndex].value;
				break;
				case "radio":
				ConditionValue=GetRadioValue(ObjFields[x].OptionalArgs.FieldName);
				break;
			}
			if(ObjFields[x].OptionalArgs.CompareValue==ConditionValue)
			{
				ObjFields[x].IsOptional=false;
			}
			else
			{
				ObjFields[x].IsOptional=true;
			}
		}

		if(ObjFields[x].IsOptional==false && ObjFields[x].FieldType!="radio" && Value==ObjFields[x].DefaultValue)
		{
			
			DisplayError(ObjFields[x].ErrorDiv,ObjFields[x].EmptyMessage);
			document.getElementById(ObjFields[x].FieldName).focus();
			return false;
		}
		
		switch(ObjFields[x].ValidationType)
		{
			
			case "string":
			if(Value!="" && IsNumeric(Value))
			{
				DisplayError(ObjFields[x].ErrorDiv,ObjFields[x].Message);
				document.getElementById(ObjFields[x].FieldName).focus();
				return false;
			}
			break;
			case "compare":
			if(Value!="" && document.getElementById(ObjFields[x].OptionalArgs.FieldName).value!=Value)
			{
				DisplayError(ObjFields[x].ErrorDiv,ObjFields[x].Message);
				document.getElementById(ObjFields[x].FieldName).focus();
				return false;
			}
			break;
			case "checked":
			if(Value!="" && !IsRadioSelected(ObjFields[x].FieldName))
			{
				DisplayError(ObjFields[x].ErrorDiv,ObjFields[x].Message);				
				return false;
			}
			break;
			case "date":
			if(Value!="" && !IsDate(Value))
			{
				DisplayError(ObjFields[x].ErrorDiv,ObjFields[x].Message);
				document.getElementById(ObjFields[x].FieldName).focus();
				return false;
			}
			break;
			case "email":
			if(Value!="" && !IsValidEmail(Value))
			{
				DisplayError(ObjFields[x].ErrorDiv,ObjFields[x].Message);
				document.getElementById(ObjFields[x].FieldName).focus();
				return false;
			}
			break;
			case "numeric":
			if(Value!="" && !IsNumeric(parseInt(Value)))
			{
				DisplayError(ObjFields[x].ErrorDiv,ObjFields[x].Message);
				document.getElementById(ObjFields[x].FieldName).focus();
				return false;
			}
			break;
			case "any":
				DisplayError(ObjFields[x].ErrorDiv,ObjFields[x].Message);
				document.getElementById(ObjFields[x].FieldName).focus();
				return false;
			
			break;
			case "range":
			if(Value!="" && !IsBetweenRange(parseInt(Value),ObjFields[x].OptionalArgs.MinVal,ObjFields[x].OptionalArgs.MaxVal))
			{
				DisplayError(ObjFields[x].ErrorDiv,ObjFields[x].Message);
				document.getElementById(ObjFields[x].FieldName).focus();
				return false;
			}
			break;
			case "length":
			if(Value!="" && !IsLengthValid(Value,ObjFields[x].OptionalArgs.MinVal,ObjFields[x].OptionalArgs.MaxVal))
			{
				DisplayError(ObjFields[x].ErrorDiv,ObjFields[x].Message);
				document.getElementById(ObjFields[x].FieldName).focus();
				return false;
			}
			break;
			case "phone":
			if(Value!="" && !IsValidPhone(Value))
			{
				DisplayError(ObjFields[x].ErrorDiv,ObjFields[x].Message);
				document.getElementById(ObjFields[x].FieldName).focus();
				return false;	
			}
			break;
			case "mobile":
			if(Value!="" && !IsValidMobile(Value))
			{
				DisplayError(ObjFields[x].ErrorDiv,ObjFields[x].Message);
				document.getElementById(ObjFields[x].FieldName).focus();
				return false;	
			}
			break;
		}
		DisplayError(ObjFields[x].ErrorDiv,"");
	}
	return true;
}

function IsValidPhone(Value)
{
	var objRegExp  =/(^[+]{1}[0-9]{2,4}\s[0-9]{2,6}\s[0-9]{3,10}$)/i;
	return objRegExp.test(Value);
}

function IsValidMobile(Value)
{
	var objRegExp  =/(^[+]{1}[0-9]{2,4}\s[0-9]{5,10}$)/i;
	return objRegExp.test(Value);
}

function GetRadioValue(RadioGroupName) 
{
	var RadioGroup = document.getElementsByName(RadioGroupName);
	for(i=0;i<=RadioGroup.length-1;i++)
	{
		if(RadioGroup[i].checked)
		{
			return RadioGroup[i].value;
		}
	}
	return false;
}

function IsProvided(FieldName)
{
	if(document.getElementById(FieldName).value == "")
	{
		return false;
	}
	else
	{
		return true;
	}
}

function IsNumeric(Value)
{
	var objRegExp  =/^[0-9]+$/;
	return objRegExp.test(Value);
}

function IsValidEmail(strValue)
{
	var objRegExp  =/(^[a-z0-9]([a-z0-9_\.]*)@([a-z0-9_\-\.]*)([.][a-z]{3})$)|(^[a-z0-9]([a-z0-9_\.]*)@([a-z0-9_\-\.]*)(\.[a-z]{2,3})(\.[a-z]{2})*$)/i;
	return objRegExp.test(strValue);
}

function IsDate(dateStr)
{
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	var matchArray = dateStr.match(datePat); // is the format ok?

	if (matchArray == null)
	{
		//alert("Please enter date as either dd/mm/yyyy or dd-mm-yyyy.");
		return false;
	}

	month = matchArray[3]; // p@rse date into variables
	day = matchArray[1];
	year = matchArray[5];

	if (month < 1 || month > 12)
	{ // check month range
		//alert("Month must be between 1 and 12.");
		return false;
	}

	if (day < 1 || day > 31)
	{
		//alert("Day must be between 1 and 31.");
		return false;
	}

	if ((month==4 || month==6 || month==9 || month==11) && day==31)
	{
		//alert("Month "+month+" doesn`t have 31 days!")
		return false;
	}

	if (month == 2)
	{
		// check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap))
		{
			//alert("February " + year + " doesn`t have " + day + " days!");
			return false;
		}
	}
	return true; // date is valid
}

function IsBetweenRange(Value,RangeStart,RangeEnd)
{

	if(Value>RangeStart)
	{
		
		if(Value>RangeEnd)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	else
	{
		return false;
	}
}

function IsLengthValid(Value,RangeStart,RangeEnd)
{

	if(Value.length>RangeStart)
	{
		if(Value.length>RangeEnd)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	else
	{
		return false;
	}
}

function IsRadioSelected(RadioGroupName) 
{
	var RadioGroup = document.getElementsByName(RadioGroupName);
	for(i=0;i<=RadioGroup.length-1;i++)
	{
		if(RadioGroup[i].checked!="")
		return true;
	}
	return false;
}

function IsItemSelected(OptionName,DefaultValue)
{
	var temp = document.getElementById(OptionName);
	if(temp.options[temp.selectedIndex].value == DefaultValue)
	{
		return false;
	}
	else
	{
		return true;
	}
}

function AreEquel(FieldName_1,FieldName_2)
{
	if(document.getElementById(FieldName_1).value != document.getElementById(FieldName_2).value)
	{
		return false;
	}
	else
	{
		return true;
	}
}