//Add Event
function addEvent( obj, type, fn ) {
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
		EventCache.add(obj, type, fn);
	}
	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] );
		EventCache.add(obj, type, fn);
	}
	else {
		obj["on"+type] = obj["e"+type+fn];
	}
}

// Event Cache
var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler){
			listEvents.push(arguments);
		},
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				item[0][item[1]] = null;
			};
		}
	};
}();
addEvent(window,'unload',EventCache.flush);


// Print function
function printit(){
    if (window.print) {
        window.print() ;
    } else {
        alert('Please use the print feature of your browser.');
    }
} 

//Empty form fields
function clearText(objField){
    if (objField.defaultValue==objField.value)
    objField.value = ""
}
function resetText(objField){
    if (objField.value=="")
    objField.value = objField.defaultValue
}

	
	
/// Stripe table rows
 function hasClass(obj) {
    var result = false;
    if (obj.getAttributeNode("class") != null) {
        result = obj.getAttributeNode("class").value;
    }
    return result;
 }   

/// Stripe table rows
function stripe() {
   var even = false;
   var evenColor = arguments[1] ? arguments[1] : "#f1f1c0";
   var oddColor = arguments[2] ? arguments[2] : "#fff";
   var table = document.getElementById('newslist');
   if (! table) { return; }
   var tbodies = table.getElementsByTagName("tbody");
   for (var h = 0; h < tbodies.length; h++) {
     var trs = tbodies[h].getElementsByTagName("tr");
     for (var i = 0; i < trs.length; i++) {
    if (!hasClass(trs[i]) && ! trs[i].style.backgroundColor) {
         var tds = trs[i].getElementsByTagName("td");
         for (var j = 0; j < tds.length; j++) {
       
           var mytd = tds[j];
        if (! hasClass(mytd) && ! mytd.style.backgroundColor) {
       
	      mytd.style.backgroundColor = even ? evenColor : oddColor;
             
           }
         }
       }
       even =  ! even;
     }
   }
 }


/// Added for Glossary functionality
Array.prototype.inArray = function (value) {
	var i;
	for (i=0; i < this.length; i++) {
		if (this[i] === value) {
			return true;
		}
	}
	return false;
};



// instructional text for pages with glossary items
function loadGlossary(){
	var oDFNs=document.getElementsByTagName('dfn');
	if(oDFNs.length>0){
		var oGlossary=document.getElementById('glossary');
		var oInfoRoll=document.createElement('p');
		oInfoRoll.innerHTML="Roll over the orange words with a <span class=\"glossterm\">dotted</span> underline to view a definition.";
		
		oGlossary.appendChild(oInfoRoll);
		//var oInfoLink=document.createElement('p');
		//oInfoLink.innerHTML="A full listing of terms is available in the <a href=\"/glossary.asp\" title=\"Go to a full listing of glossary terms.\">glossary</a>.";
		//oGlossary.appendChild(oInfoLink);
	} 	
}



// blur/clear onmouseover images
function showClear() {	
	var clearlis = document.getElementById('symptomcomp').getElementsByTagName('li');
	var l=clearlis.length;
		for (i = 0; i < l; i++){
			clearlis[i].onmouseover = function(){this.style.backgroundPosition="-124px 4.4em"};
			clearlis[i].onmouseout = function(){this.style.backgroundPosition="0 4.4em"};
			}
	}


// Load on page load
addEvent(window,'load',loadGlossary);

// Swapping bg image above navigation for each section
function swapBG(section) {
	var images=new Array();
	images[0]="/img/nav-" + section + "-01.jpg";
	images[1]="/img/nav-" + section + "-02.jpg";
	images[2]="/img/nav-" + section + "-03.jpg";
	
	var i=Math.floor(Math.random()*images.length);
	document.getElementById('contentsubwrapper').style.backgroundImage = "url('" + images[i] + "')";
}

// creating separate function for each section so I can call using addEvent
function swapBGlearn() {
	swapBG('learn');
}
function swapBGunderstand() {
	swapBG('understand');
}
function swapBGseek() {
	swapBG('seek');
}
function swapBGlife() {
	swapBG('life');
}
function swapBGresource() {
	swapBG('resource');
}
function swapBGutility() {
	swapBG('utility');
}


// setMaxLength function from QuirksMode - www.quirksmode.org
function setMaxLength() {
	var x = document.getElementsByTagName('textarea');
	var counter = document.createElement('p');
	counter.className = 'characters';
	var getnote = document.getElementById('textnote');
	for (var i=0;i<x.length;i++) {
		if (x[i].getAttribute('maxlength')) {
			var counterClone = counter.cloneNode(true);
			counterClone.relatedElement = x[i];
			counterClone.innerHTML = 'Characters typed: <span>0<\/span>/'+x[i].getAttribute('maxlength');
			x[i].parentNode.insertBefore(counterClone,getnote.nextSibling);
			x[i].relatedElement = counterClone.getElementsByTagName('span')[0];

			x[i].onkeyup = x[i].onchange = checkMaxLength;
			x[i].onkeyup();
		}
	}
}

function checkMaxLength() {
	var maxLength = this.getAttribute('maxlength');
	var currentLength = this.value.length;
	if (currentLength > maxLength)
		this.relatedElement.className = 'toomuch';
	else
		this.relatedElement.className = '';
	this.relatedElement.firstChild.nodeValue = currentLength;
	// not innerHTML
}


/// Tell Others form validation
function ValTOInputs ()
{
	erroris = "";

	var email1 = document.getElementById('email-recip01').value;	
	if ((email1 == "") ||
		(email1.indexOf('@') == -1) ||
		(email1.indexOf('.') == -1)) {
		erroris += "\n     -  E-mail address of recipient(s)";
	}

	var email2 = document.getElementById('email').value;	
	if ((email2 == "") ||
		(email2.indexOf('@') == -1) ||
		(email2.indexOf('.') == -1)) {
		erroris += "\n     -  Your e-mail address";
	}

	if (erroris != "") {
		erroris ="_____________________________\n" +
		"You failed to correctly fill in your:\n" +
		erroris + "\n_____________________________" +
		"\nPlease re-enter and submit again.";
		alert(erroris);
		return false;
	}
	else return true;
}


/// Contact form validation
function ValInputs ()
{
	erroris = "";

	if (document.getElementById('firstName').value == "") {
		erroris += "\n     -  First Name";
	}

	if (document.getElementById('lastName').value == "") {
		erroris += "\n     -  Last Name";
	}
	
	if (document.getElementById('phone').value == "") {
		erroris += "\n     -  Phone Number";
	}
	
	var email1 = document.getElementById('email').value;	
	if ((email1 == "") ||
		(email1.indexOf('@') == -1) ||
		(email1.indexOf('.') == -1)) {
		erroris += "\n     -  E-mail address";
	}

	if (document.getElementById('country').value == "") {
		erroris += "\n     -  Country";
	}

	if (document.getElementById('request').value == "") {
		erroris += "\n     -  Your question/request";
	}

	if (erroris != "") {
		erroris ="_____________________________\n" +
		"You failed to correctly fill in your:\n" +
		erroris + "\n_____________________________" +
		"\nPlease re-enter and submit again.";
		alert(erroris);
		return false;
	}
	else return true;
}


/// Tell Your story validation
function ValTYSInputs ()
{
	erroris = "";

	if (document.getElementById('firstName').value == "") {
		erroris += "\n     -  First Name";
	}

	if (document.getElementById('lastName').value == "") {
		erroris += "\n     -  Last Name";
	}
	
	var email1 = document.getElementById('email').value;	
	if ((email1 == "") ||
		(email1.indexOf('@') == -1) ||
		(email1.indexOf('.') == -1)) {
		erroris += "\n     -  E-mail address";
	}

	if (document.getElementById('country').value == "") {
		erroris += "\n     -  Country";
	}

	if (document.getElementById('story').value == "") {
		erroris += "\n     -  Your story";
	}

	if (erroris != "") {
		erroris ="_____________________________\n" +
		"You failed to correctly fill in your:\n" +
		erroris + "\n_____________________________" +
		"\nPlease re-enter and submit again.";
		alert(erroris);
		return false;
	}
	else return true;
}

// AJAX for home page quiz
function loadurl(dest, whichnum) {
	if (whichnum != undefined) {
		if ((document.getElementById('q0' + whichnum + 't').checked==false) && (document.getElementById('q0' + whichnum + 'f').checked==false)) {			
			alert("Please select true or false");			
		}
		else {	
			try {
				xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {
			}			
			xmlhttp.onreadystatechange = triggered;
			xmlhttp.open("GET", dest);
			xmlhttp.send(null);
		}
	} else {	
			try {
				xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {
			}			
			xmlhttp.onreadystatechange = triggered;
			xmlhttp.open("GET", dest);
			xmlhttp.send(null);
	}
}
	
function triggered() {
	if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
		document.getElementById("quiz01").innerHTML = xmlhttp.responseText;
	}
}


// Lifestyle Assessment quiz functionality
function selectedAnswer(tabid) {
	var tabgroup = tabid.substring(0,tabid.length-1);
	var tabform = tabgroup.substring(0,tabgroup.length-1);
	
	var tabarr = new Array(); tabarr = document.getElementById(tabform + "frm").getElementsByTagName("div");
	
	for(var i=1; i < tabarr.length+1; i++)
	{
		document.getElementById(tabgroup + i).setAttribute('class', ' ');
  		document.getElementById(tabgroup + i).setAttribute('className', ' ');					
		document.getElementById(tabgroup + i + 'desc').style.display = 'none';
	}

	document.getElementById(tabid).setAttribute('class', 'selected');
	document.getElementById(tabid).setAttribute('className', 'selected');
	document.getElementById(tabid + 'desc').style.display = 'block';
}