var ajaxMaxTries = 4;

function changeVisibility(id)
{
    e = document.getElementById(id);
    if (e.style.display) {
        e.style.display = '';
    } else {
        e.style.display = 'none';
    }
}

function ajaxUpdateById(scriptUrl, id, targetId)
{
    display = $('#' + targetId).css('display');
    $('#' + targetId).html('<div style="text-align: center"><img src="/img/ajax-loader.gif" /></div>');
    if (display == 'none') {
        $('#' + targetId).css('display', '');
    }

    $.ajax({
        url: scriptUrl + '/id/' + id,
        type: "get",
        timeout: 10000,
        error: function(){
/*
            if (ajaxTries < ajaxMaxTries) {
                ajaxTries++;
            } else {
                alert("Connection error.");
            }
*/
        },
        success: function(res){
            // null tries num
            ajaxTries = 0;

            $('#' + targetId).html(res);
        }
    });

    return false;
};

function ajaxUpdateByIdCallback(scriptUrl, id, targetId, callbackFunction)
{
    display = $('#' + targetId).css('display');
    $('#' + targetId).html('<div style="text-align: center"><img src="/img/ajax-loader.gif" /></div>');
    if (display == 'none') {
        $('#' + targetId).css('display', '');
    }

    $.ajax({
        url: scriptUrl + '/id/' + id,
        type: "get",
        timeout: 3000,
        error: function(){
            if (ajaxTries < ajaxMaxTries) {
                ajaxTries++;
            } else {
                alert("Connection error.");
            }
        },
        success: function(res){
            // null tries num
            ajaxTries = 0;
            callbackFunction(id, targetId, res);
        }
    });

    return false;
};

function submitForm(scriptUrl, form, targetId) {
    // prepare post data
    var postData = '';
    for(var i=0; i < form.elements.length; i++) {
        if (form.elements[i].type == "radio" && !form.elements[i].checked) continue;

        postData += form.elements[i].name + '=' + escape(form.elements[i].value) + '&';
    }

    // perform request
    display = $('#' + targetId).css('display');
    $('#' + targetId).html('<div style="text-align: center"><img src="/img/ajax-loader.gif" /></div>');
    if (display == 'none') {
        $('#' + targetId).css('display', '');
    }

    $.ajax({
        url: scriptUrl,
        type: "post",
        data: postData,
        timeout: 3000,
        error: function(){
            if (ajaxTries < ajaxMaxTries) {
                ajaxTries++;
            } else {
                alert("Connection error.");
            }
        },
        success: function(res){
            // null tries num
            ajaxTries = 0;

            $('#' + targetId).html(res);
        }
    });

    return false;
}

function showSlidingBox()
{
    var targetId = $(this).attr('x-target');

    if ($('#' + targetId)) {
        if ($('#' + targetId).css('display') == 'none') {
            $('#' + targetId).slideDown();
        } else {
            $('#' + targetId).slideUp();
        }

    }
}

/* menu function */
function ieMenuFix () {
    if (document.all && document.getElementById) {
        navRoot = document.getElementById("topNav");
        for (i = 0; i < navRoot.childNodes.length; i++) {
            node = navRoot.childNodes[i];
            if (node.nodeName == "LI") {
                node.onmouseover = function() {
                    this.className += " over";
                }
                node.onmouseout = function() {
                    this.className = this.className.replace(" over", "");
                }
            }
        }
    }
}
