function Ajax() {}

Ajax.RestUrl = '/geosearch/';
Ajax.operationInProgress = false;


//================================================================================
Ajax.getLocationFromIP = function(callbackFunction)
{
        if (Ajax.operationInProgress) return;
        var req = Ajax.getHTTPRequest();
        req.open('POST', Ajax.RestUrl, true);
        req.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
        req.onreadystatechange = function() { Ajax.callback(req, callbackFunction); };
        Ajax.operationInProgress = true;
           req.send('type=ip&csrfmiddlewaretoken=' + CSRF_TOKEN);
};


//================================================================================
Ajax.searchCountry = function(country, lang, maxResults, callbackFunction) {
        var req = Ajax.getHTTPRequest();
        req.open('POST', Ajax.RestUrl, true);
        req.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
        req.onreadystatechange = function() { Ajax.callback(req, callbackFunction); };
        req.send('type=country&country=' + encodeURIComponent(country) +
                        '&lang=' + lang + '&max=' + maxResults + '&csrfmiddlewaretoken=' + CSRF_TOKEN);
};


//================================================================================
Ajax.getCountries = function(lang, callbackFunction) {
        var req = Ajax.getHTTPRequest();
        req.open('POST', Ajax.RestUrl, true);
        req.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
        req.onreadystatechange = function() { Ajax.callback(req, callbackFunction); };
        req.send('type=country&lang=' + lang + '&csrfmiddlewaretoken=' + CSRF_TOKEN);
};


//================================================================================
Ajax.searchLocationByPoint = function(longitude, latitude,
                                                                        resolution, maxResults,
                                                                        lang, callbackFunction)
{
        if (Ajax.operationInProgress) return;
        var req = Ajax.getHTTPRequest();
        req.open('POST', Ajax.RestUrl, true);
        req.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
        req.onreadystatechange = function() { Ajax.callback(req, callbackFunction); };
        Ajax.operationInProgress = true;
           req.send('type=point' +
                           '&longitude=' + encodeURIComponent(longitude) +
                           '&latitude=' + encodeURIComponent(latitude) +
                           '&resolution=' + encodeURIComponent(resolution) +
                           '&lang=' + lang +
                           '&max=' + maxResults + '&csrfmiddlewaretoken=' + CSRF_TOKEN);
};


//================================================================================
Ajax.searchLocationByName = function(searchQuery, country, maxResults, lang, callbackFunction)
{
        if (Ajax.operationInProgress) return;
        var req = Ajax.getHTTPRequest();
        req.open('POST', Ajax.RestUrl, true);
        req.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
        req.onreadystatechange = function() { Ajax.callback(req, callbackFunction); };
        Ajax.operationInProgress = true;
           req.send('type=name' +
                           '&query=' + encodeURIComponent(searchQuery) +
                           '&country=' + encodeURIComponent(country) +
                           '&max=' + maxResults + '&lang=' + lang + '&csrfmiddlewaretoken=' + CSRF_TOKEN);
};


//================================================================================
Ajax.searchLocationFromText = function(searchQuery, maxResults, lang, callbackFunction)
{
        if (Ajax.operationInProgress) return;
        var req = Ajax.getHTTPRequest();
        req.open('POST', Ajax.RestUrl, true);
        req.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
        req.onreadystatechange = function() { Ajax.callback(req, callbackFunction); };
        Ajax.operationInProgress = true;
           req.send('type=text' +
                           '&query=' + encodeURIComponent(searchQuery) +
                           '&max=' + maxResults + '&lang=' + lang + '&csrfmiddlewaretoken=' + CSRF_TOKEN);
};


//================================================================================
Ajax.getHTTPRequest = function() {
        var xmlHttp = null;
        try {
                xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
                try {
                        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch (e2) {
                        xmlHttp = null;
                }
        }
        if (xmlHttp == null && typeof(XMLHttpRequest) != 'undefined') xmlHttp = new XMLHttpRequest();
        return xmlHttp;
};


//================================================================================
Ajax.callback = function(req, callbackFunction) {
        Ajax.operationInProgress = false;
        if (req.readyState == 4) {
                callbackFunction(req.status == 200 ? Ajax.parseResponseText(req.responseText) : null);
        }
};


//================================================================================
Ajax.parseResponseText = function(responseText) {
        if (typeof responseText != 'string' ||
                responseText === null ||
                responseText.length == 0)
        {
                return null;
        }

        var response = null;
        try {
                response = eval('(' + responseText + ')');
        }
        catch (e) {
                debugout('Exception when parsing response text, ' + e);
                debugout(responseText);
        }
        return response;
};
