

 var units_loaded=[];  // Total Loaded Units for a page
 var debug=false;      // Debugging mode flag
   
 
 //Function to create Units object and show the data in an expanded region
 function ShowUnits(ob,fundCode,univCode,ctrlId,tableName)
 {
      var unitsObj=null;
      unitsObj=new Units();
      unitsObj.ShowUnits(ob,fundCode,univCode,ctrlId,tableName);
      delete unitsObj;
 }

 //Units Object Constructor 
 function Units()
 {
   this.objClick=null;
   this.rank=new String();
 }
  
 //Function to Show the Non-Super driver units in the Expanded area
 Units.prototype.ShowUnits=function (ob,fundCode,univCode,ctrlId,tableName)
 {
      try
      {
    
        var linkNode=document.getElementById('Id_'+fundCode);
        
        if(linkNode&&linkNode.parentNode)
        {
            this.objClick=linkNode.parentNode;
            
            if(typeof univCode =='undefined')
            {
                univCode='E';
            }
            
            if(typeof ctrlId =='undefined')
            {
                ctrlId='CustomisedTable1';
            }
            
            if(typeof tableName == 'undefined')
            {
                tableName='default';
            }
          
            loaded=false;
        
         
            this.rank=this.objClick.previousSibling.firstChild.nodeValue;
            
            
            this.rank=this.rank.trim();
            
            for(var i=0;i<units_loaded.length;i++)
            {
               if(this.rank==units_loaded[i])
               {
               
                 loaded=true;
               } 
            }
            
            if(!loaded)
            {
                
                this.doCall(fundCode,univCode,ctrlId,tableName);
                HighLightSelectedRows();
            }
            else
            {
                this.HideUnits(this.rank);
            }
           
          }  
        
        }
        catch(e)
        {   
            if(debug)
            {
                  alert(e+" [at ShowUnits]");
            }
            
        }  
    }
    

    //Function to hide the Non-Super driver units in the Expanded area
    Units.prototype.HideUnits=function(rank)
    {
      try
      {  
          var node=this.objClick.parentNode.nextSibling;
         
          
          do
          {  
               if(node.className.indexOf('sub')>-1)
               {
                        
                    if(node.style.display=='')
                    {
                           
                        node.style.display='none';
                     }
                     else
                     {
                        node.style.display='';
                     }   
                        
                }
                else
                {
                    break;
                }
            
            
            
          } while(node=node.nextSibling);
      
            var txt=this.objClick.firstChild.innerHTML;
                
            if(txt.trim()=="[+]")
            {
               this.objClick.firstChild.innerHTML="[-]";
               this.objClick.firstChild.title="Collapse Units";
            }
            else
            {
               this.objClick.firstChild.innerHTML="[+]";
               this.objClick.firstChild.title="Expand Units";
            }
          
      }
      catch(e)
      {
        
        if(debug)
        {
            alert(e+" [at HideUnits]");
        }
        
      }
      
    }

    //Function to load the Non-Super driver units data and build the HTML DOM
    Units.prototype.doCall=function(fundCode,univCode,ctrlId,tableName) 
    {
        try
        {
            var fileName=location.pathname;
            
            var file=fileName.substr(fileName.lastIndexOf('/')+1,(fileName.lastIndexOf('.')-fileName.lastIndexOf('/')-1));
           
              
	        var p = TrustnetX.Controls.CustomisedTable.GetUnitRows(fundCode,univCode,file,ctrlId,tableName);
    	   
	        p=p.value;
        
     	    if(typeof p =="string")
     	    {
     	      
     	      var t=p.split('</tr>');
     	      
     	      var beforeNode=this.objClick.parentNode;
     	      
     	      for(var index=0;index<t.length-1;index++)
     	      {
     	        var roman=toRoman(index+1);
     	        
     	        t[index]=t[index].replace(/tkn:Rank;/,' ');
     	        t[index]=t[index].replaceAll("tkn:UniverseCode;",univCode);
     	        t[index]=t[index].replaceAll("&amp;",'&');
     	        
     	        var node=writeCode(t[index]+"</tr>");
         	        
         	         
     	        node.className="sub"+this.objClick.parentNode.className.replaceAll(" selected","");
                
                
                this.objClick.parentNode.parentNode.insertBefore(node, beforeNode.nextSibling);
                beforeNode=node;
               }
 
               if(t.length>1)
               {
                   units_loaded[units_loaded.length]=this.rank;
                   this.rank=''; 
                   
                    
	               var txt=this.objClick.firstChild.innerHTML;
    	                
	                if(txt.trim()=="[+]")
	                {
	                   this.objClick.firstChild.innerHTML="[-]";
    	               this.objClick.firstChild.title="Collapse Units";
	                }
	                else
	                {
	                   this.objClick.firstChild.innerHTML="[+]";
	                   this.objClick.firstChild.title="Expand Units";
	                }  
               
            
	            }  
	            else
	            {
	             
	                 this.objClick.firstChild.innerHTML="";
    	        
               
	            } 
	           
     	    }
     	    

	        p = null;
	    }
	    catch(e)
	    {   
	        if(debug)
            {
                alert(e+" [at doCall]");
            }
            
	    }
	    
    }

    //Function to parse and process the HTML string 
    //Based on:http://www.oreillynet.com/pub/a/javascript/2001/04/13/essential_js.html
    function writeCode(s, pointer) 
    {
        var parent, tag, j;
        
        try
        {
        
            for ( var i = 0; i < s.length; i++) 
            {
	            var c = s.charAt(i);
	            if (c == "<") 
	            {
	                    var j = s.indexOf(">", i + 1);
	                    tag = s.substring(i, j + 1);
	                    if (tag.charAt(tag.length - 2) == "/") 
	                    {
	                        if (pointer != null && pointer.nodeType == 3) 
	                        {
	                            pointer = pointer.parentNode;
	                        }
    	   
	                        pointer.appendChild(createElementFromString(tag));
	                    } 
	                    else if (tag.charAt(1) != "/") 
	                    {
	                        if (pointer != null && pointer.nodeType == 3&&pointer.parentNode!=null) 
	                        {
	                            pointer = pointer.parentNode;
	                        }
        	                
	                        if(pointer==null)
	                        {   
	                            pointer=createElementFromString(tag);
	                            parent=pointer;
	                        }
	                        else
	                        {   
	                            pointer = pointer.appendChild(createElementFromString(tag));
        	                    
	                        }
	                    } 
	                    else 
	                    {   
	                        if(tag=="</td>")
	                        pointer=parent;
	                        
	                        if (pointer.parentNode != null && pointer.parentNode.parentNode != null&&pointer.parentNode.parentNode.nodeType!=11) 
	                        {
	                            pointer = pointer.parentNode.parentNode;
	                             
	                        }
	                        
	                        
	                    }
	                        i = j;
	            } 
	            else 
	            {
	                if (pointer.nodeType == 3) 
	                {
	                    n = s.indexOf("<", i + 1);
	                    if (n == -1) 
	                    {
	                        pointer.nodeValue += s.substr(i);
	                        i = s.length;
	                    } 
	                    else 
	                    {
	                        pointer.nodeValue += s.substring(i, n);
	                        i = n - 1;
	                    }
    	                
	                    var txt=pointer.nodeValue;
	                    if(txt.trim()=="[+]")
	                    {
	                      pointer.nodeValue=" ";
	                      }
    	                
	                    
	                } 
	                else if(pointer.tagName!="TR")
	                {   
    	                
	                    pointer = pointer.appendChild(document.createTextNode(c));
    	               
	                }
	            }
	        }
	    }
	    catch(e)
	    {
	        if(debug)
            {
                alert(e+" [at writeCode]");
            }
	    }
	    
	    
	    return parent;
	}


     //Function to build HTML DOM Nodes with the HTML string 
     //Based on:http://www.oreillynet.com/pub/a/javascript/2001/04/13/essential_js.html
   	 function createElementFromString(str) 
   	 {
   	 
   	    //to be changed[Temporary Fix]
	    if(str.indexOf('ShowUnits(')>-1)return document.createTextNode(" ");
	    
	    var tempNode;
	      
	    
	    try
	    {
	    
	        var a = str.match(/<(\w+)(\s+)?([^>]+)?>/);
    	    
	        if (a != null) 
	        {
    	        
	            tempNode = document.createElement(a[1]);
    	      
	            if (a[3] != null) 
	            {
	                var attrs = a[3].split('" ');
	                if (attrs!=null&&attrs.length > 0) 
	                {
	                    for ( var i = 0; i < attrs.length; i++) 
	                    {
	                        var att = attrs[i].split("=");
    	                    
	                        if (att!=null&&att.length>1&&att[0].length > 0 &&  att[0] != "/" && att[1].length != 2) 
	                        {
	                            var a_n = document.createAttribute(att[0]);
	                            
	                            var temp="";
	                            for(var v=1;v<att.length;v++)
	                            {
	                              temp+=att[v];
    	                          
	                              if(v<att.length-1)
	                              { temp+="=";
	                              }
	                            }
    	                        
	                            var reg=/^["'](.+)["']$/;
    	                          
	                            var attr_val=temp.replace(reg, "$1");
	                            a_n.value=attr_val.replaceAll('"','');
	                            
	                             
    	                        
	                            if(tempNode.tagName=="TD"&&a_n.name=="class")
	                            {
	                              tempNode.className=a_n.value;
    	                                                   
	                              }
	                            else if(tempNode.tagName=="TD"&&a_n.name.toLowerCase()=="colspan")
	                            {
	                              tempNode.colSpan=a_n.value;
    	                                                   
	                              }
	                            else
	                            {  
	                              tempNode.setAttribute(a_n.name,a_n.value);
	                        
	                            }
    	                         
	                        }
    	                  
	                    }
	                }
	            }
	          
	        }
	    }
	    catch(e)
	    {
	        if(debug)
            {
                alert(e+" [at createElementFromString]");
            }
	    }
	    return tempNode;
	  }
    
   

    //Utility function to convert any number to roman numerals
    function toRoman(num) 
    {
        var Roman = "";
        
        try
        {
            onesArray = new Array("I","II","III","IV","V","VI","VII","VIII","IX");
            tensArray = new Array("X","XX","XXX","XL","L","LX","LXX","LXXX","XC");
            hundredsArray = new Array("C","CC","CCC","CD","D","DC","DCC","DCCC","CM");

            ones = num % 10;
            num = (num - ones) / 10;
            tens = num % 10;
            num = (num - tens) / 10;
            hundreds = num % 10;
            num = (num - hundreds) / 10;

            
        
            for (i=0; i < num; i++){
                Roman += "M";
            }

            if (hundreds) {
                Roman += hundredsArray[hundreds-1];
            }
        
            if (tens) {
                Roman += tensArray[tens-1];
            }
        
            if (ones) {
                Roman += onesArray[ones-1];
            }
        }
        catch(e)
        {
            if(debug)
            {
				alert(e+" [at toRoman]");
            }
        }
        return Roman.toLowerCase();
    }

    //Utility function to trim a string
    String.prototype.trim = function() 
    {
	    return this.replace(/^\s+|\s+$/g,"");
    }

	//Utility function to replace all occurences of a string in the target string
	String.prototype.replaceAll = function( strTarget, strSubString )
	{
		var strText = this;
		var intIndexOfMatch = strText.indexOf( strTarget );

		while (intIndexOfMatch != -1){
			strText = strText.replace( strTarget, strSubString )
			intIndexOfMatch = strText.indexOf( strTarget );
         }
        return( strText );
    }
           
