
function el(e) { return document.getElementById(e); }

function addDocumentToSubscriberFavouriteList(){
  //old
  //NodeUpdateMonitor.addDocumentToFavouriteList(addDocumentToSubscriberFavouriteListStatus);
  //new
   w = 340;
   h = (screen.height) ? screen.height : 700;
   if(h>650) h=700;
   left = window.screenX+200;
   win1=open("mywarefavoritefolders.html","AWMyware","left="+left+",width="+w+",height="+h+",menubar=no,toolbar=no,scrollbars=no,locationbar=no,resizable=yes");    
   if (window.focus) {win1.focus()}
}

/*function addDocumentToSubscriberFavouriteListStatus(status){
  if(status<0){
    alert('ERROR: Adding document to favorite list failed!');
  }else{
    alert('Document was added to favorite list.');
  }
}*/

function doSubscriberFavouriteAction(action){
switch(action)
{
case 0:
      // Display Favorite Documents
      el('dinfo').style.display = 'block';
      el('ainfo').style.display = 'none';
      el('pinfo').style.display = 'none';
      
      el('history_tab').className = 'off';
      el('sdi_tab').className = 'off';
      el('personal_info_tab').className = 'off';
      el('favorites_tab').className = 'on';      
      
      oTextbox = new TreeControl(document.getElementById('wyware-folders-whitebox'), new AWNode(),'001');
      //getDocumentsFromSubscriberFavouriteList();
    break;
case 1:
  //execute code block 1
  if(confirm('Do you want to remove all records from favorite  list?')){
     removeDocumentsFromSubscriberFavouriteList(null);
  }
  break;
case 2:
  //execute code block 2
  s = '';
  cb = document.getElementsByTagName("INPUT");
  len= cb.length;
  for(i=0;i<len;i++){
    if(cb[i].type == "checkbox" && cb[i].name.indexOf('SFD_')==0 && cb[i].checked){
      if(s.length>0) s+=',';
       s+=cb[i].name.substring(4);
    }
  }
  if(s.length>0){
     removeDocumentsFromSubscriberFavouriteList(s);
  }
  break;
case 3:
  s = '';
  cb = document.getElementsByTagName("INPUT");
  len= cb.length;
  for(i=0;i<len;i++){
    if(cb[i].type == "checkbox" && cb[i].name.indexOf('SFD_')==0 && cb[i].checked){
      if(s.length>0) s+=',';
       s+=cb[i].name.substring(4);
    }
  }
  searchDocumentsFromSubscriberFavouriteList(s);
  break;
case 4:
  // Display Personal Info
  el('dinfo').style.display = 'none';
  el('ainfo').style.display = 'none';
  el('pinfo').style.display = 'block';
  
  el('history_tab').className 	= 'off';
  el('sdi_tab').className 	= 'off';
  el('personal_info_tab').className 	= 'on';
  el('favorites_tab').className 	= 'off';    
  
  break; 
default:
  //code to be executed if n is different from case 1 and 2
}
}


function searchDocumentsFromSubscriberFavouriteList(val){
  NodeUpdateMonitor.searchFavouriteDocuments(val,searchDocumentsFromSubscriberFavouriteListStatus);
}

function searchDocumentsFromSubscriberFavouriteListStatus(){
  // redirect to result page
  document.location ='/testweb/main.jsp?flag=result&smd=1&page=1';
}

function removeDocumentsFromSubscriberFavouriteList(val){
   NodeUpdateMonitor.removeDocumentFromFavouriteList(val,getDocumentsFromSubscriberFavouriteList);
}

function getDocumentsFromSubscriberFavouriteList(){
   NodeUpdateMonitor.getDocumentFavouriteList(dsplaySubscriberFavouriteListSet);
}

function dsplaySubscriberFavouriteListSet(result){
  clearResult();
  line = '';
  if(!result || result.length == 0){
    line='<div class="subscribernote">Your favorite list is empty.</div>';
    el('dmenu').style.display = 'none';
  }else{
   el('dmenu').style.display = 'block';
   line='<table width="100%" border="0" cellSpacing=1 cellPadding=1 id="results">';
   if(result.length>0) {
        // write header
     line+='<tr><th scope="col"  align="left" class="results-header-column">#</th>';
     for (var j=0; j < result[0].length; j++) {
       line+='<th scope="col"  align="left" class="results-header-column">';
       if(j==1){
         line+='<span class="normalitalic">Select All</span>';
       }else{
         if(j==0){
           line+='<input type=checkbox name=toggle onclick="ToggleSFD(this)">';
         }else{
           line+=result[0][j];
         }
       }
       line+='</th>';   
     } 
     line+="</tr>";
   }
   for (var i=1; i < result.length; i++) {
    rstyle = 'oddrow';
    if(i%2==0)
      rstyle = 'evenrow';
    //line='<div class="'+rstyle+'">';  
    line+='<tr valign="top" class="resultbg"><td align="left"><span class="metadatatext">'+i+'.</td>';
    for (var j=0; j < result[i].length; j++) {
    
      if(j==1){
        line+='<td align="left"><span class="subhead">';
      }else{
        line+='<td align="left"><span class="metadatatext">';
      }
      if(j==0){
        line+='<input type="checkbox" name="SFD_'+result[i][j]+'" value="selected">';
      }else{
           if(result[i][j].length==0)
             line+='&nbsp;';
      
        line+= result[i][j];
      }
      line+='</span></td>';    
    }
    line+="</tr>";
    //line+='</div>'
    //writeResult(line);
   }
   line+='</table>'; 
  }
  writeResult(line);
}

function clearResult(){
  outResultText = '';
  var evalOut = el('dbody');
  evalOut.innerHTML = outResultText;
  evalOut.scrollTop = evalOut.scrollHeight;
}


function writeResult(output) {
  var evalOut = el('dbody');
  if (output == null) {
    output = 'null';
  } else if (typeof output == 'object') {
    try {
      output = '[object]  type=' + output.getType();
    } catch (e) {
      output = '[object]';
    }
  } else if (output == undefined) {
    output = 'undefined';
  } else {
    output = '' + output;
  }

  //outResultText += '<div style="border-bottom:1px solid #B2B3B5;">' +
  //    output.replace(/\n/g, '<br>\n') + '</div>';
  
  outResultText += output.replace(/\n/g, '<br>\n');
  evalOut.innerHTML = outResultText + '<br><br>';
  evalOut.scrollTop = evalOut.scrollHeight;
}

function ToggleSFD(val){
  var cb = document.getElementsByTagName("INPUT");
  len= cb.length;
  if(val.checked){
   for(i=0;i<len;i++){
    if(cb[i].type == "checkbox" && cb[i].name.indexOf('SFD_')==0){
       cb[i].checked = true;
    }
   }
  }else{
   for(i=0;i<len;i++){
    if(cb[i].type == "checkbox" && cb[i].name.indexOf('SFD_')==0){
       cb[i].checked = false;
    }
   }
  }
}


function renameMyWareFavoriteFolder(){
  if(isBranchFocus()){
    helement = el(searchNodeId);
    if(helement){
      helement = el('branch_'+searchNodeId);
      ename=helement.innerHTML.substring(helement.innerHTML.indexOf('>')+1);
      helement.innerHTML = '<img class="browse-image_nav" src="./img/node/minus.gif" id="I'+
      searchNodeId+'" name="'+ename+searchNodeId+'"><input type="text" id="new_child_value'+searchNodeId+
      '" onblur="set_rename_folder()" onkeypress="if(window.event && window.event.keyCode==13){this.blur();}" value="'+ename+'">';
      el('new_child_value'+searchNodeId).focus();
    }else{
       helement = el('A-'+searchNodeId);
       ename=helement.innerHTML.substring(helement.innerHTML.indexOf('>')+1);
       eparent = helement.parentNode;
       ehtml = eparent.innerHTML;
       i1= ehtml.toLowerCase().indexOf('<a href="#" id="a-'+searchNodeId+'"');
       if(i1<0)
         i1= ehtml.toLowerCase().indexOf('<a id=a-'+searchNodeId+' ');
       i2= ehtml.toLowerCase().indexOf('</a>',i1+1)+4;    
       eparent.innerHTML = ehtml.substring(0,i1)+'<img class="browse-image_nav" src="img/node/doc.gif" border="0"><input type="text" id="new_child_value'+searchNodeId+
      '" onblur="set_rename_folder()" onkeypress="if(window.event && window.event.keyCode==13){this.blur();}" value="'+ename+'">'+ehtml.substring(i2);
       
       el('new_child_value'+searchNodeId).focus();
       
    }
  }
}
function set_rename_folder(){
    var npid = searchNodeId;
    helement = el('new_child_value'+searchNodeId);
    if(npid!=null && npid.indexOf('_')>-1){
       npid = npid.substring(npid.indexOf('_')+1);
    }    
    NodeUpdateMonitor.setMyWareFavoriteFolderName(npid,el('new_child_value'+searchNodeId).value,function(node){
    helement = el(searchNodeId);
    if(helement){
      helement = el('branch_'+searchNodeId);
      helement.innerHTML = '<img class="browse-image_nav" src="./img/node/minus.gif" id="I'+
      searchNodeId+'" name="'+ename+searchNodeId+'">'+el('new_child_value'+searchNodeId).value;
    }else{
      helement = el('new_child_value'+searchNodeId);
      ename = helement.value;
      eparent = helement.parentNode;
      ehtml = eparent.innerHTML;
      i1= ehtml.toLowerCase().indexOf('<img class="browse-image_nav" src="img/node/doc.gif" border="0"><input');
      if(i1<0){
        i1 = ehtml.substring(0,ehtml.toLowerCase().indexOf('<input')).toLowerCase().lastIndexOf('<img');
      }
      i2= ehtml.toLowerCase().indexOf('<br>',i1+1)+4;           
      eparent.innerHTML = ehtml.substring(0,i1)+'<a href="#" id="A-'+searchNodeId+'" class="aleaf" onclick="showDocument(\''+
      searchNodeId+'\',\''+ename+'\',1)"><img class="browse-image_nav" src="img/node/doc.gif" border="0">'+
      ename+'</a><br>'+ehtml.substring(i2);
    }
    });
}

function addNewMyWareFavoriteFolder(){
  if(!isBranchFocus()){
     // add root folder
     el('myware-folders-body').innerHTML=el('myware-folders-body').innerHTML+'<span style="display: block;" class="leaf">'+
     '<input type="text" id="new_root_element_value" onblur="set_new_root_name()" onkeypress="if(window.event && window.event.keyCode==13){this.blur();}" value="New Root Folder"></span>';
  }else{
	  helement = el(searchNodeId);
	  if(helement){
	     helement.innerHTML=helement.innerHTML+'<span style="display: block;" class="leaf" id="new_child_'+searchNodeId+
	     '"><input type="text" id="new_child_value'+searchNodeId+'" onblur="set_new_name()" onkeypress="if(window.event && window.event.keyCode==13){this.blur();}" value="New Folder"></span>';
	  }else{
	     var npid = searchNodeId;
	     if(npid!=null && npid.indexOf('_')>-1){
		 npid = npid.substring(npid.indexOf('_')+1);
	     }  
	     helement = el('A-'+searchNodeId);
	     ename=helement.innerHTML.substring(helement.innerHTML.indexOf('>')+1);
	     ehtml = helement.parentNode.innerHTML;
	     i1= ehtml.toLowerCase().indexOf('<a href="#" id="a-'+searchNodeId+'"');
	     if(i1<0)
	       i1= ehtml.toLowerCase().indexOf('<a id=a-'+searchNodeId+' ');
	     i2= ehtml.toLowerCase().indexOf('<br>',i1+1)+4;
	     ehtml = ehtml.substring(0,i1)+
	     '<span class="branch_focus_on" onclick="showBranch(\'branch_'+npid+'\');" id="branch_branch_'+npid+'">'+
	     '<img class="browse-image_nav" src="img/node/minus.gif" id="Ibranch_'+npid+
	     '" name="'+ename+'_'+npid+'">'+ename+'</span><span class="leaf" style="display: block;" id="branch_'+npid+'">'+
	     '<span style="display: block;" class="leaf" id="new_child_'+searchNodeId+
	     '"><input type="text" id="new_child_value'+searchNodeId+'" onblur="set_new_name()" onkeypress="if(window.event && window.event.keyCode==13){this.blur();}" value="New Folder"></span>'+
	     '</span>'+
	     ehtml.substring(i2);
	     helement.parentNode.innerHTML = ehtml;
	  }
	  el('new_child_value'+searchNodeId).focus(); 
  }
}

function set_new_root_name(){
  helement = el('new_root_element_value');
  if(helement && helement.value.length>0){
	  NodeUpdateMonitor.addMyWareFavoriteRootFolder(helement.value,function(node){
	    if(node>-1){
	      helement = el('new_root_element_value');
	      evalue = helement.value;
	      eparent = helement.parentNode;
	      eparent.removeChild(helement);
	      eparent.parentNode.removeChild(eparent);
	      el('myware-folders-body').innerHTML=el('myware-folders-body').innerHTML+
              '<span class="branch_focus_on" onclick="showBranch(\'node_'+node+'\');" id="branch_node_'+
              node+'"><img class="browse-image_nav" src="./img/node/minus.gif" id="Inode_'+
              node+'" name="Default_node_'+
              node+'1">'+evalue+'</span><span style="display: block;" class="leaf" id="node_'+
              node+'"></span>';
              searchNodeId = 'node_'+node;
	    }else{
		 alert('Wrong Folder Name!');
		 helement = el('new_root_element_value');
		 helement.focus();
	    }
	  });
  }else{
        helement.parentNode.removeChild(helement);     
  }
}

function set_new_name(){
 var npid = searchNodeId;
 helement = el('new_child_value'+searchNodeId);
 if(npid!=null && npid.indexOf('_')>-1){
       npid = npid.substring(npid.indexOf('_')+1);
 }
 if(helement && helement.value.length>0){
   NodeUpdateMonitor.addMyWareFavoriteChildFolder(npid,helement.value,function(node){
      if(node>-1){
        evalue = el('new_child_value'+searchNodeId).value;
        helement = el('new_child_'+searchNodeId);
        helement.parentNode.removeChild(helement);
        helement = el(searchNodeId);
        if(!helement){
          helement = el('branch_'+searchNodeId.substring(searchNodeId.indexOf('_')+1));
        }
        ehtml=helement.innerHTML;
        helement.innerHTML=ehtml+'<a href="#" id="A-leaf_'+node+'" class="aleaf" onclick="showDocument(\'leaf_'+
        node+'\',\''+evalue+'\',1)"><img class="browse-image_nav" src="img/node/doc.gif" border="0">'+
        evalue+'</a><br>';
        if(searchNodeId.indexOf('leaf_')==0)
          searchNodeId='branch_'+searchNodeId.substring(5);
      }else{
         alert('Wrong Folder Name!');
         helement = el('new_child_value'+searchNodeId);
         helement.focus();
      }
   });
  }else{
        evalue = el('new_child_value'+searchNodeId).value;
        helement = el('new_child_'+searchNodeId);
        helement.parentNode.removeChild(helement);     
  }
}

function deleteMyWareFavoriteFolder(){
 var npid = searchNodeId;
 if(npid!=null && npid.indexOf('_')>-1){
       npid = npid.substring(npid.indexOf('_')+1);
 }
 helement = el('branch_'+searchNodeId);
 if(!helement)
    helement = el('A-'+searchNodeId); 
 eclassname = helement.className;
 if(confirm('Do you really want to delete selected folder?')){
 if(eclassname.indexOf('_focus_on')>0){
       NodeUpdateMonitor.deleteMyWareFavoriteFolder(npid,function(){
	   helement = el('A-'+searchNodeId);
	   if(helement){
	     eparent = helement.parentNode;
	     ehtml = eparent.innerHTML;
	     i1 = ehtml.toLowerCase().indexOf('<a href="#" id="'+'a-'+searchNodeId+'"');
	     if(i1==-1)
	        i1 = ehtml.toLowerCase().indexOf('<a id='+'a-'+searchNodeId+' ');
	     i2 = ehtml.toLowerCase().indexOf('</a><br>',i1+1);
	     ehtml = ehtml.substring(0,i1)+ehtml.substring(i2+8);
	     eparent.innerHTML = ehtml;
	   }else{
	      helement = el(searchNodeId);
	      if(helement){
		pelement = helement.parentNode;
		pelement.removeChild(helement);
		helement = el('branch_'+searchNodeId); 
		pelement.removeChild(helement);
	      }
	   }
       });
 }else{
    alert("You have to select folder first!");
 }
 }
}

function showMyWareDocumentsListdetails(){
 el('top-details-nav').style.display='none';
 el('myware-documents-details-body').style.display='none';
 el('myware-documents-body').style.display='block';
 el('dmenu').style.display='block';
}

function details(did,lib){
  currentitem = did+'-'+lib;

  url="lb_document_id="+did+"&awlibraries="+lib;
  
  nav = getNextDetailsId(did,lib);
  next     = nav[0];
  next_lib = nav[1];
  
  nav = getPrevDetailsId(did,lib);
  prev     = nav[0];
  prev_lib = nav[1];
  
  nvmenu = '';
  if(prev==-1 && next==-1){
    nvmenu = '';
  }else{
  if(prev!=-1){
    nvmenu+='<a href="#" onClick="details('+prev+',\''+prev_lib+'\');">&lt; Prev</a>';  
  }else{
    nvmenu+='<span class="hidemenu">&lt; Prev</span>';
  }

  if(next!=-1){
    nvmenu+='&nbsp;&nbsp;<a href="#" onClick="details('+next+',\''+next_lib+'\');">Next &gt;</a>';  
  }else{
    nvmenu+='&nbsp;&nbsp;<span class="hidemenu">Next &gt</span>';
  }
  }
  nvmenu+='&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" onClick="showMyWareDocumentsListdetails();">Back to documents list</a>';  
  el('myware-documents-body').style.display='none';
  el('dmenu').style.display='none';
  el('top-details-nav').style.display='block';
  el('myware-documents-details-body').innerHTML='';
  el('myware-documents-details-body').style.display='block';
  el('top-details-nav').innerHTML=nvmenu;
  el('myware-documents-details-body').innerHTML='<table cellspacing="0" cellpadding="0" width="100%" height="400"><tr><td valign="center" align="center"><img src="./img/graph/ajax-loader.gif" border="0"></td></tr></table>';
  NodeUpdateMonitor.getMyWareFavoriteDocumentDetails(url,0,dsplayMetadata);
}

function getNextDetailsId(did,lib){
  txt  = el('myware-documents-body').innerHTML;
  ind  = txt.indexOf("details('"+did+"','"+lib);
  ind  = txt.indexOf("details('"+did+"',",ind+1);
  ind2 = txt.indexOf("details('"+did+"',",ind+1);
  if(ind2>-1) ind=ind2;
  ind = txt.indexOf("details(",ind+1);
  if(ind>-1){
    return [txt.substring(ind+8,txt.indexOf(',',ind)),
    txt.substring(txt.indexOf(',',ind)+2,txt.indexOf(')',ind)-1)];
  }
  return [-1,''];
}

function getPrevDetailsId(did,lib){
  txt = el('myware-documents-body').innerHTML;
  ind = txt.indexOf("details('"+did+"','"+lib);
  ind = txt.substring(0,ind-1).lastIndexOf("details(",ind+1);
  if(ind>-1){
    return [txt.substring(ind+8,txt.indexOf(',',ind)),
    txt.substring(txt.indexOf(',',ind)+2,txt.indexOf(')',ind)-1)];
  }
  return [-1,''];
}

function dsplayMetadata(result){
  line = '';
  koha_link = '';
  did = '';
  awserverip='10.1.1.17:8080';
  if(result.length>1) {
    line ='<table border="0" cellSpacing="1" cellPadding="1" width="100%">';
    line+='<tr><td colspan="2" class="result-subtitle">'+result[1][1]+'</td></tr>';
    // show back to search
    /*if(el('search-result-list-body').innerHTML.length>0)
      el('top-details-nav-back').style.display  = 'block';
    else
      el('top-details-nav-back').style.display  = 'none';*/
      
    //line+='<tr><td colspan="2"><a href="#" onClick="showSearchResult();" class="menu">Back to search results</a></td></tr>';
    line+='<tr><td colspan="2" class="metadatatopheader">Metadata</td></tr>';
    for (var j=3; j < result[0].length; j++) {
       if(result[1][j].length>0){
         if(result[0][j]=='Link to Koha'){
           koha_link = result[1][j];
           ind = koha_link.indexOf("http");
           koha_link = koha_link.substring(ind,koha_link.indexOf("'",ind+1));
           //koha_link = koha_link.substring(0,koha_link.indexOf('External Link'))+'Show Item Status</a>'; 
           koha_link = '<a href="#" onclick="showKohaRecord(\''+koha_link+'\')" >Show Item Status</a>';
         }else{
           line+='<tr ><th scope="row"  align="left" valign="top" class="metadataheader" width="150">'+result[0][j]+':</th>';
           line+='<td   align="left">'+result[1][j]+'</td></tr>';
           if(result[0][j] == 'Document Id')
             did = result[1][j];
         }
       }
     }  
     if(result[1][0].length>0 || koha_link.length>0){
       line+='<tr><td colspan="2">&nbsp;</td></tr>';
       line+='<tr><td colspan="2" class="metadatatopheader">Links</td></tr>';
       if(result[1][0].length>0){
         line+='<tr ><th scope="row"  align="left" valign="top" class="metadataheader">Document File:</th>';
         line+='<td   align="left"><a href="#" onClick="popupWindow(\''+result[1][0].replace(/'/g,"\\'")+'\');">View File</a>';
         if(result[1][2].length>0){
           if(result[1][0].indexOf('.pdf')>-1 || result[1][0].indexOf('.PDF')>-1){
              line+='&nbsp;&nbsp;|&nbsp;&nbsp;<a href="#" onClick="popupWindow(\''+result[1][2].replace(/'/g,"\\'")+'\');">HTML</a>';
           }else{
              line+='&nbsp;&nbsp;|&nbsp;&nbsp;<a href="#" onClick="popupWindow(\''+result[1][2].replace('.html','.txt').replace(/'/g,"\\'")+'\');">TEXT</a>';
           }
         }
         line+='</td></tr>';
       }
       if(koha_link.length>0){
         line+='<tr ><th scope="row"  align="left" valign="top" class="metadataheader">Link:</th>';
         line+='<td   align="left"><div id="sharethis"><ul><li class="awfind">'+koha_link+'</li></ul></div></td></tr>';
       }
     }
     if(currentitem.length>0){
       // Permalink
       /*
       line+='<tr ><th scope="row"  align="left" valign="top" class="metadataheader">Permalink:</th>';
       line+='<td   align="left">'+
             '<a href="http://'+awserverip+'/awweb/search/index.html?item='+
             currentitem+'">Link to this record</a></td></tr>';       
       */      
       // share
       /*line+='<tr ><td colspan="2">&nbsp;</td></tr>';
       
       line+='<tr ><th scope="row"  align="left" valign="top" class="metadataheader">Share this:</th>';
       line+='<td   align="left">'+
             '<div id="sharethis"><ul><li class="delicious">'+
             '<a href="http://del.icio.us/post" onclick="window.open(\'http://del.icio.us/post?v=4&amp;noui&amp;jump=close&amp;url=http://'+awserverip+'/awweb/search/index.html?item='+
             currentitem+'&amp;title=Digital+Collection+-+'+result[1][1].replace(/'/g,"\\'")+'\', \'delicious\',\'toolbar=no,width=700,height=400\'); return false;">Add to del.icio.us</a>'+
             '</li></ul></div>'+
             '</td></tr>';
       */      
       // email, print
       line+='<tr ><td colspan="2" valign="top"><hr class="black-1-cccccc" /></td></tr>';
       /*line+='<tr ><td colspan="2" valign="center">'+'<div id="top-details-nav-icon-print" class="lefttext"><ul><li class="awprint"><a href="#" onClick="javascript:window.print();">Print</a></li></ul></div>';
       line+='<div class="lefttext">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>';
       line+='<div id="top-details-nav-icon-email" class="lefttext"><ul><li class="awemail"><a href="mailto:?subject=AW Digital Library:%20'+result[1][1].replace(/'/g,"\\'")+
             '&amp;body=This%20URL%20points%20to%20a%20page%20on%20AW%20Digital%20Library%20that%20may%20interest%20you:%20'+'http://'+awserverip+'/awweb/search/index.html?item='+
             currentitem+'">Email</a></li></ul></div>';
       
       if(navigator.userAgent.toLowerCase().indexOf('chrome') < 0){
        line+='<div class="lefttext">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>';
        line+='<div id="top-details-nav-icon-bookmark" class="lefttext"><ul><li class="bookmark">'+
             '<a href="#" onclick="javascript:bookmark_us(\'http://'+awserverip+'/awweb/search/index.html?item='+
             currentitem+'\',\''+result[1][1].replace(/'/g,"\\'")+'\');">Add to favourite list</a>'+
             '</li></ul></div>';
       }       
       line+='</td></tr>';*/
       line+='<tr ><td colspan="2">&nbsp;</td></tr>';
     }
     line+='</table>';  
  }
  el('myware-documents-details-body').innerHTML = line;
}

function popupWindow(url){
      w = (screen.width) ? screen.width : 700;
      h = (screen.height) ? screen.height : 700;
      w =60*(w/100)
      //if(w>100) w = w-100;
      if(h>150) h= h-150;
  
      win1=open(url,"AWare","width="+w+",height="+h+",menubar=no,toolbar=no,scrollbars=yes,locationbar=no,resizable=yes");    
      if (window.focus) {win1.focus()}
    return false;
}


isfolder = true;
function moveSubscriberFavouriteFolder(isMoveFolder){
  isfolder = isMoveFolder;
  if(searchNodeId){
      helement = el('branch_'+searchNodeId);
      if(!helement)
        helement = el('A-'+searchNodeId); 
      eclassname = helement.className;
      if(eclassname.indexOf('_focus_on')>0){
         w = 400;
         h = (screen.height) ? screen.height : 700;
         if(h>650) h=700;
         left = window.screenX+300;
         win1=open("mywarefavoritefolders.html","AWMyware","left="+left+",width="+w+",height="+h+",menubar=no,toolbar=no,scrollbars=no,locationbar=no,resizable=yes");    
         if (window.focus) {win1.focus()}
      }else{
        alert("You have to select folder first!");
      }
  }else{
    alert("You have to select folder first!");
  }
}

var parentNodeID;
function doMoveSubscriberFavouriteFolder(to){
  parentNodeID  = to;
  toId   = to.substring(to.indexOf('_')+1);
  if(searchNodeId && (el(searchNodeId) || el('A-'+searchNodeId))){
       if(isfolder){
	  // move folder
	  fromId = searchNodeId.substring(searchNodeId.indexOf('_')+1);
	  NodeUpdateMonitor.moveMyWareFavoriteFolder(fromId,toId,function(node){
	     if(node == -1){
		alert("Wrong parent folder. Failed to move selected folder!");
	     }else{
	       //remove node from
	       var helement       = el(searchNodeId);
	       var helementbranch = el('branch_'+searchNodeId);
	       if(helement){
		 // add to new parent
		 var leafparent  = helement.parentNode;
		 pelement = el(parentNodeID); 
		 npid     = searchNodeId.substring(searchNodeId.indexOf('_')+1);
		 if(pelement){
		     ehtml = pelement.innerHTML+
		    '<span class="branch" onclick="showBranch(\'branch_'+npid+'\');" id="branch_branch_'+npid+'">'+
		     helementbranch.innerHTML+'</span><span class="leaf" style="display: block;" id="branch_'+npid+'">'+
		     helement.innerHTML+'</span>';
		     parentehtml   = ehtml;
		     currentelementid = pelement.id;
		     deleteDHTMLFolder();
		     el(currentelementid).innerHTML = parentehtml;
		     convertBranchToLeaf(leafparent.id);
		     if(searchNodeId.indexOf('node_')==0){
		       convertBranchToLeaf(el('branch_'+searchNodeId.substring(5)).id);
		     }
		       
		 }else{
		     pelement = el('A-'+parentNodeID);
		     if(pelement){
			     npid     = parentNodeID.substring(parentNodeID.indexOf('_')+1);
			     npidchild= searchNodeId.substring(searchNodeId.indexOf('_')+1);
			     ename=pelement.innerHTML.substring(pelement.innerHTML.indexOf('>')+1);
			     ehtml = pelement.parentNode.innerHTML;
                             i1= ehtml.toLowerCase().indexOf('<span class="branch_focus_on" onclick="showBranch(\''+searchNodeId+'\');"');
                             if(i1<0)
                               i1= ehtml.toLowerCase().indexOf('<span class="branch" onclick="showBranch(\''+searchNodeId+'\');"');
                             if(i1<0){
                               i1= ehtml.toLowerCase().indexOf('<span id=branch_'+searchNodeId);
                             }
	                     isCallDelete = true;
	                     if(i1>0){
	                        i2= ehtml.toLowerCase().indexOf('</span>',i1+1);
	  	                i2=ehtml.toLowerCase().indexOf('</span>',i2+1)+7;		     
	                        ehtml = ehtml.substring(0,i1)+ehtml.substring(i2);  
	                        isCallDelete = false;
			     }			     
			     i1= ehtml.toLowerCase().indexOf('<a href="#" id="a-'+parentNodeID+'"');
			     if(i1<0)
			       i1= ehtml.toLowerCase().indexOf('<a id=a-'+parentNodeID+' ');
			     i2= ehtml.toLowerCase().indexOf('<br>',i1+1)+4;
			     ehtml = ehtml.substring(0,i1)+
			     '<span class="branch" onclick="showBranch(\'branch_'+npid+'\');" id="branch_branch_'+npid+'">'+
			     '<img class="browse-image_nav" src="img/node/minus.gif" id="Ibranch_'+npid+
			     '" name="'+ename+'_'+npid+'">'+ename+'</span><span class="leaf" style="display: block;" id="branch_'+npid+'">'+
			     '<span class="branch" onclick="showBranch(\'branch_'+npidchild+'\');" id="branch_branch_'+npidchild+'">'+
				     helementbranch.innerHTML+'</span><span class="leaf" style="display: block;" id="branch_'+npidchild+'">'+
				     helement.innerHTML+'</span>'+	     
			     '</span>'+
			     ehtml.substring(i2);
			     parentehtml   = ehtml;
			     currentelementid = pelement.parentNode.id;
			     //elementobject = pelement.parentNode;
			     if(isCallDelete){
			        deleteDHTMLFolder();
			     }
			     el(currentelementid).innerHTML = parentehtml;
			     convertBranchToLeaf(leafparent.id);
			     if(searchNodeId.indexOf('node_')==0){
			       convertBranchToLeaf(el('branch_'+searchNodeId.substring(5)).id);
			     }			     
		     }else{
		             deleteDHTMLFolder();
		     } 
		 }
	       }else{
		 //move leaf
		 var helement    = el('A-'+searchNodeId);
		 var leafparent  = helement.parentNode;
		 evalue = helement.innerHTML;
		 i1 = evalue.toLowerCase().indexOf('<img');
		 evalue = evalue.substring(evalue.indexOf('>',i1+1)+1);
		 pelement = el(parentNodeID); 
		 npid     = searchNodeId.substring(searchNodeId.indexOf('_')+1);
		 if(pelement){
		     ehtml = pelement.innerHTML+
		     '<a href="#" id="A-leaf_'+npid+'" class="aleaf" onclick="showDocument(\'leaf_'+
				  npid+'\',\''+evalue+'\',1)"><img class="browse-image_nav" src="img/node/doc.gif" border="0">'+
		     evalue+'</a><br>';
		     parentehtml   = ehtml;
		     elementobject = pelement;
		     deleteDHTMLFolder();
		     elementobject.innerHTML = parentehtml;
		     convertBranchToLeaf(leafparent.id);
		  }else{
		     pelement = el('A-'+parentNodeID);
		     if(pelement){
			     npid     = parentNodeID.substring(parentNodeID.indexOf('_')+1);
			     npidchild= searchNodeId.substring(searchNodeId.indexOf('_')+1);
			     ename=pelement.innerHTML.substring(pelement.innerHTML.indexOf('>')+1);
			     ehtml = pelement.parentNode.innerHTML;
            	             i1= ehtml.toLowerCase().indexOf('<a href="#" id="a-'+searchNodeId+'"');
                             if(i1<0)
			       i1= ehtml.toLowerCase().indexOf('<a id=a-'+searchNodeId+' ');            	             
	                     isCallDelete = true;
	                     if(i1>0){
	                        i2= ehtml.toLowerCase().indexOf('<br>',i1+1)+4;			     
	                        ehtml = ehtml.substring(0,i1)+ehtml.substring(i2);  
	                        isCallDelete = false;
			     }
			     i1= ehtml.toLowerCase().indexOf('<a href="#" id="a-'+parentNodeID+'"');
			     if(i1<0)
			       i1= ehtml.toLowerCase().indexOf('<a id=a-'+parentNodeID);
			     i2= ehtml.toLowerCase().indexOf('<br>',i1+1)+4;
			     ehtml = ehtml.substring(0,i1)+
			     '<span class="branch" onclick="showBranch(\'branch_'+npid+'\');" id="branch_branch_'+npid+'">'+
			     '<img class="browse-image_nav" src="img/node/minus.gif" id="Ibranch_'+npid+
			     '" name="'+ename+'_'+npid+'">'+ename+'</span><span class="leaf" style="display: block;" id="branch_'+npid+'">'+
			     '<a href="#" id="A-leaf_'+npidchild+'" class="aleaf" onclick="showDocument(\'leaf_'+
				  npidchild+'\',\''+evalue+'\',1)"><img class="browse-image_nav" src="img/node/doc.gif" border="0">'+
				  evalue+'</a><br></span>'+
			     ehtml.substring(i2);
			     parentehtml   = ehtml;
			     elementobject = pelement.parentNode;
			     if(isCallDelete){
			        deleteDHTMLFolder();
			     }   
			     elementobject.innerHTML = parentehtml;
			     convertBranchToLeaf(leafparent.id);
			     
		     }else{
		             deleteDHTMLFolder();   
		     }
		  }
	       }
	     }
	  });
      }else{
        // move documents
        fdids = getMyWareSelectedFavoriteDocuments();
        if(fdids.length>0){
          var ptnid = parentNodeID;
          if(ptnid!=null && ptnid.indexOf('_')>-1){
            ptnid = ptnid.substring(ptnid.indexOf('_')+1);
          }
          NodeUpdateMonitor.addMyWareFavoriteDocumentsListToFolder(ptnid,fdids,function(){
            deleteMyWareFavoriteDocumentsFromFolder();
           });        
        }
      }
  }else{
    // add document to folder
     NodeUpdateMonitor.addMyWareFavoriteDocumentToFolder(toId,function(){
       //alert("Document was added to favorite list.");
     });
  }
}

function mywareWait(){}

function convertBranchToLeaf(id){
     ebranch = el(id);
     if(ebranch){
       if((ebranch.innerHTML.length==0 || ebranch.innerHTML=='</SPAN>') && ebranch.id.indexOf('node_')<0){
          ehtml = ebranch.parentNode.innerHTML;
	  i1= ehtml.toLowerCase().indexOf('<span class="branch" onclick="showBranch(\''+ebranch.id+'\');"');
	  if(i1<0)
	    i1= ehtml.toLowerCase().indexOf('<span id=branch_'+ebranch.id);
	  i2= ehtml.toLowerCase().indexOf('</span>',i1+1);
	  i3= ehtml.toLowerCase().indexOf('<img ',i1+1);
	  evalue = ehtml.substring(ehtml.indexOf('>',i3+1)+1,ehtml.indexOf('</',ehtml.indexOf('>',i3+1)+1));
	  i2=ehtml.toLowerCase().indexOf('</span>',i2+1)+7;
	  branchid     = ebranch.id.substring(ebranch.id.indexOf('_')+1);
	  ehtml = ehtml.substring(0,i1)+'<a href="#" id="A-leaf_'+branchid+'" class="aleaf" onclick="showDocument(\'leaf_'+
             branchid+'\',\''+evalue+'\',1)"><img class="browse-image_nav" src="img/node/doc.gif" border="0">'+
             evalue+'</a><br>'+ ehtml.substring(i2);
          ebranch.parentNode.innerHTML = ehtml;       
       }
     }  
}

function deleteDHTMLFolder(){
   htmlelement = el('A-'+searchNodeId);
   if(htmlelement){
     eparent = htmlelement.parentNode;
     ehtml = eparent.innerHTML;
     i1 = ehtml.toLowerCase().indexOf('<a href="#" id="'+'a-'+searchNodeId+'"');
     if(i1<0)
       i1= ehtml.toLowerCase().indexOf('<a id=a-'+searchNodeId+' ');     
     i2 = ehtml.toLowerCase().indexOf('</a><br>',i1+1);
     ehtml = ehtml.substring(0,i1)+ehtml.substring(i2+8);
     eparent.innerHTML = ehtml;
   }else{
      htmlelement = el(searchNodeId);
      if(htmlelement){
        branchelement = htmlelement.parentNode;
        branchelement.removeChild(htmlelement);
        htmlelement = el('branch_'+searchNodeId); 
        branchelement.removeChild(htmlelement);
        if(branchelement.innerHTML.length==0 && branchelement.id.indexOf('node_')<0){
          ehtml = branchelement.parentNode.innerHTML;
	  i1= ehtml.toLowerCase().indexOf('<span class="branch" onclick="showBranch(\''+branchelement.id+'\');"');
	  if(i1<0)
	    i1= ehtml.toLowerCase().indexOf('<span id=branch_'+branchelement.id);
	  i2= ehtml.toLowerCase().indexOf('</span>',i1+1);
	  i3= ehtml.toLowerCase().indexOf('<img ',i1+1);
	  evalue = ehtml.substring(ehtml.indexOf('>',i3+1)+1,ehtml.indexOf('</',ehtml.indexOf('>',i3+1)+1));
	  i2=ehtml.toLowerCase().indexOf('</span>',i2+1)+7;
	  nodepid     = branchelement.id.substring(branchelement.id.indexOf('_')+1);
	  ehtml = ehtml.substring(0,i1)+'<a href="#" id="A-leaf_'+nodepid+'" class="aleaf" onclick="showDocument(\'leaf_'+
             nodepid+'\',\''+evalue+'\',1)"><img class="browse-image_nav" src="img/node/doc.gif" border="0">'+
             evalue+'</a><br>'+ ehtml.substring(i2);
          branchelement.parentNode.innerHTML = ehtml;  
        }
      }
   }
}

function doPage(page){
  showDocument(searchNodeId,'',page);
}

function doFavoriteDocumentsSort(v){
  alert("Select Sort Order from Preferences!");
}

function getMyWareSelectedFavoriteDocuments(){
  dids='';
  var cb = document.getElementsByTagName("INPUT");
  len= cb.length;
  for(i=0;i<len;i++){
    if(cb[i].type == "checkbox" && cb[i].id.indexOf('mywaredocument_')==0 && cb[i].checked){
       if(dids.length>0) dids+=",";
       dids+=cb[i].id.substring(15);
    }
  }
  return dids;
}

function deleteMyWareFavoriteDocumentsFromFolder(){
 dids = getMyWareSelectedFavoriteDocuments();
 if(dids.length>0){
	 var npid = searchNodeId;
	 if(npid!=null && npid.indexOf('_')>-1){
	       npid = npid.substring(npid.indexOf('_')+1);
	 }  
         if(dids.length>0 && npid!=null){
	      helement = el('branch_'+searchNodeId);
	      if(!helement)
		helement = el('A-'+searchNodeId); 
	      eclassname = helement.className;
	      if(eclassname.indexOf('_focus_on')>0){
		 NodeUpdateMonitor.removeMyWareFavoriteDocumentFromFolder(npid,dids,function(){
		    showDocument(searchNodeId,'',1);
		 });
	      }
	  }
}else{
  alert("You should select documents first!");
}
}
