/*
1. PHONE NUMBER VALIDATION
Usage: 
Element  name of the control, like frm.phone
Message  Field Name that we want to display in alert message.
Required  Set this to yes if the field is mandatory, otherwise no.

 if(!PhoneValidation(frm.phone,'Phone Number','yes'))
 return;
*/
function PhoneValidation(element, msg, required)
{	
	var VarPhone = element.value;
	if (VarPhone== "")
	{
		if (required.toLowerCase() == "yes" || required == 1)
		{
			alert("Please enter "+msg);
			element.focus();
			return false;
		}
	}
	
	if (VarPhone != "")
	{
		var Phno;
		Phno=VarPhone;
		var valid = "+ 0123456789-() ";
		var hyphencount = 0;
		for (var i=0; i < Phno.length; i++) 
		{
			temp = "" + Phno.substring(i, i+1);
			if (valid.indexOf(temp) == "-1")
			{
				alert("Invalid characters in your "+msg+". Please try again.");
				element.focus();
				return false;
			}
		}
		
		if(VarPhone.length < 2) {
		
			alert("Please Enter a Valid Number. Please try again.");
			element.focus();
			return false;
		}
		
		if(VarPhone.length > 20) {
		
			alert("Please Enter a Valid Number. Please try again.");
			element.focus();
			return false;
		}
     } 
	
	 return true;      
}

/**
	 FUNCTION PASSVALIDATION(element1,element2) 
	 **/
	
	function PassValidation(Element1,Element2) {
	
		if(Element1.value != Element2.value) {
			alert("Retype Password doesn't match");
			Element2.focus();
			return 0;
		}
		else
			return 1;
		
	} // closing the function PassValidation()
	

/*
2. NUMBER VALIDATION
Usage:
Element  name of the control, like frm.number
Message  Field Name that we want to display in alert message.
Required  Set this to yes if the field is mandatory, otherwise no.

 if(!NumberValidation(frm.num,'Roll Number','yes'))
 return;
*/
function NumberValidation5(element, msg, required)
{  
	var VarNumber = element.value;
	if(VarNumber == "")
	{
		if (required.toLowerCase() == "yes" || required == 1)
		{
			alert("Please enter "+msg);
			element.focus();
			return false;
		}
	}
	if (VarNumber != "")
	{
		var Num;
		Num=VarNumber;
		var valid = "0123456789";
		var hyphencount = 0;
		if (Num.length!=3) 
		{
			alert("SSN code length should be 3 ");
			element.focus();
			return false;
		}
		for (var i=0; i < Num.length; i++) 
		{
			temp = "" + Num.substring(i, i+1);
			if (temp == "-") hyphencount++;
			if (valid.indexOf(temp) == "-1")
			{
				alert("Invalid characters in your numbers.  Please try again.");
				element.focus();
				return false;
			}
		}
		
    }   // end if
	
    return true; 
}  // end function
function NumberValidation1(element, msg, required)
{  
	var VarNum = element.value;
	if(VarNum == "")
	{
		if (required.toLowerCase() == "yes" || required == 1)
		{
			alert("Please enter "+msg);
			element.focus();
			return false;
		}
	}
	if (VarNum != "")
	{
		var Nu;
		Nu=VarNum;
		var valid = "0123456789";
		var hyphencount = 0;
		if (Nu.length!=2) 
		{
			alert("SSN code length should be 2 ");
			element.focus();
			return false;
		}
		for (var i=0; i < Nu.length; i++) 
		{
			temp = "" + Nu.substring(i, i+1);
			if (temp == "-") hyphencount++;
			if (valid.indexOf(temp) == "-1")
			{
				alert("Invalid characters in your numbers.  Please try again.");
				element.focus();
				return false;
			}
		}
		
    }   // end if
	
    return true; 
}  // end function
function NumberValidation2(element, msg, required)
{  
	var VarNumb = element.value;
	if(VarNumb == "")
	{
		if (required.toLowerCase() == "yes" || required == 1)
		{
			alert("Please enter "+msg);
			element.focus();
			return false;
		}
	}
	if (VarNumb != "")
	{
		var Nume;
		Nume=VarNumb;
		var valid = "0123456789";
		var hyphencount = 0;
		if (Nume.length!=4) 
		{
			alert("SSN code length should be 4 ");
			element.focus();
			return false;
		}
		for (var i=0; i < Nume.length; i++) 
		{
			temp = "" + Nume.substring(i, i+1);
			if (temp == "-") hyphencount++;
			if (valid.indexOf(temp) == "-1")
			{
				alert("Invalid characters in your number.  Please try again.");
				element.focus();
				return false;
			}
		}
		
    }   // end if
	
    return true; 
}  // end function
/*
3. EMAIL ADDRESS VALIDATION

Usage: 
Element  name of the control, like frm.email
Required  Set this to yes if the field is mandatory, otherwise no.

	 if(!EmailValidation(frm.email,'yes'))
	 return;
*/

function EmailValidation(element, required)
{
	var VarEmail = element.value;
	if(VarEmail == "")
	{
		if (required.toLowerCase() == "yes" || required == 1)
		{
			alert("Please enter Email Address");
			element.focus();
			return false;
		}
	}	
	if(VarEmail != "")
	{

		if(chkEmail(VarEmail) == false)
		{
			 alert("Email address seems to be incorrect (check @ and .'s)");
			 element.focus();
			 return false;
		}
		/*
		var emailStr = VarEmail;
		 
		var emailPat=/^(.+)@(.+)$/
		var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
		var validChars="\[^\\s" + specialChars + "\]"
		var firstChars=validChars
		var quotedUser="(\"[^\"]*\")"
		var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
		var atom="(" + firstChars + validChars + "*" + ")"
		var word="(" + atom + "|" + quotedUser + ")"
		var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
		var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
		var matchArray=emailStr.match(emailPat)
		if (matchArray==null) 
		{
			 alert("Email address seems to be incorrect (check @ and .'s)");
			 element.focus();
			 return false;
		}
		var user=matchArray[1]
		var domain=matchArray[2]
		if (user.match(userPat)==null) 
		{
			alert("The username doesn't seem to be valid.");
			element.focus();
			return false;
		}
		var IPArray=domain.match(ipDomainPat)
		if (IPArray!=null) 
		{
			for (var i=1;i<=4;i++) 
			{
				if (IPArray[i]>255) 
				{
					 alert("Destination IP address is invalid!");
					 element.focus();
					 return false;
				}
			}
		}
		var domainArray=domain.match(domainPat)
		if (domainArray==null) 
		{
			alert("The domain name doesn't seem to be valid.");
			element.focus();
			return false;
		}
		var atomPat=new RegExp(atom,"g");
		var domArr=domain.match(atomPat);
		var len=domArr.length;
		if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3) 
		{
		   alert("The address must end in a three-letter domain, or two letter country.");
		   element.focus();
		   return false;
		}
		if (domArr[domArr.length-1].length==2 && len<3) 
		{
			var errStr = "This address ends in two characters, which is a country";
			errStr    += " code.  Country codes must be preceded by ";
			errStr	  += "a hostname and category (like com, co, pub, pu, etc.)";
			alert(errStr);
			element.focus();
			return false;
		}
		if (domArr[domArr.length-1].length==3 && len<2) 
		{
			 var errStr="This address is missing a hostname!";
			 alert(errStr);
			 element.focus();
			 return false;
		}*/
	}
	return true;
}

/* 
4. ZIPCODE/PINCODE  VALIDATION

Usage:
	Element  name of the control, like frm.zipcode
	Required  Set this to yes if the field is mandatory, otherwise no.

	if(!ZipcodeValidation(frm.zip,'yes'))
	return;
*/
function ZipcodeValidation(element,required) 
{
	var valid = "0123456789-";
	var hyphencount = 0;
	var field = element.value;
	var str   = required;
	if(field == "")
	{
		if (required.toLowerCase() == "yes" || required == 1)
		{
			alert("Please enter ZipCode");
			element.focus();
			return false;
		}
	}	
	if (field != "")
	{
		if (field.length!=5) 
		{
			alert("Zip code length should be 5 ");
			element.focus();
			return false;
		}
		for (var i=0; i < field.length; i++) 
		{
			temp = "" + field.substring(i, i+1);
			if (temp == "-") hyphencount++;
			if (valid.indexOf(temp) == "-1")
			{
				alert("Invalid characters in your zip code.  Please try again.");
				element.focus();
				return false;
			}
		}
	}
	return true;
}

/*
5. ALPHA VALIDATION
//to find if the field contains alphabets only
usage:
Element  name of the control, like frm.firstname
Message  Field Name that we want to display in alert message.
Required  Set this to yes if the field is mandatory, otherwise no.

 if(!AlphaValidation(frm.firstname,'First Name','yes'))
 return;
*/

function AlphaValidation(element, msg, required)
{
	var i=0;
	var ValidData="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz. ";
	var Data=element.value;
	if(element.value == "")
	{
		if (required.toLowerCase() == "yes" || required == 1)
		{
			alert("Please enter "+msg);
			element.focus();
			return false;
		}
	}	
	if(element.value != "")
	{
		for(i=0;i<Data.length;i++)
		{
			if(ValidData.indexOf(Data.charAt(i))==-1)
			{
				alert("Please enter alphabetic Data Only");
				element.focus();
				return false;
			}
		}
	}
    return true;
}
/* alphabetic validation without any space*/
function AlphaValidationnospace(element, msg, required)
{
	var i=0;
	var ValidData="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	var Data=element.value;
	if(element.value == "")
	{
		if (required.toLowerCase() == "yes" || required == 1)
		{
			alert("Please enter "+msg);
			element.focus();
			return false;
		}
	}	
	if(element.value != "")
	{
		for(i=0;i<Data.length;i++)
		{
			if(ValidData.indexOf(Data.charAt(i))==-1)
			{
				alert("Please enter alphabetic Data Only");
				element.focus();
				return false;
			}
		}
	}
    return true;
}

/*
6. ALPHANUMERIC VALIDATION
//to check if the field contains both alphabets and numbers
usage:
Element  name of the control, like frm.firstname
Message  Field Name that we want to display in alert message.
Required  Set this to yes if the field is mandatory, otherwise no.

 if(!AlphaNumericValidation(frm.firstname,'First Name','yes'))
 return;
*/

function AlphaNumericValidation(element, msg, required)
{
	var i=0;
	var ValidData=". ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890/_-() ";
	var Data=element.value;
	
	if(element.value == "")
	{
		if (required.toLowerCase() == "yes" || required == 1)
		{
			alert("Please enter "+msg);
			element.focus();
			return false;
		}
	}	
	if(element.value != "")
	{
		for(i=0;i<Data.length;i++)
		{
			if(ValidData.indexOf(Data.charAt(i))==-1)
			{
				alert("Invalid entry.\t\t");
				element.focus();
				return false;
			}
		}
	}
	return true;
}
/* alpha numaric validation for postal code*/
function AlphaNumericValidationPostal(element, msg, required)
{
	var i=0;
	var ValidData=". ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890/_- ";
	var Data=element.value;
	
	if(element.value == "")
	{
		if (required.toLowerCase() == "yes" || required == 1)
		{
			alert("Please enter "+msg);
			element.focus();
			return false;
		}
	}	
	if(element.value != "")
	{
		for(i=0;i<Data.length;i++)
		{
			if(ValidData.indexOf(Data.charAt(i))==-1)
			{
				alert("Invalid entry.\t\t");
				element.focus();
				return false;
			}
		}
	}
	return true;
}



/* alpha numari validation without anny chars*/
function AlphaNumericValidationwChars(element, msg, required)
{
	var i=0;
	var ValidData="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
	var Data=element.value;
	
	if(element.value == "")
	{
		if (required.toLowerCase() == "yes" || required == 1)
		{
			alert("Please enter "+msg);
			element.focus();
			return false;
		}
	}	
	if(element.value != "")
	{
		for(i=0;i<Data.length;i++)
		{
			if(ValidData.indexOf(Data.charAt(i))==-1)
			{
				alert("Invalid entry.\t\t");
				element.focus();
				return false;
			}
		}
	}
	return true;
}

/*
7. URL VALIDATION

Usage:
Element  name of the control, like frm.url
Message  Field Name that we want to display in alert message.
Required  Set this to yes if the field is mandatory, otherwise no.

	 if(!isValidURL(frm.url, "URL", "yes")) 
	 return;
*/
function isValidURL(element, msg, required)
{
	if(element.value == "")
	{
		if (required.toLowerCase() == "yes" || required == 1)
		{
			alert("Please enter "+msg);
			element.focus();
			return false;
		}
	}
	if(element.value != "")
	{
		var oRegExp = /[^:]+:\/\/[^:\/]+(:[0-9]+)?\/?.*/;
		if (!oRegExp.test(element.value))
		{
			alert('\r\n The URL you have entered is invalid.\n Please check it for accuracy.\n Example: http://www.abc.com');
			element.focus();
			element.select();
			return false;
		}
	}
	return true;
}

/*
8. DATE VALIDATION

Usage:
Element  name of the control, like frm.date
Message  Field Name that we want to display in alert message.
Required  Set this to yes if the field is mandatory, otherwise no.

Valid date formats are MM-DD-YYYY, MM/DD/YYYY and MM.DD.YYYY  

	 if(!DateValidation(frm.startdate, "Started Date", "yes")) 
	 return;
*/
function DateValidation(element, msg, required) 
{
	var dateStr = element.value;
	var valid = "0123456789-/.";
	var temp = "";
	var hyphencount = 0 ;
	var slashcount = 0 ;
	var dotcount = 0 ;
	if(element.value == "")
	{
		if (required.toLowerCase() == "yes" || required == 1)
		{
			alert("Please enter "+msg);
			element.focus();
			return false;
		}
	}
	if(element.value != "")
	{
		for (var i=0; i < dateStr.length; i++)
		{
			temp = "" + dateStr.substring(i, i+1);
			 if  (temp == "-")  { hyphencount++; }
				else  if (temp == "/")  {  slashcount++; }
				   else  if (temp == ".") { dotcount++; }
			if ( ( hyphencount == 2 ) || ( slashcount == 2 ) || ( dotcount == 2 ) )
			{
				t=dateStr.substring(6,10);
				if(t.length==4)
				{
					if(t<1900)
					 {
						alert("the century of date should be greater than or equal to 1900.");
						element.focus();
						return false;
					 }
				}
			}          
			if (valid.indexOf(temp) == "-1")
			{
				alert("Invalid characters in your date.  Please try again.");
				element.focus();
				return false;
			}
		} //end of for loop   
		//alert ( "hyphen  " + hyphencount + " slash " + slashcount + " dot " + dotcount )  ; 
		//if ( ( hyphencount== 2) & (slashcount !=2) && (dotcount !=2) ) {}
		//else if ( ( hyphencount!= 2) & (slashcount ==2) && (dotcount !=2) ) {}
		//else if ( ( hyphencount!= 2) & (slashcount !=2) && (dotcount ==2) ) {}
		//else 
		//{	
			//alert("The hyphen character should be used with a properly formatted. Please try again.");
			//element.focus();
			//return false;
		//}
		   
		
			
			if (month < 1 || month > 12) 
			{ // check month range
				alert("Month must be between 1 and 12.");
				element.focus();
				return false; 
			}
			if (day < 1 || day > 31) 
			{
				alert("Day must be between 1 and 31.");
				element.focus();
				return false; 
			}
			if ((month==4 || month==6 || month==9 || month==11) && day==31) 
			{
				alert("Month "+month+" doesn't have 31 days!")
				element.focus();
				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!");
					element.focus();
					return false;
				}
			} // end of month = 2 condition
		} // end of date string length = 8 or length = 10 condition
		else 
		{
		   alert("Entered date is invalid format, Date should be 8 to 10 chars. length.");
		   element.focus();
		   return false;
		} 
	
	return true;  // date is valid
}

/*
9. SELETC VALIDATION

Usage:
Element  name of the control, like frm.firstname
Message  Field Name that we want to display in alert message.

if(!SelectValidation(frm.country,'Country'))
return;
*/
function SelectValidation(element,msg) 
{
	if(element.value == "0") 
	{
		alert("Please select a "+msg+" ");
		element.focus();
		return false;
	}
	return true;
}

//if the select value is null 
function SelectValidationNull(element,msg) 
{
	if(element.value == "") 
	{
		alert("Please select a "+msg+" ");
		element.focus();
		return false;
	}
	return true;
}

/*
10. FUNTION SELECTALL CHECK BOXES

Usage:
frm  name of the form

SelectAll(frm);
*/

function SelectAll(frm) 
{
   if(frm.selectall.checked == true) 
   {
	 for(i=0;i<frm.elements.length;i++) 
	 {
	   if((frm.elements[i].type == "checkbox") && (frm.elements[i].name != "selectall")) 
	   {
		 frm.elements[i].checked = true;
	   } // if statement
	 } // for loop
   }
   else if(frm.selectall.checked == false) 
   {
	  for(i=0;i<frm.elements.length;i++) 
	  {
		 if((frm.elements[i].type == "checkbox") && (frm.elements[i].name != "selectall")) 
		 {
		   frm.elements[i].checked = false;
		 } // if statement
	  } // for loop
   } // if - else - if condition
} // closing the function SelectAll()

/*
11. IS LEAP YEAR

Usage:  Y  year
If the given year is a leap year, this function will return true. otherwise false.
isLeapYear('2004');

*/
function isLeapYear(y) 
{
	if( y % 4 == 0) 
	{
		if( y % 100 == 0 ) 
		{
			if( y % 400 == 0) 
			{
				return true;
			}
			else 
			{
				return false;
			}
		}
		else 
		{
			return true;
		}
	}
	else 
	{
		return false;
	}
} // closing the function isLeapYear()

/*
12. CONFIRM PASSWORD

Usage:
Element1 name of the first control like frm.password.
Element2 name of the second control like frm.confirmpassword.

if(!ConfirmPassValidation(frm.pwd, frm.conpwd))
return;
*/
function ConfirmPassValidation(element1,element2) 
{
	if(element1.value != element2.value)
	{
		alert("Retype Password doesn't match");
		element2.focus();
		return false;
	}
	else
		return true;
} // closing the function PassValidation()

/*
13. GET COUNT OF

Usage:
This can be used for any character validation.
For example in a valid date the count of - or / should not be more than 2
Likewise in a valid numer there should be only one.

*/
function getCountOf(vChr, txt)
{
	var i = 0;
	var iCount = 0;
	for( i=0; i < txt.length; i++ )
	{
		if( txt.charAt(i) == vChr )
		{
			iCount++;
		}
	}
	return iCount;
}

/*
14. IS BLANK
To check if trim(value) is blank
Usage:
	This fucntion can be used to check if a given text contains only spaces or 0 in length.

	INPUT: Text [txt]
				Minimum Length [minlen] optional
				Indicates that the text should be atleast 'minlen' in length

	OUTPUT: returns true if blank else false

*/	
function isBlank(txt, minlen)
{
	if( txt.length == getCountOf('\n', txt) )
	{
		// This condition avoids the entry of just newlines in text areas.
		return true;
	}
	if( txt.length == getCountOf(' ', txt) || txt.length == 0 )
	{
		return true;
	}
	else if( minlen > 0 )
	{
		if( txt.length < minlen )
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		return false;
	}
	return true;
}

/*
15. INCLUDE SPECIAL CHARACTER

In some cases, we want to validate whether a given value contains at 
least one special character or not. Below mentioned function will be useful 
for such situations.

Usage:
	Element  name of the control like frm.password.
	Message  Field Name that we want to display in alert message.

if(!IncludeSpecialchar(frm.pwd, "Password"))
return;
*/
function IncludeSpecialchar(element, msg)
{
	var alp = '!@#$%&*_1234567890';
	var count = 0;
	for (var i=0;i<element.value.length;i++)
	{
		temp=element.value.substring(i,i+1);
		if (alp.indexOf(temp)!= -1)
		{
			count = 1;
		}
	} // closing the for loop
	if(count == 0)
	{
	  alert(msg+" should contain atleast one special character (!,@,#,$,%,&,*, _  and numbers can be used).");
	  element.focus();
	  return false;
	}
	return true;
}
 
/*
16. GENERAL VALIDATION

Usage:
	Element  name of the control like frm.password.
	Message  Field Name that we want to display in alert message.

if(!GenValidation(frm.name, "Name"))
return;
*/
function GenValidation(element,msg) 
{
	if(element.value.length == 0)	
	{
		alert("Please enter the "+ msg);
		element.focus();
		return false;
	}
	else if(isBlank(element.value))
	{
		alert("Please enter the "+ msg);
		element.focus();
		return false;
	}
	return true;
} // closing the function GenValidation()

/*
17.  FUNCTION SPLCHARACTERS(element)
No speaicl Characters
This function will not accept any special characters.
Valid entries for this function are [a-z][A-Z][0-9][ _ ].
Usage:
	Element  name of the control like frm.password.

if(!SplCharacters(frm.name))
return;
*/

function SplCharacters(element) 
{
	var alp = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
	for (var i=0;i<element.value.length;i++)
	{
		temp=element.value.substring(i,i+1);
		if (alp.indexOf(temp)==-1)
		{
			alert("No special characters\r\nValid entries are [a-z][A-Z][0-9][ _ ]");
			element.focus();
			return false;
		}
	} // closing the for loop
	 return true;
} // closing the function SplCharacters()

/*
18. SplCharactersSpace
Valid entries for this function are [a-z][A-Z][0-9][ space ].
Usage:
	Val  specify the value which we are going to validate.

if(!SplCharactersSpace(frm.name.value))
return;
*/	
function SplCharactersSpace(Val)
{
	var alp = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ";

	for (var i=0;i<Val.value.length;i++){
		temp=Val.value.substring(i,i+1);
		if (alp.indexOf(temp)==-1){
			alert("No special characters \nValid entries are [a-z][A-Z][0-9][ space ]");
			Val.focus();
			return false;
		}
	} // closing the for loop
	return true;
} // closing the function SplCharactersSpace()

/*

19. FUNCTION SPLNUMBERS(element) 

if(!SplNumbers(frm.num.value))
return;
*/
	
function SplNumbers(Val)
{
	var alp = "0123456789+-";
	for (var i=0;i<Val.value.length;i++)
	{
		temp=Val.value.substring(i,i+1);
		if (alp.indexOf(temp)==-1)
		{
			alert("No special characters \nValid entries are [0-9][ + - ]");
			Val.focus();
			return false;
		}
	} // closing the for loop
	return true;
} // closing the function SplNumbers()

/*
20.
	This function checks if the characters in a given text are part of a given character set.

	INPUT:	Text ti be verified [txt]
				String of character that forms the reference [charset]

	OUTPUT: Returns true if all of the characters in txt are part of charset, else false.

	USAGE:
	for example:

		checkInCharSet( "guru", "aeiouAEIOU" ) this fucntion returns false as "guru" contains 'g' and 'r'
		whcih are not part of "aeiouAEIOU".

		checkInCharSet( "abC", "abcdefABCDEF" ) this statement returns true as all "abC" contains characters
		that are present in "abcdefABCDEF"
*/
function checkInCharSet(txt, charset)
{
	var b = true;
	for(i = 0; i < txt.length; i++ )
	{
		if( charset.indexOf(txt.charAt(i)) == -1 )
		{
			b = false;
		}
	}
	return b;
}

/*
21. GET SELECTED INDEX
This can used while validating radio button groups. 
If none of the buttons is selected then the function	
returns -1 else the id.

E.g: frm is the name of a form and radSearchType is the radiobutton group name.

if( getSelectedIndex(frm.radSearchType) == -1 )
{
	alert("Please select search type." );
	frm.radSearchType[0].focus();
	return;
}

*/
function getSelectedIndex(radgroup)
{
	/* Returns back the id of selected radio button in a radio button group  */
	var j = -1;
	
	if(radgroup.length)   
	{
		for( i=0; i < radgroup.length; i++ )
		{
			if(radgroup[i].checked)
			{
				j = i;
			}
		}
	}
	return j;
}

/*
22. TextareaValidation(elem,msg,len)
This function can be used to validate the length of Text area's in forms.
For example...if the value of text area should not exceed 500 characters.

Arguments :
elem : The element(TextArea)
msg : Message to be alerted
	  For example "Description"
len : Noof characters not to be exceeded

E.g: frm is the name of a form and desc is a text area name.

Usage in form: 
if(!TextareaValidation(frm.desc,'Description',500))
return;
*/

function TextareaValidation(elem,msg,len) 
{
	if(elem.value.length > 0)
	{
		if(isBlank(elem.value)) 
		{
			alert("Please enter the value");
			elem.focus();
			return false;
		}
		else if(elem.value.length > len) 
		{
			alert(msg+" should not exceed "+len+" characters");
			elem.focus();
			return false;
		}	
	}
	return true;
} // closing the function TextareaValidation()

function NumberValidation(element, msg, required)
{  
	var VarNumber = element.value;
	if(VarNumber == "")
	{
		if (required.toLowerCase() == "yes" || required == 1)
		{
			alert("Please enter "+msg);
			element.focus();
			return false;
		}
	}
	if (VarNumber != "")
	{
		var Num;
		Num=VarNumber;
		var valid = "0123456789";
		var hyphencount = 0;
		for (var i=0; i < Num.length; i++) 
		{
			temp = "" + Num.substring(i, i+1);
			if (valid.indexOf(temp) == "-1")
			{
			  alert("Invalid characters in your "+msg+".  Please try again.");
			  element.focus();
			  return false;
			}
	   } // end for loop
    }   // end if
    return true; 
}  // end function	
