﻿/**
* Emulate window.XMLHttpRequest in IE6-
*/
if (!window.XMLHttpRequest) {
    var ms_xhr_ver = false;
    window.XMLHttpRequest = function() {
        if (ms_xhr_ver) return new ActiveXObject(ms_xhr_ver);
        var xhr = false;
        var versions = [
              "Msxml2.XMLHTTP.7.0",
              "Msxml2.XMLHTTP.6.0",
              "Msxml2.XMLHTTP.5.0",
              "Msxml2.XMLHTTP.4.0",
              "MSXML2.XMLHTTP.3.0",
              "MSXML2.XMLHTTP",
              "Microsoft.XMLHTTP"];
        var n = versions.length;
        for (var i = 0; i < n; i++) {
            try {
                if (xhr = new ActiveXObject(versions[i])) {
                    ms_xhr_ver = versions[i];
                    break;
                }
            } catch (e) { /* try next version */ }
        }
        return xhr;
    };
}

function KNKDealerMapping(originLat, originLong, miles, segment, sCountry, serviceURL, callBack) {
    var http_request = new XMLHttpRequest();

    http_request.onreadystatechange = function() {
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {

                if (http_request.getResponseHeader("Content-Type").indexOf("application/JSON") >= 0) {
                    results = eval("(" + http_request.responseText + ")");
                    results.responseText = http_request.responseText;
                    if (results.error != '') {
                        callBack(results);
                        http_request = null;
                        return;
                    }
                    else if (results.dealers.length == 0) {
                        results.error = 'No dealers found in the specified area. Please try increasing the search radius or changing the search location.';
                        callBack(results);
                    }
                    else {
                      callBack(results);
                    }
                }
                else {
                    results = new Object();
                    results.error = 'There was a problem with the map service, please try again later.';
                    ShowResults(results);
                    http_request = null;
                    return;
                }
            }
            else {
                results = new Object();
                results.error = 'There was a problem with the URL (' + http_request.status + ').';
                ShowResults(results);
                http_request = null;
                return;
            }

            http_request = null;
        }
        dealers = null;
    };
    
    var ServiceHost = location.hostname;
    if( ServiceHost != 'localhost' ) {
      ServiceHost = location.hostname.substr(location.hostname.lastIndexOf('.')+1);
    }

    http_request.open('GET', serviceURL + '?lat=' + originLat + '&lon=' + originLong + '&dst=' + miles + '&seg=' + segment + '&sc=' + sCountry + '&sh=' + ServiceHost );
    http_request.send(null);

}

