//////////////////////////////////////////////////////////////////////////////////////////////
//**-----Function to Perform LEFT TRIM-Removes Leading space from the Input String--------**//
//__________________________________________________________________________________________//
function lTrim(txtBoxValue)//----->INPUT PARAMETER TYPE "String"
{		
	for(startIndx=0;startIndx<txtBoxValue.length;++startIndx){   
		if(txtBoxValue.charAt(startIndx)!=' ')
			break;
		else if((startIndx+1)<txtBoxValue.length){				
			txtBoxValue=txtBoxValue.substring((startIndx+1),txtBoxValue.length);
			startIndx=-1;
		}
	}return txtBoxValue;
}
//////////////////////////////////////////////////////////////////////////////////////////////
//**-----Function to Perform RIGHT TRIM-Removes Trailing space from the Input String------**//
//__________________________________________________________________________________________//
function rTrim(txtBoxValue)//----->INPUT PARAMETER TYPE "String"
{	
	for(startIndx=(txtBoxValue.length-1);startIndx>=0;--startIndx){   
		if(txtBoxValue.charAt(startIndx)!=' ')
			break;
		else{			
			txtBoxValue=txtBoxValue.substring(0,startIndx);
			startIndx=txtBoxValue.length;
		}
	}return txtBoxValue;
}
//////////////////////////////////////////////////////////////////////////////////////////////
//**----Function to Perform TRIM-Removes leading&trailing space from the Input String-----**//
//__________________________________________________________________________________________//
function trim(txtBoxValue)//----->INPUT PARAMETER TYPE "String"
{
	return lTrim(rTrim(txtBoxValue));
}
//////////////////////////////////////////////////////////////////////////////////////////////
//**---Function to Perform ROUNDOF-Truncates the decimal part to 2 Digit(16.656->16.66)---**//
//__________________________________________________________________________________________//
function roundOf(val)//----->INPUT PARAMETER TYPE "Integer","Float","Double"...
{
	return Math.round(val*100)/100;
}
//////////////////////////////////////////////////////////////////////////////////////////////
//**----Function to Perform NUMBERWITH-Checks Whether the input String contains only------**//
//**----numbers with the strings that are passed through Function-------------------------**//
//__________________________________________________________________________________________//
function isNumWith(txtBoxValue,strOthers)    
{ 
	var strTest = new String('0123456789')    
	strTest= strTest + strOthers;    
	var start_i_Indx,start_j_Indx,start_k_Indx= parseInt(txtBoxValue.length,10),n_Length=parseInt(strTest.length,10)    
	var strTemp = new String(txtBoxValue);    
	strTemp =strTemp.toUpperCase();    
	var bTest   
	if(0<start_k_Indx){
		for(start_i_Indx=0;start_i_Indx<start_k_Indx;start_i_Indx++){	
			for(start_j_Indx=0;start_j_Indx<n_Length;start_j_Indx++){    
				if(strTemp.charAt(start_i_Indx)==strTest.charAt(start_j_Indx)){	
					bTest =true;    	
					break;    
				}    
				else{ 
					bTest =false;    
				}    
			}    
			if( bTest)    
				continue;    
			else{
				//alert("Enter numerical value");
				return false;    
			}
		}	
		return true;
	}else{
		//alert("Enter numerical value");
		return false;    
	} 
} 
//////////////////////////////////////////////////////////////////////////////////////////////
//**--Function to CHECK e-Mail-Checks Whether the input String is a Valid e-Mail ID or Not**//
//__________________________________________________________________________________________//
function validEmail(txtmail)
{ 
	invalidChars = '/:,'; 
	if (txtmail == '') {return false;} 
	for (start_i_Indx=0; start_i_Indx<invalidChars.length; start_i_Indx++){ 
		badChar = invalidChars.charAt(start_i_Indx);  
		if (txtmail.indexOf(badChar,0) > -1){ 
			return false;
		}
	} 
	atPos = txtmail.indexOf('@',1);  
	if (atPos == -1) {return false;} 
	if (txtmail.indexOf('@',atPos+1) != -1) {return false;}
	periodPos = txtmail.indexOf('.',atPos);  
	if (periodPos == -1) {return false;} 
	if (periodPos+3 > txtmail.length){return false;}
	return true;
} 
//////////////////////////////////////////////////////////////////////////////////////////////
//**--Function to CHECK e-Mail-Checks Whether the input String is a Valid e-Mail ID or Not from text area where email ids are separated with coma**//
//__________________________________________________________________________________________//
	function validEmailMulti(txtmail)
	{ 
		var list = txtmail.split(",");
		for(var i=0;i<list.length;i++) {
			invalidChars = '/:,'; 
			if (list[i] == '') {return false;} 
			for (start_i_Indx=0; start_i_Indx<invalidChars.length; start_i_Indx++)
			{ 
				badChar = invalidChars.charAt(start_i_Indx);  
				if (list[i].indexOf(badChar,0) > -1)
				{ 
					return false;
				}
			} 
			atPos = list[i].indexOf('@',1);  
			if (atPos == -1) {return false;} 
			if (list[i].indexOf('@',atPos+1) != -1) {return false;}
			periodPos = list[i].indexOf('.',atPos);  
			if (periodPos == -1) {return false;} 
			if (periodPos+3 > list[i].length){return false;}
			periodPos = list[i].lastIndexOf('.');  
			if (periodPos == (list[i].length-1)){return false;}
		}
		return true;
	} 
//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
//**--Function to Perform Entry Check and to show appropriate ALERT(EXCEPT RADIO BUTTON)--**//
//**--isValid(OBJECT,CHECKFOR,MESSAGE,SETFOCUS,SELECTTEXT,CHARLEN)	 ---------------------**//
//**--OBJECT		--->document.formName.objectName				 ---------------------**//
//**--CHECKFOR		--->By Default will be SPACE("") for TEXT,TEXTAREA,COMBOBOX-----------**//
//**--				--->for CHECKBOX its false						 ---------------------**//
//**--				---># for checking if it is a +/- whole number or Not-----------------**//
//**--				--->.# for checking if it is a +/-decimal number or Not---------------**//
//**--				--->+# for checking if it is a + whole number or Not------------------**//
//**--				--->+.# for checking if it is a + decimal number or Not---------------**//
//**--				--->-# for checking if it is a - whole number or Not------------------**//
//**--				--->-.# for checking if it is a - decimal number or Not---------------**//
//**--				--->@ for checking if it is a valid e-mail or Not---------------------**//
//**--				--->* for Password									 -----------------**//
//**--				--->$(@,#,+#...) Skip the Checking if not entered	 -----------------**//
//**--MESSAGE		--->The Error message to be Shown(ALERT)		 ---------------------**//
//**--SETFOCUS		--->Should set Focus or not,by Default True		 ---------------------**//
//**--SELECTTEXT	--->Should Select Text or not,by Default False	 ---------------------**//
//**--CHARLEN		--->To Check the Length of the Input String		 ---------------------**//
//__________________________________________________________________________________________//
function isValid(obj,checkFor,message,setFocus,selectText,charLen)    
{
	var objVal;			//----------FOR STORING OBJECT VALUE
	var objValNum;			//----------FOR STORING OBJECT VALUE NUMERIC PART
	var objType=obj.type;	//----------FORM OBJECT TYPE
	if(objType==null) return false;
	if(objType=="text" || objType=="textarea" || objType=="password" || objType=="hidden") objVal=trim(obj.value);
	if(objType=="select-one") objVal=obj.options[obj.selectedIndex].value;
	if(objType=="checkbox") objVal=obj.checked;
	if(checkFor==null || checkFor=="") checkFor="";
	if(setFocus==null  || setFocus=="") setFocus=true; 
	if(selectText==null  || selectText=="") selectText=false;
	if(objType!='text' || objType!="textarea" || objType!="password") selectText=false;
	if(objType=="checkbox" && checkFor=="") checkFor=false;
	if(charLen==null || charLen=="") charLen=-1;
	objValNum=objVal;
	if(objVal.toString().indexOf(',')!=-1){	
		objValNum=objVal.replace(',','.');
	}
	if(checkFor.length>0){
		if(checkFor.indexOf("$",0)==0){
			if(checkFor.length==1) return true;
			else checkFor=checkFor.substring(1,checkFor.length);
			if(objVal=="") return true;
		}
	}
	 if(checkFor=='@#')//check multiple email ids from text area separated by coma
	 {
	  if(!validEmailMulti(objVal))
	  {
		  if(message!=""  && message!=null) alert(message);
		  if(setFocus==true) obj.focus();
		  if(selectText==true) obj.select();
		  return false;
	  }
	 }
	else if(checkFor=='@'){
		if(!validEmail(objVal)){
			if(message!=""  && message!=null) alert(message);
			if(setFocus==true) obj.focus();
			if(selectText==true) obj.select();
			return false;
		}
	}
	else if(checkFor=='#'){
		if(!isNumWith(objVal,"-") || isNaN(objValNum)){
			if(message!=""  && message!=null) alert(message);
			if(setFocus==true) obj.focus();
			if(selectText==true) obj.select();
			return false;
		}
	}
	else if(checkFor=='.#'){	  
		if(!isNumWith(objVal,",-") || isNaN(objValNum)){
			if(message!=""  && message!=null) alert(message);
			if(setFocus==true) obj.focus();
			if(selectText==true) obj.select();
			return false;
		}
	}
	else if(checkFor=='+#'){
		if(!isNumWith(objVal) || isNaN(objValNum)){
			if(message!=""  && message!=null) alert(message);
			if(setFocus==true) obj.focus();
			if(selectText==true) obj.select();
			return false;
		}
	}
	else if(checkFor=='+.#'){	  
		if(!isNumWith(objVal,",") || isNaN(objValNum)){
			if(message!=""  && message!=null) alert(message);
			if(setFocus==true) obj.focus();
			if(selectText==true) obj.select();
			return false;
		}
	}
	else if(checkFor=='-#'){
		if(!isNumWith(objVal,"-") || isNaN(objValNum) || parseInt(objValNum)>0){
			if(message!=""  && message!=null) alert(message);
			if(setFocus==true) obj.focus();
			if(selectText==true) obj.select();
			return false;
		}
	}
	else if(checkFor=='-.#'){
		if(!isNumWith(objVal,"-,") || isNaN(objValNum) || parseFloat(objValNum)>0){
			if(message!=""  && message!=null) alert(message);
			if(setFocus==true) obj.focus();
			if(selectText==true) obj.select();
			return false;
		}
	}
	else if(objVal==null || objVal==checkFor){
		if(message!=""  && message!=null) alert(message);
		if(setFocus==true) obj.focus();
		if(selectText==true) obj.select();
		return false;
	}
	if(charLen>0 && objVal.length<charLen){
		if(message!=""  && message!=null) alert(message);
		if(setFocus==true) obj.focus();
		if(selectText==true) obj.select();
		return false;
	}
	return true;
}
//////////////////////////////////////////////////////////////////////////////////////////////
