var url = "/javascript/request.php"; // The server-side script
var result;

function handleHttpResponseS() {
	document.getElementById('liveResults').innerHTML = '<blink>Searching...</blink>';
	if (http.readyState == 4) {
	    if (http.responseText) {
			document.getElementById('liveResults').innerHTML = http.responseText;
			result = http.responseText;
		}
	}
}

function handleHttpResponseR() {
    document.getElementById('liveResultsR').innerHTML = '<blink>Searching...</blink>';
    if (http.readyState == 4) {
        if (http.responseText) {
            document.getElementById('liveResultsR').innerHTML = http.responseText;
            result = http.responseText;
        }
    }
}

function handleHttpResponseB() {
	document.getElementById('liveResultsB').innerHTML = '<blink>Searching...</blink>';
    if (http.readyState == 4) {
        if (http.responseText) {
            document.getElementById('liveResultsB').innerHTML = http.responseText;
            result = http.responseText;
        }
    }
}

function checkResult()
{
		if (result > 50)
		{
			alert("The result list is too large. Please refine your research.");
			return false;
		}
}

function callInProgress(http) {
    switch (http.readyState) {
        case 1, 2, 3:
            return true;
	        break;

        // Case 4 and 0
        default:
            return false;
		    break;
    }
}

function request(target) {
    var state = '';
	var continent = '';
	var country = '';
	var city = '';
	var neighborhood = '';
	var SalesStatus = '';
	var newProperty = '';

	for (i=0; i<target.elements.length; i++) {
		var field = target.elements[i];
		if (field.name == "Continent[]" || field.name == "Continent") {
			if (field.name.indexOf("[]") > 0) {
				continent += "&Continent[]=" + field.value;
			} else {
	    	    continent = "&Continent=" + field.value;
			}
	    }
        if ((field.name == "s[]" || field.name == "s") && field.disabled == false) {
			if (field.name.indexOf("[]") > 0) {
                state += "&s[]=" + field.value;
            } else {
	            state = "&s=" + field.value;
			}
        }
		if ((field.name == "co[]" || field.name == "co") && field.disabled == false) {
			if (field.name.indexOf("[]") > 0) {
                country += "&co[]=" + field.value;
            } else {
	    	    country = "&co=" + field.value;
			}
	    }
		if ((field.name == "Neighborhood[]" || field.name == "Neighborhood") && field.disabled == false) {
			if (field.name.indexOf("[]") > 0) {
                neighborhood += "&Neighborhood[]=" + field.value;
            } else {
	    	    neighborhood = "&Neighborhood=" + field.value;
			}
	    }
		if ((field.name == "ci[]" || field.name == "ci") && field.disabled == false) {
			if (field.name.indexOf("[]") > 0) {
                city += "&ci[]=" + field.value;
            } else {
	            city = "&ci=" + field.value;
			}
        }
		if (field.name == "SalesStatus") {
            status = "&SalesStatus=" + field.value;
        }
		if ((field.name == "NewProperty") && (field.checked == true)) {
            newProperty = "&NewProperty=" + field.checked;
        }
    }
	var keyword = target.k.value;
	if (target.id == "sForm" || target.id == "rForm") {
	    var category = target.c.value;
	    var mnp = target.mnp.value;
	    var mxp = target.mxp.value;
		var t = target.T.value;
	}
	if (target.id == "bForm") {
        var keywordAdditional = '';
        var isBroker = '';
        var isAgent = '';
		if (target.isBroker.type != 'checkbox') {
			isBroker = "&isBroker=1";
			isAgent = "&isAgent=1";
		} else {
            if (target.isBroker.checked == true) {
                isBroker = "&isBroker=1";
            }
            if (target.isAgent.checked == true) {
    	        isAgent = "&isAgent=1";
            }
		}
        if (target.KeywordAdditional.type != 'checkbox') {
            keywordAdditional = "&KeywordAdditional=1";
        } else {
            if (target.KeywordAdditional.checked == true) {
                keywordAdditional = "&KeywordAdditional=1";
            }
        }
	}
	if (target.id == "sForm" || target.id == "rForm") {
		http.open("GET", url + "?LREApproved=1&k=" + keyword + country + continent + state + city + neighborhood + "&c=" + category + "&mnp=" + mnp + "&mxp=" + mxp + "&T=" + t + status + newProperty, true);
		//alert( url + "?LREApproved=1&k=" + keyword + country + continent + state + city + neighborhood + "&c=" + category + "&mnp=" + mnp + "&mxp=" + mxp + "&T=" + t + status + newProperty);
		if (target.id == "sForm") {
			http.onreadystatechange = handleHttpResponseS;
		} else {
			http.onreadystatechange = handleHttpResponseR;
		}
	} else if (target.id == "bForm") {
//alert(url + "?k=" + keyword + country + continent + state + city + isBroker + isAgent + keywordAdditional);
		http.open("GET", url + "?k=" + keyword + country + continent + state + city + isBroker + isAgent + keywordAdditional, true);
		http.onreadystatechange = handleHttpResponseB;
	}
	if (!callInProgress(http)) {
		http.send(null);
	} else {
		http.abort();
	}
	
}

function mapRequest(target) {
    var category = target.c.value;
    var mnp = target.mnp.value;
    var mxp = target.mxp.value;
    var keyword = target.k.value;
	http.open("GET", url + "?co=USA" + "&s=MI" + "&c=" + category + "&mnp=" + mnp + "&mxp=" + mxp + "&k=" + keyword, true);
	http.onreadystatechange = handleHttpResponse;
	http.send(null);
}


function getHTTPObject() {
	var xmlhttp;
	/*@cc_on 
	@if (@_jscript_version >= 5)
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); 
		} catch (e) { 
			try { 
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
			} catch (E) { 
				xmlhttp = false; 
			} 
		} 
	@else 
	xmlhttp = false; 
	@end @*/  
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') { 
		try { 
			xmlhttp = new XMLHttpRequest(); 
		} catch (e) { 
			xmlhttp = false; 
		} 
	} 
	return xmlhttp; 
} 

function getResult(daForm) {
    if (document.getElementById(daForm)) {
        var searchForm = document.getElementById(daForm);
        request(searchForm);
    }
}

var http = getHTTPObject(); // create the HTTP Object


