/*
Coder: John Marchiano
*/

var xcoord;
var ycoord;

function loadXMLDoc(dname)
{
var xmlDoc;
if (window.XMLHttpRequest)
  {
  xmlDoc=new window.XMLHttpRequest();
  xmlDoc.open("GET",dname,false);
  xmlDoc.send("");
  return xmlDoc.responseXML;
  }
// IE 5 and IE 6
else if (ActiveXObject("Microsoft.XMLDOM"))
  {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async=false;
  xmlDoc.load(dname);
  return xmlDoc;
  }
alert("Error loading document");
return null;
}

var url = "";

var vsmain = document.getElementById ('vsearch');
//alert (main);

//var droot = main.parentNode;
//droot.onscoll = function () { main.style.top = droot.scrollTop; }
//main.style.border = 'dashed black 3px';
vsmain.style.position = "absolute";
var aw = screen.availWidth;
//var mw = main.offsetWidth;
//alert ('mw := ' + mw);
//alert ('aw := ' + String(aw / 2));
//main.style.left = String ((aw / 2)) + 'px';
//main.style.left = (String (Math.round (aw / 2 - mw)) + 'px');
var stmain = document.getElementById ('stmain');
//alert ('stmain.offsetWidth := ' + stmain.offsetWidth);
vsmain.style.width = String (stmain.offsetWidth) + "px";
//alert ('main.width := ' + main.offsetWidth);
var mw = vsmain.offsetWidth;
//alert (aw / 2 - mw);
vsmain.style.left = String (aw / 2 - mw / 2) + "px";
vsmain.style.zIndex = 999999;
//alert (main.style.left);

var town_header = document.getElementById ('town_header');
var town_handle = document.getElementById ('town_handle');
var extra_header = document.getElementById ('extra_header');
var bathrooms_prices = document.getElementById ('bathrooms_prices');
var prop_types = document.getElementById ('prop_types');
var cmds = document.getElementById ('r4_tmain');

town_header.style.visibility = "hidden";
town_handle.style.visibility = "hidden";
extra_header.style.visibility = "hidden";
bathrooms_prices.style.visibility = "hidden";
prop_types.style.visibility = "hidden";
cmds.style.visibility = "hidden";

function is_ie () {

         var bn = navigator.appName;
         return (bn.match (/netscape/i) == null);

}

function drag_trigger (vsobj, event) {

         if (document.getElementById ('dragon')) {
            vsobj.removeChild (document.getElementById ('dragon'));
         }
         vsobj.style.cursor = "move";
         var drag = document.createElement ('input');
         drag.setAttribute ('type', 'hidden');
         drag.setAttribute ('id', 'dragon');
         drag.setAttribute ('value', '1');
         vsobj.appendChild (drag);

}

function drag_coords (vsobj, event) {

         if (document.getElementById ('dragon')) {
            //vsobj.style.left = String (event.clientX) + "px";
            //vsobj.style.top = String (event.clientY) + "px";
            xcoord = event.clientX;
            ycoord = event.clientY;
            document.body.onmousemove = function (event) { vsobj.style.left = String (event.clientX) + "px"; vsobj.style.top = String (event.clientY) + "px"; }
            document.body.onmouseup = function () { document.body.onmousemove = ""; vsobj.style.cursor = "default"; nodrag (vsobj); }
            //document.body.onmouseup = drag_set (vsobj, event);
         }
         var t = setTimeout ("drag_coords (vsobj, event)", 0);
}

/*
function drag_set (vsobj, event) {
         if (document.getElementById ('dragon')) {
           vsobj.removeChild (document.getElementById ('dragon'));
         }
         
         document.body.onmousemove = "";
         vsobj.style.cursor = "default";
         alert ("x := " + xcoord + "\n\ny := " + ycoord);
         //document.body.onmouseup = nodrag (vsobj);
}
*/

function nodrag (vsobj) {
         if (document.getElementById ('dragon')) {
            vsobj.removeChild (document.getElementById ('dragon'));
         }
}

//alert (is_ie ());

function minimize () {

         var stmain = document.getElementById ('stmain');
         var rows = stmain.rows;
         //alert (is_ie ());

         for (var i = 1; i < rows.length; i++) {
             try {
                 if (!is_ie ()) {
                    rows[i].style.visibility = 'collapse';
                    //alert ('rows[' + i + '].style.visibility := ' + rows[i].style.visibility);
                 } else {
                    rows[i].style.display = 'none';
                 }
             } catch (ex) {
               rows[i].style.display = 'none';
             }
         }

}

function maximize () {

	var stmain = document.getElementById ('stmain');
	var rows = stmain.rows;
			
        for (var i = 1; i < rows.length; i++) {
		if (rows[i].style.display == 'none') {
			rows[i].style.display = '';
		} else {
			rows[i].style.visibility = 'visible';
		}
	}
			
        cmds.style.visibility = (!document.getElementById ('xtown') ? 'hidden' : 'visible');

}

function exit () {
	var main = document.getElementById ('vsearch');
	/*var droot = main.parentNode;
	droot.removeChild (main);*/
	main.style.display = "none";
}

function proc_county (c) {
        //alert (c);
	if (document.getElementById ('xcounty')) {
		document.getElementById ('xcounty').setAttribute ('value', c.options[c.selectedIndex].value);
	} else {
		var x = document.createElement ('input');
		x.setAttribute ('type', 'hidden');
		x.setAttribute ('id', 'xcounty');
		x.setAttribute ('name', 'xcounty');
		x.setAttribute ('value', c.options[c.selectedIndex].value);
		document.getElementById ('vsearch').appendChild (x);
	}

	var seltown = document.getElementById ('town');
	//alert (seltown);
	seltown.options.length = 1;

	if (c.selectedIndex > 0) {
		var xmlDoc = loadXMLDoc ("/float_search/vtowns.php?cc=" + c.options[c.selectedIndex].value);
		//alert (xmlDoc);
		var towns = xmlDoc.getElementsByTagName ('town');
		for (var i = 0; i < towns.length; i++) {
			var nto = document.createElement ('option');
			nto.setAttribute ('value', String (towns[i].getAttribute ('tid')));
			nto.innerHTML = String (towns[i].childNodes[0].nodeValue);
			seltown.appendChild (nto);
		}
	}

	town_header.style.visibility = "visible";
	town_handle.style.visibility = "visible";

}

function proc_town (t) {

	if (document.getElementById ('xtown')) {
		document.getElementById ('xtown').setAttribute ('value', t.options[t.selectedIndex].value);
	} else {
		var y = document.createElement ('input');
		y.setAttribute ('type', 'hidden');
		y.setAttribute ('id', 'xtown');
		y.setAttribute ('name', 'xtown');
		y.setAttribute ('value', t.options[t.selectedIndex].value);
		//alert (y);
		document.getElementById ('vsearch').appendChild (y);
	}
	
        extra_header.style.visibility = "visible";
	bathrooms_prices.style.visibility = "visible";
	prop_types.style.visibility = "visible";
	cmds.style.visibility = "visible";

}

function reset () {

	var xcounty = document.getElementById ('xcounty');
	var xtown = document.getElementById ('xtown');
	xcounty.parentNode.removeChild (xcounty);
	xtown.parentNode.removeChild (xtown);
	var cs = document.getElementById ('county');
	var ts = document.getElementById ('town');
	cs.selectedIndex = 0;
	ts.selectedIndex = 0;
	var lp = document.getElementById ('lo_price');
	var hp = document.getElementById ('hi_price');
	var fb = document.getElementById ('full_bathrooms');
	var hb = document.getElementById ('half_bathrooms');
	lp.selectedIndex = 0;
	hp.selectedIndex = 0;
	fb.selectedIndex = 0;
	hb.selectedIndex = 0;
	var pt = document.getElementById ('prop_frm');
	pt = pt.type;

	for (var i = 0; i < pt.length; i++) {
		if (pt[i].checked) {
			pt[i].checked = false;
		}
	}

	town_header.style.visibility = "hidden";
	town_handle.style.visibility = "hidden";
	extra_header.style.visibility = "hidden";
	bathrooms_prices.style.visibility = "hidden";
	prop_types.style.visibility = "hidden";
	cmds.style.visibility = "hidden";

}

function search () {

	var cc = document.getElementById ('xcounty');
	var tc = document.getElementById ('xtown');
	var lp = document.getElementById ('lo_price');
	var hp = document.getElementById ('hi_price');
	var fb = document.getElementById ('full_bathrooms');
	var hb = document.getElementById ('half_bathrooms');
	var pt = document.getElementById ('prop_frm');
	pt = pt.type;

	var b = false;
	var pinx = -1;

	var x = Number (lp.options[lp.selectedIndex].value);
	var y = Number (hp.options[hp.selectedIndex].value);

	if (Number (cc.value) == -1) {
		alert ('You must select a county!');
		return;
	}

	if (Number (tc.value) == -1) {
		alert ('You must select a town!');
		return;
	}

	if (x > y && x != -1 && y != -1) {
		alert ('The low price cannot be greater than the high price!');
		return;
	}

	for (var i = 0; i < pt.length; i++) {
		if (pt[i].checked) {
			pinx = i;
			b = true;
			break;
		}
	}

	if (!b) {
		alert ('Property type must be selected!');
		return;
	}

	var output = '';
	output += ('county := ' + String (cc.value) + '\n');
	output += ('town := ' + String (tc.value) + '\n');
	output += ('lo price := ' + String (lp.options[lp.selectedIndex].value) + '\n');
	output += ('hi price := ' + String (hp.options[hp.selectedIndex].value) + '\n');
	output += ('full bath := ' + String (fb.options[fb.selectedIndex].value) + '\n');
	output += ('half bath := ' + String (hb.options[hb.selectedIndex].value) + '\n');
	//output += String (pt.length);
	output += ('property := ' + pt[pinx].value);
	
	//alert (output);


	var sfrm = document.createElement ('form');
	sfrm.setAttribute ('method', 'post');
	sfrm.setAttribute ('action', '/search.php#nresults');

	var fcc = document.createElement ('input');
	fcc.setAttribute ('type', 'hidden');
	fcc.setAttribute ('name', 'county');
	fcc.setAttribute ('value', String (cc.value));

	var ftc = document.createElement ('input');
	ftc.setAttribute ('type', 'hidden');
	ftc.setAttribute ('name', 'town');
	ftc.setAttribute ('value', String (tc.value));

	var flp = document.createElement ('input');
	flp.setAttribute ('type', 'hidden');
	flp.setAttribute ('name', 'lo_price');
	flp.setAttribute ('value', String (lp.options[lp.selectedIndex].value));

	var fhp = document.createElement ('input');
	fhp.setAttribute ('type', 'hidden');
	fhp.setAttribute ('name', 'hi_price');
	fhp.setAttribute ('value', String (hp.options[hp.selectedIndex].value));

	var ffb = document.createElement ('input');
	ffb.setAttribute ('type', 'hidden');
	ffb.setAttribute ('name', 'full_bathrooms');
	ffb.setAttribute ('value', String (fb.options[fb.selectedIndex].value));

	var fhb = document.createElement ('input');
	fhb.setAttribute ('type', 'hidden');
	fhb.setAttribute ('name', 'half_bathrooms');
	fhb.setAttribute ('value', String (hb.options[hb.selectedIndex].value));

        var ptt = document.createElement ('input');
        ptt.setAttribute ('type', 'hidden');
        ptt.setAttribute ('name', 'type');
        ptt.setAttribute ('value', pt[pinx].value);

        var psf = document.createElement ('input');
        psf.setAttribute ('type', 'hidden');
        psf.setAttribute ('name', 'do_search');
        psf.setAttribute ('value', '1');

	sfrm.appendChild (fcc);
	sfrm.appendChild (ftc);
	sfrm.appendChild (flp);
	sfrm.appendChild (fhp);
	sfrm.appendChild (ffb);
	sfrm.appendChild (fhb);
	sfrm.appendChild (ptt);
	sfrm.appendChild (psf);

        document.getElementById ('vsearch').appendChild (sfrm);
	sfrm.submit ();

}