﻿function setStyleSheet(theme) {
   
   var i, a, main;
   a = document.getElementsByTagName("link");
   for(i=0; i < a.length; i++) 
   {
     
     if(a[i].getAttribute("rel").indexOf("style") != -1
        && a[i].getAttribute("title") && a[i].rel) 
        {
       a[i].disabled = true;
       if((a[i].getAttribute("title") == theme) || (theme != "system" && a[i].getAttribute("title") == "layout"))
        {
            
            a[i].disabled = false;
            if (a[i].rel == "alternative stylesheet")
                a[i].rel = "stylesheet";
        }
       
     }
   }
   
  
    
    var s = document.getElementById(styleID);
  
   if (theme == 'system')
   {
      //  s.rel = "alternative stylesheet";
      s.disabled = true;            
   }
   else
   {
      //  s.rel = "stylesheet";
      s.disabled = false;
   }
   
   
}

function ShowPrintView()
{   

    var i, a, main;
    a = document.getElementsByTagName("link");
    for(i=0; i < a.length; i++) 
    {

        if (a[i].title == "print")
        {
            a[i].media = "screen";
            a[i].disabled = false;
            a[i].rel = "stylesheet";
        }
        else
            a[i].disabled = true;
    }
    
    var s = document.getElementById(styleID);
    s.disabled = true;
    var bodies = document.getElementsByTagName('body');
    for (var i = 0; i < bodies.length; i++)
    {
        bodies[i].style.backgroundColor = "white";
    }
}

function SaveStyle(theme)
{
    var expires = "";
	//document.cookie = "rubensteinstyle"+"="+theme+expires+"; path=/";
	createCookie("rubensteinstyle", theme, 365);
}

function LoadStyle()
{
    var style;
    var query = window.location.search.substring(1);
    if (query.indexOf("printview") < 0) 
    {
        setStyleSheet("layout");
        //style = document.cookie.split("=")[1];    
        style = readCookie("rubensteinstyle");
        if (style == null || style == 'undefined')
        {
            style = 'screen';
            var expires = "";
	        //document.cookie = "rubensteinstyle"+"=screen"+expires+"; path=/";
	        createCookie("rubensteinstyle", "screen", 365);
        }
        setStyleSheet(style);
        
    }
    else
    {
        style = 'print';
        var bodies = document.getElementsByTagName('body');
        
        setStyleSheet(style);
        for (var i = 0; i < bodies.length; i++)
        {
            bodies[i].style.backgroundColor = "white";
        }
    }  
    
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

var kw;

function HighlightText() 
{
    
    var printOption = document.getElementById('printOption').innerHTML;
    
    var bdy = document.getElementById('contentcentrecontent').innerHTML; 
    for (var i = kw.length - 1; i >= 0; i--) 
    {
        var re = new RegExp('(\\b'+kw[i]+'\\b)','ig'); 
        bdy = bdy.replace(re,'<span class="hl">$1<\/span>'); 
        //var re1 = new RegExp('(<[^>]*?)<span class="hl">('+kw[i]+')<\/span>(.*?>)','ig'); 
        //bdy = bdy.replace(re1,'$1$2$3');var re2 = new RegExp('(<script.*?>)<span class="hl">('+kw[i]+')<\/span>(<\/script>)','ig'); 
        //bdy = bdy.replace(re2,'$1$2$3'); var re3 = new RegExp('(<textarea.*?>)<span class="hl">('+kw[i]+')<\/span>(<\/textarea>)','ig'); 
        //bdy = bdy.replace(re3,'$1$2$3');
    } 
    document.getElementById('contentcentrecontent').innerHTML = bdy;
    document.getElementById('printOption').innerHTML = printOption;
}

function LoadHighlight()
{
    if (kw == null)
        return;
        
    if (kw.length > 0)
        HighlightText();
}

function openPrintOnly()
{
    var newWin = window.open('print.aspx','newWin');
    newWin.document.getElementById('Content').innerHTML = document.getElementById('contentcentrecontent').innerHTML;
    
}

function ExpandCollapseTopicsByParentInstanceId(id, parentInstanceId)
{
    $(".topic-container[parentinstanceid=" + parentInstanceId + "]").each(
        function()
        {
            if ($(this).hasClass("hidden"))
            {
                $(this).removeClass("hidden");
                document.getElementById(id).innerHTML = "Collapse (-)";
                $(this).show();                        
            }
            else
            {
                $(this).addClass("hidden");
                document.getElementById(id).innerHTML = "Expand (+)"; 
                $(this).hide();                                              
            }
        }
    );
}

function CollapseTopicsByParentInstanceId(id, parentInstanceId)
{
    $(".topic-container[parentinstanceid=" + parentInstanceId + "]").each(
        function()
        {
            $(this).addClass("hidden");
            //$(this).hide();            
        }
    );
}

function AddArticleTypeSelectEvent()
{
     $(".article-container input[@type='checkbox']").click(function()
     {
        var selectedIndex = this.id.substring(this.id.length - 1); 
             
        if (selectedIndex == 0)
        {
            $(".article-container input[@type='checkbox']").each(function()
            {
                var index = this.id.substring(this.id.length - 1);
                
                if (index != 0)
                {
                    this.checked = false;
                }
            }); 
        }
        else if (selectedIndex == 1)
        {
            $(".article-container input[@type='checkbox']").each(function()
            {
                var index = this.id.substring(this.id.length - 1);
                
                if (index != 1)
                {
                    this.checked = false;
                }
            }); 
        }
        else
        {
            $(".article-container input[@type='checkbox']").each(function()
            {
                var index = this.id.substring(this.id.length - 1);
                
                if (index == 0)
                {
                    this.checked = false;
                }
                if (index == 1)
                {
                    this.checked = false;
                }
            }); 
        }
     }); 
}

function AddTopicSelectEvent()
{
     $(".topic-panel input[@type='checkbox']").click(function()
     {
        var selectedInstanceId = this.parentNode.getAttribute("instanceid");
        var selectedParentInstanceId = this.parentNode.parentNode.getAttribute("parentinstanceid");
        var selectedParentID = this.parentNode.parentNode.getAttribute("id");
        var selectedHierarchyLevel = this.parentNode.getAttribute("hierarchylevel");
                                              
        if (selectedInstanceId == -1)
        {
            $(".topic-panel input[@type='checkbox']").each(function()
            {
                var instanceId = this.parentNode.getAttribute("instanceid");
                
                if (instanceId != -1)
                {
                    this.checked = false;
                    
                }
            }); 
        }
        else
        {
            if (selectedHierarchyLevel == 1 && this.checked) //for sub topics, untick parent
            {
               $(".topic-panel input[@type='checkbox']").each(function()
               {
                if (this.parentNode.getAttribute("instanceid") == selectedParentInstanceId)
                {
                    this.checked = false;                    
                }
                
               });
               
               
            }
            $(".topic-panel input[@type='checkbox']").each(function()
            {
                var instanceId = this.parentNode.getAttribute("instanceid");
                var hierarchyLevel = this.parentNode.getAttribute("hierarchylevel");
                var parentInstanceId = this.parentNode.parentNode.getAttribute("parentinstanceid");

                if (instanceId == -1)
                {
                    this.checked = false;
                }
                
                if (selectedParentInstanceId != parentInstanceId && hierarchyLevel == 1)
                {
                    this.checked = false;
                    CollapseTopicsByParentInstanceId(this.id, parentInstanceId);
                }
                
                if (selectedHierarchyLevel == 1 && hierarchyLevel == 0)
                {
                    this.checked = false;
                }
            });
        
        }
        
        $(".topic-panel input[@type='checkbox']").each(function()
        {            
            var instanceId = this.parentNode.getAttribute("instanceid");
            var hierarchyLevel = this.parentNode.getAttribute("hierarchylevel");
            var parentInstanceId = this.parentNode.parentNode.getAttribute("parentinstanceid");   
            if ($(this).hasClass("hidden"))
            {
                $(this).hide();                          
            }
            else
            {  
                $(this).show();
            }
        });
        $(".searchExpandCollapse").each(function()
        {
            var id = this.id;
            var parentInstanceId = this.getAttribute("ParentInstanceId");
            if (parentInstanceId == selectedParentInstanceId) {
                document.getElementById(id).innerHTML = "Collapse (-)";
            } else {
                document.getElementById(id).innerHTML = "Expand (+)";
            }
        });
     });
}

function replaceValue(obj, val)
{
    if (obj.value == val)
        obj.value = '';    
}