var CAT_NO_SELECTED = "All"
var DIV_PREF = "div"

// aCatList array indices
var X_ID   = 0
var X_NAME = 1
var X_SUBS = 2

var CatBox = null
var CatId  = null
var SubId  = ''
var SsubId = ''
var CatNoSelected

function GetSubCats( cCat, cSub, cSsub, cCNS ) { var i;

 CatNoSelected = (GetSubCats.arguments.length >= 4 && cCNS.length > 0) ? cCNS : CAT_NO_SELECTED;
 var oCat  = document.getElementById(cCat);
 var oSub  = document.getElementById(cSub);
 var oSsub = document.getElementById(cSsub);
 var dSub  = document.getElementById(DIV_PREF + cSub);
 var dSsub = document.getElementById(DIV_PREF + cSsub);

 var cat_id  = oCat.value;
 var sub_id  = oSub.value;
 var ssub_id = oSsub == null ? '' : oSsub.value;
 
 var cat_x = -1, sub_x  = -1, ssub_x = -1;

 if( CatBox != cCat ) { CatId = null; SubId = ''; SsubId = ''; }

 if( CatId != cat_id ) {  // Category changed
   if( dSsub != null ) dSsub.style.display = 'none';
   if( oSsub != null ) oSsub.options.length = 0;
   if( cat_id.length > 0) cat_x = find_x( aCatList, cat_id );

   if( cat_x >= 0 && aCatList[cat_x].length > X_SUBS &&
       aCatList[cat_x][X_SUBS].length > 0 ) {
     if( dSub != null ) dSub.style.display = '';
     sub_options( oSub, aCatList[cat_x][X_SUBS] );

     if( CatId == null && sub_id.length > 0 &&
         (sub_x = find_x( aCatList[cat_x][X_SUBS], sub_id )) >=0 )
       oSub.value = sub_id;
     else  oSub.value = '';  }

   else {  // No Cat selected | No Subcats Descr
     if( dSub != null ) dSub.style.display = 'none';
     oSub.options.length = 0;  }}

 if( CatId == cat_id && SubId != sub_id || CatId == null ) {  // Subcategory changed

   cat_x = find_x( aCatList, cat_id );
   if( cat_x >= 0 )  sub_x = find_x( aCatList[cat_x][X_SUBS], sub_id )

   if( sub_x >= 0 && aCatList[cat_x][X_SUBS][sub_x].length > X_SUBS && 
       aCatList[cat_x][X_SUBS][sub_x][X_SUBS].length > 0 ) {
     if( dSsub != null ) dSsub.style.display = '';
     sub_options( oSsub, aCatList[cat_x][X_SUBS][sub_x][X_SUBS] );

     if( CatId == null && ssub_id.length > 0 && 
         (ssub_x = find_x( aCatList[cat_x][X_SUBS][sub_x][X_SUBS], ssub_id )) >=0 )
       oSsub.value = ssub_id;
     else  oSsub.value = '';  }

   else {  // No Subcat selected | No Subsubcats Descr
     if( dSsub != null ) dSsub.style.display = 'none';
     if( oSsub != null ) oSsub.options.length = 0;  }}

 CatBox = cCat; CatId = cat_id; SubId = sub_id;  }

function find_x( a, id ) { var i;
 for( i=0; i<a.length; i++)  if( a[i][X_ID] == id)  return i;
 return -1; }

function sub_options( o, a ) { var i;
 o.options.length = 0;
 o.options[0] = new Option(CatNoSelected, '');
 for( i=0; i < a.length; i++ )
   o.options[i+1] = new Option(a[i][X_NAME], a[i][X_ID]); }

function ShowSuggest( cCat, cSub, cSsub, cCat2, cSub2, cSsub2 ) {

 var oCat  = document.getElementById(cCat);
 var oSub  = document.getElementById(cSub);
 var oSsub = cSsub == null ? null : document.getElementById(cSsub);
 var tCat  = document.getElementById(cCat2);
 var tSub  = document.getElementById(cSub2);
 var tSsub = cSsub2 == null ? null : document.getElementById(cSsub2);
 var dCat  = document.getElementById(DIV_PREF + cCat2);
 var dSub  = document.getElementById(DIV_PREF + cSub2);
 var dSsub = cSsub2 == null ? null : document.getElementById(DIV_PREF + cSsub2);

 var cat_id  = oCat.value;
 var sub_id  = oSub.value;
 var ssub_id = oSsub == null ? null : oSsub.value;

 if( cat_id.length == '' ) {
   dCat.style.visibility = 'visible';
   dSub.style.visibility = 'visible';
   if( dSsub != null ) dSsub.style.visibility = 'visible'; }

 else if( sub_id.length == '' ) {
   dCat.style.visibility = 'hidden';
   tCat.value = '';
   dSub.style.visibility = 'visible';
   if( dSsub != null ) dSsub.style.visibility = 'visible'; }

 else if( ssub_id != null && ssub_id.length == '' ) {
   dCat.style.visibility = 'hidden';
   tCat.value = '';
   dSub.style.visibility = 'hidden';
   tSub.value = '';
   if( dSsub != null ) dSsub.style.visibility = 'visible'; }

 else {
   dCat.style.visibility = 'hidden';
   tCat.value = '';
   dSub.style.visibility = 'hidden';
   tSub.value = '';
   if( dSsub != null ) dSsub.style.visibility = 'hidden';
   if( tSsub != null ) tSsub.value = ''; }}

function GetSeeAlso(cSelect, cDiv) {
  var nCat = document.getElementById(cSelect).options[document.getElementById(cSelect).selectedIndex].value;
  if(nCat < 1) {
    document.getElementById(cDiv).innerHTML = "";
    return false;
  } 	
  var oXmlHttp = new XHConn();
  if (!oXmlHttp) {
     document.getElementById(cDiv).innerHTML = "";
	 return false;
  }
 
  var fnOnReady = function (oXML) {
      document.getElementById(cDiv).innerHTML = oXML.responseText;
      }  
  oXmlHttp.connect("inc/getseealso.asp", "POST", "cat=" + nCat, fnOnReady);
}  