function SelectAll(frm) {
 //alert(frm.selectall.checked);
   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()


function ValidDates(dd1, mm1, yyyy1, dd2, mm2, yyyy2, msg) {

 xFlag = 0;

 /*The Following Code has been commented by Ravi Julapalli
 if((DateValidation(dd1,mm1,yyyy1) == 0) && (DateValidation(dd2,mm2,yyyy2) == 0))*/

 // Start of Code Added by Ravi
 if((DateValidation(dd1,mm1,yyyy1,'null') == 0) || (DateValidation(dd2,mm2,yyyy2,'null') == 0))
	xFlag = 1;
 if(xFlag==1)
 {
   return 0
 }

 // End of Code Added by Ravi

	if(xFlag == 0) {
		var ddd1 = new Number(dd1.value) ;
		var mmm1 = new Number(mm1.value) - 1;
		var yyy1 = new Number(yyyy1.value);

		var ddd2 = new Number(dd2.value) ;
		var mmm2 = new Number(mm2.value) - 1;
		var yyy2 = new Number(yyyy2.value);

		var dObj1 = new Date(yyy1,mmm1,ddd1,0,0,0,0);
		var dObj2 = new Date(yyy2,mmm2,ddd2,0,0,0,0);

		if(dObj1 > dObj2) {
			alert(msg);
			dd1.focus();
			return 0;
		}
	}
	else 
		return 1;

} // closing the function ValidDates()


function dval(yyy,mmm,ddd) {

  var dObj = new Date(yyy,mmm,ddd,0,0,0,0);

  var dd = dObj.getDate();
  var mm = dObj.getMonth();
  var yy = dObj.getFullYear();

  if((dd == ddd) && (yy == yyy) && (mm == mmm)) {
	return true;
  }  
  else {
	return false;
  }

}


function DateValidation(dd, mm, yy, msg) {

 if(NumValidation(dd,'Date','','num') == 0)
 return 0;

 if(NumValidation(mm,'Month','','num') == 0)
 return 0;

 if(NumValidation(yy,'Year','','num') == 0)
 return 0;



 d = parseInt(dd.value);
 m = parseInt(mm.value);
 y = parseInt(yy.value);

 if(m > 12 || m == 0) {
	alert("Invalid month selected for " + msg);
	mm.focus();
	return 0;
 }
 else {

 var vDays = [ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
 var flag = 0;
 if(m == 2) {
	if(isLeapYear(y)) {
	  if( d > 29 || d < 1 ) {
	   flag = 0;
	  }
	  else {
	   flag = 1;
	  }
	}
	else if( d > vDays[m] || d < 1 ) {
	 flag = 0;
	}
	else {
		 flag = 1;
	}
 }
 else {
	if( d > vDays[m] || d < 1 ) {
	 flag = 0;
	}
	else {
	 flag = 1;
	}  
 }
 }
 if(flag == 0) {

	alert("Invalid day selected for " + msg);

	dd.focus();
	return 0;
 }
 else {
	return 1;
 }
}


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;
 }
} 


function PassValidation(Element1,Element2) {

	if(Element1.value != Element2.value) {
		alert("Retype Password doesn't match");
		Element2.focus();
		return 0;
	}
	else
		return 1;

} 


function SelectValidation(Element,Message) {
	if(Element.value == "0") {
		alert("Please select "+Message+" from the list");
		Element.focus();
		return 0;
	}

}


function EmailValidation(element, msg)
{
	if(element.value != "")
	{
		var reg = /^[-a-zA-Z0-9!#$%&'*+\/=?^_`{|}~]+(\.[-a-zA-Z0-9!#$%&'*+\/=?^_`{|}~]+)*@(([a-zA-Z0-9]([-a-zA-Z0-9]*[a-zA-Z0-9]+)?){1,63}\.)+([a-zA-Z0-9]([-a-zA-Z0-9]*[a-zA-Z0-9]+)?){2,63}$/;
		if(!reg.test( element.value ))
		{
			alert("Please enter valid "+msg);
			element.focus();	
			return false;
		}
		else
		{
			return true;		
		}
	}
	else
	{
		alert("Please enter "+msg);
		element.focus();	
		return false;
	}	
}


function NumValidation(Element, MessageLen0, spl, OnlyNum)
{
	if(MessageLen0.length != 0)
	{
		if(isBlank(Element.value) || Element.value.length == 0)
		{
			alert("Please enter the "+ MessageLen0);
			Element.focus();
			return 0;
		}
	}

	if(OnlyNum == "num")
	{
		if(isNaN(Element.value))
		{
			alert("Please enter only Numeric Data");
			Element.focus();
			return 0;
		}
		if(parseInt(Element.value) < 0)
		{
			alert("Negative values are not allowed for this field.");
			Element.focus();
			return 0;
		}
	}

	if(spl == "spl" && OnlyNum != "num")
	{
		if(SplNumbers(Element) == 0)
		return 0;
	}	


} 


function GenValidation(Element,MessageLen0,MessageLen4,spl) {

	if(MessageLen0.length != 0)
	{
		if(Element.value.length == 0)
		{
			alert("Please enter the "+ MessageLen0);
			Element.focus();
			return 0;
		}
		else if(isBlank(Element.value))
		{
			alert("Please enter the "+ MessageLen0);
			Element.focus();
			return 0;
		}
	}

	if(MessageLen4.length != 0)
	{
		if(Element.value.length < 4)
		{
			alert( MessageLen4 + " should be more than 4 characters");
			Element.focus();
			return 0;
		} // closing the if - else condtion for if(MessageLen4.length != 0)
	}

	if(spl == "spl")
	{
		if(SplCharacters(Element) == 0)
		return 0;
	}
	else if(spl == "space")
	{
		if(SplCharactersSpace(Element) == 0)
		return 0;
	}
} 


function SplCharacters(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][ _ ]");
			Val.focus();
			return 0;
		}
	} // closing the for loop

} 


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 0;
		}
	} // closing the for loop
} 


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 0;
		}
	} // closing the for loop

} 


//To check if trim(value) is blank
function isBlank(txt, minlen)
{
	/*
		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
	*/

	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;
	}	
}

//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;
}


function getSelectedIndex(radgroup)
{
	/* Returns the id of selected radio button in a radio button group  */
	var j = -1;

	for( i=0; i < radgroup.length; i++ ){
		if( radgroup[i].checked ){
			j = i;
		}
	}

	return j;
}


function TextareaValidation(elem,msg,len) {

	   if(elem.value.length > 0)
	   {
		if(isBlank(elem.value)) 
		{
			alert("Please enter the value");
			elem.focus();
			return 0;
		}else if(elem.value.length > len) 
		{
			alert(msg+" should not exceed "+len+" characters");
			elem.focus();
			return 0;
		}	
	   }

} 


function checkInCharSet(txt, charset)
{
	/*
		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( "jack", "aeiouAEIOU" ) this fucntion returns false as "jack" contains 'j', 'c' and 'k'
						whcih are not part of "aeiouAEIOU".

						checkInCharSet( "abC", "abcdefABCDEF" ) this statement returns true as all "abC" contains characters
						that are present in "abcdefABCDEF"
	*/

	var b = true;

	for(i = 0; i < txt.length; i++ )
	{
		if( charset.indexOf(txt.charAt(i)) == -1 )
		{
			b = false;
		}
	}

	return b;
}


function isValidDate(dd, mm, yy)
{
	/*
		This fucntion can be used for date validations.

		INPUT:	Day in numeric format [d]
					Month in numeric format [m]
					4 digit year [y]

		OUTPUT: Returns true if the date is valid else false.

		USAGE:
					isValidDate( 1, 4, 2001 )	- Returns true

					isValidDate( 1, 13, 2002 )	- Returns false coz month is > 12

					isValidDate( 30, 2, 2001)	- Returns false coz Feb will never have 30th
	*/

	var d = parseInt(dd);
	var m = parseInt(mm);
	var y = parseInt(yy);

	if( isNaN(d) || isNaN(m) || isNaN(y) )
		return false;

	if( d <= 0 || m <= 0 || y <=0 )
		return false;

	if( d > 31 || m > 12 )
		return false;

	if( y < 1000 || y > 9999 )
		return false;

	var vDays = [ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];

	if( m == 2 )
	{
		if( isLeapYear(y) )
		{
			if( d > 29 || d < 1 )
			{
				return false;
			}
			else
			{
				return true;
			}
		}
		else if( d > vDays[m] || d < 1 )
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	else if( d > vDays[m] || d < 1 )
	{
		return false;
	}
	else
	{
		return true;
	}
}

function showPopUpCenter(filename,winName,winWidth,winHeight)
{
	attributes = "scrollbars=yes,status=yes,width=" + winWidth + ",height=" + winHeight + ",left=" + (screen.width-winWidth)/2 + ",top=" + (screen.height-winHeight)/2;
	var childwin = window.open(filename,winName,attributes);
	childwin.focus();
}

function showStatus(vStatus)
{
	window.status = vStatus;
}

function whatIsThis()
{
		alert("Are you not sure what this option/feature is all about?\r\n\r\nWrite to Guru immediately.");
}
//function for ltrim,rtrim and trim both(ltrim and rtrim)
function Trim(str)
{
  return RTrim(LTrim(str));
}
function RTrim(str)
{
  var whitespace = new String(' \t\n\r');
  var s = new String(str);
  if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
	// We have a string with trailing blank(s)...
	var i = s.length - 1;       // Get length of string
	// Iterate from the far right of string until we
	// don't have any more whitespace...
	while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
	  i--;
	// Get the substring from the front of the string to
	// where the last non-whitespace character is...
	s = s.substring(0, i+1);
  }
  return s;
}
function LTrim(str)
{
  var whitespace = new String(' \t\n\r');
  var s = new String(str);
  if (whitespace.indexOf(s.charAt(0)) != -1) {
	// We have a string with leading blank(s)...
	var j=0, i = s.length;
	// Iterate from the far left of string until we
	// don't have any more whitespace...
	while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
	j++;
	// Get the substring from the first non-whitespace
	// character to the end of the string...
	s = s.substring(j, i);
  }
  return s;
}
