/*
 * Responses are directed through this. (From the request)
 * Handles various gui states.  Also breaks the chain and
 * displays the appropriate message if a link receives an error.
 */
var MISEROR = "THISISANERROR";
function requestReturned(xhr, action){
    if (xhr.readyState == 4){ 
        document.body.style.cursor = 'default';
        setStatusReady();
        qspop.op = true;
        qspop.hideqsnodeNow();
        document.getElementById('doplot').value = null;
        if(!hasError(xhr)){
            action(xhr);
        }else{
             //ui_wfisExec(false);
        }
    }
}
function setHeaderColor(hid, col){
    var he = document.getElementById(hid);
    he.style.background = col;
    he.parentNode.firstChild.style.background = col;
    he.parentNode.parentNode.style.background = col;
    if(col != '')he.parentNode.parentNode.className = 'cvsel';
    else he.parentNode.parentNode.className = '';
    he.parentNode.parentNode.parentNode.style.background = col;
}
function setHeaderFGColor(hid, col){
    var he = document.getElementById(hid);
    he.style.color = col;
    he.parentNode.firstChild.style.color = col;
    he.parentNode.parentNode.style.color = col;
    he.parentNode.parentNode.parentNode.style.color = col;
}
function tHover(a){
    setHeaderColor(a+'_title','#ff0000');   
}
function tHOut(a){
    setHeaderColor(a+'_title','green');   
}
function r_plot(xhr){    
    p = document.getElementById('centerPane');   
    while(p.childNodes.length > 0)
        p.removeChild(p.childNodes[0]);
    var sets = xhr.responseXML.getElementsByTagName('legend')[0].getElementsByTagName('set');
    for(i=0;i<sets.length;i++){
        var pcname = sets[i].getAttribute('name').split('_');
        var snum = pcname[pcname.length-1];
        setHeaderColor('p1s'+snum+'_title','#'+sets[i].getAttribute('color'));
     }   
    var objd = document.createElement("img");
    objd.src = xhr.responseXML.getElementsByTagName('imglink')[0].firstChild.nodeValue;
    p.appendChild(objd);
}
function hasError(xhr){
    var i = xhr.responseText.indexOf(MISEROR);
    if(i > -1 && i<5){//whitespace in there sometime
        setMessage(xhr.responseText);
        return true;
    }
    return false;
}
function setMessage(msg){
    var mp = document.getElementById('messagePane');
    var i = msg.indexOf(MISEROR);
    if(i < 5){
        mp.className = 'error';
        msg = msg.substring(i+MISEROR.length,msg.length);
     }else
        mp.className = null;   
    mp.innerHTML = msg;
    qspop.showqsnodeNow('messagePane',227, 1);
}

