
var req_num = 0;


var req;

function askForXMLDoc(url) {


    req = false;
    

    // Create XMLHttpRequest Object.


    if( window.XMLHttpRequest ) {
        // native object
        try {
            req = new XMLHttpRequest();
        } catch(e) {
            req = false;
        }

    } else if ( window.ActiveXObject ) {
        // ActiveX?
        try {
            req = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
            // no? well, how about...
            try {
                req = new ActiveXObject("Microsoft.XMLHTTP");
            } catch(e) {
                req = false;
            }
        }
    }


    // do we have an object or not?
    if( req ) {

        // get the thing going
        req.onreadystatechange = handleReq;
        req.open( "GET", url, true );

        msgAjax( "Sending request for: <a href=\"" + url + "\">" + url + "</a> [request #" + ++req_num + "]");
        req.send("");

    }

}


function handleReq() {

    switch( req.readyState ) {
        case 0:
            stateText = "Uninitialized";
            break;
        case 1:
            stateText = "Loading";
            break;
        case 2:
            stateText = "Loaded";
            break;
        case 3:
            stateText = "Interactive";
            break;
        case 4:
            stateText = "Complete";
            break;
    }

    msgAjax(  "XMLHttpRequest object state changed. Current state: " + req.readyState + " " + stateText );
    switch( req.readyState ) {


        case 4: // loaded
            if( req.status == 200 ) { // OK
                handleResponse( req );
            } else {
                window.document.getElementById("ajax").innerHTML += "<p>[!] Error accessing the XML document. \n(" + req.status + ": " + req.statusText + ")</p>";
            }
            msgAjax( "AJAX transaction terminated. Result: HTTP Code: " + req.status + " " + req.statusText );
            separatorAjax();
            highlight("box");

//            window.document.getElementById("ajax").scroll(0,1000);
            break;

    }


}

function msgAjax( msg ) {

    var ajax = window.document.getElementById("ajax");

    ajax.innerHTML += "<p>" + msg + "</p>";

}

function separatorAjax() {
    window.document.getElementById("ajax").innerHTML += "<hr />";
}

var step;
function highlight(id) {
    step = 0x0;

    var obj = window.document.getElementById(id);
    
    obj.style.backgroundColor = "#ff3";
    
    setTimeout( "fade('" + id + "');", 100);

}

function fade( id ) {
    var obj = window.document.getElementById(id);
    
    
    if( step <= 15 ) {

        obj.style.backgroundColor = "#ff" + hexalizer(step);
    
        setTimeout( "fade('" + id + "');", 50);
    } else
        obj.style.backgroundColor = "#fff";
        
    step++;
}

function hexalizer( n ) {

    var lista = new Array( "a", "b", "c", "d", "e", "f" );

    if( n <= 9 )
        return n;
    else {
        n -= 10;
        return lista[n];
    }
}
