//Get query parameter from URL.
function getQueryVariable(variable) {
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
			return pair[1];
		}
  	} 
}
//Open new window with page.
function pageUtilsOpenPrint() {	
	var location = window.location;
	var printURL = location.toString();
	var quest = new RegExp('\\?', "gi"); //Check to see if URL has other query parameters.
	//var pound = new RegExp('\\#', "gi");url.replace(pound, "");	
	if (printURL.lastIndexOf("#") != -1) {
		var tmp = printURL.lastIndexOf("#");
		printURL = printURL.substring(0,tmp);
	}	
	if (printURL.match(quest)) {
		printURL = printURL+"&mode=print";
	} else {
		printURL = printURL+"?mode=print";
	}
	var printWin = window.open(printURL,'printWin','width=850,height=625,top=45,left=45,scrollbars=yes,resizable');
	printWin.focus();	
}
//Hide elements on pop-up window.
function pageUtilsPrintPage() {
	var mode = getQueryVariable("mode");
	if (mode == "print") {
		//if popup print window then change the look of the page.
		//turn off navigation		
		var utilbar = document.getElementById('utilbar');
		if (utilbar != null ) {
			utilbar.style.display = "none";
		}
		var navbar = document.getElementById('navbar');
			navbar.style.display = "none";		
		var leftcol = document.getElementById('leftcol');
		if (leftcol != null) {
			leftcol.style.display = "none";
		}
		var pageutils = document.getElementById('pageutils');
			pageutils.style.display = "none";
		//alter the page margins.
		var mainbody = document.getElementById('mainbody');
			mainbody.style.width = "100%";
			mainbody.style.marginLeft = "-15px";
			mainbody.style.border = "none";
		var maincol = document.getElementById('maincol');
			maincol.style.paddingRight = "0px";
		//hide table select menu's in top right corner of pages.
		var tblSelect = document.getElementById('tbl-select');
		if (tblSelect != null ) { //for industry and strategic sales pages only
			tblSelect.style.display = "none";
		}
		//hide background ID formatting
		var divs = document.getElementsByTagName('div');
		for (var i = 0; i < divs.length; i++) {
			var tmp = divs[i];
			if (tmp.getAttribute('id') == 'background') {
				tmp.style.backgroundColor = "#ffffff";
				tmp.style.width = "100%";
			}
		}
		//add target to <a> tags
		pageUtilsAddTarget();
		//replace the breadcrumb p tag
		var bc = document.getElementById('breadcrumb');
		bc.innerHTML = "<a href='javascript:window.close();'>close window</a>";
		bc.style.marginBottom = "-15px";
		bc.style.textAlign = "right";
		//bc.style.marginRight = "-15px";
		var ftr = document.getElementById('page-footer');
		if (ftr != null) {
			ftr.style.width = "100%";
		}
		var rl = document.getElementById('related-links');
		if (rl != null ) { //for main section pages only
			rl.style.marginTop = "20px";
			rl.style.marginRight = "-10px";
		}
		print();		
	}
//end print function
}
//Alter the URL's to open in new window or turn off.
function pageUtilsAddTarget() {
	var aTags = document.getElementsByTagName("a");
	for (i = 0; i < aTags.length; i++) {
		var target = "";
		var href = aTags[i].getAttribute('href');
		var quest = new RegExp('\\?', "i");
		if (href != null ) {
			if (href.match('javascript') || href.match('#')) { 
				//do nothing
			} else if (href.match(quest) && href.match("show")) {
				href = href+"&mode=print";
				aTags[i].setAttribute('href',href);
			} else if (href.match(quest) && href.match('peoplesearch')) {
				target = "_new";
			} else {
				target = "_new";				
			}
			aTags[i].setAttribute('target',target);
		}
	}
	//turn off the select menus
	var selectTags = document.getElementsByTagName('select');
	for (i = 0; i < selectTags.length; i++) {			
		var onchange = selectTags[i].getAttribute('onhange');
		onchange = "";
		selectTags[i].setAttribute('onchange',onchange);
	}
}
//Add page to favorites.
function pageUtilsAddBookmark() {
	bookmarkurl= document.URL;
	bookmarktitle= document.title;
	if (document.all) {
		window.external.AddFavorite(bookmarkurl,bookmarktitle);
	} else {
		alert("Please use Control + D to set a bookmark for this page");
	}
}
