﻿function ToTranslit(val) {
    val = val.replace(/\а/g, 'a');
    val = val.replace(/\б/g, 'b');
    val = val.replace(/\в/g, 'v');
    val = val.replace(/\г/g, 'g');
    val = val.replace(/\д/g, 'd');
    val = val.replace(/\е/g, 'e');
    val = val.replace(/\ё/g, 'jo');
    val = val.replace(/\ж/g, 'zh');
    val = val.replace(/\з/g, 'z');
    val = val.replace(/\и/g, 'i');
    val = val.replace(/\й/g, 'j');
    val = val.replace(/\к/g, 'k');
    val = val.replace(/\л/g, 'l');
    val = val.replace(/\м/g, 'm');
    val = val.replace(/\н/g, 'n');
    val = val.replace(/\о/g, 'o');
    val = val.replace(/\п/g, 'p');
    val = val.replace(/\р/g, 'r');
    val = val.replace(/\с/g, 's');
    val = val.replace(/\т/g, 't');
    val = val.replace(/\у/g, 'u');
    val = val.replace(/\ф/g, 'f');
    val = val.replace(/\х/g, 'h');
    val = val.replace(/\ц/g, 'c');
    val = val.replace(/\ч/g, 'ch');
    val = val.replace(/\ш/g, 'sh');
    val = val.replace(/\щ/g, 'sch');
    val = val.replace(/\ъ/g, "'");
    val = val.replace(/\ы/g, 'y');
    val = val.replace(/\ь/g, "'");
    val = val.replace(/\э/g, 'e');
    val = val.replace(/\ю/g, 'u');
    val = val.replace(/\я/g, 'ja');
    val = val.replace(/\А/g, 'A');
    val = val.replace(/\Б/g, 'B');
    val = val.replace(/\В/g, 'V');
    val = val.replace(/\Г/g, 'G');
    val = val.replace(/\Д/g, 'D');
    val = val.replace(/\Е/g, 'E');
    val = val.replace(/\Ё/g, 'JO');
    val = val.replace(/\Ж/g, 'ZH');
    val = val.replace(/\З/g, 'Z');
    val = val.replace(/\И/g, 'I');
    val = val.replace(/\Й/g, 'J');
    val = val.replace(/\К/g, 'K');
    val = val.replace(/\Л/g, 'L');
    val = val.replace(/\М/g, 'M');
    val = val.replace(/\Н/g, 'N');
    val = val.replace(/\О/g, 'O');
    val = val.replace(/\П/g, 'P');
    val = val.replace(/\Р/g, 'R');
    val = val.replace(/\С/g, 'S');
    val = val.replace(/\Т/g, 'T');
    val = val.replace(/\У/g, 'U');
    val = val.replace(/\Ф/g, 'F');
    val = val.replace(/\Х/g, 'H');
    val = val.replace(/\Ц/g, 'C');
    val = val.replace(/\Ч/g, 'CH');
    val = val.replace(/\Ш/g, 'SH');
    val = val.replace(/\Щ/g, 'SCH');
    val = val.replace(/\Ъ/g, "'");
    val = val.replace(/\Ы/g, 'Y');
    val = val.replace(/\Ь/g, "'");
    val = val.replace(/\Э/g, 'E');
    val = val.replace(/\Ю/g, 'U');
    val = val.replace(/\Я/g, 'JA');
    return val;
}
/*
function MakeAlias(value) {
    var reg = /[\x21-\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5e\x60\x7b\x7d\xa3]/ig;
    var regS = /[\s\x2f]/ig;
    var regRus = /[\u0400-\u04FF]/ig;
    return ToTranslit(value).toLowerCase().replace(reg, "").replace(regRus, "").replace(regS, "_").replace(/\x2D{2,}/ig, '_').replace(/№/g, "").replace(/\?/g, "").replace(/_{2,}/g, "");
}*/

function isExistError() {
    var isError = false;
    $(".validate").each(function() {
        var currentInput = $(this);
        if (currentInput.val().length <= 0) {
            currentInput.addClass("err-border");
            
            currentInput.unbind("keypress").keypress(function() {
                var input = $(this);
                input.unbind("keypress");
                input.removeClass("err-border");
            });
            isError = true;
        }
    });
    $(".validateMail").each(function() {
        var currentInput = $(this);
        if (!currentInput.val().match(/^([a-zA-Z0-9\._-]+?@[a-zA-Z0-9\._-]+\.[a-zA-Z]{2,4})$/)) {
            currentInput.addClass("err-border");

            currentInput.unbind("keyup").keyup(function() {
                
                var input = $(this);
                if (input.val().match(/^([a-zA-Z0-9\._-]+?@[a-zA-Z0-9\._-]+\.[a-zA-Z]{2,4})$/)) {
                    input.removeClass("err-border");
                    input.unbind("keyup");
                }
            });
            isError = true;
        }
    });
    /*
    $(".validateNum").each(function() {
        var currentInput = $(this);
        if (!currentInput.val().match(/^([a-zA-Z0-9\._-]+?@[a-zA-Z0-9\._-]+\.[a-zA-Z]{2,4})$/)) {
            currentInput.addClass("err-border");

            currentInput.unbind("keypress").keypress(function() {

                var input = $(this);
                if (input.val().match(/^([a-zA-Z0-9\._-]+?@[a-zA-Z0-9\._-]+\.[a-zA-Z]{2,4})$/)) {
                    input.toggleClass("err-border");
                    input.unbind("keypress");
                }
            });
            isError = true;
        }
    });*/
    return isError;
}

function MoveToFirstUnvalid() {
    $(document).scrollTo($(".err-border:first"), 800, { offset: -10 });
}

$(function() {
    $(".validationButton").click(function() { if (isExistError()) { $.notifier("Некоторые поля заполнены неверно.", "error"); MoveToFirstUnvalid(); return false; } });
    $(".validateNum").keypress(function(event) {
        if (((event.keyCode >= 35) && (event.keyCode <= 46))||(event.keyCode ==190)) {
            return true;
        }
        else if (((event.which > 46) && (event.which < 57)))
            
            return true;
        else
            return false;
    });
});
$(function() {
    //var currentOffset = document.documentElement.scrollTop || document.body.scrollTop; // body for Safari	
    //var containerHeight = document.getElementById('complectation-table-holder').clientHeight;
    //var containerTop = document.getElementById('complectation-table-holder').offsetTop;
    //var desiredOffset = currentOffset - height;
    //var cssObj = {
    //    "padding-top": "50px"
    //  }
    //$('#complectation-price').click(function() {alert(containerHeight+" "+containerTop)});
    //$('#complectation-price').css(cssObj);

    $('ul.choose-search li').not(".all").click(function() {
        if ($('ul.choose-search li').hasClass('here')) {
            $('ul.choose-search li').removeClass('here')
        }
        typelink = $(this).children("a")
        $('#sitesearch').attr("value", typelink.attr('rel'));
        $(this).addClass('here');
        return false
    });

    $('li.show-extend').hover(function() {
        $(this).parent().find('.hover').not($(this)).removeClass('hover')
        $(this).addClass('hover')
    }, function() {
        $(this).removeClass('hover')
    });

    $('li.show-extend a.expand').click(function() {
        if ($('li#current-nav').not($(this)).hasClass('selected')) {
            hidenav($('li#current-nav'))
        }

        $(this).parent().parent().addClass('selected').attr("id", "current-nav");
        $("#current-nav").mousedown(function() { return false });

        $(document).bind("mousedown", function() {
            var elnav = $("#current-nav");
            hidenav(elnav)
        })

        $("#current-nav .hide-expand").bind("click", function() {
            var elnav = $("#current-nav");
            hidenav(elnav)
        });

        if ($(this).attr("id") == "catalog-li") {
            $('.search-vendor #inp-s').focus();
        }
        return false
    });

    //	$('#complectation-table tr.opt').click(function() {
    //		checkid = $(this).find("label");
    //		$('#complectation-table tr.opt').toggleCheckboxes("#"+checkid.attr('for')+"");
    //	});
    //	$('#complectation-table tr.opt, #packages-table tr.opt').toggle(function() {
    //			checkid = $(this).find("label");
    //			$("#"+checkid.attr('for')+"").attr("checked", "checked");
    //			if ($("#"+checkid.attr('for')+"").parent().parent().find('.label').hasClass('here')) {
    //			$("#"+checkid.attr('for')+"").parent().parent().find('.label').removeClass('here')
    //			};
    //			$("#"+checkid.attr('for')+"").parent().parent().find('.label').addClass('here');
    //		},
    //		function() {
    //			checkid = $(this).find("label");
    //			$("#"+checkid.attr('for')+"").attr("checked", "");
    //			$("#"+checkid.attr('for')+"").parent().parent().find('.label').removeClass('here');
    //	});

    $('div.bnav-bl, div.show-generation li, div.table-complectation tr.opt, div.other-complectation tr').hover(function() {
        $(this).addClass("hover");
    }, function() {
        $(this).removeClass("hover");
    });

    $('ul.upload-photos a.ico-del').hover(function() {
        $(this).parent().addClass("hover");
    }, function() {
        $(this).parent().removeClass("hover");
    });

    $('div.show-generation li, div.other-complectation tr').click(function() {
        golink = $(this).find("a");
        window.location = golink.attr('href')
    });

    $('#search-inp').bind("keyup", function() {
        var searchval = $(this).val()
        if (searchval.length < 0 || searchval == '') {
            $('.search-block .reset').hide()
            //alert('del');
        }
        else {
            $('.search-block .reset').show()
            //alert('add')
            $('.search-block .reset').click(function() {
                $('#search-inp').focus()
                $(this).hide()
            });
        }
    });

    $('.search-vendor .reset').click(function() {
        $('#inp-s').val("")
        $('dl#marks dd').removeClass('result')
    })

    //	$('div.report-error a').click(function() {	
    //		window.open( $(this).attr('href') );
    //      return false;
    //})

    //$('div.report-error a').click(function() {
    //	$("body","html").css({height: "100%", width: "100%"}); 
    //	if (typeof document.body.style.maxHeight === "undefined") {
    //		if (document.getElementById("TB_HideSelect") === null) {
    //			$("body").append("<iframe id='ie_HideSelect'></iframe><div id='overlay-page'></div><div id='error-page'></div>");
    //			$("#overlay-page").click(error_remove);
    //		}
    //	}else{
    //		if(document.getElementById("TB_overlay") === null){
    //			$("body").append("<div id='overlay-page'></div><div id='error-page'></div>");
    //			$("#overlay-page").click(error_remove);
    //		}
    //	}
    //	$("#error-page")
    //		.css({marginTop: document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop + 'px'})
    //		.load($(this).attr('href'), function(){
    //			$("#close-button").click(error_remove); 
    //		}
    //		)
    //	return false
    //});
});

function hidenav(elnav) {
    elnav.removeClass("selected");
    elnav.attr("id", "")
    $(document).bind("mousedown", function() {
        return true
    })
}

function error_remove() {
    $("#close-button").unbind("click");
    $("#error-page, #overlay-page, #ie_HideSelect").trigger("unload").unbind().remove()
    $("body", "html").css({ height: "", width: "" });
}

//New Header (Countries, Search and other)
$(function() {
    $("#searchText").unbind("keydown").keydown(function(e) {
        var keynum;
        if (window.event) // IE
            keynum = e.keyCode;
        else if (e.which) // Netscape/Firefox/Opera
            keynum = e.which;
        if (keynum == 13 && $(this).val() == "")
            return false;
        else
            return keynum;
    });
});

$(document).bind("mousedown", function() {
    $("#aspnetForm").removeAttr("onsubmit");
    ResetPopups();
});

function ResetPopups() {
    $('#divCountry').removeClass('tn-region-open');
    $('#divUser').removeClass('b-usernav-open');
    $('#divSearch').removeClass('tn-search-open');
    $('#divFave').removeClass('tn-fave-open');
    $("#dlArticleBookmark").removeClass("b-social-bkmrk-open");
}

function CountryClick() {
    $('#divUser').removeClass('b-usernav-open');
    $('#divSearch').removeClass('tn-search-open');
    $('#divFave').removeClass('tn-fave-open');
    $('#divCountry').toggleClass('tn-region-open');
    $("#divCountry").mousedown(function() { return false });
}

function FaveClick() {
    $('#divCountry').removeClass('tn-region-open');
    $('#divUser').removeClass('b-usernav-open');
    $('#divSearch').removeClass('tn-search-open');
    $('#divFave').toggleClass('tn-fave-open');
    $("#divFave").mousedown(function() { return false });
}

function UserClick() {
    $('#divCountry').removeClass('tn-region-open');
    $('#divSearch').removeClass('tn-search-open');
    $('#divFave').removeClass('tn-fave-open');
    $('#divUser').toggleClass('b-usernav-open');
    $("#divUser").mousedown(function() { return false });
}

function SearchClick() {
    $('#divCountry').removeClass('tn-region-open');
    $('#divUser').removeClass('b-usernav-open');
    $('#divFave').removeClass('tn-fave-open');
    $('#divSearch').toggleClass('tn-search-open');
    $("#divSearch").mousedown(function() { $('#searchtext').focus(); return false });
    $('input#searchText').focus();
}
//End New Header