var xmlHttp;
var country;
var aquaticarea;
var xmlHttp;
var NodeId;
var div;
var imageType;
var numberOfChildren;
var nodeDividers;
var iImage;
var fImage;
var loading;
var win;
var taxaElements = new Array();
    var taxa;

//Sets up the XMLHttpRequest Object for use with Ajax
function createXMLHttpRequest() {
    if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    }
}

//Shows the search results
function populate() {
    //document.getElementById("contentarea").innerHTML = "<span style='font-size:0.8em;font-weight:bold;'><img src='images/ajax-loader.gif' /> Loading results...</span>"
    document.getElementById("contentarea").innerHTML = "<img src='images/ajax-loader.gif' /> Loading results..."
    if (xmlHttp.readyState == 4) {
        if (xmlHttp.status == 200) {            
            document.getElementById("contentarea").innerHTML = xmlHttp.responseText;
            var contLen = document.getElementById("contentarea").childNodes.length;
            var totalrec = 0;
            for (i=0; i<contLen; i++){
                if (document.getElementById("contentarea").childNodes[i].id == "TotalResult"){
                    totalrec = document.getElementById("contentarea").childNodes[i].value;
                    document.getElementById("numresults").innerHTML = "Number of search results : " + totalrec;
                }
                if (document.getElementById("contentarea").childNodes[i].id == "TotalMatch") {
                    var matchdet="";
                    var totalmatch=0;
                    if(taxa != ""){
                    totalmatch=document.getElementById("contentarea").childNodes[i].value;
                    matchdet = matchdet + totalrec + " exact matches for " + taxa + "<br/>";
                    matchdet = matchdet + totalmatch + " matches (duplicates possible) in the hierarchy below " + taxa;
                    document.getElementById("matchrec").innerHTML = matchdet;
                    }
                }
            }
        }
    }
}

//Pulls back and shows the TextSearch Results
function showTextSearchResults(expertPhrase) {

    document.getElementById("numresults").innerHTML = "";
    document.getElementById("matchrec").innerHTML = "";
    if (document.getElementById("contentarea").innerHTML != null){
        document.getElementById("contentarea").innerHTML = "";
    }
    var isValue = false;
    var checkboxes = document.getElementById("conservation").checked + ":" +  document.getElementById("ecology").checked + ":" +  document.getElementById("economy").checked + ":" +  document.getElementById("genetics").checked + ":" +  document.getElementById("legislation").checked + ":" +  document.getElementById("management").checked + ":" +  document.getElementById("pathways").checked + ":" +  document.getElementById("physiology").checked + ":" +  document.getElementById("risk").checked + ":" +  document.getElementById("taxonomy").checked + ":0";
    var country="";
    var aquatic="";
    var area="";
    taxa="";

    taxa = document.getElementById("taxaselection").value;
    
    if (document.getElementById("Select1").selectedIndex != 0){
        country = document.getElementById("Select1").value;
        isValue = true;
    }
    if (document.getElementById("Select2").selectedIndex != 0){
        aquatic = document.getElementById("Select2").value;
        isValue = true;
    }
    if (document.getElementById("Select3").selectedIndex != 0){
        area = document.getElementById("Select3").value;
        isValue= true;
    }
    if (expertPhrase != ""){
        isValue = true;
    }
    if (taxa != "" ){
        isValue = true;
    }
    if (checkboxes.indexOf("true") >= 0){
        isValue = true;
    }

    if (isValue == true){
        createXMLHttpRequest();
        var url = "expertSearchResults.do?expertPhrase=" + expertPhrase + "&checkboxes=" + checkboxes+ "&country=" + country + "&aquatic=" + aquatic + "&area=" + area + "&taxa=" + taxa;
        xmlHttp.open("GET", url, true);
        xmlHttp.onreadystatechange = populate;
        xmlHttp.send(null);
    }
//  }
}

//Shows or hides the advanced options
function showhideoptions(obj) {
    var table = document.getElementById("searchoptions");
        
    if (table.style.visibility == "visible") {
        table.style.visibility = "hidden";
        table.style.display = "none";
        obj.className = "down";
    } else {
        table.style.visibility = "visible";
        table.style.display = "inline";
        obj.className = "up";
    }
}

//Either expand or collapse the node


//If the node is expanded, collapse it


function taxaPopup() {
    win = new Window({
        className: "bluelighting",
        resizable:false,
        minimizable:false,
        maximizable:false,
        title: "Please Select a Taxa",
        width:820,
        height:300,
        showEffect:Effect.BlindDown,
        hideEffect: Effect.BlindUp
    });
    //win.getContent().innerHTML = "<p class='popupdisp'><b>To explore -</b> Click on the folder <img src ='img/folder.gif' align='middle' />., <b>To select text -</b> Click on the text in the tree.</p><div id='ajaxtaxatree'><div class='TreeNode'><a id='P0' class='pnode' href=\"javascript:Toggle('0','-1','7','1:');\"><img id='I0' src='img/base.gif'/>Taxon Tree</a><div id='D0' class='clip'></div></div></div>";
    win.getContent().innerHTML = "<div class='dividewin'><p><b>To explore -</b> Click on the folder <img src='img/folder.gif' align='top' />.,<b>To select text -</b> Click on the text in the tree.</p>"+
    "<table width='100%' align='center' border='0'><tr><td>"+
    "<div id='ajaxtaxatree' style='height:210px;' ><div class='TreeNode'><a id='P0' class='pnode' href=\"javascript:Toggle('0','-1','7','1:');\"><img id='I0' src='img/base.gif' />Taxon Tree</a><div id='D0' class='clip'></div></div></div>"+
    "</td></tr></table></div><div style='width:290px;height:250px;float:right;'><p><b>Select Taxanomic</b></p><table width='100%' border='0'><tr><td>"+
    "<input type='text' id='srctaxa' style='width:150px; margin-left:5px;' onkeyup=\"doCompletion(document.getElementById('srctaxa').value);\" autocomplete='off' />"+
    "<div id='popup'><div id='scrollpopup'><table id='taxa_table'><tbody id='taxa_table_body'></tbody></table></div></div>"+
    "<input type='button' id='btnsearch' value='Select taxa' onclick='SelectTaxa();'/></td></tr></table></div>";

    win.onload = init();
    win.showCenter(true);
}

function lstTaxaPopup() {
    var rtnValue;
    var browser = navigator.userAgent;
    

    // if(browser.indexOf("MSIE") > 0){
    rtnValue = window.showModalDialog("expertTaxaListPopup.jsp", "Taxanomic List", "dialogHeight:350px;dialogWidth:820px;center:1;resizable:0;status:0;");
    // }else{
    //         rtnValue = window.open("expertTaxaListPopup.jsp", "Taxanomic List", "centerscreen,height=560,width=800,toolbar=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,modal=YES,dialog=YES,dependent=YES");
    //rtnValue = window.open("expertTaxaListPopup.jsp", "Taxanomic", "height=300,width=820,centerscreen,dependent=NO,dialog=YES,modal=YES,resizable=NO,scrollbars=NO,location=0,status=0,menubar=0,toolbar=0");
    //  }


    if ((rtnValue != null) && (rtnValue.length >0)){
        document.getElementById("taxaselection").value = rtnValue;
    }
}

function taxaCollection(rId){
    var elementId;
    var taxaNm;
    elementId = rId.id;
    taxaNm = elementId.substring(4,elementId.length);

    if (document.getElementById(elementId).checked == true){
        if (taxaElements.length > 0){
            document.getElementById(elementId).checked = false;
            alert("Sorry you can't select more than a one taxa");
        }else {
            document.getElementById("lbl"+elementId).style.color = "#FFFFFF";
            document.getElementById("lbl"+elementId).style.backgroundColor = "#372DFF";
            taxaElements.push(taxaNm);
        }
    }

    if (document.getElementById(elementId).checked == false){
        document.getElementById("lbl"+elementId).style.color = "#000000";
        document.getElementById("lbl"+elementId).style.backgroundColor = "#FFFFFF";
        for(var i=0; i<taxaElements.length; i++){
            if(taxaElements[i].toLowerCase() == taxaNm.toLowerCase()){
                taxaElements.length = 0;
            //taxaElements.splice(i, 1);
            }
        }
    }
}

function taxaCheck(tId){
    var elementId;
    var taxaNm;
    elementId = tId.id.substring(tId.id.indexOf('taxa'), tId.id.length);
    taxaNm = elementId.substring(4,elementId.length);

    if (document.getElementById(elementId).checked == true){
        for(var i=0; i<taxaElements.length; i++){
            if(taxaElements[i].toLowerCase() == taxaNm.toLowerCase()){
                taxaElements.length = 0;
            //taxaElements.splice(i, 1);
            }
        }
    }

    if (document.getElementById(elementId).checked == false){
        //taxaNm = document.getElementById("lbl"+elementId).innerHTML;
        if (taxaElements.length > 0){
            alert("Sorry you can't select more than a one taxa");
        }else {
            taxaElements.push(taxaNm);
        }
    }
}

function Refresh(){
    document.getElementById("srchexpert").reset();
    document.getElementById("numresults").innerHTML = "";
    document.getElementById("matchrec").innerHTML = "";
    document.getElementById("contentarea").innerHTML = "";

}

function taxaClose(){
    window.returnValue = "";
    window.close();
}

function taxaSubmit(){
    if ((taxaElements != null) && (taxaElements.length > 0)){        
        window.returnValue = taxaElements.toString();
        taxaElements.length = 0;
        window.close();
    }else
    {
        window.returnValue = "";
        window.close();
    }
}

function SearchTaxa(){
    var taxaNm;
    taxaNm = document.getElementById("srctaxa").value;    
    if ((document.getElementById("lbltaxa"+taxaNm) != null) && (document.getElementById("lbltaxa"+taxaNm).innerHTML.toUpperCase() == taxaNm.toUpperCase())){
        if(taxaElements.length > 0) {
            alert("Sorry you can't select more than a one taxa");
        }
        else{
            document.getElementById("taxa"+taxaNm).focus();
            document.getElementById("taxa"+taxaNm).checked = true;
            document.getElementById("lbltaxa"+taxaNm).style.color = "#FFFFFF";
            document.getElementById("lbltaxa"+taxaNm).style.backgroundColor = "#372DFF";
            taxaElements.push(taxaNm);
        }
    }
    else {
        alert("Record not found.");
        document.getElementById("srctaxa").focus();
    }
}

function getResult(event)
{
    var expName,SourceEle;
    expName = document.getElementById('names').value;
    SourceEle = event.srcElement;

    if ((event.keyCode==13) && (SourceEle.id != "btnRefresh") &&(SourceEle.id != "btnTaxa")) {
        document.getElementById("btnSearch").focus();
        showTextSearchResults(expName);
    }
}

function getFocus()
{
    document.getElementById("btnSearch").focus();
    document.getElementById("names").select();
}

//function populateTaxaSelection(name) {
//    Windows.returnValue = name;
//    Windows.closeAll();
//}
//function SelectTaxa(){
//    Windows.returnValue = taxaField.value;
//    Windows.closeAll();
//}
