var prev;

var uri=location;

function ch(id) {
        document.getElementById('vacs_div').style.display = 'block';
        if (prev != 0) {
            document.getElementById('vs'+prev).style.display = 'none';
        }
        if (id != 0) {
            document.getElementById('vs'+id).style.display = 'block';
        } else {
            document.getElementById('vacs_div').style.display = 'none';
        }
}

//=====================================================

function popup(id, type) {
        if (type=='poll') {
                window.open("/loyalty/polls/id."+id+"/", "poll","width=270,height=270,top=200,left=200,titlebar=no,toolbar=no,location=no,statusbar=no,menubar=no,scrollbars=no,resizable=no");
        }

        if (type=='card') {
                window.open("/company/loyalty/card_order/order/", "card","width=388,height=320,top=200,left=200,titlebar=no,toolbar=no,location=no,statusbar=no,menubar=no,scrollbars=no,resizable=no");
        }

        if (type=='guest') {
                window.open("/loyalty/guestbook/answer/topic."+id+"/", "guest","width=386,height=310,top=200,left=200,titlebar=no,toolbar=no,location=no,statusbar=no,menubar=no,scrollbars=no,resizable=no");
        }

        if (type=='b') {
                if (id > 0) {
                        window.open("/corporate_clients/order/", "order","");
                } else {
                        window.open("/corporate_clients/order/", "order","");
                }
        }

        if (type=='bb') {
                if (id > 0) {
                        window.open("/corporate_clients/order/", "order","");
                } else {
                       window.open("/corporate_clients/order/", "order","");
                }
        }

        if (type=='t') {
               window.open("/corporate_clients/order/", "order","");
        }

        if (type=='trans') {
                window.open("/corporate_clients/order/", "order","");
        }

        if (type=='r') {
                if (id > 0) {
                       window.open("/corporate_clients/order/", "order","");
                } else {
                        window.open("/corporate_clients/order/", "order","");
                }

        }

        if (type=='h') {
                window.open("/corporate_clients/order/", "order","");
        }

        if (type=='vac') {
                window.open("/corporate_clients/order/", "order","");
        }

        if (type=='drive') {
                window.open('/drive.php?id='+id,'drive','width=625,height=590,top=100,left=100,titlebar=no,toolbar=no,location=no,statusbar=no,menubar=no,scrollbars=no,resizable=no;');
        }

        if (type=='emp') {
                window.open("/gallery/id."+id+"/type.emp/", "gallery","width=835,height=592,top=20,left=20,titlebar=no,toolbar=no,location=no,statusbar=no,menubar=no,scrollbars=no,resizable=no");
        }

        if (type>0) {
                window.open("/gallery/id."+id+"/type."+type+"/", "gallery","width=835,height=592,top=20,left=20,titlebar=no,toolbar=no,location=no,statusbar=no,menubar=no,scrollbars=no,resizable=no");
        }

}

function popcv(r, v) {
                window.open("/company/job/cv_send/rubric."+r+"/id."+v+"/", "cv_send","width=270,height=270,top=200,left=200,titlebar=no,toolbar=no,location=no,statusbar=no,menubar=no,scrollbars=no,resizable=no");
}

function poprub(r) {
                window.open("/company/job/cv_send/rubric."+r+"/", "cv_send","width=270,height=270,top=200,left=200,titlebar=no,toolbar=no,location=no,statusbar=no,menubar=no,scrollbars=no,resizable=no");
}

function res(w) {
        window.resizeTo(w,document.getElementById('maint').offsetHeight+57);
}

function res_wh(w, h) {
        window.resizeTo(w,h);
}

//=====================================================

function chck_interval(frm) {

        if ((frm.bm.value!=0) && (frm.by.value==0)) {
                alert('Укажите год начала периода!');
                return false;
        }

        if ((frm.em.value!=0) && (frm.ey.value==0)) {
                alert('Укажите год окончания периода!');
                return false;
        }


        if (((frm.by.value+frm.bm.value) > (frm.ey.value+frm.em.value)) && (frm.ey.value!=0)) {
                alert('Дата начала периода должна быть раньше даты окончания!');
                return false;
        }

        return true;
}

//======================================================

function openCloseBC(id, a) {
    var div = document.getElementById('centres_in_'+id);
    var next_div = document.getElementById('city_header_'+cities_chain[id]);

    if ((typeof(div) != 'undefined') && (div != null)) {
        if (div.style.display == 'none') {
            div.style.display = 'block';
            if (a.parentNode.className.indexOf('divh1') != -1) {
                a.parentNode.className = 'divh1 cl1';
            } else {
                a.parentNode.className = 'divh2 cl1';
            }
            a.style.color = '#FF0000';
            if ((typeof(next_div) != 'undefined') && (next_div != null)) {
                next_div.className = 'divh2';
            }
        } else {
            div.style.display = 'none';
            if (a.parentNode.className.indexOf('divh1') != -1) {
                a.parentNode.className = 'divh1';
            } else {
                a.parentNode.className = 'divh2';
            }
            a.style.color = '';
            if ((typeof(next_div) != 'undefined') && (next_div != null)) {
                next_div.className = 'divh1';
            }
        }
    }
}

//======================================================

function openPollDiv(id) {
    var div = document.getElementById('poll_div_'+id);
    var arrow = document.getElementById('arrow_for_'+id);
    var cursor = document.getElementById('cursor_div_'+id);

    if ((typeof(arrow) != 'undefined') && (arrow != null)) {
        arrow.style.display = 'none';
    }

    if ((typeof(cursor) != 'undefined') && (cursor != null)) {
        cursor.style.cursor = '';
    }

    if ((typeof(div) != 'undefined') && (div != null)) {
        div.style.display = 'block';
    }
}

function showAddForm() {
    var opener = document.getElementById('add_msg_opener');
    var div = document.getElementById('add_form');

    if ((typeof(opener) != 'undefined') && (opener != null)) {
        opener.style.display = 'none';
    }

    if ((typeof(div) != 'undefined') && (div != null)) {
        div.style.display = 'block';
    }
}

function badFieldFocus(field, str) {
    if (field.value == str) {
        field.value = '';
    }
}

function badFieldBlur(field, str) {
    if (field.value == '') {
        field.value = str;
    }
}

function clearNames() {
    var fld_comm = document.getElementById('comm');
    var fld_name = document.getElementById('name');
    var fld_mail = document.getElementById('mail');
    var fld_subj = document.getElementById('header');
    var fld_text = document.getElementById('txt');
    var fld_qnty = document.getElementById('quantity');
    var fld_perd = document.getElementById('period');
    var fld_phon = document.getElementById('phone');
    var fld_to_name = document.getElementById('to_name');
    var fld_to_mail = document.getElementById('to_mail');
    var fld_cv = document.getElementById('cv');
    var fld_count = document.getElementById('count');
	var fld_keystring = document.getElementById('keystring');


    if ((typeof(fld_name) != 'undefined') && (fld_name != null)) {
        fld_name.onfocus();
    }

    if ((typeof(fld_mail) != 'undefined') && (fld_mail != null)) {
        fld_mail.onfocus();
    }

    if ((typeof(fld_to_name) != 'undefined') && (fld_to_name != null)) {
        fld_to_name.onfocus();
    }

    if ((typeof(fld_to_mail) != 'undefined') && (fld_to_mail != null)) {
        fld_to_mail.onfocus();
    }

    if ((typeof(fld_subj) != 'undefined') && (fld_subj != null)) {
        fld_subj.onfocus();
    }

    if ((typeof(fld_text) != 'undefined') && (fld_text != null)) {
        fld_text.onfocus();
    }

    if ((typeof(fld_qnty) != 'undefined') && (fld_qnty != null)) {
        fld_qnty.onfocus();
    }

    if ((typeof(fld_perd) != 'undefined') && (fld_perd != null)) {
        fld_perd.onfocus();
    }

    if ((typeof(fld_phon) != 'undefined') && (fld_phon != null)) {
        fld_phon.onfocus();
    }

    if ((typeof(fld_cv) != 'undefined') && (fld_cv != null)) {
        fld_cv.onfocus();
    }

    if ((typeof(fld_comm) != 'undefined') && (fld_comm != null)) {
        fld_comm.onfocus();
    }

    if ((typeof(fld_count) != 'undefined') && (fld_count != null)) {
        fld_count.onfocus();
    }
	if ((typeof(fld_keystring) != 'undefined') && (fld_keystring != null)) {
        fld_keystring.onfocus();
    }
}

function turnBackNames() {
    var fld_comm = document.getElementById('comm');
    var fld_name = document.getElementById('name');
    var fld_mail = document.getElementById('mail');
    var fld_subj = document.getElementById('header');
    var fld_text = document.getElementById('txt');
    var fld_qnty = document.getElementById('quantity');
    var fld_perd = document.getElementById('period');
    var fld_phon = document.getElementById('phone');
    var fld_to_name = document.getElementById('to_name');
    var fld_to_mail = document.getElementById('to_mail');
    var fld_cv = document.getElementById('cv');
    var fld_count = document.getElementById('count');
	var fld_keystring = document.getElementById('keystring');


    if ((typeof(fld_comm) != 'undefined') && (fld_comm != null)) {
        fld_comm.onblur();
    }

    if ((typeof(fld_name) != 'undefined') && (fld_name != null)) {
        fld_name.onblur();
    }

    if ((typeof(fld_mail) != 'undefined') && (fld_mail != null)) {
        fld_mail.onblur();
    }

    if ((typeof(fld_to_name) != 'undefined') && (fld_to_name != null)) {
        fld_to_name.onblur();
    }

    if ((typeof(fld_to_mail) != 'undefined') && (fld_to_mail != null)) {
        fld_to_mail.onblur();
    }

    if ((typeof(fld_subj) != 'undefined') && (fld_subj != null)) {
        fld_subj.onblur();
    }

    if ((typeof(fld_text) != 'undefined') && (fld_text != null)) {
        fld_text.onblur();
    }

    if ((typeof(fld_qnty) != 'undefined') && (fld_qnty != null)) {
        fld_qnty.onblur();
    }

    if ((typeof(fld_perd) != 'undefined') && (fld_perd != null)) {
        fld_perd.onblur();
    }

    if ((typeof(fld_phon) != 'undefined') && (fld_phon != null)) {
        fld_phon.onblur();
    }

    if ((typeof(fld_cv) != 'undefined') && (fld_cv != null)) {
        fld_cv.onblur();
    }

    if ((typeof(fld_count) != 'undefined') && (fld_count != null)) {
        fld_count.onblur();
    }
	if ((typeof(fld_keystring) != 'undefined') && (fld_keystring != null)) {
        fld_keystring.onblur();
    }
}

function newsDtChange(path, form) {
    var new_href = path+'/dt.'+form.mnth.value+form.year.value+'/';
    location.href = new_href;
}

function inviteFriend() {
    var inv_btn = document.getElementById('invite_button');
    var inv_frm = document.getElementById('invite_div');

    if ((typeof(inv_frm) != 'undefined') && inv_frm && (typeof(inv_btn) != 'undefined') && inv_btn) {
        inv_btn.style.display = 'none';
        inv_frm.style.display = 'block';
    }
}

//==============================================================================
//        Calendar functions
var working_with = null;

function cShowCalendar(work_with) {
    working_with = work_with;
    cPrepareSelects();
    cWriteDays();
    document.getElementById('calendar').style.visibility = 'visible';
}

function cPrepareSelects() {
    if (working_with) {
        var mnth_select = document.getElementById('c_mnth');
        var year_select = document.getElementById('c_year');
        var dt_value = document.getElementById(working_with).value;

        var dt = /\d{2}\.(\d{2})\.(\d{4})/.exec(dt_value);

        if (dt) {
            for (i in mnth_select.childNodes) {
                if (mnth_select.childNodes[i].tagName == 'OPTION') {
                    if (mnth_select.childNodes[i].value == (dt[1] - 1)) {
                        mnth_select.value = mnth_select.childNodes[i].value;
                    }
                }
            }

            for (i in year_select.childNodes) {
                if (year_select.childNodes[i].tagName == 'OPTION') {
                    if (year_select.childNodes[i].value == dt[2]) {
                        year_select.value = year_select.childNodes[i].value;
                    }
                }
            }
        }
    }
}

function cWriteDays(change_date) {
    var days_div = document.getElementById('c_days');
    var days_num = cCountDays();
    var days = new Array();
    var i;
    var dt_value = document.getElementById(working_with).value;

    var dt = /(\d{2})\.\d{2}\.\d{4}/.exec(dt_value);

    if (dt && (dt[1] > days_num)) {
        dt[1] = days_num;
    }

    for (j in days_div.childNodes) {
        while ((typeof(days_div.childNodes[j]) != 'undefined') && (days_div.childNodes[j].tagName == 'DIV')) {
            days_div.removeChild(days_div.childNodes[j]);
        }
    }
    for (i = 1; i <= days_num; i++) {
        days[i] = document.createElement('div');
        if (dt && (i == (dt[1] -1 + 1))) {
            days[i].className = 'chisloactiv';
        } else {
            days[i].className = 'chislo';
        }
        days[i].id = 'day_'+i;
        days[i].innerHTML = i;
        days[i]['onclick'] = function() {cSelectDay(this);};
        days_div.appendChild(days[i]);
    }
    days['fake'] = document.createElement('div');
    days['fake'].className = 'chislo default_cursor';
    days['fake'].id = 'day_fake';
    days['fake'].innerHTML = '';
    days_div.appendChild(days['fake']);

    if (change_date) {
        var day_div = document.getElementById('day_'+dt[1]);
        cSelectDay(day_div);
    }
}

function cCountDays() {
    var mnth_select = document.getElementById('c_mnth');
    var year_select = document.getElementById('c_year');
    var i = 1;
    var calculated = false;

    if ((typeof(mnth_select) != 'undefined') && (typeof(year_select) != 'undefined') && mnth_select && year_select) {
        var mnth = mnth_select.value;
        var year = year_select.value;
        var dt = new Date(year, mnth, 1);
        var dt_tmp = new Date(year, mnth, 1);

        while ((i < 32) && (!calculated)) {
            dt_tmp.setDate(dt.getDate()+i);
            if (dt_tmp.getMonth() == dt.getMonth()) {
                i++;
            } else {
                calculated = true;
            }
        }
    }
    return i;
}

function cSelectDay(day_div) {
    var matches = /day_(\d+)/.exec(day_div.id);
    var mnth_select = document.getElementById('c_mnth');
    var year_select = document.getElementById('c_year');
    var dt_input = document.getElementById(working_with);

    var day = (matches[1] > 9) ? matches[1] : '0' + matches[1];
    var mnth = mnth_select.value - 1 + 2;
    mnth = (mnth > 9) ? mnth : '0' + mnth;
    var year = year_select.value;

    var dt = day + '.' + mnth + '.' + year;


    for (i in day_div.parentNode.childNodes) {
        if ((typeof(day_div.parentNode.childNodes[i]) != 'undefined') && (day_div.parentNode.childNodes[i].tagName == 'DIV')) {
            if (day_div.parentNode.childNodes[i].className == 'chisloactiv') {
                day_div.parentNode.childNodes[i].className = 'chislo';
            }
        }
    }
    day_div.className = 'chisloactiv';

    if (working_with == 'begin_dt') {
        var prefix = 'с ';
        var by = document.getElementById('by_input');
        var bm = document.getElementById('bm_input');
        var bd = document.getElementById('bd_input');
        by.value = year;
        bm.value = mnth;
        bd.value = day;
    } else if (working_with == 'ends_dt') {
        var prefix = 'по ';
        var ey = document.getElementById('ey_input');
        var em = document.getElementById('em_input');
        var ed = document.getElementById('ed_input');
        ey.value = year;
        em.value = mnth;
        ed.value = day
    } else {
        var prefix = '';
    }

    dt_input.value = prefix+dt;
}

function cOk() {
    document.getElementById('calendar').style.visibility = 'hidden';
}

//==============================================================================

function showCvForm() {
    var div = document.getElementById('cv_form');
    var show_btn = document.getElementById('cv_form_btn');

    if ((typeof(div) != 'undefined') && div) {
        div.style.display = 'block';
    }

    if ((typeof(show_btn) != 'undefined') && show_btn) {
        show_btn.style.display = 'none';
    }
}

//==============================================================================

function showCardOrderForm() {
    var btn = document.getElementById('card_order_btn');
    var div = document.getElementById('card_order_frm');

    if ((typeof(btn) != 'undefined') && btn) {
        btn.style.display = 'none';
    }

    if ((typeof(div) != 'undefined') && div) {
        div.style.display = 'block';
    }
}

//==============================================================================

function showParticipateTrnmFrm() {
    var btn = document.getElementById('participate_trnm_btn');
    var div = document.getElementById('participate_trnm');

    if ((typeof(btn) != 'undefined') && btn) {
        btn.style.display = 'none';
    }

    if ((typeof(div) != 'undefined') && div) {
        div.style.display = 'block';
    }
}

