var canwork = 1;

function popup(url, name, width, height, menubar, toolbar, returnWindow)
{
    if (canwork)
    {
        canwork = 0;

        if (!name)      name    ="popup";
        if (!height)    height  = 500;
        if (!width)     width   = 830;
        if (!menubar)   menubar = "no";
        if (!toolbar)   toolbar = "no";

        if (name != 'popup')
        {
            eval('if (window.' + name + ' && !window.' + name + '.closed) ' + name + '.close();');
        }

        w = window.open(url, name,
                        "directories=no,height=" + height +
                        ",width=" + width +
                        ",location=no,menubar=" +  menubar +
                        ",resizable=yes,scrollbars=yes,status=no,toolbar=" + toolbar);
        w.focus();

        canwork = 1;

        if (returnWindow) return w;
    }
}

function show_in_main_window() 
{
    if (window.opener && (window.name == 'popup' || window.name.indexOf('popup') == 0))
    {
        window.opener.location.href = window.location.href;
        window.opener.focus();
        window.close();
    }
}

function notEmpty(obj, msg)
{
    if ( obj.value.match(/^\s*$/g) )
    {
        if ( msg && msg != '')
        {
            alert(msg)
            obj.focus();
        }
        return false;
    }
    return true;
}

// Select/unselect checkboxes list
// form - form object
// name - checkboxes name
// state - true/false
function setCheckboxesState(form, name, state)
{
    for (i = 0; i < form.length; i++)
    {
        if (form.elements[i].type == "checkbox" &&
            form.elements[i].name.search(name) != -1)
        {
            form.elements[i].checked = state;
        }
    }
}

// Select/unselect options in multiple select
// s - select object
// state - true/false
function setOptionsState(s, state)
{
    for (i = 0; i < s.options.length; i++)
    {
        s.options[i].selected = state;
    }
}

// Get selected value from a selector object
function getVal(el)
{
    if (el.type == "select-one")
        if(el.selectedIndex != undefined)
            return parseInt(el.options[el.selectedIndex].value, 10);
        else
            // Opera 6.x
            return parseInt(el.options[el.value].value, 10);
    else
        return parseInt(el.value, 10);
}

function isEmailValid(email)
{
    return email.match(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/);
}

function isNotEmpty(str)
{
    return !str.match(/^\s*$/g);
}

// checks form field and shows alert in case of error
// validationFunc - pointer to a string that gives one
// string parameter and returns true in case of valid
// value else false
function checkField(field, msg, validationFunc)
{
    eval("var isValid = " + validationFunc + "(field.value)");

    if (isValid)
        return true;

    alert(msg);
    field.focus();
    return false;
}

// Function trims specified value
function trim(str)
{
    if(str == "") return str;
    return str.replace(/^\s+/, "").replace(/\s+$/, "");
}

// Function normalizes specified string
function normalize(str)
{
    var re = /\s{2,}/;

    while (re.test(str))
    {
        str = str.replace(re, " ");
    }

    return str;
}

// Function trims all text form fields
function trimAllTextFields(f)
{
    for (i = 0; i < f.length; i++)
    {
        if (f.elements[i].type == 'text')
        {
            f.elements[i].value = trim(f.elements[i].value);
        }
    }
}

// Function normalizes all text form fields
function normalizeAllTextFields(f)
{
    for (i = 0; i < f.length; i++)
    {
        if (f.elements[i].type == 'text')
        {
            f.elements[i].value = normalize(f.elements[i].value);
        }
    }
}

// Function normalizes specified string
function toUnixLineFeeds(str)
{
    return str.replace(/\r/g, '');
}

function shiftTo(obj, x, y) {
    var units = (typeof obj.style.left == "string") ? "px" : 0;
    obj.style.left = x + units;
    obj.style.top = y + units;
}

function isMSIE() {
    return ((navigator.appVersion.indexOf("MSIE")!= -1)&&!window.opera)? true : false;
}

function calculateCoords(event) {
    var targetEl = event.srcElement;
    if (!targetEl)
        targetEl = event.target;
    return calculateObjectCoords(targetEl);
}

function calculateObjectCoords(obj) {
    return {x:findPosX(obj) - 100, y:findPosY(obj) + 25};
}

function findPosX(obj)
{
    var curleft = 0;
    if (obj.offsetParent)
    {
        while (obj.offsetParent)
        {
            curleft += obj.offsetLeft
            obj = obj.offsetParent;
        }
    }
    else if (obj.x)
        curleft += obj.x;
    return curleft;
}

function findPosY(obj)
{
    var curtop = 0;
    if (obj.offsetParent)
    {
        while (obj.offsetParent)
        {
            curtop += obj.offsetTop
            obj = obj.offsetParent;
        }
    }
    else if (obj.y)
        curtop += obj.y;
    return curtop;
}

function scrollWindowIfNeeded(object) {
    var leftUpperCorner = { x:object.offsetLeft, y:object.offsetTop };
    var rightBottomCorner = { x:object.offsetLeft + object.clientWidth, y:object.offsetTop + object.clientHeight };
    var viewportLeftUpperCorner = { x:document.body.scrollLeft, y:document.body.scrollTop };
    var viewportRightBottomCorner = { x:document.body.scrollLeft + document.body.clientWidth, y:document.body.scrollTop + document.body.clientHeight };

    var scrollX = 0;
    if (leftUpperCorner.x < viewportLeftUpperCorner.x)
        scrollX = leftUpperCorner.x - viewportLeftUpperCorner.x;

    var scrollY = 0;
    if (rightBottomCorner.y > viewportRightBottomCorner.y)
        scrollY = rightBottomCorner.y - viewportRightBottomCorner.y;

    if (scrollX != 0 || scrollY != 0) {
/*        alert("VIEWPORT\ntop-left.x = " + viewportLeftUpperCorner.x + ", top-left.y = " + viewportLeftUpperCorner.y +
              "; bottom-right.x = " + viewportRightBottomCorner.x + ", bottom-right.y = " + viewportRightBottomCorner.y + "\n\n" +
              "object\ntop-left.x = " + leftUpperCorner.x + ", top-left.y = " + leftUpperCorner.y +
              "; bottom-right.x = " + rightBottomCorner.x + ", bottom-right.y = " + rightBottomCorner.y + "\n\n" +
              "SCROLL BY\nscrollX = " + scrollX + ", scrollY = " + scrollY); */
        window.scrollBy(scrollX, scrollY);
    }
}