﻿function StandardiseEvent(e) {
//from http://www.mozillazine.org/talkback.html?article=2433
    if (!e.stopPropagation) {
        e.stopPropagation = new Function('this.cancelBubble = true');
    }
    
    // if (!e.pageY) {
	//	e.pageY = new Function('return document.body.scrollTop + this.clientY');
    //}

    //if (!e.pageX) {
	//	e.pageX = new Function('return document.body.scrollLeft + this.clientX');
    //}

    if (!e.preventDefault) {
        e.preventDefault = new Function('this.returnValue = false');
    } 

    if (typeof e.layerX == 'undefined' && typeof e.offsetX == 'number') {
        e.layerX = e.offsetX;
        e.layerY = e.offsetY;
    }

    if (!e.target && e.srcElement) {
        e.target = e.srcElement;
        if (e.type == 'onmouseout') {
            e.relatedTarget = e.toElement;
        } else if (e.type == 'onmouseover') {
            e.relatedTarget = e.fromElement;
        }
    }
} //StandardiseEvent


function FindChildName(parentObject, childName, n, nsofar) {
	//n-1 is how many tags to skip of this name
	//don't pass nsofar

	if (!n) 
		n = 1;

	if (!nsofar) {
		nsofar = new Object();
		nsofar.num = 0;
	}

	var o = parentObject;	
	if (!o) 
		return null;
	
	if (o.name == childName) {
		nsofar.num++;
		if (nsofar.num == n) 
			return o;
	}
	
	for (var i=0; i < parentObject.childNodes.length; i++) {
		o = FindChildName(parentObject.childNodes[i], childName, n, nsofar);
		if (o)
			return o;
	}		

	return null;
} //FindChildName

function FindChildTagName(parentObject, childTagName, n, nsofar) {
	//n-1 is how many tags to skip of this name
	//don't pass nsofar

	if (!n)
		n = 1;

	if (!nsofar) {
		nsofar = new Object();
		nsofar.num = 0;
	}
		
	var o = parentObject;	

	if (!o.tagName) 
		return null;

	if (o.tagName.toUpperCase() == childTagName.toUpperCase()) {
		nsofar.num++;
		if (nsofar.num == n) 
			return o;
	}
	
	for (var i=0; i < parentObject.childNodes.length; i++) {
		o = FindChildTagName(parentObject.childNodes[i], childTagName, n, nsofar);
		if (o)
			return o;
	}		
	return null;
} //FindChildTagName

function FindParentTagName(childObject, parentTagName, n) {
	//n-1 is how many tags to skip of this name, e.g. finding td of outer table containing a toolset

	if (!n) 
		n = 1;
	var nsofar = 0;
	var o = childObject;		

	//if (!o.tagName) 
	//	return null;

	parentTagName = parentTagName.toUpperCase();

	while (true) {
		if (o.tagName) {
			if (o.tagName.toUpperCase() == parentTagName) {
				nsofar++;
				if (nsofar == n) {
					return o;
				}
			}
		}
		if (!o.parentNode) {
			return null;
		}
		o = o.parentNode;		
	}

} //FindParentTagName

function DeleteAllRowsFromTable(tbl) {
	while (tbl.rows.length > 0)
		tbl.deleteRow(0);
} //DeleteAllRowsFromTable


function getCookie(sName) {
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++) {
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0]) 
      return unescape(aCrumb[1]);
  }
  return null;  //otherwise
} //getCookie

function delCookie(sName) {
  document.cookie = sName + "=; expires=Fri, 31 Dec 1999 23:59:59 GMT;";
} //delCookie

function setCookie(sName, sValue) {
	expires = new Date();
	expires.setTime(expires.getTime()+(15*24*60*60*1000)); //15 days
	document.cookie = sName + '=' + escape(sValue) + ";expires=" + expires.toUTCString();
} //setCookie
