/********* UTILITY FUNCTIONS AND GLOBALS / CONSTANTS ****************/
var DEFAULT_SHOW_ERRORS = 1;
var gbIE = false;
if (window.getComputedStyle)
{
	gbIE = false;
} else 
{
	gbIE = true;
}
		
//var KEY_RIGHT_ARROW = 39;

/* jQuery submit - learn from http://net.tutsplus.com/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/ */
/* a jquery example
 $(function() {  
   $(".button").click(function() {  
   });  
 });

$.ajax({  
   type: "POST",  
   url: "index.php?rebirth",  
   data: dataString,  
   success: function() {  
     //display message back to user here  
   }  
 });  
*/
 //return false;
/* ****** end of jQuery submit code ******** */

function stripPX(psString){
	//NB Not needed. parseInt does the same thing. Decent model for string manipulation, however.
	return (psString.toString().indexOf("px")>0) ? psString.toString().substring(0,psString.toString().indexOf("px")) : psString;
}
function highlight(psElementId)
{ //used to highlight screen elements as part of the help
	try{
		var e = document.getElementById(psElementId);
		e.style.zIndex = "500";
		//e.style.borderStyle="solid";
		e.style.borderColor=HELP_HIGHLIGHT;
		//e.style.borderWidth="1px";
	} catch(err)
	{errorHandler("highlight "+psElementId,err)}
}
function unhighlight(psElementId,piZindex)
{
	try{
		var e; 
		if (piZindex == "" || piZindex == null)
		{
			piZindex == "100";
		}	
		e = document.getElementById(psElementId);
		e.style.borderColor="transparent";
		if (!ie)
		{
			e.style.zIndex = piZindex;
		}
	} catch(err)
		{errorHandler("unhighlight "+psElementId,err)}
}
function loaded(i,f) 
{
	try
	{
		if (document.getElementById && document.getElementById(i) != null) f();
		else if (!$.bm_main.page_loaged) setTimeout('loaded(\''+i+'\','+f+')',100);
	} catch(err)
	{
		errorHandler("loaded "+i,err);
	}
}

function loaded_two(e,f,piIndex) 
{
	try
	{
		if (document.getElementById && document.getElementById(e) != null) 
		{
			f(piIndex);
		} else if (!$.bm_main.page_loaded) setTimeout('loaded_two(\''+e+'\','+f+','+piIndex+')',100);
	} catch(err)
	{
		errorHandler("loaded_two for element "+e,err);
	}
}
function loaded_three(e,f,piIndex,psType) 
{
	try
	{
		if (document.getElementById && document.getElementById(e) != null) 
		{
			f(psType,piIndex);
		} else if (!$.bm_main.page_loaded) setTimeout('loaded_three(\''+i+'\','+f+','+piIndex+','+psType+')',100);
	} catch(err)
	{
		errorHandler("loaded_three "+e,err);
	}
}
function errorHandler(psFunctionName,psErr)
{
	try{
		if (DEFAULT_SHOW_ERRORS == 1)
		{
			alert("Error in "+psFunctionName+" "+psErr.message);
		} else
		{
			//NB Brian to do - log errors
		}
	} catch(err)
	{
		alert("Error calling errorhandler "+psErr);
	}
}
function integerToBoolean(piValue)
{
	if (piValue == 1)
	{
		return true;
	} else
	{
		return false;
	}
}
function booleanToInteger(pbValue){
	if (pbValue)
	{
		return 1;
	} else
	{
		return 0;
	}
}
function getKeystroke(evt)
{
	var key;
	// determine which key was just pressed
	// IE and non-ie code.
	try
	{
		key = (evt.which) ? evt.which : evt.keyCode;
	} catch(err)
	{
		// ie7 and perhaps other versions
		key = document.event.keyCode;	
	}
	return key;
}
function getShiftPressed(evt)
{
	try
	{
		return (evt.shiftKey==1);
	} catch(err)
	{
		// ie7 and perhaps other versions
		return (document.event.shiftKey==1);	
	}
}
function getCtrlPressed(evt)
{
	try
	{
		return (evt.ctrlKey==1);
	} catch(err)
	{
		// ie7 and perhaps other versions
		errorHandler("getCtrlPressed",err);
		return (document.event.ctrlKey==1);	
	}
}
function getAltPressed(evt)
{
	
}


function getNodeCountVerbose(psElementId,psClassName)
{
	try
	{
		var iDebugAssist = 0;
		//alert("Node count test "+iReturn);
		var eStory = document.getElementById(psElementId);
		var sClassName;
		var iReturn = 0;
		iDebugAssist = 1;
		for (var i = 0; i<eStory.childNodes.length;i++)
	    	{
			sClassName = eStory.childNodes[i].className;
			if (sClassName==undefined)
			{} else
			{
				if (sClassName.indexOf(psClassName)>0 || sClassName == psClassName)
				{
					iReturn++;
				} 
			}
		}
	} catch(err)
	{
		alert("IE Error in getNodeCountVerbose: Your browser returned the following message for className "+psClassName+" "+err.message+". This appears to be a bug with internet explorer 7. "+iDebugAssist);
	}
	iReturn--;
	return iReturn; 
}
function setBorder(e,psWidth,psStyle,psColor)
{
	e.style.borderStyle=psStyle;
	e.style.borderColor=psColor; // should be a preference
	e.style.borderWidth=psWidth;	
}
function isNumber(psStringToTest)
{
  	var sNumberList = "0123456789.";
	var bReturnValue = true;
	var sTemp;

	if (psStringToTest == "NaN")
	{
		return false;
	}
 
   for (i = 0; i < psStringToTest.length && bReturnValue == true; i++) 
      { 
      sTemp = psStringToTest.charAt(i); 
      if (sNumberList.indexOf(sTemp) == -1) 
         {
         bReturnValue = false;
         }
      }
   return bReturnValue;
   
}
function getNodeCount(psElementId,psClassName)
{
	var iReturn=0;
	var sClassName;
	try 
	{
		var iDebugAssist = 0;
		//-1 was added to the following expression result to make it zero-based
		iReturn = Number(document.getElementById(psElementId).getElementsByClassName(psClassName).length)-1;

		if (iReturn==0)
		{
			iDebugAssist = 1;
			iReturn = getNodeCountVerbose(psElementId,psClassName);
		}

		//alert("node count" + iReturn);
		return iReturn;
	} catch(err)
	{
		//This is a serious, unexplained error with firefox on vista and ie
		//The above statement throws an error
		if (!window.getComputedStyle)
		{
			//alert("IE Error: Your browser returned the following message for className "+psClassName+" "+err.message+". This appears to be a bug with FireFox running under the Vista operating system and with IE. "+iDebugAssist);
		}
		try
		{
			iDebugAssist = 2;
			iReturn = getNodeCountVerbose(psElementId,psClassName);
			return iReturn;
		} catch(err)
		{
			return 0;
		}
	}
}



