var prvDeptId = '';
var prvSubDeptId = '';
var prvKeyId = '';
var type = '';
var ddata = JSON.parse(darray);
var adata = JSON.parse(aarray);
var zdata = JSON.parse(zarray);
var rdata = null;
function getPTypes(obj, idx) {
    document.getElementById('dptypes').style.background = '#fff';
    document.getElementById('dages').style.background = '#fff';
    document.getElementById('dlocs').style.background = '#fff';
    if ( idx == 1 ) {
      rdata = ddata;
      type = 'Program Type';
    } else if ( idx == 2 ) {
      rdata = adata;
      type = 'Age Groups';
    } else if ( idx == 3 ) {
      rdata = zdata;
      type = 'Location';
    }
    obj.style.background = '#c7d9f0';
    try {
        document.getElementById("obox2").innerHTML = "";
        document.getElementById("obox3").innerHTML = "";
        document.getElementById("obox4").innerHTML = "";
        prvSubDeptId = '';
        prvDeptId = '';
        prvKeyId = '';
        for ( var deptId = 0; deptId < rdata.depts.length; deptId ++ ) {
            var div = document.createElement("div");
            div.id = 'd2-'+deptId;
            div.className = 'line';
            div.value = rdata.depts[deptId].dept;
            div.onclick = function deptClick() { 
                this.style.background = '#c7d9f0';
                var ix = this.id.substring(3);
                if ( prvDeptId != '' && prvDeptId != this.id ) {
                    document.getElementById(prvDeptId).style.background = '#fff';
                }
                prvDeptId = this.id;
                getSubDept(this.value, ix);
            };
            if ( rdata.depts[deptId].subs.length > 0 ) {
                var img = document.createElement("img");
                img.src = '../jmx-console/icons/rightarrow.gif';
                div.appendChild(img);
                div.appendChild( document.createTextNode(rdata.depts[deptId].dept) );
            } else {
                div.appendChild( document.createTextNode(rdata.depts[deptId].dept) );
            }
            document.getElementById("obox2").appendChild( div );
        }
    } catch (err) {
        alert('ERROR: '+err);
    }
}
function getSubDept(dept, deptId) {
    try {
        document.getElementById("obox3").innerHTML = "";
        document.getElementById("obox4").innerHTML = "";
        prvSubDeptId = '';
        prvKeyId = '';
        if ( deptId > -1 && deptId < rdata.depts.length ) {
            for ( var subDeptId = 0; subDeptId < rdata.depts[deptId].subs.length; subDeptId ++ ) {
                var div = document.createElement("div");
                div.id = 'd3-'+subDeptId;
                div.className = 'line';
                div.value = rdata.depts[deptId].subs[subDeptId].sub;
                div.onclick = function subDeptClick() { 
                    this.style.background = '#c7d9f0';
                    var ix = this.id.substring(3);
                    if ( prvSubDeptId != '' && prvSubDeptId != this.id ) {
                        document.getElementById(prvSubDeptId).style.background = '#fff';
                    }
                    prvSubDeptId = this.id;
                    getKeys(this.value, deptId, ix);
                };
                if ( rdata.depts[deptId].subs[subDeptId].keys.length > 0 ) {
                    var img = document.createElement("img");
                    img.src = '../jmx-console/icons/rightarrow.gif';
                    div.appendChild(img);
                    div.appendChild( document.createTextNode(rdata.depts[deptId].subs[subDeptId].sub) );
                } else {
                    div.appendChild( document.createTextNode(rdata.depts[deptId].subs[subDeptId].sub) );
                }
                document.getElementById("obox3").appendChild( div );
            }
        }
    } catch (err) {
        alert("ERROR: "+err);
    }
}
function getKeys (sdept, deptId, subDeptId) {
    try {
        document.getElementById("obox4").innerHTML = "";
        prvKeyId = '';
        if ( deptId > -1 && subDeptId > -1 && rdata.depts.length ) {
            for ( var keysId = 0; keysId < rdata.depts[deptId].subs[subDeptId].keys.length; keysId ++ ) {
                var div = document.createElement("div");
                div.className = 'line';
                div.id = 'd4-'+keysId;
                div.value = rdata.depts[deptId].subs[subDeptId].keys[keysId].key;
                div.onclick = function keysClick () {
                    this.style.background = '#c7d9f0';
                    var ix = this.id.substring(3);
                    if ( prvKeyId != '' && prvKeyId != this.id ) {
                        document.getElementById(prvKeyId).style.background = '#fff';
                    }
                    prvKeyId = this.id;
                    doFinalSearch(deptId, subDeptId, ix);
                };
                div.appendChild( document.createTextNode(rdata.depts[deptId].subs[subDeptId].keys[keysId].key) );
                document.getElementById("obox4").appendChild( div );
            }
        }
    } catch (err) {
        alert("ERROR: "+err);
    }
}
function doFinalSearch (deptId, subDeptId, keyId ) {
    doSearch4(rdata.depts[deptId].dept, rdata.depts[deptId].subs[subDeptId].sub, rdata.depts[deptId].subs[subDeptId].keys[keyId].key);
}
function doSearch1() {
   document.ffind.group1.value = '';
   document.ffind.group2.value = '';
   document.ffind.group3.value = '';
   document.ffind.location.value ='';
   document.ffind.ageGroup.value = '';
   document.ffind.searchType.value = '';
   document.ffind.keyword.value = document.getElementById('skey').value;
   //clearFields();
   document.ffind.submit();
}
function doSearch2() {
   document.ffind.group1.value = '';
   document.ffind.group2.value = '';
   document.ffind.group3.value = '';
   document.ffind.location.value ='';
   document.ffind.ageGroup.value = '';
   document.ffind.searchType.value = '';
   if  (isNaN(document.getElementById('sclsid').value) ){
      document.getElementById('classnumerr').style.display = 'block';
     } else { 
      document.ffind.clsid.value = document.getElementById('sclsid').value;
      //clearFields();
      document.ffind.submit();
   }
}
function clearFields () {
   document.getElementById('sclsid').value = '';
   document.getElementById('skey').value = '';
   document.getElementById('dptypes').style.background = '#fff';
   document.getElementById('dages').style.background = '#fff';
   document.getElementById('dlocs').style.background = '#fff';
   document.getElementById('obox2').innerHTML = "";
   document.getElementById('obox3').innerHTML = "";
   document.getElementById('obox4').innerHTML = "";
}
function doSearch4(group1, group2, group3) {
   document.ffind.group2.value = group2;
   document.ffind.group3.value = group3;
   if ( type == 'Program Type' ) {
      type = '';
      document.ffind.group1.value = group1;
      document.ffind.searchType.value = 'Program Type';
      document.ffind.clsid.value = '';
      document.ffind.keyword.value = '';
      //clearFields();
      document.ffind.submit();
   } else if ( type == 'Age Groups' ) {
      type = '';
      document.ffind.ageGroup.value = group1;
      document.ffind.searchType.value = 'Age Groups';
      document.ffind.clsid.value = '';
      document.ffind.keyword.value = '';
      //clearFields();
      document.ffind.submit();
   } else if ( type == 'Location' ) {
      type = '';
      document.ffind.location.value = group1;
      document.ffind.searchType.value = 'Location';
      document.ffind.clsid.value = '';
      document.ffind.keyword.value = '';
      //clearFields();
      document.ffind.submit();
   } else {
      document.ffind.group1.value = '';
      document.ffind.group2.value = '';
      document.ffind.group3.value = '';
      document.ffind.location.value ='';
      document.ffind.ageGroup.value = '';
      document.ffind.searchType.value = '';
   }
}

