
var browseID;
var listingTop;
var lastCatClass;
var lastCatObj;
var lastCatId;
var lastCatRootClass;
var lastCatRootObj;
var lastCatRootId;
var kbbase;
if(userIsAuthenticated)
{
  kbbase = '/support';
}
else
{
    kbbase = '/nas';
}

function toggleKB(id, popped)
{
        var obj = document.getElementById("kbccat_" + id);
        if(obj)                     
        {
                if(obj.style.display == "none")
                {
                        obj.style.display = "block";
                        var iObj = document.getElementById("kbicat_" + id);
                        if(iObj)
                        {
                                iObj.src = "/cobrand/" + signupPath + "/images/arrow_down.gif";
                        }
                        script = '/home/home/setPref.cgi?pType=list&pAction=add&pVar=kb.category.expand&pVal=' + id;

			if(!obj.innerHTML)
			{
				browseID = id;
				url = kbbase + "/browse2.cgi?pID=" + id + '&pType=category&pPop=' + popped;
        		        var client = new HTTPClient();	
                		client.init(url);	
	                	client.asyncGET(browseKB);			

				iobj = document.getElementById('kbccat_' + browseID);
				if(iobj)	
				{
					iobj.innerHTML = "<img src='/cobrand/DEFAULT/images/loading.gif'>";
				}
			}
                }                                                        
    	  	else
	        {                                                                
        	        obj.style.display = "none";
                	var iObj = document.getElementById("kbicat_" + id);
	                if(iObj)
        	        {
                        	iObj.src = "/cobrand/" + signupPath + "/images/arrow_right.gif"; 
                	}
		        script = '/home/home/setPref.cgi?pType=list&pAction=remove&pVar=kb.category.expand&pVal=' + id;
      		}
		hitUrl(script);
	}
}                                       

var browseKB = {
  onLoad: function(data)
  {
        if (!data)
	{
		data = "No data found.";
        }
	obj = document.getElementById('kbccat_' + browseID);
	if(obj)
	{
		obj.innerHTML = data;
		IELoadFixes();
	}
		browseID = '';	
 },
  onError: function() {
                data = "Error retrieving data.";
	obj = document.getElementById('kbccat_' + browseID);
	if(obj)
	{
		obj.innerHTML = data;
	}
		browseID = '';
  }

}


function getSearchResults()
{
	var srObj;
	var st;
	srObj = document.getElementById('kbSearchTerms');
	if(srObj)
	{
		st = srObj.value;
	}
	cat = document.getElementsByName('pSearchCategories');
	if(cat)
	{
		url = kbbase + "/index.cgi?";
		if(cat[0].checked)
		{
			art = document.getElementsByName('pSearchArticles');	
			if(art[0].checked)
			{
				url += "pSearchArticles=1&";
			}	

			url += "pSearchCategories=1&pSearchTerms=" + myEncode(st);
			window.location = url;
			return(false);
		}
	        moveListings(cat[0]);
	}
	
	moveContent('articleListTitle', 'lastArticleListTitle');
	moveContent('articleListContent', 'lastArticleListContent');	
	replaceContent("articleListTitle", "Searching...");
	replaceContent("articleListContent", "<img src='/cobrand/DEFAULT/images/loading.gif'>");	

	url = "/support/kbsearch.cgi?pSearchTerms=" + myEncode(st);
        var client = new HTTPClient();
        client.init(url);
        client.asyncGET(displaySearchResults);

	return(false);
}

function moveListings(clickObj)
{
     if(!listingTop)
     {
	obj = document.getElementById('kbItemListings');
	if(obj)
	{
        
		listingTop = getObjectTop(obj);
	}
     }
     
     currentObj = document.getElementById('kbItemListings');	
     currentTop = getObjectTop(currentObj);
     clickTop = getObjectTop(clickObj);
     
     if(clickTop > 500 || currentTop > 300)
     {

newTop = clickTop;
	if(newTop < listingTop)
	{
		newTop = listingTop;
	}	
 /*
    if(clickTop > listingTop)
    {
        newTop = clickTop - listingTop;
    }
    else
    {
        newTop = listingTop;
    }
    */
    steps = 5;
    dur = 150;
    for(i=0; i< Math.abs(currentTop - clickTop); i+=100)
    {
        steps = steps + 1;
        dur = dur + 25;
    }
    
    
     	new Rico.Effect.Position( 'kbItemListings', 0, newTop, dur, steps);
       
    }
     
}

function getArticles(id, pop)
{
    
	newT = 'Previous Articles for ' + getContent('articleListTitle', ' Articles');
	/*
	moveContent('articleListTitle', 'lastArticleListTitle');
	*/
	replaceContent('lastArticleListTitle', newT);
	moveContent('articleListContent', 'lastArticleListContent');	
	moveContent('catTitle_' + browseID, 'articleListTitle');
	replaceContent("articleListTitle", 'Looking Up Articles');
	replaceContent('articleListContent', "<img src='/cobrand/DEFAULT/images/loading.gif'>");

		url = kbbase + "/browse2.cgi?pID=" + myEncode(id);
		if(pop)
			url += "&pPop=1";
                var client = new HTTPClient();
                client.init(url);
                client.asyncGET(articleList);
		browseID = id;
		
		var tid = id;
		tid = id.split('/', 3);
		rootid = '/' + tid[1];
		if(rootid != lastCatRootId)
		{
			robj = document.getElementById('kbocat_' + rootid);
			if(robj)
			{
				if(lastCatRootObj)
				{
					lastCatRootObj.className = lastCatRootClass;
				}
				else
				{
					if(lastCatRootId)
					{
						lobj = document.getElementById('kbocat_' + lastCatRootId);
						if(lobj)
						{
							lobj.className = 'unhighlightRootCategory';
						}
					}
				}
				lastCatRootClass = robj.className;

				lastCatRootId = rootid;
				robj.className = 'highlightRootCategory';
				lastCatRootObj = robj;
			}
		} 
		obj = document.getElementById('kbcat_' + id);
		if(obj)
		{
			if(lastCatObj)
			{
				lastCatObj.className = lastCatClass;
			}
			else
			{
				if(lastCatId)
				{
					lcobj = document.getElementById(lastCatId);	
					if(lcobj)
					{
						lastCatId = '';
						lcobj.className = lcobj.className.replace(new RegExp(" highlightCategory\\b"), "");
					}
				}
			}
			lastCatClass = obj.className;
			obj.className += " highlightCategory";
			lastCatObj = obj;
			
			moveListings(obj);		
		}
}

function getContent(id, strip)
{
	cObj = document.getElementById(id);
	if(cObj)
	{
		cont = cObj.innerHTML;
		if(strip)
		{
			cont = cont.replace(strip, '');
		}
	}
	return(cont);
}

function moveContent(from, to, replace)
{
	fromObj = document.getElementById(from);
	if(fromObj)
	{
		toObj = document.getElementById(to);	
		if(toObj)
		{
			toObj.innerHTML = fromObj.innerHTML;
		}
	}
}

function replaceContent(id, data)
{
	obj = document.getElementById(id);
	if(obj)
	{
		obj.innerHTML = data;
	}
}

function appendContent(id, data)
{
	obj = document.getElementById(id);
	if(obj)
	{
		obj.innerHTML = obj.innerHTML + data;
	}
}

function prependContent(id, data)
{
	obj = document.getElementById(id);
	if(obj)
	{
		obj.innerHTML = data + obj.innerHTML;
	}
}

function getPreview(id)
{
	obj = document.getElementById('activeContent');

	if(obj)
	{
		contentHTML = obj.value;
		url = "/support/kbpreview.cgi";
                var client = new HTTPClient();
                client.init(url);
                client.asyncPOST(showPreview, "pContent=" + postEncode(contentHTML));
		replaceContent('previewAreaContent', "<img src='/cobrand/DEFAULT/images/loading.gif'/>");
	}
	return(false);
}


function postEncode(input)
{
	if(input)
	{
        	// ampers
		input = input.replace(/\&/g,'%26');
	}
	return(input);
}

var articleList = {
  onLoad: function(data)
  {
        if(!data)
	{
		data = "No data found.";
        }

	/*
	moveContent('articleListTitle', 'lastArticleListTitle');
	moveContent('articleListContent', 'lastArticleListContent');	
	*/
	moveContent('catTitle_' + browseID, 'articleListTitle');
	appendContent('articleListTitle', " Articles");
	replaceContent('articleListContent', data);

        /* add window scrolling */

        currentObj = document.getElementById('articleListContent');	
        currentTop = getObjectTop(currentObj);
        currentHeight = getObjectHeight(currentObj)
        currentBottom = currentTop + currentHeight;

        previousObj = document.getElementById('lastArticleListContent');	
        previousTop = getObjectTop(previousObj);
        previousHeight = getObjectHeight(previousObj) + 25;
        previousBottom = previousTop + previousHeight;	

	windowHeight = getInsideWindowHeight();
	scrolledY = getScrollXY();
        pageTop = windowHeight + scrolledY;
	if(currentBottom + previousHeight > windowHeight + scrolledY)
	{
		finalPosition = currentBottom + previousHeight - windowHeight + scrolledY;
		inc = (finalPosition - windowHeight) / 6;
		// scrollMe(scrolledY, inc, 0, 6);
		window.scrollTo(0, currentBottom + previousHeight - windowHeight + scrolledY);	
	}
	IELoadFixes();
  },
  onError: function() {
        data = "Error retrieving data.";
	obj = document.getElementById('kbccat_' + browseID);
	if(obj)
	{
		obj.innerHTML = data;
	}
  }
}

function scrollMe(current, inc, step, steps)
{
	if(step >= steps)
	{
		if(scrollTime)
		{
			clearTimeout(scrollTime);
		}
	}
	else
	{
		newCurrent = current + inc;
		window.scrollTo(0, newCurrent);
		scrollTime = setTimeout("scrollMe(\"" + newCurrent + "," + inc + "," + step+1 + "," + steps + "\");", 175);
	}
}


var displaySearchResults = {
  onLoad: function(data)
  {
/*
	moveContent('articleListTitle', 'lastArticleListTitle');
	moveContent('articleListContent', 'lastArticleListContent');	
*/
	moveContent('catTitle_' + browseID, 'articleListTitle');
	replaceContent('articleListTitle', "Article Search Results");
	replaceContent('articleListContent', data);
	return(false);
  },
  onError: function() 
  {
/*
	moveContent('articleListTitle', 'lastArticleListTitle');
	moveContent('articleListContent', 'lastArticleListContent');	
*/
	moveContent('catTitle_' + browseID, 'articleListTitle');
	replaceContent('articleListTitle', "Article Search Results");
	replaceContent('articleListContent', "No data found.");
	return(false);
  }
}

var showPreview = {
  onLoad: function(data)
  {
        if (!data)
	{
		data = "No data found.";
        }

	replaceContent('previewAreaContent', data);
  },
  onError: function() {
                data = "Error retrieving data.";
	replaceContent('previewAreaContent', data);	

  }
}

function rateArticle(id, score)
{
	url = "/support/feedback.cgi?pID=" + id + "&pScore=" + score;
	hitUrl(url);
	obj = document.getElementById('kbRatingPoor');
	if(obj)
	{
		if(score != 1)
			obj.style.visibility = 'hidden';
		obj.disabled = true;
	}
	obj = document.getElementById('kbRatingFairPoor');
	if(obj)
	{
		if(score != 3)
			obj.style.visibility = 'hidden';
		obj.disabled = true;
	}
	obj = document.getElementById('kbRatingFair');
	if(obj)
	{
		if(score != 5)
			obj.style.visibility = 'hidden';
		obj.disabled = true;
	}
	obj = document.getElementById('kbRatingFairGood');
	if(obj)
	{
		if(score != 7)
			obj.style.visibility = 'hidden';
		obj.disabled = true;
	}
	obj = document.getElementById('kbRatingGood');
	if(obj)
	{
		if(score != 9)
			obj.style.visibility = 'hidden';
		obj.disabled = true;
	}
	obj = document.getElementById('kbImgScorePoor');
	if(obj)
	{
		if(score != 1)
			obj.style.visibility = 'hidden';
		obj.disabled = true;
	}
	obj = document.getElementById('kbImgScoreFair');
	if(obj)
	{
		if(score != 5)
			obj.style.visibility = 'hidden';
		obj.disabled = true;
	}
	obj = document.getElementById('kbImgScoreGood');
	if(obj)
	{
		if(score != 9)
			obj.style.visibility = 'hidden';
		obj.disabled = true;
	}
}


function sendArticleTo(id)
{
	obj = document.getElementById('sendTheArticle');
	if(obj)
	{
		to = obj.value;
		if(to && to.search('@'))
		{
			url = '/support/sendArticle.cgi?pID=' + id + '&pTO=' + to;
			hitUrl(url);
			obj.disabled = true;
		}
	}
	return(false);
}


function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  // return [ scrOfX, scrOfY ];
  return(scrOfY);
}

function loadInParent(url)
{
	window.top.opener.location = url;
	return(false);
}

function IELoadFixes()
{
var IE = document.all?true:false;
if(!IE)
return(true);

        var boxItems = document.getElementsByTagName("li");
        if(boxItems)
        {
                for(var i=0; i<boxItems.length; i++)
                {
                        if(boxItems[i].className == "popBoxTriggerItem")
                        {
                                boxItems[i].onmouseover=function() {
                                        this.className+=" navHover";
                                }
                                boxItems[i].onmouseout=function() {
                                        this.className=this.className.replace(new RegExp(" navHover\\b"), "");
                                }
                        }
                }
        }

}