// for use with Console2 firefox extension to ouput trace info to js console.
function debug(aMsg) {
   setTimeout(function() { throw new Error("[debug] " + aMsg); }, 0);
}

/*
 * awsome fuction that handles attaching events at loadtime in a cross-browser way.
 * most useful for attaching multile window.onload events thus:
 *
 *	addEvent(window, 'load', someFunction);
 *
 * from: http://www.scottandrew.com/weblog/articles/cbs-events
 */
function addEvent(obj, evType, fn)
{
	if (obj.addEventListener){
		obj.addEventListener(evType, fn, true);
		
		return true;
	} else if (obj.attachEvent){
		var r = obj.attachEvent("on"+evType, fn);
		//alert(fn+" added");return r;
	} else {
		return false;
	}
}

// JavaScript Document
function MM_openBrWindow(theURL,winName,features) { //v2.0
  newWindow = window.open(theURL,winName,features);
  newWindow.focus();
  //alert('yeah!');
}
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 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_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);
// centralised way of opening resource cntr.
function openResourceCentre(url){
	var width = 800;
	var height = 600;
	if(arguments[1] != null) width = arguments[1];
	if(arguments[2] != null) height = arguments[1];
	MM_openBrWindow(url,'resource_centre','location=no,scrollbars=yes,resizable=yes,width='+width+',height='+height);
}
function NS_showHideLayers() { //v6.0
//MODIFIED by NED SCHWARTZ to change the 'display' of the div rather than
//the 'visibility' thus removing the div from the document flow.
//used for fly-down content.
  var i,p,v,obj,args=NS_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_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; }
}
function NS_toggleLayers() {
//by Ned Schwartz - toggles visibility of layer
	var i,obj,args=NS_toggleLayers.arguments;
	for(i = 0; i < args.length; i++){
		if ((obj=MM_findObj(args[i]))!=null) {
			a = obj.parentNode;//.getElementsByTagName('a');
			td = a.parentNode;
			if (obj.className == 'accordian' || obj.className == 'accordianShow') {
				if(obj.className=='accordian'){obj.className='accordianShow';a.setAttribute('id','articleActive');}
				else{obj.className='accordian';a.setAttribute('id','hiddenLayer');}
			}
			else if (obj.style.display != null) {
				if(obj.style.display=='none'){obj.style.display='block';}
				else{obj.style.display='none';}
			}
		}
	}
}

/*  
toggleSection Usage:
---------------------------
toggleSection('section', '3');	// will hide sectionX, except "section3"
<div id="section1"></div>
<div id="section2"></div>
<div id="section3"></div>
*/
function toggleSection() {
	var i, obj;
	var tmp = true;
	var args = toggleSection.arguments;
	
	// hide all sections
	for(i = 1; tmp; i++)
	{	
		obj = document.getElementById(args[0]+i);
	
		if(obj == null)
			tmp = false;
		else
		{
			if (obj.style.display != null)
				obj.style.display='none';
		}
	}
	
	// display only the specified section
	if(args.length == 2)
	{
		if ((obj=MM_findObj(args[0]+args[1])) != null) 
		{
			if (obj.style.display != null) 
			{
				if(obj.style.display=='none')
					obj.style.display='block';
				else
					obj.style.display='none';
			}
		}
	}
}	

function swapBtnTxt(btn, msg1, msg2){
// little function to swap the text of a btn	
	btn.value == msg1 ? btn.value = msg2 : btn.value = msg1;
}
function swapTxt(elem, msg1, msg2){
// little function to swap the text of an element	
	if(elem.nodeType == undefined) elem = document.getElementById(elem);
	//debug(elem.innerHTML);
	if(elem.innerHTML.indexOf(msg1) != -1) elem.innerHTML = elem.innerHTML.replace(msg1,msg2);
	else elem.innerHTML = elem.innerHTML.replace(msg2,msg1);
}
function NS_toggleTabs(){
// toggle Tabs
// takes 2 arguments:
//		1: the id of the element to change
//		2: whetehr to show it or hide it
// by ned schwartz
	var obj,a,i,j, args=NS_toggleTabs.arguments; //args gets args from arguments array of the function call
	for(i=0;i<args.length-1;i+=2){
		if((obj=MM_findObj(args[i]))!=null){	//use macroimedia findObj function to locate element
			if(args[i+1] =='show'){				//if the second arg is 'show':
				a = obj.getElementsByTagName("a");	//chnage link fonts to black
				for(j = 0; j<a.length;j++){
					a[j].style.color = "black";
				}
				obj.style.backgroundColor = "#E8E6CF";	// set bg color of tab to lighter green
			}else{								// if the second arg is NOT 'show':
				a = obj.getElementsByTagName("a");		// change links to white;
				for(j = 0; j<a.length;j++){
					a[j].style.color = "white";
				}
				obj.style.backgroundColor = "#747B58";	// set bg color to darker green
			}
		}
	}
}

function NS_toggleImage(){
//toggles image by changing display btwn none and inline
	var obj,i,a,b,sr,args=NS_toggleImage.arguments;
	for(i=0;i<args.length;i++){
		if((obj=MM_findObj(args[i]))!==null){
			if (obj.style) {
				obj=obj.style;
				if(obj.display=='none'){
					obj.display='inline';
				}else{
					obj.display='none';
				}
			}
		}
	}
}
function NS_swapImages(imgId1, imgId2){
	if((img1 = MM_findObj(imgId1)) == null)return;
	if((img2 = MM_findObj(imgId2)) == null)return;
	
	var swapScratch = img1.style.display;
	img1.style.display = img2.style.display;
	img2.style.display = swapScratch;
	return;
}
function NS_swapCssBgImg(obj){
	alert(obj.style.backgroundImage);
}
function checkQuestionsStatus(){
//function to show questions tab in lesson articles page if returning from submitting anseres.
	if(document.location.href.indexOf('questions=true')!= -1){

		NS_toggleTabs('readingTab','hide','questionsTab','show');
		NS_showHideLayers('readingTips','','hide','questions','','show');

		//alert("showing questions");
	}
}

function valid(form) {
// FUNCTION FOR VALIDATING THAT A FORM FIELD IS NOT BLANK
// used particularly for the articles questions and answers
	for(i=0;i<form.length;i++){
		var e = form.elements[i];
			if((e.type.toLowerCase() == "textarea" || e.type.toLowerCase() == "text")&& e.value==""){
											//if text area not filled in
				alert("Please answer all questions before submitting your answers.");
				e.focus();
				e.select();
				return false;
			}
			if(e.type.toLowerCase() =="radio"){// if radio button
				mybutton = form[i].name; //get the name of the radio button group
				var check = false;			// set up boolean for btn group checked status
				for(j=0;j<form[mybutton].length;j++){
											// loop through btn group, if btn checked
											// set checked to true and break loop
					if(form[mybutton][j].checked == true){check=true;break}
				}
				if(check == false){			// if checked not set to true during loop = no btns were checked
					alert("Please answer all questions before submitting your answers.");
					e.focus();
					e.select();
					return false;
				}
											// advance through form members by (btn group memebers lenth)
											// to avoid running the same check on every btn in a group.
				i+=(form[mybutton].length) -1;

			}
	}
	return true;							//if all filled out you get here and return true.
}

function breadCrumbs(splitStr){
// charts bread crumb navigation based on page title
// takes 'splitStr' as arg - the string to split the page title on
// the current page is the last itme in the page title list.
// by Ned Schwartz
var here = document.title;
//here = here.substring(discard.length);
here = here.split(splitStr);
return here[here.length - 1];
}

function getEgMsg(msgName){
// retrieves the content of a div <msgName> and returns it as a string
// used for eg button tooltip content
	var msgText = 'test getEgmsg';
	egObj = MM_findObj(msgName,document);
	msgText = egObj.innerHTML;

	return msgText;
}

