function NbspAsSpace(aText){
 var res=aText;
 res = res.replace(/&nbsp;/g, " ");
 return res;
}

function TrimNbsp(aText){
 var res=aText;
 res = res.replace(/&NBSP;/g, "&nbsp;");
 if (res.indexOf("&nbsp;")==0){
  res = res.substr(6);
 }
 if (res.lastIndexOf("&nbsp;")==res.length-6){
  res = res.substring(0,res.length-6);
 }
 return res;
}

function replaceSpaceWithNbsp(aText){
 var res=aText;
 res = res.replace(/ /g, "&nbsp;");
 return res;
}

function LinkIsActive(aUrl){
 if (isParentPage){
  return aUrl=="#";
 }else{
  return ((aUrl=="index.html")||(aUrl=="../index.html")||(aUrl=="../../index.html"));
 };
}

function GetMenuLinks(aDiv, aType){
 return GetNavigationLinks(aDiv, "txtnavsuba "+aType, "txtnavsub "+aType, "", ""/*, false*/);
}

function GetNavigationLinks(aDiv, aClassActive, aClassNotActive, aExcludeContaining, aStartWith/*, test*/){ 
 var cTagBegin="<NOBR>[";
 var cTagEnd="]</NOBR>";
 var res="";
 var aLinks=aDiv.innerHTML;
 var matchesStart = [];
 var aPos=0;
 var arrayIdx=0;
 
 aLinks = aLinks.replace(/nobr>/g, "NOBR>");
 aLinks = aLinks.replace(/<A /g, "<a ");
 aLinks = aLinks.replace(/<\/A>/g, "</a>");
//alert(aLinks);

 do{
  aPos=aLinks.indexOf(cTagBegin,aPos+1);
  if (aPos>-1){
   matchesStart[arrayIdx++]=aPos+cTagBegin.length;
  } 
 }while (aPos!=-1);
//alert(matchesStart.join("\n"));

 var matchesEnd = [];
 aPos=0;
 arrayIdx=0;
 do{
  aPos=aLinks.indexOf(cTagEnd,aPos+1);
  if (aPos>-1){
   matchesEnd[arrayIdx++]=aPos;
  } 
 }while (aPos!=-1);

 var matches=[];
 for (var i=0;i<(matchesStart.length>matchesEnd.length?matchesEnd.length:matchesStart.length);i++){
  var aText = aLinks.substring(matchesStart[i], matchesEnd[i]);
  var aLenBefore = matches.length;
  aText.replace(/[^<]*(<a href="([^"]+)" target="">([^<]+)<\/a>)/g, function () {
    matches.push(Array.prototype.slice.call(arguments, 1, 4));
  });
  if (aLenBefore==matches.length){
   matches[aLenBefore]=[];
   matches[aLenBefore][0]=aText;
   matches[aLenBefore][1]="#";
   matches[aLenBefore][2]=aText;
  }
 }
//alert(matches.join("\n"));

 for (var i=0;i<matches.length;i++){
  if((aExcludeContaining=="")||(matches[i][2].indexOf(aExcludeContaining)==-1)){  
//alert(""+matches[i][2]+"|"+aExcludeContaining);  
   if ((matches[i][2].substring(0,2)!="x:")&&(matches[i][2].substring(0,8)!="&nbsp;x:")){
    if ((aStartWith=="")||(matches[i][2].indexOf(aStartWith)==0)){
     matches[i][2] = matches[i][2].substr(aStartWith.length);
     res = res+"<div class='"+(LinkIsActive(matches[i][1])?aClassActive:aClassNotActive)+"'><a href='"+matches[i][1]+"'>"+NbspAsSpace(TrimNbsp(matches[i][2]))+"</a></div>\n";
    }
   }  
  }
 }
 return res;
}

