/****************************************************************************************
Copyright (c) 2007 Ethicon Endo-Surgery, Inc.(EES).

This software is the confidential and proprietary information of EES. ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only in accordance with the terms of the license agreement you entered into with EES.

VERSION 	1.1
AUTHOR		Doug Scamahorn

DATE       	NAME           	DESCRIPTON
11/27/2007 	Doug Scamahorn  Initial creation.
12/17/2007	Doug Scamahorn	Adjusted fncNewWindowListener function for final SWF application sizing. Adjusted centerNewWindow function to include Windows Task Bar height of 28 px in calculation of top positioning.

****************************************************************************************/
/* UTILITY SCRIPTS ---------------------------------------------------------------- */
//Add an event for onload
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}
//Add a class to an element
function addClass(element,value) {
	if (!element.className) {
		element.className = value;
	} else {
		newClassName = element.className;
		newClassName+= " ";
		newClassName+= value;
		element.className = newClassName;
	}
}
//Extend the DOM
function getElementsByClassName(name) {
	var results = new Array;
	var elems = document.getElementsByTagName("*");
	for (var i = 0; i < elems.length; i++) {
		if (elems[i].className.indexOf(name) != -1) {
			results[results.length] = elems[i];
		}
	}
	return results;
}
//Calculate window position
function centerNewWindow(width,height){
	//Gather screen details
	var vScreenWidth = screen.width;
	var vScreenHeight = screen.height;
	//Calcualate left and top position
	var vPosition = new Array();
	vPosition["left"] = (vScreenWidth-width)/2;
	vPosition["top"] = ((vScreenHeight-height)/2)-28;
	//Return the array
	return vPosition;
}
//Identify the file and target for the contents of the file
function grabFile(file, target) {
	var contentTarget = target;
	var request = getHTTPObject();
	if (request) {
		request.onreadystatechange = function() {
			if (request.readyState == 4) {
				if (request.status == 200 || request.status == 304) {
					contentTarget[0].innerHTML = request.responseText;
				}
			}
		};
		request.open("GET", file, true);
		request.send(null);
		return true;
	} else {
		return false;
	}
}
//Cross-browser AJAX XMLHttpRequest function
function getHTTPObject() {
	var xhr = false;
	if (window.XMLHttpRequest) {
			xhr = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
			try {
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
			xhr = false;
			}
		}
	}
	return xhr;
}
/* PRESENTATION SCRIPTS ---------------------------------------------------------------- */
//Add alternating colored rows to the DataGrids
function underlineMessageTableRows() {
	//Test for the existence of messages
	if (getElementsByClassName("messageList")=="") return false;
	//Gather page information
	var messages = getElementsByClassName("messageList");
	var rows = messages[0].getElementsByTagName("tr");
	//Loop through the rows
	for (var i=0; i<rows.length-1; i++) {
		//Get all the theads in the current row
		var theads = rows[i].getElementsByTagName("th");
		for (var k=0;k<theads.length;k++) {
			addClass(theads[k],"cellBorder");
		}
		//Get all the cells in the current row
		var cells = rows[i].getElementsByTagName("td");
		for (var j=0;j<cells.length;j++) {
			addClass(cells[j],"cellBorder");
		}
	}
}
//Insert the content for the Privacy, Terms, and Contact pages
function fncInsertPolicy(){
	//Gather page information
	var bodyID = document.getElementsByTagName("body");
	var pageID = bodyID[0].getAttribute("id");
	var contentTarget = getElementsByClassName("intro");
	switch(pageID) {
		case "privacyPolicy":
			grabFile("articles/Help00006PrivacyPolicy.htm", contentTarget);
			break;
		case "termsOfUse":
			grabFile("articles/Help00005TermsOfUse.htm", contentTarget);
			break;
		case "customerSupport":
			grabFile("articles/Help00007ContactUs.htm", contentTarget);
			break;
		case "launchPage":
			grabFile("LaunchIntro.htm", getElementsByClassName("intromsg"));
			grabFile("LaunchLearnMore.htm", getElementsByClassName("learnmore"));
			grabFile("LaunchContact.htm", getElementsByClassName("customerSupport"));
			break;
		default:
		  		return false;
	}
	grabFile("footerlinks.htm", getElementsByClassName("ftrLinks"));
	var replaces = navigator.systemLanguage;
	if(replaces==undefined)
	{
		replaces = navigator.language;
	}
	
	// Upper case the country code for the copyright file.  Production environment is case sensative.
	var localeSplit = new Array();
	localeSplit = replaces.toString().split('-');
	localeSplit[localeSplit.length - 1] = localeSplit[localeSplit.length - 1].toString().toUpperCase();
	replaces = localeSplit[0] + "_" + localeSplit[1];
	
	//replaces = (replaces.toString()).replace("-","_");
	//alert("replaces-patientcopyright_"+ replaces +".txt");
	grabFile("patientcopyright_"+ replaces +".txt", getElementsByClassName("copyright"));			
}
/* BEHAVIOR SCRIPTS ---------------------------------------------------------------- */
//Add onclick listeners to offsite links
function fncNewWindowListener() {
	//Create a variable to hold all the elements with the class externalLink
	var vOffsiteLinks = getElementsByClassName("offsiteLink");
	//Create a variable to hold all the elements with the class documentLink
	var vLoginLinks = getElementsByClassName("loginLink");
	//Loop through each of the external links
	for (var count=0;count<vOffsiteLinks.length;count++) {
		//For each, enable the click events
		vOffsiteLinks[count].onclick=function() {
			//Pass the function the URL and window size
			fncNewWindow(this.href,'width=800,height=600,channelmode=no,directories=no,fullscreen=no,left=50,location=yes,menubar=yes,resizable=yes,scrollbars=yes,status=no,titlebar=yes,toolbar=yes,top=50'); 
			return false;
		}
	}
	//Loop through each of the document links
	for (var count=0;count<vLoginLinks.length;count++) {
		//For each, enable the click events
		vLoginLinks[count].onclick=function() {
			var vWindowPosition = centerNewWindow(960,700);
			//Pass the function the URL and window size
			fncNewWindow(this.href,'width=960,height=700,left='+vWindowPosition["left"]+',top='+vWindowPosition["top"]+',titlebar=yes,channelmode=no,directories=no,fullscreen=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no'); 
			return false;
		}
	}
}

function bookmarkPage(title,url) {
	agt = navigator.userAgent.toLowerCase();
	if (window.sidebar) { // firefox
		window.sidebar.addPanel(title, url, "");
	} else if (window.external &&
	        (!document.createTextNode ||
	        typeof window.external.AddFavorite == "unknown")) {
		window.external.AddFavorite(url, title);
	} else if (agt.substr(agt.indexOf("opera") + 6, 1) < 9) {
		alert("We are unable to setup a bookmark automatically for you.\n\nPlease do so manually by pressing Crtl/Cmd+T.");
	} else if (agt.substr(agt.indexOf("opera") + 6, 1) >= 9) {
		alert("We are unable to setup a bookmark automatically for you.\n\nPlease do so manually by pressing Crtl/Cmd+D.");
	} else {
		alert("We are unable to setup a bookmark automatically for you.\n\nPlease do so manually with your browser's menu.");
	}
}

function bookmarkScript(){
	var pageLocation=location.href;
	var pageTitle=document.title;
	bookmarkPage(pageTitle, pageLocation);
}

//Open new window function
function fncNewWindow(winURL,winAttributes) {
	window.open(winURL,"",winAttributes);
	//Available window attributes - http://www.w3schools.com/htmldom/met_win_open.asp
	/*channelmode=yes,directories=yes,fullscreen=yes,height=pixels,left=pixels,location=yes,menubar=yes,resizable=yes,scrollbars=yes,status=yes,titlebar=yes,toolbar=yes,top=pixels,width=pixels*/
}
/* PAGE LOAD EVENT ---------------------------------------------------------------- */
//On Page Load Events
addLoadEvent(underlineMessageTableRows);
addLoadEvent(fncNewWindowListener);
addLoadEvent(fncInsertPolicy);