var ErrMessage;
var ErrItem;
var ErrFound = false;
var EmailIdLastMaxChar = 20
var EmailIdLastMinChar = 0
var NormalCharString = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\r\n\f";
var NumericString = "1234567890";

//Check the given item value is null or not.
//If the given Item value is null and compalsary(C) then it sets the error message and return true, Otherwise, it just return true.
//If the given item value is not null then it returns false.
function CheckNull(){	
	Item = arguments[0];
	if ((Item.type == "text" || Item.type == "password" || Item.type == "textarea" || Item.type == "file" || Item.type == "hidden") && Item.value == "")
		return (setError("Please Enter  A "+Item.name, Item, true));
	else if (Item.name!="paymentto" && Item.type == "select-one" && Item.selectedIndex <= 0)	
		return (setError("Please Select "+Item.name, Item, true));
	return false;
}

//Check All Spaces
function CheckAllSpaces(){
	for(var i=0;i<arguments[0].value.length;i++)
		if (arguments[0].value.charAt(i) != " ") return false;
	return (setError("Please Enter A "+Item.name, Item, true));
}

//Remove Leading spaces
function LTrimSpaces(){
	for (var i=0;i<arguments[0].value.length;i++)
		if (arguments[0].value.charAt(i) != " ") break;
	return arguments[0].value.substr(i);
}	

//Remove Trailing spaces
function RTrimSpaces(){
	for (var i=arguments[0].value.length-1;i>0;i--)
		if (arguments[0].value.charAt(i) != " ") break;
	return arguments[0].value.substr(0,i+1);
}	

//Check the every charecter in the given Item value, that is in the given character list or not.
//If the character not in the given character list it sets the error message and return true.
//If the all the characters in the given Item value are in the given character list, it returns false.
function CheckeachChar(){
	Item = arguments[0];
	if (Item.type == "text" || Item.type == "password" || Item.type=="textarea" || Item.type == "file" || Item.type == "hidden"){
		var CharString = arguments[1];
		for(var i=0;i<Item.value.length;i++){
			if (CharString.indexOf(Item.value.charAt(i)) == -1){
				return(setError("Invalid "+Item.name+"  ! Please Re-enter ", Item, true));
			}
		}
	}
	return false;
}






//Check the given Item value is email Id or not.
//If the '@' symbol at begining or not entered it will set Error message.
//If the '@' symbol occured more than one's it set's Error message.
//After the '@' symbol there is no '.' symbol it will set's Error message.
//If '@' symbol and '.' symbol occured together then it will set's Error message.
//If '.' symbol occured at end of the Item value it will set's Error message.
//If the the email id is correct then it just return false.
function CheckEmailId(){
	Item = arguments[0];
	var IndexofAtdharate = Item.value.indexOf("@");
	var LastIndexofPeriod = Item.value.lastIndexOf(".");
	var IndexOfPeriod = Item.value.indexOf(".");
	if ((LastIndexofPeriod + EmailIdLastMinChar + 1 > Item.value.length) || (Item.value.length > LastIndexofPeriod + EmailIdLastMaxChar + 1))
		setError("Invalid Email Address! Please Re-enter ", Item, true);
	else if (IndexofAtdharate == -1 || IndexofAtdharate == 0)
		setError("Invalid Email Address! Please Re-enter ", Item, true);
	else if (IndexofAtdharate < Item.value.lastIndexOf("_"))
		setError("Invalid Email Address! Please Re-enter ", Item, true);
	else if (IndexofAtdharate == (Item.value.indexOf(".") - 1))
		setError("Invalid Email Address! Please Re-enter ", Item, true);
	else if (IndexofAtdharate != Item.value.lastIndexOf("@"))
		setError("Invalid Email Address! Please Re-enter ", Item, true);
	else if (Item.value.substring(IndexofAtdharate+1).indexOf(".") == -1) 
		setError("Invalid Email Address! Please Re-enter ", Item, true);
	else if (Item.value.substring(0,IndexofAtdharate).lastIndexOf(".") == IndexofAtdharate-1)
		setError("Invalid Email Address! Please Re-enter ", Item, true);
 	else
	{
		var CharString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_-.@";
		for(var i=0;i<Item.value.length;i++){
			if (CharString.indexOf(Item.value.charAt(i)) == -1){
				setError("Invalid Email Address! Please Re-enter ", Item, true);
				return true;
			}
		}
		return false;
	}
	return true;
}




//Check the Item length with in the given limits or not. If it is then returns false, Otherwise set the Error message and return true.
function CheckLength(){
	Item = arguments[0];
	minLength = arguments[1];
	maxLength = arguments[2];
	if (Item.value.length < minLength || Item.value.length > maxLength)
		return(setError(
		
	Item.name +" must contains at least "+minLength+" characters and no more than "+maxLength+ " characters ", Item, true));
	
	
	
//	"Please Enter minimum of " + minLength 
	//		+ " Characters and maximum of " + maxLength + " Characters in " + Item.name, Item, true));
	else	return false;
	
}	

//Check the given Item begins with any given charecters then it sets error message and return true, Otherwise return false.
function CheckBeginingChars(Item,Chars){
	Item = arguments[0];
	Chars = arguments[1];
	var NotAllowedChars = Chars.split("");
	for(var i=0;i<NotAllowedChars.length;i++){
		if (Item.value.indexOf(NotAllowedChars[i]) == 0){
			/*switch(NotAllowedChars[i]){
				case "." : return (setError("Starting letter should not be period(.) in "+Item.name, Item, true));
				case "/" : return (setError("Starting letter should not be slash(/) in "+Item.name, Item, true));
				case "'" : return (setError("Starting letter should not be astric(') in "+Item.name, Item, true));
				case "-" : return (setError("Starting letter should not be hyphen(-) in "+Item.name, Item, true));
				case "_" : return (setError("Starting letter should not be underscore(_) in "+Item.name, Item, true));
				case "," : return (setError("Starting letter should not be comma(,) in "+Item.name, Item, true));	 */
				return (setError("Starting letter should not '"+NotAllowedChars[i]+"'  in "+Item.name, Item, true));
			}
	}
	return false;
}

//Check the Field is Numeric or not
function CheckNumeric(){
	Item = arguments[0];
	sign=arguments[1];
	type=arguments[2];
	if(sign=="POSITIVE" && Item.value<0)
		return(setError(Item.name + " sholud contain only Positive Numeric values", Item, true));
	else if(sign=="NEGATIVE" && Item.value>0)
		return(setError(Item.name + " sholud contain only Negative Numeric values", Item, true));
	else if(type=="INTEGER" && Item.value.indexOf('.')!=-1)
		return(setError(Item.name + " sholud contain only Whole Numbers", Item, true));
	else {
		if (isNaN(Item.value)){
			return(setError(Item.name + " sholud contain only Numeric values(0-9)", Item, true));
		}
		return false;
	}
}


//Set the error message
function setError(){
	if (arguments.length == 3){
		ErrMessage = arguments[0];
		ErrItem = arguments[1];
		return true;
	}
	return false;
}

// If check box corresponding to the terms and conditionsTerms and conditions
function CheckTerms()
{
	Item = arguments[0];	
	if(!(Item.checked) && (Item.type =="checkbox") && (Item.name == "terms"))
	{	
			ErrorMessage="Please agree to the Terms";
			DisplayError(ErrorMessage,Item);
			return true;
		}
}
			
//Display the given error message and place the control on the particular field.
function DisplayError(ErrorMessage, Item){
	alert(ErrorMessage);
	ErrMessage = "";
	ErrFound = false;
	Item.focus();
}

//This function for all the checking 
function CheckValidate(){
	Item = arguments[0];
	ErrFound = false;
	CheckMax = arguments.length;	
	if (arguments[CheckMax-2] == "UserDefined")	CheckMax = CheckMax -2 ;
	for(var i=1;i<CheckMax;){
		var IncArgVal = 1;
		TypeOfValidation = arguments[i];
		switch(TypeOfValidation){
			case "NULL" : if (CheckNull(Item)){ ErrFound = true;}
															break;
			case "NUMERIC" : if (CheckNumeric(Item,arguments[i+1],arguments[i+2])){ ErrFound = true;}break;
			case "EMAIL" :  CheckAllSpaces(Item);
					if (!CheckNull(Item) && CheckEmailId(Item)){ ErrFound = true;}break;
			case "LENGTH" : if(!CheckNull(Item) && CheckLength(Item,arguments[i+1],arguments[i+2])){
						ErrFound = true;}IncArgVal = 3;break;
			case "CHARS" : if(!CheckNull(Item) &&  CheckeachChar(Item,arguments[i+1])){
						ErrFound = true;}IncArgVal = 2;break;
			case "BEGINCHAR" : if (!CheckNull(Item) && CheckBeginingChars(Item,arguments[i+1])){
						ErrFound = true;}IncArgVal = 2;break;
			case "TERMS"     : if(CheckTerms(Item)){
								Errfound = true; return false;}// break;	
			
			case "PASSWORD" : if (arguments[i+1].value != Item.value){
						setError(
							
						
						"Confirm Password should match with the Password! Please Re-enter"
						
						,arguments[i+1],true);ErrFound = true;}
						IncArgVal = 2;break;
			
		}
		if (ErrFound) break;
		i += IncArgVal;
	}
	if (Item.type == "text" || Item.type == "password" || Item.type == "textarea" || Item.type == "file")
		if (!CheckNull(Item) && CheckAllSpaces(Item))
			ErrFound = true;
	if (ErrFound && arguments[arguments.length-1] == "No Alert"){
		return false;
	}
	if (ErrFound && arguments[arguments.length-2] != "UserDefined") {
		DisplayError(ErrMessage, ErrItem);
		return false;
	}
	else if (ErrFound && arguments[arguments.length-2] == "UserDefined") {

		DisplayError(ErrMessage.substring(0,ErrMessage.lastIndexOf(" ")) + " " + arguments[arguments.length-1], ErrItem);
		return false;
	}
	
	return true;
}

function getObj(name)
{
	if (document.all)
	{		
		return document.all(name);
	}
	else
	{
		return document.getElementById(name);
	}
}

function open_window(url,width,height,name)
{
	if (open_window.arguments[4] == 1)  
	{	
		remote=window.open(url,name,'status=no,scrollbars=yes,resizable=yes,top=100,left=100,height='+height+',width='+width);
	     remote.focus(); 
	}
	else
	{
		remote=window.open(url,name,'status=no,scrollbars=yes,resizable=no,top=100,left=100,height='+height+',width='+width);
		 remote.focus();
	}
	if (remote != null) 
	{	
		if (remote.opener == null)
			remote.opener = self;
		 
	}

}

function checkAll(val)
{
// alert(arguments[1]);
 if(arguments[1])
 {
  eval("objFrm = document." + arguments[1]+";");
 }
 else
 {
  objFrm = document.forms[1];
 }
 for(var i=0;i<objFrm.elements.length;i++)
 {
  if(objFrm.elements[i].type == "checkbox")
  {
   objFrm.elements[i].checked = val;
  }
 }
}





/*
function checkAll(val)
{
	for(var i=0;i<document.forms[0].elements.length;i++)
	{
		if(document.forms[0].elements[i].type == "checkbox")
		{
			document.forms[0].elements[i].checked = val;
		}
	}
}*/

function checkAllInForm(val,formName)
{
	var objForm = eval("document." + formName);
	for(var i=0;i<objForm.elements.length;i++)
	{
		if(objForm.elements[i].type == "checkbox")
		{
			objForm.elements[i].checked = val;
		}
	}
}

function embedMusic(source)
{
	document.write('<EMBED SRC="' + source + '" HIDDEN="true" AUTOSTART="true" >');
}

