/* ======================================================================
	Author				: Mahendra Patil 
	version				: 
	created Date		: 1/7/2003
	Last Modified By	: 
	Last Modified Date  : 
	Explanation			: This Script is used for validations 
	========================================================================== */






/* ======================================================================
	Function	: checkEmpty
	Input		: obj	  : any form object 				  
	Retrieval	: boolean : If object contains no data then returns false else returns true
	Explanation : Used to check whether object contains data or not (i.e blank field)
	========================================================================== */
function checkEmpty(obj)
{
	if(obj.value == "")
	{
		
		//alertFoc(err_MSG[0],obj);
		return false;
	}
	return true;
}

/* ======================================================================
	Function	: checkEmptyCombo
	Input		: obj	  : Select Box	Object		  
	Retrieval	: boolean : If object contains no data then returns false else returns true
	Explanation : Used to check whether object contains data or not (i.e blank field)
	========================================================================== */


function checkEmptyCombo(obj)
{
	if(obj.options[obj.options.selectedIndex].value == "")
	{
		//alertFoc(err_MSG[1],obj);
		return false;
	}
	return true;
}

/* ======================================================================
	Function	: trim
	Input		: obj	  : Any Form Object 				  
	Retrieval	: String  : removes spaces from both the sides of the String and will return
	Explanation : Used to trim the data contain by specified object
	========================================================================== */
function trim(obj)
{
	var strVal = obj.value
	var newstr=""
	var retstr=""
	var strlen=strVal.length
	for(var start=0;start<strlen;start++)
	{
		if(strVal.substring(start,start+1)!=" ")
		{ 
			newstr = strVal.substring(start,strlen) 
			break;
		}
	}
	strlen = newstr.length
	for(start=strlen;start>0;start--)
	{
		if(newstr.substring(start-1,start)!=" ")
		{ 
			retstr=newstr.substring(0,start) 
			break;
		}
	}
	obj.value = retstr;
}



/* ======================================================================
	Function	: replaceSpecialChars
	Input		: obj	  : Any Form Object 				  
	Retrieval	: String  
	Explanation : Used to replace special characters from data contain by specified object 
				  replace  ' , ", \, < by ` , ` , / , <space  respectivaly 	
	========================================================================== */
function replaceSpecialChars(obj)
{
	var str=""; 
	var j=0;
	var strValue = obj.value;
	for(var i=0; i< strValue.length;i++) 
	{
		if(strValue.charAt(i) == '<' && strValue.charAt(i+1) != ' ')
		{  
			str= str+strValue.substring(j,i)+'< '
			j=i+1		
		}
		if((strValue.charAt(i) =="'") ||(strValue.charAt(i) == "\"") || (strValue.charAt(i) == "~")) 	{
			str= str+strValue.substring(j,i)+"`"
			j=i+1		
		}

		if((strValue.charAt(i) =="\\")) 	{
			str= str+strValue.substring(j,i)+"/"
			j=i+1		
		}
	}
	obj.value = str + strValue.substring(j)
}



/* ======================================================================
	Function	: checkValidCode
	Input		: obj	  : Any Form Object 				  
	Retrieval	: boolean : if Data contains only alphabets, numbers & underscore then returns true else false
	Explanation : Used To validate Data contain by specified object 
				  i.e Data should only alphabets, numbers & underscore
	========================================================================== */
function checkValidCode(obj)
{
  var theinput = obj.value
  var thelength = theinput.length
  var thechar = "";
  for(ctr=0;ctr<thelength;ctr++)
  {
	thechar	=	theinput.substring(ctr,ctr+1)
	if(thechar	>=	"0" && thechar	<=	"9" || thechar	>=	"a" && thechar	<=	"z" || thechar	>=	"A" && thechar	<=	"Z" || thechar == "_" || thechar == ".") 
	 {
	 }
	else 
	 {
		//alertFoc(err_MSG[2],obj);		  
		return false
	 }
  }
   return true;
}  


/* ======================================================================
	Function	: validCharString
	Input		: str	  : String				  
	Retrieval	: boolean : if Data  contain only  characters then returns true else returns false
	Explanation : To Check that the Data should contain only  characters
	========================================================================== */
function validCharString(obj)
 {
	 var str = obj.value
	var len = str.length
	var ch = "";
	for( i=0;i<len;i++)
	{
		ch=str.substring(i,i+1)
		if((ch >='a' && ch<='z') || (ch >='A' && ch<='Z'))
		{
			
		}
		else
		{
			//alertFoc(err_MSG[9],obj);	
			return false
		}
	}	
    return true; 
 }

/* ======================================================================
	Function	: validEmail
	Input		: obj	  : Any Form Object 				  
	Retrieval	: boolean : if email id is blank or not contains @ or . then returns false  else return true
	Explanation : Chekcing valid emailid
	========================================================================== */

function validEmail(obj) 
{
	var strValue = obj.value;

	if (strValue != "" && (strValue.indexOf("@") == -1 || strValue.indexOf(".") == -1)) 
	{
		//alertFoc(err_MSG[3],obj);
		return false;
	 }
	  return true;
}


/* ======================================================================
	Function	: checkChar
	Input		: str	  : String				  
	Retrieval	: boolean : if Data  contain only  characters then returns true else returns false
	Explanation : To Check that the Data should contain only  characters
	========================================================================== */
function checkChar(str)
 {
	var len = str.length
	var ch = "";
	for( i=0;i<len;i++)
	{
		ch=str.substring(i,i+1)
		if((ch >='a' && ch<='z') || (ch >='A' && ch<='Z'))
		{}
		else
		{
			return false
		}
	}	
    return true; 
 }

/* ======================================================================
	Function	: checkMinLength
	Input		: strData : Object Name				  
	Retrieval	: boolean : if minlength less then false , else true is returned
	Explanation : To Check that the Data exceeds max length
	========================================================================== */


function checkMinLength(objName,len)
{
	
	if (objName.value != "")
	{
		if(objName.value.length < len)
		{
			//alertFoc("Please eneter minimum " + len + " characters.",objName);
			//alertFoc("Minimum " + len + " characters has to be entered",objName);
			return false;
		}	
	}
	return true;
}


/* ======================================================================
	Function	: isANumber
	Input		: strData : String				  
	Retrieval	: boolean : if Data  is valid number  then returns true else returns false
	Explanation : To Check that the Data is valid number  
	========================================================================== */


function isANumber(objName)
{
	if (isNaN(objName.value))
	{
		objName.value = 0;
		//alertFoc(err_MSG[4],objName);		
		return false;
	}
	else
	{
		return true;
	}
}

/* ======================================================================
	Function	: isValidDuration
	Input		: strData : String				  
	Retrieval	: boolean : if Data  is valid number  then returns true else returns false
	Explanation : To Check that the Data is valid number  
	========================================================================== */


function isValidDuration(objName)
{
	 if (!isNaN(objName.value))
	{
		 if((objName.value).indexOf(".")==-1)
		{
			if(parseInt(objName.value,10)<=0)
			{
				//alertFoc(err_MSG[5],objName);
				return false
			}	
			
		}
		else
		{
			//alertFoc(err_MSG[5],objName);
			return false
		}
	}
	else
	{
		//alertFoc(err_MSG[5],objName);
		return false;
	}

		return true

}


/* ======================================================================
	Function	: isANumberOnly
	Input		: strData : String				  
	Retrieval	: boolean : if Data  is valid Integer  then returns true else returns false
	Explanation : To Check that the Data is valid Integer (i.e not contains .)  
	========================================================================== */

function isANumberOnly(strData)
{ 

	 if (!isNaN(strData))
	{
		 if(strData.indexOf(".")==-1)
		{
			return true;
		}
		else
		{
			return false
		}
	}
	else
	{
		return false;
	}

}             





/* ======================================================================
	Function	: alertFoc
	Input		: strAlert : alert message to be displayed
				  obj	   : Any Form Object
	Explanation : To alert message and focus on specific form object
	========================================================================== */
function  alertFoc(strAlert,obj)
{
	alert(strAlert);
	obj.focus();
}




/* ======================================================================
	Function	: isADecimalOnly
	Input		: strData   : String 				  				  
	Retrieval	: If Data is Decimal Number Only then returns true else Return false
	Explanation : To check whether specified Data contains Decimal Number Only
	========================================================================== */

function isADecimalOnly(strData)
{
	if((isNaN(strData))&&(strData.indexOf(".")!=-1))
	{
		return true
	}
	return false
}                


 

 /* ======================================================================
	Function	: roundOff
	Input		: strData	: any number or string
				  precision : int : number of digits to be rounded after .
	Retrieval	: number rounded by specified  precesion
	Explanation : rounds the given value to the given precision
	========================================================================== */
	function roundOff(strData, precision)
	{
		if(isNaN(strData)==false)
		{
			return
		}
		  value = ""+strData //convert value to string
		  precision = parseInt(precision);

		  var whole = "" + Math.round(value * Math.pow(10, precision));

		  var decPoint = whole.length - precision;

		  if(decPoint != 0)
		  {
				 result = whole.substring(0, decPoint);
				 result += ".";
				 result += whole.substring(decPoint, whole.length);
		  }
		  else
		  {
				 result = whole;
		  }
		  return result;
	}


	function checkTrim(str)
	 {
		newstr=""
		retstr=""
		strlen=str.length
		for(start=0;start<strlen;start++)
		{
			if(str.substring(start,start+1)!=" ")
			{ newstr=str.substring(start,strlen) }
		}
		
		strlen=newstr.length
		for(start=strlen;start>0;start--)
		{
			if(newstr.substring(start-1,start)!=" ")
			{ retstr=newstr.substring(0,start) }
		}
		
		return retstr
	 }

	function checkMaxLength(obj,iMaxLen)
	{
		var strValue = obj.value
		var iLen = strValue.length
		if(parseInt(iLen) > parseInt(iMaxLen))
		{
			alert(err_MSG[6] + iMaxLen + " characters.",obj)
			return true;
		}
		return false;
	}
	
	
	function checkAlphanumericOnly(obj)
	{	
		var str = obj.value;
		var alpha = false;
		var numeric = false;
		
		var len = str.length
		var ch = "";
		for( i=0;i<len;i++)
		{
			ch=str.substring(i,i+1)
			if((ch >='a' && ch<='z') || (ch >='A' && ch<='Z'))
			{
				alpha = true;
			}
			else if((ch >='0' && ch<='9'))
			{
				 numeric = true;
			}
		}	
	    
	    if((!alpha) || (!numeric))
	    	return false;
	    	
	    return true;	
	}

