function formatMoneyField (obj, bConvert, bUseDecs)
// set bConvert to true to convert a normal numeric to a money field
// set bConvert to false to convert back to money field...
//
{

	
	if (obj == null)
	{
		return;
	}

	sText = obj.value;


	bDebug = false;
	
	if (!bConvert){
		sText = replace (sText, ',', '');
		if (sText != '')
			obj.value = parseFloat (sText);
	}	
	else if (sText != ''){
		if (isNumeric (sText)){
		
			// first remove any ','s if they exist
			//
			sText = replace (sText,',', '');
			obj.value = sText;

			// now add in the decimal point...
			//			
			dec_point (obj) 
			
			// now reformat the string so that it's properly comma seperated...
			//
			sText = obj.value;
			sParts = sText.split ('.');
			
			sWholeNumber = sParts[0];
			
			
			sNewWhole = '';
			iCount = 0;
			

			for (i=sWholeNumber.length -1; i >= 0; i--){
				if (iCount == 3){
					sNewWhole = ',' + sNewWhole
					iCount = 0;
				}
	
				sNewWhole = sWholeNumber.substring (i, i+1) + sNewWhole;
				iCount = iCount + 1;
			}
		
		    if (bUseDecs)
			    obj.value = sNewWhole + '.' + sParts[1];
			else
			    obj.value = sNewWhole;

			
		}
		else {
			alert ("Value must be numeric!");
			obj.focus =true;
		}	
	}	
}


function isNumeric(str)
// returns true if str is numeric
// that is it contains only the digits 0-9
// returns false otherwise
// returns false if empty
{
  var len= str.length;
  var iDec = 0;
  
  if (len==0)
    return false;
  
  var p=0;
  var ok= true;
  var ch= "";
  
  while (p<len)
  {
    ch= str.charAt(p);
    if ('0'<=ch && ch<='9')
	{
	}
	else if (ch!='.')
      ok= false;
	
	if (ch=='.')
		iDec++; 
	 
	p++;
  }
  
  if (ok==true){
  // check for decimal place...
  //
  	if (iDec > 1 )
		ok=false;
  }
  
  
  return ok;
}

function replace (s, t, u) {
	iIndex = s.indexOf (t);
	r= '';
	if (iIndex == -1) return s;
	
	r+= s.substring (0,iIndex) + u;
	if (iIndex + t.length < s.length)
		r += replace (s.substring (iIndex + t.length, s.length), t, u);
	return r;
}

function dec_point (obj){
		
		var last_char
		if (isNumeric (obj.value))
		{
				i=0;
		
				//alert(obj.value)
				length_of_string=obj.value.length
				//alert(length_of_string)	

			decpoint=obj.value.indexOf('.');
			//alert(decpoint)
			
			//if(length_of_string-decpoint==3)
			//{
			//return;
			//}
			
			if(decpoint==-1)
			{
			obj.value=obj.value + ".00";
			//alert(obj.value)
			return ;
			}
			if(length_of_string-decpoint==2)
			{
			//alert("2")
			obj.value=obj.value + "0";
			return ;
			}
			
			if(length_of_string-decpoint==1)
			{
			//alert("2")
			obj.value=obj.value + "00";
			return ;
			}

			
			round_up=obj.value.charAt(decpoint+4);
			obj.value=obj.value.substring(0,decpoint+4);

			
			//alert(round_up)
			if(round_up>=1)
			{
			
			var temp;
			temp=parseFloat(obj.value);
			
			temp = temp + .001;
			obj.value=temp;
			obj.value=obj.value.substring(0,decpoint+4);
			dec_point (obj);
			}
	
			if (decpoint != -1){
				round_up=obj.value.charAt(decpoint+3);
				obj.value=obj.value.substring(0,decpoint+3);
			}

			/*
			//alert(round_up)
			if(round_up>=1)
			{
			var temp;
			temp=parseFloat(obj.value);
			
			temp = temp + .01;
			obj.value=temp;
			obj.value=obj.value.substring(0,decpoint+3);
			dec_point (obj);
			}*/	
		}
		else
		{
		alert ("Value must be numeric!");
		obj.value = "";
		}			
} 
