function getBrowser()
{
	var browser = "IE";
	if(document.layers) browser = "NN4";
    else if(!document.all && document.getElementById) browser = "NN6";
	else browser = "IE";
	return browser;
}

// add decimals to the value.
function roundToDecimal(num, decimals) 
{
    var result1 = num * Math.pow(10, decimals);
    var result2 = Math.round(result1);
    var result3 = result2 / Math.pow(10, decimals);
    return padWithZeros(result3, decimals);
}

// make sure we have the trailing zeros.
function padWithZeros(num, decimal_places) 
{
    var value_string = num.toString();
    var decimal_location = value_string.indexOf(".");
    if (decimal_location == -1) {
        decimal_part_length = 0;
        value_string += decimal_places > 0 ? "." : ""
    } else {
        decimal_part_length = value_string.length - decimal_location - 1;
    }
    
    var pad_total = decimal_places - decimal_part_length;
    if (pad_total > 0) {
        for (var counter = 1; counter <= pad_total; counter++) value_string += "0";
    }
    return value_string;
}

// add the thousandth (,) separator.
function addCommaSeperator(num)
{
	while (num.match(/^\d\d{3}/)) { num = num.replace(/(\d)(\d{3}(\.|,|$))/, '$1,$2'); }
    return num;
}

// strip the comma for calculation.
function replaceCharacter(theString, oldChar, newChar) 
{
	var i = 0;
	var j = theString.length;
	for(i=0; i < theString.length; i++) {
		if(theString.charAt(i) == oldChar) {
			theString = theString.substring(0,i) + newChar + theString.substring(i+1,theString.length);
			if(i > j) { break; }
		}
	}
	return theString;
}

// Calculate the value of an improvement.
function getImprovementValue(region, imp, area)
{
	// Values for each improvement by region.
	var total = new Array('0','0','0');
	var impVal = new Array();
	// ('fireplace', 'det garage', 'att garage', 'pool', 'bsmnt', 'bath', 'air', 'deck', 'sunroom')
	// ** Fireplace and bath are flat rate values.
	// ** Others are calculated on the square footage of the improvement.
	// ** Air is calculated on the sq. ft. of the home.
	// ** Basement and Sunroom return a price range (from $300 to $500).
	impVal['01'] = new Array('1000', '14.00', '27.00', '15.00', '4.50-16.00', '2800', '4.00', '7.00', '21.00-30.00'); 
	impVal['02'] = new Array('1100', '14.75', '30.50', '6.00', '4.00-14.00', '2800', '3.75', '14.75', '24.00-42.00'); 
	impVal['03'] = new Array('1400', '17.00', '31.25', '13.50', '3.50-11.50', '3300', '2.25', '14.75', '10.00-16.00'); 
	impVal['04'] = new Array('5800', '10.50', '11.00', '5.00', '4.75-16.00', '1000', '2.25', '6.75', '22.00-31.00'); 
	impVal['05'] = new Array('2800', '16.25', '24.50', '7.50', '4.00-14.00', '2400', '3.75', '10.50', '26.00-47.00'); 
	impVal['06'] = new Array('3800', '19.50', '38.75', '11.00', '3.50-11.50', '1500', '3.50', '5.00', '18.00-30.00'); 
	impVal['07'] = new Array('6100', '17.00', '20.50', '5.00', '2.00-7.00', '3100', '1.25', '4.50', '19.00-34.00'); 
	impVal['08'] = new Array('1000', '15.25', '20.50', '17.50', '4.50-16.00', '2600', '2.50', '5.50', '10.00-22.00'); 
	impVal['09'] = new Array('6000', '12.50', '24.00', '5.00', '3.50-12.00', '3200', '3.00', '10.00', '9.00-16.00'); 
	impVal['10'] = new Array('1000', '13.00', '26.50', '11.00', '6.00-21.00', '3200', '3.25', '14.00', '21.00-52.00'); 

	if (region != "")
	{
		var val = impVal[region][imp];
		
		// perform the calculations
		if (val > "") {
			if (imp=='0' || imp=='5') { 
				total[0] = roundToDecimal(val, 2); 
				total[1] = '0'; 
				total[2] = roundToDecimal(val, 2);
			} else if (imp=='1' || imp=='2' || imp=='3' || imp=='7' || imp=='7') {
				total[0] = val * area; total[1] = '0';
				total[0] = roundToDecimal(total[0], 2);
				total[2] = roundToDecimal(val, 2);
			} else {
				var temp = val.split('-');
				total[0] = temp[0] * area;	total[1] = temp[1] * area;
				total[0] = roundToDecimal(total[0], 2);
				total[1] = roundToDecimal(total[1], 2);
				total[2] = val;
			}
		}
	}
	return total;
}

// Displays and hides the Sq.Ft text area.
function showHideArea(lyr)
{
	var browser = getBrowser();
	var imp = document.form1.fImprovement.value;
	document.form1.fArea.value='';
	if (imp=="" || imp=="0" || imp=="5") {
		if (browser == "NN4") document.showArea.style.display='none'; 
		else if (browser == "NN6") document.getElementById("showArea").style.display='none';
		else document.all.showArea.style.display='none';
	} else {
		if (browser == "NN4") document.showArea.style.display='block';
		else if (browser == "NN6") document.getElementById("showArea").style.display='block';
		else document.all.showArea.style.display='block';
	} 
}

// displays and hides the item layers for the calculation table.
function showHideLayer(lyr, sh)
{
	var browser = getBrowser();
	if (browser == "NN4") document.lyr.style.display=sh;
	else if (browser == "NN6") document.getElementById(lyr).style.display=sh;
	else document.getElementById(lyr).style.display=sh;
}

// called whenever the value of the region/area changes.
function calcImprovement()
{
	var region = document.form1.hRegion.value;
	if (region > 0) { addImprovement(); 
	} else { removeImprovement('all'); }
}

function calcTotals()
{
	var frm = document.form1;
	var val_0 = frm.hFireplace.value * 1;	var val_1 = frm.hGarage_d.value * 1;
	var val_2 = frm.hGarage_a.value * 1;	var val_3 = frm.hPool.value * 1;
	var val_4 = frm.hBasement_min.value * 1; 	var val_4a = frm.hBasement_max.value * 1;
	var val_5 = frm.hBath.value * 1;	var val_6 = frm.hAir.value * 1;		var val_7 = frm.hDeck.value * 1;
	var val_8 = frm.hSunroom_min.value * 1;		var val_8a = frm.hSunroom_max.value * 1;
			
	var total_1 = val_0 + val_1 + val_2 + val_3 + val_4 + val_5 + val_6 + val_7 + val_8;
	var total_2 = val_0 + val_1 + val_2 + val_3 + val_4a + val_5 + val_6 + val_7 + val_8a;
	total_1 = roundToDecimal(total_1, 2);
	total_2 = roundToDecimal(total_2, 2);
	if ((total_2*1) > (total_1*1)) { frm.cTotal.value = ""+addCommaSeperator(total_1)+" - "+addCommaSeperator(total_2)+""; } 

            else { frm.cTotal.value = ""+addCommaSeperator(total_1); }

}

// Every time a new improvement is selected, add the value of this improvement.
function addImprovement()
{
	var frm = document.form1;
	var region = frm.hRegion.value;
	var imp = frm.fImprovement.value;
	var area = frm.fArea.value * 1;
	var content = "";

	if (region == "") { alert("Select an area of the city by using the 'Click Here' link."); }
	else if (imp == "") { alert("Select an improvement from the list."); } 
	else {
		if ((area < "1") && (imp!='0' && imp!='5')) { alert("Please enter the number of square feet of the improvement."); } 
		else {
			var browser = getBrowser();
			if (area == "" || area < 1) area = 1;
			var imp_val = getImprovementValue(region, imp, area);
			
			if (imp == "0") { 
				frm.hFireplace.value = imp_val[0]; 
				frm.cFireplace.value = addCommaSeperator(imp_val[0]); 
				showHideLayer("lyrFireplace", "block");
				content="(flat rate)";
				if (browser == "NN6") { document.getElementById('contentFireplace').innerHTML = content;
				} else { document.all.contentFireplace.innerHTML=content; }
			} else if (imp == "1") { 
				frm.hGarage_d.value = imp_val[0]; 
				frm.cGarage_d.value = addCommaSeperator(imp_val[0]); 
				showHideLayer("lyrGarage_d", "block");
				content="($"+imp_val[2]+" x "+area+" sq.ft.)";
				if (browser == "NN6") { document.getElementById('contentGar_d').innerHTML = content;
				} else { document.all.contentGar_d.innerHTML=content; }
			} else if (imp == "2") { 
				frm.hGarage_a.value = imp_val[0]; 
				frm.cGarage_a.value = addCommaSeperator(imp_val[0]); 
				showHideLayer("lyrGarage_a", "block");
				content="($"+imp_val[2]+" x "+area+" sq.ft.)";
				if (browser == "NN6") { document.getElementById('contentGar_a').innerHTML = content;
				} else { document.all.contentGar_a.innerHTML=content; }
			} else if (imp == "3") { 
				frm.hPool.value = imp_val[0]; 
				frm.cPool.value = addCommaSeperator(imp_val[0]); 
				showHideLayer("lyrPool", "block");
				content="($"+imp_val[2]+" x "+area+" sq.ft.)";
				if (browser == "NN6") { document.getElementById('contentPool').innerHTML = content;
				} else { document.all.contentPool.innerHTML=content; }
			} else if (imp == "4") { 
				frm.hBasement_min.value = imp_val[0]; 
				frm.hBasement_max.value = imp_val[1]; 
				frm.cBasement.value = addCommaSeperator(imp_val[0])+" - "+addCommaSeperator(imp_val[1]); 
				showHideLayer("lyrBasement", "block");
				// basements have price ranges.
				var temp = imp_val[2].split("-");
				content="($"+temp[0]+" - $"+temp[1]+" x "+area+" sq.ft.)";
				if (browser == "NN6") { document.getElementById('contentBasement').innerHTML = content;
				} else { document.all.contentBasement.innerHTML=content; }
			} else if (imp == "5") { 
				frm.hBath.value = imp_val[0]; 
				frm.cBath.value = addCommaSeperator(imp_val[0]); 
				showHideLayer("lyrBath", "block");
				content="(flat rate)";
				if (browser == "NN6") { document.getElementById('contentBath').innerHTML = content;
				} else { document.all.contentBath.innerHTML=content; }
			} else if (imp == "6") { 
				frm.hAir.value = imp_val[0]; 
				frm.cAir.value = addCommaSeperator(imp_val[0]); 
				showHideLayer("lyrAir", "block");
				content="($"+imp_val[2]+" x "+area+" sq.ft.)";
				if (browser == "NN6") { document.getElementById('contentAir').innerHTML = content;
				} else { document.all.contentAir.innerHTML=content; }
			} else if (imp == "7") { 
				frm.hDeck.value = imp_val[0]; 
				frm.cDeck.value = addCommaSeperator(imp_val[0]); 
				showHideLayer("lyrDeck", "block");
				content="($"+imp_val[2]+" x "+area+" sq.ft.)";
				if (browser == "NN6") { document.getElementById('contentDeck').innerHTML = content;
				} else { document.all.contentDeck.innerHTML=content; }
			} else if (imp == "8") { 
				frm.hSunroom_min.value = imp_val[0]; 
				frm.hSunroom_max.value = imp_val[1]; 
				frm.cSunroom.value = addCommaSeperator(imp_val[0])+" - "+addCommaSeperator(imp_val[1]); 
				showHideLayer("lyrSunroom", "block");
				// sunrooms have price ranges.
				var temp = imp_val[2].split("-");
				content="($"+temp[0]+" - $"+temp[1]+" x "+area+" sq.ft.)";
				if (browser == "NN6") { document.getElementById('contentSunroom').innerHTML = content;
				} else { document.all.contentSunroom.innerHTML=content; }
			} else { /* nothing */ }
			
			// calculate the total amount.
			calcTotals();
		}
	}
}

// Remove selected or all improvememts from the calculations. 
function removeImprovement(field)
{
	var frm = document.form1;
	frm.fImprovement.options[0].selected=true;
	if (field == 'all') {
		frm.fArea.value="0";	frm.cTotal.value="0.00";	
		frm.hFireplace.value="0";	frm.hGarage_d.value="0";		frm.hGarage_a.value="0";
		frm.hPool.value="0";	frm.hBasement_min.value="0";	frm.hBasement_max.value="0";
		frm.hBath.value="0";	frm.hAir.value="0";				frm.hDeck.value="0";
		frm.hSunroom_min.value="0";		frm.hSunroom_max.value="0";
		
		showHideLayer('lyrFireplace', 'none');	showHideLayer('lyrGarage_d', 'none');
		showHideLayer('lyrGarage_a', 'none');	showHideLayer('lyrPool', 'none');
		showHideLayer('lyrBasement', 'none');	showHideLayer('lyrBath', 'none');
		showHideLayer('lyrAir', 'none');		showHideLayer('lyrDeck', 'none');
		showHideLayer('lyrSunroom', 'none');
	} else {
		if (field == "fireplace") { frm.hFireplace.value="0"; showHideLayer('lyrFireplace', 'none'); }
		if (field == "garage_d") { frm.hGarage_d.value="0"; showHideLayer('lyrGarage_d', 'none'); }
		if (field == "garage_a") { frm.hGarage_a.value="0"; showHideLayer('lyrGarage_a', 'none'); }
		if (field == "pool") { frm.hPool.value="0"; showHideLayer('lyrPool', 'none'); }
		if (field == "basement") { frm.hBasement_min.value="0"; frm.hBasement_max.value="0"; showHideLayer('lyrBasement', 'none'); }
		if (field == "bath") { frm.hBath.value="0"; showHideLayer('lyrBath', 'none'); }
		if (field == "air") { frm.hAir.value="0"; showHideLayer('lyrAir', 'none'); }
		if (field == "deck") { frm.hDeck.value="0"; showHideLayer('lyrDeck', 'none'); }
		if (field == "sunroom") { frm.hSunroom_min.value="0"; frm.hSunroom_max.value="0"; showHideLayer('lyrSunroom', 'none'); }
		
		// re-calculate the results.
		calcTotals();
	}
	// hide the sq.ft. text box.
	showHideArea();
}

function popupwind(URL) 
{
	window.open(URL,'map','toolbar=0,scrollbars=1,location=0,status=0,menubar=0,resizable=1,width=590,height=510,left = 0,top = 0');
	removeImprovement('all');
}
