var req = null;

function InitXMLHttpRequest() {
    // Make a new XMLHttp object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
}


function SelectCountry(section, destination, operation_type, form_type) {
    InitXMLHttpRequest();
    // Load the result from the response page
    if (operation_type){
        operation_str = "&operation_type=" + operation_type;
    }else{
        operation_str = '';
    }
    var class_loc;
    if (form_type == 'left_search_form'){
        operation_str = operation_str + "&is_left_search_form=1";
        class_loc = 'location_2';
    }else{
        class_loc = 'location';
    }
    if (req) {
        req.onreadystatechange = function() {
            if (req.readyState == 4) {
                destination.innerHTML = req.responseText;
            } else {
                destination.innerHTML = "<select class='"+class_loc+"'><option>Loading data...</option></select>";
            }
        }
        req.open("GET", site_root+"/location.php?sec=" + section + "&sel=country" + operation_str, true);
        req.send(null);
    } else {
        destination.innerHTML = 'Browser unable to create XMLHttp Object';
    }
    refreshSelectBox();
}

function SelectRegion(section, id_country, destination, destination2, text, text2, operation_type, form_type, from_index) {
    from_index = from_index || 2;

    InitXMLHttpRequest();
    if (operation_type){
        operation_str = "&operation_type=" + operation_type;
    }else{
        operation_str = '';
    }
    var class_loc;
    if (form_type=='left_search_form'){
        operation_str = operation_str + "&is_left_search_form=1";
        class_loc = 'location_2';
    }else{
        if (section == 'rnte' || section == 'rntel') {
            class_loc = 'location_add_edit';
        }
        else {
            class_loc = 'location';
        }
    }

    if (id_country != '') {
        if (req) {
            req.onreadystatechange = function() {
                if (req.readyState == 4) {
                    destination.innerHTML = req.responseText;
                    if (from_index == 1) {
                        //var select_region_id = ($(req.responseText).attr('id'));
                        //$("#"+select_region_id).selectBox();
                        refreshSelectBox();
                    }
                } else {
                    destination.innerHTML = "<select class='"+class_loc+"'><option>"+text+"</option></select>";
                    if (from_index == 1) {
                        // attach selectbox
                        //$("select."+class_loc).selectBox();
                        refreshSelectBox();
                    }
                }
            }
            req.open("GET", site_root + "/location.php?sec=" + section + "&sel=region&id_country=" + id_country + operation_str + "&from_page="+from_index, true);
            req.send(null);
        } else {
            destination.innerHTML = 'Browser unable to create XMLHttp Object';
        }
    } else {
        req.open('GET',  site_root + '/location.php?sec=' + section + '&sel=region' + operation_str, false);
        req.send(null);
        destination.innerHTML = req.responseText;
    }
    if (from_index == 1) {
        refreshSelectBox();
    }
}

function SelectRegion2(section, id_country, destination, destination2, text, text2, destination3) {
    InitXMLHttpRequest();
    if (id_country != '') {
        if (req) {
            req.onreadystatechange = function() {
                if (req.readyState == 4) {
                    destination.innerHTML = req.responseText;
                } else {
                    destination.innerHTML = "<select class=\"location\"><option>"+text+"</option></select>";
                }
            }
            req.open("GET",  site_root + "/location.php?sec=" + section + "&sel=region2&id_country=" + id_country, true);
            req.send(null);
        } else {
            destination.innerHTML = 'Browser unable to create XMLHttp Object';
        }
    } else {
        req.open('GET',  site_root + '/location.php?sec=' + section + '&sel=region', false);
        req.send(null);
        destination.innerHTML = req.responseText;
    }
    destination2.innerHTML = "<select class=\"location\"><option>"+text2+"</option></select>";
    if ((section == 'rnte') || (section == 'rmte') || (section == 'rntel')){
        destination3.innerHTML = "";
    }
}

function SelectLocation(section, operation_type, country_id, region_id, city_id, destination_country, destination_region, destination_city,ip_load_region_TEXT, ip_city_TEXT, ip_load_city_TEXT, form_type, from_index){

    from_index = from_index || 2;

    InitXMLHttpRequest();
    // Load the result from the response page
    if (operation_type){
        operation_str = "&operation_type=" + operation_type;
    }else{
        operation_str = '';
    }
    if (country_id){
        operation_str = operation_str + "&country_id=" + country_id;
    }
    if (region_id){
        operation_str = operation_str + "&region_id=" + region_id;
    }
    if (city_id){
        operation_str = operation_str + "&city_id=" + city_id;
    }
    var class_loc;
    if (form_type=='left_search_form'){
        operation_str = operation_str + "&is_left_search_form=1";
        class_loc = 'location_2';
    }else{
        class_loc = 'location';
    }
    if (req) {
        req.onreadystatechange = function() {
            if (req.readyState == 4) {

                var response_arr = req.responseText.split('__');
                if (country_id != -1) {
                    destination_country.innerHTML = response_arr[0];
                    if (from_index == 1) {
                        //var select_country_id = ($(response_arr[0]).attr('id'));
                        //$("#"+select_country_id).selectBox();
                        refreshSelectBox();
                    }
                }
                if (region_id != -1) {
                    destination_region.innerHTML = response_arr[1];
                    if (from_index == 1) {
                        //var select_region_id = ($(response_arr[1]).attr('id'));
                        //$("#"+select_region_id).selectBox();
                        refreshSelectBox();
                    }
                }
                if (city_id != -1) {
                    destination_city.innerHTML = response_arr[2];
                    if (from_index == 1) {
                        //var select_city_id = ($(response_arr[2]).attr('id'));
                        //$("#"+select_city_id).selectBox();
                        refreshSelectBox();
                    }
                }
            } else {
                if (country_id != -1) {
                    destination_country.innerHTML = "<select class='"+class_loc+"'><option>Loading data...</option></select>";

                }
                if (region_id != -1) {
                    destination_region.innerHTML = "<select class='"+class_loc+"'><option>"+ip_load_region_TEXT+"</option></select>";
                }
                if (city_id != -1) {
                    destination_city.innerHTML = "<select class='"+class_loc+"'><option>"+ip_load_city_TEXT+"</option></select>";
                }

                if (from_index == 1) {
                    // attach selectbox
                    //$("select."+class_loc).selectBox();
                    refreshSelectBox();
                }
            }
        }
        req.open("GET", site_root+"/location.php?sec=" + section + "&sel=all_location" + operation_str+"&from_page="+from_index, true);
        req.send(null);
    } else {
        destination.innerHTML = 'Browser unable to create XMLHttp Object';
    }
    if (from_index == 1) {
        refreshSelectBox();
    }
}

function SelectCity(section, id_region, destination, text, operation_type, form_type, from_index){

    from_index = from_index || 2;

    InitXMLHttpRequest();
    if (operation_type){
        operation_str = "&operation_type=" + operation_type;
    }else{
        operation_str = '';
    }
    var class_loc;

    if (form_type=='left_search_form'){
        operation_str = operation_str + "&is_left_search_form=1";
        class_loc = 'location_2';
    }else{
        if (section == 'rnte' || section == 'rntel') {
            class_loc = 'location_add_edit';
        }
        else {
            class_loc = 'location';
        }
    }
    if (id_region != '') {
        // Load the result from the response page
        if (req){
            req.onreadystatechange = function() {
                if (req.readyState == 4) {
                    destination.innerHTML = req.responseText;
                    if (from_index == 1) {
                        //var select_city_id = ($(req.responseText).attr('id'));
                        //$("#"+select_city_id).selectBox();
                        refreshSelectBox();
                    }
                } else {
                    destination.innerHTML = "<select class='"+class_loc+"'><option>"+text+"</option></select>";
                    if (from_index == 1) {
                        // attach selectbox
                        //$("select."+class_loc).selectBox();
                        refreshSelectBox();
                    }
                }
            }
            var href = document.location.href;
            if ( href.match(/mortgage/) ) var mortgage_path = "/mortgage";
            else var mortgage_path = "";
            req.open('GET',  site_root + mortgage_path + '/location.php?sec=' + section + '&sel=city&id_region=' + id_region + operation_str, true);
            req.send(null);
        }
        else{
            destination.innerHTML = 'Browser unable to create XMLHttp Object';
        }
    }
    else {
        req.open('GET',  site_root + '/location.php?sec=' + section + '&sel=city' + operation_str, false);
        req.send(null);
        destination.innerHTML = req.responseText;
    }
    if (from_index == 1) {
        refreshSelectBox();
    }
}

function SelectCity2(section, id_region, destination, text, destination2){
    InitXMLHttpRequest();
    if (id_region != '') {
        // Load the result from the response page
        if (req){
            req.onreadystatechange = function() {
                if (req.readyState == 4) {
                    destination.innerHTML = req.responseText;
                } else {
                    destination.innerHTML = "<select class=\"location\"><option>"+text+"</option></select>";
                }
            }
            req.open('GET',  site_root + '/location.php?sec=' + section + '&sel=city2&id_region=' + id_region, true);
            req.send(null);
        }
        else{
            destination.innerHTML = 'Browser unable to create XMLHttp Object';
        }
    }
    else {
        req.open('GET',  site_root + '/location.php?sec=' + section + '&sel=city2', false);
        req.send(null);
        destination.innerHTML = req.responseText;
    }
    if ((section == 'rnte') || (section == 'rmte') || (section == 'rntel')){
        destination2.innerHTML = "";
    }
}

function ShowSubways(section, city_id, destination, text, text2){
    if ((section == 'rnte') || (section == 'rmte') || (section == 'rntel')){
        if ((city_id=='3159_4312_4400') || (city_id=='3159_4925_4962')){
            InitXMLHttpRequest();
            if (req){
                req.onreadystatechange = function() {
                    if (req.readyState == 4) {
                        destination.innerHTML = req.responseText;
                    } else {
                        destination.innerHTML = "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" height=\"25\"><tr><td height=\"27\" width=\"100\">" + text2 + ":&nbsp;</td><td><select class=\"location\"><option>" + text +"</option></select></td></tr></table>";
                    }
                }
                req.open('GET',  site_root + '/location.php?sec=' + section + '&sel=subway&id_city=' + city_id, true);
                req.send(null);
            }
            else{
                destination.innerHTML = 'Browser unable to create XMLHttp Object';
            }
        } else {
            destination.innerHTML = '';
        }
    } else {
        destination.innerHTML = '';
    }
}

function UpdatingProcess(div, update_text){
    $("#update_div").remove();
    $("#update_text").remove();
    $("#update_img").remove();
    $div_el = $("#"+div);

    if ($div_el.width() != null){
        var width = $div_el.width();
        var height = $div_el.height();
        var left_pos = $div_el.offset().left;
        var top_pos = $div_el.offset().top;
        $("body").append('<div id="update_div"></div>');
        $("#update_div").css("z-index", 5)
        .css("position", "absolute")
        .css("left", left_pos)
        .css("top", top_pos)
        .css("width", width)
        .css("height", height)
        .css("border", "0px solid #cccccc")
        .css("background-image", "url('"+bg_img+"')");

        var left_img = left_pos + width/2 - 16;
        var left_text = left_pos + width/2 + 24;
        var top_img = top_pos + height/2 -16;
        var top_text = top_img + 8;
        $("body").append("<img id='update_img' src='"+indicator+"' style='position:absolute;z-index:6; left: "+left_img+"px;top: "+top_img+"px;'>");
        $("body").append("<div id='update_text' style='position:absolute;z-index:6; left: "+left_text+"px;top: "+top_text+"px;'>"+update_text+"</div>");
    }
};
function refreshSelectBox() {
    $('#country_select').selectBox("destroy").selectBox();
    $('#region_select').selectBox("destroy").selectBox();
    $('#city_select').selectBox("destroy").selectBox();
}
