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();

    return wnd;
}


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();

    return true;
}



// 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
 *****************************************************************************/
 /*
    MOD DE APEL:

    definirea fiecarui tab :
    1. text,
    2. cod,
    3. functie care se lanseaza pre-activare tab,
    4. functie care se lanseaza post-activare tab

    tabmenu = new TabMenu("tabmenu1", [
        ["Editare",         "tab1"],
        ["Redactare text",  "tab2"],
        ["Previzualizare",  "tab3", "", preview],
        ]);
    tabmenu.setCssStyle("tab_menu", "stil_nou");
    tabmenu.generate();
 */
function TabMenu(name, tabs)
{
    this.name = name;
    this.idTabMenu = "id_" + name;
    this.idHidden = "hid_" + name;
    this.idTabMenuObj = "obj_" + name;
    this.tabs = tabs;

    this.cssStyle = new Array();
    this.cssStyle["tab_menu"]           = "tab_menu";
    this.cssStyle["tab_label"]          = "tab_label";
    this.cssStyle["tab_label_active"]   = "tab_label_active";

    document[this.idTabMenuObj] = this;
}


/**
 * Genereaza HTML-ul
 */
TabMenu.prototype.generate = function()
{
    document.write('<div id="' + this.idTabMenu + '_id" class="' + this.cssStyle["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="' + this.cssStyle["tab_label"] + '">' +
            '<a href="#" onclick="document[\'' + this.idTabMenuObj + '\'].switchTabOnClick(' + i + ')">' + this.tabs[i][0] + '</a>' +
            '</td>');
    }
    document.write('</tr></table>');
    document.write('</div>');
};


/**
 * Activeaza tab-ul de pe o anumita pozitie, impreuna cu posibile evenimente
 */
TabMenu.prototype.switchTabOnClick  = function(pos) {
    // dc exista un eveniment asociat care sa se lanseze inainte de
    // activarea tabului, il lansez
    if (typeof(this.tabs[pos][2]) == "function") {
        this.tabs[pos][2]();
    }

    // schimb tabul curent
    this.switchTab(pos);

    // dc exista un eveniment asociat care sa se lanseze dupa
    // activarea tabului, il lansez
    if (typeof(this.tabs[pos][3]) == "function") {
        this.tabs[pos][3]();
    }

}

/**
 * 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 = this.cssStyle["tab_label"];
    }

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

    strLabelId = this.idTabMenu + '_' + pos;
    document.getElementById(strLabelId).className = this.cssStyle["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;
}

/**
 * schimba unul dintre stilurile predefinite
 */
TabMenu.prototype.setCssStyle = function(sDef, sVal)
{
    this.cssStyle[sDef] = sVal;
    return;
}

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


/**
 * 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');
        }
    }
}


// js function  asociated with "tab_xchg_rate_convert Box".
function tab_xchg_rate_convert_currency_convert()
{
    frm = document.xchg_conv_from_box
    index1 = frm.currency_1.selectedIndex;
    index2 = frm.currency_2.selectedIndex;

    from_value  = frm.currency_1.options[index1].value;
    to_value    = frm.currency_2.options[index2].value;
    tmp_val = (frm.amount.value * from_value) / to_value;

    if (isNaN(tmp_val)) {
        frm.exchange_to_qt.value = "eroare conversie";
    } else {
        frm.result.value = Math.round(tmp_val * Math.pow(10, 4)) / Math.pow(10, 4);
    }
}


// movie slider
movieSlideClass = new Class.create();
movieSlideClass.prototype = {
    initialize : function(objName, movieClipObj, movieShowWidth, sliderBarObj, sliderObj, barSliderWidth)
    {
        var self = this;    // referinta

        this.objName             = objName;

        this.movieObj            = $(movieClipObj);
        this.movieWidth          = this.movieObj.offsetWidth;
        this.movieShowWidth      = parseInt(movieShowWidth);
        this.sliderBarObj        = $(sliderBarObj);
        this.sliderObj           = $(sliderObj);
        this.sliderBarObj.style.width = parseInt(barSliderWidth) + 'px';

        this.doClip(0, this.movieShowWidth, this.movieObj.offsetHeight, 0);

        this.stepBy              = 20;
        this.sliderRange        = this.movieWidth - this.movieShowWidth;

        this.slider  = new Control.Slider(sliderObj, sliderBarObj, {
                range            : $R(0, self.sliderRange),
                increment        : 0,
                onSlide          : function(v) {
                    self.doClip(0, (v + self.movieShowWidth), self.movieObj.offsetHeight, v);
                },
                onChange         : function(v){
                    self.doClip(0, (v + self.movieShowWidth), self.movieObj.offsetHeight, v);
                }
            }
        );

    },

    doClip : function(u, r, d, l)
    {
        this.movieObj.style.clip =
                'rect(' + parseInt(u) + 'px '+
                parseInt(r) + 'px ' + parseInt(d) + 'px ' +
                parseInt(l) + 'px)';
        this.movieObj.style.left = -l + 'px';
    },

    moveLeft : function()
    {
        this.slider.setValueBy(-this.stepBy);
    },

    moveRight : function()
    {
        this.slider.setValueBy(this.stepBy);
    },

    goFirstPos : function()
    {
        this.slider.setValue(0);
    },

    goLastPos : function()
    {
        this.slider.setValue(this.sliderRange);
    }
}


function addslashes(text) {
    // escape backslshes
    text = text.replace(/\\/g, '\\\\');
    // escape quotes
    text = text.replace(/'/g, '\\\'');
    // escape double quotes
    text = text.replace(/"/g, '\\\"');

    return text;
}


function openURLInPopup(url, width, height)
{
    if (typeof(width) == "undefined") {
        var width = 800;
        var height = 600;
    }

    if (typeof(height) == "undefined") {
        var height = 600;
    }

    popup(url, 'slideShow', width, height, 'menubar=0,location=0,toolbar=0,status=0,scrollbars=1');
}


function openURLInNewPage(url, target)
{
    myWidth = 800;
    myHeight = 600;

    if( typeof( window.innerWidth ) == 'number' ) {
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if( document.documentElement &&
            ( document.documentElement.clientWidth
            || document.documentElement.clientHeight ) ) {
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    }

    if (typeof(target) == 'undefined') {
        target = 'newpage_' + Math.floor(Math.random() * 1000000000);
    }

    var features = 'width=' + myWidth + ',height=' + myHeight;
    features += 'menubar=1,location=1,toolbar=1,status=1,scrollbars=1';
    var wnd = window.open(url, target, features);
    wnd.focus();
    return;
}

// Simulates PHP's date function
Date.prototype.format = function(format) {
    var returnStr = '';
    var replace = Date.replaceChars;
    for (var i = 0; i < format.length; i++) {
        var curChar = format.charAt(i);
        if (replace[curChar])
        returnStr += replace[curChar].call(this);
    else
        returnStr += curChar;
    }
    return returnStr;
};

var dateFromatISORegexp = /(\d{4})\-(\d{2})\-(\d{2}) (\d{2}):(\d{2}):(\d{2})/;
var dateFromatISO = 'Y-m-d H:i:s';

Date.replaceChars = {
    shortMonths: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
    longMonths: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
    shortDays: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
    longDays: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],

    // Day
    d: function() { return (this.getDate() < 10 ? '0' : '') + this.getDate(); },
    D: function() { return Date.replace.shortDays[this.getDay()]; },
    j: function() { return this.getDate(); },
    l: function() { return Date.replace.longDays[this.getDay()]; },
    N: function() { return this.getDay() + 1; },
    S: function() { return (this.getDate() % 10 == 1 && this.getDate() != 11 ? 'st' : (this.getDate() % 10 == 2 && this.getDate() != 12 ? 'nd' : (this.getDate() % 10 == 13 && this.getDate() != 1 ? 'rd' : 'th'))); },
    w: function() { return this.getDay(); },
    z: function() { return "Not Yet Supported"; },

    // Week
    W: function() { return "Not Yet Supported"; },

    // Month
    F: function() { return Date.replace.longMonths[this.getMonth()]; },
    m: function() { return (this.getMonth() < 11 ? '0' : '') + (this.getMonth() + 1); },
    M: function() { return Date.replace.shortMonths[this.getMonth()]; },
    n: function() { return this.getMonth() + 1; },
    t: function() { return "Not Yet Supported"; },

    // Year
    L: function() { return "Not Yet Supported"; },
    o: function() { return "Not Supported"; },
    Y: function() { return this.getFullYear(); },
    y: function() { return ('' + this.getFullYear()).substr(2); },
    // Time

    a: function() { return this.getHours() < 12 ? 'am' : 'pm'; },
    A: function() { return this.getHours() < 12 ? 'AM' : 'PM'; },
    B: function() { return "Not Yet Supported"; },
    g: function() { return this.getHours() == 0 ? 12 : (this.getHours() > 12 ? this.getHours() - 12 : this.getHours()); },
    G: function() { return this.getHours(); },
    h: function() { return (this.getHours() < 10 || (12 < this.getHours() < 22) ? '0' : '') + (this.getHours() < 10 ? this.getHours() + 1 : this.getHours() - 12); },
    H: function() { return (this.getHours() < 10 ? '0' : '') + this.getHours(); },
    i: function() { return (this.getMinutes() < 10 ? '0' : '') + this.getMinutes(); },
    s: function() { return (this.getSeconds() < 10 ? '0' : '') + this.getSeconds(); },

    // Timezone
    e: function() { return "Not Yet Supported"; },
    I: function() { return "Not Supported"; },
    O: function() { return (this.getTimezoneOffset() < 0 ? '-' : '+') + (this.getTimezoneOffset() / 60 < 10 ? '0' : '') + (this.getTimezoneOffset() / 60) + '00'; },
    T: function() { return "Not Yet Supported"; },
    Z: function() { return this.getTimezoneOffset() * 60; },

    // Full Date/Time
    c: function() { return "Not Yet Supported"; },
    r: function() { return this.toString(); },
    U: function() { return this.getTime() / 1000; }
}