$().ready(function() {
    $('table.clickable tr').click(function(event) {
        var $url = $(this).attr("ref");
        if ($url == undefined || $url == '') {
            return;
        }
        if (event.target.nodeName == 'A') {
            return;
        }
        window.location.href = $url;
    });
});

function il_random_num(lbound, ubound) {
    return (Math.floor(Math.random() * (ubound - lbound)) + lbound);
}

function il_random_char(number, lower, upper) {
    var numberChars = "0123456789";
    var lowerChars = "abcdefghijklmnopqrstuvwxyz";
    var upperChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    var charSet;
    if (number)
        charSet += numberChars;
    if (lower)
        charSet += lowerChars;
    if (upper)
        charSet += upperChars;
    return charSet.charAt(il_random_num(0, charSet.length));
}

function il_generate_uid(length) {
    var rc = "";
    if (length > 0) {
        rc = rc + il_random_char(true, true, true);
    }
    for (var idx = 1; idx < length; ++idx) {
        rc = rc + il_random_char(true, true, true);
    }
    return rc;
}

function il_reload_captcha(url) {
    var uid = il_generate_uid(16);
    url = url.replace(/-[0-9a-zA-Z]+\.jpg/, '-' + uid + '.jpg');
    $('img._captcha_img').attr('src', url);
}

function il_switch_language(code) {
    var current = window.location.href;
    var index = current.indexOf("#");
    if (index != -1) {
        current = current.substring(0, index);
    }

    index = current.indexOf("_lang=");
    if (index != -1) {
        var toReplace = current.substring(index, index+11);
        location.href = current.replace(toReplace, "_lang=" + code);
        return;
    }

    index = current.indexOf("?");
    if (index != -1) {
        location.href = current + "&_lang=" + code;
    } else {
        location.href = current + "?_lang=" + code;
    }
}

function il_switch_currency(code) {
    var current = window.location.href;
    var index = current.indexOf("#");
    if (index != -1) {
        current = current.substring(0, index);
    }

    index = current.indexOf("_currency=");
    if (index != -1) {
        var toReplace = current.substring(index, index+13);
        location.href = current.replace(toReplace, "_currency=" + code);
        return;
    }

    index = current.indexOf("?");
    if (index != -1) {
        location.href = current + "&_currency=" + code;
    } else {
        location.href = current + "?_currency=" + code;
    }
}
