nav = navigator.appName.indexOf("Microsoft") != -1 ? 1 : 2;

//////////PROJECT SPECIFIC FUNCTIONS



//////////COMMON FUNCTIONS

function popup(url, name, width, height, scroll, modal, resizable){
    width = Math.min(screen.availWidth, width);
    height = Math.min(screen.availHeight - 40, height);
    var poz_x = (screen.availWidth - width) / 2;
    var poz_y = (screen.availHeight - height - 30) / 2;
    newwin = window.open(url, name, 'scrollbars=' + scroll + ', menubar=no, width=' + width + ', height=' + height + ', resizable=' + (resizable ? 'yes' : 'no') + ',toolbar=no, left=' + poz_x + ', top=' + poz_y + ', location=no, status=yes');
    if (modal) {
        popInt = setInterval(function(n){
            if (typeof(newwin.name) == "string") {
                if (newwin.document.body) {
                    openerfocus = function(){
                        if (typeof(newwin.name) == "string") {
                            newwin.focus();
                        }
                        else {
                            document.body.onfocus = null;
                        }
                    }
                    document.body.onfocus = openerfocus;
                    newwin.window.document.body.onunload = function(){
                        alert(66)
                    }
                    newwin.focus();
                    /*with(newwin){
                     self.focus()
                     opener.document.body.onfocus=function(){self.focus();opener.alert(4);}
                     self.document.body.onunload=function(){opener.document.body.onfocus=null}
                     }*/
                    clearInterval(popInt);
                }
            }
            else {
                clearInterval(popInt)
            }
        }, 1)
    }
}

function hex2rgb(val){
    //hex2rgb convert
    //created by Sebastian Serban v1.0 (17.06.2005)
    return [val >> 16, val >> 8 & 0xff, val & 0xff];
};

function formatNumber(nr, decimals, dec_point, sep){
    //created by Sebastian Serban
    //v1.2 (3.03.2008)
    if (decimals == undefined) {
        decimals = 2;
    }
    else 
        if (decimals < 0) {
            decimals = Math.abs(decimals);
            var exact_decimals = 1;
        }
        else {
            var exact_decimals = 0;
        }
    dec_point = dec_point == null ? "." : dec_point;
    sep = sep == null ? "," : sep;
    nr = String(nr).split(".");
    nat = nr[0];
    nr[0] = "";
    if (nr[1] == null) {
        nr[1] = "";
    }
    if (exact_decimals) {
        for (var i = nr[1].length; i < decimals; i++) {
            nr[1] += "0";
        }
    }
    for (var i = nat.length; i > 0; i -= 3) {
        nr[0] = nat.substr(Math.max(i - 3, 0), 3 + Math.min(i - 3, 0)) + sep + nr[0];
    }
    nr[0] = nr[0].substr(0, nr[0].length - sep.length);
    return nr[0] + (nr[1] != "" ? (decimals > 0 ? dec_point : "") + nr[1].substr(0, decimals) : "");
}

function formatSize($size, $dec_point, $thousand_step){
    $dec_point = $dec_point == null ? "." : $dec_point;
    $thousand_step = $thousand_step == null ? "," : $thousand_step;
    $sizes = {
        "TB": Math.pow(1024, 4),
        "GB": Math.pow(1024, 3),
        "MB": Math.pow(1024, 2),
        "KB": 1024
    };
    for ($key in $sizes) {
        if ($size >= ($value = $sizes[$key])) {
            return formatNumber($size / $value, 3 - (Math.floor($size / $value) + "").length, $dec_point, $thousand_step) + " " + $key;
        }
    }
    return formatNumber($size, 0, "", $thousand_step) + " B";
}

function backgroundTrans(obj, color, time, prop, callback){
    //created by Sebastian Serban
    //v1.3 (26.10.2005)
    if (prop == undefined) {
        prop = "bgColor";
    }
    clearInterval(obj.interval);
    var endValue = color;
    color = hex2rgb(Number("0x" + color.substr(1)));
    var startValue = hex2rgb(Number("0x" + obj[prop].substr(1)));
    var timePast = 0;
    obj.stopTrans = function(){
        clearInterval(this.interval);
    }
    obj.endTrans = function(){
        this.stopTrans();
        this[prop] = endValue;
        if (callback != null) {
            callback();
        }
    }
    var callf = function(){
        timePast += 50;
        if (timePast >= time) {
            obj.endTrans();
        }
        else {
            var new_color = (parseInt(startValue[0] + timePast * (color[0] - startValue[0]) / time)) * 0x10000;
            new_color += (parseInt(startValue[1] + timePast * (color[1] - startValue[1]) / time)) * 0x100;
            new_color += (parseInt(startValue[2] + timePast * (color[2] - startValue[2]) / time));
            new_color = new_color.toString(16);
            try {
                obj[prop] = "#" + new_color;
            } 
            catch (err) {
                //alert(err.description+new_color);
            }
        }
    }
    obj.interval = setInterval(callf, 10);
    callf();
}

function bgTrans(obj, start_color, color, time, callback){
    //created by Sebastian Serban
    //v1 (25.01.2006)
    obj.style.backgroundColor = start_color;
    clearInterval(obj.interval);
    var endValue = color;
    color = hex2rgb(Number("0x" + color.substr(1)));
    //var startValue = hex2rgb(Number("0x"+obj.style.backgroundColor.substr(1)));
    var startValue = hex2rgb(Number("0x" + start_color.substr(1)));
    var timePast = 0;
    obj.stopTrans = function(){
        clearInterval(this.interval);
    }
    obj.endTrans = function(){
        this.stopTrans();
        this.style.backgroundColor = endValue;
        if (callback != null) {
            callback();
        }
    }
    var callf = function(){
        timePast += 50;
        if (timePast >= time) {
            obj.endTrans();
        }
        else {
            var new_color = (parseInt(startValue[0] + timePast * (color[0] - startValue[0]) / time)) * 0x10000;
            new_color += (parseInt(startValue[1] + timePast * (color[1] - startValue[1]) / time)) * 0x100;
            new_color += (parseInt(startValue[2] + timePast * (color[2] - startValue[2]) / time));
            new_color = new_color.toString(16);
            try {
                obj.style.backgroundColor = "#" + new_color;
            } 
            catch (err) {
                //alert(err.description+new_color);
            }
        }
    }
    obj.interval = setInterval(callf, 10);
    callf();
}

function addOption(theSel, theText, theValue){
    var newOpt = new Option(theText, theValue);
    var selLength = theSel.length;
    theSel.options[selLength] = newOpt;
}

function deleteOption(theSel, theIndex){
    var selLength = theSel.length;
    if (selLength > 0) {
        theSel.options[theIndex] = null;
    }
}

function moveOptions(theSelFrom, theSelTo, mode){
    //mode: 0-move,1-copy,2-delete
    
    mode = mode == null ? 0 : mode;
    var selLength = theSelFrom.length;
    var selectedText = new Array();
    var selectedValues = new Array();
    var selectedCount = 0;
    
    // Find the selected Options in reverse order
    // and delete them from the 'from' Select.
    for (var i = selLength - 1; i >= 0; i--) {
        if (theSelFrom.options[i].selected) {
            selectedText[selectedCount] = theSelFrom.options[i].text;
            selectedValues[selectedCount] = theSelFrom.options[i].value;
            if (mode != 1) {
                deleteOption(theSelFrom, i);
            }
            selectedCount++;
        }
    }
    
    // Add the selected text/values in reverse order.
    // This will add the Options to the 'to' Select
    // in the same order as they were in the 'from' Select.
    if (mode < 2) {
        for (i = selectedCount - 1; i >= 0; i--) {
            if (mode == 1) {
                var duplicate = 0;
                for (var j = 0; j < theSelTo.options.length; j++) {
                    if (theSelTo.options[j].value == selectedValues[i]) {
                        duplicate = 1;
                        break;
                    }
                }
                if (duplicate) {
                    continue;
                }
            }
            addOption(theSelTo, selectedText[i], selectedValues[i]);
        }
    }
    if (theSelFrom.onchange) {
        theSelFrom.onchange();
    }
    if (theSelTo.onchange) {
        theSelTo.onchange();
    }
}
function selectAllOptions(theSel){
    for (i = 0; i < theSel.options.length; i++) 
        theSel.options[i].selected = true;
}

function isBisYear(year){
    return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0
}

function correctDate(f, nume){
    var zileinluna = [31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    zileinluna[2] = isBisYear(f[nume + '[0]'].value) ? 29 : 28;
    var nrz = zileinluna[Number(f[nume + '[1]'].value)];
    if (f[nume + '[2]'].value > nrz) {
        try {
            f[nume + '[2]'].selectedIndex = nrz - (f[nume + '[2]'].options.length == 31 ? 1 : 0);
        } 
        catch (e) {
        }
    }
}

Array.prototype.find = function(elem, all){
    //function for finding elements in arrays
    //created by Sebastian Serban, v3.2 (18.10.2004)
    //this function receives "elem" which may be an number or an array and returns the position or an array of the positions
    //if param "all" is defined, then function return array with all the positions instead of position of the first element found
    if (elem.length == undefined || typeof(elem) != "object") {
        elem = [elem];
        single = true;
    }
    else {
        single = false;
    }
    var result = [];
    if (all == undefined) {
        for (var k = 0; k < elem.length; k++) {
            result[k] = -1;
            toFind = elem[k];
            for (var i = 0; i < this.length; i++) {
                if (this[i] == toFind) {
                    result[k] = i;
                    break;
                }
            }
        }
    }
    else {
        for (var k = 0; k < elem.length; k++) {
            result[k] = [];
            toFind = elem[k];
            for (var i = 0; i < this.length; i++) {
                if (this[i] == toFind) {
                    result[k].push(i);
                }
            }
            if (result[k][0] == undefined) {
                result[k] = -1;
            }
        }
    }
    if (single) {
        return result[0];
    }
    else {
        return result;
    }
}

function writeFlash(divid, url, width, height, bgcolor, id, vars, transparent, menu, style){
    document.getElementById(divid).innerHTML = '<object charset="UTF-8" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" id="' + id + '" width="' + width + '" height="' + height + '" style="' + style + '" flashvars="' + vars + '"><param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="' + url + '" /><param name="quality" value="high" /><param name="allowFullScreen" value="true" /><param name="bgcolor" value="' + bgcolor + '" /><param name="flashVars" value="' + vars + '" />' + (menu == 0 || menu == 1 ? '<param name="menu" value="' + menu + '" />' : '') + (transparent ? '<param name="wmode" value="transparent" />' : '') + '<embed style="' + style + '" name="' + id + '" src="' + url + '" quality="high" allowFullScreen="true" bgcolor="' + bgcolor + '" width="' + width + '" height="' + height + '" flashVars="' + vars + '" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>';
}

function updateCounter(txt, counter, limit, cut){
    var l = limit - txt.value.replace(/\r\n/g, '\n').replace(/\n/g, '\r\n').length;
    if (counter.value != null) {
        counter.value = l;
    }
    else {
        counter.innerHTML = l;
    }
    if (cut && l <= 0) {
        txt.value = txt.value.replace(/\r\n/g, '\n').replace(/\n/g, '\r\n').substr(0, limit);
    }
    counter.style.color = l < 0 ? "#ff0000" : "#6699cc";
}

function isNumberKey(evt){
    var charCode = (evt.which) ? evt.which : event.keyCode
    return charCode < 31 || charCode > 48 && charCode < 57;
}

arr_escape = {
    "ă": "%C4%83",
    "Ă": "%C4%82",
    "î": "%C3%AE",
    "Î": "%C3%8E",
    "â": "%C3%A2",
    "Â": "%C3%82",
    "ş": "%C5%9F",
    "Ş": "%C5%9E",
    "ţ": "%C5%A3",
    "Ţ": "%C5%A2",
    "Ţ": "%C5%A2",
    "+": "%2B",
    "/": "%2F"
};

function rawurlencode(str){
    var result = "";
    for (var i = 0; i < str.length; i++) {
        var chr = str.charAt(i);
        if (arr_escape[chr] != undefined) {
            result += arr_escape[chr];
        }
        else {
            result += escape(chr);
        }
    }
    return result;
}

function rawurldecode(str){
    for (var i in arr_escape) {
        str = str.replace(new RegExp(arr_escape[i], "g"), i);
    }
    return unescape(str);
}

function setCookie(name, value, expires, path, domain, secure){
    if (expires) {
        var d = new Date();
        d.setTime(expires * 1000);
        expires = d.toGMTString();
    }
    document.cookie = name + "=" + escape(value) + (expires ? "; expires=" + expires : "") + (path ? "; path=" + path : "") + (domain ? "; domain=" + domain : "") + (secure ? "; secure" : "");
}

function mailTo(email){
    email = decodeURIComponent(email);
    var new_email = '';
    for (var i = email.length - 1; i >= 0; i--) {
        new_email += email.charAt(i);
    }
    document.location = 'mailto:' + new_email;
}

function isValid($value, $type, $allow_null){
    switch ($type) {
        case "novoid":
            $valid = $value.trim() != "";
            break;
        case "nonull":
            $valid = $value != "";
            break;
        case "name":
            $valid = $value.trim().match(/^[^`~!@#$%^&*()_=+\[{\]}\\|;:'",<.>\/?]+$/);
            break;
        case "username":
            $valid = $value.match(/^[^\s]+$/i);
            break;
        case "email":
            $valid = $value.match(/^([-0-9a-z_~%+]+[-\.0-9a-z_~%+]*[-0-9a-z_~%+]+|[-0-9a-z_~%+])@[-0-9a-z_~]+(\.[-0-9a-z_~]+)*\.[0-9a-z_]{2,5}$/i);
            break;
        case "int":
            $valid = $value.match(/^\-?[0-9]+$/);
            break;
        case "int+":
            $valid = $value.match(/^[0-9]+$/);
            break;
        case "float":
            $valid = $value.match(/^[0-9\-]+((,|\.)[0-9\-]+)?$/);
            break;
        case "float+":
            $valid = $value.match(/^\-?[0-9\-]+((,|\.)[0-9\-]+)?$/);
            break;
        case "phone":
            $valid = $value.trim().match(/^\+?([0-9]+[\s\-\.]?)*[0-9]+$/);
            break;
        case "password":
            $valid = $value.length >= 6 && $value.length <= 50 && $value.match(/[0-9].*[a-z]|[a-z].*[0-9]/i);
            break;
        case "nozero":
            $valid = $value != "0";
            break;
        default:
            var $m = $type.match(/\/(.*)\/(.*)/);
            $valid = $m != null ? $value.match(new RegExp($m[1], $m[2])) : null;
    }
    return $value == "" && $allow_null || Number($valid) != 0;
}
