var xmlHttp; 
var is_ie = (navigator.userAgent.indexOf('MSIE') >= 0) ? 1 : 0; 
var is_ie5 = (navigator.appVersion.indexOf("MSIE 5.5")!=-1) ? 1 : 0; 
var is_opera = ((navigator.userAgent.indexOf("Opera6")!=-1)||(navigator.userAgent.indexOf("Opera/6")!=-1)) ? 1 : 0; 
//netscape, safari, mozilla behave the same??? 
var is_netscape = (navigator.userAgent.indexOf('Netscape') >= 0) ? 1 : 0; 
var test_result; // 0 -- not ok (server did not reply), 1 -- not ok (logged off), 2 -- ok    

var orgInfoURL;
var orgInfo_LastDivID; // stores the ID of the target HTML control (posts results go here) 

////////////////////////////////////////////////////////////////////////////////////

// XMLHttp send GET request 
function xmlHttp_Get(xmlhttp, url) { 
    xmlhttp.open('GET', url, true); 
    xmlhttp.send(null); 
} 

function GetXmlHttpObject(handler) { 
    var objXmlHttp = null;    //Holds the local xmlHTTP object instance 

    //Depending on the browser, try to create the xmlHttp object 
    if (is_ie){ 
        //The object to create depends on version of IE 
        //If it isn't ie5, then default to the Msxml2.XMLHTTP object 
        var strObjName = (is_ie5) ? 'Microsoft.XMLHTTP' : 'Msxml2.XMLHTTP'; 
          
        //Attempt to create the object 
        try{ 
            objXmlHttp = new ActiveXObject(strObjName);            
            objXmlHttp.onreadystatechange = handler; 
        } 
        catch(e){ 
            //Object creation errored 
            alert('IE detected, but object could not be created. Verify that active scripting and activeX controls are enabled'); 
            return; 
        } 
    } 
    else if (is_opera){ 
        //Opera has some issues with xmlHttp object functionality 
        alert('Opera detected. The page may not behave as expected.'); 
        return; 
    } 
    else{ 
        // Mozilla | Netscape | Safari         
        objXmlHttp = new XMLHttpRequest(); 
        objXmlHttp.onload = handler; 
        objXmlHttp.onerror = handler; 
    } 
      
    //Return the instantiated object 
    return objXmlHttp; 
}                 

/////////////////////////////////////////////////////////////////////////////////////

function showError(strError)
{
    var myErr = document.getElementById('mainErrorBox');                  
    // if the error box is hidden then make it visible and show an error message
    if (myErr.style.display != '') 
    {
        myErr.style.display='';
        alert(strError);        
    }
}

function hideError()
{
    var myErr = document.getElementById('mainErrorBox');                  
    myErr.style.display='none';          
} 

function processError()
{
    if (test_result == 1)
        showError('You are no longer logged in on the server!');
    else
        if (test_result == 0)
            showError('Error communicating with the server!');
        else
            if (test_result == 2)
                hideError(); 
} 

function responseToTestResult(str, http_status)
{
    if ((str == '!LoggedIn')||(str == '')||(str == null)||(http_status != 200))
    {
      if (str == '!LoggedIn')
        return 1; 
      else
        return 0; 
    }           
    else
      return 2; 
} 

function boo(elemID) // blockOnOff
{
	  var x = document.getElementById(elemID);
    if (x.style.display=="") { x.style.display="none"; } else {x.style.display="";}
}  

/////////////////////////////////////////////////////////////////////////////////////

function gi(organizationID, divID) // getOrgInfo
{       
    var desc = document.getElementById(divID).innerHTML;        
    if (!((desc == '')||(desc == null))) return;
    try
    { 
      orgInfo_LastDivID = divID;
      xmlHttp = GetXmlHttpObject(getOrgInfoHandler);                  
      //Send the xmlHttp get to the specified url; makes the URL different/random to avoid caching...
      xmlHttp_Get(xmlHttp, orgInfoURL + '?cid=' + organizationID + '&random=' + Math.random() * Date.parse(new Date()) );
    }
    catch(e)
    {
      showError(e);
    }            
} 

function getOrgInfoHandler() 
{ 
    //readyState of 4 or 'complete' represents that data has been returned 
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == 'complete'){ 
        //Gather the results from the callback 
        var str = xmlHttp.responseText;                                 
        test_result = responseToTestResult(str,xmlHttp.status);         
        processError();        
        // update div content
        var content = document.getElementById(orgInfo_LastDivID);                
        content.innerHTML = str;
    } 
}

/////////////////////////////////////////////////////////////////////////////////////

function ClickSearch(event, search, url) {
    if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) {
        window.location = url + "?type=directory&q=" + escape(search.value) + "&page=1";
    }
}

function SearchRedirect(event, id, url) {
    var search = document.getElementById(id);
    window.location = url + "?type=directory&q=" + escape(search.value) + "&page=1";
}