window.onload = bodyOnLoad;
window.onresize = bodyOnLoad;
window.onbeforeprint = set_to_print;
window.onafterprint = reset_form;

var g_viewPortWidth = 0;
var g_viewPortHeight = 0;

var g_ieVer = getIEVersion();

function getIEVersion() {
//determines the IE version. Returns 0 if not IE
	var verNum = 0
	if (navigator.appName == "Microsoft Internet Explorer") {
		var sVer = window.navigator.userAgent
		var msie = sVer.indexOf ( "MSIE " )
		if ( msie > 0 ) {	// browser is Microsoft Internet Explorer; return version number
			verNum = parseFloat( sVer.substring ( msie+5, sVer.indexOf ( ";", msie ) ) );
		}
	}
	return verNum;
}

function resizeBan(){


	var oBanner = document.getElementById("divBanner");

	if ( oBanner == null )
		return;

	var oContent = document.getElementById("divContent");
	var odivProductTitle = document.getElementById("divProductTitle");
	var odivPageTitle = document.getElementById("divPageTitle");
	var odivTopMenu = document.getElementById("divTopMenu");
	var oFooter = document.getElementById("divFooter");
	var bMainContent = true;

	if ( ( odivTopMenu != null ) && ( odivTopMenu.innerHTML.length == 0 ) ) {
		odivTopMenu.style.display = "none";	
	}

	if (oContent == null) {
		oContent = document.getElementById("divToCContent");		
		if (oContent == null) return;
		oFooter = document.getElementById("divToCFooter");
		oFooter.style.display = "none";
		bMainContent = false;	
	}

	if (oFooter == null) return;


	var oClassic = document.getElementById("divClassic");
	if (oClassic != null) {
		return;
	}

	if (document.body.clientWidth == 0) return;

	if (odivPageTitle != null){
		odivPageTitle.style.padding = "0px 10px 0px 22px; ";
	}

	if (oBanner != null /*&& g_ieVer >= 5*/ ){
		

		if ( document.all && !window.opera ) {
			// hide the reserved scroll bar space in IE
			document.body.scroll = "no"
		}

		oContent.style.overflow = "auto";


		var bodyWidth = getWindowWidth();
		var bodyHeight = getWindowHeight ();
		
		if ( bodyWidth == g_viewPortWidth && bodyHeight == g_viewPortHeight ) {
			return;	// escape for loops in IE due to resize jitter
		}
		
		g_viewPortHeight = bodyHeight;
		g_viewPortWidth = bodyWidth;

		var bannerHeight = oBanner.offsetHeight;
		var footerHeight = oFooter.offsetHeight;
		var contentWidth = oContent.offsetWidth;
		var contentHeight = oContent.offsetHeight;

		var marginOffset = 0;

		oContent.style.top = 0;  

		if (bMainContent) {
			var contentURL = new String (window.location);

			if ( ( ( contentURL.substr ( 0, 4) == "http" ) || ( contentURL.substr ( 0, 4) == "file" ) ) && ( ( parent.left == null ) || ( parent.right == null ) ) ) {
				// this is not being hosted inside the CHM viewer
				var lastPos = contentURL.lastIndexOf ( "/" );

				top.location = "SQLDoc.htm?" + contentURL.substr ( lastPos + 1 );
			}
			else if ( ( contentURL.substr ( 0, 4) == "http" ) || ( contentURL.substr ( 0, 4) == "file" )  ) {
				// not in CHM, so show the external links
				document.getElementById("divHTMLContent").style.display = "inline";
				
				if ( document.all && !window.opera ) {
					// IE so show the book mark
					document.getElementById("divBookmark").style.display = "inline";
				}
				else if ( window.sidebar && ( contentURL.substr ( 0, 4) == "http" ) ) {
					// Firefox, and we're on a web server (the only way to have sidebar code work)
					document.getElementById("divBookmark").style.display = "inline";
				}
			}

			oContent.style.paddingRight = "20px"; // Width issue code

			if ( null == window.innerWidth ) {
				// ie needs the content style to be set for the scroll bars to appear
				oContent.style.width = document.body.offsetWidth - 4;
			} 

			if ( window.opera ) {
				// old IE compatability mode
				bodyHeight = document.body.clientHeight;
			}

			if (bodyHeight > bannerHeight) {
				
				oContent.style.height = ( bodyHeight - bannerHeight - footerHeight ) ;

				marginOffset = document.body.scrollHeight - bodyHeight;
				if ( marginOffset > 0 ) {
					oContent.style.height = ( bodyHeight - bannerHeight - footerHeight - marginOffset - 2 ) ;				
				}
			}
		}
		else {

			var contentURL = new String (window.location);
			if ( ( ( contentURL.substr ( 0, 4) == "http" ) || ( contentURL.substr ( 0, 4) == "file" ) ) && ( ( parent.left == null ) || ( parent.right == null ) ) ) {
				// this is not being hosted inside the CHM viewer
				top.location = "SQLDoc.htm";
			}

			if ( null == window.innerWidth ) {
				// ie needs the content style to be set for the scroll bars to appear, and the offset is for the frameset border
				oContent.style.paddingRight = "20px"; // Width issue code
				oContent.style.width = document.body.offsetWidth - ( document.body.offsetWidth - document.body.clientWidth ) ;
				
			} 


			if ( window.opera ) {
				// old IE compatability mode
				bodyHeight = document.body.clientHeight;

			}

			if (bodyHeight > bannerHeight) {
				oContent.style.height = ( bodyHeight - bannerHeight ) + 'px' ;

				if ( oContent.offsetHeight > ( bodyHeight - bannerHeight - footerHeight ) ) {
					oContent.style.height = ( bodyHeight - bannerHeight - footerHeight ) - ( oContent.offsetHeight - ( bodyHeight - bannerHeight - footerHeight ) );
				}

			}
			

		}

	}	

	try{oContent.setActive();} //allows scrolling from keyboard as soon as page is loaded. Only works in IE 5.5 and above.
	catch(e){}
} 

function bodyOnLoad(){
	resizeBan();
}


function set_to_print(){
//breaks out of divs to print

	var i;

	if (window.text) document.all.text.style.height = "auto";

	
	if ( document.all && !window.opera ) {
		document.body.scroll = "yes";
	}

	var oContent = document.getElementById("divContent");
	if ( oContent ) {
		oContent.style.overflow = "visible";
		oContent.style.top = "5px";
		oContent.style.width = "100%";
		oContent.style.padding = "0px 10px 0px 30px";
		oContent.style.height = "";
	}

	oContent = null;
	oContent = document.getElementById("divHTMLContent");
	if ( oContent ) {
		oContent.style.display = "none";
	}

	oContent = null;
	oContent = document.getElementById("divBanner");
	if ( oContent ) {
		oContent.style.margin = "0px 0px 0px 0px";
		oContent.style.width = "100%";
	}
	
}


function reset_form(){
//returns to the div nonscrolling region after print

	 document.location.reload();
}

var plusImg = new Image();
plusImg.src = "plus.gif";

var minusImg = new Image();
minusImg.src = "minus.gif";


function expandCollapse ( folder, img ) {
// does the expando thing on the menus

	var el = document.getElementById(folder);

	if ( el.style.display == "none" ) {
		el.style.display = "block";
		img.src = minusImg.src;
		img.title = "-";
	}
	else {
		el.style.display = "none";
		img.src = plusImg.src;
		img.title = "+";
	}

	resizeBan ();

}

function getWindowHeight() {

    // makes the FireFox content div scroll correctly
    if(window.innerHeight)
            return window.innerHeight;
          
    
    if(window.document.documentElement.clientHeight)
            return document.documentElement.clientHeight;
    
    return window.document.body.clientHeight;
}

function getWindowWidth() {

    // makes the FireFox content div scroll correctly
    if(window.innerWidth)
            return window.innerWidth;
            
    
    if(window.document.documentElement.clientWidth)
            return document.documentElement.clientWidth;
    
    return window.document.body.clientWidth;
}


function openLink ( linkItem ) {

	var item = document.getElementById( linkItem );
	try {
		item.click();
	}
	catch (e) {
		parent.right.location = item.href;
	}
}

function printContent () {

	try {

		if (!document.all || window.opera) {
			// IE has onBeforePrint event, no one else does

			set_to_print();
		}

		parent.right.focus();
		parent.right.print();

		if (!document.all || window.opera) {
			// IE has onAfterPrint event, no one else does
			if (window.opera) {
				// opera ignores the frame reload on print cancel
				resizeBan ();
			}
			else {
				// FF has dimensioning problems, so force a reload of the frame
				reset_form();
			}
		}

	} 
	catch (e) {}

}

function bookmarkContent () {

	try {
		var title = window.document.title;
		var contentURL = new String (window.location);
		var lastPos = contentURL.lastIndexOf ( "/" );
		var parentURL = new String (parent.window.location);

		contentURL = contentURL.substr ( lastPos + 1 );
		lastPos = parentURL.lastIndexOf ( "?" );
		if ( lastPos > 0 ) {
			parentURL = parentURL.substr ( 0, lastPos );
		}

		contentURL = parentURL + "?" + contentURL;
		
		if ( document.all ) {
			window.external.AddFavorite ( contentURL, title );
		}
		else if ( window.sidebar ) {
			top.sidebar.addPanel ( title, contentURL, "" );
		}

	}
	catch (e) {}
}

function syncContent () {
	// executes in the context of the right frame
	var contentURL = new String (window.location);
	var parentURL = new String (parent.left.location);
	var lastPos = parentURL.lastIndexOf ( "/" );
	
	contentURL = contentURL.substr ( lastPos + 1 );
	parentURL = parentURL.substr ( lastPos + 1 );
	
	if ( parentURL.toLowerCase() == "dbdochtmlindex.html" ) {
		parent.SetLocateURL ( contentURL );
		parent.left.location = "HTMLToC.html";
	}
	else {
		parent.left.findContentItem ( contentURL );
	}
}

function findContentItem ( contentURL ) {
	// executes in the context of the left frame
	var bookPos = contentURL.indexOf ( "#" );
	if ( bookPos > 0 )
		contentURL = contentURL.substr ( 0, bookPos );
	
	var arAnchors = document.getElementById("divToCContent").getElementsByTagName("A");
	
	for ( var i = 0; i < arAnchors.length ; i++ ) {
		if ( arAnchors[i].href.indexOf ( contentURL ) > 0 ) {
			var parentID = document.getElementById( arAnchors[i].parentNode.id );
			var foundItemID = parentID; // saved for later
			
			var blockID = null;
			var imageID = null;
			
			var passCount = 0;
			
			while ( parentID && parentID.id.substr ( 0, 4 ) == "span" ) {
				// really want expandCollapse, but that's a bit harder
				
				// the sections are contained in a div tag with no style, the parent div is 
				// the first parameter of the expandCollapse, the image of the previous sibling of this parent
				// div is the second parameter
				blockID = document.getElementById( parentID.parentNode.id );

				if ( !blockID.previousSibling || !blockID.previousSibling.firstChild || ( blockID.id.substr ( 0, 4 ) != "span" )) {
					if ( passCount == 0 ) {
						// top level item, so ensure that we click it out
						imageID = document.getElementById( parentID.previousSibling.firstChild.id );
						if ( parentID.style.display != "block" )
							expandCollapse ( parentID.id, imageID );
					}
					break;
				}
				passCount ++;

				imageID = document.getElementById( blockID.previousSibling.firstChild.id );
				
				if ( blockID.style.display != "block" )
					expandCollapse ( blockID.id, imageID );
				
				if ( blockID.parentNode ) {
					parentID = document.getElementById( blockID.id );
				}
				else {
					parentID = null;
				}
				
			}
			
			// want to scroll the found item into the top most position
			if ( window.opera ) {
				// no support for scrollIntoView in Opera, but Opera auto scrolls if the focus is set to 
				// a given element.  Note that this doesn't mean that the item is at the top of the
				// view port, only that it is someplace within it.  At least in Opera 8.5, they highlight the
				// item (nice touch).
				
				arAnchors[i].focus();
			}
			else {
				// FF when scrolling takes the literal approach of "put the requested element at 0,0"
				// not a bad approach, were it not for the leading images and indentation.  Pity there
				// wasn't agreement on what these methods would accomplish regardless of browser.
				
				arAnchors[i].scrollIntoView(true);
				arAnchors[i].focus();
			}
			
			break;
		} 
	}
}