////////////////////////////////////////////////////////////////////////////////
// Имя: work.js
// Описание: поиск
////////////////////////////////////////////////////////////////////////////////

function onsearh () {
    setCookie("countSearch",0,"Mon, 13-Jan-2015 00:00:00 GMT","/",".tehsvet.ru");
    setCookie("resultSearch","","Mon, 13-Jan-2015 00:00:00 GMT","/",".tehsvet.ru");
    var strSearch = document.getElementById("strSearch").value;
    strSearch = $.trim(strSearch);
    if (strSearch.length<3) {
        alert("\u0414л\u0438на строки запроса должна быть больше 2х символов");
    } else {
        var href = "search.php?strFind="+encodeURIComponent(strSearch);
        $.get(href, {
            ajax: true
        }, function (res) {

            $('.li2').removeClass('cli1').addClass('cli2');
            $('.li1').removeClass('cli2').addClass('cli1');
            $('.li3').removeClass('cli2').addClass('cli1');
            $('.li4').removeClass('cli2').addClass('cli1');
            $('.li5').removeClass('cli2').addClass('cli1');
            $('.li6').removeClass('cli2').addClass('cli1');
            $('.li7').removeClass('cli2').addClass('cli1');
            $('centerBlock').focus();

            var resSearch = res.split("@@");
            var countSearch = resSearch[0];
            var resultSearch = resSearch[1];
            setCookie("countSearch",countSearch,"Mon, 13-Jan-2015 00:00:00 GMT","/",".tehsvet.ru");
            setCookie("resultSearch",resultSearch,"Mon, 13-Jan-2015 00:00:00 GMT","/",".tehsvet.ru");

            var href1 = "index.php?search=1";
            $.get(href1, {
                ajax: true
            }, function (res1) {
                $("div.centerBlock").html(res1);
            });

        });
    }
}

// фильтр на вывод объявлений
$(function(){
    $('input[name="idCheck"]').live('click', function(){
        var id = $(this).attr('id');
        var typeAnnounce=0;
        // спрос
        if (id=="idCheck1"){
            typeAnnounce=1;
        } else {
            // предложение
            if (id=="idCheck2") {
                typeAnnounce=2;
            } else {
                // все
                typeAnnounce=0;
            }
        }
        setCookie("typeAnnounce",typeAnnounce,"Mon, 13-Jan-2015 00:00:00 GMT","/",".tehsvet.ru");
        var href="menu/announcement.php";
        $.get(href, {
            ajax: true
        }, function (res) {
            $("div.centerBlock").html(res);
        });
    })
});

// распечатка корзины (с фото, без)
$(function(){
    $('input[name="idPh"]').live('click', function(){
        var id = $(this).attr('id');
        var withРhoto = 0;
        // без фото
        if (id=="idPh1"){
            setCookie("withРhoto",1,"Mon, 13-Jan-2015 00:00:00 GMT","/",".tehsvet.ru");
            document.location = "printBasket0.php";
        } else {
            setCookie("withРhoto",0,"Mon, 13-Jan-2015 00:00:00 GMT","/",".tehsvet.ru");
            document.location = "printBasket.php";
        }
    })
});

// поиск по объявлениям
$(function() {
    $("#searchAnno").live("click", function() {
        setCookie("countSearchA",0,"Mon, 13-Jan-2015 00:00:00 GMT","/",".tehsvet.ru");
        setCookie("resultSearchA","","Mon, 13-Jan-2015 00:00:00 GMT","/",".tehsvet.ru");
        var strSearch = document.getElementById("textSearchAnn").value;
        strSearch = $.trim(strSearch);
        if (strSearch.length<3) {
            alert("\u0414л\u0438на строки запроса должна быть больше 2х символов");
        } else {
            var href = "searchAnnounce.php?strFind="+encodeURIComponent(strSearch);
            
            $.get(href, {
                ajax: true
            }, function (res) {
                $('.li2').removeClass('cli2').addClass('cli1');
                $('.li1').removeClass('cli2').addClass('cli1');
                $('.li3').removeClass('cli2').addClass('cli1');
                $('.li4').removeClass('cli2').addClass('cli1');
                $('.li5').removeClass('cli1').addClass('cli2');
                $('.li6').removeClass('cli2').addClass('cli1');
                $('.li7').removeClass('cli2').addClass('cli1');
                $('centerBlock').focus();

                var resSearch = res.split("@@");
                var countSearch = resSearch[0];
                var resultSearch = resSearch[1];
                setCookie("countSearchA",countSearch,"Mon, 13-Jan-2015 00:00:00 GMT","/",".tehsvet.ru");
                setCookie("resultSearchA",resultSearch,"Mon, 13-Jan-2015 00:00:00 GMT","/",".tehsvet.ru");

                var href1 = "index.php?is_announcement=1&is_search=1&rand="+Math.random();
                $.get(href1, {
                    ajax: true
                }, function (res1) {
                    $("div.centerBlock").html(res1);
                });

            });

        }
    });
});

//кнопка "отправить заявку"
$(function() {
    $("#btnSendOrder").live("click", function() {
        var href="index.php?is_demand=1&rand="+Math.random();
        $.get(href, {
            ajax: true
        }, function (res) {
            $("div.centerBlock").html(res);
        });
    });
});

//кнопка "очистить корзину"
$(function() {
    $("#btnClearBasket").live("click", function() {
        if (confirm("\u041e\u0447ист\u0438ть корзину?")) {
            setCookie("goodsArray", "", "Mon, 13-Jan-2015 00:00:00 GMT", "/", ".tehsvet.ru");
            setCookie("goodsCount", 0, "Mon, 13-Jan-2015 00:00:00 GMT", "/", ".tehsvet.ru");
            var href="index.php?is_basket=1&rand="+Math.random();
            $.get(href, {
                ajax: true
            }, function (res) {
                $("div.centerBlock").html(res);
            });
        }
    });
});

//кнопка "распечатать корзину"
$(function() {
    $("#btnPrintBasket").live("click", function() {
        window.open("printBasket.php");
    });
});

$(function() {
    $("#search").live("click", function() {
        onsearh();
    });
});

$(function() {
    $("#strSearch").keydown(function(event) {
        if (event.keyCode == 13 || event.keyCode == 10) {
            onsearh () ;
        }
    });
});


// распечатка заказа (с фото, без)
$(function(){
    $('input[name="idPhZ"]').live('click', function(){
        var id = $(this).attr('id');
        var idOrder = $('input[name="idOrder"]').val();
        // без фото
        if (id=="idPhZ1"){
            //var idOrder = $('input[name="idOrder"]').val();
            //setCookie("idOrder",idOrder,"Mon, 13-Jan-2015 00:00:00 GMT","/",".tehsvet.ru");
            document.location = "printOrder.php?idOrder="+idOrder;
        } else {
            //var idOrder = $('input[name="idOrder"]').val();
            //setCookie("idOrder",idOrder,"Mon, 13-Jan-2015 00:00:00 GMT","/",".tehsvet.ru");
            document.location = "printOrderF.php?idOrder="+idOrder;
        }
    })
});



// предварительная печать
$(function(){
    $('#printPre').live('click', function(){
        var href = "inc/printOrderFPre.php?clientType=";
        var clType = $("#clType").val();
        if (clType==0){
            // 0 - заявка от физического лица
            href = href+encodeURIComponent(clType);

            //указано название организации
            var name_org=document.getElementById("ordFio").value;
            name_org = $.trim(name_org);
            href = href+"&name_org="+encodeURIComponent(name_org);

            // регион
            var id_region=document.getElementById("ordReg").value;
            id_region = $.trim(id_region);
            href=href+"&id_region="+encodeURIComponent(id_region);

            // адрес
            var addres=document.getElementById("ordAdress").value;
            addres = $.trim(addres);
            href=href+"&addres="+encodeURIComponent(addres);

            // телефон
            var phone=document.getElementById("ordTel").value;
            phone = $.trim(phone);
            href=href+"&phone="+encodeURIComponent(phone);

            // е-мейл
            var email=document.getElementById("ordMail").value;
            email = $.trim(email);
            href=href+"&email="+encodeURIComponent(email);

            // паспортные данные
            var pasp=document.getElementById("ordPasp").value;
            pasp = $.trim(pasp);
            href=href+"&pasp="+encodeURIComponent(pasp);

            // откуда узнали
            var uzn = document.getElementById("uzn").value;
            uzn = $.trim(uzn);
            if (uzn==0){
                uzn = "";
            };
            href=href+"&uzn="+encodeURIComponent(uzn);

            // способ доставки
            if (spDos=="Другое") {
                dosKom = document.getElementById("bodyAnn1").value;
                dosKom = $.trim(dosKom);
                href=href+"&spDs="+encodeURIComponent(dosKom);
            } else {
                href=href+"&spDs="+encodeURIComponent(spDos);
            }

            // дополнения
            var bodyDop = document.getElementById("bodyDop").value;
            bodyDop = $.trim(bodyDop);
            href=href+"&bodyDop="+encodeURIComponent(bodyDop);

        } else {
            // 1 - заявка от юридического лица
            href = href+encodeURIComponent(clType);

            href = href +encodeURIComponent(clType);
            var nds = $('input[name="ndsType"]').filter(':checked').val();
            var inds = parseInt(nds);
            if (nds==null){
                inds=2;
            }
            href = href + "&nds=" + encodeURIComponent(inds);

            //указано название организации
            var name_org=document.getElementById("ordFio").value;
            name_org = $.trim(name_org);
            href = href+"&name_org="+encodeURIComponent(name_org);

            // регион
            var id_region=document.getElementById("ordReg").value;
            id_region = $.trim(id_region);
            href=href+"&id_region="+encodeURIComponent(id_region);

            // юр. адрес
            var addresCorp=document.getElementById("ordAdressCorp").value;
            addresCorp = $.trim(addresCorp);
            href=href+"&addresCorp="+encodeURIComponent(addresCorp);

            // адрес
            var addres=document.getElementById("ordAdress").value;
            addres = $.trim(addres);
            href=href+"&addres="+encodeURIComponent(addres);

            // телефон
            var phone=document.getElementById("ordTel").value;
            phone = $.trim(phone);
            href=href+"&phone="+encodeURIComponent(phone);

            // контактное лицо
            var ordKontL = document.getElementById("ordKontL").value;
            ordKontL = $.trim(ordKontL);
            href=href+"&ordKontL="+encodeURIComponent(ordKontL);

            // е-мейл
            var email=document.getElementById("ordMail").value;
            email = $.trim(email);
            href=href+"&email="+encodeURIComponent(email);

            // руководитель
            var fio_ruk=document.getElementById("orfRuk").value;
            fio_ruk = $.trim(fio_ruk);
            if (fio_ruk=="") {
                fio_ruk=""
            };
            href=href+"&fio_ruk="+encodeURIComponent(fio_ruk);
            // инн
            var inn_kpp=document.getElementById("ordINN").value;
            inn_kpp = $.trim(inn_kpp);
            if (inn_kpp==""){
                inn_kpp=""
            };
            href=href+"&inn_kpp="+encodeURIComponent(inn_kpp);

            // откуда узнали
            var uzn = document.getElementById("uzn").value;
            uzn = $.trim(uzn);
            if (uzn==0){
                uzn = "";
            };
            href=href+"&uzn="+encodeURIComponent(uzn);

            // способ доставки
            if (spDos=="Другое") {
                dosKom = document.getElementById("bodyAnn1").value;
                dosKom = $.trim(dosKom);
                href=href+"&spDs="+encodeURIComponent(dosKom);
            } else {
                href=href+"&spDs="+encodeURIComponent(spDos);
            }

            // дополнения
            var bodyDop = document.getElementById("bodyDop").value;
            bodyDop = $.trim(bodyDop);
            href=href+"&bodyDop="+encodeURIComponent(bodyDop);
        }

        window.open(href);

    })

});

// предварительная распечатка (с фото, без)
$(function(){
    $('input[name="idPhZPre"]').live('click', function(){
        var id = $(this).attr('id');
        // без фото
        if (id=="idPhZ1Pre"){
            var name_orgPre = $('input[name="name_orgPre"]').val();
            var href = "name_org="+encodeURIComponent(name_orgPre);

            var addresPre = $('input[name="addresPre"]').val();
            var href = href+"&addres="+encodeURIComponent(addresPre);

            var phonePre = $('input[name="phonePre"]').val();
            var href = href+"&phone="+encodeURIComponent(phonePre);

            var emailPre = $('input[name="emailPre"]').val();
            var href = href+"&email="+encodeURIComponent(emailPre);

            var fio_rukPre = $('input[name="fio_rukPre"]').val();
            var href = href+"&fio_ruk="+encodeURIComponent(fio_rukPre);

            var inn_kppPre = $('input[name="inn_kppPre"]').val();
            var href = href+"&inn_kpp="+encodeURIComponent(inn_kppPre);

            var id_regionPre = $('input[name="id_regionPre"]').val();
            var href = href+"&id_region="+encodeURIComponent(id_regionPre);

            var uznPre = $('input[name="uznPre"]').val();
            var href = href+"&uzn="+encodeURIComponent(uznPre);

            var spDsPre = $('input[name="spDsPre"]').val();
            var href = href+"&spDs="+encodeURIComponent(spDsPre);

            var ordKontLPre = $('input[name="ordKontLPre"]').val();
            var href = href+"&ordKontL="+encodeURIComponent(ordKontLPre);

            var bodyDopPre = $('input[name="bodyDopPre"]').val();
            var href = href+"&bodyDop="+encodeURIComponent(bodyDopPre);


            var cliTypePre = $('input[name="cliType"]').val();
            var href = href+"&clientType="+encodeURIComponent(cliTypePre);

            var ndsPre = $('input[name="ndss"]').val();
            var href = href+"&nds="+encodeURIComponent(ndsPre);

            var addressCorp = $('input[name="addresCorpPre"]').val();
            var href = href+"&addresCorp="+encodeURIComponent(addressCorp);

            var paspPre = $('input[name="pasp"]').val();
            var href = href+"&pasp="+encodeURIComponent(paspPre);

            document.location = "printOrderPre.php?"+href;
        } else {
                        var name_orgPre = $('input[name="name_orgPre"]').val();
            var href = "name_org="+encodeURIComponent(name_orgPre);

            var addresPre = $('input[name="addresPre"]').val();
            var href = href+"&addres="+encodeURIComponent(addresPre);

            var phonePre = $('input[name="phonePre"]').val();
            var href = href+"&phone="+encodeURIComponent(phonePre);

            var emailPre = $('input[name="emailPre"]').val();
            var href = href+"&email="+encodeURIComponent(emailPre);

            var fio_rukPre = $('input[name="fio_rukPre"]').val();
            var href = href+"&fio_ruk="+encodeURIComponent(fio_rukPre);

            var inn_kppPre = $('input[name="inn_kppPre"]').val();
            var href = href+"&inn_kpp="+encodeURIComponent(inn_kppPre);

            var id_regionPre = $('input[name="id_regionPre"]').val();
            var href = href+"&id_region="+encodeURIComponent(id_regionPre);

            var uznPre = $('input[name="uznPre"]').val();
            var href = href+"&uzn="+encodeURIComponent(uznPre);

            var spDsPre = $('input[name="spDsPre"]').val();
            var href = href+"&spDs="+encodeURIComponent(spDsPre);

            var ordKontLPre = $('input[name="ordKontLPre"]').val();
            var href = href+"&ordKontL="+encodeURIComponent(ordKontLPre);

            var bodyDopPre = $('input[name="bodyDopPre"]').val();
            var href = href+"&bodyDop="+encodeURIComponent(bodyDopPre);


            var cliTypePre = $('input[name="cliType"]').val();
            var href = href+"&clientType="+encodeURIComponent(cliTypePre);

            var ndsPre = $('input[name="ndss"]').val();
            var href = href+"&nds="+encodeURIComponent(ndsPre);

            var addressCorp = $('input[name="addresCorpPre"]').val();
            var href = href+"&addresCorp="+encodeURIComponent(addressCorp);

            var paspPre = $('input[name="pasp"]').val();
            var href = href+"&pasp="+encodeURIComponent(paspPre);
            
            document.location = "printOrderFPre.php?"+href;
        }
    })
});

// конопка физ лицо(форма заявки)
$(function(){
    $('#idPhysType').live('click', function(){
        var href = 'inc/physicalPerson.php';
        $('input[name="ordd"]').val(0);
        $.get(href, {
            ajax: true
        }, function (res) {
            $("div.orderClient").html(res);
        })
    })
});

// конопка юр лицо(форма заявки)
$(function(){
    $('#idCorpType').live('click', function(){
        var href = 'inc/corporatePerson.php';
        $('input[name="ordd"]').val(1);
        $.get(href, {
            ajax: true
        }, function (res) {
            $("div.orderClient").html(res);
        })
    })
});

/**
 * @see http://github.com/NV/placeholder.js
 */
jQuery.fn.textPlaceholder = function () {

    return this.each(function(){

        var that = this;

        if (that.placeholder && 'placeholder' in document.createElement(that.tagName)) return;

        var placeholder = that.getAttribute('placeholder');
        var input = jQuery(that);

        if (that.value === '' || that.value == placeholder) {
            input.addClass('text-placeholder');
            that.value = placeholder;
        }

        input.focus(function(){
            if (input.hasClass('text-placeholder')) {
                this.value = '';
                input.removeClass('text-placeholder')
            }
        });

        input.blur(function(){
            if (this.value === '') {
                input.addClass('text-placeholder');
                this.value = placeholder;
            } else {
                input.removeClass('text-placeholder');
            }
        });

        that.form && jQuery(that.form).submit(function(){
            if (input.hasClass('text-placeholder')) {
                that.value = '';
            }
        });

    });

};

