var toolTipOn = false;
var DOMutils = {
	/* http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html */
	addEvent:function(obj, type, fn) {
		if (obj.addEventListener)
			obj.addEventListener(type, fn, false);
		else if (obj.attachEvent) {
			obj["e"+type+fn] = fn;
			obj[type+fn] = function() {obj["e"+type+fn](window.event);}
			obj.attachEvent("on"+type, obj[type+fn]);
		}
	},
	/* http://www.robertnyman.com/2005/11/07/the-ultimate-getelementsbyclassname/ */
	getElementsByClassName:function(oElm, strTagName, strClassName) {
	    var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
	    var arrReturnElements = new Array();
	    strClassName = strClassName.replace(/\-/g, "\\-");
	    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	    var oElement;
	    for(var i=0; i<arrElements.length; i++){
	        oElement = arrElements[i];      
	        if(oRegExp.test(oElement.className)){
	            arrReturnElements.push(oElement);
	        }   
	    }
	    return (arrReturnElements)
	}
}

window.onload = function() {
	/* Callouts

	if ( document.getElementById('callout') ) {
		document.getElementById('callout').innerHTML = '<span id="calloutSwap"><a href="#" onclick="javascript:checkPatientDisclaimerCookie();"><img src="' + imgPath +'inset_jackie.gif" alt="In Jackies Words" border="0"></a></span><br><a href="http://www.spfiles.com/pipeg-intron.pdf" target="_blank"><img src="' + imgPath +'btn_prescribing_info.gif" alt="Prescribing Information" border="0"></a><a href="#safetyInfo"><img src="' + imgPath +'btn_safety_info.gif" alt="Safety Information" border="0"></a><br><p align="center"><a href="#">See more stories</a><img src="' + imgPath +'callout_link_right_arrow.gif" width="10" height="10" alt="" border="0"></p>';
	}

	*/
	// Tooltips
	if ( document.getElementById('tooltip') ){
		theToolTip = document.all ? document.all["tooltip"] : document.getElementById ? document.getElementById("tooltip") : "";
		document.onmousemove = positionToolTip;
	}
	
	
	// External Links
	var els = DOMutils.getElementsByClassName(document,"a","external");
	for (var i=0, len=els.length; i<len; i++){
		DOMutils.addEvent(els[i],"click",function(e){
			if (confirm("Thank you for visiting pegintron.com.\n\nMerck does not review or control the content at the site to which this hyperlink connects;\ntherefore, this hyperlink does not constitute an endorsement by Merck of the content of any\nnon-Merck site.\n\nDo you wish to continue?") ){ return false; };
			if (e.preventDefault) e.preventDefault();
			else e.returnValue = false;
			if (e.stopPropagation) e.stopPropagation();
			else e.cancelBubble = true;
		});
	}
	
}

//('calloutSwap',' + mediaPath + '<%=sInset_name%><%=sInset_swf_suffix%>.swf\', 165, 124, \'callout\')
function swap(target, swfFile, width, height, name){
	document.getElementById(target).innerHTML = swfWriter(swfFile, width, height, name);
}

function checkCookieValue(name) {
		var name = name +"=";
		var value = "";
		var pairs = document.cookie.split(';');
		for(var i=0;i < pairs.length;i++) {
			var pair = pairs[i];
			while (pair.charAt(0)==' ') pair = pair.substring(1,pair.length);
			if (pair.indexOf(name) == 0) {
				value = pair.substring(name.length,pair.length);
				break;
			}
		}
		return value;
}

function checkPatientDisclaimerCookie(target, swfFile, width, height, name){
	if (document.cookie) {
		if (checkCookieValue('seenPatientDisclaimer')) swap(target,swfFile, width, height, name);
		else popUp('/peg/application?namespace=main&event=content_display&event_input=patient_disclaimer&target='+target+'&swfFile='+swfFile+'&width='+width+'&height='+height+'&name='+name, 320, 500);
	}	else popUp('/peg/application?namespace=main&event=content_display&event_input=patient_disclaimer&target='+target+'&swfFile='+swfFile+'&width='+width+'&height='+height+'&name='+name, 320, 500);
}

function checkPatientDisclaimerCookieRLS(){
	if (document.cookie) {
		if (checkCookieValue('seenPatientDisclaimer')) return 1;
		else return 0; 
	}	else return 0;  
}

function setPatientDisclaimerCookie(target,swfFile, width, height, name){
		opener.swap(target,swfFile, width, height, name);
		document.cookie = "seenPatientDisclaimer=true;";
}

function cookieSetter() { // Called from RLS Flash
		document.cookie = "seenPatientDisclaimer=true;";
}




// Used for the testimonial callouts
function swfWriter(swfFile, width, height, id, ver) {
if (isNaN(ver)) ver = 5;
var result = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' + ver + ',0,0,0" width="' + width + '" height="'+ height + '" id="' + id + '" align="" style="margin:0px;">\n';
result += '<param name=movie value="' + swfFile + '">\n';
result += '<param name=quality value=high>\n';
result += '<param name=bgcolor value=#FFFFFF>\n';
result += '<param name="wmode" value="transparent">\n';
result += '<embed src="' + swfFile + '" quality=high bgcolor=#FFFFFF  width="' + width +'" height="'+ height + '" name="' + id + '" align="" TYPE="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"  wmode="transparent"></embed></object>';
return result;
}

function showToolTip(text) {
document.getElementById('tooltip').innerHTML = text;
toolTipOn = true;
return false;
}


function positionToolTip(e){
if (toolTipOn){
	if (!e) var e = window.event;
	if (e.pageX || e.pageY)	{
		curX = e.pageX;
		curY = e.pageY;
	}	else if (e.clientX || e.clientY) {
		curX = e.clientX + document.body.scrollLeft;
		curY = e.clientY + document.body.scrollTop;
	}
	theToolTip.style.left = curX +"px";
	theToolTip.style.top = curY + 20 + "px";
	theToolTip.style.visibility = "visible";
	}
}

function hideToolTip() {
	toolTipOn=false;
	theToolTip.style.visibility="hidden";
}


function showhide(obj, lyr, action, imgId)
{
	var x = new getObj(lyr);
	setLyr(obj,lyr, imgId);
	x.style.visibility = action;
}

function setLyr(obj,lyr, imgId)
{
	var newX = findPosX(obj);
	var newY = findPosY(obj);
	var x = new getObj(lyr);

	x.style.top = newY + 'px';
	x.style.left = newX + 'px';
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		//if (navigator.userAgent.indexOf("MSIE") == -1) 	obj = obj.offsetParent;

		while (obj.offsetParent)
		{
			//alert ("parent=" + obj.offsetParent.tagName);
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	else if (obj.y) {
		curtop += obj.y;
	}
	return curtop;
}


function getObj(name)
{
 if (document.getElementById)
 {
	   this.obj = document.getElementById(name);
	   this.style = document.getElementById(name).style;
 }
 else if (document.all)
 {
	   this.obj = document.all[name];
	   this.style = document.all[name].style;
 }
 else if (document.layers)
 {
	   if (document.layers[name])
	   {
	   	this.obj = document.layers[name];
	   	this.style = document.layers[name];
	   }
	   else
	   {
	    this.obj = document.layers.testP.layers[name];
	    this.style = document.layers.testP.layers[name];
	   }
 }
}


/* PLUG-IN CHECK
----------------- */

var requiredVersion = 6;

var flash2Installed = false;
var flash3Installed = false;
var flash4Installed = false;
var flash5Installed = false;
var flash6Installed = false;
var flash7Installed = false;
var flash8Installed = false;
var flash9Installed = false;
var maxVersion = 9;
var actualVersion = 0;
var hasRightVersion = false;
var jsVersion = 1.0;

var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;

jsVersion = 1.1;

if(isIE && isWin){
  document.write('<SCR' + 'IPT LANGUAGE=VBScript\> \n');
  document.write('on error resume next \n');
  document.write('flash2Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.2"))) \n');
  document.write('flash3Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.3"))) \n');
  document.write('flash4Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.4"))) \n');
  document.write('flash5Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.5"))) \n');
  document.write('flash6Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.6"))) \n');
  document.write('flash7Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.7"))) \n');
  document.write('flash8Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.8"))) \n');
  document.write('flash9Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.9"))) \n');
  document.write('<\/SCR' + 'IPT\> \n');
}


function detectFlash() {
  if (navigator.plugins) {
    if (navigator.plugins["Shockwave Flash 2.0"]
        || navigator.plugins["Shockwave Flash"]) {

      var isVersion2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
      var flashDescription = navigator.plugins["Shockwave Flash" + isVersion2].description;

      var flashVersion = parseInt(flashDescription.substring(16));

      flash2Installed = flashVersion == 2;
      flash3Installed = flashVersion == 3;
      flash4Installed = flashVersion == 4;
      flash5Installed = flashVersion == 5;
      flash6Installed = flashVersion == 6;
      flash7Installed = flashVersion == 7;
      flash8Installed = flashVersion == 8;
      flash9Installed = flashVersion >= 9;
    }
  }

  for (var i = 2; i <= maxVersion; i++) {
    if (eval("flash" + i + "Installed") == true) actualVersion = i;
  }

  if(navigator.userAgent.indexOf("WebTV") != -1) actualVersion = 4;

  if (actualVersion >= requiredVersion) {
    hasRightVersion = true;
  }
}

detectFlash();


function runFlash(swfFile,width,height,id, ver) {
if (isNaN(ver)) ver = 5;
document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' + ver + ',0,0,0" width="' + width + '" height="'+ height + '" id="' + id + '" align="">\n');
document.write('<param name=movie value="' + swfFile + '">\n');
document.write('<param name=quality value=high>\n');
document.write('<param name=bgcolor value=#FFFFFF>\n');
document.write('<param name="wmode" VALUvalueE="transparent">\n');
document.write('<embed src="' + swfFile + '" quality=high bgcolor=#FFFFFF  width="' + width +'" height="'+ height + '" name="' + id + '" align="" TYPE="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"  wmode="transparent"></embed></object>');
}
/* PLUG-IN CHECK END */



function popUp(URL, height, width, attributes){
  wleft = (screen.width - width) / 2;
  wtop = (screen.height - height) / 2;
  var win = window.open(URL,
    '_blank',
    'width=' + width + ', height=' + height + ', ' +
    'left=' + wleft + ', top=' + wtop + ', ' + attributes	);
  // Just in case dimensions and position are ignored
  win.resizeTo(width, height);
  win.moveTo(wleft, wtop);
  win.focus();
}


function hcpCookieCheck (promptURL, height, width, hcpURL) {
	if (document.cookie) {
		if (checkCookieValue('hcp')) {
			var hcp = window.open(hcpURL);
			hcp.focus();
		} else window.location = promptURL;
	}
	else window.location = promptURL;
}

function hcpConfirm(answer, URL) {
	if (answer){
		document.cookie = "hcp=true;";
		window.open(URL);
	}
	else history.go(-1);
}

function bmhcpConfirm(answer, URL) {
	if (answer){
		document.cookie = "hcp=true;";
		document.location=URL;
	}
	else history.go(-1);
}

function UShcpCookieCheck(articleURL, cookieName) {
	if (document.cookie) {
		if (checkCookieValue(cookieName)) {
			var article = window.open(articleURL);
			article.focus();
		} else prepareUShcpArticle(articleURL, cookieName);
	}
	else prepareUShcpArticle(articleURL, cookieName);
}

function UShcpConfirm(answer, URL, cookieName) {
	if (answer){
		document.cookie = cookieName + '=true;';
		window.open(URL);
	}	else history.go(-1);
}

function prepareUShcpArticle(URL, cookieName){
	URL = URL.replace("event_input", "event__input");
	URL = URL.replace(/&/g, "_ampersand_");
	URL = URL.replace("event", "_event_");
	window.location="/peg/application?namespace=main&event=content_display&event_input=ushcp_prompt&url="+URL+"&c="+cookieName;
}

function linkOffSite(URL){
	URL = URL.replace("event_input", "event__input");
	URL = URL.replace(/&/g, "_ampersand_");
	URL = URL.replace("event", "_event_");
	window.location="/peg/application?namespace=leave_site&origin=template_lower.jsp&event=leave_site&url="+URL;
}

//               ('buttonsWrapper', this, 'diagnosis', 'stage');
function timeline(btnDiv, activeBtn, srcId, targetId){
	var btnDiv = document.getElementById(btnDiv);
	var imgs = btnDiv.getElementsByTagName("img");

	for( var i=0; i < imgs.length; i++ ) {
		// reset all
		imgs[i].src =  imgPath + 'tab_' + imgs[i].id + '_off.gif' ;
		
		if (imgs[i] == activeBtn) {
			imgs[i].src = imgPath + 'tab_'+imgs[i].id+'_on.gif';
		}
	}

	document.getElementById(targetId).innerHTML = document.getElementById(srcId).innerHTML;
}


/*  FLASH CHECK
*****************/
var requiredVersion = 7;

var flash2Installed = false;
var flash3Installed = false;
var flash4Installed = false;
var flash5Installed = false;
var flash6Installed = false;
var flash7Installed = false;
var flash8Installed = false;
var flash9Installed = false;
var maxVersion = 9;
var actualVersion = 0;
var hasRightVersion = false;
var jsVersion = 1.0;

var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;

jsVersion = 1.1;

if(isIE && isWin){
  document.write('<SCR' + 'IPT LANGUAGE=VBScript\> \n');
  document.write('on error resume next \n');
  document.write('flash2Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.2"))) \n');
  document.write('flash3Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.3"))) \n');
  document.write('flash4Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.4"))) \n');
  document.write('flash5Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.5"))) \n');
  document.write('flash6Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.6"))) \n');
  document.write('flash7Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.7"))) \n');
  document.write('flash8Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.8"))) \n');
  document.write('flash9Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.9"))) \n');
  document.write('<\/SCR' + 'IPT\> \n');
}


function detectFlash() {
  if (navigator.plugins) {
    if (navigator.plugins["Shockwave Flash 2.0"]
        || navigator.plugins["Shockwave Flash"]) {

      var isVersion2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
      var flashDescription = navigator.plugins["Shockwave Flash" + isVersion2].description;

      var flashVersion = parseInt(flashDescription.substring(16));

      flash2Installed = flashVersion == 2;
      flash3Installed = flashVersion == 3;
      flash4Installed = flashVersion == 4;
      flash5Installed = flashVersion == 5;
      flash6Installed = flashVersion == 6;
      flash7Installed = flashVersion == 7;
      flash8Installed = flashVersion == 8;
      flash9Installed = flashVersion >= 9;
    }
  }

  for (var i = 2; i <= maxVersion; i++) {
    if (eval("flash" + i + "Installed") == true) actualVersion = i;
  }

  if(navigator.userAgent.indexOf("WebTV") != -1) actualVersion = 4;

  if (actualVersion >= requiredVersion) {
    hasRightVersion = true;
  }
}

detectFlash();


function runFlash(swfFile,width,height,id, ver) {
if (isNaN(ver)) ver = 6;
document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' + ver + ',0,0,0" width="' + width + '" height="'+ height + '" id="' + id + '" align="">\n');
document.write('<param name=movie VALUE="' + swfFile + '">\n');
document.write('<param name=quality VALUE=high>\n');
document.write('<param name=bgcolor VALUE=#FFFFFF>\n');
document.write('<param name="wmode" VALUE="transparent">\n');
document.write('<embed src="' + swfFile + '" quality=high bgcolor=#FFFFFF  WIDTH="' + width +'" height="'+ height + '" name="' + id + '" align="" TYPE="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" wmode="transparent"></embed></object>');
}

function backToPatientSite(URL){
	if (window.opener && !window.opener.closed) {
		window.opener.location = URL;
		window.opener.focus();
		window.close();
	} else window.location = URL;
}

function openInPatientSite(URL){
	if (window.opener && !window.opener.closed) {
		window.opener.location = URL;
		window.opener.focus();
	} else window.location = URL;
}

function disableChoices(action, radioBtns) {
  for (var i=0; i<radioBtns.length; i++) {
      radioBtns[i].disabled = action;
      if (radioBtns[i].disabled) { radioBtns[i].checked = false; }
  }
}

function setOptinValues(theForm, bbdo_opt_in, optin_direct, optin_email) {
   if (theForm.bbdo_opt_in[1].checked) { disableChoices(true, theForm.bbdo_opt_in_preference) };
   if (bbdo_opt_in) return;
   if (optin_direct.indexOf("true")>=0 && optin_email.indexOf("true")>=0) {
     theForm.bbdo_opt_in_preference[0].checked=true;
     theForm.bbdo_opt_in[0].checked=true;
     return;
   }
   if (optin_direct.indexOf("false")>=0 && optin_email.indexOf("false")>=0) {
     theForm.bbdo_opt_in[1].checked=true;
     return;
   }
	 if (optin_direct.indexOf("true")>=0 ) {
     theForm.bbdo_opt_in_preference[2].checked=true;
     theForm.bbdo_opt_in[0].checked=true;
     return;
   }
   if (optin_email.indexOf("true")>=0 ) {
     theForm.bbdo_opt_in_preference[1].checked=true;
     theForm.bbdo_opt_in[0].checked=true;
     return;
   }
   if (optin_email.indexOf("false")>=0 )  { theForm.bbdo_opt_in[1].checked=true; }
   if (optin_direct.indexOf("false")>=0 ) { theForm.bbdo_opt_in[1].checked=true; }
}


function realStories(srcId, targetId, bgImgId){
	document.getElementById(targetId).innerHTML = document.getElementById(srcId).innerHTML;
	if(bgImgId == 'topics') document.getElementById(targetId).style.backgroundImage = "none";
	else document.getElementById(targetId).style.backgroundImage = "url(/framework/skins/default_default/images/rls_bg_" + bgImgId + ".gif)";
	document.getElementById(targetId).scrollTop = 0;
}

