var groupID=null;
var qsID=null;
var qsNoNode=null;
var curClicked = null;
var GroupIDs = null;


function MM_openBrWindow(theURL,winName,features) { //v2.0
  self.name = "main";

  lbnEle=document.getElementById('nodeLBN');
  if (!!lbnEle)
    lbnEle.hash=document.location.hash;
    
  window.open(theURL,winName,features);
}

function loadRightMenu(ID)
{ 
  if (ID == '')
    ID = "intro";
	
  if (curClicked)
    if(curClicked==ID)
      return;

  curClicked = ID;
  groupID = ID;
  if (qsNoNode)
    if(qsNoNode.id==ID)
    {
      qsNoNode.style.color=HIGHLIGHT_COLOR;
      qsNoNode.style.backgroundColor=HIGHLIGHT_BG;
    }
    else
    {
      qsNoNode.style.color=lastClickedColor;
      qsNoNode.style.backgroundColor=lastClickedColor;
      qsNoNode=null;
    };

  http_request = false;
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
     http_request = new XMLHttpRequest();
     if (http_request.overrideMimeType) {
        http_request.overrideMimeType('text/xml');
     }
  } else if (window.ActiveXObject) { // IE
     try {
        http_request = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (e) {
        try {
           http_request = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {}
     }
  }
  if (!http_request) {
     alert('Cannot create XMLHTTP instance');
     return false;
  }
  try {
    http_request.onreadystatechange = onLoadContents;
    http_request.open('GET',ID + '.html', true);
    http_request.send(null);
  } catch (e) {}

}

function loadRecentUpdate()
{
  if (curClicked)
    if(curClicked=='RU')
      return;

  clickOnFolder("0");
  qsNoNode = document.getElementById('nodeRU');
  loadRightMenu('RU');
}

function highlightSelectedQS() {
  ele=document.getElementById(qsID);

  if(ele)
     ele.focus();

  ele=document.getElementById('tar'+qsID);
  if(ele)
  {
    document.location.hash ='#' + qsID;
    ele.className='locatedhilit';
  }
  else
  {
    if (!groupID)
       document.location.hash ='#' + groupID;
  }

}

function onLoadContents() {
  ritMenu=document.getElementById('rightMenu') ;
  if (ritMenu){
    if (http_request.readyState == 4)
    {
      ritMenu.innerHTML=http_request.responseText;

      if (ritMenu.innerHTML.trim()!='')
      {
        if (qsID)
        {
          highlightSelectedQS();
          document.location.hash ='#' + qsID;	

          dhtmlHistory.add(qsID);

        }
        else
        if (!!groupID)
	{
          document.location.hash ='#' + groupID;
  
          dhtmlHistory.add(groupID);
	}

        groupID = null;
        qsID = null;
      }
    }
    else
      { ritMenu.innerHTML="";}
  }
}

function selectAnchor(anchorID) {
var nodeObj, name, parNode, anchorID;
var objArray = new Array();
var i=0;

  nodeObj=findQSObjNode(anchorID);    

  if (!!nodeObj)
  {
    parNode=nodeObj.parentObj;

    while (!!parNode){
      objArray[i]=parNode;	      		
      parNode=parNode.parentObj;
      i++;	
    }

    for (i=objArray.length-2;i>=0;i--)
    {
      if (objArray[i].isOpen != true)
        clickOnNode(objArray[i].id);
    }
  }
  gotoNode(anchorID);
}


function gotoNode(ID)
{
var nodeObj;
  nodeObj=findQSObjNode(ID);

  if (!!nodeObj)
  {
    clickOnFolder(nodeObj.id);
    //nodeObj.navObj.focus();
  }

  loadRightMenu(ID);
}


function GetParamValue(params, param)
{
var IDStr=null;
  for (i=0;i<params.length;i++)
  {
    p=params[i].split("=");
    if ((p.length==2)&&((p[0].trim().toLowerCase())==param.toLowerCase()))
    {
      IDStr=p[1];
      break;
    }
  }
  return IDStr;
}

String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

function selectNode(id){
  var paramStr= document.location.search;
  if (!!id)
    selectItem(id)
  else
  if (document.location.search != '')
  {
    if (paramStr.charAt(0)=='?')paramStr = paramStr.substr(1);

    if(paramStr != "")
    {
     params=paramStr.split("&");

     groupID=GetParamValue(params, "group");
     qsID=GetParamValue(params, "id");

     if(!!groupID)
     {
       if (!!qsID)
          document.location.href = document.location.protocol + "//" + document.location.host + document.location.pathname + '#' + qsID;
       else
       if (document.location.hash != '')
         document.location.href = document.location.protocol + "//" + document.location.host + document.location.pathname + document.location.hash;
       else
         document.location.href = document.location.protocol + "//" + document.location.host + document.location.pathname + '#' + groupID;
     }
     else
     if (document.location.hash!='')
       loadRightMenu(document.location.hash.substr(1));
     else
       selectAnchor("intro");
    }
    else
      selectAnchor("intro");
  }
  else
  if (document.location.hash != '')
  {
    groupID=null;
    qsID = null;

    if (GroupIDs)
    {
	selectItem(document.location.hash.substr(1));
    }
   else
      selectAnchor(document.location.hash.substr(1));
  }
  else
    selectAnchor("intro");
}

function selectItem(ItemID)
{
  if (GroupIDs) 
   groupID = GroupIDs[ItemID];
  else
   groupID = ItemID;  
  
  if(!!groupID)
    qsID = ItemID;
  else
    groupID = ItemID;

  selectAnchor(groupID);

}

function getQSID()
{
  var paramStr= document.location.search;

  if (paramStr.charAt(0)=='?')paramStr = paramStr.substr(1);

  if(paramStr != "")
  {
   params=paramStr.split("&");

   if (document.location.hash!='')
     qsID=document.location.hash.substr(1);
   else
     qsID=null;
  }
  else
    qsID=null;
}


function doLoad()
{
  initOnLoad();
}

function doLoadArchiveLists(ID)
{
  archDiv = document.getElementById('div' + ID);

  if (!!archDiv)
  {
    ArchivedID = ID;
    http_request = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
       http_request = new XMLHttpRequest();
       if (http_request.overrideMimeType) {
          http_request.overrideMimeType('text/xml');
       }
    } else if (window.ActiveXObject) { // IE
       try {
          http_request = new ActiveXObject("Msxml2.XMLHTTP");
       } catch (e) {
          try {
             http_request = new ActiveXObject("Microsoft.XMLHTTP");
          } catch (e) {}
       }
    }
    if (!http_request) {
       alert('Cannot create XMLHTTP instance');
       return false;
    }
    try {
      http_request.onreadystatechange = onLoadArchiveLists;
      http_request.open('GET','../' + 'archives_' + REGION + '/' + ID + '_arch_vers.html', true);
      http_request.send(null);
    } catch (e) {}

  }
  else
    ArchivedID=null;

}

function onLoadArchiveLists()
{
  archDiv = document.getElementById('div' + ArchivedID);

  if (archDiv){
    if (http_request.readyState == 4)
    {
      archDiv.innerHTML=http_request.responseText;
    }
    else
    {
      archDiv.innerHTML="";
    }
  }
}



function initOnLoad() {
  selectNode();

  dhtmlHistory.initialize();
  dhtmlHistory.addListener(selectItem);
}

