var _olk_debug_php = 0;

function olk_obj(sName, d) {
    if (!d) d = document;
    return d.getElementById(sName);
}

function olk_param_value(addr, paramName) {
    var a = addr.split('&');
    var na = '', pair;

    for (var i = 0; i < a.length; i++) {
        if (a[i] != '') {
            pair = a[i].split('=');
            if (pair[0] == paramName) na = pair[1];
        }
    }
    return na;
}

function olk_link_without_param(url, paramName) {
    var uri = url.split('?');
    var addr = uri[1];
    var a = addr.split('&');
    var na = uri[0] + '?';
    var first = true;

    for (var i = 0; i < a.length; i++) {
        if (a[i] != '') {
            pair = a[i].split('=');
            if (pair[0] != paramName) {
                if (!first) na += '&';
                na += a[i];
                first = false;
            }
        }
    }
    return na;
}

function olk_isphplocal(url) {
    if (url.indexOf('://') > -1) return false;
    var qm = url.indexOf('?');
    if (qm != -1) url = url.substr(0, qm);
    return (url.lastIndexOf('.php') != -1);
}

/**
 * Opens a new window in predefined size
 * @param {string} name - name of the window
 * @param {string} url - url to execute
 * @param {string} options - options with comma separated parameters
 */
function olk_window_open(name, url, options) {
    var olk_window, i, s_options, a, pair, width, height, location, toolbar, left, top, format, myfilter;
    var resizable, menubar, scrollbars, status, fullscreen, dependent;

    s_options = '';
    width = 1004;
    height = 620;
    format = '';
    left = top = '1';
    toolbar = location = 'no';
    resizable = 'yes';
    menubar = 'no';
    scrollbars = 'yes';
    status = 'yes';
    fullscreen = 'no';
    dependent = 'no';

    if (options) {
        a = options.split(',');
        for (i = 0; i < a.length; i++) {
            pair = a[i].split('=');
            if (pair[0] == 'resizable')   resizable = pair[1];
            if (pair[0] == 'menubar')     menubar = pair[1];
            if (pair[0] == 'scrollbars')  scrollbars = pair[1];
            if (pair[0] == 'status')      status = pair[1];
            if (pair[0] == 'fullscreen')  fullscreen = pair[1];
            if (pair[0] == 'dependent')   dependent = pair[1];

            if (pair[0] == 'width')       width = parseInt(pair[1]);
            if (pair[0] == 'height')      height = parseInt(pair[1]);
            if (pair[0] == 'location')    location = pair[1];
            if (pair[0] == 'toolbar')     toolbar = pair[1];
            if (pair[0] == 'left')        left = parseInt(pair[1]);
            if (pair[0] == 'top')         top = parseInt(pair[1]);
            if (pair[0] == 'format')      format = pair[1];
        }
    }
    s_options = 'resizable=' + resizable + ',menubar=' + menubar + ',scrollbars=' + scrollbars;
    s_options += ',status=' + status + ',fullscreen=' + fullscreen + ',dependent=' + dependent;
    s_options += ',width=' + width + ',height=' + height + ',location=' + location + ',toolbar=' + toolbar + ',left=' + left + ',top=' + top;
    if (typeof(session_id) == 'string' && olk_isphplocal(url)) url = url + '&sid=' + session_id;
    var tmp_format = format;
    if (tmp_format == 'pdml') tmp_format = 'pdf';
    if (format != '') url = url + '&format=' + format + '&tmp=.' + tmp_format;

    olk_window = window.open(url, name, s_options);
    if (!olk_window.opener) olk_window.opener = top;
    olk_window.focus();
}

function olk_radio_setvalue(sName, val, d) {
    var o, el, j;
    if (!d) d = document;
    o = d.getElementsByName(sName);
    if (o) {
        for (j = 0; j < o.length; j++) {
            o[j].checked = o[j].value == val;
        }
    }
}

function olk_radio_value(sName, d) {
    var o, result = -1, el, j;
    if (!d) d = document;
    o = d.getElementsByName(sName);
    if (o) {
        for (j = 0; j < o.length; j++) {
            if (o[j].checked) {
                result = o[j].value;
                break;
            }
        }
    }
    return result;
}

function olk_select_setvalue(sName, val) {
    var o = olk_obj(sName);

    if (o) {
        for (var i = 0; i < o.length; ++i) {
            if (o.options[i].value == val) {
                o.options[i].selected = true;
            }
        }
    } else {
        alert("Id not found: " + sName);
    }
}

function olk_value(s) {
    var o = olk_obj(s);
    if (o) {
        if (o.tagName == 'DIV' || o.tagName == 'div') {
            return o.innerHTML;
        } else {
            if (o.type == 'checkbox') {
                return o.checked ? 1 : 0;
            } else {
                return o.value;
            }
        }
    }
    return null;
}

function olk_email_validate(str) {
    if (str.length > 0) {
        if (!str.match(/^[\w]{1}[\w\.\-_]*@[\w]{1}[\w\-_\.]*\.[\w]{2,6}$/i)) {
            return false;
        }
    }
    return true;
}

function olk_url_validate(str) {
    if (str.length > 0) {
        if (!str.match(/^(http(s)?\:\/\/)?(\S+\.)+(\S+)(\/\S+)*(\.\S+)?\??(\S*)$/i)) {
            return false;
        }
    }
    return true;
}

function olk_field_set(id, value) {
    var o = olk_obj(id);
    if (!o) {
        return;
    }
    if (o.type == 'checkbox') {
        o.checked = (value == 1);
        o.defaultChecked = (value == 1);
    } else {
        o.value = value;
        o.defaultValue = value;
    }
}

function olk_innerhtml_set(id, value) {
    var o = olk_obj(id);
    if (o) o.innerHTML = value;
}

function olk_ShowHTML(windowname, obj, options) {
    var prot = window.location.protocol;
    var anfang = prot + '//' + window.location.hostname + '/';
    var s = obj.href, url;

    if (s.indexOf('index.php') >= 0) {
        if (s.substr(0, anfang.length) == anfang || s.substr(0, 1) == '/') {
            url = s + '&nohead=1';
        } else {
            url = s;
        }
    } else if (s.indexOf('.html') >= 0) {
        if (s.substr(0, anfang.length) == anfang) {
            var s2 = s.substr(anfang.length);
            var n2 = s2.lastIndexOf('/');
            if (n2 > -1) {
                url = 'small_' + s2.substr(n2 + 1);
            } else {
                url = anfang + 'small_' + s.substr(anfang.length);
            }
        } else if (s.substr(0, 1) == '/') {
            url = '/small_' + s.substr(1);
        } else {
            url = s;
        }
    } else {
        url = s;
    }
    olk_window_open(windowname, url, options);
    return false;
}

String.prototype.htmlEntities = function() {
    var chars = new Array(0x26, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
            0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4,
            0xf5, 0xf6, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, 0xc0,
            0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb,
            0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6,
            0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0x80, 0x22, 0xdf, 0x3c,
            0x3e, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab,
            0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
            0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe);

    var entities = new Array('amp', 'agrave', 'aacute', 'acirc', 'atilde', 'auml', 'aring',
            'aelig', 'ccedil', 'egrave', 'eacute', 'ecirc', 'euml', 'igrave',
            'iacute', 'icirc', 'iuml', 'eth', 'ntilde', 'ograve', 'oacute',
            'ocirc', 'otilde', 'ouml', 'oslash', 'ugrave', 'uacute', 'ucirc',
            'uuml', 'yacute', 'thorn', 'yuml', 'Agrave', 'Aacute', 'Acirc',
            'Atilde', 'Auml', 'Aring', 'AElig', 'Ccedil', 'Egrave', 'Eacute',
            'Ecirc', 'Euml', 'Igrave', 'Iacute', 'Icirc', 'Iuml', 'ETH', 'Ntilde',
            'Ograve', 'Oacute', 'Ocirc', 'Otilde', 'Ouml', 'Oslash', 'Ugrave',
            'Uacute', 'Ucirc', 'Uuml', 'Yacute', 'THORN', 'euro', 'quot', 'szlig',
            'lt', 'gt', 'cent', 'pound', 'curren', 'yen', 'brvbar', 'sect', 'uml',
            'copy', 'ordf', 'laquo', 'not', 'shy', 'reg', 'macr', 'deg', 'plusmn',
            'sup2', 'sup3', 'acute', 'micro', 'para', 'middot', 'cedil', 'sup1',
            'ordm', 'raquo', 'frac14', 'frac12', 'frac34');

    newString = this;
    for (var i = 0; i < chars.length; i++) {
        myRegExp = new RegExp();
        myRegExp.compile(chars[i], 'g');
        newString = newString.replace(myRegExp, '&' + entities[i] + ';');
    }
    return newString;
};
