/*******************************************************************************
____________________________ API DOCUMENTATION BEGIN ___________________________
````````````````````````````````````````````````````````````````````````````````
Variables and functions used throughout website.

````````````````````````````````````````````````````````````````````````````````
_____________________________ API DOCUMENTATION END ____________________________
*******************************************************************************/

//------------------------------------------------------------------------------
// VARIABLES BEGIN
//------------------------------------------------------------------------------

pageLoaded = false; //set page loaded flag
globalMetrics = null; //set metrics flag. Note: clearMetrics() must be called from the onclick event for cancel buttons in dynamic dialogs
theURL = window.document.location.href; //set window location

//------------------------------------------------------------------------------
// VARIABLES END
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// AUTO INVOKE FUNCTIONALITY BEGIN
//------------------------------------------------------------------------------

if(top.location.href != self.location.href) top.location.href = self.location.href; //prevent unauthorized framing

//------------------------------------------------------------------------------
// AUTO INVOKE FUNCTIONALITY END
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// NEW BROWSER WINDOW FUNCTIONS BEGIN
//------------------------------------------------------------------------------

//FUNCTION-- creates a new 3/4 browser window for links to other websites throughout site
function openExternalWin(argURL) {
 externalWin = window.open(argURL, null, 'toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1,width=600,height=500');
 if(externalWin != null) externalWin.focus();
 if(globalMetrics) doMetrics(globalMetrics); //metrics measuring
}

//------------------------------------------------------------------------------

//FUNCTION-- creates a new browser window for body diary
function openBodyDiaryWin(argURL) {
 var showToolBar = 0;
 if(gBrowser.ie && gBrowser.majorVersion > 6) showToolBar=1;
 bodyDiaryWin = window.open(argURL, "bodyDiary", 'scrollbars=1,location=0,statusbar=0,menubar=1,resizable=1,width=750,height=425,toolbar='+showToolBar);
 if(bodyDiaryWin != null) bodyDiaryWin.focus();
}

//------------------------------------------------------------------------------

//FUNCTION-- creates disclaimer window
function openDisclaimerWin(argURL) {
 var showToolBar = 0;
 if(gBrowser.ie && gBrowser.majorVersion > 6) showToolBar=1;
 disclaimerWin = window.open(argURL, null, 'scrollbars=1,location=0,statusbar=0,menubar=1,resizable=1,width=450,height=400,toolbar='+showToolBar);
 if(disclaimerWin != null) disclaimerWin.focus();
}

//------------------------------------------------------------------------------

//FUNCTION-- creates a popup window for pdfs
function newPDFWin(argURL) {
 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(argURL,"",popupWinFeatures);
 }
 else window.location.href = argURL; //mac fix for OS X
 if(globalMetrics) doMetrics(globalMetrics); //metrics measuring
}

//------------------------------------------------------------------------------

//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,"yaz_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-- launches facts and myths quiz
function launchQuiz(argURL) {
 var theScreenW = getScreenWidth();
 var leftPosition = (theScreenW/2)-220;
 quizWin = window.open(argURL,"viewQuiz",'directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,titlebar=yes,toolbar=no,width=440,height=505,top='+25+',left='+leftPosition);
 quizWin.focus();
}

//------------------------------------------------------------------------------

//FUNCTION-- creates a mini popup browser window for text message help
function textMessageHelpWin(url) {
 var showToolBar = 0;
 if(gBrowser.ie && gBrowser.majorVersion > 6) showToolBar=1;
 textMessageHelpWin1= window.open(url,"yaz_textMessageHelp",'directories=no,location=no,menubar=1,resizable=yes,scrollbars=yes,status=no,titlebar=yes,width=690,height=275,toolbar='+showToolBar);
 if(textMessageHelpWin1 != null) textMessageHelpWin1.focus();
}

//------------------------------------------------------------------------------
// NEW BROWSER WINDOW FUNCTIONS END
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// CSS FUNCTIONS BEGIN
//------------------------------------------------------------------------------

//FUNCTION-- sets CSS class
function setCssClass(argID, argClass) {
 document.getElementById(argID).className = argClass;
}

//------------------------------------------------------------------------------
// CSS FUNCTIONS END
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// ROLLOVER FUNCTIONS BEGIN
//------------------------------------------------------------------------------

//FUNCTION-- preload images
function preloadImage(argVirtualName, argFilePath, argFileName) {
 eval(argVirtualName +' = new Image()'); //create a new image object
 eval(argVirtualName +'.src = "' + argFilePath + argFileName + '"');
}

//------------------------------------------------------------------------------

//FUNCTION-- dynamically swaps one image
function swapImageSingle(argImageName, argImageState) {
 document.images[argImageName].src = eval(argImageName + argImageState + ".src");
}

//------------------------------------------------------------------------------

//FUNCTION-- sets rollover highlight
function setRollover(argID, argFilePath, argFileName) {
 preloadImage(argID +"_off", argFilePath, argFileName);
 preloadImage(argID +"_on", argFilePath, argFileName);
 swapImageSingle(argID, '_on');
}

//------------------------------------------------------------------------------
// ROLLOVER FUNCTIONS END
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// METRICS BEGIN
//------------------------------------------------------------------------------

//FUNCTION-- metrics logic
function metricsManager(argAction) {
 switch(parseInt(argAction))
 {
  case 1: 
    //tracks yaz_website_coupon.pdf
    tagAction("http://switch.atdmt.com/action/denbxs_YazCoupon_1/");
    break;
  case 2:    
    //tracks submit clicks on tools_support/e_reminders.jsp
    tagAction("http://switch.atdmt.com/action/denbxs_yazsignupforemailreminder_10/");
    break;
  case 3:    
    //tracks clicks for launching the quiz
    tagAction("http://switch.atdmt.com/action/denyaz_YazHTMLBirthControlQuiz_10/");
    break;
  case 4:    
    //tracks thankyou page within quiz
    tagAction("http://switch.atdmt.com/action/denyaz_YazHTMLBirthControlAnswers_10/");
    break;
  default:
    alert("metricsManager: NO MATCHES FOUND");
 }
}

metricsArr = new Array(); //create array to hold image objects

//FUNCTION-- tags action
function tagAction(argSrc) {
 var dateObj = Date().replace(/\:/g, ""); //create date and remove colons
 dateObj = dateObj.replace(/\s/g, ""); //remove white space from date
 var imageObj = new Image(); //create image object
 imageObj.src = argSrc + "&date=" + dateObj; //append time stamp to image src as parameter to avoid image caching
 metricsArr.push(imageObj); //store the image object in an array
}

//------------------------------------------------------------------------------
// METRICS END
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// FORM FUNCTIONS BEGIN
//------------------------------------------------------------------------------

function clearInputField(obj) {
 if(obj.value == "Search") obj.value = "";
}

//------------------------------------------------------------------------------
// FORM FUNCTIONS END
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// COOKIE FUNCTIONS BEGIN
//------------------------------------------------------------------------------

//FUNCTION-- sets a cookie - jsv 1.0
function setCookie(cookieName, cookieValue, expireDate) { 
 if(expireDate != null)
 {
  //set expiration date for cookie
	var expires = new Date( );                                   
	expires.setTime(expires.getTime( ) + (1000 * 60 * 60 * 24 * expireDate)); //days ahead
 }
 document.cookie = cookieName + "=" + escape(cookieValue) + "; path=/" + ((expireDate == null) ? ";" : "; expires=" + expires.toGMTString());
}

//FUNCTION-- retrieves a cookie - jsv 1.0
function getCookie(cookieName) {
 var cookieNameStr = cookieName + "=";               
 var dc = document.cookie;
	            
 if(dc.length > 0)
 {              
  var beginStr = dc.indexOf(cookieNameStr);       
  if(beginStr != -1) 
  {           
   beginStr += cookieNameStr.length;       
   var endStr = dc.indexOf(";", beginStr);
   if(endStr == -1) {endStr = dc.length;}
   return unescape(dc.substring(beginStr, endStr));
  } 
 }
 return null;
}

//FUNCTION-- deletes a cookie - jsv 1.0
function deleteCookie(cookieName) {
 document.cookie = cookieName + "=; expires=Thu, 01-Jan-70 00:00:01 GMT" + "; path=/";
}

//------------------------------------------------------------------------------
// COOKIE FUNCTIONS END
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// SCREEN FUNCTIONS BEGIN
//------------------------------------------------------------------------------

//FUNCTION-- returns screen height
function getScreenHeight() {
 return parseInt(screen.height);
}

//------------------------------------------------------------------------------

//FUNCTION-- returns screen width
function getScreenWidth() {
 return parseInt(screen.width);
}

//------------------------------------------------------------------------------

//FUNCTION-- returns height of working area of system's screen, excluding windows taskbar
function getScreenAvailHeight() {
 return parseInt(screen.availHeight);
}

//------------------------------------------------------------------------------

//FUNCTION-- returns width of working area of system's screen, excluding windows taskbar
function getScreenAvailWidth() {
 return parseInt(screen.availWidth);
}

//------------------------------------------------------------------------------
// SCREEN FUNCTIONS END
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// BROWSER WINDOW FUNCTIONS BEGIN
//------------------------------------------------------------------------------

//FUNCTION-- returns height of browser window
function getBrowserWindowHeight() {
 if(document.all) //ie
 {
	if(checkMode()) return document.documentElement.clientHeight;
	else return document.body.clientHeight;
 }
 else if(isScrollMax()) //mozilla
 {
	if(hasHorizontalScrollbar()) return window.innerHeight-getScrollbarOffSet();
	else return window.innerHeight;
 }
 else return window.innerHeight;
}

//------------------------------------------------------------------------------

//FUNCTION-- returns width of browser window
function getBrowserWindowWidth() {
 if(document.all) //ie
 {
	if(checkMode()) return document.documentElement.clientWidth;
	else return document.body.clientWidth;
 }
 else if(isScrollMax()) //mozilla
 {
	if(hasVerticalScrollbar()) return window.innerWidth-getScrollbarOffSet();
	else return window.innerWidth;
 }
 else if(gBrowser.safari && hasVerticalScrollbar()) //safari
 {
	return window.innerWidth - getScrollbarOffSet();
 }
 else return window.innerWidth;
}

//------------------------------------------------------------------------------
// BROWSER WINDOW FUNCTIONS END
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// BROWSER BODY/DOCUMENT FUNCTIONS BEGIN
//------------------------------------------------------------------------------

//FUNCTION-- returns height of body
function getBodyHeight() {
 if(document.all) //ie
 {
	if(checkMode()) return document.documentElement.scrollHeight;
	else return document.body.scrollHeight;
 }
 else if(isScrollMax()) //mozilla
 {
	var scrollOffSet = 0;
	if(hasHorizontalScrollbar()) scrollOffSet = getScrollbarOffSet();
	
	if(hasVerticalScrollbar())	return (window.innerHeight+window.scrollMaxY)-scrollOffSet;
  else return window.innerHeight-scrollOffSet;
 }
 else return document.body.scrollHeight;
}

//------------------------------------------------------------------------------

//FUNCTION-- returns width of body
function getBodyWidth() {
 if(document.all) //ie
 {
	if(checkMode()) return document.documentElement.scrollWidth;
	else return document.body.scrollWidth;
 }
 else if(isScrollMax()) //mozilla
 {
	var scrollOffSet = 0;
	if(hasVerticalScrollbar()) scrollOffSet = getScrollbarOffSet();
	
	if(hasHorizontalScrollbar())	return (window.innerWidth+window.scrollMaxX)-scrollOffSet;
  else return window.innerWidth-scrollOffSet;
 }
 else return document.body.scrollWidth;
}

//------------------------------------------------------------------------------
// BROWSER BODY/DOCUMENT FUNCTIONS END
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// BROWSER SCROLLBAR FUNCTIONS BEGIN
//------------------------------------------------------------------------------

//FUNCTION-- determines horizontal scrollbar existence
function hasHorizontalScrollbar() {
 if(!document.all && isScrollMax()) //mozilla
 {
  if(window.scrollMaxX > 0) return true; //has scrollbar
	else return false; //has no scrollbar
 }
 else {alert("Function not supported by browser"); return;}
}

//------------------------------------------------------------------------------

//FUNCTION-- determines vertical scrollbar existence
function hasVerticalScrollbar() {
 if(!document.all && isScrollMax()) //mozilla
 {
  if(window.scrollMaxY > 0) return true; //has scrollbar
	else return false; //has no scrollbar
 }
 else if(gBrowser.safari) //safari
 {
	if(window.innerWidth == getBodyWidth()) return false;
	else return true;
 }
 else {alert("Function not supported by browser"); return;}
}

//------------------------------------------------------------------------------

//FUNCTION-- checks if scrollMaxY and scrollMaxX properties are supported
function isScrollMax() {
 if(window.scrollMaxY >= 0 && window.scrollMaxX >= 0) return true;
 else return false;
}

//------------------------------------------------------------------------------

//FUNCTION-- returns vertical scroll position
function getVerticalScrollPosition() {
 if(window.pageYOffset) {return window.pageYOffset;}
 else if(document.documentElement && document.documentElement.scrollTop) {return document.documentElement.scrollTop;}
 else if(document.body) {return document.body.scrollTop;}
}

//------------------------------------------------------------------------------

//FUNCTION-- returns horizontal scroll position
function getHorizontalScrollPosition() {
 if(window.pageXOffset) {return window.pageXOffset;}
 else if(document.documentElement && document.documentElement.scrollLeft) {return document.documentElement.scrollLeft;}
 else if(document.body) {return document.body.scrollLeft;}
}

//------------------------------------------------------------------------------

//FUNCTION-- returns offset value equal to scrollbar width
function getScrollbarOffSet() {
 if(gBrowser.firefox)
 {
  if(gBrowser.isWin) return 17;
	else if(gBrowser.isMac) return 15;
 }
 else if(gBrowser.safari) {return 15;}
}

//------------------------------------------------------------------------------
// BROWSER SCROLLBAR FUNCTIONS END
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// BROWSER COMPATMODE FUNCTIONS BEGIN
//------------------------------------------------------------------------------

//FUNCTION-- checks document mode for IE
function checkMode() {
 if(document.all) {return (document.compatMode && document.compatMode != "BackCompat");}
 else {alert("Mode not detected");}
}

//------------------------------------------------------------------------------

//FUNCTION-- sets document mode for IE (documentElement or body based on DOCTYPE used)
function setMode() {
 if(document.all)
 {
	if(checkMode()) {DOCUMENTMODE = document.documentElement;}
	else {DOCUMENTMODE = document.body;}
 }
 else {alert("Unable to set mode");}
}

//------------------------------------------------------------------------------
// BROWSER COMPATMODE FUNCTIONS END
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// OBJECT POSITIONING FUNCTIONS BEGIN
//------------------------------------------------------------------------------

//FUNCTION-- returns left coordinates for object centering
function getObjectPositionLeft(argScreenWidth, argObjWidth) {
 if(argObjWidth < argScreenWidth)
 {
  var halfScreenWidth = parseInt(argScreenWidth/2);
  var halfObjWidth = parseInt(argObjWidth/2); 
  return ((halfScreenWidth-halfObjWidth)+getHorizontalScrollPosition());
 }
 else return 0;
}

//------------------------------------------------------------------------------

//FUNCTION-- returns top coordinates for object centering
function getObjectPositionTop(argScreenHeight, argObjHeight) {
 if(argObjHeight < argScreenHeight)
 {	
  var halfScreenHeight = parseInt(argScreenHeight/2);
  var halfObjHeight = parseInt(argObjHeight/2); 
  return ((halfScreenHeight-halfObjHeight)+getVerticalScrollPosition());
 }
 else return getVerticalScrollPosition();
}	

//------------------------------------------------------------------------------

//FUNCTION-- validate top position
function isValidatePositionTop(argTopPosition, argObjectHeight, argBrowserWindowHeight) {
 if(argTopPosition <= 0 || (argTopPosition+argObjectHeight) > argBrowserWindowHeight) return false;
 else return true;
}

//------------------------------------------------------------------------------

//FUNCTION-- validate left position
function isValidatePositionLeft(argLeftPosition, argObjectWidth, argBrowserWindowWidth) {
 if(argLeftPosition <= 0 || (argLeftPosition+argObjectWidth) > argBrowserWindowWidth) return false;
 else return true;
}

//------------------------------------------------------------------------------
// OBJECT POSITIONING FUNCTIONS END
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// MENU BEGIN
//------------------------------------------------------------------------------

menus = new Array(); 

//CLASS CONSTRUCTOR-- creates menu object with foundation properties
function menu(argID, argPage, argParentMenu, argSubMenu, argMenuOnClass) {
 this.id = argID;
 this.page = argPage;
 this.parentMenu = argParentMenu;   
 this.subMenu = argSubMenu; 
 this.menuOnClass = argMenuOnClass;

 menus.push(this);
}

//------------------------------------------------------------------------------

function setMenu() {
 var pageId = document.getElementById("leftColumnDiv").className; //set target class

 for(var j=0;j<menus.length;j++)
 {
  if(pageId == menus[j].page)
  {
   setCssClass(menus[j].id, menus[j].menuOnClass); //set selected state of menu for page

   //if menu has a subMenu then expand it
   if(menus[j].subMenu)
   {
    document.getElementById(menus[j].subMenu).style.display = "inline";
   }

   var parentMenu = menus[j].parentMenu;

   while(parentMenu)
   {
	  document.getElementById(parentMenu.subMenu).style.display = "inline"; //expand parent menu
	  setCssClass(parentMenu.id, parentMenu.menuOnClass); //set selected state of parent menu
	  parentMenu = parentMenu.parentMenu; //set flag for next parentMenu check
   }
   break;
  }
 } 
}

//------------------------------------------------------------------------------
// MENU END
//------------------------------------------------------------------------------

//FUNCTION-- process flash requests
function flashExternalInterfaceReceiver(argValue) { 
 switch(parseInt(argValue))
 {
  case 1: 
    window.location.href = "/consumer/safety/index.jsp";
    break;
  case 2:
    leavingSiteSavingsCardDialog.m_setResource("https://signup.triplefin.com/yaz/"); 
    //window.location.href = "/consumer/tools_support/try_yaz.jsp";
    break;
  case 3:
    //
    break;
  case 4: 
    window.location.href = "/consumer/tools_support/body_diary/index.jsp";
    break;
  default:
    alert("flashExternalInterfaceReceiver: NO MATCHES FOUND");
 }
}

//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+"')";
 }
}

//------------------------------------------------------------------------------
// DIALOG BEGIN
//------------------------------------------------------------------------------

pdfDialog = new Spawn_Object("pdfMessageDiv");
leavingDialog = new Spawn_Object("leavingSiteDiv");
leavingSiteSavingsCardDialog = new Spawn_Object("leavingSiteSavingsCardDiv");

//FUNCTION-- init dailogs
function initDialogs() {
 var grayMask = initMask("mask01", "/consumer/images/grayAlpha.png"); //initialize mask

 pdfDialog.m_initDialogProperties(grayMask, "pdf"); //init common dialog properties [mask=mask object or null | dialogType=integer]
 leavingDialog.m_initDialogProperties(grayMask, "siteExit"); //init common dialog properties [mask=mask object or null | dialogType=integer]
 leavingSiteSavingsCardDialog.m_initDialogProperties(grayMask, "siteExit"); //init common dialog properties [mask=mask object or null | dialogType=integer]

 activeDialog = null;
 window.onresize = adjustDialog;
}

addOnLoadEvent(initDialogs);

//------------------------------------------------------------------------------
// DIALOG END
//------------------------------------------------------------------------------