/*********************************************************************************
 Global variables
*********************************************************************************/
//Default browsercheck
function browsercheck(){
    this.ver=navigator.appVersion
    this.agent=navigator.userAgent
    this.dom=document.getElementById?1:0;
    this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom)?1:0;
    this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom)?1:0;
    this.ie4=(document.all && !this.dom)?1:0;
    this.ie=this.ie4||this.ie5||this.ie6
    this.mac=this.agent.indexOf("Mac")>-1
    this.opera5=this.agent.indexOf("Opera 5")>-1
    this.ns6=(this.dom && parseInt(this.ver) >= 5) ?1:0;
    this.ns4=(document.layers && !this.dom)?1:0;
    this.bw=(this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.opera5 || this.dom)
    //this.bw=document.getElementById?1:0;
    return this
}
var bw=browsercheck();

window.name = "MainWin";

var PageImages = new Array();
var PageAltImages = new Array();
var nest = "";
var loaded = 0;


/*********************************************************************************
Images Preload
*********************************************************************************/

//Kevin suggests rewritting to use ~getElemntByType, or something along those lines
//Re-write for -on and -off??
function LoadActiveImages() {
	var num = document.images.length;
	for (var i = 0; i < num; i++) {
		var thisImage = document.images[i];
		var thisImageSrc = thisImage.src;
		var imageName = thisImageSrc.substring(0,thisImageSrc.lastIndexOf("_o"));
		//document.write(imageName + "<BR>");
		if (thisImageSrc.lastIndexOf("_off") != -1) {
			PageImages[i] = new Image(thisImage.width, thisImage.height);
      	    PageImages[i].src = imageName + "_on.jpg";
		} else if (thisImage.src.lastIndexOf("_on") != -1) {
			PageAltImages[i] = new Image(thisImage.width, thisImage.height);
      	    PageAltImages[i].src = imageName + "_off.jpg";
		}
    }
}

window.onload = LoadActiveImages;
	
/*********************************************************************************
Image Swap
*********************************************************************************/
function imageSwap(which, state, type) {
     	
	//which - required input of image name
	//state - required input of image rollover state (1 for on, 0 for off)
	//div1,div2,... - optional input layer hierarchy for Netscape 4.0 browsers
	var divNest=''
	var argLength = arguments.length
	
	//loop over the arguments, to check for any passed div id's, and set the nesting
	//used for NN4 only
	if (argLength>2) {
		for (var i = 2; i < argLength; i++)  
		divNest=divNest+'document.'+arguments[i]+'.';
	}
	
	//get the element obj reference
 bw.dom?thisImage=document.getElementById(which):bw.ie4?document.all[which]:thisImage=eval(divNest+"document[which]");
	
	//find the image src name
	var imageName = thisImage.src.substring(0,thisImage.src.lastIndexOf("_o"));
	//toggle the src name based on state
	if (type == 'jpg') {
		state == 0?endString = "_off.jpg":endString = "_on.jpg";
	} else {
		state == 0?endString = "_off.gif":endString = "_on.gif";
	}
	//alert(layerOver);
	//set the image src to the new src
	bw.dom?thisImage.setAttribute("src",imageName + endString):thisImage.src=imageName + endString;
}

function imageSwapTab(which, state) {
     	
	//which - required input of image name
	//state - required input of image rollover state (1 for on, 0 for off)
	//div1,div2,... - optional input layer hierarchy for Netscape 4.0 browsers
	var divNest=''
	var argLength = arguments.length
	
	//loop over the arguments, to check for any passed div id's, and set the nesting
	if (argLength>2) {
		for (var i = 2; i < argLength; i++)  
		divNest=divNest+'document.'+arguments[i]+'.';
	}
	
	//get the element obj reference
 bw.dom?thisImage=document.getElementById(which):bw.ie4?document.all[which]:thisImage=eval(divNest+"document[which]");
	
	//find the image src name
	var imageName = thisImage.src.substring(0,thisImage.src.lastIndexOf("_o"));
	var hereState = thisImage.src.indexOf("_hr");
	//alert(thisImage.src);
	if (hereState == -1) { 
		//toggle the src name based on state
		state == 0?endString = "_off.gif":endString = "_on.gif";
		//set the image src to the new src	
		bw.dom?thisImage.setAttribute("src",imageName + endString):thisImage.src=imageName + endString;
	}
}

/*********************************************************************************
Window Functions
*********************************************************************************/	

// suppress errors
// uncomment after debugging is complete
/*function stopError() {
	return true;
}
window.onerror = stopError;
*/

if (window.focus) {
	self.focus();
}

function popWindow(URL,name,widgets) {
	popwin = window.open(URL,name,widgets);
	popwin.focus();	
	popwin.opener = self;
	popwin.name = name;
	// wrap in an if statement; don't rename if window.name already exists
	window.name = "MainWin";			
}
	
function closeWin(){
	window.close()
}

function printPage(lang) {
  if (window.print) {
    window.print() 
  } else if (lang == "fr/") {
    alert("Sorry, your browser doesn't support this feature. [FR] \nUse the print option on your browser to print this page"); 
  } else if (lang == "en/") {
    alert("Sorry, your browser doesn't support this feature. \nUse the print option on your browser to print this page"); 
  } else {
  	alert("Sorry, your browser doesn't support this feature. \nUse the print option on your browser to print this page"); 
  }
}

/**************************************************************************************
show and hide a div
**************************************************************************************/
function getStyleObject(strId) {
	//alert(strId);
	if (document.getElementById && document.getElementById(strId)) {
		return document.getElementById(strId).style;
	} else if (document.all && document.all(strId)) {
		return document.all(strId).style;
	} else {
		return false;
	}
}
	
function fnShow(strDivId) {
	//alert(strDivId);
	var objStyle = document.getElementById(strDivId);
	//alert(objStyle);
	objStyle.style.display = "block"; 
}

function fnHide(strDivId) {
	var objStyle = document.getElementById(strDivId);
	objStyle.style.display = "none"
}

function toggleImg(imgId, imgFileName, srcFolder) {
	// *** Note: srcFolder is optional. It can be included if the image to be swapped is not located in /images/ 
	// (i.e. /images/en)
	var argLength = arguments.length;
	var imgFolder = "/images/";	
	if (argLength > 2) {
		imgFolder = imgFolder + srcFolder + '/';
	}
	var strFileExtension = ".gif";
	//alert(imgFolder + imgFileName + strFileExtension);
	document.getElementById(imgId).src = imgFolder + imgFileName + strFileExtension;
}

function getElementbyClass(className){
	classCollection=new Array()
	var inc=0
	var alltags = document.getElementsByTagName("*")
	for (i=0; i<alltags.length; i++){
		if (alltags[i].className==className)
		classCollection[inc++]=alltags[i]
	}
}


function fnToggleVis(targetId, targetIdImg) {
	if (document.getElementById) {
		target = document.getElementById ( targetId );
		div1 = document.getElementById ( 'detectionDiv' );
		div2 = document.getElementById ( 'diagnosisDiv' );
		div3 = document.getElementById ( 'treatmentDiv' );
		div4 = document.getElementById ( 'recoveryDiv' );
		div5 = document.getElementById ( 'survivorshipDiv' );
		
		div1.style.display = "none";		
		div2.style.display = "none";
		div3.style.display = "none";
		div4.style.display = "none";
		div5.style.display = "none";
		document.images['tabDetection'].src = 'images/en/tabDetection_off.gif';
		document.images['tabDiagnosis'].src = 'images/en/tabDiagnosis_off.gif';
		document.images['tabTreatment'].src = 'images/en/tabTreatment_off.gif';
		document.images['tabRecovery'].src = 'images/en/tabRecovery_off.gif';
		document.images['tabSurvivorship'].src = 'images/en/tabSurvivorship_off.gif';
		
		
		var thisImage=document.getElementById(targetIdImg)
		var imageName = thisImage.src.substring(0,thisImage.src.lastIndexOf("_o"));
		bw.dom?thisImage.setAttribute("src",imageName + "_hr.gif"):thisImage.src=imageName + "_hr.gif";
		
		target.style.display = "block";							
			
	}
}

function fnToggleVisFr(targetId, targetIdImg) {
	if (document.getElementById) {
		target = document.getElementById ( targetId );
		div1 = document.getElementById ( 'detectionDiv' );
		div2 = document.getElementById ( 'diagnosisDiv' );
		div3 = document.getElementById ( 'treatmentDiv' );
		div4 = document.getElementById ( 'recoveryDiv' );
		div5 = document.getElementById ( 'survivorshipDiv' );
		
		div1.style.display = "none";		
		div2.style.display = "none";
		div3.style.display = "none";
		div4.style.display = "none";
		div5.style.display = "none";
		document.images['tabDetection'].src = 'images/fr/tabDetection_off.gif';
		document.images['tabDiagnosis'].src = 'images/fr/tabDiagnosis_off.gif';
		document.images['tabTreatment'].src = 'images/fr/tabTreatment_off.gif';
		document.images['tabRecovery'].src = 'images/fr/tabRecovery_off.gif';
		document.images['tabSurvivorship'].src = 'images/fr/tabSurvivorship_off.gif';
		
		
		var thisImage=document.getElementById(targetIdImg)
		var imageName = thisImage.src.substring(0,thisImage.src.lastIndexOf("_o"));
		bw.dom?thisImage.setAttribute("src",imageName + "_hr.gif"):thisImage.src=imageName + "_hr.gif";
		
		target.style.display = "block";							
			
	}
}

function fnToggleFea(targetId, targetIdImg) {
	if (document.getElementById) {
		target = document.getElementById ( targetId );
		div1 = document.getElementById ( 'featureDiv' );
		div2 = document.getElementById ( 'updatesDiv' );		
		
		div1.style.display = "none";		
		div2.style.display = "none";
		
		document.images['tabFeature'].src = 'images/en/tabFeature_off.gif';
		document.images['tabUpdates'].src = 'images/en/tabUpdates_off.gif';				
		
		var thisImage=document.getElementById(targetIdImg)
		var imageName = thisImage.src.substring(0,thisImage.src.lastIndexOf("_o"));
		bw.dom?thisImage.setAttribute("src",imageName + "_hr.gif"):thisImage.src=imageName + "_hr.gif";
		
		target.style.display = "block";					
	}
}

function fnToggleFeaFr(targetId, targetIdImg) {
	if (document.getElementById) {
		target = document.getElementById ( targetId );
		div1 = document.getElementById ( 'featureDiv' );
		div2 = document.getElementById ( 'updatesDiv' );
				
		div1.style.display = "none";		
		div2.style.display = "none";
		
		document.images['tabFeature'].src = 'images/fr/tabFeature_off.gif';
		document.images['tabUpdates'].src = 'images/fr/tabUpdates_off.gif';
				
		var thisImage=document.getElementById(targetIdImg)
		var imageName = thisImage.src.substring(0,thisImage.src.lastIndexOf("_o"));
		bw.dom?thisImage.setAttribute("src",imageName + "_hr.gif"):thisImage.src=imageName + "_hr.gif";
		
		target.style.display = "block";							
			
	}
}


function toggleDiv(whichLayer){
	if (document.getElementById){
		var style2 = document.getElementById(whichLayer).style;
		if(style2.display == "block"){
			style2.display = "none";
		}else{
			style2.display = "block";	
		} 
	}else if (document.all){
		var style2 = document.all[whichLayer].style;
		if(style2.display == "block"){
			style2.display = "none";
		}else{
			style2.display = "block";	
		} 
		
	}else if (document.layers){
		var style2 = document.layers[whichLayer].style;
		if(style2.display == "block"){
			style2.display = "none";
		}else{
			style2.display = "block";	
		} 
	}
}

/*************************/
// handle return key (13)
/*************************/

function KeyPress(e){
	var key;
	if(window.event){
		key = window.event.keyCode; //IE
	} else {
		key = e.which; //firefox
	}
	if(key == 13){
		return false;
	} else {
		return true;
	}
}

/*	*************************
IE problem: users can't submit the Member Sign In form by hitting [Enter] in text/password field. Apparently, IE won't do it because the form is originally in a div styled display='none'; other forms don't have this problem.
	Other browsers (WinFF, MacFF, Safari) submit the form OK.
	Solution: trap the Enter key; explicitly submit if form validates. Either way, return 'false' to disable the keystroke. (Reason: Safari runs validateSignIn AGAIN from the form's onSubmit handler if we allow the keystroke to bubble through.)
	************************* */
function signInOnEnter(e) {
	var key;
	var theForm = document.frmSignIn;
	if (window.event) {
		key = window.event.keyCode; //IE and Safari
	} else {
		key = e.which; //Firefox, Mozilla
	}
	if (key == 13) {
		if (validateSignIn(theForm)) {
			theForm.submit();
		}
		return false;
	} else {
		return true;
	}
}


/* ************************
	Handles SurveyMonkey interface.
	fnCheckSurvey checks is cookie is created that disables the SurveyMonkey interface from being displayed. 
	If the cookie exists and the value is 'hideSurvey' fnHide will hide the DIV element.

	fnHideSurvey is triggered by the user to disable the SurveyMonkey text. It creates cookie and hide the DIV element.
*/

	// Cookie name to be used to check if the user disabled Survey Message
	function fnCheckSurvey(DivObj,SurveyCookieName){
		var SurveyCookie = readCookie(SurveyCookieName);		
		// get Cookie information and check the 
			if (SurveyCookie != null && SurveyCookie == 'hideSurvey') {
				fnHide(DivObj);
			}
			else {
				fnShow(DivObj);
			}
	}
	
	function fnHideSurvey(DivObj,SurveyCookieName,cookieExpires){
//		User selected to hide the Survey. Create a cookie with proper value.
		if (cookieExpires){
			createCookie(SurveyCookieName,'hideSurvey',cookieExpires);
		}
		else {
			createCookie(SurveyCookieName,'hideSurvey');
		}
		fnHide(DivObj);
	}
	
	function fnCheckHideSurvey(DivObj,SurveyCookieName,cookieExpires){
		if (document.getElementById(DivObj)){
			fnHide(DivObj);
		}
	}

	function createCookie(name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	}
	
	function readCookie(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	}
	
	function eraseCookie(name) {
		createCookie(name,"",-1);
	}
	function SelectAll(id)
	{
		document.getElementById(id).focus();
		document.getElementById(id).select();
	}
