var Fat = {
	make_hex : function (r,g,b) 
	{
		r = r.toString(16); if (r.length == 1) r = '0' + r;
		g = g.toString(16); if (g.length == 1) g = '0' + g;
		b = b.toString(16); if (b.length == 1) b = '0' + b;
		return "#" + r + g + b;
	},
	fade_all : function ()
	{		
		var a = document.getElementsByTagName("*");
		for (var i = 0; i < a.length; i++) 
		{
			var o = a[i];
			var r = /fade-?(\w{3,6})?/.exec(o.className);
			if (r)
			{
				if (!r[1]) r[1] = "";
				if (o.id) Fat.fade_element(o.id,null,null,"#"+r[1]);
			}
		}
	},
	fade_element : function (id, fps, duration, from, to) 
	{
		if (!fps) fps = 30;
		if (!duration) duration = 3000;
		if (!from || from=="#") from = "#8A1C01";
		if (!to) to = this.get_bgcolor(id);
		
		var frames = Math.round(fps * (duration / 1000));
		var interval = duration / frames;
		var delay = interval;
		var frame = 0;
		
		if (from.length < 7) from += from.substr(1,3);
		if (to.length < 7) to += to.substr(1,3);
		
		var rf = parseInt(from.substr(1,2),16);
		var gf = parseInt(from.substr(3,2),16);
		var bf = parseInt(from.substr(5,2),16);
		var rt = parseInt(to.substr(1,2),16);
		var gt = parseInt(to.substr(3,2),16);
		var bt = parseInt(to.substr(5,2),16);
		
		var r,g,b,h;
		while (frame < frames)
		{
			r = Math.floor(rf * ((frames-frame)/frames) + rt * (frame/frames));
			g = Math.floor(gf * ((frames-frame)/frames) + gt * (frame/frames));
			b = Math.floor(bf * ((frames-frame)/frames) + bt * (frame/frames));
			h = this.make_hex(r,g,b);
		
			setTimeout("Fat.set_bgcolor('"+id+"','"+h+"')", delay);

			frame++;
			delay = interval * frame; 
		}
		setTimeout("Fat.set_bgcolor('"+id+"','"+to+"')", delay);
	},
	set_bgcolor : function (id, c)
	{
		var o = document.getElementById(id);
		o.style.backgroundColor = c;
	},
	get_bgcolor : function (id)
	{
		var o = document.getElementById(id);
		while(o)
		{
			var c;
			if (window.getComputedStyle) c = window.getComputedStyle(o,null).getPropertyValue("background-color");
			if (o.currentStyle) c = o.currentStyle.backgroundColor;
			if ((c != "" && c != "transparent") || o.tagName == "BODY") { break; }
			o = o.parentNode;
		}
		if (c == undefined || c == "" || c == "transparent") c = "#FFFFFF";
		var rgb = c.match(/rgb\s*\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)/);
		if (rgb) c = this.make_hex(parseInt(rgb[1]),parseInt(rgb[2]),parseInt(rgb[3]));
		return c;
	}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function toggleOn(objectID) {
	var object = document.getElementById(objectID);
	object.style.display = 'block';
}
function toggleOff(objectID) {
	var object = document.getElementById(objectID);
	object.style.display = 'none';
}
function toggleDisplay(objectID) {
	var object = document.getElementById(objectID);
	state = object.style.display;
	if (state == 'none')
		object.style.display = 'block';
	else if (state != 'none')
		object.style.display = 'none'; 
}
function sendfriendemail() {
	var n = document.getElementById("name").value;
	var e = document.getElementById("email").value;
	var m = document.getElementById("message").value;
	var p = document.getElementById("home_id").value;
	x_sendfriendemail(n, e, m, p, sendfriendemail_cb);
}
function sendfriendemail_cb(z) {
	document.getElementById("msg").innerHTML = z;
	Fat.fade_all();
}
function zipcode_look(zip) {
	x_zipcode_look(zip, zipcode_look_cb);
}
function zipcode_look_cb(z) {
	a = z.split(":");
	document.contact_form.city.value = a[0];
	document.contact_form.state.options[a[1]].selected = true;		
}
function showLoading(theOption) {
	var s = document.getElementById(theOption);
	s.length = 0;
	s.options[0] = new Option("loading...", 0);	
}
function loadOptionBox(theOption, name, id, initial) {	
	var o = document.getElementById(theOption);
	o.options[0] = new Option(initial, 0);
	o.options[o.length] = new Option(name, id);
	o.options[0].selected = true;	
}
function changeGoButton(v) {
	if (v == 0) v = "the";
	var text = "View " + v + " Home";
	if (v > 1 || v == "the") text += "s";
	document.getElementById("go").value = text;
	document.getElementById("resultsmsg").innerHTML = "";
	document.getElementById("results").innerHTML = "";
}
function getCategoryOptions(v) {
	showLoading("type");
	showLoading("sq_foot");
	showLoading("stories");
	showLoading("bedrooms");
	showLoading("bathrooms");
	showLoading("garage");
	x_getCategoryOptions(v, getCategoryOptions_cb);
}
function getCategoryOptions_cb(z) {
//alert(z);
	var o = z.split("|");
	outputOptions(o[0], "type", "", "Single or Multi");
	outputOptions(o[1], "sq_foot", " sq ft", "Any Sq Footage");
	outputOptions(o[2], "stories", " Stories", "Number of Stories");
	outputOptions(o[3], "bedrooms", " Bedrooms", "Bedrooms");
	outputOptions(o[4], "bathrooms", " Bathrooms", "Bathrooms");
	outputOptions(o[5], "garage", " Car Garage", "Garage Spaces");
	document.getElementById("resultsmsg").innerHTML = "";
	document.getElementById("results").innerHTML = "";
	document.getElementById("go").value = "View the Homes";
}
function getTypeOptions(v) {
	if (v == 0 || v == "Single or Multi") {
		getCategoryOptions(document.getElementById("category").value);
		return;
	}
	showLoading("sq_foot");
	showLoading("stories");
	showLoading("bedrooms");
	showLoading("bathrooms");
	showLoading("garage");
	x_getTypeOptions(document.getElementById("category").value, v, getTypeOptions_cb);
}
function getTypeOptions_cb(z) {
	var o = z.split("|");
	outputOptions(o[0], "sq_foot", " sq ft", "Any Sq Footage");
	outputOptions(o[1], "stories", " Stories", "Number of Stories");
	outputOptions(o[2], "bedrooms", " Bedrooms", "Bedrooms");
	outputOptions(o[3], "bathrooms", " Bathrooms", "Bathrooms");
	outputOptions(o[4], "garage", " Car Garage", "Garage Spaces");
	changeGoButton(o[5]);
}
function getSqFootOptions(v) {
	if (v == 0) {
		getCategoryOptions(document.getElementById("category").value);
		return;
	}
	showLoading("stories");
	showLoading("bedrooms");
	showLoading("bathrooms");
	showLoading("garage");
	x_getSqFootOptions(document.getElementById("category").value, document.getElementById("type").value, v, getSqFootOptions_cb);
}
function getSqFootOptions_cb(z) {
	var o = z.split("|");
	outputOptions(o[0], "stories", " Stories", "Number of Stories");
	outputOptions(o[1], "bedrooms", " Bedrooms", "Bedrooms");
	outputOptions(o[2], "bathrooms", " Bathrooms", "Bathrooms");
	outputOptions(o[3], "garage", " Car Garage", "Garage Spaces");
	changeGoButton(o[4]);
}
function getStoriesOptions(v) {
	if (v == 0) {
		getCategoryOptions(document.getElementById("category").value);
		return;
	}
	showLoading("bedrooms");
	showLoading("bathrooms");
	showLoading("garage");
	x_getStoriesOptions(document.getElementById("category").value, document.getElementById("type").value, document.getElementById("sq_foot").value, v, getStoriesOptions_cb);
}
function getStoriesOptions_cb(z) {
	var o = z.split("|");
	outputOptions(o[0], "bedrooms", " Bedrooms", "Bedrooms");
	outputOptions(o[1], "bathrooms", " Bathrooms", "Bathrooms");
	outputOptions(o[2], "garage", " Car Garage", "Garage Spaces");
	changeGoButton(o[3]);
}
function getBedroomOptions(v) {
	if (v == 0) {
		getCategoryOptions(document.getElementById("category").value);
		return;
	}
	showLoading("bathrooms");
	showLoading("garage");
	x_getBedroomOptions(document.getElementById("category").value, document.getElementById("type").value, document.getElementById("sq_foot").value, document.getElementById("stories").value, v, getBedroomOptions_cb);
}
function getBedroomOptions_cb(z) {
	var o = z.split("|");
	outputOptions(o[0], "bathrooms", " Bathrooms", "Bathrooms");
	outputOptions(o[1], "garage", " Car Garage", "Garage Spaces");
	changeGoButton(o[2]);	
}
function getBathroomOptions(v) {
	if (v == 0) {
		getCategoryOptions(document.getElementById("category").value);
		return;
	}
	showLoading("garage");
	x_getBathroomOptions(document.getElementById("category").value, document.getElementById("type").value, document.getElementById("sq_foot").value, document.getElementById("stories").value, document.getElementById("bedrooms").value, v, getBathroomOptions_cb);
}
function getBathroomOptions_cb(z) {
	var o = z.split("|");
	outputOptions(o[0], "garage", " Car Garage", "Garage Spaces");
	changeGoButton(o[1]);	
}
function getGarageOptions(v) {
	x_getGarageOptions(document.getElementById("category").value, document.getElementById("type").value, document.getElementById("sq_foot").value, document.getElementById("stories").value, document.getElementById("bedrooms").value, document.getElementById("bathrooms").value, v, getGarageOptions_cb);	
}
function getGarageOptions_cb(z) {
	changeGoButton(z);
}
function getResults() {
	var c = document.getElementById("category").value;
	var t = document.getElementById("type").value;
//alert(t);
	x_getResults(c, t, document.getElementById("sq_foot").value, document.getElementById("stories").value, document.getElementById("bedrooms").value, document.getElementById("bathrooms").value, document.getElementById("garage").value, getResults_cb);
}
function getResults_cb(z) {
	var r = z.split("|");	
	document.getElementById("resultsmsg").innerHTML = r[0];
	document.getElementById("results").innerHTML = r[1];
}
function outputOptions(theString, theOption, extra, initial) {
	var x = theString.split(",");
	for (i = 0; i < x.length; i++) {
		loadOptionBox(theOption, x[i]+extra, x[i], initial);
	}
}
function floor(number) {
  return Math.floor(number*Math.pow(10,2))/Math.pow(10,2);
}

function dosum() {
  var mi = document.temps.IR.value / 1200;
  var base = 1;
  var mbase = 1 + mi;
  for (i=0; i<document.temps.YR.value * 12; i++) {
    base = base * mbase
  }
  document.temps.PI.value = floor(document.temps.LA.value * mi / ( 1 - (1/base)))
  document.temps.MT.value = floor(document.temps.AT.value / 12)
  document.temps.MI.value = floor(document.temps.AI.value / 12)
  var dasum = document.temps.LA.value * mi / ( 1 - (1/base)) +
	document.temps.AT.value / 12 + 
	document.temps.AI.value / 12;
  document.temps.MP.value = floor(dasum);
}
function showTheSpecs(v) {
	x_showTheSpecs(v, showTheSpecs_cb);
}
function showTheSpecs_cb(z) {
	document.getElementById("specs").innerHTML = z;
}
