/*
common.js
Author: Kashif Iftikhar
License: GNU GPL v2
Description: Contains common javascript functions that come handy in various situations.
*/

/* shortcut function replacement for document.getElementById */
function E(element_id){
    return document.getElementById(element_id)
}


/* AJAX related functions that need to be used with dojo */
function loadIntoNode(data, xhr){
    if(xhr.args.node){
        xhr.args.node.innerHTML = data;
    }
}

function error(data, xhr){
    if(xhr.args.node){
        xhr.args.node.innerHTML = '<font color="red">Error!</font>';
    }
}

function intoNode(node, url){
    return {
        url: url,
        node: dojo.byId(node),
        load: loadIntoNode,
        error: error
    };
}

function getAsJSON(url, func_name){
    return {
        url: url,
        handleAs: "json",
        load: func_name,
        error: error
    };
}

/* Fetches data (probably html) from given url (page) and loads it as the inner html of given html element id (target) */
function load_data(page, target){
    //alert(dojo.byId('results_div').innerHTML);
    E('ajax_status').style.visibility='visible';
    dojo.xhrGet(intoNode(target, page));
    E('ajax_status').style.visibility='hidden';
}

/* fetches jason data from given url (page) and calls the given function (func_name) with retreived data */
function load_json(page, func_name){
    
    dojo.xhrGet(getAsJSON(page, func_name));
}

