/*******************************************************************************
FILE NAME    :global.js
DEPENDENCIES :browser.js
********************************************************************************
____________________________ API DOCUMENTATION BEGIN ___________________________
````````````````````````````````````````````````````````````````````````````````
Functions used throughout website.

````````````````````````````````````````````````````````````````````````````````
_____________________________ API DOCUMENTATION END ____________________________
*******************************************************************************/

//-- global variables and functionality begin ----------------------------------
pageLoaded = false; //set page loaded flag
homePage = false;
if(top.location.href != self.location.href) top.location.href = self.location.href; //prevent unauthorized framing
theURL = window.document.location.href;

//used for dialog functionality
maskInitialized = false;
leavingSiteInitialized = false;
pdfInitialized = false;
bridgeMessageInitialized = false;
legislationMessageInitialized = false;
recruitmentMessageInitialized = false;
h2H_bridgeMessageInitialized = false;
h2H_segmentMessageInitialized = false;
optinUpdatesInitialized = false;
viewRecruitment = false; 
windowResizeSet = false;
currentDialog = null; //used for dynamic dialog tracking
dialogEnabled = false;
dailogType = 0;
externalURL = "";
pdfURL = "";
linkTarget = 0;
//-- global variables and functionality end ------------------------------------

//------------------------------------------------------------------------------
// ROLLOVER FUNCTIONS BEGIN
//------------------------------------------------------------------------------

//FUNCTION-- preload images
function preloadImage(virtualName, filePath, fileName) {
 eval(virtualName +' = new Image()'); //create a new image object
 eval(virtualName +'.src = "' + filePath + fileName + '"');
}

//FUNCTION-- dynamically swaps one image (also used for rollovers in layers for Netscape 4)
function swapImageSingle(imageName, imageState, imageLayer) {
 if(gBrowser.ns4 && imageLayer!=null) eval('document.' + imageLayer + '.document.images["'+ imageName +'"].src =' + imageName + imageState + ".src"); //path to nested layer for Netscape 4
 else document.images[imageName].src = eval(imageName + imageState + ".src");
}

//FUNCTION-- sets rollover highlight
function setRollover(imageId, imageFilePath, imageFileName) {
 preloadImage(imageId+"_off", imageFilePath, imageFileName);
 preloadImage(imageId+"_on", imageFilePath, imageFileName);
 swapImageSingle(imageId, '_on', null);
}

//------------------------------------------------------------------------------
// ROLLOVER FUNCTIONS END
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// NEW BROWSER WINDOW FUNCTIONS BEGIN
//------------------------------------------------------------------------------

//FUNCTION-- creates a new browser window for links to other websites
function newWin(url) {
 if(url != null || url != "")
 {
  newWin1 = window.open(url,"suboxone_newwin",'directories=yes,location=yes,menubar=yes,resizable=yes,scrollbars=yes,status=no,titlebar=yes,toolbar=yes');
  if(newWin1 != null) newWin1.focus( );
 }
}

//FUNCTION-- creates a new browser window for md letter
function letterWin(url) {
 if(url != null || url != "")
 {
	var showToolBar = 0;
	if(gBrowser.ie && gBrowser.majorVersion > 6) showToolBar=1;
  letterWin1 = window.open(url,"suboxone_letterWin",'directories=no,location=no,menubar=yes,resizable=yes,scrollbars=yes,status=no,titlebar=yes,width=550,height=350,toolbar='+showToolBar);
  if(letterWin1 != null) letterWin1.focus( );
 }
}

//FUNCTION-- creates a new browser window for Practice Management Tool Kit (PMTK)
function pmtkWin(url) {
 if(url != null || url != "")
 {
  pmtkWin1 = window.open(url,"suboxone_pmtk",'directories=no,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,titlebar=no,toolbar=no');
  if(pmtkWin1 != null) pmtkWin1.focus( );
 }
}

//FUNCTION-- creates a new window for video
function videoWin(url) {
 if(url != null || url != "")
 {
  videoWin1 = window.open(url,'','directories=no,location=no,menubar=no,resizable=yes,scrollbars=no,status=no,titlebar=yes,width=435,height=390,toolbar=0');
  if(videoWin1 != null) videoWin1.focus( );
 }
}

//FUNCTION-- creates a new window for webinar bio
function videoHelpWin(url) {
 if(url != null || url != "")
 {
	var showToolBar = 0;
	if(gBrowser.ie && gBrowser.majorVersion > 6) showToolBar=1;
  videoHelpWin1 = window.open(url,'','directories=no,location=no,menubar=yes,resizable=yes,scrollbars=yes,status=no,titlebar=yes,width=575,height=325,toolbar='+showToolBar);
  if(videoHelpWin1 != null) videoHelpWin1.focus( );
 }
}

//FUNCTION-- creates a new window for webinars
function webinarWin(url) {
 if(url != null || url != "")
 {
  webinarWin1 = window.open(url,'','directories=no,location=no,menubar=no,resizable=yes,scrollbars=no,status=no,titlebar=yes,width=800,height=635,toolbar=0');
  if(webinarWin1 != null) webinarWin1.focus( );
 }
}

//FUNCTION-- creates a new window for interactive case studies
function caseStudyWin(url) {
 if(url != null || url != "")
 {
  caseStudyWin1 = window.open(url,'','directories=no,location=no,menubar=no,resizable=yes,scrollbars=no,status=no,titlebar=yes,width=900,height=635,toolbar=0');
  if(caseStudyWin1 != null) caseStudyWin1.focus( );
 }
}

//FUNCTION-- creates a new window for Community Outreach Toolkit
function cotkWin(url) {
 if(url != null || url != "")
 {
  cotkWin1 = window.open(url,'','directories=no,location=no,menubar=no,resizable=yes,scrollbars=no,status=no,titlebar=yes,width=996,height=635,toolbar=0');
  if(cotkWin1 != null) cotkWin1.focus( );
 }
}

//FUNCTION-- creates a new window for Screening Kit

function odskWin(url) {
 if(url != null || url != "")
 {
  odskWin1 = window.open(url,'','directories=no,location=no,menubar=no,resizable=yes,scrollbars=no,status=no,titlebar=yes,width=996,height=634,toolbar=0');
  if(odskWin1 != null) odskWin1.focus( );
 }
}

//FUNCTION-- creates a new window for hcp moa
function moaWin(url) {
 if(url != null || url != "")
 {
  moaWin1 = window.open(url,'','directories=no,location=no,menubar=no,resizable=yes,scrollbars=no,status=no,titlebar=yes,width=400,height=375,toolbar=0');
  if(moaWin1 != null) moaWin1.focus( );
 }
}

//FUNCTION-- creates a new window for webinar bio
function bioWin(url) {
 if(url != null || url != "")
 {
	var showToolBar = 0;
	if(gBrowser.ie && gBrowser.majorVersion > 6) showToolBar=1;
  bioWin1 = window.open(url,'','directories=no,location=no,menubar=yes,resizable=yes,scrollbars=yes,status=no,titlebar=yes,width=575,height=325,toolbar='+showToolBar);
  if(bioWin1 != null) bioWin1.focus( );
 }
}

//FUNCTION-- creates a popup window for pdfs
function newPDFWin(url) {
 if(!gBrowser.isMac) //if its not a Mac
 {
	popupWinFeatures = "directories=no,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,titlebar=yes,toolbar=no";
  window.open(url,"",popupWinFeatures);
 }
 else window.open(url); //mac fix for OS X
}

//FUNCTION-- launches popup window for Dependence Indentifier results
function viewDIResults(argURL) {
 newPDFWin(argURL);
}

//FUNCTION-- creates a mini popup browser window
function openEmailWin(argURL) {
 if(argURL != null || argURL != "")
 {
  var showToolBar = 0;
  if(gBrowser.ie && gBrowser.majorVersion > 6) showToolBar = 1; //if IE7 show tool bar
  openEmailWin1 = window.open(argURL, "po_email", 'directories=0,location=0,menubar=1,resizable=1,scrollbars=1,status=0,titlebar=1,toolbar='+showToolBar);
  if(openEmailWin1 != null) openEmailWin1.focus();
 }
 else alert("Error: Missing URL parameter");
}

//FUNCTION-- creates a mini popup browser window for glossary
function glossaryWin(url)
{
	var showToolBar = 0;
	if(gBrowser.ie && gBrowser.majorVersion > 6) showToolBar=1;
 glossaryWin1= window.open(url,"suboxone_glossary",'directories=no,location=no,menubar=1,resizable=yes,scrollbars=yes,status=no,titlebar=yes,width=690,height=275,toolbar='+showToolBar);
 if(glossaryWin1 != null) glossaryWin1.focus( );
}

//FUNCTION-- determines the target for loading a new href
function doLink() {
 switch(linkTarget)
 {
  case 1:
   linkTarget = 0;
   window.location.href = externalURL; //load into same window
   break;
	case 3:
   linkTarget = 0;
   newPDFWin(externalURL); //load pdf into popup window
   break;
  default:
   linkTarget = 0;
   newWin(externalURL); //load into popup window
 }
}

//FUNCTION-- sets cookie if user either clicks Cancel or Read More for the legislation dialog
function messageDone() {
 setLegislationMessage();
}

//FUNCTION-- creates a popup browser window
function sampleResults(argURL) {
 if(argURL != null || argURL != "")
 {
  var showToolBar = 0;
  if(gBrowser.ie && gBrowser.majorVersion > 6) showToolBar = 1; //if IE7 show tool bar
  sampleResultsWin = window.open(argURL, "sd", 'directories=0,location=0,menubar=1,resizable=1,scrollbars=1,status=0,titlebar=1,toolbar='+showToolBar);
  if(sampleResultsWin != null) sampleResultsWin.focus();
 }
 else alert("Error: Missing URL parameter");
}

//FUNCTION-- creates a popup browser window
function openDisclaimerWin(argURL) {
 if(argURL != null || argURL != "")
 {
  disclaimerWin = window.open(argURL, "disclaimer", 'directories=1,location=1,menubar=1,resizable=1,scrollbars=1,status=0,titlebar=1,toolbar=1');
  if(disclaimerWin != null) disclaimerWin.focus();
 }
 else alert("Error: Missing URL parameter");
}

//------------------------------------------------------------------------------
// NEW BROWSER WINDOW FUNCTIONS END
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// DIALOG AND MASKING BEGIN
//------------------------------------------------------------------------------

//FUNCTION-- moves dialog off screen
function moveOffScreen() {
 currentDialog.m_moveTo(0, -1000);
}

//FUNCTION-- sets dialog
function setDialog() {
 if(dialogEnabled) 
 {
  if(dailogType == 1)
  {
   mask01.objCss.height = 50;
   mask01.objCss.width = 50;
   mask01.m_setMask();
   pdfMessage.m_updateHeight();
   pdfMessage.m_positionObj(false);
  }
  else if(dailogType == 2)
  {
   mask01.objCss.height = 50;
   mask01.objCss.width = 50;
   mask01.m_setMask();
   leavingSite.m_updateHeight();
   if(theURL.indexOf("webinars/")>0 || theURL.indexOf("videos/")>0 || theURL.indexOf("case_study/")>0) {leavingSite.m_positionObj(false);}
	 else {leavingSite.m_positionObj(false);}
  }
  else if(dailogType == 3)
  {
   mask01.objCss.height = 50;
   mask01.objCss.width = 50;
   mask01.m_setMask();
   bridgeMessage.m_updateHeight();
   bridgeMessage.m_positionObj(false);
  }
  else if(dailogType == 4)
  {
   mask01.objCss.height = 50;
   mask01.objCss.width = 50;
   mask01.m_setMask();
   legislationMessage.m_updateHeight();
   legislationMessage.m_positionObj(false);
  }
  else if(dailogType == 5)
  {
   mask01.objCss.height = 50;
   mask01.objCss.width = 50;
   mask01.m_setMask();
   recruitmentMessage.m_updateHeight();
   recruitmentMessage.m_positionObj(false);
  }
  else if(dailogType == 8)
  {
   mask01.objCss.height = 50;
   mask01.objCss.width = 50;
   mask01.m_setMask();
   h2H_bridgeMessage.m_updateHeight();
   h2H_bridgeMessage.m_positionObj(false);
  }
  else if(dailogType == 9)
  {
   mask01.objCss.height = 50;
   mask01.objCss.width = 50;
   mask01.m_setMask();
   optinUpdates.m_updateHeight();
   optinUpdates.m_positionObj(false);
  }
  else if(dailogType == 10)
  {
   mask01.objCss.height = 50;
   mask01.objCss.width = 50;
   mask01.m_setMask();
   h2H_segmentMessage.m_updateHeight();
   h2H_segmentMessage.m_positionObj(false);
  }
  else {alert("Error: dailogType undefined");}
 }
}

//FUNCTION-- clears dialog and Mask
function clearDialogMask() {
 dialogEnabled = false;
 moveOffScreen();
 if(dailogType == 1) {mask01.m_setDisplay(2);}
 else if(dailogType == 2) {mask01.m_setDisplay(2);}
 else if(dailogType == 3) {mask01.m_setDisplay(2);}
 else if(dailogType == 4) {mask01.m_setDisplay(2);}
 else if(dailogType == 5) {mask01.m_setDisplay(2);}
 else if(dailogType == 8) {mask01.m_setDisplay(2);}
 else if(dailogType == 9) {mask01.m_setDisplay(2);}
 else if(dailogType == 10) {mask01.m_setDisplay(2);}
 else {alert("Error: dailogType undefined");}
}

//FUNCTION-- clears dialog only and retains mask (used when swapping mutliple dialogs when mask is visible)
function clearDialog() {
 dialogEnabled = false;
 moveOffScreen();
}

//FUNCTION-- creates a new window for optout
function optOutWin(url) {
 if(url != null || url != "")
 {
	var showToolBar = 0;
	if(gBrowser.ie && gBrowser.majorVersion > 6) showToolBar=1;
  optOutWin1 = window.open(url,'','directories=no,location=no,menubar=yes,resizable=yes,scrollbars=yes,status=no,titlebar=yes,width=725,height=325,toolbar='+showToolBar);
  if(optOutWin1 != null) optOutWin1.focus( );
 }
}

//------------------------------------------------------------------------------
// DIALOG AND MASKING END
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// PDF MESSAGE BEGIN
//------------------------------------------------------------------------------

function setPDFMessage(formObj) {
 if(formObj.checked) setCookie("me", 1, 183);
 else deleteCookie("me");
}

function isPDFMessage() {
 if(getCookie("me")) return true;
 else return false;
}

//------------------------------------------------------------------------------
// PDF MESSAGE END
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// LEGISLATION MESSAGE BEGIN
//------------------------------------------------------------------------------

function setLegislationMessage() {
 setCookie("lm", 1, null);
}

function isLegislationMessage() {
 if(getCookie("lm")) return false;
 else return true;
}

//------------------------------------------------------------------------------
// LEGISLATION MESSAGE END
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// RECRUITMENT MESSAGE BEGIN
//------------------------------------------------------------------------------

/*function setRecruitmentMessage() {
 setCookie("rm", 1, null);
}*/

/*function isRecruitmentMessage() {
 if(getCookie("rm")) return false;
 else return true;
}*/

//------------------------------------------------------------------------------
// RECRUITMENT MESSAGE END
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// CSS FUNCTIONS BEGIN
//------------------------------------------------------------------------------

//FUNCTION-- swap class
function swapClass(theId, theClass) {
 var objId = document.getElementById(theId);
 objId.className = theClass;
}

//------------------------------------------------------------------------------
// CSS FUNCTIONS END
//------------------------------------------------------------------------------

//FUNCTION-- enables linking to home page based on homePage flag
function goHome(url) {
 if(!homePage) window.location.href = url;
}

//FUNCTION-- determines if the browser used supports the print method
function doPrint() {
 if(!(gBrowser.isMac && (gBrowser.ie && gBrowser.majorVersion <=5 || gBrowser.ns && gBrowser.majorVersion <=4 || gBrowser.safari && gBrowser.majorVersion == 85))) window.print(); //supports print method
 else alert("Mac users press command+P");
}

//FUNCTION-- fixes png transparency in unsupported browsers
function setPng(argId, argClearEl, argClearBg, argImagePath, argSizingMethod) {
 if(document.all)
 {
  var targetEl = document.getElementById(argId);
  if(argClearEl) targetEl.innerHTML = "";
  if(argClearBg) targetEl.style.backgroundImage = "none";
  targetEl.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+argImagePath+"', sizingMethod='"+argSizingMethod+"')";
 }
}

//FUNCTION-- searches for specified URL parameter and returns its value
function getURLParameter(argParentDoc, argURLParam) {
 var theURL = "";
 var theURLParam = argURLParam + "=";
 var URLParameters = "";
 var parameterStartPos = "";
 var parameterEndPos = "";
	
 if(argParentDoc) {theURL = window.top.document.location.href;} //get top frameset URL
 else {theURL = window.document.location.href;} //get document URL

 if(theURL.indexOf("?")>0) {URLParameters = theURL.substring(theURL.indexOf("?")+1, theURL.length);} //strip parameters from URL
 else {return null;} //? not found in URL

 if(URLParameters.indexOf(theURLParam)>=0)
 {
  parameterStartPos = (URLParameters.indexOf(theURLParam)+theURLParam.length);
  if(URLParameters.indexOf("&", parameterStartPos)>0) 
 {
  parameterEndPos = URLParameters.indexOf("&", parameterStartPos);
  return URLParameters.substring(parameterStartPos, parameterEndPos);
 }
  else {return URLParameters.substring(parameterStartPos, URLParameters.length);}
 }
 else return null; //parameter not found
}

//FUNCTION-- adds onload events
function addOnLoadEvent(func) {
 var oldonload = window.onload;
 if(typeof window.onload != "function") {window.onload = func;}
 else {window.onload = function() {if(oldonload) {oldonload();} func();}}
}

//---END