function callFormatter(field,decimal_places){
  var x=document.getElementsByName(field);
  x[0].value = formatNumber(stringTrim(x[0].value),null,decimal_places);
}

 function GetDecimalDelimiter(countryCode)
{
  switch (countryCode)
  {
    case 3:   
           return '#';
    case 2:   
           return ',';
    default:
           return '.';
  }
}

function GetCommaDelimiter(countryCode)
{
  switch (countryCode)
  { 
    case 3:          
           return '*';
    case 2:   
           return ',';
    default:
           return ',';
  }
 
}

function GetDecimalPlaces(decimalPlaces)
{
	decimalPlaces = parseInt(decimalPlaces);
	if(!Number(decimalPlaces))
	   	decimalPlaces=0;
	
	return decimalPlaces;
  
}

function roundOffNumber(myNum, numOfDec) 
{ 

var decimal = 1 
for(i=1; i<=numOfDec;i++) 
decimal = decimal *10 
var myFormattedNum = (Math.round(myNum * decimal)/decimal).toFixed(numOfDec) 
return myFormattedNum + "";

}



function FormatClean(num)
{
     var sVal='';
     var nVal = num.length;
     var sChar='';
     
   try
   {
      for(c=0;c<nVal;c++)
      {
         sChar = num.charAt(c);
         nChar = sChar.charCodeAt(0);
         if (((nChar >=48) && (nChar <=57))|| (nChar == 46))  { sVal += num.charAt(c);   }
      }
   }
    catch (exception) { AlertError("Format Clean",exception); }
    return sVal;
}
  
//will remove spl char, comma.
function FormatCleaner1(num)
{
     var sVal='';
     var nVal = num.length;
     var sChar='';
     
   try
   {
      for(c=0;c<nVal;c++)
      {
         sChar = num.charAt(c);
         nChar = sChar.charCodeAt(0);
         if (((nChar >=97) && (nChar <=122))||((nChar >=65) && (nChar <=90))||((nChar >=48) && (nChar <=57))|| (nChar == 46))  { sVal += num.charAt(c);   }
      }
   }
    catch (exception) { AlertError("Format Clean",exception); }
    return sVal;
}
  
//will remove comma.
function FormatCleaner(num)
{
	if(num.replace(/,/g,'').length > 0 && !isNaN(num.replace(/,/g,'')))
		num = num.replace(/,/g,'');
	return num;
}
  
function formatNumber(number,countryCode,decimalPlaces)
{       
  var minus='';
  var comma='';
  var decimalDelimeter='';
  var preDecimal='';
  var postDecimal='';
  var cleanedNum='';
  var formatedValue='';
  var num = ""+number;
  
  num = FormatCleaner(num);
  
  try 
  {
  
  	if(isNaN(num) || !num.length>0)
  		return num;
  	
	num = roundOffNumber(num, decimalPlaces)
    decimalPlaces = GetDecimalPlaces(decimalPlaces);
    comma = GetCommaDelimiter(countryCode);
    decimalDelimeter = GetDecimalDelimiter(countryCode);
    
    if (decimalPlaces < 1) { decimalDelimeter = ''; }
    
    if (num.lastIndexOf("-") == 0) { minus='-'; }
   
    cleanedNum = FormatClean(num);
   
    if(cleanedNum.lastIndexOf(".") != -1){
	    var x = cleanedNum.split('.');
	    preDecimal = x[0]; 
	  	postDecimal = x.length > 1 ? x[1] : '';
	  	} 
     else{
     	preDecimal = cleanedNum;
     }
    
    if (preDecimal.length < 1)
    {
       preDecimal = '0';
    }
    else if (parseFloat(preDecimal) == 0)
    {
      preDecimal = '0';
    }
    
    if (postDecimal.length < decimalPlaces)
    {
       postDecimal = includeLeadingNumber(postDecimal,decimalPlaces);
    }
    else if (postDecimal.length == decimalPlaces)
    {
       postDecimal = includeLeadingNumber(postDecimal,decimalPlaces);
    }

	else if (postDecimal.length > decimalPlaces)
    {
		var str = "" + postDecimal;
		var str = str.substring(0, decimalPlaces);
		postDecimal = str ;
    }
   
   formatedValue = getConfiguredFortmat(minus , preDecimal ,decimalDelimeter ,postDecimal);
    
    var regex  = new RegExp('(-?[0-9]+)([0-9]{3})');
 
    while(regex.test(formatedValue))
    {
       formatedValue = formatedValue.replace(regex, '$1' + comma + '$2');
    }
       
  }
  catch (exception) { AlertError("Format Number",exception); }
  return formatedValue;
}

function includeLeadingNumber(postDecimal,decimalPlaces){
 
 var postDecimal = postDecimal;
 var pLength =0;
  
 if(postDecimal.length > 0)
	pLength = parseInt(postDecimal.length);
	
  for(i = pLength; i < decimalPlaces; i++)
    {
      postDecimal += '0';
    }
     return postDecimal;
 }

 function AlertError(methodName,e)
 {
            if (e.description == null) { alert(methodName + " Exception: " + e.message); }
            else {  alert(methodName + " Exception: " + e.description); }
 }

function getConfiguredFortmat(minus , preDecimal ,decimalDelimeter ,postDecimal){

	if(DECIMAL_CONFIGURATION == true && decimalDelimeter != ''){
		return minus + preDecimal + decimalDelimeter + postDecimal ;
	}
	else{
		return minus + preDecimal;
	}
}


function NumberFormatRoundOff(txtName){
	
	if( !isNaN(txtName.value) & txtName.value > 0 ){
		if(isFinite(txtName.value))
			txtName.value = Math.round(txtName.value);
		}

}


