/**
 *
 * Function for enhancing all occurancies of <a> and <form> tags with rel="external" with a target="_blank"
 *
 * @author Rory Bol, LETO Grafisch Serviceburo, <r.bol@letoservice.nl>
 *
 */

function externalLinks() {
    // Return if JS DOM1 is not supported
    if (!document.getElementsByTagName) return;
    // Fetch all <a>'s
    var anchors = document.getElementsByTagName("a");
    // Loop through all <a>'s
    for (var i=0; i<anchors.length; i++) {
        var anchor = anchors[i];
        // Check if the element contains a href and a rel "external" attribute
        if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
            // Set the target to Blank for those
            anchor.target = "_blank";
        }
    }

    // Do the same for forms with a rel = external
    var forms = document.getElementsByTagName("form");
    for (var i=0; i<forms.length; i++) {
        var frm = forms[i];
        if (frm.getAttribute("action") && frm.getAttribute("title") == "external") {
            frm.target = "_blank";
        }
    }
}


/**
 *
 * Functions which sets the focus on a formfield
 *
 */

function focus(variabele) {
    document.getElementById(variabele).focus();
}

/**
 *
 * Standard function for all the AJAX items
 *
 **/

function ajax(url, urlvar, elementid) {
    var xmlHttp=null;
    try{
        // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
    } catch (e) {
        //Internet Explorer
        try{
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
    }

    if (xmlHttp==null) {
        // alert ("Browser does not support HTTP Request")
        return
    }
    url=url+"&q="+urlvar
    url=url+"&sid="+Math.random()
    xmlHttp.onreadystatechange=stateChanged
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)
    function stateChanged() {
        if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
            document.getElementById(elementid).innerHTML=xmlHttp.responseText
        }
    }
}


/**
 *
 * Favorites link function for all browsers (Jquery)
 *
 **/

$(document).ready(function(){
    $("a.favorites").click(function(e){
        e.preventDefault(); // this will prevent the anchor tag from going the user off to the link
        var bookmarkUrl = this.href;
        var bookmarkTitle = document.title;
        if (window.sidebar) { // For Mozilla Firefox Bookmark
            window.sidebar.addPanel(bookmarkTitle, bookmarkUrl,"");
        } else if( window.external || document.all) { // For IE Favorite
            window.external.AddFavorite( bookmarkUrl, bookmarkTitle);
        } else if(window.opera) { // For Opera Browsers
            $("a.favorites").attr("href",bookmarkUrl);
            $("a.favorites").attr("title",bookmarkTitle);
            $("a.favorites").attr("rel","sidebar");
        } else { // for other browsers which does not support
//                     alert('Your browser does not support this bookmark action');
             return false;
        }
    });
});
