var px = 'px';
var browser = window.navigator.appName;
var sDocDir = document.dir;
var oOverlay = null;
var oNavCell = null;
var oTreeCell = null;
var oSplitterCell = null;
var sSplitterCookieX = 0;
var sSplitterDefault = '300px';
var sTmpNavCellWidth = '300px';
var sNameTmpNavCellWidth = sTmpNavCellWidth;
var oContentCell = null;
var winWidth;
var winHeight;
var days = 60;
var startTop = 78;

var sUserAgent = navigator.userAgent;
var isOpera = sUserAgent.indexOf("Opera") > -1;
var isIE = sUserAgent.indexOf("compatible") > -1  && sUserAgent.indexOf("MSIE") > -1 && !isOpera;
var spliterBarWidth = 5;       
var treeHeightChange = -1; // Firefox does not change the scrollbar automatilly if there are not needs
                         // we change the height by  1 or -1 each time, then it will resize

var minWidth = 0;
var ideUA = false;
var dragging = false;

function windowLoaded(evt) 
{  
  // prevent IE text selection while dragging!!! Little-known trick!
  document.body.ondrag = function () { return !dragging; };
  document.body.onselectstart = function () { return !dragging; };
}


function DoControlSetup()
{
  minWidth = 300;
	
	this.name = "DoControlSetup";
	
  if (browser == "Netscape") 	{
    document.addEventListener("onkeypress",KeyPressOpenCloseToc,true);
  }
  document.onkeypress = KeyPressOpenCloseToc;


  FetchResizableAreaCells();
  return;
}


//  Layout the <div> elements based on the client side renderable area
function FetchResizableAreaCells()//resize)
{   
  winWidth = parseInt(document.body.clientWidth);
  oOverlay = document.getElementById('OverlayBar');
  
  oTreeCell = document.getElementById('divTree');
  
  oSplitterCell = document.getElementById('divSplitter');
  oContentCell = document.getElementById('divContent');
  oSplitterCell.style.width = spliterBarWidth + px;

  
  var def = getCookie('treePanelWidth');

  if (def != "" && def != null)
    sSplitterDefault = def;  

	if (oTreeCell.style.width != sSplitterDefault) {
    //oTreeCell.style.width = sSplitterDefault;
    SetTreeCellWidth(sSplitterDefault);
    oSplitterCell.style.left = sSplitterDefault;
    oContentCell.style.left = parseInt(oSplitterCell.style.width) + parseInt(sSplitterDefault) + px;
	}    

  if (winWidth - parseInt(oContentCell.style.left) < minWidth) {
  	var newWidth = winWidth-605;
    if (newWidth < 0) {
  		newWidth = parseInt(oTreeCell.style.width);
    }

    //oTreeCell.style.width = newWidth + px;
    SetTreeCellWidth(newWidth + px);
    oSplitterCell.style.left = oTreeCell.style.width;
    oContentCell.style.left = parseInt(oSplitterCell.style.width) + newWidth  + px;
	}
	
	if ( document.body.clientHeight < startTop )
	  oTreeCell.style.height="0px"
	else 
    oTreeCell.style.height= (parseInt(document.body.clientHeight) -startTop) + "px"
  //if ( resize == 1 )
    ResizeContentArea();
  return;
}

  

//  Attach Event Handlers
function SelectSplitter()
{	// Required for Mozilla 1.0, Netscape 7.2
  oOverlay.className = "OverlayShow";
  if (browser == "Netscape") {	
	  //	NS4: document.captureEvents(Event.MOUSEMOVE);
    oOverlay.addEventListener("onmousemove",ResizePanel,true);
    oOverlay.addEventListener("onmouseup",ReleaseSplitter,true);
	}

	//	IE
  document.onmousemove = ResizePanel;
  document.onmouseup = ReleaseSplitter;
	//oOverlay.onmousemove = ResizePanel;
	//oOverlay.onmouseup = ReleaseSplitter;
	dragging = true;
	return;
}


//	Changed to OnMouseMove functionality
function ResizePanel(e)
{
  // oSplitterCell
  var contentSizeLimit;
  
  if (browser == "Netscape") {
    e.cancelBubble = true;
    splitterX = e.pageX;
  }
  else {
    window.event.cancelBubble = true;
    if (window.event.clientX - 4 < 0) {
      splitterX = 0;
    }
    else {
      splitterX = window.event.clientX - 4;
    }
  }

  if((winWidth - splitterX- spliterBarWidth) < minWidth)
    splitterX = winWidth - minWidth - spliterBarWidth

  window.status = splitterX
  sSplitterCookieX =splitterX

  oSplitterCell.style.left = parseInt(sSplitterCookieX) + px;

  return;
}

//	Changed to OnMouseUp functionality
function ReleaseSplitter()
{
  oOverlay.className = "OverlayHidden";

  sTmpNavCellWidth = parseInt(sSplitterCookieX) + px;
  //oTreeCell.style.width = parseInt(sSplitterCookieX) + px;
  SetTreeCellWidth(parseInt(sSplitterCookieX) + px);
  oSplitterCell.style.left = parseInt(sSplitterCookieX) + px;
  oContentCell.style.left = sSplitterCookieX + parseInt(oSplitterCell.style.width) + px;
  ResizeContentArea();
	SetCookie('treePanelWidth',oTreeCell.style.width, days);
	//oOverlay.onmousemove = null;
	//oOverlay.onmouseup = null;
	document.onmousemove = null;
	document.onmouseup = null;
	dragging = false;
	return;
}


//	ONKEYPRESS:
//	CHECK WHICH KEYS HAVE BEEN PRESSED
//	IF A MATCH IS FOUND, SHOW/HIDE TOC PANEL
function KeyPressOpenCloseToc(e)
{
  if (browser == "Netscape") 	{	
    //   alert(e.which + "\n" + e.target.tagName);
		if (e.which == 116) {
		  if (e.target.tagName.toLowerCase() != "input" && e.target.tagName.toLowerCase() != "textarea" ) {
		    OpenClosePanel();
      }
			e.cancelBubble = true;
		}
	}
	else if(window.event.keyCode == 116) {   
		if (window.event.srcElement.tagName.toLowerCase() != "textarea" && 
		    window.event.srcElement.tagName.toLowerCase() != "input") {
		  OpenClosePanel();
		}
		window.event.cancelBubble = true;
	}
	return;
}



//  Simple Hide/Show Functionality
//  Need to add cookie setting
//  Add CSS ClassName change for ContentPanel
function OpenClosePanel()
{
  if (oTreeCell.style.width != "0px") {
    sTmpNavCellWidth = oTreeCell.style.width;
    oTreeCell.style.width = "0px";
    oContentCell.style.left = parseInt(oSplitterCell.style.width) + px;
    oSplitterCell.style.left = "0px";
  }
  else if ( (oTreeCell.style.width == "0px" && sTmpNavCellWidth == undefined) || 
            (oTreeCell.style.width == "0px" && sTmpNavCellWidth == "0px")) {
    //oTreeCell.style.width = "250px";
    SetTreeCellWidth("250px");
    oSplitterCell.style.left = "250px";
    sTmpNavCellWidth = oTreeCell.style.width;
    oContentCell.style.left = parseInt(oTreeCell.style.width) + parseInt(oSplitterCell.style.width) + px;
  }
  else {
    //oTreeCell.style.width = sTmpNavCellWidth;
    SetTreeCellWidth(sTmpNavCellWidth);
    oSplitterCell.style.left = sTmpNavCellWidth;
    oContentCell.style.left = parseInt(oTreeCell.style.width) + parseInt(oSplitterCell.style.width) + px;
  }

  if (winWidth - parseInt(oContentCell.style.left) < minWidth) {
    var newWidth = winWidth - minWidth - spliterBarWidth;

    if (newWidth < 0) {
      if (parseInt(oTreeCell.style.width) > 76) {
        newWidth=76;
      } 
      else {
        newWidth = parseInt(oTreeCell.style.width);
      } 
    }
    
    //oTreeCell.style.width = newWidth + px;
    SetTreeCellWidth(newWidth + px);
    oSplitterCell.style.left = oTreeCell.style.width;
    oContentCell.style.left = parseInt(oSplitterCell.style.width) + newWidth  + px;
  }

  SetCookie('treePanelWidth',oTreeCell.style.width, days);
  ResizeContentArea();
  return;
}

//  Change the size of the Content container
function ResizeContentArea()
{
  if (winWidth - parseInt(oContentCell.style.left) < minWidth)  { 
    oContentCell.style.width = minWidth + px;
  } 
  else { 
    oContentCell.style.width = winWidth - parseInt(oContentCell.style.left)   + px;
  } 

  var odivInnerContent = document.getElementById('divInnerContent');
  var tempWidth = parseInt(oContentCell.style.width);
  if ( tempWidth > 50 )
    odivInnerContent.style.width = (tempWidth - 50) + px;
  else 
    odivInnerContent.style.width="0px";
  //odivInnerContent.style.marginLeft = "20px";
  //odivInnerContent.style.marginRight = "20px";
  
  var tempHeight = parseInt(document.body.clientHeight);
  if ( tempHeight > startTop )
    oContentCell.style.height= ( tempHeight - startTop) + "px";
  else 
    oContentCell.style.height = "0px";
  return;
}



function SetTreeCellWidth(width)
{
  oTreeCell.style.width  = width;
  var oDivInnerTree = document.getElementById('divInnerTree');
  var tempWidth = parseInt(oTreeCell.style.width);
  if ( tempWidth > 40 )
    oDivInnerTree.style.width = ( tempWidth- 40) + px;
  else 
    oDivInnerTree.style.width = "0px";
  return;
}


//Cookies and Misc Functions
// Set cookie on each event Function to set the Toc panel size Browser must support cookies and script
function SetCookie(sName, sValue, days)
{
  if (window.navigator.cookieEnabled == true) {
    var expires = ";";
    if (days > 0){
      var cookieDate = new Date();
      cookieDate.setTime(cookieDate.getTime()+(days*24*360000));
          expires = "; expires=" + cookieDate.toGMTString();
    }
    var CookieInfo = sName + "=" + escape(sValue) + "; expires=-1" + "; path=/"; 
    document.cookie = CookieInfo;

    var CookieInfo = sName + "=" + escape(sValue) + expires + "; path=/"; 
    document.cookie = CookieInfo;
  }
  return;
}


function PrintDocument()
{ 
  debugger;
  if (window.print) {      
    var printWin = window.open('', 'printWin');
    
    with ( printWin.document ) {
      write('<head><title>');
      write(window.document.title);
      write('</title></head>');
      write(window.document.getElementById("topBanner").innerHTML);
      write(window.document.getElementById("divContent").innerHTML);
      close();
      printWin.print();
    }
  }
}

function SetFocus(element)
{
  var targetElementt = document.getElementById(element)
  targetElementt.focus();
}  





function ToggleVisible(buttonElementID, targetElementID, name)
{ 
  var targetElement = document.getElementById(targetElementID)
  var buttonElement = document.getElementById(buttonElementID)
  if(targetElement != null) {
    if(targetElement.style.display != "block") {
      targetElement.style.display="block"
      buttonElement.value = "Hide " + name
    }
    else {
      targetElement.style.display= "none"
      buttonElement.value = "Show " + name
    }
  }

  return false
}


function DoNothing()
{
}  

            
function getCookie(sName) 
{
  var sRE = "(?:; )?" + sName + "=([^;]*);?";
  var oRE = new RegExp(sRE);

  if (oRE.test(document.cookie)) {
    return decodeURIComponent(RegExp["$1"]);
  } 
  else {
    return null;
  }

}                

function deleteCookie(sName, sPath, sDomain) {
  var sCookie = sName + "=; expires=" + (new Date(0)).toGMTString();
  if (sPath) {
    sCookie += "; path=" + sPath;
  }

  if (sDomain) {
    sCookie += "; domain=" + sDomain;
  }

  document.cookie = sCookie;
}
    
    
var lastNode;
// Get the currently loaded URL from the other frame

// Expand or collapse all nodes
function ExpandOrCollapseAll(expandNodes)
{
    var divIdx, childIdx, img, divs = document.getElementsByTagName("DIV");
    var childNodes, child, div, link, img;

    for(divIdx = 0; divIdx < divs.length; divIdx++)
        if(divs[divIdx].className == "Hidden" ||
          divs[divIdx].className == "Visible")
        {
            childNodes = divs[divIdx].parentNode.childNodes;

            for(childIdx = 0; childIdx < childNodes.length; childIdx++)
            {
                child = childNodes[childIdx];

                if(child.className == "TreeNodeImg")
                    img = child;

                if(child.className == "Hidden" || child.className == "Visible")
                {
                    div = child;
                    break;
                }
            }

            if(div.className == "Visible" && !expandNodes)
            {
                div.className = "Hidden";
                //img.src = "TreeCollapsed.gif";
                img.src = img.src(/TreeExpanded/, "TreeCollapsed");
            }
            else
                if(div.className == "Hidden" && expandNodes)
                {
                    div.className = "Visible";
                    img.src = img.src(/TreeCollapsed/, "TreeExpanded");
                    //img.src = "TreeExpanded.gif";
                }
        }
}

// Toggle the state of the specified node
function Toggle(node)
{
  var i, childNodes, child, div, link;

  childNodes = node.parentNode.childNodes;
  
  for(i = 0; i < childNodes.length; i++) {
    child = childNodes[i];

    if(child.className == "Hidden" || child.className == "Visible") {
      div = child;
      try {
        var isMainWebPage = getCookie("IsMainWebPage")
        var treeStatuCookieName;
        if ( isMainWebPage == "1" ) 
          treeStatuCookieName = "MainWebTreeStatus";
        else 
          treeStatuCookieName = "DocTreeStatus";
          
        var origin = getCookie(treeStatuCookieName);
        var position = parseInt(div.id.substring(3))
        var newExpand = origin.substring(0, position);
        if ( child.className == "Hidden" ) // was hidden and is changed to visisble
          newExpand += "e";
        else 
          newExpand += "n";
        
        if ( origin.length > position )
          newExpand += origin.substring(position + 1);
        SetCookie(treeStatuCookieName, newExpand, 1);
      }
      catch(exception){
      }
      break;
    }
  }

  if(div.className == "Visible") {
    div.className = "Hidden";
    node.src = node.src.replace(/TreeExpanded/, "TreeCollapsed");
  }
  else {
    div.className = "Visible";
    node.src = node.src.replace(/TreeCollapsed/, "TreeExpanded");
  }
 
  var tree = document.getElementById('ctl00_tree');
  //alert("tree heigh: " + tree.style.height + "  oTreeCell heigh:" + oTreeCell.style.height);
  oTreeCell.style.height = (parseInt(oTreeCell.style.height) + treeHeightChange) + "px" ;
  treeHeightChange = -1 * treeHeightChange;
  
  //oTreeCell.style.width = (parseInt(oTreeCell.style.width)+30) + "px" ;
  
}

// Expand the selected node
function Expand(node)
{
  var i, childNodes, child, div, img;

  
  //if(node.tagName == "span")
      childNodes = node.parentNode.childNodes;
  /*else
      childNodes = node.childNodes;*/

  for(i = 0; i < childNodes.length; i++) {
    child = childNodes[i];

    if(child.className == "TreeNodeImg")
      img = child;

    if(child.className == "Hidden" || child.className == "Visible") {
      div = child;
      break;
    }
  }

  /*if(lastNode != null)
    lastNode.className = "UnselectedNode";*/

  if(div.className == "Visible"){
    div.className = "Hidden";
    img.src = img.src.replace(/TreeExpanded/, "TreeCollapsed");
  }
  else {
    div.className = "Visible";
    img.src = img.src.replace(/TreeCollapsed/, "TreeExpanded");
  }

  //node.className = "SelectedNode";
  //lastNode = node;

  return true;
}


// Set the style of the specified node to "selected"
function SelectNode(node)
{

    if(lastNode != null)
        lastNode.className = "UnselectedNode";

    node.className = "SelectedNode";
    lastNode = node;

    return true;
}

    
function ChangeLanguage()
{
  var theForm = document.forms['aspnetForm'];
  if (!theForm) {
      theForm = document.aspnetForm;
  }
  theForm.submit();
}