/*
 * Utility method to dump arrays, objects, hashes
 * @param arr   The object to print
 * @param level 
 */
function dump(arr,level) {
    var dumped_text = "";
    if(!level) level = 0;

    //The padding given at the beginning of the line.
    var level_padding = "";
    for(var j=0;j<level+1;j++) level_padding += "    ";

    if(typeof(arr) == 'object') { //Array/Hashes/Objects
        for(var item in arr) {
            var value = arr[item];

            if(typeof(value) == 'object') { //If it is an array,
                dumped_text += level_padding + "'" + item + "' ...\n";
                dumped_text += dump(value,level+1);
            } else {
                dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
            }
        }
    } else { //Stings/Chars/Numbers etc.
        dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
    }
    return dumped_text;
}

/**
 * @parameter item
 * @parameter arr the array to search
 *
 */
function in_array(item,arr) {
    for(p=0;p<arr.length;p++) {
        if (item == arr[p]) {
            return p;
        }
    }
    return false;
}

/*
 * Checks the key pressed and in case it was the Enter key submits the
 * form handed over as a parameter
 */
function keyPressed(e,form){
    var code;
    if (!e) {
        var e = window.event;
    }
    if (e.keyCode){
        code = e.keyCode;
    }
    else if (e.which){
        code = e.which;
    }
    if (code == 13) { // Enter key
        form.submit();
    }
}

/*
 * Check if the Enter key has been pressed
 */
function enterKeyPressed(evt){
    var code;
    if (!evt) {
        var evt = window.event;
    }
    if (evt.keyCode){
        code = evt.keyCode;
    }
    else if (evt.which){
        code = evt.which;
    }
    return code == dojo.keys.ENTER ? true:false;
}

function redirect(to){
    location = to;
}

/*
 * Simulates that in IE pressing the Enter key does the same as the onChange
 * trigger without having to blur the input field.
 */
function IEonChange(evt,to){
    if (enterKeyPressed(evt)){
        redirect(to);
    }
}
/* ++++++++++++++++++++++ Institution ++++++++++++++++++++++++++++++++++++++++ */

function changeOrganisation(){
        selectorlist = document.getElementById('organisation_select');
        selectorother = document.getElementById('organisation');
        if (organisationListOrOther()== 'list'){
            selectorlist.style.display = 'block';
            selectorother.style.display = 'none';
        } else if (organisationListOrOther()== 'other'){
            selectorother.style.display = 'block';
            selectorlist.style.display = 'none';
        }
    }

function organisationListOrOther() {
    selector = document.getElementsByName('selector');
    // List of organisations
    if (selector[0].checked){
        return 'list'
    } else {  /// if(selector[1].checked) Other organisation
        return 'other';
    }
}

function checkOrganisation(){
    organisation_select = document.getElementById('organisation_select');
    organisation = document.getElementById('organisation');
    selectedType = document.getElementById('selectedType');
    try{
        if(organisationListOrOther()=='list'){
            organisation.value = organisation_select.value;
        }
        if (organisation.value == ''){
            alert('Please enter the organisation name');
            return false;
        } else{
            selectedType.value = organisationListOrOther();
            return true;
        }
    } catch(e){
        // in case organisation is undefined which happens when the 'save' function
        // in the form.xhtml is being called without the involvment from and
        // organisation field.
        return true;
    }
}

function setOrganisationField(){
    selectedValue = document.getElementById('selectedValue');
    selectedType = document.getElementById('selectedType');
    selectorlist = document.getElementById('organisation_select');
    selectorother = document.getElementById('organisation');
    selector = document.getElementsByName('selector');
    
    if (selectedType.value == 'other'){
        selectorother.style.display = 'block';
        selectorlist.style.display = 'none';
        selector[1].checked = true;
    } else if (selectedType.value == 'list'||selectedType.value == '' ){
        selectorlist.value = selectedValue.value;
        selectorother.style.display = 'none';
        selectorlist.style.display = 'block';
        selector[0].checked = true;
    }
}

