/*******************************************************************************
FILE NAME    :global.js
DEPENDENCIES :none
********************************************************************************
____________________________ API DOCUMENTATION BEGIN ___________________________
````````````````````````````````````````````````````````````````````````````````
Functions used throughout consumer website.

````````````````````````````````````````````````````````````````````````````````
_____________________________ API DOCUMENTATION END ____________________________
*******************************************************************************/

//-- global variables/functionality begin ----------------------------------
flashEnabled = true; // enables (true) and disables (false) all flash on the site.
logoEnabled = false; //set flag for logo visibility on flash headers
//-- global variables/functionality end ------------------------------------

// menu
menus = new Array();

//CLASS CONSTRUCTOR-- creates an 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);
}

// finds object
function findObj(n, d) {
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

// toggles layers
function toggleLayers() {
  var i,p,v,obj,args=toggleLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'block':(v=='hide')?'none':v; }
    obj.display=v; }
}

// image preloader
function preloadImages() {
  var d=document; if(d.images){ if(!d.p) d.p=new Array();
    var i,j=d.p.length,a=preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.p[j]=new Image; d.p[j++].src=a[i];}}
}

// print page function
function printPage() {
	window.print();  
}

// FONT SIZE

function incrementTextSize() {
 if(getCookie("zolfs") == "font_large") setActiveStyleSheet("null");
 else if(getCookie("zolfs") == "font_medium") setActiveStyleSheet("font_large");
 else setActiveStyleSheet("font_medium");
}

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
 createCookie("zolfs", title, 30, "/vorinostat");
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function getFontSize() {
 var cookie = getCookie("zolfs");
 var title = cookie ? cookie : getPreferredStyleSheet();
 setActiveStyleSheet(title);
}

var cookie = getCookie("zolfs");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);

// TIP CHOOSER JS

var intFatigue = 0;
var intDiarrhea = 1;
var intNausea = 2;
var intTaste = 3;
var intDryMouth = 4;


var intCurrentTab = 0;
//var intWeekdaySelected = 1;

function objTip(text) {
	this.ticked = false;
	this.text = text;	
	}


var category0 = { 
		'Title' : 'Fatigue',
		'Summary' : 'Some patients describe fatigue as a total lack of energy.',
		'Intro' : 'Here are tips to help you manage fatigue (tiredness):',
		'includeOnCalendar' : false,
		'Tips' :	[
			new objTip('Allow rest periods during the day.'),
			new objTip('Talk with your doctor about an appropriate exercise program.'),
			new objTip('Eat a well-balanced diet and drink plenty of fluids.'),
			new objTip('Choose activities that match your energy level.'),
			new objTip('Sit down when you are bathing and drying off.'),
			new objTip('Plan your work and leisure activities for times when you have the most energy.'),
			new objTip('Do the most important things first.'),
			new objTip('Ask others for help with activities and tasks.'	)
		]
	}

var category1 = { 
		'Title' : 'Diarrhea',
		'Summary' : 'When you have diarrhea, food passes quickly through your body.  Your body does not have a chance to absorb nutrients and water from the food. Contact your doctor or nurse if the diarrhea is severe and lasts for more than 1 day.',
		'Intro' : 'Here are tips to help you manage diarrhea:',
		'includeOnCalendar' : false,
		'Tips' :	[
			new objTip('Drink plenty of fluids.'),
			new objTip('Eat small amounts of food throughout the day instead of 3 large meals.'),
			new objTip('Eat foods and drink liquids that contain sodium (salt) and potassium, such as broth, bananas, and peaches.'),
			new objTip('Other foods to try may include rice, noodles, potatoes, and white bread.'),
			new objTip('Avoid greasy, fatty, or fried foods.'),
			new objTip('Limit caffeinated foods and drinks, such as chocolate, coffee, tea, and some sodas.'),
			new objTip('Try to avoid milk and milk products.'),
			new objTip('Talk with your doctor or nurse about taking antidiarrheal medicine.')
		]
	}

var category2 = { 
		'Title' : 'Nausea',
		'Summary' : 'Nausea (feeling sick to your stomach) can keep your body from getting enough food and nutrients to stay healthy.',
		'Intro' : 'Here are tips to help you manage nausea:',
		'includeOnCalendar' : false,
		'Tips' :	[
			new objTip('Eat foods that are less likely to upset your stomach, such as toast, crackers, and applesauce.'),
			new objTip('Avoid foods that are greasy, sweet, or spicy, or have strong odors.'),
			new objTip('Eat small amounts of food often and slowly.'),
			new objTip('Eat before you get hungry.  Feeling hungry can make nausea worse.'),
			new objTip('Drink liquids slowly or suck on ice chips throughout the day.'),
			new objTip('Rest after meals.'),
			new objTip('Wear loose-fitting clothes.'),
			new objTip('Ask your doctor or nurse about antinausea medicines that may be right for you.')
		]
	}

var category3 = { 
		'Title' : 'Changes in your sense of taste',
		'Summary' : 'Some patients may notice a change in the way foods and drinks taste.',
		'Intro' : 'Here are tips to help you manage changes in your sense of taste:',
		'includeOnCalendar' : false,
		'Tips' :	[
			new objTip('Prepare and eat foods that look, smell, and taste good to you.'),
			new objTip('Use seasonings with lots of flavor, such as basil, oregano, or rosemary.'),
			new objTip('Visit your dentist to check for dental problems and talk about oral care during treatment.')
		]
	}

var category4 = { 
		'Title' : 'Dry mouth',
		'Summary' : 'Some patients may notice that they have a dry mouth.',
		'Intro' : 'Here are tips to help you manage dry mouth:',
		'includeOnCalendar' : false,
		'Tips' :	[
			new objTip('Drink fluids throughout the day to make swallowing and talking easier.'),
			new objTip('Eat soft, moist foods, such as pureed fruits and vegetables, smoothies, and ice pops.'),
			new objTip('Moisten your food with sauce, salad dressing, broth, or gravy.'),
			new objTip('Suck on hard candy, ice chips, or ice pops, or chew gum to increase saliva.'),
			new objTip('Use lip balms to keep your lips moisturized.'),
			new objTip('Ask your doctor or nurse about artificial saliva if your dry mouth is severe.')
		]
	}


var Categories = [category0, category1, category2, category3, category4];
Categories.weekdaySelected = 0; 
Categories.tipsChecked = function() {
	var intTotal = 0;
	for( var i = 0; i < Categories.length; i++ ) {
		var category = Categories[i];
		for( var j = 0; j < category.Tips.length; j++ ) {
			var tip = category.Tips[j];
			if(tip.ticked)
				intTotal += 1;			
			}
		}
	return intTotal;
	}


var browserIsIE = true;	// user's browser is Internet Explorer

function init() {

	if (window.addEventListener)
		browserIsIE = false;
	else
		browserIsIE = true;

	tabClicked(intFatigue);
	
	//set weekday click events
	var arTDs = document.getElementById('trWeekdayList').cells;
	for( var i = 0; i < arTDs.length; i++ ) {
		var span = arTDs[i];
		if(window.addEventListener){ 
			span.addEventListener('click', weekdayClicked, false);
			} 
		else { // IE
			//object.attachEvent('onmouseover', testevent);
			span.attachEvent('onclick', weekdayClicked);
			}
		}
	}

	
function tabClicked(tabNumber) {

	intCurrentTab = tabNumber;
	setTabsNeutral();
	setTabSelected(tabNumber);
	setTipList(tabNumber);
			
	}

function setTabsNeutral() {
	var tr = document.getElementById('trCalendarTabs');
	
	for( var i = 0 ; i < tr.cells.length ; i++ ) {
		var td = tr.cells[i];
		td.className = 'TabUnselected';
		}
	}

function setTabSelected(tabNumber) {
	var tr = document.getElementById('trCalendarTabs');
	var divCalBody = document.getElementById('divCalendarBody');
	var divSummary = document.getElementById('divSideEffectSummary');
	var divTipsIntro = document.getElementById('divTipsIntro');

	tr.cells[tabNumber].className= 'TabSelected';
		
	divSummary.innerHTML = Categories[tabNumber].Summary;
	divTipsIntro.innerHTML = Categories[tabNumber].Intro;
	
	}


function setTipList(tabNumber) {
	
	var ul = document.getElementById('ulTipList');
	removeAllChildNodes(ul);
	loadTipsForCategory(tabNumber, ul);
	}

	

function removeAllChildNodes(el) {
	while (el.hasChildNodes()) {
		el.removeChild(el.firstChild);
		}
	}

function loadTipsForCategory(tabNumber, elList) {
	
	for (var i =0; i < Categories[tabNumber].Tips.length; i++) {
		//alert( Categories[tabNumber].Tips[tip] );
		var li = document.createElement('LI');
		elList.appendChild(li);		
		li.innerHTML = Categories[tabNumber].Tips[i].text;
		li.index = i;
				
		if ( Categories[tabNumber].Tips[i].ticked == 1 )
			li.className = 'liCheckboxOn';
		else
			li.className = 'liCheckboxOff';
			
		addClickEvent(li);
		}
	}

function addClickEvent(li) {

	if( browserIsIE == false ){ 
		//object.addEventListener('mouseover', testevent, false);
		li.addEventListener('click', tipClicked, false);
		} 
	else { // IE
		//object.attachEvent('onmouseover', testevent);
		li.attachEvent('onclick', tipClicked);
		}
	}


function tipClicked(evt) {
	var li;
	if( browserIsIE )  //IE
		li = evt.srcElement;
	else				 // not IE
		li = evt.target;
		
	if( li.className == 'liCheckboxOff' ) {
		li.className = 'liCheckboxOn';
		Categories[intCurrentTab].Tips[li.index].ticked = true;
		}
	else {
		li.className = 'liCheckboxOff' ;
		Categories[intCurrentTab].Tips[li.index].ticked = false;
		}
		
	addTipsToTracker();
	refreshSideEffectsList();

	if (Categories.tipsChecked() > 0) 
		showHideBottomControls(true);
	else
		showHideBottomControls(false);
	}


function addTipsToTracker() {
	var blnTipsChecked = false;
	
	for( var i = 0; i < Categories[intCurrentTab].Tips.length; i++ ) {
		var tip = Categories[intCurrentTab].Tips[i];
		if ( tip.ticked == true ) {
			blnTipsChecked = true;
			Categories[intCurrentTab].includeOnCalendar = true;
			refreshSideEffectsList();
			showHideBottomControls(true);
			break;
		}
	}
	
	if(blnTipsChecked == false) {
		Categories[intCurrentTab].includeOnCalendar = false;
		refreshSideEffectsList();
		showHideBottomControls(false);
		}
	
	}
	


function refreshSideEffectsList() {
	var ul = document.getElementById('ulYourSideEffectList');
	removeAllChildNodes(ul);
	for (var i = 0; i < Categories.length; i++ ) {
		var category = Categories[i];
		if(category.includeOnCalendar == true) {

			var li = document.createElement('LI');
			li.className = 'liCheckboxOff';
			ul.appendChild(li);		
			li.innerHTML = category.Title;
			}
		}	
	}


function showHideBottomControls(displayNow) {
		
	if(displayNow == true) {
		document.getElementById('trYourSideEffectsList').style.display = '';
		document.getElementById('trChooseDayOfWeek').style.display = '';
		document.getElementById('trPrintButton').style.display = '';
		}
	else {
		document.getElementById('trYourSideEffectsList').style.display = 'none';
		document.getElementById('trChooseDayOfWeek').style.display = 'none';
		document.getElementById('trPrintButton').style.display = 'none';
		}
	}


function weekdayClicked(evt) {
	var td;
	if( evt.srcElement )  //IE
		td = evt.srcElement;
	else				 // not IE
		td = evt.target;
			
	var arTDs = document.getElementById('trWeekdayList').cells;

	showHideBottomControls(true);

	for( var i = 0; i < arTDs.length; i++ ) {
		if( arTDs[i].innerHTML == td.innerHTML ) {
			Categories.weekdaySelected = i;
			td.className = 'spnChooseDayOfWeek_on';
			}
		else {
			arTDs[i].className = 'spnChooseDayOfWeek_off';
			}
		}
	}

function printCalendar(argPage) {
	
	var winHeight = 620;
	var winWidth = 815;

	if( browserIsIE == false ) {
		 // NOT IE
		winHeight += 30;
		window.open(argPage, 'winCalendar','height=' + winHeight + ',width=' + winWidth + 'resizable=0,menubar=0,status=0,toolbar=0,titlebar=0');
		}
	else {
		// IE 
		window.showModalDialog(argPage, Categories, 'dialogWidth:815px; dialogHeight:620px; ');
		}

	}

//---END