function format(value, lead, sep)
{
	var strValue = new String(value);
	var len = strValue.length;
	var n;
	var strRet = '';
	var ctChar = 3 - (len%3);
	if (ctChar == 3) ctChar =0;
	for (n=0; len > n; n++) {
		if (ctChar == 3) {
			strRet += sep;
			ctChar = 0;
		}
		ctChar++;
		strRet += strValue.substring(n,n+1)		
	}
	if (lead == '%') {
		return strRet + lead;
	}
	else {
		return lead + strRet;
	}
}
function replace(szBuf, szFind, szReplace, lStart)
{
	var b_MSIE3 = navigator.userAgent.lastIndexOf('MSIE 3.') != -1;
	var lFind = 0;
	if (!lStart) lStart = 0;
	
	while (lFind != -1) {
		lFind = szBuf.indexOf(szFind, lStart);

		if (b_MSIE3) {
			if (lFind != 0 &&(lFind == '' || lFind == null)) lFind = -1;
		}

		if (lFind != -1) {
			szBuf = szBuf.substring(0,lFind) + szReplace + szBuf.substring(lFind + szFind.length);
			lStart = lFind + szReplace.length;
		}
	}
	return szBuf;
}
function cleanNumber(strNum)
{
	if (!strNum) return strNum;
	strNum = replace(strNum, '$', '', 0);
	strNum = replace(strNum, ',', '', 0);
	strNum = replace(strNum, '%', '', 0);	
	return strNum;
}

function numFormat(elem, lead, sep)
{
	if (elem.value == '') return true;
	
	var value = parseInt(cleanNumber(elem.value), 10);
	if (0 > value) value = 0;
	if (isNaN(value)) {
		alert('You may have entered an incorrect character for bid amount! \nPlease check your information and try again.');
		elem.value = '';
		elem.focus();
		return false;
	}
	elem.value = format(value, lead, sep);
	return true;
}