var last_group_block = '';

function hide_block() {
    for(var i=0; i<arguments.length; i++) {
        element = document.getElementById(arguments[i]);
        if (element) { element.style.display = 'none'; }
    }
}

function show_block() {
    for(var i=0; i<arguments.length; i++) {
        element = document.getElementById(arguments[i]);
        if (element) { element.style.display = 'block'; }
    }
}

function submitAction(action_desc, question) {
    if (!question) {
        question = 'Вы действительно хотите';
    }
    if (!action_desc) {
        action_desc = 'выполнить данное действие';
    }
    question = question + ' ' + action_desc + '?';

    if (confirm(question)) {
        return true;
    }

    return false;
}

function confirmFeedbackAction(form, action, attention) {
    if (attention) {
        alert('Данное действие отменить будет невозможно!');
    }
    submit = submitAction(action);
    if (submit) {
        form.confirm.value='yes';
    }
    return submit;
}

function is_blank(str) {
    if(/\S+/.test(str))
    {
        return false;
    } else {
        return true;
    }
}

function check_form(the_form, form_fields, form_fields_names) {
    alert_string = "";
    focus_element = null;

    nom = 0;
    for (i = 0; i < form_fields.length; i++) {
        if (("checkbox" == the_form[form_fields[i]].type && false == the_form[form_fields[i]].checked) ||
        is_blank(the_form[form_fields[i]].value))
        {
            alert_string += ++nom + ". " + form_fields_names[i] + "\n";
            if (null == focus_element) focus_element = the_form[form_fields[i]];
        }
    }

    if ("" != alert_string) {
        alert(alert_string);
        focus_element.focus();
        return false;
    }

    return true;
}

function getPageSize(){

    var xScroll, yScroll;

    if (window.innerHeight && window.scrollMaxY) {
        xScroll = window.innerWidth + window.scrollMaxX;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }

    var windowWidth, windowHeight;

    if (self.innerHeight) {	// all except Explorer
        if(document.documentElement.clientWidth){
            windowWidth = document.documentElement.clientWidth;
        } else {
            windowWidth = self.innerWidth;
        }
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }

    if(yScroll < windowHeight){
        pageHeight = windowHeight;
    } else {
        pageHeight = yScroll;
    }

    if(xScroll < windowWidth){
        pageWidth = xScroll;
    } else {
        pageWidth = windowWidth;
    }

    oCanvas = document.getElementsByTagName((document.compatMode && document.compatMode == "CSS1Compat") ? "HTML" : "BODY")[0];
    scrollLeft = oCanvas.scrollLeft;
    scrollTop = oCanvas.scrollTop;

    arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight,scrollLeft,scrollTop);
    return arrayPageSize;
}

function disableBody () {
    var objOverlay = document.getElementById('overlay');
    var arrayPageSize = getPageSize();
    objOverlay.style.width = arrayPageSize[0]+'px';
    objOverlay.style.height = arrayPageSize[1]+'px';
    objOverlay.style.display = 'block';
}

function enableBody () {
    var objOverlay = document.getElementById('overlay');
    objOverlay.style.display = 'none';
}

function centerBlock (blockName) {
    disableBody();
    show_block(blockName);
    element = document.getElementById(blockName);
    var arrayPageSize = getPageSize();
    element.style.left = (arrayPageSize[4] + arrayPageSize[2]/2-element.clientWidth/2)+'px';
    element.style.top = (arrayPageSize[5] + arrayPageSize[3]/2-element.clientHeight /2)+'px';
}

function open_group_block (group_name) {
    hide_block(last_group_block);
    if (group_name == last_group_block) {
        hide_block(group_name);
        last_group_block = '';
    } else {
        show_block(group_name);
        last_group_block = group_name;
    }
}
