// This function decodes the any string
// that's been encoded using URL encoding technique
function _unescape(encodedString) {
    // Create a regular expression to search all +s in the string
    var lsRegExp = /\+/g;
    // Return the decoded string
    return unescape(String(encodedString).replace(lsRegExp, " "));
}

function $$jq(id, context) {
    var el = $jq("#" + id, context);
    if (el.length < 1)
        el = $jq("[id$=_" + id + "]", context);
    return el;
}

$jq(document).ready(function() {
    /* top navigation search input field. auto submit on ENTER */
    $jq("#search input").keypress(function(e) {
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
            $jq("#search input[type=button]").click();
        }
    });
});

function openWindow(url) {
    if (url) {
        var newWindow = window.open(url, "window", "");
        newWindow.focus();
    }
    return false;
}

function openWindow(url, width, height) {
    if (url) {
        var newWindow = window.open(url, "window", "width=" + width + ",height=" + height);
        try {
            newWindow.focus();
        } catch (e) { }
    }
    return false;
}

function openWindow(url, width, height, parameters) {
    if (url) {
        var newWindow = window.open(url, "window", "width=" + width + ",height=" + height + "," + parameters);
        try {
            newWindow.focus();
        } catch (e) { }
    }
    return false;
}

/* utility function to read a custom URL parameter or to get all url parameters */
$jq.extend({
    getUrlVars: function() {
        var vars = [], hash;
        var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
        for (var i = 0; i < hashes.length; i++) {
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }
        return vars;
    },
    getUrlVar: function(name) {
        return $jq.getUrlVars()[name];
    }
});

/*
 * utility function to set common group names for radio buttons
 * within a repeater control
 * http://support.microsoft.com/default.aspx?scid=kb;en-us;Q316495
 */
function SetUniqueRadioButton(nameregex, current) {
    re = new RegExp(nameregex);
    for (i = 0; i < document.forms[0].elements.length; i++) {
        elm = document.forms[0].elements[i]
        if (elm.type == 'radio') {
            if (re.test(elm.name)) {
                elm.checked = false;
            }
        }
    }
    current.checked = true;

    if (current.value != "-1") {
        $jq(".download_link").show();
    } else {
        $jq(".download_link").hide();
    }
}

function _parseInt(value, fallback) {
    var result = parseInt(value, 10);
    if (isNaN(result)) {
        return fallback;
    }
    return result;
}

function _parseFloat(value, fallback) {
    var result = parseFloat(value);
    if (isNaN(result)) {
        return fallback;
    }
    return result;
}

/**************************/
/* Contact List Functions */
/**************************/

function changeDivision() {
    try {
        $jq.ajax({
            url: "http://localhost/en/.aspx",
            type: "POST",
            data: "",
            dataType: "html",
            success: function(data) {
                $jq("#StepS2").html(data);
            }
        });
    } catch (e) { }
}
