var d = document;
var winIE = (navigator.userAgent.indexOf("Opera")==-1 && (d.getElementById && d.documentElement.behaviorUrns)) ? true : false;
tabs=0;
function show(b,r,u,i){ 
    var r_elem = document.getElementById(r);
    var u_elem = document.getElementById(u);
    if    (u_elem.style.display=='none'){ 
        for    (c=0;c<=i;c++){
            setTimeout(r_elem.id+'.style.height='+c+';if('+c+'=='+i+'){'+u_elem.id+'.style.display=\'\'}',3*c)
        }
        document.getElementById('stext').innerHTML="Скрыть поиск";
        u_elem.style.display='block';
    }
    else{ 
        u_elem.style.display='none' 
        for (c=i;c>0;c--){
            setTimeout('tt('+c+','+r_elem.id+')',3*(i-c))
        }
        if    (c==0){
            document.getElementById('stext').innerHTML="Показать поиск";
        }
    }
} 

function tt(c,r){ 
    r.style.height=c
} 
function Reg() {
    if (document.getElementById('status').value=='Юридическое лицо') {
        document.getElementById('spec').innerHTML='*';
        document.getElementById('comp').innerHTML='*';
        document.getElementById('dol').innerHTML='*';
    } else {
        document.getElementById('spec').innerHTML='';
        document.getElementById('comp').innerHTML='';
        document.getElementById('dol').innerHTML='';
    }
}
function SetCities(num, place) {
    var oCitiesList = document.getElementById(place+'_cities');
    oCitiesList.length = 0; // удаляем все элементы из списка
    if (document.getElementById('countries'+num)) {
        sCurrCountry = document.getElementById('countries'+num).value;
        if (aCities[sCurrCountry]) {
            aCntCities = aCities[sCurrCountry];
            for (i in aCntCities) {
                if (document.createElement){
                    var newCitiesListOption = document.createElement("OPTION");
                    newCitiesListOption.text = aCntCities[i];
                    newCitiesListOption.value = aCntCities[i];
                    if (aCntCities[i]==(document.getElementById('selected').innerHTML)) newCitiesListOption.selected=true;
                    // тут мы используем для добавления элемента либо метод IE, либо DOM, которые, alas, не совпадают по параметрам…
                    (oCitiesList.options.add) ? oCitiesList.options.add(newCitiesListOption) : oCitiesList.add(newCitiesListOption, null);
                }else{
                    // для NN3.x-4.x
                    oCitiesList.oCitiesList.options[i] = new Option(aCntCities[i], aCntCities[i], false, false);
                }
            }
        }
    }
    SelCity='';
}
function SetCitiesRss() {
    var oCitiesList = document.getElementById('rss_cities');
    oCitiesList.length = 0; // удаляем все элементы из списка
    if (document.getElementById('rss_countries')) {
        sCurrCountry = document.getElementById('rss_countries').value;
        if (aCities[sCurrCountry]) {
            aCntCities = aCities[sCurrCountry];
            for (i in aCntCities) {
                if (document.createElement){
                    var newCitiesListOption = document.createElement("OPTION");
                    newCitiesListOption.text = aCntCities[i];
                    newCitiesListOption.value = aCntCities[i];
                    if (aCntCities[i]==(document.getElementById('selected').innerHTML)) newCitiesListOption.selected=true;
                    // тут мы используем для добавления элемента либо метод IE, либо DOM, которые, alas, не совпадают по параметрам…
                    (oCitiesList.options.add) ? oCitiesList.options.add(newCitiesListOption) : oCitiesList.add(newCitiesListOption, null);
                }else{
                    // для NN3.x-4.x
                    oCitiesList.oCitiesList.options[i] = new Option(aCntCities[i], aCntCities[i], false, false);
                }
            }
        }
    }
    SelCity='';
}
$(document).ready(function() {
    SetCitiesRss();
});


function init(){
    if(winIE) {
        bodySize();
    }
}

function sendForm(form, value)
{ 
    if (value==1) form.action="login.php/action/process";
    if (value==2) form.action="affiliate_affiliate.php/action/process";
}
function form_client() {
    MM_changeProp('link_login','','<a href="http://expomap.ru/create_account.php">Регистрация</a>');
    MM_changeProp('link_psw','','<a href="http://expomap.ru/password_double_opt.php">Забыли пароль?</a>');
}
function form_partner() {
    MM_changeProp('link_login','','<a href="http://expomap.ru/affiliate_signup.php">Регистрация</a>');
    MM_changeProp('link_psw','','<a href="http://expomap.ru/affiliate_password_forgotten.php">Забыли пароль?</a>');
}


function MM_findObj(n, d) { //v4.01
    var p,i,x;
    if(!d) d=document;
    if((p=n.indexOf("?"))>0&&parent.frames.length) {
        d=parent.frames[n.substring(p+1)].document;
        n=n.substring(0,p);
    }
    if(!(x=d[n])&&d.all) x=d.all[n];
    for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
    for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
    if(!x && d.getElementById) x=d.getElementById(n);
    return x;
}

function MM_changeProp(objName,x,theValue) { //v6.0
    var obj = MM_findObj(objName);
    theProp='innerHTML';
    if (obj && (theProp.indexOf("style.")==-1 || obj.style)){
        if (theValue == true || theValue == false)
            eval("obj."+theProp+"="+theValue);
        else eval("obj."+theProp+"='"+theValue+"'");
    }
}
function MM_validateForm() { //v4.0
    if (document.getElementById){
        var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
        for (i=0; i<(args.length-2); i+=3) {
            test=args[i+2];
            val=document.getElementById(args[i]);
            if (val) {
                nm=val.name;
                if ((val=val.value)!="") {
                    if (test.indexOf('isEmail')!=-1) {
                        
                        p=val.indexOf('@');
                        if (p<1 || p==(val.length-1)) errors+='Поле Email должен содержать адрес существующего e-mail адреса !';
                    } else if (test!='R') {
                        num = parseFloat(val);
                        if (isNaN(val)) errors+='- '+nm+' должен содержать номер.\n';
                        if (test.indexOf('inRange') != -1) {
                            p=test.indexOf(':');
                            min=test.substring(8,p);
                            max=test.substring(p+1);
                            if (num<min || max<num) errors+='\n '+nm+' должен вмещать номер между '+min+' ?? '+max+'.\n';
                        }
                    }
                } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n';
            }
        }
        if (errors) alert(errors);
        document.MM_returnValue = (errors == '');
    }
}

function check_price(name){
    var x;
    if (name==1){
        if (document.all.stands.checked) {
            x=parseFloat(document.all.total_count.value);
            x=x+100.0000;
            document.all.total_count.value=x+'.00 €';
        } else {
            x=parseFloat(document.all.total_count.value);
            x=x-100.0000;
            document.all.total_count.value=x+'.00 €';
        }
    }
    if (name==2){
        if (document.all.photo.checked) {
            x=parseFloat(document.all.total_count.value);
            x+=100.0000;
            document.all.total_count.value=x+'.00 €';
        } else {
            x=parseFloat(document.all.total_count.value);
            x=x-100.0000;
            document.all.total_count.value=x+'.00 €';
        }
    }
    if (name==3){
        if (document.all.video.checked) {
            x=parseFloat(document.all.total_count.value);
            x=x+100.0000;
            document.all.total_count.value=x+'.00 €';
        } else {
            x=parseFloat(document.all.total_count.value);
            x=x-100.0000;
            document.all.total_count.value=x+'.00 €';
        }
    }
    if (name==4){
        if (document.all.official.checked) {
            x=parseFloat(document.all.total_count.value);
            x=x+50.0000;
            document.all.total_count.value=x+'.00 €';
        } else {
            x=parseFloat(document.all.total_count.value);
            x=x-50.0000;
            document.all.total_count.value=x+'.00 €';
        }
    }
}
function checkpack(oForm,ch){
    oForm["pack"].checked = false;
}
function checkother(oForm,ch){
    oForm["oficial"].checked = ch;
    oForm["ad"].checked = ch;
    oForm["buklet"].checked = ch;
    if (oForm["visitors"]) oForm["visitors"].checked = ch;
}

function addplace(choice){
    if (choice==1) {
		
        document.getElementById('count_studyblock').value=parseFloat(document.getElementById('count_studyblock').value)+1;
        document.getElementById('study').innerHTML+='<br />';
        document.getElementById('study').innerHTML+='<p><span style="padding-left:10px; padding-right:13px;">Учебное заведение</span>'+(document.getElementById('count_studyblock').value)+'. <input name="place'+(document.getElementById('count_studyblock').value)+'" type="text" style="width:152px; "></p>';
        document.getElementById('study').innerHTML+='<p><span style="padding-left:10px;padding-right:42px;">Специальность</span>'+(document.getElementById('count_studyblock').value)+'. <input name="spec'+(document.getElementById('count_studyblock').value)+'" type="text" style="width:152px; "></p>';
        document.getElementById('study').innerHTML+='<p><span style="padding-left:10px;padding-right:36px;">Дата окончания</span>'+(document.getElementById('count_studyblock').value)+'. <input name="stadyend'+(document.getElementById('count_studyblock').value)+'" type="text" style="width:152px; "></p>';
		
    }
    if (choice==2) {
        document.getElementById('count_workblock').value=parseFloat(document.getElementById('count_workblock').value)+1;
        document.getElementById('workplace').innerHTML+='<br />';
        document.getElementById('workplace').innerHTML+='<p><span style="padding-left:10px; padding-right:133px;">Дата начала</span>'+(document.getElementById('count_workblock').value)+'. <input name="datestart'+(document.getElementById('count_workblock').value)+'" type="text" style="width:152px; "></p>';
        document.getElementById('workplace').innerHTML+='<p><span style="padding-left:10px; padding-right:110px;">Дата окончания</span>'+(document.getElementById('count_workblock').value)+'. <input name="dateend'+(document.getElementById('count_workblock').value)+'" type="text" style="width:152px; "></p>';
        document.getElementById('workplace').innerHTML+='<p><span style="padding-left:10px; padding-right:87px;">Название компании</span>'+(document.getElementById('count_workblock').value)+'. <input name="company'+(document.getElementById('count_workblock').value)+'" type="text" style="width:152px; "></p>';
        document.getElementById('workplace').innerHTML+='<p><span style="padding-left:10px; padding-right:146px;">Должность</span>'+(document.getElementById('count_workblock').value)+'. <input name="job'+(document.getElementById('count_workblock').value)+'" type="text" style="width:152px; "></p>';
        document.getElementById('workplace').innerHTML+='<p><span style="padding-left:10px; padding-right:133px;">Обязанности</span>'+(document.getElementById('count_workblock').value)+'. <input name="work'+(document.getElementById('count_workblock').value)+'" type="text" style="width:152px; "></p>';
        document.getElementById('workplace').innerHTML+='<p><span style="padding-left:10px; padding-right:13px;">Рекомендации (ФИО, телефон)</span>'+(document.getElementById('count_workblock').value)+'. <input name="recommend'+(document.getElementById('count_workblock').value)+'" type="text" style="width:152px; "></p>';
    }
}

$(window).resize(function(){
    var WBody = 0;
    WBody = $(document).width();
    if (WBody<1100){
        $(".service .w-75 .w-25").css("min-width","165px");
        $(".services .header h1").css("font-size","16px");
        $(".services .header .w-px").css("margin-right","10px");
    } else if (WBody<1200){
        $(".service .w-75 .w-25").css("min-width","200px");
        $(".services .header h1").css("font-size","18px");
        $(".services .header .w-px").css("margin-right","10px");
    } else {
        $(".service .w-75 .w-25").css("min-width","inherit");
        $(".services .header h1").css("font-size","21px");
    }

});

jQuery(document).ready(function(){
    var WBody = 0;
    WBody = $(window).width();

    if (WBody<1100){
        $(".service .w-75 .w-25").css("min-width","165px");
        $(".services .header h1").css("font-size","16px");
        $(".services .header .w-px").css("margin-right","10px");
    } else if (WBody<1200){
        $(".service .w-75 .w-25").css("min-width","200px");
        $(".services .header h1").css("font-size","18px");
        $(".services .header .w-px").css("margin-right","10px");
    }
});
function servstyles(){
    $("#hserv1").attr("class","w-px");
    $("#hserv2").attr("class","w-px");
    $("#hserv3").attr("class","w-px");
}
function serv(num){
    switch(num) {
        case 1:
            $("#serv1").css("display","block");
            $("#serv2").css("display","none");
            $("#serv3").css("display","none");

            servstyles();
            $("#hserv1").addClass("active");
            break;
        case 2:
            $("#serv1").css("display","none");
            $("#serv2").css("display","block");
            $("#serv3").css("display","none");

            servstyles();
            $("#hserv2").addClass("active");
            break;
        case 3:
            $("#serv1").css("display","none");
            $("#serv2").css("display","none");
            $("#serv3").css("display","block");

            servstyles();
            $("#hserv3").addClass("active");
            break;
        default:
            $("#serv1").css("display","block");
            $("#serv2").css("display","none");
            $("#serv3").css("display","none");

            servstyles();
            $("#hserv1").addClass("active");
    }
}
// Obtaib GET parameters
function getVar(name)
{
    var get_string = document.location.search;
    var return_value = '';

    do { //This loop is made to catch all instances of any get variable.
        var name_index = get_string.indexOf(name + '=');

        if(name_index != -1)
        {
            get_string = get_string.substr(name_index + name.length + 1, get_string.length - name_index);

            var end_of_value = get_string.indexOf('&');
            var value = '';
            if(end_of_value != -1)
                value = get_string.substr(0, end_of_value);
            else
                value = get_string;

            if(return_value == '' || value == '')
                return_value += value;
            else
                return_value += ', ' + value;
        }
    } while(name_index != -1)

    //Restores all the blank spaces.
    var space = return_value.indexOf('+');
    while(space != -1)
    {
        return_value = return_value.substr(0, space) + ' ' +
        return_value.substr(space + 1, return_value.length);

        space = return_value.indexOf('+');
    }
    return(return_value);
}
function RssExpoFeed()
{
    if (!document.getElementById('expo_feed').checked &&
        !document.getElementById('conference_feed').checked)
        document.getElementById('rss_expo_criteria').style.display = 'none';
    else
        document.getElementById('rss_expo_criteria').style.display = 'block';
    RssCanSubmit();
}
function RssCanSubmit()
{
    if (!document.getElementById('expo_feed').checked &&
        !document.getElementById('conference_feed').checked &&
        !document.getElementById('news_feed').checked &&
        !document.getElementById('articles_feed').checked)
        document.getElementById('rss_submit').style.display = 'none';
    else
        document.getElementById('rss_submit').style.display = 'block';
}
function RssExpoTypeSelector()
{
    if (document.getElementById('rss_expo_types').value == 'expo_new_added')
        document.getElementById('rss_expo_periods').style.display = 'none';
    else
        document.getElementById('rss_expo_periods').style.display = 'block';
}

/*
 *
 * box_rubric
 * 
 */

// Autocompetion for search
$(document).ready(function () {
    $(document).bind('keydown', 'esc', function() {
        $('.listbox').hide();
    });
    $('#search-name').keyup(function(event) {
        // disable for esc, return, left, up, right, down, shift, alt gr/ctrl, alt
        // respectively
        if (event.keyCode != 27 &&
            event.keyCode != 13 &&
            event.keyCode != 37 &&
            event.keyCode != 38 &&
            event.keyCode != 39 &&
            event.keyCode != 40 &&
            event.keyCode != 16 &&
            event.keyCode != 17 &&
            event.keyCode != 18) {
            var uid = $('#search-name').val();
            var data = 'name=' + uid;
            $.ajax({
                type: "POST",
                url: "search_ajax.php",
                data: data,
                success: function(html) {
                    if (html) {
                        $('.listbox').html(html);
                        $('.listbox').show();
                    } else {
                        $('.listbox').hide();
                        $('.listbox').html('');
                    }
                }
            });
        }
    });
});
// Tree
$(document).ready(function() {
    if ($.browser.msie) {
        $("#cities-tree").jstree({
            "plugins" : [ "themes", "json_data", "ui", "checkbox" ],
            "themes" : {
                "theme" : "orange",
                "dots" : false,
                "icons" : false
            },
            "json_data" : {
                "ajax" : {
                    "url" : "/get_cities_json.php",
                    "data" : function(n) {
                        return {
                            id : n.attr ? n.attr("id").replace("country_", "") : 0
                        };
                    }
                }
            }
        });
    } else {
        $("#cities-tree").jstree({
            "plugins" : [ "themes", "json_data", "ui", "checkbox", "cookies" ],
            "themes" : {
                "theme" : "orange",
                "dots" : false,
                "icons" : false
            },
            "json_data" : {
                "ajax" : {
                    "url" : "/get_cities_json.php",
                    "data" : function(n) {
                        return {
                            id : n.attr ? n.attr("id").replace("country_", "") : 0
                        };
                    }
                }
            }
        });
    }
});
// Create hidden input for selected cities in tree
$(document).ready(function() {
    $("#advanced-search_form").submit(function () {
        $("#cities-tree").jstree("get_checked").each(function() {
            var checkedId = this.id;
            $("#advanced-search_form").append(
                '<input type="hidden" name="places[]" value="' + checkedId + '" />');
        });
    });
});
// Reset form
function reset_tree() {
    $("#cities-tree").jstree("uncheck_all");
    $("#cities-tree").jstree("close_all");
}
$(document).ready(function() {
    // if no POST data, reset tree (to prevent restore cache)
    if (!$.browser.msie) {
        if ($('#reset_form').val() == 'yes') {
            setTimeout("reset_tree()", 1500);
        }
    }
    // reset form by reset button
    $('#advanced-search_reset').click(function(event) {
        event.preventDefault();
        reset_tree();
        // $('#advanced-search_form')[0].reset(); // reset to defaults (not cleaning)
        $(':input','#advanced-search_form')
        .not(':button, :submit, :reset, :hidden')
        .val('')
        .removeAttr('checked')
        .removeAttr('selected');
    });
});
// Create date inputs
$(document).ready(function() {
    $('#date_start').datepicker({
        onSelect: function(dateText, inst) {
            $('#date_end').datepicker('option', 'minDate', dateText);
        }
    });
    $('#date_start').datepicker('option', {
        dateFormat: 'dd.mm.yy'
    });
    $('#date_end').datepicker({
        onSelect: function(dateText, inst) {
            $('#date_start').datepicker('option', 'maxDate', dateText);
        }
    });
    $('#date_end').datepicker('option', {
        dateFormat: 'dd.mm.yy'
    });
});

function subscribe_mail_validate() {
    var email = $('#email_subscribe').val();
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

    if (email == '' || reg.test(email) == false) {
        $('#email_warning').show();
        return false;
    } else {
        $('#email_warning').hide();
        return true;
    }
}
