////////////////////////////////////////////////////////////////////////////////////////////////////
// GLOBAL JS VARIABLES
  var flashTestVersion = 9;     //set this to the version of flash that is required for the site
  var disableFlash     = false; //should only be set to true if testing site without flash
///////////////////////////////////////////////////////////////////////////////////////////////////

var navlock = ""; //for locking top nav buttons

function openPrinterFriendly(url){

           popWindow(url,'PrinterFriendly','760','600');

}

function alertPrint() {
  var whichPlatform = navigator.userAgent;
  if (whichPlatform.indexOf("mac") != -1){
	alert("Your browser does not support automatic printing. Please press command + P on your keyboard to print.");
	return true;
  } else {
	alert("Your browser does not support automatic printing. Please press control + P on your keyboard to print.");
	return true;
  }
}

function printPage(){
  if (window.print){
	window.print();
  } else {
	alertPrint();
	//setTimeout("goToURL();", 3000);
  }
}

function MM_findObj(n, d) { //v4.01
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=MM_findObj(n,d.layers[i].document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_swapImgRestore() { //v3.0
var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function changeNext(next, current, rowNum){
	var i = 0;
	for(i=0; i<2; i++)
	{	
		enableField(next[i]);
		next[i].checked = false;
	}

	document.getElementById("bgRow" + rowNum).className = "quiz_blue_mid";
	document["quizR" + rowNum].src = "images/quiz_blue_right.gif"; 	
	document["quizL" + rowNum].src = "images/quiz_blue_left.gif"; 	
}

function changeNext2(next, current, rowNum){
	var i = 0;
	for(i=0; i<2; i++)
	{	
		enableField(next[i]);
	}
	
	document.getElementById("bgRow" + rowNum).className = "quiz_blue_mid2";
	document["quizR" + rowNum].src = "images/quiz_blue_right.gif"; 	
	document["quizL" + rowNum].src = "images/quiz_blue_left.gif"; 	
}

function enableField(f){
	f.disabled = false;
}

function disableField(f){
	f.disabled = true;
}

function clearField(){
	//pass as many fields as you need
	for(var i=0; i<arguments.length; i++){
		arguments[i].value = "";
	}
}

function clearForm(formObj){ 
with (formObj) { 
  for (var i=0;i < elements.length;i++){ 
    fldObj = elements[i]; 
      if(fldObj.type == "checkbox" || fldObj.type == "radio"){ 
         fldObj.checked = false; 
      }
      if(fldObj.type == "hidden" || fldObj.type == "text" || fldObj.type=="password" || fldObj.type=="textarea"){ 
         fldObj.value = ""; 
      }
      if(fldObj.type == "select-one"){ 
         fldObj.selectedIndex = 0; 
      }
    } 
  } 
}

function m_showHideDiv(a) {
	if(d.getElementById(a).style.display=='block')
		m_hideDiv(a);
	else
		m_showDiv(a);
}

function m_showDiv(a) {
	d.getElementById(a).style.display='block';
}

function m_hideDiv(a) {
	d.getElementById(a).style.display='none';
}

function m_toggleLyr(lyrID,lyrState) // 1 visible, 0 hidden
{
   var obj = document.layers ? document.layers[lyrID] :
   document.getElementById ?  document.getElementById(lyrID).style :
   document.all[lyrID].style;
   obj.visibility = document.layers ? (lyrState ? "show" : "hide") :
   (lyrState ? "visible" : "hidden");
}

function dhtml_createDIVElement(newid,html){
	var newDiv = document.createElement("DIV");
	newDiv.id  = newid;
	document.body.appendChild(newDiv);
	newDiv.innerHTML = html;
}

function dhtml_removeDIVElement(id){
        var d = document.getElementById(id);
	d.parentNode.removeChild(d);
}

function so_testFlashVersion(reqver){
	if(disableFlash) return false; //for testing purposes only

	var version = deconcept.SWFObjectUtil.getPlayerVersion();
	var versionOK = false;
	if (document.getElementById && version["major"] >= reqver) {
		versionOK = true;
	}
	return versionOK;
}

function m_getpos(el) {
	//alert("page being resized" + obj);
    var left = el.offsetLeft;
    var top = el.offsetTop;
    if ( el.offsetParent && el.offsetParent.clientLeft || el.offsetParent.clientTop ) {
        left += el.offsetParent.clientLeft;
        top += el.offsetParent.clientTop;
    }
    while ( el = el.offsetParent ) {
        left += el.offsetLeft;
        top += el.offsetTop;
    }
    return { x:left, y:top };
}

function m_overlapDiv(el,elto, xadj, yadj){ //this function overlaps div(el) over div(elto) then adjusts by x/y

	var d = document.getElementById(elto);
	var pos = m_getpos(d);

	var x = (pos.x + xadj);
	var y = (pos.y + yadj);

		//alert('x='+x+', y='+y);

	document.getElementById(el).style.left = x+"px";
	document.getElementById(el).style.top = y+"px";
}

function changeWindowHash(u){
	//so URLs will be unique and can be bookmarked.

	if(window.top)
	  var wnd = window.top;
	else
	  var wnd = window;

	if(u==""){
		window.location.hash = "home";
	} else {

		if(u.indexOf('/')==-1){
			wnd.location.hash = u;
			//alert("changing hash to "+u);
		} else {
			var li   = u.lastIndexOf('/');
			var anch = u.substring(li+1,u.length);
			wnd.location.hash = anch;
			//alert("changing hash to "+anch);
		}
	}
}

function changeImages() {
	//alert(changeImages.arguments[0] + navlock);
	  if (document.images) 
	  {
	  	if(changeImages.arguments[0] != navlock)
	  	{
			for (var i=0; i<changeImages.arguments.length; i+=2) 
			{
				document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
			}
		}
	  }
}

function changeImagesOut() {
	//alert(navlock+ " " + changeImagesOut.arguments[1]);
	  if (document.images) {
	    if(navlock!=changeImagesOut.arguments[0]){
		for (var i=0; i<changeImagesOut.arguments.length; i+=2) {
			document[changeImagesOut.arguments[i]].src = changeImagesOut.arguments[i+1];
		}
	    }
	  }
}

function setNavLock(which,ovrImg){

	//alert(which + "|" +ovrImg);

	navlock = which;
	document[which].src = ovrImg;

	  unsetNavLock();
}

function setNavLock1(){
	setNavLock('nav01','/images/nav_home_on.gif');
}

function setNavLock2(){
	setNavLock('nav02','/images/nav_wilt_on.gif');
}

function setNavLock3(){
	setNavLock('nav03','/images/nav_do_on.gif');
}

function setNavLock4(){
	setNavLock('nav04','/images/nav_cando_on.gif');
}

function setNavLock5(){
	setNavLock('nav05','/images/nav_signup_on.gif');
}


function setNavLock6(){
	setNavLock('nav06','/images/nav_spouses_on.gif');
}

function unsetNavLock(){
	if(navlock!="nav01") document["nav01"].src = '/images/nav_home.gif';
	if(navlock!="nav02") document["nav02"].src = '/images/nav_wilt.gif';
	if(navlock!="nav03") document["nav03"].src = '/images/nav_do.gif';
	if(navlock!="nav04") document["nav04"].src = '/images/nav_cando.gif';
	if(navlock!="nav05") document["nav05"].src = '/images/nav_signup.gif';
	if(navlock!="nav06") document["nav06"].src = '/images/nav_spouses.gif';
}


function unsetNavLockAll(){
	navlock = "";
	unsetNavLock();
}

function thisMovie(movieName) {
   var isIE = navigator.appName.indexOf("Microsoft") != -1;
   return (isIE) ? window[movieName] : document[movieName];
}

function closeTarget(){

	//try moving this to end of function once flash bugs are fixed. then retest ie6
	changeWindowHash("home");
	setNavLock1();

	//alert("debug 0");

	if (document.getElementById('iFrameCont')){
		dhtml_removeDIVElement('iFrameCont');  //first remove div from page if it exists!!!
	}

	//alert("debug 1");

	var versionOK = so_testFlashVersion(flashTestVersion);
	if(!versionOK || disableFlash) m_toggleLyr('iFramePng',0);

	//alert("debug 2");

	if(!disableFlash)
	  swfCallFunc('mainFlash','Close','#');

	//alert("debug 3");
}

function openWiltPage(){
	// flash js opener for wilt page
	openTarget("wilt.html"); 
	setNavLock2();
}

function openDoctorPage(){
	// flash js opener for doctor page
	openTarget("doctor.html"); 
	setNavLock4();
}

function openQuizPage(){
	// flash js opener for quiz page
	openTarget("lowt_quiz.html"); 
	setNavLock3();
}

function openSpousePage(){
	// flash js opener for spouse page
	openTarget("spouses.html"); 
	setNavLock6();
}

function openTarget(u){
	showMainNav(); //bullet-proofing, always show nav when this is called.

	//check for referral tracking from spouses page.
	//var testURL1 = document.URL;
	//var testURL2 = u;
	//if(testURL1.indexOf("spouses.html")!=-1 && testURL2.indexOf("signup.asp")!=-1)
	//	u += "?isfromspouses=1";

	if (document.getElementById('iFrameCont')){
		dhtml_removeDIVElement('iFrameCont');  //first remove div from page if it exists!!!
	}

	changeWindowHash(u);

	setTimeout(function() {openTargetMain(u);}, 500); //delay 1/2 sec before actually calling function
}

function openTargetMain(u){
	swfCallFunc('mainFlash','Open',u); //call function that will finally make call to flash movie
}


function clickHomeButton(){
	if (document.getElementById('iFrameCont')){
		dhtml_removeDIVElement('iFrameCont');  //first remove div from page if it exists!!!
	}

	var versionOK = so_testFlashVersion(flashTestVersion);
	if(!versionOK || disableFlash) m_toggleLyr('iFramePng',0);

	if(!disableFlash)
	  swfCallFunc('mainFlash','CloseHome','#'); //call function that will finally make call to flash movie

	changeWindowHash("home");
	setNavLock1();
}

function swfCallFunc(movieName,str,u){

   var versionOK = so_testFlashVersion(flashTestVersion); //check for our version of flash
   var ffBug     = getFFVersionBug();
   if(versionOK && !ffBug){ 	//if flash exists and we dont have buggy mac/ff, open or close animated background from flash
	if(str=="Open"){
	  thisMovie(movieName).asFunc("",u); //1st param not used, 2nd param is passed thru
	}
	if(str=="Close"){	//closing the popup layer
	  thisMovie(movieName).closePopup();
	}
	if(str=="CloseHome"){	//navigating back to home
	  thisMovie(movieName).homeClick();
	}

   } else {

	if(str=="Open"){

	  if(ffBug) renderBlankFlashContainer();
	  m_toggleLyr('iFramePng',1);  //for buggy ff, need to remove flash from div
	  ifr_render(u); //render iframe in newly created div element

	}
	if(str=="Close" || str=="CloseHome"){

	   flver = 2;

	  if(ffBug) renderMainFlashFF("yes",flver);  //for buggy ff, need to restart flash tell it to skipIntro
	  m_toggleLyr('iFramePng',0);
	  dhtml_removeDIVElement('iFrameCont');

	}
   }
}

function openThisDiv(passthru){
	//passthru is passed into flash and then back (2nd parameter)
	//alert("JS Call From Flash: passthru="+passthru);
	ifr_render(passthru); //render iframe in newly created div element
}

function repositionDHTML(){
	  //adjust layer positions on page
	if(document.getElementById('iFramePng'))  m_overlapDiv('iFramePng','flashcontainer', 33, 40);
	if(document.getElementById('iFrameCont')) m_overlapDiv('iFrameCont','flashcontainer', 33, 40);
}

function ifr_construct(u){
	//var htmlstr = '<iframe src="'+u+'" class="mainFrame" allowtransparency="true" scrolling="no" id="ifrmain" name="main" frameborder="0" width="908" height="461"></iframe>';
	var htmlstr = '<iframe src="blank.html" class="mainFrame" allowtransparency="true" scrolling="no" id="ifrmain" name="main" frameborder="0" width="908" height="461"></iframe>';
	//using function changeFrameUrl to change the href after iframe is built
	//alert(htmlstr);
	return htmlstr;
}

function ifr_render(u){
	  var htmlstr = ifr_construct(u); //build inner html for div
	  dhtml_createDIVElement('iFrameCont',htmlstr,u);
}

function dhtml_createDIVElement(newid,html,u){
        var d = document.getElementById(newid);
	if(d) dhtml_removeDIVElement(id);

	var newDiv = document.createElement("DIV");
	newDiv.id  = newid;
	document.body.appendChild(newDiv);
	repositionDHTML();
	  setTimeout(function() {dhtml_displayHTML(newDiv,html,u);}, 500); //needs pause here, otherwise we see content flash before repositioned
}

function dhtml_displayHTML(newDiv,html,u){
	newDiv.innerHTML = html;
 	changeFrameUrl('ifrmain',u);
}

function dhtml_removeDIVElement(id){
        var d = document.getElementById(id);
	if(d)
	  d.parentNode.removeChild(d);
}

function changeFrameUrl(id,url){
	if(document.getElementById){
		document.getElementById(id).src = url;
	}
	else if(window.frames){
		window.frames.id.location.href = url;
	}
	else{
		alert("Sorry, your browser does not support this action. Please try using a different browser.");
	}
}

function getFFVersion()
{
if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)){ //test for Firefox/x.x or Firefox x.x (ignoring remaining digits);
 var ffversion=new Number(RegExp.$1) // capture x.x portion and store as a number
 if (ffversion>=3)
  return 3;
 else if (ffversion>=2)
  return 2;
 else if (ffversion>=1)
  return 1;
}
else
 return 0;
}

function getFFVersionBug(){
	var ffBug = false;
	if(detectMacXFF()){
		/*
		var ffBugTest = getFFVersion();
		if(ffBugTest<3 && ffBugTest!=0){
			ffBug = true;
		}
		*/
		ffBug = true;
	}

	return ffBug;
}

function detectMacXFF(){
  var userAgent = navigator.userAgent.toLowerCase();
  if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1)
	return true;
  else
	return false;
}

function renderMainFlash(ver){
	// using "ver" variable for flash versioning:
	// ver = 1 - is older version of flash without spouses
	// ver = 2 - is newer version of flash with spouses
	// mika 12/23/2008

	//alert("running version "+ ver + " of flash ");

	var testdl = checkForDeepLink(); //check for deep-link
	var ffBug  = getFFVersionBug();  //check for buggy ff on a mac

	if(!ffBug){

		//alert("Not FF Bug");
	  if(ver==2)	
	   var so = new SWFObject("loader09.swf", "mainFlash", "970", "617", flashTestVersion, "#ffffff");
	  else
	   var so = new SWFObject("loader.swf", "mainFlash", "970", "617", flashTestVersion, "#ffffff");
	   so.addParam("wmode", "transparent");

	   if(testdl!="") so.addVariable("deepLinkClick","yes");
	   else so.addVariable("deepLinkClick","no");
	   
	   so.write("flashcontainer");

	} else { 

		//alert("Need to Fix FF Bug");

			// this code handles issues with 
			// Firefox on Mac, version 1 or 2.

		if(testdl!=""){

			//alert("renderBlankFlashContainer");

			renderBlankFlashContainer();

		} else {

			//alert("renderMainFlashFF");

			renderMainFlashFF("no",ver); //pass "yes" or "no" for skipIntro
		}
	}
}

function renderMainFlashFF(skipIntro,ver){
	  if(ver==2)
	   var so = new SWFObject("loader09.swf", "mainFlash", "970", "617", flashTestVersion, "#ffffff");
	  else
	   var so = new SWFObject("loader.swf", "mainFlash", "970", "617", flashTestVersion, "#ffffff");
	   so.addParam("wmode", "transparent");
	   so.addVariable("deepLinkClick","no");
	   so.addVariable("skipToHome",skipIntro);
	   so.write("flashcontainer");
}

function renderBlankFlashContainer(){
	document.getElementById("flashcontainer").innerHTML = "";
}

function showMainNav()
{
	m_toggleLyr("nav",1);
}

function checkForDeepLink()
{

	var du = window.location.hash;

	//deeplinking for iframes
	if(du.indexOf("#") != -1){

		var li = du.lastIndexOf('#');
		var dl = du.substring(li+1,du.length);

		if(dl.indexOf("home.html")==-1 && (dl.indexOf(".html")!=-1 || dl.indexOf(".asp")!=-1) ){ //this is a deep-link!

			return dl; //return the Deep-Link
		}
	}

	return "";
}
