/****************************************************************************
* Description	:	This is the common javascript file
*			which has different functions with their
*			definitions.
*
* Author	:  V S Prasad Vasamsetty
* Date	: 13th April 2009
*
* Revision	:
* Modified By	: xxxxxx
* Modified Date: xxxxxx
* Reason	: xxxxxx
* USAGE:
*	element/fieldname – name of the control like frm.password.
*	Msg – Field Name that we want to display in alert message.
****************************************************************************/

/************************ To validate name *********************/
//function to chk whether given text field is empty or not
function isValidName(element,msg) 
{  
  var re = new RegExp("^[a-zA-Z0-9]+([a-zA-Z0-9])$");		
  var m = re.exec(element.value);
  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;
	}
	else if(element.value.length == 1)
	{
		alert("Please recheck the number of characters entered in "+msg);
		element.focus();
		return false;
	}
	else if(m==null)
	{
		alert("Invalid entry in "+msg);
		element.focus();
		return false;
	}				
	return true;	
} // closing the function isValidName()


/*
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;
}


/*
IS BLANK
To check if trim(value) is blank
Usage:
	This function 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;
}
/************************ To validate name *********************/

/************************ To validate Numeric field *********************/
function isValidNumber(element,msg)
{
	if(element.value.length == 0)
	{
		alert("Please Enter "+msg);
		element.focus();
		return false;
	}///if 
	else
	{
		///var regnum = new RegExp("^\+[0-9]+$");
		var regnum = new RegExp(/^[+0-9][0-9\s-.]+$/);

		if(!(element.value.match(regnum)))
		{
			alert("Invalid Entry in "+msg);
			element.focus();
			return false;
		}			
	}/// else closed
	return true;
} //isValidNumber() closed
/************************ To validate Numeric field *********************/

/************************ To validate input text *********************/
function isEmpty(element,msg)
{
	if(element.value.length == 0)
	{
		alert("Please Enter the "+ msg);
		element.focus();
		return false;
	}//// if
	else
	{
		var regname = /[\+\-\.\*\!\@\#\$\%\^\&\(\)\|]+/;
		if(element.value.match(regname) != null)
		{
			alert("Invalid Entry in "+msg);
			element.focus();
			return false;
		}
	}/// else
	return true;
}//// function IsEmpty() closed
/************************ To validate input text *********************/


/************************ To validate Email *********************/
function isValidEmail(element,msg)
{
	if(element.value.length==0 || element.value=='Email')
	{
		alert("Please Enter the "+msg);
		element.focus();
		return false;
	}///if 
	else
	{
		var regemail = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
		if(!element.value.match(regemail))
		{
			alert("Invalid "+msg);
			element.focus();
			return false;
		}		
	}//// else
   return true;
}// isValidEmail() closed
/************************ To validate Email *********************/

/************************ To validate Select field *********************/
function isValidSelect(element,msg) 
{
	if(element.value == "-1" || element.value == "" || element.value == 0) 
	{
		alert("Please select "+msg+" from the list");
		element.focus();
		return false;
	}
	return true;
}
/************************ To validate Select field *********************/

//function to chk whether given text field is empty or not
function isValidEntry(element,msg) 
{  
    if(element.value.length == 0)
	{
		alert("Please enter the "+ msg);
		element.focus();
		return false;
	}	
	return true;	
} // closing the function isValidEntry()

//function to verify entered password and confirm password fields
function isMatchedNames(element1,element2) 
{
	if(element1.value == element2.value)
	{
		alert("First Name and Last Name are matching, recheck once");
		element2.focus();
		return false;
	}
	else
		return true;
}