//
// Copyright (c) 2000-2001 by experITy
//       All Rights Reserved
//

function Currency_int_onChange(field)
{
	field.value = Currency_int_Format(field.value);
}

function Currency_int_onInput(inputChar, field, bPositive)
{		
	if(	
		(inputChar < 48 || inputChar > 57) &&	// not a number 
		((!bPositive && inputChar!= 45) || bPositive))// If allow Negative and not "-"
	{
		return false;
	}	

	var strInput = field.value;
	if( inputChar == 45 && strInput.length >1) // "-" only allow at the beginning
	{
		return false;
	}	

	if ((strInput.length == 1 && strInput != "$") ||			
		(inputChar == 45 && strInput.length == 1 && strInput != "$"))	
		field.value = "$" + strInput	
		
	return true;
}

function Currency_int_Format(strInput)
{		
	if(strInput.length == 0)
		return strInput;
			
	if(strInput.substring(0, 1) != "$") 		
		strInput = "$" + strInput;
		
	if(strInput.indexOf("-", 0) == 1)
	{
		strHead = strInput.substring(0, 2);
		strInput = strInput.substring(2); 		
	}
	else
	{
		strHead = strInput.substring(0, 1);	
		strInput = strInput.substring(1); 
	}	

	if (strInput.length == 0)
		strInput = "0";

	strTail = "";

	if(strInput.indexOf(",", 0) == -1 && strInput.length >= 4)  // not formated yet
	{	
		while(strInput.indexOf("0", 0) == "0")
		{
			strInput = strInput.substring(1);
		}
		
		while(strInput.length >= 4)
		{
			pos = strInput.length - 3		
			strTail = "," + strInput.substring(pos) + strTail;		
			strInput = strInput.substring(0, pos);		
		}
	}
	
	return strHead + strInput + strTail;		
}
