var req;
var isIE = false;

var disabled_search = new Image(227, 53); disabled_search.src = "img/content/startsearch_disabled_btn.jpg";

function allSearchFieldsFilled () {
	var l = document.getElementById("select_location"); var l_value = l.value;
	var nu = document.getElementById("select_new_used"); var nu_value = nu[nu.selectedIndex].value;
	var vt = document.getElementById("select_vehicle_type"); var vt_value = vt[vt.selectedIndex].value;
	var vm = document.getElementById("vehiclemake"); var vm_value = vm[vm.selectedIndex].value;
	var vpl = document.getElementById("price_r_low"); var vpl_value = vpl[vpl.selectedIndex].value;
	var vph = document.getElementById("price_r_high"); var vph_value = vph[vph.selectedIndex].value;
	var l_valid = l_value == "" ? false : true;
	var nu_valid = nu_value == "" ? false : true;
	var vt_valid = vt_value == "" ? false : true;
	var vph_valid = vph_value > vpl_value ? true :false;
	var vm_valid = vm_value == "" ? false : true;
	
	return (l_valid && nu_valid && vt_valid && vph_valid) ? true : false;
}

function checkSearchButton () {
	b = document.getElementById("startsearch");
	if (allSearchFieldsFilled()) {
		b.src = "img/content/startsearch_btn.jpg";
		//b.disabled = false;
	} else {
		b.src = "img/content/startsearch_disabled_btn.jpg";
		//b.disabled = true;
	}
}

function init () {
	checkSearchButton();
	
	if (gotLocation) {
		highlightStep("step2");
		highlightMenu(document.getElementById("price_r_high"));
	} else {
		highlightStep("step1");
		highlightMenu(document.getElementById("select_location"));
	}
}

function highlightStep (step) {
	var highlight_color = "#faf6e4";
	var non_highlight_color = "#fff";
	var el = document.getElementById(step);
	var step1 = document.getElementById("step1");
	var step2 = document.getElementById("step2");
	var step3 = document.getElementById("step3");
	
	if (el == step1) {
		step1.style.backgroundColor = highlight_color;
		step2.style.backgroundColor = non_highlight_color;
		step3.style.backgroundColor = non_highlight_color;
	} else if (el == step2) {
		step1.style.backgroundColor = non_highlight_color;
		step2.style.backgroundColor = highlight_color;
		step3.style.backgroundColor = non_highlight_color;
	} else if (el == step3) {
		step1.style.backgroundColor = non_highlight_color;
		step2.style.backgroundColor = non_highlight_color;
		step3.style.backgroundColor = highlight_color;
	}
}

function highlightMenu (el) {
	//el.style.backgroundColor = "#fbdd5b";
	var f = document.getElementById("theform");
	
	for (var i=0; i < f.elements.length; i++) {
		if (f.elements[i].tagName.toLowerCase() == "select") {
			if (f.elements[i] == el) {
				f.elements[i].style.backgroundColor = "#fcd000";
			} else {
				resetColor(f.elements[i]);
			}
		}
	};
	// el.style.backgroundColor = "#fcd000";
}

function loadXMLDoc (url) {
	toggleSubmit("disable");
	document.getElementById("wait_wrapper").style.display = "block";
	document.getElementById("the_body").style.cursor = "wait";
	req = false;
	
	if (window.XMLHttpRequest && !(window.ActiveXObject)) {
		try {
			req = new XMLHttpRequest();
		} catch (e) {
			req = false;
		}
	} else if (window.ActiveXObject) {
		isIE = true;
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				req = false;
			}
		}
	}
	
	if (req) {
		req.onreadystatechange = processReqChange;
		req.open("GET", url, true);
		req.send("");
	}
}

function getElementTextNS(prefix, local, parentElem, index) {
    var result = "";
    if (prefix && isIE) {
        // IE/Windows way of handling namespaces
        result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
    } else {
        result = parentElem.getElementsByTagName(local)[index];
    }
    if (result) {
        // get text, accounting for possible
        // whitespace (carriage return) text nodes 
        if (result.childNodes.length > 1) {
            return result.childNodes[1].nodeValue;
        } else {
            return result.firstChild.nodeValue;    		
        }
    } else {
        return "n/a";
    }
}

function processReqChange () {
	// only if req shows "loaded"
	if (req.readyState == 4) {
		// only if OK
		if (req.status == 200) {
			clearMakes();
			listMakes();
			toggleSubmit("enable");
			//checkSearchButton();
			document.getElementById("wait_wrapper").style.display = "none";
			document.getElementById("the_body").style.cursor = "auto";
		} else {
			alert("There was a problem building the Available Makes menu.\n" + req.statusText);
		}
	}
}

function clearMakes () {
	var select = document.getElementById("vehiclemake");
	while (select.length > 1) {
		select.remove(1);
	}
}

function appendToSelect(select, value, content) {
    var opt;
    opt = document.createElement("option");
    opt.value = value;
    opt.appendChild(content);
    select.appendChild(opt);
}

function insertSeparator (select) {
	appendToSelect(select, "", document.createTextNode(""));
}

function listMakes () {
	var select = document.getElementById("vehiclemake");
	var p = req.responseXML.getElementsByTagName("make");
	
	//resetColor(select);
	highlightMenu(select);
	
	var area = Array(
		Array("North American", false), 
		Array("European", false), 
		Array("Japanese / Asia", false)
	);
	var makes = Array(p.length);
	for (var i=0; i < p.length; i++) {
		var area_id = getElementTextNS("", "area", p[i], 0);
		var id = getElementTextNS("", "id", p[i], 0);
		var name = getElementTextNS("", "name", p[i], 0);
		
		area[area_id-1][1] = true;
		makes[i] = Array(id, name);
	};
	
	insertSeparator(select); // between static and regions
	
	if (area[0][1] || area[1][1] || area[2][1]) {
		appendToSelect(select, "all", document.createTextNode("All Manufacturers"));
		insertSeparator(select);
		for (var i=0; i < area.length; i++) {
			if (area[i][1] == true){
				appendToSelect(select, (i+1)*-1, document.createTextNode(area[i][0]));
			}
		};
	} else {
		appendToSelect(select, "", document.createTextNode("No makes found,"));
		appendToSelect(select, "", document.createTextNode("please widen your search."));
	}
	
	// if (makes.length > 0) {
	// 	insertSeparator(select); // between regions and individual makes
	// 	
	// 	for (var i=0; i < makes.length; i++) {
	// 	appendToSelect(select, makes[i][0], document.createTextNode(makes[i][1]));
	// 	};
	// }
	
	// for (var i=0; i < p.length; i++) {
	// 	appendToSelect(select, 
	// 		getElementTextNS("", "id", p[i], 0), 
	// 		document.createTextNode(getElementTextNS("", "name", p[i], 0)));
	// };
}

function amIDisabled () {
	var vm = document.getElementById("vehiclemake");
	var vm_value = vm[vm.selectedIndex].value;
	if (allSearchFieldsFilled() && vm_value != "") {
		return false;
	} else {
		// var l = document.getElementById("select_location"); var l_value = l[l.selectedIndex].value;
		var nu = document.getElementById("select_new_used"); var nu_value = nu[nu.selectedIndex].value;
		var vt = document.getElementById("select_vehicle_type"); var vt_value = vt[vt.selectedIndex].value;
		var vpl = document.getElementById("price_r_low"); var vpl_value= vpl[vpl.selectedIndex].value;
		var vph = document.getElementById("price_r_high"); var vph_value = vph[vph.selectedIndex].value;
		// l.style.backgroundColor = l_value == "" ? "#ff9998" : "#fff";
		nu.style.backgroundColor = nu_value == "" ? "#ff9998" : "#fff";
		vt.style.backgroundColor = vt_value == "" ? "#ff9998" : "#fff";
		vm.style.backgroundColor = vm_value == "" ? "#ff9998" : "#fff";
		vph.style.backgroundColor = vph_value > vpl_value ? "#fff" : "#ff9998";
		return true;
	}
}

function makeMenuChanged (el) {
	resetColor(el);
	if (el[el.selectedIndex].value == "") {
		el.selectedIndex = 0;
	}
	
	checkSearchButton();
}

function menuChanged (el) {
	resetColor(el);
	
	if (el[el.selectedIndex].value == "") {
		el.selectedIndex = 0;
	}
	
	if (el.id == "select_location") {
		highlightStep("step2");
		highlightMenu(document.getElementById("price_r_high"));
	}
	
	if (el.id == "price_r_low") {
		highlightStep("step3");
		highlightMenu(document.getElementById("select_new_used"));
	}
	
	if (el.id == "price_r_high") {
		highlightStep("step3");
		highlightMenu(document.getElementById("select_new_used"));
	}
	
	if (el.id == "select_new_used") {
		highlightStep("step3");
		highlightMenu(document.getElementById("select_vehicle_type"));
	}
	
	if (el.id == "select_vehicle_type") {
		highlightStep("step3")
		highlightMenu(document.getElementById("vehiclemake"));
	}
	
	if (el.id == "vehiclemake") {
		highlightStep("step3");
		checkSearchButton();
	}
	
	if (el.id == "price_r_low" || el.id == "price_r_high") {
		var h = document.getElementById("price_r_high");
		var l = document.getElementById("price_r_low");
		if (l.value >= h.value) {
			var newValue = l.value / 10000
			h.selectedIndex = newValue < h.length ? newValue : h.length - 1;
		}
	}
	
	if (allSearchFieldsFilled()) {
		// var l = document.getElementById("select_location"); var l_value = l[l.selectedIndex].value;
		// var nu = document.getElementById("select_new_used"); var nu_value = nu[nu.selectedIndex].value;
		// var vt = document.getElementById("select_vehicle_type"); var vt_value = vt[vt.selectedIndex].value;
		// var pl = document.getElementById("price_r_low"); var pl_value = pl[pl.selectedIndex].value;
		// var ph = document.getElementById("price_r_high"); var ph_value = ph[ph.selectedIndex].value;
		// var url = "vehicleMakes.php?l=" + l_value + "&nu=" + nu_value + "&vt=" + vt_value + "&pl=" + pl_value + "&ph=" + ph_value;
		// //alert(url);
		// loadXMLDoc(url);
		
		highlightStep("step3");
		highlightMenu(document.getElementById("vehiclemake"));
	}
	
	//checkSearchButton();
	//checkMakesMenu();
}

function checkMakesMenu () {
	var m = document.getElementById("vehiclemake");
	var vt = document.getElementById("select_vehicle_type");
	var types = new Array("CAR", "SUV", "TRUCK", "VAN");
	if (allSearchFieldsFilled() && array_search(vt[vt.selectedIndex].value.toUpperCase(), types) != -1) {
		// m.disabled = false;
		// m.style.color = "#000";
	} else {
		//toggleSubmit("disable");
	}
}

function array_search (needle, haystack) {
	var idx = -1;
	for (var i=0; i < haystack.length; i++) {
		if (haystack[i] == needle) idx = i;
	};
	
	return idx;
}

function toggleSubmit (action) {
	var b = document.getElementById("startsearch");
	var m = document.getElementById("vehiclemake");
	if (action == "enable") {
		b.disabled = false;
		//b.src = "img/content/startsearch_btn.jpg";
		m.disabled = false;
		m.style.color = "#333";
	} else {
		b.disabled = true;
		//b.src = "img/content/startsearch_disabled_btn.jpg";
		// m.disabled = true;
		m.style.color = "#999";
	}
}

function resetColor (el) {
	el.style.backgroundColor = "#fff";
}

