//	General Functions    //

var space_comm = " \t\n\r" ;
var test="test2"
function isempty(value)
{ //to check whether the value is a empty or not
	var i ;
	if (value==null) return true ;
	for(i=0; i<value.length; i++)
	{
		var c = value.charAt(i);
		if (space_comm.indexOf(c) == -1) 
		return false;
	}
	return true;
}

function isnumber(value)
{ //to check whether the value is a number or not
	var i=0 ;
	if (value==null) return true ;

	for(i=0;i<value.length;i++)
	{
		var c=value.charAt(i) ;
		if (!((c >= "0") && (c <= "9")) && !(c == ".") )
			return false ;
	}
	return true;
}

function lpadzero(value, count)
{ //Used to left pad 0 the given no of times
	var mpad = ""
	if (value == null) value = "" ;
	if (count == 0)
		return value ;
	count -= value.length ;
	for (var i=0; i < count; i++)
		mpad += "0" ;
		return mpad + "" + value ;
}

function padzero(value, count)	//	old
{ //Used to left pad 0 the given no of times
	var mpad = ""
	if (count == 0)
		return value ;
	for (var i=1; i <= count; i++)
		mpad = mpad + "0" ;
		return mpad + "" + value ;
}

function checkempty(field, msg)
{ //Used to check if a field is empty
	if (isempty(field.value))
	{
		alert(msg) ;
		field.focus() ;
		field.select() ;
		return false ;
	}
	else
		return true ;
}

function checknumber(field, msg)
{  //to validate all the number only fields
	if (!isnumber(field.value))
	{
		alert(msg) ;
		field.focus() ;
		field.select() ;
		return false ;
	}
	return true ;
}

function checklengthless(col, size, msg)
{
//////////////////////////////////////////////////////////////
//Function	: checklengthless				    						//
//Purpose	: to check whether the length of the column	//
//				  is less than the given length							//
//Arguments : col - The column for which you want to check  //
//				  the length.												//
//				  size - The minimum size the field should have	//
//				  msg - The message that should be displayed 	//			  	
//Returns	: Boolean				    								//
//////////////////////////////////////////////////////////////

	var txtlen
	txtlen = col.value
	if (txtlen==null) return true ;
	if (txtlen.length==0) return true ;
	if (txtlen.length < size )
	{
		alert(msg)
		col.focus();
		col.select();
		return false;
	}
	return true;
}

function checklengthgreater(col, size, msg)
{
//////////////////////////////////////////////////////////////
//Function	: checklengthgreater				    						//
//Purpose	: to check the whether the lenght of the column	//
//				  is greater than the given length							//
//Arguments : col - The column for which you want to check  //
//				  the length.												//
//				  size - The minimum size the field should have	//
//				  msg - The message that should be displayed 	//			  	
//Returns	: Boolean				    								//
//////////////////////////////////////////////////////////////

	var txtlen
	txtlen = col.value
	if (txtlen==null) return true ;
	if (txtlen.length==0) return true ;
	if (txtlen.length > size )
	{
		alert(msg)
		col.focus();
		col.select();
		return false;
	}
	return true;
}

function checklengthequal(col, size, msg)
{
//////////////////////////////////////////////////////////////
//Function	: checklengthless				    						//
//Purpose	: to check the whether the lenght of the column	//
//				  is equal to the given length							//
//Arguments : col - The column for which you want to check  //
//				  the length.												//
//				  size - The minimum size the field should have	//
//				  msg - The message that should be displayed 	//			  	
//Returns	: Boolean				    								//
//////////////////////////////////////////////////////////////

	var txtlen
	txtlen = col.value
	if (txtlen==null) return true ;
	if (txtlen.length==0) return true ;
	if (txtlen.length != size )
	{
		alert(msg)
		col.focus();
		col.select();
		return false;
	}
	return true;
}

function checksite(field, msg)
{
//////////////////////////////////////////////////////////////
//Function	: checksite()			    	                //
//Purpose	: to validate the Web Site URL.                 //
//Arguments	: Text Box objects, message to display	        //
//Returns	: Boolean				                        //
//////////////////////////////////////////////////////////////
	if (!validatesite(field.value))
	{
		alert(msg) ;
		field.focus() ;
		field.select() ;
		return false ;
	}
	return true ;
}

function validatesite(value)
{
////////////////////////////////////////////////////////
//Function	: validatesite()			                  //
//Purpose	: to check URL Properly Ended           	//
//Arguments	: String value	 				            //
//Returns	: Boolean					                  //
////////////////////////////////////////////////////////
	var i=0,j=0, slash1=2,slash2,slashcount;
	var dot1,dot2,dotcount,space,dotincr=0;
	if (value==null) return true ;
	if (value.length==0) return true ;
	
	for(;i<value.length;i++)
	{
		var c=value.charAt(i) ;
		if (c == ".") 
                {
                 dotincr++;
                 var dotstr = value.indexOf(".",0);
                 var ddotstr = value.substring(dotstr,dotstr+2);
                 if (ddotstr == "..") return false;
     		}
                 var bslash = value.indexOf("/",0);
                 var dbslash = value.substring(bslash,bslash+2);
                 if ((bslash >0)  && (dbslash != "//")) return false;         
                   
                 var space  = value.indexOf(" ",1);             
  		 var slash1 = value.indexOf("/",0);
                 var dot1   = value.indexOf(".",0); 
                 if ((slash1 == 0) || (dot1 == 0) || (space > 0)) return false; 
	 }  
         var lastchr = value.length;
         for (j=lastchr; j>=lastchr; j--)
         {
              var chrs = value.charAt(j-1);
              if (chrs == ".") return false;
         }  
	if (dotincr < 1) return false;
        return true;
}

function multiselectvalidate_less(field, len, msg)
/*	To check if the number of entries selected in a multiselect 
	combo box is less than the given length	*/
{	var j=0;
	for (var i=0; i< field.length; i++)
	{
		if (field[i].selected) j++ ;
		if (j > len)
		{
			alert(msg) ;
			field.focus() ;
			return false ;	
		}
	}	
	return true;
}

function multiselectvalidate_greater(field, len, msg)
/*	To check if the number of entries selected in a multiselect 
	combo box is greater than the given length	*/
{	var j=0;
	for (var i=0; i< field.length; i++)
	{
		if (field[i].selected) j++ ;
		if (j < len)
		{
			alert(msg) ;
			field.focus() ;
			return false ;	
		}
	}
	return true;
}

function multiselectvalidate_equal(field, len, msg)
/*	To check if the number of entries selected in a multiselect 
	combo box is equal to the given length	*/
{	var j=0;
	for (var i=0; i< field.length; i++)
	{
		if (field[i].selected) j++ ;
		if (j != len)
		{
			alert(msg) ;
			field.focus() ;
			return false ;	
		}
	}	
	return true;
}

// Function for displaying the alert box for improper entry in the fields
function warninvalid (field, alertmes)
{
	field.focus()
	field.select()
	alert(alertmes)   
	return false
}

// Function to change the every first letter of the word to Upper case
// Parameter : Text field object
function initcap(frmObj) {
	var index;
	var tmpStr;
	var tmpChar;
	var preString;
	var postString;
	var strlen;
	tmpStr = frmObj.value.toLowerCase();
	strLen = tmpStr.length;
	if (strLen > 0)  
	{
		for (index = 0; index < strLen; index++)  
		{
			if (index == 0)  
			{
				tmpChar = tmpStr.substring(0,1).toUpperCase();
				postString = tmpStr.substring(1,strLen);
				tmpStr = tmpChar + postString;
			}
		else 
			{
				tmpChar = tmpStr.substring(index, index+1);
				if (tmpChar == " " && index < (strLen-1))  
				{
					tmpChar = tmpStr.substring(index+1, index+2).toUpperCase();
					preString = tmpStr.substring(0, index+1);
					postString = tmpStr.substring(index+2,strLen);
					tmpStr = preString + tmpChar + postString;
				}
			}
		}
	}
	frmObj.value = tmpStr;
}


// to select an item from a list box
function selectitem(pfield, pvalue)
{
	var len=pfield.length ;
	for (var i=0; i<len;i++)
	{
		if (pfield[i].value==pvalue)
		{
			pfield[i].selected=true;
			break ;
		}
	}
}

// to select an item from a list box
function selectitem(pfield, pvalue)
{
	var len=pfield.length ;
	for (var i=0; i<len;i++)
	{
		if (pfield[i].value==pvalue)
		{
			pfield[i].selected=true;
			break ;
		}
	}
}


//Login Enter Key pressed
function keyPressed(pvalue)
{
	var preturn = false;
	if (window.event.keyCode == pvalue ) preturn = true;
	return preturn;
}


// Valid Zip Code: NNNNN-NNNN/123456789/nnnn<=9
function iszipcode(value)
{ //to check whether the value is a number or not

	var i=0;
	var mhypen_count=0;
	if (value==null) return true ;

	for(i=0;i<value.length;i++)
	{
		var c=value.charAt(i) ;
		if (c == "-") mhypen_count = mhypen_count + 1;
	}
	if (mhypen_count >= 2) return false;
	if ( (mhypen_count == 0) && ( value.length >= 10 )) return false;

	for(i=0;i<value.length;i++)
	{
		var c=value.charAt(i) ;
		if ( !(  ((c >= "0") && (c <= "9")) || ( (parseInt(i) == 5) && (c == "-") )  )  ) 
			{
				return false;					
			}
	}
	return true;
}

// For window status is null
function fn_onmouse()
{
	window.status='';
	return true;
}


function checkFloat(fld,pre,sca,msg)
{

	var maxval=0;
	var a=fld.value
	if(a.length > 0)
	{
		if(a.indexOf('.') == -1)
		{	
			fld.value = a+".00";
		}
		if(a.indexOf('.') > 0)
		{	
			
			var fldfloatval = a.substring(a.indexOf('.')+1);
			
			
			if(fldfloatval.length > 0 && fldfloatval.length <= 2)
			{
				fld.value = a;
			}
			else if(fldfloatval.length > 2)
			{
				var fldtrimval = a.substring(0,a.indexOf('.')+3);
				var roundval1 = a.substring(a.indexOf('.')+3,a.indexOf('.')+4);
				
				if(roundval1 >= 5)
					fldtrimval = parseFloat(fldtrimval) + 0.01;
				
				
				if((fldtrimval.toString()).indexOf('.') == -1)
				{	
					fldtrimval = fldtrimval+".00";
				}
				fld.value = fldtrimval;
			}
			else
			{
				fld.value = a+"00";
			}
		}
	}
	maxval=1;
	for(i=0;i<(parseInt(pre)-parseInt(sca));i++)
		maxval*=10;
		
	if(parseFloat(a) >= maxval) return false ;
/*	{
		alert(msg);
		fld.select();
		fld.focus();
		return false
	}*/
	return true;
}

function validatefileext(pobject,pfileext,msg)
{
	var mfilevalue = pobject.value ;
	for(var j=0;j<mfilevalue.length;j++)
	{
		var c=mfilevalue.charAt(j) ;
		if (c == ".") 
		{
		     var dotstr = mfilevalue.indexOf(".",0);
		     var fileext = mfilevalue.substring(dotstr+1,mfilevalue.length);
		     fileext = fileext.toUpperCase() ;
		     pfileext = pfileext.toUpperCase() ; 
			 switch (pfileext)
			 {
				case "HTM":
							if ((fileext != "HTM")&&(fileext != "HTML")) 
								{
									alert(msg) ;
									pobject.focus() ;
									return false ;
								}	
							break ;
				default:
							if (fileext != pfileext)
								{
									alert(msg) ;
									pobject.focus() ;
									return false ;
								}	
							break ;
			 }
		}		
	}
	return true ;
}

function ValidateTime(pObject,msg)
	{
		if (pObject[pObject.selectedIndex].value == "")
		{
			alert(msg) ;	
			pObject.focus() ;
			return false ;
		}
		return true ;
	}
