function trim ( s ){

  return rtrim(ltrim(s));
}

function ltrim ( s ){

	return s.replace( /^\s*/, "" )
}

function rtrim ( s ){

	return s.replace( /\s*$/, "" );
}
function isDigit (c){

	return ((c >= "0") && (c <= "9"))
}

function validateNumericOnlyWithException( tempVal  ){

	var validData = true;
	for (i = 0; i < tempVal.length; i++){
	
		var c = tempVal.charAt(i);
	
		if( c != '' && c!= '*' ){
		
			if (isDigit(c) == false){
			
	       			validData = false;
	       			break;
			}
		}
	}
	
	return validData;

}


function validateNumericOnly( tempVal ){

	var validData = true;
	for (i = 0; i < tempVal.length; i++){
	
		var c = tempVal.charAt(i);
	
		if( c != '' ){
		
			if (isDigit(c) == false){
			
	       			validData = false;
	       			break;
			}
		}
	}
	
	return validData;

}

function validateCurrency( tempVal ){

	var j		= 0;
	var validData 	= true;
	
	for (var i = 0; i < tempVal.length; i++)
	{
		c = tempVal.charAt(i);

		if( c != '.' && c!='' )
		{
			if (isDigit(c) == false)
			{
				validData = false;
				break;
			}
		}

		if(c=='.')
		{
			j=j+1;
		}
		if(tempVal.charAt(i)=='.')
		{
			if (tempVal.substring(i+1,tempVal.length).length>2)
			{
				validData = false;
				break;
			}
		}
	}

	if(j>1)
	{
		validData = false;		
	}
	
	return validData;
}


function checkDate(dateString){

 
 	if(dateString==''){
 	
   		return true;
   	}
 	if (dateString.indexOf("/")==-1){
  		return false;
  	}
 	else{
  		var sdate = dateString.split("/");
  
 
 		if(sdate.length != 3){
 		
			return false;
 		}
 		if(!isdigit(sdate[0]) || !isdigit(sdate[1]) || !isdigit(sdate[2])){ 		
	
			return false;
 		}
 		if(sdate[2].length!=4){
 		
     			return false;
 		}
 
 		if(1*sdate[2]<50){
 		
			sdate[2] = "20"+sdate[2]
			dateString = sdate.join("/")
 		}
 		var chkDate = new Date(Date.parse(dateString))
 		var cmpDate = (chkDate.getMonth()+1)+"/"+(chkDate.getDate())+"/"+(sdate[2])
 		var newDate = (1*sdate[0])+"/"+(1*sdate[1])+"/"+(sdate[2])
 		
 		if ((""+newDate)!= (""+cmpDate) || isNaN(chkDate)){
	
			return false;
 		}
 	}
 	return true;
}

function disableRequiredControls( accLevel ){	
	
	
	var frm = document.companyDetailsFrm;
	
	frm.butAddNew.disabled  = true;
	frm.butDelete.disabled  = true;	
	frm.butUploadImage.disabled  = true;	
	if( accLevel == 'View Only' ){
		
		frm.butSave.disabled  = true;	
	}
	
	var elements 	= frm.elements;
	var elmnAME	= '';	
	var bDisable	= false;
	for( var i=0; i < elements.length; i++ ){
		
		var elmnAME = elements[i].name;
		
		if( bDisable == true ){
			
			elements[i].disabled	= true;
			
			if( accLevel == 'Keywords Only' ){
				
				if( elmnAME == 'Key_1' || elmnAME == 'Key_2' || elmnAME == 'Key_3' || elmnAME == 'Key_4' || elmnAME == 'Key_5'){
					
					elements[i].disabled	= false;
				}				
			}
			
		}
		
		if( elmnAME == 'UserName' ){
			
			bDisable = true;
		}
	}

	if( accLevel == 'Keywords Only' ){
		
		document.companyDetailsFrm.Key_1.focus();
	}
	
	frm.butClose.disabled  = false;	
}
