/* #############################################################
    These functions are a part of the mls interface
############################################################## */
var PriceMin = 0;
var PriceMax = 100000000;

function onPriceChange(pElem)
{
	var v = cleanNumber(pElem.value);
	var bInvalid = false;

	if (isNaN(Number(v)))
	{
		bInvalid = true;
	}

	numFormat(pElem, '$', ',');

	var frm = window.document.searchoptions;

	if (parseFloat(cleanNumber(frm.pricemin.value)) > parseFloat(cleanNumber(frm.pricemax.value)))
	{
		var temp = frm.pricemax.value;
		frm.pricemax.value = frm.pricemin.value;
		frm.pricemin.value = temp;
	}

	return(true);
}



function onSqfChange(pElem)
{
	var v = cleanNumber(pElem.value);
	var bInvalid = false;

	if (isNaN(Number(v)))
	{
		bInvalid = true;
	}

	numFormat(pElem, '', ',');

	var frm = window.document.searchoptions;

	if (parseFloat(cleanNumber(frm.sqftmin.value)) > parseFloat(cleanNumber(frm.sqftmax.value)))
	{
		var temp = frm.sqftmax.value;
		frm.sqftmax.value = frm.sqftmin.value;
		frm.sqftmin.value = temp;
	}

	return(true);
}

function cleanNumber(strNum)
{

	if (!strNum) return strNum;
	// alert('You are here');
	strNum = replace(strNum, '$', '', 0);
	strNum = replace(strNum, ',', '', 0);
	strNum = replace(strNum, '%', '', 0);
	return strNum;
}

function replace(szBuf, szFind, szReplace, lStart)
{
	var lFind = 0;
	if (!lStart) lStart = 0;

	while (lFind != -1) {
		lFind = szBuf.indexOf(szFind, lStart);

		if (lFind != -1) {
			szBuf = szBuf.substring(0,lFind) + szReplace + szBuf.substring(lFind + szFind.length);
			lStart = lFind + szReplace.length;
		}
	}
	return szBuf;
}

function numFormat(elem, lead, sep)
{
	if (elem.value == '')
	{
		elem.value = format('0', lead, sep);
		return true;
	}

	var value = parseInt(cleanNumber(elem.value), 10);

	if (lead == '')
	{
		if (0 > value)
		{
			alert('You have exceeded the range for the interior size.\nPlease check your information and try again.');
			value = 0;
		}

		if (value > 99999)
		{
			alert('You have exceeded the range for the interior size.\nPlease check your information and try again.');
			value = 99999;
		}
	}

	if (lead == '$')
	{
		if (0 > value)
		{
			alert('You have exceeded the range for the price.\nPlease check your information and try again.');
			value = 0;
		}

		if (value > 100000000)
		{
			alert('You have exceeded the range for the price.\nPlease check your information and try again.');
			value = 100000000;
		}
	}

	if (isNaN(value)) {
		alert('You have entered an incorrect character on this field. \nPlease check your information and try again.');
		elem.value = format('0', lead, sep);
		elem.focus();
		return false;
	}
	elem.value = format(value, lead, sep);
	return true;
}

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;
	}
}
