function createRequestObject()
{
    var ro;
    var browser = navigator.appName;

    if(browser == "Microsoft Internet Explorer")
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    else
        ro = new XMLHttpRequest();

    return ro;
}

var citySearchCount = 0;

function searchCitys()
{
    citySearchCount++;
    var count = citySearchCount;

    var http = createRequestObject();
    http.open('get', '/index.php?output=ajax&task=citys&state=' + document.getElementById('state').value + '&country=' + document.getElementById('country').value + '&name=' + encodeURIComponent(document.getElementById('cityName').value));

    http.onreadystatechange = function ()
    {
        document.getElementsByTagName('body')[0].style.cursor = 'wait';

        if(http.readyState == 4)
        {
            if(http.status == 200)
            {
                if(count == citySearchCount)
                {
                    document.getElementById('citySearch').innerHTML = '';

                    var xml = http.responseXML.documentElement;
                    var citys = xml.getElementsByTagName('r');

                    for(i = 0;i < citys.length;i++)
                    {
                        document.getElementById('citySearch').innerHTML += '<div onmousedown="setCity();" onmouseover="setSelectedCity(this);" onmouseout="unsetSelectedCity();"><span id="' + citys[i].getAttribute('did') + '">' + citys[i].getAttribute('name') + '</span> <span style="color: #AFAFAF; font-size: 9px;">' + citys[i].getAttribute('stateName') + '</span></div>';
                    }

                    document.getElementById('citySearch').style.visibility = 'visible';
                    listFocus = 'citySearch';
                    selectedList = 'city';
                    pressedKeyEvent = 'moveList';
                    unsetSelectedCity();
                }

                document.getElementsByTagName('body')[0].style.cursor = 'default';
            }
        }
    }

    http.send(null);
}

function searchCitysMenu()
{
    citySearchCount++;
    var count = citySearchCount;

    var http = createRequestObject();
    http.open('get', '/index.php?output=ajax&task=citys&state=' + document.getElementById('stateMenu').value + '&country=' + document.getElementById('countryMenu').value + '&name=' + encodeURIComponent(document.getElementById('cityNameMenu').value));

    http.onreadystatechange = function ()
    {
        document.getElementsByTagName('body')[0].style.cursor = 'wait';

        if(http.readyState == 4)
        {
            if(http.status == 200)
            {
                if(count == citySearchCount)
                {
                    document.getElementById('citySearchMenu').innerHTML = '';

                    var xml = http.responseXML.documentElement;
                    var citys = xml.getElementsByTagName('r');

                    for(i = 0;i < citys.length;i++)
                    {
                        document.getElementById('citySearchMenu').innerHTML += '<div onmousedown="setCityMenu();" onmouseover="setSelectedCity(this);" onmouseout="unsetSelectedCity();"><span id="' + citys[i].getAttribute('did') + '">' + citys[i].getAttribute('name') + '</span> <span style="color: #AFAFAF; font-size: 9px;">' + citys[i].getAttribute('stateName') + '</span></div>';
                    }

                    document.getElementById('citySearchMenu').style.visibility = 'visible';
                    listFocus = 'citySearchMenu';
                    selectedList = 'cityMenu';
                    pressedKeyEvent = 'moveList';
                    unsetSelectedCity();
                }

                document.getElementsByTagName('body')[0].style.cursor = 'default';
            }
        }
    }

    http.send(null);
}

function searchCitysGoto()
{
    citySearchCount++;
    var count = citySearchCount;

    var http = createRequestObject();
    http.open('get', '/index.php?output=ajax&task=citys&state=' + document.getElementById('stateGoto').value + '&country=' + document.getElementById('countryGoto').value + '&name=' + encodeURIComponent(document.getElementById('cityNameGoto').value));

    http.onreadystatechange = function ()
    {
        document.getElementsByTagName('body')[0].style.cursor = 'wait';

        if(http.readyState == 4)
        {
            if(http.status == 200)
            {
                if(count == citySearchCount)
                {
                    document.getElementById('citySearchGoto').innerHTML = '';

                    var xml = http.responseXML.documentElement;
                    var citys = xml.getElementsByTagName('r');

                    for(i = 0;i < citys.length;i++)
                    {
                        document.getElementById('citySearchGoto').innerHTML += '<div onmousedown="setCityGoto();" onmouseover="setSelectedCity(this);" onmouseout="unsetSelectedCity();"><span id="' + citys[i].getAttribute('did') + '">' + citys[i].getAttribute('name') + '</span> <span style="color: #AFAFAF; font-size: 9px;">' + citys[i].getAttribute('stateName') + '</span></div>';
                    }

                    document.getElementById('citySearchGoto').style.visibility = 'visible';
                    listFocus = 'citySearchGoto';
                    selectedList = 'cityGoto';
                    pressedKeyEvent = 'moveList';
                    unsetSelectedCity();
                }

                document.getElementsByTagName('body')[0].style.cursor = 'default';
            }
        }
    }

    http.send(null);
}

function searchCitysSearch()
{
    citySearchCount++;
    var count = citySearchCount;

    var http = createRequestObject();
    http.open('get', '/index.php?output=ajax&task=citys&name=' + encodeURIComponent(document.getElementById('cityNameMenu').value));

    http.onreadystatechange = function ()
    {
        document.getElementsByTagName('body')[0].style.cursor = 'wait';

        if(http.readyState == 4)
        {
            if(http.status == 200)
            {
                if(count == citySearchCount)
                {
                    document.getElementById('citySearchSearch').innerHTML = '';

                    var xml = http.responseXML.documentElement;
                    var citys = xml.getElementsByTagName('r');

                    for(i = 0;i < citys.length;i++)
                    {
                        document.getElementById('citySearchSearch').innerHTML += '<div onmousedown="setCitySearch();" onmouseover="setSelectedCity(this);" onmouseout="unsetSelectedCity();"><span id="' + citys[i].getAttribute('did') + '">' + citys[i].getAttribute('name') + '</span> <span style="color: #AFAFAF; font-size: 9px;">' + citys[i].getAttribute('stateName') + '</span></div>';
                    }

                    document.getElementById('citySearchSearch').style.visibility = 'visible';
                    listFocus = 'citySearchSearch';
                    selectedList = 'citySearchSearch';
                    pressedKeyEvent = 'moveList';
                    unsetSelectedCity();
                }

                document.getElementsByTagName('body')[0].style.cursor = 'default';
            }
        }
    }

    http.send(null);
}

function getCityId()
{
    var http = createRequestObject();
    http.open('get', '/index.php?output=ajax&task=citys&equal=1&state=' + document.getElementById('state').value + '&country=' + document.getElementById('country').value + '&name=' + encodeURIComponent(document.getElementById('cityName').value) + '&id=' + encodeURIComponent(document.getElementById('city').value));

    http.onreadystatechange = function ()
    {
        if(http.readyState == 4)
        {
            if(http.status == 200)
            {
                var xml = http.responseXML.documentElement;
                var city = xml.getElementsByTagName('r')[0];

                if(typeof(city)=='undefined')
                    document.getElementById('city').value = '';
                else
                {
                    document.getElementById('city').value = city.getAttribute('id');

                    if(!document.getElementById('state').value)
                        getStateById(city.getAttribute('state'));
                }
            }
        }
    }

    http.send(null);
}

function getCityIdMenu()
{
    var http = createRequestObject();
    http.open('get', '/index.php?output=ajax&task=citys&equal=1&state=' + document.getElementById('stateMenu').value + '&country=' + document.getElementById('countryMenu').value + '&name=' + encodeURIComponent(document.getElementById('cityNameMenu').value) + '&id=' + encodeURIComponent(document.getElementById('cityMenu').value));

    http.onreadystatechange = function ()
    {
        if(http.readyState == 4)
        {
            if(http.status == 200)
            {
                var xml = http.responseXML.documentElement;
                var city = xml.getElementsByTagName('r')[0];

                if(typeof(city)=='undefined')
                    document.getElementById('cityMenu').value = '';
                else
                {
                    document.getElementById('cityMenu').value = city.getAttribute('id');

                    if(!document.getElementById('stateMenu').value)
                        getStateByIdMenu(city.getAttribute('state'));
                }
            }
        }
    }

    http.send(null);
}

function getCityIdGoto()
{
    var http = createRequestObject();
    http.open('get', '/index.php?output=ajax&task=citys&equal=1&state=' + document.getElementById('stateGoto').value + '&country=' + document.getElementById('countryGoto').value + '&name=' + encodeURIComponent(document.getElementById('cityNameGoto').value));

    http.onreadystatechange = function ()
    {
        if(http.readyState == 4)
        {
            if(http.status == 200)
            {
                var xml = http.responseXML.documentElement;
                var city = xml.getElementsByTagName('r')[0];

                if(typeof(city)=='undefined')
                    document.getElementById('cityGoto').value = '';
                else
                {
                    document.getElementById('cityGoto').value = city.getAttribute('id');

                    if(!document.getElementById('stateGoto').value)
                        getStateByIdGoto(city.getAttribute('state'));
                }
            }
        }
    }

    http.send(null);
}

var stateSearchCount = 0;

function searchStates()
{
    stateSearchCount++;
    var count = stateSearchCount;

    var http = createRequestObject();
    http.open('get', '/index.php?output=ajax&task=states&country=' + document.getElementById('country').value + '&name=' + encodeURIComponent(document.getElementById('stateName').value));

    http.onreadystatechange = function ()
    {
        document.getElementsByTagName('body')[0].style.cursor = 'wait';

        if(http.readyState == 4)
        {
            if(http.status == 200)
            {
                if(count == stateSearchCount)
                {
                    document.getElementById('stateSearch').innerHTML = '';

                    var xml = http.responseXML.documentElement;
                    var states = xml.getElementsByTagName('r');

                    for(i = 0;i < states.length;i++)
                    {
                        document.getElementById('stateSearch').innerHTML += '<div id="' + states[i].getAttribute('did') + '" onmousedown="setState();" onmouseover="setSelectedState(this);" onmouseout="unsetSelectedState();">' + states[i].getAttribute('name') + '</div>';
                    }

                    document.getElementById('stateSearch').style.visibility = 'visible';
                    listFocus = 'stateSearch';
                    selectedList = 'state';
                    pressedKeyEvent = 'moveList';
                    unsetSelectedState();
                }

                document.getElementsByTagName('body')[0].style.cursor = 'default';
            }
        }
    }

    http.send(null);
}

function searchStatesMenu()
{
    stateSearchCount++;
    var count = stateSearchCount;

    var http = createRequestObject();
    http.open('get', '/index.php?output=ajax&task=states&country=' + document.getElementById('countryMenu').value + '&name=' + encodeURIComponent(document.getElementById('stateNameMenu').value));

    http.onreadystatechange = function ()
    {
        document.getElementsByTagName('body')[0].style.cursor = 'wait';

        if(http.readyState == 4)
        {
            if(http.status == 200)
            {
                if(count == stateSearchCount)
                {
                    document.getElementById('stateSearchMenu').innerHTML = '';

                    var xml = http.responseXML.documentElement;
                    var states = xml.getElementsByTagName('r');

                    for(i = 0;i < states.length;i++)
                    {
                        document.getElementById('stateSearchMenu').innerHTML += '<div id="' + states[i].getAttribute('did') + '" onmousedown="setStateMenu();" onmouseover="setSelectedState(this);" onmouseout="unsetSelectedState();">' + states[i].getAttribute('name') + '</div>';
                    }

                    document.getElementById('stateSearchMenu').style.visibility = 'visible';
                    listFocus = 'stateSearchMenu';
                    selectedList = 'stateMenu';
                    pressedKeyEvent = 'moveList';
                    unsetSelectedState();
                }

                document.getElementsByTagName('body')[0].style.cursor = 'default';
            }
        }
    }

    http.send(null);
}

function searchStatesGoto()
{
    stateSearchCount++;
    var count = stateSearchCount;

    var http = createRequestObject();
    http.open('get', '/index.php?output=ajax&task=states&country=' + document.getElementById('countryGoto').value + '&name=' + encodeURIComponent(document.getElementById('stateNameGoto').value));

    http.onreadystatechange = function ()
    {
        document.getElementsByTagName('body')[0].style.cursor = 'wait';

        if(http.readyState == 4)
        {
            if(http.status == 200)
            {
                if(count == stateSearchCount)
                {
                    document.getElementById('stateSearchGoto').innerHTML = '';

                    var xml = http.responseXML.documentElement;
                    var states = xml.getElementsByTagName('r');

                    for(i = 0;i < states.length;i++)
                    {
                        document.getElementById('stateSearchGoto').innerHTML += '<div id="' + states[i].getAttribute('did') + '" onmousedown="setStateGoto();" onmouseover="setSelectedState(this);" onmouseout="unsetSelectedState();">' + states[i].getAttribute('name') + '</div>';
                    }

                    document.getElementById('stateSearchGoto').style.visibility = 'visible';
                    listFocus = 'stateSearchGoto';
                    selectedList = 'stateGoto';
                    pressedKeyEvent = 'moveList';
                    unsetSelectedState();
                }

                document.getElementsByTagName('body')[0].style.cursor = 'default';
            }
        }
    }

    http.send(null);
}

function getStateId()
{
    var http = createRequestObject();
    http.open('get', '/index.php?output=ajax&task=states&equal=1&country=' + document.getElementById('country').value + '&name=' + encodeURIComponent(document.getElementById('stateName')).value);

    http.onreadystatechange = function ()
    {
        if(http.readyState == 4)
        {
            if(http.status == 200)
            {
                var xml = http.responseXML.documentElement;
                var state = xml.getElementsByTagName('r')[0];

                if(typeof(state)=='undefined')
                    document.getElementById('state').value = '';
                else if(document.getElementById('state').value != state.getAttribute('id'))
                {
                    document.getElementById('state').value = state.getAttribute('id');
                    unsetCity();
                }
            }
        }
    }

    http.send(null);
}

function getStateIdMenu()
{
    var http = createRequestObject();
    http.open('get', '/index.php?output=ajax&task=states&equal=1&country=' + document.getElementById('countryMenu').value + '&name=' + encodeURIComponent(document.getElementById('stateNameMenu')).value);

    http.onreadystatechange = function ()
    {
        if(http.readyState == 4)
        {
            if(http.status == 200)
            {
                var xml = http.responseXML.documentElement;
                var state = xml.getElementsByTagName('r')[0];

                if(typeof(state)=='undefined')
                    document.getElementById('stateMenu').value = '';
                else if(document.getElementById('stateMenu').value != state.getAttribute('id'))
                {
                    document.getElementById('stateMenu').value = state.getAttribute('id');
                    unsetCityMenu();
                }
            }
        }
    }

    http.send(null);
}

function getStateIdGoto()
{
    var http = createRequestObject();
    http.open('get', '/index.php?output=ajax&task=states&equal=1&country=' + document.getElementById('countryGoto').value + '&name=' + encodeURIComponent(document.getElementById('stateNameGoto')).value);

    http.onreadystatechange = function ()
    {
        if(http.readyState == 4)
        {
            if(http.status == 200)
            {
                var xml = http.responseXML.documentElement;
                var state = xml.getElementsByTagName('r')[0];

                if(typeof(state)=='undefined')
                    document.getElementById('stateGoto').value = '';
                else if(document.getElementById('stateGoto').value != state.getAttribute('id'))
                {
                    document.getElementById('stateGoto').value = state.getAttribute('id');
                    unsetCityGoto();
                }
            }
        }
    }

    http.send(null);
}

function getStateById(state)
{
    var http = createRequestObject();
    http.open('get', '/index.php?output=ajax&task=states&id=' + state);

    http.onreadystatechange = function ()
    {
        if(http.readyState == 4)
        {
            if(http.status == 200)
            {
                var xml = http.responseXML.documentElement;
                var state = xml.getElementsByTagName('r')[0];

                if(typeof(state)=='undefined')
                    document.getElementById('state').value = '';
                else
                {
                    document.getElementById('state').value = state.getAttribute('id');
                    document.getElementById('stateName').value = state.getAttribute('name');
                }
            }
        }
    }

    http.send(null);
}

function getStateByIdMenu(state)
{
    var http = createRequestObject();
    http.open('get', '/index.php?output=ajax&task=states&id=' + state);

    http.onreadystatechange = function ()
    {
        if(http.readyState == 4)
        {
            if(http.status == 200)
            {
                var xml = http.responseXML.documentElement;
                var state = xml.getElementsByTagName('r')[0];

                if(typeof(state)=='undefined')
                    document.getElementById('stateMenu').value = '';
                else
                {
                    document.getElementById('stateMenu').value = state.getAttribute('id');
                    document.getElementById('stateNameMenu').value = state.getAttribute('name');
                }
            }
        }
    }

    http.send(null);
}

function getStateByIdGoto(state)
{
    var http = createRequestObject();
    http.open('get', '/index.php?output=ajax&task=states&id=' + state);

    http.onreadystatechange = function ()
    {
        if(http.readyState == 4)
        {
            if(http.status == 200)
            {
                var xml = http.responseXML.documentElement;
                var state = xml.getElementsByTagName('r')[0];

                if(typeof(state)=='undefined')
                    document.getElementById('stateGoto').value = '';
                else
                {
                    document.getElementById('stateoto').value = state.getAttribute('id');
                    document.getElementById('stateNameGoto').value = state.getAttribute('name');
                }
            }
        }
    }

    http.send(null);
}

var countrySearchCount = 0;

function searchCountries()
{
    countrySearchCount++;
    var count = countrySearchCount;

    var http = createRequestObject();
    http.open('get', '/index.php?output=ajax&task=countries&name=' + encodeURIComponent(document.getElementById('countryName').value));

    http.onreadystatechange = function ()
    {
        document.getElementsByTagName('body')[0].style.cursor = 'wait';

        if(http.readyState == 4)
        {
            if(http.status == 200)
            {
                if(count == countrySearchCount)
                {
                    document.getElementById('countrySearch').innerHTML = '';

                    var xml = http.responseXML.documentElement;
                    var countries = xml.getElementsByTagName('r');

                    for(i = 0;i < countries.length;i++)
                    {
                        document.getElementById('countrySearch').innerHTML += '<div id="' + countries[i].getAttribute('did') + '" onmousedown="setCountry();" onmouseover="setSelectedCountry(this);" onmouseout="unsetSelectedCountry();">' + countries[i].getAttribute('name') + '</div>';
                    }

                    document.getElementById('countrySearch').style.visibility = 'visible';
                    listFocus = 'countrySearch';
                    selectedList = 'country';
                    pressedKeyEvent = 'moveList';
                    unsetSelectedCountry();
                }

                document.getElementsByTagName('body')[0].style.cursor = 'default';
            }
        }
    }

    http.send(null);
}

function searchCountriesMenu()
{
    countrySearchCount++;
    var count = countrySearchCount;

    var http = createRequestObject();
    http.open('get', '/index.php?output=ajax&task=countries&name=' + encodeURIComponent(document.getElementById('countryNameMenu').value));

    http.onreadystatechange = function ()
    {
        document.getElementsByTagName('body')[0].style.cursor = 'wait';

        if(http.readyState == 4)
        {
            if(http.status == 200)
            {
                if(count == countrySearchCount)
                {
                    document.getElementById('countrySearchMenu').innerHTML = '';

                    var xml = http.responseXML.documentElement;
                    var countries = xml.getElementsByTagName('r');

                    for(i = 0;i < countries.length;i++)
                    {
                        document.getElementById('countrySearchMenu').innerHTML += '<div id="' + countries[i].getAttribute('did') + '" onmousedown="setCountryMenu();" onmouseover="setSelectedCountry(this);" onmouseout="unsetSelectedCountry();">' + countries[i].getAttribute('name') + '</div>';
                    }

                    document.getElementById('countrySearchMenu').style.visibility = 'visible';
                    listFocus = 'countrySearchMenu';
                    selectedList = 'countryMenu';
                    pressedKeyEvent = 'moveList';
                    unsetSelectedCountry();
                }

                document.getElementsByTagName('body')[0].style.cursor = 'default';
            }
        }
    }

    http.send(null);
}

function searchCountriesGoto()
{
    countrySearchCount++;
    var count = countrySearchCount;

    var http = createRequestObject();
    http.open('get', '/index.php?output=ajax&task=countries&name=' + encodeURIComponent(document.getElementById('countryNameGoto').value));

    http.onreadystatechange = function ()
    {
        document.getElementsByTagName('body')[0].style.cursor = 'wait';

        if(http.readyState == 4)
        {
            if(http.status == 200)
            {
                if(count == countrySearchCount)
                {
                    document.getElementById('countrySearchGoto').innerHTML = '';

                    var xml = http.responseXML.documentElement;
                    var countries = xml.getElementsByTagName('r');

                    for(i = 0;i < countries.length;i++)
                    {
                        document.getElementById('countrySearchGoto').innerHTML += '<div id="' + countries[i].getAttribute('did') + '" onmousedown="setCountryGoto();" onmouseover="setSelectedCountry(this);" onmouseout="unsetSelectedCountry();">' + countries[i].getAttribute('name') + '</div>';
                    }

                    document.getElementById('countrySearchGoto').style.visibility = 'visible';
                    listFocus = 'countrySearchGoto';
                    selectedList = 'countryGoto';
                    pressedKeyEvent = 'moveList';
                    unsetSelectedCountry();
                }

                document.getElementsByTagName('body')[0].style.cursor = 'default';
            }
        }
    }

    http.send(null);
}

function getCountryCode()
{
    var http = createRequestObject();
    http.open('get', '/index.php?output=ajax&task=countries&equal=1&name=' + encodeURIComponent(document.getElementById('countryName').value));

    http.onreadystatechange = function ()
    {
        if(http.readyState == 4)
        {
            if(http.status == 200)
            {
                var xml = http.responseXML.documentElement;
                var country = xml.getElementsByTagName('r')[0];

                if(typeof(country)=='undefined')
                    document.getElementById('country').value = '';
                else if(document.getElementById('country').value != country.getAttribute('code'))
                {
                    document.getElementById('country').value = country.getAttribute('code');
                    unsetState();
                    unsetCity();
                }
            }
        }
    }

    http.send(null);
}

function getCountryCodeMenu()
{
    var http = createRequestObject();
    http.open('get', '/index.php?output=ajax&task=countries&equal=1&name=' + encodeURIComponent(document.getElementById('countryNameMenu').value));

    http.onreadystatechange = function ()
    {
        if(http.readyState == 4)
        {
            if(http.status == 200)
            {
                var xml = http.responseXML.documentElement;
                var country = xml.getElementsByTagName('r')[0];

                if(typeof(country)=='undefined')
                    document.getElementById('countryMenu').value = '';
                else if(document.getElementById('countryMenu').value != country.getAttribute('code'))
                {
                    document.getElementById('countryMenu').value = country.getAttribute('code');
                    unsetStateMenu();
                    unsetCityMenu();
                }
            }
        }
    }

    http.send(null);
}

function getCountryCodeGoto()
{
    var http = createRequestObject();
    http.open('get', '/index.php?output=ajax&task=countries&equal=1&name=' + encodeURIComponent(document.getElementById('countryNameGoto').value));

    http.onreadystatechange = function ()
    {
        if(http.readyState == 4)
        {
            if(http.status == 200)
            {
                var xml = http.responseXML.documentElement;
                var country = xml.getElementsByTagName('r')[0];

                if(typeof(country)=='undefined')
                    document.getElementById('countryGoto').value = '';
                else if(document.getElementById('countryGoto').value != country.getAttribute('code'))
                {
                    document.getElementById('countryGoto').value = country.getAttribute('code');
                    unsetStateGoto();
                    unsetCityGoto();
                }
            }
        }
    }

    http.send(null);
}

var userSearchCount = 0;

function searchUsers()
{
    userSearchCount++;
    var count = userSearchCount;

    var http = createRequestObject();
    http.open('get', '/index.php?output=ajax&task=users&name=' + encodeURIComponent(document.getElementById('userName').value));

    http.onreadystatechange = function ()
    {
        document.getElementsByTagName('body')[0].style.cursor = 'wait';

        if(http.readyState == 4)
        {
            if(http.status == 200)
            {
                if(count == userSearchCount)
                {
                    document.getElementById('userSearch').innerHTML = '';

                    var xml = http.responseXML.documentElement;
                    var users = xml.getElementsByTagName('r');

                    for(i = 0;i < users.length;i++)
                    {
                        document.getElementById('userSearch').innerHTML += '<div id="' + users[i].getAttribute('did') + '" onmousedown="setUser();" onmouseover="setSelectedUser(this);" onmouseout="unsetSelectedUser();">' + users[i].getAttribute('name') + '</div>';
                    }

                    document.getElementById('userSearch').style.visibility = 'visible';
                    listFocus = 'userSearch';
                    pressedKeyEvent = 'moveList';
                    selectedList = 'user';
                }

                document.getElementsByTagName('body')[0].style.cursor = 'default';
            }
        }
    }

    http.send(null);
}

function refreshSession()
{
    var http = createRequestObject();
    http.open('get', '/index.php?output=ajax&task=users&act=refreshSession');
    http.send(null);
}

function getUserId()
{
    var http = createRequestObject();
    http.open('get', '/index.php?output=ajax&equal=1&task=users&name=' + encodeURIComponent(document.getElementById('userName').value));

    http.onreadystatechange = function ()
    {
        if(http.readyState == 4)
        {
            if(http.status == 200)
            {
                var xml = http.responseXML.documentElement;
                var user = xml.getElementsByTagName('r')[0];

                if(typeof(user)=='undefined')
                    document.getElementById('user').value = '';
                else
                    document.getElementById('user').value = user.getAttribute('id');
            }
        }
    }

    http.send(null);
}

var httpPM = createRequestObject();

function getPM()
{
    httpPM.abort();
    httpPM.open('get', '/index.php?output=ajax&task=pm');

    httpPM.onreadystatechange = function ()
    {
        if(httpPM.readyState == 4)
        {
            if(httpPM.status == 200)
            {
                var xml = httpPM.responseXML.documentElement;
                var pm = xml.getElementsByTagName('msg')[0];

                if(typeof(pm)!='undefined')
                {
                    document.getElementById('pmNotifierText').innerHTML = '<a href="/pm/read/' + pm.getAttribute('id') + '/">' + pm.getElementsByTagName('text')[0].getAttribute('v') + '</a>';
                    document.getElementById('pmNotifierTitle').innerHTML = '<a href="/pm/read/' + pm.getAttribute('id') + '/">' + pm.getElementsByTagName('subject')[0].getAttribute('v') + '</a>';

                    if(pm.getElementsByTagName('image')[0].getAttribute('v'))
                        document.getElementById('pmNotifierUser').innerHTML = '<a href="/user/' + encodeURI(pm.getElementsByTagName('nickname')[0].getAttribute('v')) + '/" title="' + pm.getElementsByTagName('nickname')[0].getAttribute('v') + '"><img border="0" src="/images/user/icon/' + pm.getElementsByTagName('nickname')[0].getAttribute('v') + '.jpg" alt="' + pm.getElementsByTagName('nickname')[0].getAttribute('v') + '" style="margin: ' + (userIconHeight-pm.getElementsByTagName('icon_height')[0].getAttribute('v'))/2 + 'px ' + (userIconWidth-pm.getElementsByTagName('icon_width')[0].getAttribute('v'))/2 + 'px;" /></a>';
                    else
                        if (pm.getElementsByTagName('sex')[0].getAttribute('v') == 'male')
                            document.getElementById('pmNotifierUser').innerHTML = '<a href="/user/' + encodeURI(pm.getElementsByTagName('nickname')[0].getAttribute('v')) + '/" title="' + pm.getElementsByTagName('nickname')[0].getAttribute('v') + '"><img border="0" src="/images/green/no_user_image_male_icon.jpg" alt="' + pm.getElementsByTagName('nickname')[0].getAttribute('v') + '" /></a>';
                        else if (pm.getElementsByTagName('sex')[0].getAttribute('v') == 'male')
                            document.getElementById('pmNotifierUser').innerHTML = '<a href="/user/' + encodeURI(pm.getElementsByTagName('nickname')[0].getAttribute('v')) + '/" title="' + pm.getElementsByTagName('nickname')[0].getAttribute('v') + '"><img border="0" src="/images/green/no_user_image_female_icon.jpg" alt="' + pm.getElementsByTagName('nickname')[0].getAttribute('v') + '" /></a>';
                        else
                            document.getElementById('pmNotifierUser').innerHTML = '<a href="/user/' + encodeURI(pm.getElementsByTagName('nickname')[0].getAttribute('v')) + '/" title="' + pm.getElementsByTagName('nickname')[0].getAttribute('v') + '"><img border="0" src="/images/green/no_user_image_icon.jpg" alt="' + pm.getElementsByTagName('nickname')[0].getAttribute('v') + '" /></a>';

                    if(pm.getElementsByTagName('online')[0].getAttribute('v'))
                        document.getElementById('pmNotifierOnline').style.display = 'block';
                    else
                        document.getElementById('pmNotifierOnline').style.display = 'none';

                    pmPopup = window.setInterval('shopPmPopup()', 10);
                    document.title = new_message + ' ' + encodeURI(pm.getElementsByTagName('nickname')[0].getAttribute('v'));
                }
            }
        }
    }

    httpPM.send(null);
}

function saveCoords(toiletID, lat, lng)
{
    var returnValue = false;
    var http = createRequestObject();
    http.open('get', '/index.php?output=ajax&task=toilets&act=saveCoords&toiletID=' + toiletID + '&lat=' + lat + '&lng=' + lng);

    http.onreadystatechange = function ()
    {
        if(http.readyState == 4)
        {
            if(http.status == 200)
            {
                returnValue = true;
            }
        }
    }

    http.send(null);

    return returnValue;
}

var ignoreIds = '';
var httpToilets = createRequestObject();

function getMapToilets(endFromLng, endToLng, endFromLat, endToLat, map)
{
    httpToilets.abort();
    var returnValue = false;
    httpToilets.open('get', '/index.php?output=ajax&task=toilets&act=getMapToilets&endFromLng=' + endFromLng + '&endToLng=' + endToLng + '&endFromLat=' + endFromLat + '&endToLat=' + endToLat + '&i=0' + ignoreIds);

    httpToilets.onreadystatechange = function ()
    {
        if(httpToilets.readyState == 4)
        {
            if(httpToilets.status == 200)
            {
                var xml = httpToilets.responseXML.documentElement;

                if(webDrawed)
                {
                    map.clearOverlays();
                        if(typeof(posMarker) == 'object')
                            map.addOverlay(posMarker);
                }
                
                if(typeof(xml.getElementsByTagName('marker')[0]) == 'object')
                {
                    if(xml.getElementsByTagName('marker')[0].getAttribute('v') == 1)
                    {
                        webDrawed = false;

                        var records = xml.getElementsByTagName('r');

                        for(i = 0;i < records.length;i++)
                        {
                            var point = new GLatLng(records[i].getElementsByTagName('latitude')[0].getAttribute('v'), records[i].getElementsByTagName('longitude')[0].getAttribute('v'));
                            map.addOverlay(setMarker(point, records[i]));
                            ignoreIds += ',' + records[i].getAttribute('id');
                        }
                    }
                    else
                        drawWeb(map, xml);
                }

                returnValue = true;
            }
        }
    }

    httpToilets.send(null);

    return returnValue;
}

var counter = 0;
var counterRecords = 0;
var markers = new Array();
var aroundCitys = new Object();
var aroundStates = new Object();
var aroundCountries = new Object();

function getAroundPointToilets(lng, lat, map, notIds)
{
    var radLat = 0.005;
    var radLng = 0.01;

    if(counter < 13)
    {
        counter++;

        endRadLng = Math.pow(2, counter) * radLng;
        endRadLat = Math.pow(2, counter) * radLat;

        var http = createRequestObject();
        http.open('get', '/index.php?output=ajax&task=toilets&act=getMapToilets&endFromLng=' + (lat - endRadLat)  + '&endToLng=' + (lat + endRadLat) + '&endFromLat=' + (lng - endRadLng) + '&endToLat=' + (lng + endRadLng) + "&i=0" + notIds);

        http.onreadystatechange = function ()
        {
            if(http.readyState == 4)
            {
                if(http.status == 200)
                {
                    var xml = http.responseXML.documentElement;
                    var records = xml.getElementsByTagName('r');

                    var lastCity;
                    var lastState;
                    var lastCountry;

                    for(i = 0;i < records.length;i++)
                    {
                        lastCity = records[i].getElementsByTagName('city')[0].getAttribute('v');
                        lastState = records[i].getElementsByTagName('state')[0].getAttribute('v');
                        lastCountry = records[i].getElementsByTagName('country')[0].getAttribute('v');

                        if(aroundCitys[lastCity])
                            aroundCitys[lastCity]++;
                        else
                            aroundCitys[lastCity] = 1;

                        if(aroundStates[lastState])
                            aroundStates[lastState]++;
                        else
                            aroundStates[lastState] = 1;

                        if(aroundCountries[lastCountry])
                            aroundCountries[lastCountry]++;
                        else
                            aroundCountries[lastCountry] = 1;

                        counterRecords++;

                        var point = new GLatLng(records[i].getElementsByTagName('latitude')[0].getAttribute('v'), records[i].getElementsByTagName('longitude')[0].getAttribute('v'));
                        var marker = setMarker(point, records[i]);
                        //map.addOverlay(marker);

                        var marker = new Array(marker, records[i]);
                        markers.push(marker);
                        notIds += ',' + records[i].getAttribute('id');
                    }

                    if(counterRecords >= 3)
                    {
                        counter = 13;
                        mapTour(markers, map);

                        var cityKeys = new Array();

                        for(var key in aroundCitys)
                            cityKeys.push(key);

                        if(cityKeys.length == 1)
                        {
                            document.getElementById('amtc_option_custom').onclick = function() {location.href = "/" + lastCountry + "/" + lastState + "/" + cityKeys[0] + "/"};
                        }
                        else
                        {
                            var stateKeys = new Array();

                            for(var key in aroundStates)
                                stateKeys.push(key);

                            if(stateKeys.length == 1)
                            {
                                document.getElementById('amtc_option_custom').onclick = function() {location.href = "/" + lastCountry + "/" + stateKeys[0] + "/"};
                            }
                            else
                            {
                                var countryKeys = new Array();

                                for(var key in aroundCountries)
                                    countryKeys.push(key);

                                if(countryKeys.length == 1)
                                {
                                    document.getElementById('amtc_option_custom').onclick = function() {location.href = "/" + countryKeys[0] + "/"};
                                }
                            }
                        }
                    }

                    getAroundPointToilets(lng, lat, map, notIds)
                }
            }
        }

        http.send(null);
    }
}

function saveFrontpageImageInfoTop(id, top)
{
    var http = createRequestObject();
    http.open('get', '/index.php?output=ajax&task=toilets&act=saveFrontpageImageInfoTop&id=' + id + '&top=' + top);

    http.onreadystatechange = function ()
    {
        if(http.readyState == 4)
        {
            if(http.status == 200)
            {
                alert("Gespeichert!");
            }
        }
    }

    http.send(null);
}

var listToilets = 0;

function getListToilets(list)
{
    var order = 'newest_toilet';
    var direction = 1;

    if(list.id == 'topToiletsBox')
    {
        order = 'best_ratings';
    }
    else if(list.id == 'badToiletsBox')
    {
        order = 'best_ratings';
        direction = 0;
    }

    var http = createRequestObject();
    http.open('get', '/index.php?output=ajax&task=toilets&act=getToiletsSorted&rows=1&from=' + list.getElementsByTagName('div').length/3 + '&order=' + order + '&direction=' + direction);

    http.onreadystatechange = function ()
    {
        if(http.readyState == 4)
        {
            if(http.status == 200)
            {
                var xml = http.responseXML.documentElement;
                var records = xml.getElementsByTagName('r');

                if(records.length)
                {
                    list.innerHTML += '<div class="panelRow' + (list.getElementsByTagName('div').length/3)%2 + '">' + toiletInfoHtml(records[0]) + '</div>';
                    listToilets++;
                }
            }
        }
    }

    http.send(null);

}