/*
	Format:
		Validate(Field,Type of validation,Alert Label, Mandatory, LengthCheck, LengthAlert)
		1. Field - The field that must be validated. e.g.: document.Form.FieldName
		2. Types - Refer below.
		3. Alert Label - The text that will throw up in the alert.
			Default label is available for type no.6, 7, 9
		4. Mandatory - Some value must be given if a field is mandatory(Refer Step 3).
		5. LengthCheck - Maximum and minumum length of field seperated by ,.
			e.g.: "5,15"
			a. If minimum is not necessary then "0,10"
			b. If maximum is not necessary then "10,0"
		6. LengthAlert - The alert that should pop up when the above condition fails.
		
	Types:
		1. empty.
		2. alphanumeric.
			Allows: a-z,A-Z,0-9
		3. alphabets.
			Allows: a-z,A-Z
		4. numbers.
			Allows: 0-9
		5. float.
			Allows: 0-9,.
		6. mobile/phone.
			Allows: 0-9,-,spaces
		7. email.
		8. locations.
			Allows: a-z,A-Z,0-9,-,_,.,spaces
		8. image.
			Allows: jpg,jpeg,gif
		10. html.
			Allows: html, htm formats.
		11. other format.
			Definition: other format - .FormatType (e.g: other format - csv)
			The above is for checking for other formats. Multiple formats must be seperated
			with a '|' (e.g: other format - csv|exe).
		12. Any other arguement:
			e.g: a-z,0-9,_,spaces,comma,-
				
	Steps in validation:
		1. Check validation type.
		2. Check whether field is mandatory.
		3. Mandatory will be undefined or "" if it is omitted while calling the function.
		4. Check if there is any value in the field.
		5. If there is value then validate.
		6. Check if there is any length validation.

	ASPCAE - Allow Special Characters at the end.
*/
function Validate(TheField,TheType,Label,Mandatory,LengthCheck,LengthAlert)
{
	
	TrimSpace(TheField)
	if(TheType=="url")
	{
		alert("Validate:\nThe type url has been removed.")
		return true
	}
	if(typeof(TheField)=="undefined")
	{
		alert("Validate:\nField name does not exist.")
		return true
	}
	if(typeof(TheField.value)=="undefined")
	{
		alert("Validate:\nThis field does not support 'value' method.")
		return true
	}
	TheValue=TheField.value
	TheValue=TheValue.replace(/\n/g,"")
	TheValue=TheValue.replace(/\r/g,"")
	TheType=TheType.toLowerCase()
	
	if(TheType=="empty")
	{

		if(!TheValue.match(/^.+$/ig))
		{
			if(typeof(Label)!="undefined")
				alert(Label)
			TheField.focus()
			if(TheField.type!="select-one")
			{
				TheField.select()
			}
			
			return true
		}
		TheExpression=new RegExp(".")
	}
	else if(TheType=="alphabets")
	{
		TheExpression=new RegExp("^[a-z][a-z\\-\\.]*[a-z]|^[a-z]$","gi")
	}
	else if(TheType=="locations")
	{
		TheExpression=new RegExp("^[a-z][a-z0-9\\s\\-\\._]*[a-z]$|^[a-z]$","gi")
	}
	else if(TheType=="alphanumeric")
	{
		TheExpression=new RegExp("^[a-z0-9]+$","gi")
	}
	else if(TheType=="numbers")
	{
		TheExpression=new RegExp("^[\\d]+$","gi")
	}
	else if(TheType=="float")
	{
		TheExpression=new RegExp("^[\\d]+(\\.[\\d]+)?$","gi")
	}
	else if(TheType=="phone" || TheType=="mobile")
	{
		TheExpression=new RegExp("^(\\(?[0-9]+\\)?[\\s\\-])*[0-9]+$","gi")
	}
	else if(TheType=="email")
	{
		if(Label=="")
		{
			Label="Please enter a valid email id."
		}
		TheExpression=new RegExp("^[a-z]([\\-_\\.]?[a-z0-9]+)*@([a-z0-9]+([\\-_][a-z0-9]+)*\\.)+[a-z]{2,}$","gi")
	}
	else if(TheType=="image")
	{
		if(Label=="")
		{
			Label="Please enter a valid image name."
		}
		TheExpression=new RegExp("^[^']+\\.(jpg|jpeg|gif)$","gi")
	}
	else if(TheType=="html")
	{
		TheExpression=new RegExp("^[^']+\\.(htm|html)$","gi")
	}
	else if(TheType.match(/^other format/))
	{
		Format=TheType.replace(/^other format - (.*)$/,"$1")
		TheExpression=new RegExp("\\.("+Format+")$","gi")
	}
	else if(TheType=="regex")
	{
		ThePattern=TheField.getAttribute("Pattern")
		if(ThePattern==null)
		{
			alert("Validation: The field '"+TheField.name+"' does not include the 'Pattern' keyword.")
			return false
		}
		TheExpression=new RegExp(ThePattern,"gi")
	}
	else
	{
		TheType1=TheType.replace(/spaces?/g,"")
		TheType1=TheType1.replace(/commas?/g,"")
		if((!TheType1.match(/[a-z]/)) && TheType1.indexOf("0-9")==-1)
		{
			alert("Validate:\nInvalid parameters.\n"+TheType)
			return true
		}
		TheRegExp=""
		StartString=""
		EndString=""
		
		TheSpValue=TheField.getAttribute("ASPCAE")
		if(TheSpValue==null)
			TheSpValue=""
		else
			TheSpValue=TheSpValue.toLowerCase()
		
			
		AllowedTypes=TheType.toLowerCase().split(",")
		for(j=0;j<AllowedTypes.length;j++)
		{
			if(AllowedTypes[j].match(/[a-z]-[a-z]/) || AllowedTypes[j].match(/[a-z]/))
			{
				if(AllowedTypes[j]=="spaces" || AllowedTypes[j]=="space")
				{
					TheRegExp+="\\\s"
					TheSpValue=TheSpValue.replace(/spaces/,"\\\s")
					TheSpValue=TheSpValue.replace(/space/,"\\\s")
				}
				else if(AllowedTypes[j]=="comma" || AllowedTypes[j]=="commas")
				{
					TheRegExp+=","
					TheSpValue=TheSpValue.replace(/comma/,",")
				}
				else if(AllowedTypes[j]=="dblqt")
				{
					TheRegExp+="\\\""
					TheSpValue=TheSpValue.replace(/dblqt/,"\\\"")
				}
				else if(AllowedTypes[j]=="snglqt")
				{
					TheRegExp+="'"
					TheSpValue=TheSpValue.replace(/snglqt/,"'")
				}
				else
				{
					TheRegExp+=AllowedTypes[j]
					StartString+=AllowedTypes[j]
				}
			}
			else if(AllowedTypes[j]=="0-9")
			{
				TheRegExp+="0-9"
				StartString+="0-9"
			}
			else if(AllowedTypes[j]==".")
			{
				TheRegExp+="\\\."
				TheSpValue=TheSpValue.replace(/\./,"\\\.")
			}
			else if(AllowedTypes[j]=="-")
			{
				TheRegExp+="\\\-"
				TheSpValue=TheSpValue.replace(/\-/,"\\\-")
			}
			else if(AllowedTypes[j]=="&")
			{
				TheRegExp+="\\\&"
				TheSpValue=TheSpValue.replace(/\&/,"\\\&")
			}
			else if(AllowedTypes[j]=="(")
			{
				TheRegExp+="\\\("
				TheSpValue=TheSpValue.replace(/\(/,"\\\(")
			}
			else if(AllowedTypes[j]==")")
			{
				TheRegExp+="\\\)"
				TheSpValue=TheSpValue.replace(/\)/,"\\\)")
			}
			else if(AllowedTypes[j]=="[")
			{
				TheRegExp+="\\\["
				TheSpValue=TheSpValue.replace(/\[/,"\\\[")
			}
			else if(AllowedTypes[j]=="]")
			{
				TheRegExp+="\\\]"
				TheSpValue=TheSpValue.replace(/\]/,"\\\]")
			}
			else if(AllowedTypes[j]=="?")
			{
				TheRegExp+="\\\?"
				TheSpValue=TheSpValue.replace(/\?/,"\\\?")
			}
			else if(AllowedTypes[j]!="")
			{
				TheRegExp+=AllowedTypes[j]
			}
		}
		
		if(TheSpValue!="")
		{
			TheSpValues=TheSpValue.split(",")
			for(j=0;j<TheSpValues.length;j++)
			{
				if(TheSpValues[j]!="")
				{
					EndString+=TheSpValues[j]
				}
			}
			EndString="["+EndString+"]?"
		}
		
		ThePattern="^["+StartString+"]["+TheRegExp+"]*["+StartString+"]"+EndString+"$|^["+StartString+"]"+EndString+"$"
			
		TheExpression=new RegExp(ThePattern,"gi")
	}


	//Finally the checking starts.
	if(typeof(Mandatory)=="undefined" || Mandatory=="")
	{
		if(!TheValue.match(/^$/ig))
		{
			if(!TheValue.match(TheExpression))
			{
				if(typeof(Label)!="undefined")
					alert(Label)
				TheField.focus()
				if(TheField.type!="select-one")
				{
					TheField.select()
				}

				return true
			}
			else
			{
				if(TheType=="url")
				{
					if(TheValue.match(TheExpression)[0].length!=TheValue.length)
					{
						if(typeof(Label)!="undefined")
							alert(Label)
						TheField.focus()
						if(TheField.type!="select-one")
						{
							TheField.select()
						}
						return true
					}
				}
				if(typeof(LengthCheck)!="undefined")
				{
					return CheckLength(TheField,TheValue,LengthCheck,LengthAlert)
				}
			}
		}
	}
	else
	{
		if(!TheValue.match(TheExpression))
		{
			if(typeof(Label)!="undefined")
				alert(Label)
			if(TheField.type!="select-one")
			{
				TheField.select()
			}
			return true

		}
		else
		{
			if(TheType=="url")
			{
				if(TheValue.match(TheExpression)[0].lengh!=TheValue.length)
				{
					if(typeof(Label)!="undefined")
						alert(Label)
					TheField.focus()
					if(TheField.type!="select-one")
					{
						TheField.select()
					}
					return true
				}
			}
			if(typeof(LengthCheck)!="undefined" || LengthCheck=="")
			{
				return CheckLength(TheField,TheValue,LengthCheck,LengthAlert)
			}
		}
	}
	return false
}

function CheckLength(TheField,TheString,TheLength,TheAlert)
{
	TheLength=TheLength.split(",")
	Min=parseInt(TheLength[0])
	Max=parseInt(TheLength[1])
	if(TheString.length<Min)
	{
		alert(TheAlert)
		TheField.focus()
		if(TheField.type!="select-one")
		{
			TheField.select()
		}
		return true
	}
	if(Max!=0 && TheString.length>Max)
	{
		alert(TheAlert)
		TheField.focus()
		if(TheField.type!="select-one")
		{
			TheField.select()
		}
		return true
	}
	return false
}
function validate(FunctionToCall,TheForm)
{
	
	if(typeof(TheForm)=="undefined")
		TheFormElements=document.forms[0].elements
	else
		TheFormElements=TheForm
		
	for(i=0;i<TheFormElements.length;i++)
	{
		if(TheFormElements[i].getAttribute("ValidationType")!=null)
		{
			ValidationType=TheFormElements[i].getAttribute("ValidationType")
			LengthCheck=TheFormElements[i].getAttribute("LengthCheck")	
			if(LengthCheck==null)
				LengthCheck=""
			LengthAlert=TheFormElements[i].getAttribute("LengthAlert")
			if(LengthAlert==null)
				LengthAlert=""
			if(TheFormElements[i].getAttribute("EmptyLabel")!=null)
			{
				if(TheFormElements[i].getAttribute("InvalidLabel")==null)
				{
					if(Validate(TheFormElements[i],"empty",TheFormElements[i].getAttribute("EmptyLabel"),"",LengthCheck,LengthAlert))
					{
						return false
						break
					}
				}
				else
				{
					if(Validate(TheFormElements[i],"empty",TheFormElements[i].getAttribute("EmptyLabel")))
					{
						return false
						break
					}
				}
			}
			if(TheFormElements[i].getAttribute("InvalidLabel")!=null)
			{
				if(Validate(TheFormElements[i],ValidationType,TheFormElements[i].getAttribute("InvalidLabel"),"",LengthCheck,LengthAlert))
				{
					return false
					break
				}
			}
			
			if(TheFormElements[i].getAttribute("CallNext")!=null)
			{
				TheFn=TheFormElements[i].getAttribute("CallNext")
				TheFn=TheFn.replace(/this\.value/,"'"+TheFormElements[i].value.replace(/'/g,'VALIDATE_SINGLE_QOUTES')+"'")
				if(!eval(TheFn))
				{
					return false
					break
				}
			}
		}
	}
	if(typeof(FunctionToCall)=="undefined" || FunctionToCall=="")
		return true
	else
		return eval(FunctionToCall)
}

//Credit Card Validation
function CheckCreditCard(str,TheAlert)
{
	
	str=str.replace(/VALIDATE_SINGLE_QOUTES/g,"'")
	SecondNumber=false
	NewNumber=""
	CheckSum=0
	for(j=str.length;j>-1;j--)
	{
		TheNumber=parseInt(str.substring(j,j+1))
		if(!isNaN(TheNumber))
		{
			if(SecondNumber)
			{
				NewNumber+=(TheNumber*2)
			}
			else
			{
				NewNumber+=TheNumber
			}
			SecondNumber=(!SecondNumber)
		}
	}
	for(j=NewNumber.length;j>-1;j--)
	{
		TheNumber=parseInt(NewNumber.substring(j,j+1))
		if(!isNaN(TheNumber))
		{
			CheckSum+=TheNumber
		}
	}
	if(CheckSum % 10 !=0)
	{
		if(typeof(TheAlert)=="undefined" || TheAlert=="")
			TheAlert="Invalid card number."
		alert(TheAlert)
		return false
	}
	return true
}


//Mark mandatory fields.
var MarkMandatoryFields=true
var MandatoryMark='<font color="#FF0000">*</font>'
function MarkMandatoryFieldsFn(TheForm)
{
	if(typeof(TheForm.elements)=="undefined")
		TheFormElements=document.forms[0].elements
	else
		TheFormElements=TheForm
	
	for(i=0;i<TheFormElements.length;i++)
	{
		if(TheFormElements[i].getAttribute("EmptyLabel")!=null && MarkMandatoryFields)
		{
			TheParentNode=TheFormElements[i].parentNode
			var MandatoryElem=document.createElement("span")
			MandatoryElem.innerHTML=MandatoryMark
			TheParentNode.appendChild(MandatoryElem)
		}
	}
}


//Give focus to first field.
var GiveFocusToFirstField=false
function GiveFocusToFirstFieldFn(TheForm)
{
	
	if(typeof(TheForm.elements)=="undefined")
		TheFormElements=document.forms[0].elements
	else
		TheFormElements=TheForm
	
	//alert(TheFormElements.length)

	if(GiveFocusToFirstField)
	{
		for(i=0;i<TheFormElements.length;i++)
		{
			if(TheFormElements[i].name && TheFormElements[i].type!="hidden")
			{
				//alert(TheFormElements[i].name);
				TheFormElements[i].focus()
				break
			}		
		}
	}
}

function TrimSpace(vInput)
{
		if (vInput.type=="text")
		{	
			vInput.value=vInput.value.replace(/\s+$/,"")
			vInput.value=vInput.value.replace(/^[\s]+/,"")
		}	
}


/*
	Format:
		Validate(Field,Type of validation,Alert Label, Mandatory, LengthCheck, LengthAlert)
		1. Field - The field that must be validated. e.g.: document.Form.FieldName
		2. Types - Refer below.
		3. Alert Label - The text that will throw up in the alert.
			Default label is available for type no.6, 7, 9
		4. Mandatory - Some value must be given if a field is mandatory(Refer Step 3).
		5. LengthCheck - Maximum and minumum length of field seperated by ,.
			e.g.: "5,15"
			a. If minimum is not necessary then "0,10"
			b. If maximum is not necessary then "10,0"
		6. LengthAlert - The alert that should pop up when the above condition fails.
		
	Types:
		1. empty.
		2. alphanumeric.
			Allows: a-z,A-Z,0-9
		3. alphabets.
			Allows: a-z,A-Z
		4. numbers.
			Allows: 0-9
		5. float.
			Allows: 0-9,.
		6. mobile/phone.
			Allows: 0-9,-,spaces
		7. email.
		8. locations.
			Allows: a-z,A-Z,0-9,-,_,.,spaces
		8. image.
			Allows: jpg,jpeg,gif
		10. html.
			Allows: html, htm formats.
		11. other format.
			Definition: other format - .FormatType (e.g: other format - csv)
			The above is for checking for other formats. Multiple formats must be seperated
			with a '|' (e.g: other format - csv|exe).
		12. Any other arguement:
			e.g: a-z,0-9,_,spaces,comma,-
				
	Steps in validation:
		1. Check validation type.
		2. Check whether field is mandatory.
		3. Mandatory will be undefined or "" if it is omitted while calling the function.
		4. Check if there is any value in the field.
		5. If there is value then validate.
		6. Check if there is any length validation.

	ASPCAE - Allow Special Characters at the end.
*/
function Validate(TheField,TheType,Label,Mandatory,LengthCheck,LengthAlert)
{
	TrimSpace(TheField)
	if(TheType=="url")
	{
		alert("Validate:\nThe type url has been removed.")
		return true
	}
	if(typeof(TheField)=="undefined")
	{
		alert("Validate:\nField name does not exist.")
		return true
	}
	if(typeof(TheField.value)=="undefined")
	{
		alert("Validate:\nThis field does not support 'value' method.")
		return true
	}
	TheValue=TheField.value
	TheValue=TheValue.replace(/\n/g,"")
	TheValue=TheValue.replace(/\r/g,"")
	TheType=TheType.toLowerCase()
	
	if(TheType=="empty")
	{
		if(!TheValue.match(/^.+$/ig))
		{
			if(typeof(Label)!="undefined")
				alert(Label)
			TheField.focus()
			if(TheField.type!="select-one")
			{
				TheField.select()
			}
			
			return true
		}
		TheExpression=new RegExp(".")
	}
	else if(TheType=="alphabets")
	{
		TheExpression=new RegExp("^[a-z][a-z\\-\\.]*[a-z]|^[a-z]$","gi")
	}
	else if(TheType=="locations")
	{
		TheExpression=new RegExp("^[a-z][a-z0-9\\s\\-\\._]*[a-z]$|^[a-z]$","gi")
	}
	else if(TheType=="alphanumeric")
	{
		TheExpression=new RegExp("^[a-z0-9]+$","gi")
	}
	else if(TheType=="numbers")
	{
		TheExpression=new RegExp("^[\\d]+$","gi")
	}
	else if(TheType=="float")
	{
		TheExpression=new RegExp("^[\\d]+(\\.[\\d]+)?$","gi")
	}
	else if(TheType=="phone" || TheType=="mobile")
	{
		TheExpression=new RegExp("^(\\(?[0-9]+\\)?[\\s\\-])*[0-9]+$","gi")
	}
	else if(TheType=="email")
	{
		if(Label=="")
		{
			Label="Please enter a valid email id."
		}
		TheExpression=new RegExp("^[a-z]([\\-_\\.]?[a-z0-9]+)*@([a-z0-9]+([\\-_][a-z0-9]+)*\\.)+[a-z]{2,}$","gi")
	}
	else if(TheType=="image")
	{
		if(Label=="")
		{
			Label="Please enter a valid image name."
		}
		TheExpression=new RegExp("^[^']+\\.(jpg|jpeg|gif)$","gi")
	}
	else if(TheType=="html")
	{
		TheExpression=new RegExp("^[^']+\\.(htm|html)$","gi")
	}
	else if(TheType.match(/^other format/))
	{
		Format=TheType.replace(/^other format - (.*)$/,"$1")
		TheExpression=new RegExp("\\.("+Format+")$","gi")
	}
	else if(TheType=="regex")
	{
		ThePattern=TheField.getAttribute("Pattern")
		if(ThePattern==null)
		{
			alert("Validation: The field '"+TheField.name+"' does not include the 'Pattern' keyword.")
			return false
		}
		TheExpression=new RegExp(ThePattern,"gi")
	}
	else
	{
		TheType1=TheType.replace(/spaces?/g,"")
		TheType1=TheType1.replace(/commas?/g,"")
		if((!TheType1.match(/[a-z]/)) && TheType1.indexOf("0-9")==-1)
		{
			alert("Validate:\nInvalid parameters.\n"+TheType)
			return true
		}
		TheRegExp=""
		StartString=""
		EndString=""
		
		TheSpValue=TheField.getAttribute("ASPCAE")
		if(TheSpValue==null)
			TheSpValue=""
		else
			TheSpValue=TheSpValue.toLowerCase()
		
			
		AllowedTypes=TheType.toLowerCase().split(",")
		for(j=0;j<AllowedTypes.length;j++)
		{
			if(AllowedTypes[j].match(/[a-z]-[a-z]/) || AllowedTypes[j].match(/[a-z]/))
			{
				if(AllowedTypes[j]=="spaces" || AllowedTypes[j]=="space")
				{
					TheRegExp+="\\\s"
					TheSpValue=TheSpValue.replace(/spaces/,"\\\s")
					TheSpValue=TheSpValue.replace(/space/,"\\\s")
				}
				else if(AllowedTypes[j]=="comma" || AllowedTypes[j]=="commas")
				{
					TheRegExp+=","
					TheSpValue=TheSpValue.replace(/comma/,",")
				}
				else if(AllowedTypes[j]=="dblqt")
				{
					TheRegExp+="\\\""
					TheSpValue=TheSpValue.replace(/dblqt/,"\\\"")
				}
				else if(AllowedTypes[j]=="snglqt")
				{
					TheRegExp+="'"
					TheSpValue=TheSpValue.replace(/snglqt/,"'")
				}
				else
				{
					TheRegExp+=AllowedTypes[j]
					StartString+=AllowedTypes[j]
				}
			}
			else if(AllowedTypes[j]=="0-9")
			{
				TheRegExp+="0-9"
				StartString+="0-9"
			}
			else if(AllowedTypes[j]==".")
			{
				TheRegExp+="\\\."
				TheSpValue=TheSpValue.replace(/\./,"\\\.")
			}
			else if(AllowedTypes[j]=="-")
			{
				TheRegExp+="\\\-"
				TheSpValue=TheSpValue.replace(/\-/,"\\\-")
			}
			else if(AllowedTypes[j]=="&")
			{
				TheRegExp+="\\\&"
				TheSpValue=TheSpValue.replace(/\&/,"\\\&")
			}
			else if(AllowedTypes[j]=="(")
			{
				TheRegExp+="\\\("
				TheSpValue=TheSpValue.replace(/\(/,"\\\(")
			}
			else if(AllowedTypes[j]==")")
			{
				TheRegExp+="\\\)"
				TheSpValue=TheSpValue.replace(/\)/,"\\\)")
			}
			else if(AllowedTypes[j]=="[")
			{
				TheRegExp+="\\\["
				TheSpValue=TheSpValue.replace(/\[/,"\\\[")
			}
			else if(AllowedTypes[j]=="]")
			{
				TheRegExp+="\\\]"
				TheSpValue=TheSpValue.replace(/\]/,"\\\]")
			}
			else if(AllowedTypes[j]=="?")
			{
				TheRegExp+="\\\?"
				TheSpValue=TheSpValue.replace(/\?/,"\\\?")
			}
			else if(AllowedTypes[j]!="")
			{
				TheRegExp+=AllowedTypes[j]
			}
		}
		
		if(TheSpValue!="")
		{
			TheSpValues=TheSpValue.split(",")
			for(j=0;j<TheSpValues.length;j++)
			{
				if(TheSpValues[j]!="")
				{
					EndString+=TheSpValues[j]
				}
			}
			EndString="["+EndString+"]?"
		}
		
		ThePattern="^["+StartString+"]["+TheRegExp+"]*["+StartString+"]"+EndString+"$|^["+StartString+"]"+EndString+"$"
			
		TheExpression=new RegExp(ThePattern,"gi")
	}


	//Finally the checking starts.
	if(typeof(Mandatory)=="undefined" || Mandatory=="")
	{
		if(!TheValue.match(/^$/ig))
		{
			if(!TheValue.match(TheExpression))
			{
				if(typeof(Label)!="undefined")
					alert(Label)
				TheField.focus()
				if(TheField.type!="select-one")
				{
					TheField.select()
				}

				return true
			}
			else
			{
				if(TheType=="url")
				{
					if(TheValue.match(TheExpression)[0].length!=TheValue.length)
					{
						if(typeof(Label)!="undefined")
							alert(Label)
						TheField.focus()
						if(TheField.type!="select-one")
						{
							TheField.select()
						}
						return true
					}
				}
				if(typeof(LengthCheck)!="undefined")
				{
					return CheckLength(TheField,TheValue,LengthCheck,LengthAlert)
				}
			}
		}
	}
	else
	{
		if(!TheValue.match(TheExpression))
		{
			if(typeof(Label)!="undefined")
				alert(Label)
			if(TheField.type!="select-one")
			{
				TheField.select()
			}
			return true

		}
		else
		{
			if(TheType=="url")
			{
				if(TheValue.match(TheExpression)[0].lengh!=TheValue.length)
				{
					if(typeof(Label)!="undefined")
						alert(Label)
					TheField.focus()
					if(TheField.type!="select-one")
					{
						TheField.select()
					}
					return true
				}
			}
			if(typeof(LengthCheck)!="undefined" || LengthCheck=="")
			{
				return CheckLength(TheField,TheValue,LengthCheck,LengthAlert)
			}
		}
	}
	return false
}

function CheckLength(TheField,TheString,TheLength,TheAlert)
{
	TheLength=TheLength.split(",")
	Min=parseInt(TheLength[0])
	Max=parseInt(TheLength[1])
	if(TheString.length<Min)
	{
		alert(TheAlert)
		TheField.focus()
		if(TheField.type!="select-one")
		{
			TheField.select()
		}
		return true
	}
	if(Max!=0 && TheString.length>Max)
	{
		alert(TheAlert)
		TheField.focus()
		if(TheField.type!="select-one")
		{
			TheField.select()
		}
		return true
	}
	return false
}
function validate(FunctionToCall,TheForm)
{
	if(typeof(TheForm)=="undefined")
		TheFormElements=document.forms[0].elements
	else
		TheFormElements=TheForm
		
	for(i=0;i<TheFormElements.length;i++)
	{
		if(TheFormElements[i].getAttribute("ValidationType")!=null)
		{
			ValidationType=TheFormElements[i].getAttribute("ValidationType")
			LengthCheck=TheFormElements[i].getAttribute("LengthCheck")	
			if(LengthCheck==null)
				LengthCheck=""
			LengthAlert=TheFormElements[i].getAttribute("LengthAlert")
			if(LengthAlert==null)
				LengthAlert=""
			if(TheFormElements[i].getAttribute("EmptyLabel")!=null)
			{
				if(TheFormElements[i].getAttribute("InvalidLabel")==null)
				{
					if(Validate(TheFormElements[i],"empty",TheFormElements[i].getAttribute("EmptyLabel"),"",LengthCheck,LengthAlert))
					{
						return false
						break
					}
				}
				else
				{
					if(Validate(TheFormElements[i],"empty",TheFormElements[i].getAttribute("EmptyLabel")))
					{
						return false
						break
					}
				}
			}
			if(TheFormElements[i].getAttribute("InvalidLabel")!=null)
			{
				if(Validate(TheFormElements[i],ValidationType,TheFormElements[i].getAttribute("InvalidLabel"),"",LengthCheck,LengthAlert))
				{
					return false
					break
				}
			}
			
			if(TheFormElements[i].getAttribute("CallNext")!=null)
			{
				TheFn=TheFormElements[i].getAttribute("CallNext")
				TheFn=TheFn.replace(/this\.value/,"'"+TheFormElements[i].value.replace(/'/g,'VALIDATE_SINGLE_QOUTES')+"'")
				if(!eval(TheFn))
				{
					return false
					break
				}
			}
		}
	}
	if(typeof(FunctionToCall)=="undefined" || FunctionToCall=="")
		return true
	else
		return eval(FunctionToCall)
}

//Credit Card Validation
function CheckCreditCard(str,TheAlert)
{
	
	str=str.replace(/VALIDATE_SINGLE_QOUTES/g,"'")
	SecondNumber=false
	NewNumber=""
	CheckSum=0
	for(j=str.length;j>-1;j--)
	{
		TheNumber=parseInt(str.substring(j,j+1))
		if(!isNaN(TheNumber))
		{
			if(SecondNumber)
			{
				NewNumber+=(TheNumber*2)
			}
			else
			{
				NewNumber+=TheNumber
			}
			SecondNumber=(!SecondNumber)
		}
	}
	for(j=NewNumber.length;j>-1;j--)
	{
		TheNumber=parseInt(NewNumber.substring(j,j+1))
		if(!isNaN(TheNumber))
		{
			CheckSum+=TheNumber
		}
	}
	if(CheckSum % 10 !=0)
	{
		if(typeof(TheAlert)=="undefined" || TheAlert=="")
			TheAlert="Invalid card number."
		alert(TheAlert)
		return false
	}
	return true
}


//Mark mandatory fields.
var MarkMandatoryFields=true
var MandatoryMark='<font color="#FF0000">*</font>'
function MarkMandatoryFieldsFn(TheForm)
{
	if(typeof(TheForm.elements)=="undefined")
		TheFormElements=document.forms[0].elements
	else
		TheFormElements=TheForm
	
	for(i=0;i<TheFormElements.length;i++)
	{
		if(TheFormElements[i].getAttribute("EmptyLabel")!=null && MarkMandatoryFields)
		{
			TheParentNode=TheFormElements[i].parentNode
			var MandatoryElem=document.createElement("span")
			MandatoryElem.innerHTML=MandatoryMark
			TheParentNode.appendChild(MandatoryElem)
		}
	}
}


//Give focus to first field.
var GiveFocusToFirstField=true
function GiveFocusToFirstFieldFn(TheForm)
{
	if(typeof(TheForm.elements)=="undefined")
		TheFormElements=document.forms[0].elements
	else
		TheFormElements=TheForm
	
	//alert(TheFormElements.length)

	if(GiveFocusToFirstField)
	{
		for(i=0;i<TheFormElements.length;i++)
		{
			if(TheFormElements[i].name && TheFormElements[i].type!="hidden")
			{
				//alert(TheFormElements[i].name);
				TheFormElements[i].focus()
				break
			}		
		}
	}
}

if(navigator.appName.indexOf("Microsoft")!=-1)
{
	window.attachEvent("onload",MarkMandatoryFieldsFn)
	window.attachEvent("onload",GiveFocusToFirstFieldFn)
}
else
{
	window.addEventListener('load',MarkMandatoryFieldsFn,false)
	window.addEventListener('load',GiveFocusToFirstFieldFn,false)
}

function TrimSpace(vInput)
{
		if (vInput.type=="text")
		{	
			vInput.value=vInput.value.replace(/\s+$/,"")
			vInput.value=vInput.value.replace(/^[\s]+/,"")
		}	
}


///////////////////////////////////// Javascript/////////////////////////////////////////////////////
/*
	This file contains a list of common javascript functions used through out the site.
	
	Created By - Sathiya Moorthy
*/


/*
	Function - LoadPage
	Use - To load a PHP file and send it information. This function can be used to load PHP files using javascript to check for duplication.
	
	Arguements:
		PageName - Name of the PHP file to load. This arguement is string in nature. e.g.: LoadPage('hello.php')

		This function takes "undefined" arguements. Any arguements passed to this function other than PageName are taken as fields whose values
		must be posted to the PHP file in "PageName".
		The above arguements are objects in nature. e.g.: LoadPage('hello.php',document.forms[0].field1,document.forms[0].field2)
	
	Conditions:
		This function requires that the page calling it defines 2 functions:
		a. OnRequest - This function will keep running continuously from the start of the request to the end of the request.
					   This function can hold animation etc while the request is being made.

		a. OnRequestCompletion - This function is fired once the request is completed. This function must have a parameter that can recieve the
								 output that the file called in "PageName" gives back to javascript. This output will be string in nature.
*/
var request

function LoadPage(PageName)
{
	if(typeof(OnRequest)=="undefined" || typeof(OnRequestCompletion)=="undefined")
	{
		alert("Please define the functions called 'OnRequest' and 'OnRequestCompletion'.")
		return false
	}
	document.getElementById("DuplicationElem").style.display=""
	request = new ActiveXObject("Microsoft.XMLHTTP");
	request.open('post',PageName,true)
	request.setRequestHeader('content-type','application/x-www-form-urlencoded')
	SendString=""
	for(i=1;i<LoadPage.arguments.length;i++)
	{
		FieldToSend=LoadPage.arguments[i]
		SendString+=FieldToSend.name+'='+escape(FieldToSend.value)+"&"
	}
	SendString=SendString.substring(0,SendString.length-1)
	request.send(SendString)
	request.onreadystatechange=SeekInfo
}
function SeekInfo()
{
	if(request.readyState != 4)
	{
		OnRequest()
	}
	else
	{ 
		var answer = request.responseText;
		OnRequestCompletion(answer)
	}
}



/*
	Function - AtleastOneSelected
	Use - To see if atleast one checkbox amongst a group is selected.
	
	Arguements:
		TheElement - The checkbox object.
		TheElementAlert - The alert to be thrown if none is selected.
		TheConfirmAlert - The confirmation alert to be thrown.
	
	Returns:
		No checkbox is selected:
			returns false 

		1 or more checkbox selected:
			OK is clicked in the confirm box - 	true
			Cancel is clicked in the confirm box - 	false
*/

function AtleastOneSelected(TheElement,TheEmptyAlert,TheConfirmAlert,TheHiddenField)
{
	TheHiddenField.value=","
	if(typeof(TheEmptyAlert)=="undefined" || TheEmptyAlert=="")
		TheEmptyAlert='Please select atleast one item.'

	if(typeof(TheConfirmAlert)=="undefined" || TheConfirmAlert=="")
		TheConfirmAlert='Deleted items cannot be restored.\nContinue?'
	
	if(typeof(TheElement.length)=="undefined")
	{
		if(TheElement.checked==false)
		{
			alert(TheEmptyAlert)
			return false
		}
		else
		{
			TheHiddenField.value+=TheElement.value+","
		}
	}
	else
	{
		count=0

		for(i=0;i<TheElement.length;i++)
		{
			if(TheElement[i].checked)
			{
				count++
				TheHiddenField.value+=TheElement[i].value+","
			}
		}
		if(count==0)
		{
			alert(TheEmptyAlert)
			return false
		}
	}
	return confirm(TheConfirmAlert)
}


/*
	Function - resubmit
	Use - Resubmit a form to itself when a select box item is changed.
	
	Arguements:
		TheForm - The form object.
		TheSelectBox - The select box object.
		TheCurrentValue - Resubmitted value of the select box. This will be string in nature.
		TheEmptyValue - The value given for "Please Select" option. This will be string in nature.
		ThePage - By default this function will submit the page to itself. If this option is given then the page will submit the page name given in this arguement. This will be string in nature.
		SubmitForEmpty - By default this function will submit the page only if values other than "Please Select" is chosen. If this arguement is passed then it will also resubmit for the "Please Select" option. This will be string in nature.
*/
function resubmit(TheForm,TheSelectBox,TheCurrentValue,TheEmptyValue,ThePage,SubmitForEmpty)
{

	if(typeof(TheCurrentValue)=="undefined")
		TheCurrentValue="";

	if(typeof(TheEmptyValue)=="undefined")
		TheEmptyValue="";

	if(typeof(ThePage)=="undefined" || ThePage=="")
		ThePage=location.href.replace(/.+\/(.+)$/,"$1")

	if(typeof(SubmitForEmpty)=="undefined")
		SubmitForEmpty=false
	
	if(SubmitForEmpty)
	{
		if(TheSelectBox.value!=TheCurrentValue)
		{
			TheForm.action=ThePage
			TheForm.method="post"
			TheForm.submit()
		}
	}
	else
	{
		if(TheSelectBox.value!=TheEmptyValue && TheSelectBox.value!=TheCurrentValue)
		{
			TheForm.action=ThePage
			TheForm.method="post"
			TheForm.submit()
		}
	}
}

/*
	Function - ChooseAll
	Use - Chooses all the checkboxes of a particluar group. It can take a reference checkbox.
	
	Arguements:
		TheElement - The checkbox group object.
		TheRefElement- The element that will act as the reference. This must alos be a checkbox.
*/
function ChooseAll(TheElement,TheRefElement)
{

	if(typeof(TheElement)=="undefined")
	{
		return false
	}
	TheStatus=true
	if(typeof(TheRefElement)!="undefined" && TheRefElement.type=="checkbox")
	{
		TheStatus=TheRefElement.checked
	}

	if(typeof(TheElement.length)=="undefined")
	{
		if(TheElement.type=="checkbox")
		{
			TheElement.checked=TheStatus
		}
		else if(TheElement.type=="select")
		{
			TheElement.selected=TheStatus
		}
	}
	else
	{
		for(i=0;i<TheElement.length;i++)
		{
			if(TheElement[i].type=="checkbox")
			{
				TheElement[i].checked=TheStatus
			}
			else if(TheElement[i].type=="select")
			{
				TheElement[i].selected=TheStatus
			}
		}
	}
}

/*
	Function - TrackChange
	Use - Tracks change in checkbox statuses
	
	Arguements:
		TheCheckBox - The induvidual checkbox object. e.g.: TrackChange(this)
	
	Conditions:
		The checkbox must have an attribute called "RecordId"
*/
var HideStr=""
function TrackChange(TheCheckBox)
{
	TheValue=TheCheckBox.getAttribute("RecordId")

	if(HideStr.indexOf('***'+TheValue+'-')!=-1)
	{
		HideStr=HideStr.replace('***'+TheValue+'-'+'false***','')
		HideStr=HideStr.replace('***'+TheValue+'-'+'true***','')
	}
	else
	{
		if(TheCheckBox.checked)
		{
			HideStr+='***'+TheValue+'-'+'true***'
		}
		else
		{
			HideStr+='***'+TheValue+'-'+'false***'
		}
	}
}


	/*Function - ChangeStatus
	Use - This function must always be called by the (De)Activate button.
	
	Arguements:
		TheForm - The form object.
		TheAction - The action of the form object. Must be a php file name.
		TheEmptyAlert - The alert to be shown if no changes have been made to records.
		TheConfirmAlert - The confirmation alert.
		TheHiddenField - The hidden field that will store the ids to be sent across to the page given in action attribute.*/
function ChangeStatus(TheForm,TheAction,TheEmptyAlert,TheConfirmAlert,TheHiddenField)
{
	if(HideStr=="")
	{
		alert(TheEmptyAlert)
		return false
	}
	else
	{

		if(confirm(TheConfirmAlert))
		{
			TheHiddenField.value=HideStr
			TheForm.action=TheAction
			TheForm.submit()
		}
		else
		{
			return false
		}
	}
}


/*
	Function - GoToPage
	Use - Part of the pagination framework.
	
	Arguements:
		ThePage - The page number the page must go to.
*/
function GoToPage(ThePage)
{
	TheUrl=location.href
	document.forms[0].CurrentPage.value=ThePage
	document.forms[0].action = TheUrl.replace(/.*\/(.*)/,"$1")
	document.forms[0].submit()
}


/*
	Function - OnRequest
	Use - Part of the XMLHTTP framework. This function will keep firing when the request is beign made.
	
	Conditions:
		Requires an element called "DuplicationElem" defined.
*/


function CheckPassword(NewPassWord,ConfirmPassword)
{
	var strPass = NewPassWord.value;
	var strconfPass = ConfirmPassword.value;
	if (strPass!=strconfPass) 
	{
		alert("New Password and Confirm Password are not same")
		ConfirmPassword.focus();
		return false;
	}
	return true;
}

// this function deletes the selected files. The parameters to be passed is same as for function AtleastOneSelected() and finally add where the page it has to be submitted





function EmptyField(TheField)
{
	TheField.value=""
}













