function sendBook(state)
{
	state = state == 'toBack' ? state : 'toFront';
	
	document.getElementById("flashContainer").className = state;
	
	if (state == "toFront"){
		// close any previously opened items
		closeAllOpenItems();
		
		document.getElementById("book").style.width = "711px";
		urchinTracker("/FlashBook");
	}
	else {
		if (document.getElementById("book"))
			document.getElementById("book").style.width = "350px";
	}
}

function initPage() {

	sendBook('toBack')
	
	// activate first item in def data list
	document.getElementsByTagName("dt")[0].className = "active";
	document.getElementsByTagName("dd")[0].style.display = "block";
	
	// assign click events to various elements on the page
	var dtList = document.getElementsByTagName('dt');
	dtList[0].onclick = closeItem;
	for (var i=1;i<dtList.length;i++)
	{
		dtList[i].onclick = openItem;
		dtList[i].onmouseover = hoverOn;
		dtList[i].onmouseout = hoverOff;
	}
	
	var spanList = document.getElementsByTagName('span');
	for (var i=0;i<spanList.length;i++)
	{
		if (spanList[i].className == 'learnMoreLink')
			spanList[i].onclick = expandInfoLists;
	}
	
	document.getElementById("getScore").onclick = showCalcLayer;
	document.getElementById("closeCalcLink").onclick = closePops;
	
	document.getElementById("openTipLink").onclick = showDYKLayer;
	document.getElementById("closeTipLink").onclick = closePops;
		
	document.getElementById("header").onclick = openBook;
	document.getElementById("readDisclaimer").onclick = showDisclaimer;
	
	document.getElementById("sexual").onclick = showTip;
	document.getElementById("mens").onclick = showTip;
	document.getElementById("couples").onclick = showTip;
	
	// load the flash
	loadFlash();
	
	// load the tips
	loadTips();
	
}

function loadFlash() {	
	if(document.getElementById("flashContainer")) {
		//write out the flash
		var so = new SWFObject("swf/pageTurnMain.swf", "book", "350", "677", "8", "#ffffff");
		so.addParam("allowScriptAccess", "sameDomain");
		so.addParam("wmode", "transparent");
		so.addVariable("swfPath", "swf/");
		so.write("flashContainer");	
 	}
}

function loadTips()
{
	var req = null; 
		
	if (window.XMLHttpRequest)
	{
		req = new XMLHttpRequest();
		if (req.overrideMimeType) 
			req.overrideMimeType('text/xml');
	} 
	else if (window.ActiveXObject) 
	{
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e)
		{
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	
	req.open("GET", "tips/tips.xml", true); 
	
	req.onreadystatechange = function()
	{ 
		dataHolder="Wait server...";
		if(req.readyState == 4)
		{
			if(req.status == 200)
			{
				// process a XML document here
				var oDomDoc = req.responseXML;
				var tipPreviewText = "";
						
				try {
					tipsTypes = new Array("sexual","mens","couples");
					
					for (i = 0; i < tipsTypes.length; i++){
					
						var tipCount = oDomDoc.getElementsByTagName(tipsTypes[i] + "Tip").length;
						var  tipToShow = Math.ceil(Math.random() * tipCount);
						var  tipText = oDomDoc.getElementsByTagName(tipsTypes[i] + "Tip")[tipToShow -1].firstChild.data;
						document.getElementById(tipsTypes[i] + "_copy").getElementsByTagName("p")[0].innerHTML = tipText;

						if (i == 0){
							tipPreviewText = tipText;
						}	
					}
				} 
				catch (e)
				{
					//alert(e.message+' and '+req.status);
					//xml file isn't formatted correctly
					tipPreviewText = "There are no available tips.";
					//hide the 'did u know' link
					document.getElementById("openTipLink").style.display = 'none';
				}
				
				// crop the preview text to 100 characters, 
				// appens a '...' and add it to the document.
				tipPreviewText = tipPreviewText.substring(0,100);
				tipPreviewText = tipPreviewText.replace(/(\s){1}(\S+)(.){1}$/,"...");
				document.getElementById("tipPreview").innerHTML = tipPreviewText;
			}	
		} 
	}; 
	
	req.send(null); 
}

function showTip (e){

	// close any open tips
	document.getElementById("sexual_copy").style.display = 'none';
	document.getElementById("mens_copy").style.display = 'none';
	document.getElementById("couples_copy").style.display = 'none';
	
	// display the selected tip
	var targ = getTarget(e,"LI");
	var id = targ.id;
	
	document.getElementById(id + "_copy").style.display = 'block';
}

function openBook() {
	
	if(navigator.appName.indexOf("Microsoft") != -1) 
		var flash = window.book;
	else 
		var flash = window.document.book
	
	flash.openBookFromJS();
}

function showDisclaimer(){
	document.getElementById('disclaimerText').style.display='block';
	window.scrollBy(0,150)
	urchinTracker("/Read_Disclaimer");
}

function hoverOn(e){
	var targ = getTarget(e,"DT");
	targ.className = "hoverOn";
}

function hoverOff(e){
	var targ = getTarget(e,"DT");
	targ.className = "hoverOff";
}

function openItem(e)
{
	// close any previously opened items
	closeAllOpenItems();

	//  get reference to clicked item
	var targ = getTarget(e,"DT");
	var nextSib = getSibling(targ,1);
	// Get ID for Urchin Web tracking
	var targid = targ.getAttribute("id");
	
	// set a couple of styles
	targ.className = "active";
	nextSib.style.display = 'block';
	
	//Write to Urchin Tracker
	urchinTracker("/" + targid);
	// assign/remove events for the clicked item
	
	targ.onmouseover = '';
	targ.onmouseout = '';
	targ.onclick = closeItem;
	
	if (document.getElementsByTagName("dt")[0].className == "inactive")
		document.getElementsByTagName("dt")[0].onclick = openItem
	
	// link to anchor at top of page
	backToTop();
	
}

function closeAllOpenItems (){
	// close any previously opened items
	var dts = document.getElementsByTagName("dt");
	var dds = document.getElementsByTagName("dd");

	for (var i = 0; i < dts.length; i++) 
	{
		dts[i].className = "";

		dts[i].onclick = openItem;
		dds[i].style.display = "none";

		// re-assign hover events
		dts[i].onmouseover = hoverOn;
		dts[i].onmouseout = hoverOff;
	}
}

function closeItem(e)
{
	//  get reference to clicked item
	var targ = getTarget(e,"DT");
	var nextSib = getSibling(targ,1);
	
	// set a coupla styles
	targ.className = "";
	nextSib.style.display = 'none';
	
	// assign/remove events for the clicked item
	targ.onmouseover = hoverOn;
	targ.onmouseout = hoverOff;
	targ.onclick = openItem;
}

function expandInfoLists(e)
{
	//  get reference to clicked item
	var targ = getTarget(e,"SPAN");
	var nextSib = getSibling(targ,1);
	
	// open the div
	nextSib.style.display = 'block';
	
	// hide the learn more link
	targ.style.display = 'none';
}

// gets a reference to the clicked item
function getTarget(e,element)
{
	if (!e) var e = window.event;
	if (e.target) var tg = e.target;
	else if (e.srcElement) var tg = e.srcElement;
	while (tg.nodeName != element) // Safari
		tg = tg.parentNode;
		
	// returns the item represented by var tg
	return tg;
}	

// find the next sibling
function getSibling(tg,nodeTypeInd)
{
	var nextSib = tg.nextSibling;
	while (nextSib.nodeType != nodeTypeInd)
	nextSib = nextSib.nextSibling;	
	
	return nextSib;
}

function showCalcLayer()
{
	document.getElementById('calcLayer').style.display='block';
	urchinTracker("/Calculator");
}

function showDYKLayer()
{
	document.getElementById('TipLayer').style.display='block';
	urchinTracker("/Did_You_Know");
}

function closePops()
{
	document.getElementById('calcLayer').style.display='none';
	document.getElementById('TipLayer').style.display='none';
}

function backToTop() {
    var x1 = x2 = x3 = 0;
    var y1 = y2 = y3 = 0;

    if (document.documentElement) {
        x1 = document.documentElement.scrollLeft || 0;
        y1 = document.documentElement.scrollTop || 0;
    }

    if (document.body) {
        x2 = document.body.scrollLeft || 0;
        y2 = document.body.scrollTop || 0;
    }

    x3 = window.scrollX || 0;
    y3 = window.scrollY || 0;

    var x = Math.max(x1, Math.max(x2, x3));
    var y = Math.max(y1, Math.max(y2, y3));

    window.scrollTo(Math.floor(x / 2), Math.floor(y / 2));

    if (x > 0 || y > 0) {
        window.setTimeout("backToTop()", 50);
    }
}

// calculate a score based on user selections and display the appropriate 
// text as a response. Displays an error if the user submits the form
// without providing any selections
function calcTotal()
{

	var score = 0;
	var q1Score = 0;
	var q2Score = 0;
	var q3Score = 0;
	var q4Score = 0;
	var msgCount = 0;
	var riskLabel = "";
	var riskTxt = "";
	
	var theForm = document.getElementById("calcForm");
	var questions = new Array("q1","q2","q3","q4");
	
	// question 1
	for (var i = 0; i < 7; i++)
	{
		if (theForm.q1[i].checked){
			q1Score = parseInt(theForm.q1[i].value) * .5;
			msgCount++;
		}
	}
			
	// question 2	
	for (var i = 0; i < 2; i++)
	{
		if (theForm.q2[i].checked){
			q2Score = parseInt(theForm.q2[i].value);
			msgCount++;
		}	
	}
			
	// question 3
	for (var i = 0; i < 2; i++)
	{
		if (theForm.q3[i].checked){
			q3Score = parseInt(theForm.q3[i].value) * (-1.5);
			msgCount++;
		}	
	}
	
	// question 4
	for (var i = 0; i < 2; i++)
	{
		if (theForm.q4[i].checked){
			q4Score = parseInt(theForm.q4[i].value) * 2;
			msgCount++;
		}	
	}
	
	if (msgCount == 4){
	
		score = q1Score + q2Score + q3Score + q4Score;

		if (score < 1.5){
			riskLabel = "Low risk: ";
			riskTxt = "less than 30% probability of having hypertension, hyperlipidemia, diabetes and coronary artery disease";
		}	
		if (score >= 1.5 && score <= 2.5){
			riskLabel = "Medium risk: ";
			riskTxt = "30-59% probability of having hypertension, hyperlipidemia, diabetes and coronary artery disease";
		}	
		if (score > 2.5){
			riskLabel = "High risk: ";
			riskTxt = " greater than 60% probability of having hypertension, hyperlipidemia, diabetes and coronary artery disease";
		}
		
		document.getElementById("span1").className = '';
		
	}
	else {
		riskLabel = "To calculate your risk, please answer all four of the above questions.";
		document.getElementById("span1").className = 'error';
	}
		
	if (document.getElementById("span1").hasChildNodes())
		document.getElementById("span1").removeChild(document.getElementById("span1").firstChild);
	
	if (document.getElementById("span2").hasChildNodes())
		document.getElementById("span2").removeChild(document.getElementById("span2").firstChild);
	
	document.getElementById("span1").appendChild(document.createTextNode(riskLabel));
	document.getElementById("span2").appendChild(document.createTextNode(riskTxt));
	
	document.getElementById("results").style.display = 'block';

	return false;
}









