var NS4 = (document.layers) ? true : false;

checkEnter = function(event, imageButton)
{ 	
	var code = 0;
	if (NS4)
		code = event.which;
	else
		code = event.keyCode;
		
	if (code==13) {
		if (document.getElementById(imageButton).click!=undefined) {
			document.getElementById(imageButton).click();
		} else if (document.getElementById(imageButton).href!=undefined) {
			eval(document.getElementById(imageButton).href.replace("javascript:",""));
		}
		if (NS4)
			event.which = 0;
		else
			event.keyCode = 0;
		return false;
	}
}

/*
This function checks that ieSpell is installed and if so runs the spell checking facility.
*/
RunSpellCheck = function(){
	var myAgent = navigator.userAgent.toLowerCase();
	var myVersion = parseInt(navigator.appVersion);

	var is_ie = ((myAgent.indexOf("msie") != -1) 
			&& (myAgent.indexOf("opera") == -1));
	var is_win = ((myAgent.indexOf("win")!=-1) 
			|| (myAgent.indexOf("16bit")!=-1));
			
	if(!(is_ie) && (is_win)){
		alert("Sorry, the spell checking facility is only available within Internet Explorer on a Windows operating system.\r\n\r\nIt also requires the installation of ieSpell.");
		return false;
	}


	try{
		var ieSpell = new ActiveXObject("ieSpell.ieSpellExtension");
	}
	catch(oErr){
		
		if(confirm("The spell checking facility requires the installation of ieSpell.  Click OK to go to the ieSpell web site and download ieSpell.")){
			window.open("http://www.iespell.com/download.php","Download");
		}
	}
	
	ieSpell.CheckAllLinkedDocuments(document);
	
}

IECorrectedScrollintoView = function(o,b) {
	if (navigator.appName == "Microsoft Internet Explorer") {
		var a = document.documentElement.scrollTop;
		var b = document.documentElement.clientHeight;
		var c = o.offsetTop;
		var d = o.clientHeight;
		if (a >= c) {
			window.scrollTo(0,a);
		} else if (a + b <= c+d) {
			window.scrollBy(0,(c+d)-(a+b));
		}
	} else {
		o.scrollIntoView(b);
	}
}

getElementsByTagNames = function (list,obj) {
	if (!obj) var obj = document;
	var tagNames = list.split(',');
	var resultArray = new Array();
	for (var i=0;i<tagNames.length;i++) {
		var tags = obj.getElementsByTagName(tagNames[i]);
		for (var j=0;j<tags.length;j++) {
			resultArray.push(tags[j]);
		}
	}
	var testNode = resultArray[0];
	if (!testNode) return [];
	if (testNode.sourceIndex) {
		resultArray.sort(function (a,b) {
				return a.sourceIndex - b.sourceIndex;
		});
	}
	else if (testNode.compareDocumentPosition) {
		resultArray.sort(function (a,b) {
				return 3 - (a.compareDocumentPosition(b) & 6);
		});
	}
	return resultArray;
}

ForEach = function(array, fn) {	
	if (typeof(fn)=="function")
		for (var n = 0; n < array.length; n++)
    		fn(array[n]);
}

IsKeyPressInArr = function(event, arrKeys) {
	var code = 0;
	if (NS4)
		code = event.which;
	else
		code = event.keyCode;
	ret = false;
	ForEach (arrKeys, function(n) {
		if (n == code) ret = true;
	})
	return ret;
}

doFireClick = function(objID) {
	var fireOnThis = document.getElementById(objID);
	if (document.createEvent) {
		//DOM COMPLIANT
		var evObj = document.createEvent('MouseEvents');
		evObj.initMouseEvent( 'click', true, true, window, 1, 12, 345, 7, 220, false, false, true, false, 0, null );
		fireOnThis.dispatchEvent(evObj);
	} else if (fireOnThis.fireEvent) {
		//IE
		fireOnThis.focus();
    	var newEvt = document.createEventObject();
    	newEvt.button = 3;
    	fireOnThis.fireEvent("onclick", newEvt);
    	event.cancelBubble = true;
	}
    
}

addLoadEvent = function(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}