function popup(url, target, width, height, features)
{
    var wnd, features1;

    features1 = 'width=' + width + ',height=' + height;
    if (screen) {
        features1 += ',left=' + (screen.width - width) / 2 +
                ',top=' + (screen.height - height) /2;
    }
    if (features) {
        features1 += ',' + features;
    }
    wnd = window.open(url, target, features1);
    wnd.focus();
}


function preloadImages()
{
    var a = preloadImages.arguments;
    var d = new Array();

    for (var i = 0; i < a.length; i++) {
        d[i] = new Image;
        d[i].src = a[i];
    }
}


function change_image_src(imgId, src, width, height)
{
    if (! document.getElementById(imgId)) {
        return;
    }

    document.getElementById(imgId).src = src;

    if (typeof(width) != "undefined" && ! isNaN(width)) {
        document.getElementById(imgId).width = width;
    }

    if (typeof(height) != "undefined" && ! isNaN(height)) {
        document.getElementById(imgId).height = height;
    }
}


function zoom(url)
{
    popup(url, "zoom", 100, 100, "status=1")
}


function getScriptParam(name)
{
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == name) {
            return pair[1];
        }
    }

    return false;
}


var pageFormIsSubmitting = false;
function submitForm(action1, formName)
{
    if (formName == undefined) {
        if (document.forms.length > 0) {
            formName = document.forms[0].name;
        } else {
            return;
        }
    }
    var currentForm = document.forms[formName];

    // setez action-ul
    if (currentForm.elements["action"]) {
        currentForm.elements["action"].value = action1;
    }

    // fac submit
    doSubmit(formName);
}
/* }}} form related functions */


function doSubmit(form1)
{
    if (pageFormIsSubmitting || ! document.forms[form1]) {
        return false;
    }

    pageFormIsSubmitting  = true;
    document.forms[form1].submit();
}



// detecteaza apasarea tastei Enter intr-un camp si exectuta o actiune
// parametrul actiune este string si va fi executat cu functia eval
function checkEnter(e, doAction)
{
    e = new FixEvent(e);
    if (! e) {
        return;
    }
    if (e.keyCode == 13) {
        eval(doAction);
    }
}


// aTarget  string  mousedown, keyup .... etc
// anAction string  function to do
// doCapure boolean execute on capture (true) or bubbling (false) phase
function AddEventListenerEx(e, aTarget, anAction, doCapure)
{
    if (e.addEventListener) {
        e.addEventListener(aTarget, anAction, doCapure);
    } else if (e.attachEvent) {
        e.attachEvent('on' + aTarget, anAction);
    } else {
        eval('e.on' + aTarget + ' = anAction');
    }
}

function FixEvent(evt)
{
    var e = evt || window.event;
    if (!e) {
        return;
    }

    if (e.keyCode) {
        this.keyCode = e.keyCode;
    } else if (e.which) {
        this.keyCode = e.which;
    }

    if (e.target) {
        this.target = e.target;
    } else if (e.srcElement) {
        this.target = e.srcElement;
    }
}



function xTableIterate(sec, fnCallback, data)
{
    var r, c;
    sec = document.getElementById(sec);
    if (!sec || !fnCallback) {
        return;
    }

    for (r = 0; r < sec.rows.length; ++r) {
        if (false == fnCallback(sec.rows[r], true, r, c, data)) {
            return;
        }
        for (c = 0; c < sec.rows[r].cells.length; ++c) {
            if (false == fnCallback(sec.rows[r].cells[c], false, r, c, data)) {
                return;
            }
        }
    }
}



/*****************************************************************************
 * Class TabMenu
 *****************************************************************************/
function TabMenu(name, tabs)
{
    this.name = name;
    this.idTabMenu = "id_" + name;
    this.idHidden = "hid_" + name;
    this.idTabMenuObj = "obj_" + name;
    this.tabs = tabs;

    document[this.idTabMenuObj] = this;
}


/**
 * Genereaza HTML-ul
 */
TabMenu.prototype.generate = function()
{
    document.write('<div id="' + this.idTabMenu + '_id" class="tab_menu">');
    document.write('<input id="' + this.idHidden + '" type="hidden" name="' + this.name + '">');
    document.write('<table border="0" cellspacing="0" cellpadding="0"><tr>');
    for (var i = 0; i < this.tabs.length; i++) {
        strLabelId = this.idTabMenu + '_' + i;
        document.write('<td id="' + strLabelId + '" class="tab_label">' +
            '<a href="#" onclick="document[\'' + this.idTabMenuObj + '\'].switchTab(' + i + ')">' + this.tabs[i][0] + '</a>' +
            '</td>');
    }
    document.write('</tr></table>');
    document.write('</div>');
}


/**
 * Activeaza tab-ul de pe o anumita pozitie.
 */
TabMenu.prototype.switchTab = function(pos)
{
    // le pun pe toate pe 'invisible'
    for (var i = 0; i < this.tabs.length; i++) {
        show_hide(this.tabs[i][1], false)

        strLabelId = this.idTabMenu + '_' + i;
        document.getElementById(strLabelId).className = "tab_label";
    }

    // activez tab-ul curent
    show_hide(this.tabs[pos][1], true);

    strLabelId = this.idTabMenu + '_' + pos;
    document.getElementById(strLabelId).className = "tab_label_active";

    // setez campul hidden
    document.getElementById(this.idHidden).value = this.tabs[pos][1];

    // apelez evenimentul onSwitchTab
    this.onSwitchTab();
}


/**
 * Activeaza tab-ul cu un anumit cod
 */
TabMenu.prototype.switchTabByCode = function(search_code)
{

    find_pos = -1
    for (var i = 0; i < this.tabs.length; i++) {
        if (this.tabs[i][1] == search_code) {
            find_pos = i
        }
    }

    if (find_pos != -1) {
        this.switchTab(find_pos)
    }
}

/**
 * onSwitchTab event
 */
TabMenu.prototype.onSwitchTab = function ()
{
    // TBD
}

/**
 * return current tab
 */
TabMenu.prototype.getActiveTab = function ()
{
    return document.getElementById(this.idHidden).value;
}

/*****************************************************************************/


/**
 * Afiseaza / ascunde un div
 */
function show_hide(htmlelement, show)
{
    var disp_val = show ? 'block' : 'none';
    if (document.getElementById) { // DOM3 = IE5, NS6
        if ((obj = document.getElementById(htmlelement))) {
            obj.style.display = disp_val;
        }
    } else {
        if (document.layers) { // Netscape 4
            eval('document.' + htmlelement + '.display = disp_val');
        } else { // IE 4
            eval('document.all.' + htmlelement + '.display = disp_val');
        }
    }
}
