/*
    js utility functions
        hidediv(id)
        showdiv(id)
        togglediv(id)
        overstate(id)
        offstate(id)
        overstatedouble(id1, id2)
        offstatedouble(id1, id2)
        valid_email(s)
        valid_alphanum(s)
        imagechange(id, file)
        toggleclass(id)
        setclass(id, s)
*/


// show/hide elements

function hidediv(id) {
    try {
	    document.getElementById(id).style.display = 'none';
    } catch(e) {}
}

function showdiv(id) {
    try {
	    document.getElementById(id).style.display = 'block';
    } catch(e) {}
}

function togglediv(id) {
    if(document.getElementById(id).style.display == 'none') {
        document.getElementById(id).style.display = 'block';
    } else {
        document.getElementById(id).style.display = 'none';
    }
}


// change state

function overstate(id) {
	document.getElementById(id).className="overstat";
}

function offstate(id) {
	document.getElementById(id).className="offstat";
}

function overstatedouble(id1, id2) {
	document.getElementById(id1).className="overstat";
	document.getElementById(id2).className="overlink";
} 

function offstatedouble(id1, id2) {
	document.getElementById(id1).className="offstat";
	document.getElementById(id2).className="offlink";
}


// validate strings

function valid_email(s) {
    return(s.search('^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$') != -1);
}
function valid_alphanum(s) {
    return(s.search('\\w+') != -1);
}


// rollover, set src
function imagechange(id, s) {
    document.getElementById(id).src = s;
}

// switch class on/off
function toggleclass(id) {
    if(document.getElementById(id).className == 'off'){ 
        document.getElementById(id).className = 'active';
    } else {
        document.getElementById(id).className = 'off';
    }
}

// set element class
function setclass(id, s) {
		document.getElementById(id).className = s;
}


// global element stack
masterstack = new Array();

function pushdiv(stack, id) {
    if (masterstack[stack] == undefined) masterstack[stack] = new Array();
    masterstack[stack].push(id);
}

function shiftdiv(stack) {
    if (masterstack[stack] != undefined && masterstack[stack].length > 0) {
        return(masterstack[stack].shift());
    }
}

/**
 * Function include
 * include a javascript file in the head document
 *
 * @param String filename
 *
 * @return Integer
 */
function include(filename){
    var script, url;

    filename = filename.toLowerCase();
    url = window.location.href.toLowerCase();
    
    script = document.createElement('script');

    script.setAttribute('type', 'text/javascript');
    script.setAttribute('src', filename);
    
    document.getElementsByTagName('head')[0].appendChild(script);

    // save references to this js file
    window.__js_document = window.__js_document || {};
    window.__js_document[url] = window.__js_document[url] || {};
    window.__js_document[url].includes = window.__js_document[url].includes || {};
    
    if(!window.__js_document[url].includes[filename])
        window.__js_document[url].includes[filename] = 1;
    else
        window.__js_document[url].includes[filename]++;

    return window.__js_document[url].includes[filename];
}

/**
 * Function include_once
 * include just once the javascript file
 *
 * @param String filename
 *
 * @return Boolean
 */
function include_once(filename){
    var url;

    filename = filename.toLowerCase();
    url = window.location.href.toLowerCase();
    
    // check whether the js file has been included
    try{
        if(typeof(window.__js_document[url].includes[filename]) != 'undefined')
            return false;
        else
            throw 'Not included js document';
    }
    catch(e){
        include(filename);
        return true;
    }
}
