function removeStr(iNum)
{
	if (!iNum) return iNum;
	iNum = replaceStr(iNum, '$', '', 0);
	iNum = replaceStr(iNum, ',', '', 0);
	iNum = replaceStr(iNum, '%', '', 0);
	iNum = replaceStr(iNum, '#', '', 0);	
	return iNum;
}

function replaceStr(iStr, iLocate, iReplace, iBegin)
{
	var lFind = 0;
	if (!iBegin) iBegin = 0;
	
	while (lFind != -1) {
		lFind = iStr.indexOf(iLocate, iBegin);

		if (lFind != -1) {
			iStr = iStr.substring(0,lFind) + iReplace + iStr.substring(lFind + iLocate.length);
			iBegin = lFind + iReplace.length;
		}
	}
	return iStr;
}

function format(iVal, iStart, iBreak)
{
	var strValue = new String(iVal);
	var len = strValue.length;
	var n;
	var strRet = '';
	
	var char1 = '.';
	if (strValue.indexOf(char1) != -1)
	{
		len = strValue.indexOf(char1);
	}

	var ctChar = 3 - (len%3);
	if (ctChar == 3) ctChar =0;
	for (n=0; len > n; n++) 
	{
		if (ctChar == 3) 
		{
			strRet += iBreak;
			ctChar = 0;
		}
		ctChar++;
		strRet += strValue.substring(n,n+1)		
	}
	if (iStart == '%') 
	{
		return strRet + iStart;
	}
	else 
	{
		return iStart + strRet;
	}
}

function calculateRebate()
{
	var buyAmount = removeStr(document.rebateForm.buyAmount.value);
	var sellAmount = removeStr(document.rebateForm.sellAmount.value);
	var rebateAmount = 0;
	var buymultiplier = 0;
	var sellmultiplier = 0;
	var tempBuyAmount = 0;
	var tempSellAmount = 0;
	
	
	if (isNaN(buyAmount) || buyAmount == "")
	{
		buyAmount = 0;
	}
	if (isNaN(sellAmount) || sellAmount == "")
	{
		sellAmount = 0;
	}
	
	
	if (buyAmount >= 75000)
	{
		tempBuyAmount = buyAmount - 75000;
		buymultiplier = parseInt(1 + (tempBuyAmount/50000));
		rebateAmount = eval(rebateAmount) +  (buymultiplier * 200);
	}
	else
	{
		rebateAmount = eval(rebateAmount) + 0;
	}
	if (sellAmount >= 75000)
	{
		tempSellAmount = sellAmount - 75000;
		sellmultiplier = parseInt(1 + (tempSellAmount/50000));
		rebateAmount = eval(rebateAmount) +  (sellmultiplier * 200);
	}
	else
	{
		rebateAmount = eval(rebateAmount) + 0;
	}
	
	if (buyAmount != 0)
	{
		document.rebateForm.buyAmount.value = format(buyAmount, '', ',');
	}
	if (sellAmount != 0)
	{
		document.rebateForm.sellAmount.value = format(sellAmount, '', ',');
	}
	document.rebateForm.rebateAmount.value = format(rebateAmount, '', ',');
	return;
	}


