function set_adv(elem){
    $('#adv_price').hide();
    $('#adv_shop_type').hide();
    $('#adv_shop_type_name').hide();   

    if (elem.value == 'products' || elem.value == 'requests') {
        $('#adv_price').show();
    }

    if (elem.value == 'shops'){
        $('#adv_shop_type').show();
        $('#adv_shop_type_name').show();
    }
}

function change_captcha(){
    $('#captcha').attr("src",  "/captcha?random=" + (Math.random()*10000));
}

function set_type(){
    var type = $('input[name=type]:checked').val();
    var in_tc = $('#in_tc').attr('checked')?1:0;


    $('#tr_tc').hide();
    $('#tr_ground').hide();
    $('#tr_street').hide();  $('#required_street').hide();
    $('#tr_house').hide();   $('#required_house').hide();
    $('#tr_floor').hide();
    $('#tr_office').hide();
    $('#tr_alternative').hide();
    //$('#dt_name').html('Название площадки:');

    $("#tr_tc").removeClass("tr_highlight");
    $("#tr_ground").removeClass("tr_highlight");
    $("#tr_street").removeClass("tr_highlight");
    $("#tr_floor").removeClass("tr_highlight");
    $("#tr_office").removeClass("tr_highlight");

    $('#in_tc').removeAttr("disabled");


    if (in_tc == 1) {

        if (type == 'tc') {
            $('#in_tc').attr("checked", false);
            $('#in_tc').attr('disabled','disabled');
            set_type();
            return false;
        }

        $('#tr_tc').show();         $("#tr_tc").addClass("tr_highlight");
        $('#tr_ground').show();     $("#tr_ground").addClass("tr_highlight");
        $('#tr_street').show();     $("#tr_street").addClass("tr_highlight");
        $('#tr_floor').show();      $("#tr_floor").addClass("tr_highlight");
        $('#tr_office').show();     $("#tr_office").addClass("tr_highlight");

        

    } else {

        switch(type) {
            case 'shop':
                $('#tr_street').show(); $('#required_street').show();
                $('#tr_house').show();  $('#required_house').show();
                $('#tr_floor').show();
                $('#tr_office').show();
                $('#tr_alternative').show();
                break;

            case 'ishop':
                break;

            case 'personal':
                $('#tr_street').show();
                $('#tr_house').show();
                $('#tr_floor').show();
                $('#tr_office').show();
                $('#tr_alternative').show();
                break;

            case 'tc':
                $('#tr_street').show(); $('#required_street').show();
                $('#tr_house').show();  $('#required_house').show();

                $('#in_tc').attr("checked", false);
                $('#in_tc').attr('disabled', 'disabled');
                break;

            default:
                break;
        }
        
    }
}



function add_foto_shop(url, title_value){


    if (photo_num > 9) {
        alert('Кол-во фоторгафий превышает допустимое!');
    }
    else {

        var glob_div = document.createElement('div');
        glob_div.setAttribute("style", 'margin-bottom: 30px; height: 80px; float: none;');

        $('#photos_uploaded').append(glob_div);

                var del_div = document.createElement('div');
                del_div.setAttribute("width", '25');
                del_div.setAttribute("style", 'float:left; margin-right: 10px; padding-top:25px;');

                glob_div.appendChild(del_div);

                    var a = document.createElement('a');
                    a.setAttribute("href", "#");
                    if (isBrowserPloh() != 0) {
                        a.setAttribute("onclick", new Function("delete_photo(this); return false;"));
                    }
                    else {
                        a.setAttribute("onclick", "delete_photo(this); return false;");
                    }


                    var a_img = document.createElement('img');
                    a_img.setAttribute("src", "/i/ico-delete.gif");

                    a.appendChild(a_img);

                    del_div.appendChild(a);
                    

                var photo_div = document.createElement('div');
                photo_div.setAttribute("style", 'float:left; margin-right: 10px; width: 120px; ');

                glob_div.appendChild(photo_div);



                    var img = document.createElement('img');
                    img.setAttribute("src", "/uploads/userfiles/" + uid + "/companies/100x100/" + url);
                    img.setAttribute("class", 'shop_add_img');

                    photo_div.appendChild(img);


                var title_div = document.createElement('div');
                title_div.setAttribute("height", '120');
                title_div.setAttribute("style", 'float:left; margin-right: 10px;');

                glob_div.appendChild(title_div);


                    var text = document.createElement('span');
                    text.innerHTML = 'Описание:';

                    title_div.appendChild(text);
                    title_div.appendChild(document.createElement('br'));


                    var title = document.createElement('input');
                    title.setAttribute("type", "text");
                    title.setAttribute("name", "photos_title[" + photo_id + "]");
                    title.setAttribute("value", title_value);
                    title.setAttribute("id", "photos_title[" + photo_id + "]");
                    title.setAttribute("class", "input_w_max");

                    title_div.appendChild(title);

                    
                var input = document.createElement('input');
                input.setAttribute("type", "hidden");
                input.setAttribute("name", "photos[" + photo_id + "]");
                input.setAttribute("value", url);
                input.setAttribute("id", "photos[" + photo_id + "]");

                glob_div.appendChild(input);
                

       var clear_div = document.createElement('div');
       clear_div.setAttribute("class", 'clear');

       $('#photos_uploaded').append(clear_div);


        photo_id++;
        photo_num++;
    }

}



function add_foto_product(url, title_value){

    if (photo_num > 9) {
        alert('Кол-во фоторгафий превышает допустимое!');
    }
    else {

        var glob_div = document.createElement('div');
        glob_div.setAttribute("style", 'margin-bottom: 30px; height: 80px; float: none;');

        $('#photos_uploaded').append(glob_div);


                var del_div = document.createElement('div');
                del_div.setAttribute("width", '25');
                del_div.setAttribute("style", 'float:left; margin-right: 10px; padding-top:25px;');

                glob_div.appendChild(del_div);

                    var a = document.createElement('a');
                    a.setAttribute("href", "#");
                    if (isBrowserPloh() != 0) {
                        a.setAttribute("onclick", new Function("delete_photo(this); return false;"));
                    }
                    else {
                        a.setAttribute("onclick", "delete_photo(this); return false;");
                    }


                    var a_img = document.createElement('img');
                    a_img.setAttribute("src", "/i/ico-delete.gif");

                    a.appendChild(a_img);

                    del_div.appendChild(a);


                var photo_div = document.createElement('div');
                photo_div.setAttribute("style", 'float:left; margin-right: 10px; width: 120px; ');

                glob_div.appendChild(photo_div);



                    var img = document.createElement('img');
                    img.setAttribute("src", "/uploads/userfiles/" + uid + "/products/100x100/" + url);
                    img.setAttribute("class", 'shop_add_img');

                    photo_div.appendChild(img);


                var title_div = document.createElement('div');
                title_div.setAttribute("height", '120');
                title_div.setAttribute("style", 'float:left; margin-right: 10px;');

                glob_div.appendChild(title_div);


                    var text = document.createElement('span');
                    text.innerHTML = 'Описание:';

                    title_div.appendChild(text);
                    title_div.appendChild(document.createElement('br'));


                    var title = document.createElement('input');
                    title.setAttribute("type", "text");
                    title.setAttribute("name", "photos_title[" + photo_id + "]");
                    title.setAttribute("value", title_value);
                    title.setAttribute("id", "photos_title[" + photo_id + "]");
                    title.setAttribute("class", "input_w_max");

                    title_div.appendChild(title);


                var input = document.createElement('input');
                input.setAttribute("type", "hidden");
                input.setAttribute("name", "photos[" + photo_id + "]");
                input.setAttribute("value", url);
                input.setAttribute("id", "photos[" + photo_id + "]");

                glob_div.appendChild(input);

        photo_id++;
        photo_num++;
        
    }
}



function delete_photo(element){
    var B = element.parentNode.parentNode;
    B.parentNode.removeChild(B);
    photo_num--;
}

function check_cpu(shop_id){
    var single_title = $('#single_title').val();

    $.ajax({
        type: "POST",
        url: "/advertiser/shops/check_cpu",
        data: "single_title=" + single_title + "&shop_id=" + shop_id,
        success: function(msg){
            if (single_title != msg) {
                $("#single_title").attr('style', 'background: red;');
            }
            else {
                $("#single_title").attr('style', 'background: none;');
            }
        }
    });
}


function generate_cpu(shop_id){
    var title = $('#title').val();

    $.ajax({
        type: "POST",
        url: "/advertiser/shops/check_cpu",
        data: "single_title="+title+"&shop_id="+shop_id,
        success: function(msg){
            $("#single_title").val(msg);
            check_cpu(shop_id);
        }
    });
}

function isBrowserPloh(){
    if (navigator.userAgent.match(/MSIE 6/i) != null)
        return 1;
    else
    if (navigator.userAgent.match(/MSIE 7/i) != null)
        return 2;
    else
        return 0;
}

function phones_add(value, value2){
    $('#phones').append('<p><input type="text" name="phone[]" value="' + value + '" onchange="phones(this);" class="input_w_mid">\n\
       Имя: <input type="text" name="phone_name[]" value="' + value2 + '" class="input_w_mid" />\n\
       <a href="#" onclick="delete_element(this); return false;"><img src="/i/ico-delete.gif" alt=""></a><br></p>');
}

function delete_element(element){
    var B = element.parentNode;
    B.parentNode.removeChild(B);
}

function ajax_set_region(){
    var country_id = $('#country').attr('value');

    $.ajax({
        type: "POST",
        url: "/global_ajax/get_region",
        data: "country_id=" + country_id,
        async: false,
        cache: true,
        success: function(response){

            $('#region').removeAttr("disabled");

            if (country_id == 0) {
                $('#region').attr("disabled", "disabled");
            }

            $('#region').html(response);

            ajax_set_city();
        }
    });
}

function ajax_set_city(){
    var country_id = $('#country').attr('value');
    var region_id = $('#region').attr('value');

    $.ajax({
        type: "POST",
        async: false,
        cache: true,
        url: "/global_ajax/get_city",
        data: "country_id=" + country_id + "&region_id=" + region_id,
        success: function(response){
            $('#city').html(response);
            $('#city').removeAttr("disabled");

            if (country_id == 0 || region_id == 0) {
                $('#city').attr("disabled", "disabled");
            }

            ajax_set_tc();
        }
    });
}

function ajax_set_tc(){

    var country_id = $('#country').attr('value');
    var region_id = $('#region').attr('value');
    var city_id = $('#city').attr('value');

    $.ajax({
        type: "POST",
        async: false,
        cache: true,
        url: "/global_ajax/get_tc",
        data: "country_id=" + country_id + "&region_id=" + region_id+"&city_id="+city_id,
        success: function(response){

            $('#trade_center').html(response);
            $('#trade_center').removeAttr("disabled");

            if (country_id == 0 || region_id == 0 || city_id == 0) {
                $('#trade_center').attr("disabled", "disabled");
            }

        }
    });

}












/*
 *
 * Search
 *
 */

function ajax_set_region_search(){
    var country_id = document.getElementById('country_search').value;

    $.ajax({
        type: "POST",
        url: "/global_ajax/get_region",
        data: "country_id=" + country_id,
        async: false,
        success: function(response){
            $('#region_search').removeAttr("disabled");

            if (country_id == 0) {
                $('#region_search').attr("disabled", "disabled");
            }

            $('#region_search').html(response);
            ajax_set_city();
        }
    });
}

function ajax_set_city_search(){
    var country_id = document.getElementById('country_search').value;
    var region_id = document.getElementById('region_search').value;

    $.ajax({
        type: "POST",
        async: false,
        url: "/global_ajax/get_city",
        data: "country_id=" + country_id + "&region_id=" + region_id,
        success: function(response){
            $('#city_search').html(response);
            $('#city_search').removeAttr("disabled");

            if (country_id == 0 || region_id == 0) {
                $('#city_search').attr("disabled", "disabled");
            }

            ajax_set_tc();
        }
    });
}


function empty( mixed_var ) {
    return ( mixed_var === "" || mixed_var === 0   || mixed_var === "0" || mixed_var === null  || mixed_var === false || mixed_var === undefined );
}


function select_language(elem){
    location.href = '/set_language/'+$(elem).val();    
}

function select_country(elem){
    location.href = '/set_country/'+$(elem).val();
}