
/**
* Remove leading spaces
*/
 function ltrim ( s )
 {
	return s.replace( /^\s*/, "" );
 }


/**
* Remove trailing spaces
*/
 function rtrim ( s )
 {
	return s.replace( /\s*$/, "" );
 }

/**
* Remove leading spaces and trailing spaces
*/
 function trim ( s )
 {
	return rtrim(ltrim(s));
 }

/**
* Return 2 decimal places. Remove anything more than 2 decimal places. Add commas to thousands.
* Round up to the tenth of the decimal.
*/
 function fdec( arg_num )
 {

	//check if not a number
	if (isNaN(arg_num)) 
		return("0.00");

	var num = Number(arg_num);

	//don't do anything if 0
	if (num == 0)
		return ("0.00");

	if (num < 0 ) 
		roundupval = (0.005)*(-1);
	else
		roundupval = (0.005);


	//in case there are more than two decimal places, take out all and leave only two decimal places
 	var decResult = (parseInt(((num+roundupval)*100)))/100;

	//again, check if not a number
	if (isNaN(decResult)) return("0.00");


	//if number has no or only one decimal place, then add ceros at the end
	var str_num = new String(decResult);

	if (str_num.indexOf(".") < 0 )	// if number has no decimal value, then add a .00 at the end
		str_num = str_num + ".00";
	else if (str_num.indexOf(".") > 0 && str_num.length - str_num.lastIndexOf(".") == 2 ) 	//if number has only one decimal value, then add a 0 at the end
		str_num = str_num + "0";


	//don't do anything if positive and less than 1,000
	if (decResult > 0 && decResult < 1000)
		return (str_num);
	
	//don't do anything if negative and greater than -1,000
	if (decResult < 0 && decResult > -1000)
		return (str_num);
	

	// if negative number, take out the negative sign, we'll add it later
	if (decResult < 0) 
		str_num = str_num.substr(1, str_num.length-1);


	//grab the integer part of the decimal number
	var int_part = new String(parseInt(str_num));

	// grab the decimal part of the decimal number
	var dec_part = "";
	if (str_num.lastIndexOf(".") > 0)
		dec_part = str_num.substr(str_num.lastIndexOf("."));

	//calculate where the commas go	  
	for (var i = 0; i < Math.floor((int_part.length-(1+i))/3); i++)
		int_part = int_part.substring(0,int_part.length-(4*i+3))+','+ int_part.substring(int_part.length-(4*i+3));
	
	// if negative number, add back again the negative sign
	if (decResult < 0 && int_part.substr(0,1) != "-")
		return ('-' + int_part + dec_part );
	else
		return ('' + int_part + dec_part );


 }


/**
* Remove all decimal places. Add commas to thousands. Round up to the nearest integer.
*/
 function fint( arg_num )
 {

	//check if not a number
	if (isNaN(arg_num)) 
		return("0");
	
	var num = Number(arg_num);

	//don't do anything if 0
	if (num == 0) 
		return ("0");


	if (num < 0 ) 
		roundupval = (0.5)*(-1);
	else
		roundupval = (0.5);


	// in case there are decimal places, take out all
 	var intResult = parseInt(num+roundupval);

	//don't do anything if positive and less than 1,000
	if (intResult > 0 && intResult < 1000)
		return (intResult);
	
	//don't do anything if negative and greater than -1,000
	if (intResult < 0 && intResult > -1000)
		return (intResult);
	
	var strResult = new String(intResult);

	// if negative, take out the negative sign, we'll add it later
	if (intResult < 0) 
		strResult = strResult.substr(1, strResult.length-1);


	//calculate where the commas go	  
	for (var i = 0; i < Math.floor((strResult.length-(1+i))/3); i++)
		strResult = strResult.substring(0,strResult.length-(4*i+3))+','+ strResult.substring(strResult.length-(4*i+3));



	//if negative, add back again the negative sign
	if (intResult < 0 && strResult.substr(0,1) != "-")
		return ("-" + strResult);
	else
		return (strResult);

 }


// general purpose function to see if a suspected numeric input is a positive integer
function isPosInt(inputStr) 
{
        for (var i = 0; i < inputStr.length; i++) 
        {
                var oneChar = inputStr.substring(i, i + 1);
                if (oneChar < "0" || oneChar > "9") 
                        return false;
        }
        return true;
}



function checkMail(strMail)
{

	var countAt=0
	var countDot=0
	var alphabets=".-_@abcdefghijklmnopqrstuvwxyz1234567890";
	var lowerEmail = strMail.toLowerCase();
	var temp;

	if(strMail=="")
		return false;

	if(strMail.length < 7)
		return false;


	for(loopcount=0;loopcount<strMail.length;loopcount++){
		temp=lowerEmail.substring(loopcount,loopcount+1)
		if(alphabets.indexOf(temp)==-1){
			return false;
		}
	}

	for(loopcount=0;loopcount<strMail.length;loopcount++){
		if(loopcount==0){
			if(strMail.charAt(loopcount)=="@" ||  strMail.charAt(loopcount)=="."){
				return false;
			}
		}
		else{
			if(strMail.charAt(loopcount)=="@"){
				countAt=loopcount;
			}
			else if(strMail.charAt(loopcount)=="."){
				countDot=loopcount;
			}
		}
	}

	if(countAt==0 || countDot==0){
		return false;
	}

	if(countDot < countAt){
		return false;
	}
	else if((countAt+1) == countDot){
		return false;
	}

	if(((strMail.length-(countDot+1)) < 3) || ((strMail.length-(countDot+1)) < 3)){
		return false;
	}

}

function convInt7(num)
{

	if (isNaN(num))
	  return (0);
	  
	var i_temp = 0;
	
	i_temp = parseInt(num);

	if (i_temp > 9999999) 
 	  i_temp = 9999999;
	else if (i_temp < -9999999) 
	  i_temp = -9999999;

	return (i_temp);
}

function callHistory()
{
	//08/23/02: Netscape 4.7 has an issue that document.write() will not draw properly
	// after resizing the browser window. So we added this function so that whenever the
	// window resizes, it calls itself again.
	if( document.layers && doDisplay)
	{
		//document.layers tells us that we are dealing with Netscape v4.7
		//clear the window's contents first
		document.open();
		document.write("");
		document.close();
		
		//call itself again
		self.location.href = self.location.href;
	}
}

function clearResults(){
	var url = new String(window.location);
	var theLocation = url.substring(0,url.indexOf("html")+4); //+"?cid=" + gCMScid;
	window.location = theLocation;
}

function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)) num = "0";
	
	num = ((Math.floor(num*100+0.5))).toString();
	num = num.substring(0,(num.length - 2)) + "." + num.substring((num.length - 2),num.length);
	if ( (num.substring(0,(num.length-3))) == "" ){
		num = "0" + num;
	}
	temp = num.substring(num.length-3,num.length);
	num = num.substring(0,num.length-3);
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
	num = num + "" + temp;	
	return ('$' + num );
}



