/*
File: domutils.js;
Description: some useful code;
References: none;
*/

function getParentByTagName(theNode,byTagName,orTagName){
	var theParent = theNode.parentNode;
	if (theParent != null){
		if ((theParent.tagName != byTagName)&&(theParent.tagName != orTagName)) theParent=getParentByTagName(theParent,byTagName,orTagName);
	}
	return theParent;
}

function getFirstChildByTagName(theNode,byTagName,orTagName){
	var theFirstChild = null;
	var theChildren = theNode.childNodes;
	for (nDex = 0; nDex < theChildren.length; nDex++){
		if ((theChildren[nDex].tagName == byTagName)||(theChildren[nDex].tagName == orTagName)){
			theFirstChild = theChildren[nDex];
			break;
		}
	}
	return theFirstChild;
}

function getNextSiblingByTagName(theNode,byTagName,orTagName){
	if (orTagName == null) orTagName = byTagName;
	var theNextSibling = theNode.nextSibling;
	if (theNextSibling != null){
		if ((theNextSibling.tagName != byTagName)&&(theNextSibling.tagName != orTagName)) theNextSibling=getNextSiblingByTagName(theNextSibling,byTagName,orTagName);
	}
	return theNextSibling;
}

function getParentByClassName(theNode,byClassName){
	var theParent = theNode.parentNode;
	if (theParent != null){
		if (!hasWord(theParent.className,byClassName))
		theParent=getParentByClassName(theParent,byClassName);
	}
	return theParent;
}

function hasWord(thePhrase,theWord){
	matchWordRegExpr = new RegExp("\\b"+theWord+"\\b");
	return matchWordRegExpr.test(thePhrase);
}

function getElementsByTagNameAndAttribute(theTagName,theAttributeName,theAttributeValue){
}

function getActualLeft(theNode){
	actualLeft = document.body.offsetLeft;
	if (theNode.offsetParent){
		while (theNode.offsetParent){
			actualLeft += theNode.offsetLeft;
			theNode = theNode.offsetParent;
		}
	} else if (theNode.x) actualLeft += theNode.x;
	return actualLeft;
}

function getActualRight(theNode){
	actualRight = theNode.offsetWidth;
	actualRight += getActualLeft(theNode);
	return actualRight;
}
	
function getActualTop(theNode){
	actualTop = document.body.offsetTop;
	if(theNode.offsetParent){
		var node = theNode;
		while(node.offsetParent){
			actualTop+=node.offsetTop; 
			node=node.offsetParent;
		}
		if(theNode.parentNode){
			while(theNode.offsetParent){
				actualTop-=theNode.scrollTop;
				theNode=theNode.parentNode;
			}
		}
	} else if (theNode.y) actualTop += theNode.y;
	return actualTop;
}

function getActualBottom(theNode){
	actualBottom = theNode.offsetHeight;
	actualBottom += getActualTop(theNode);
	return actualBottom;
}

// function to expand/collaps the part of the page
function openFull(el){
	//el.nextSibling.style.display=='none'?el.nextSibling.style.display='block':el.nextSibling.style.display='none';
    var theNextSibling = getNextSiblingByTagName(el,'DIV');
	theNextSibling.style.display=='none'?theNextSibling.style.display='block':theNextSibling.style.display='none';

	return false;
}

function expandAll(fl){
	var elAr = document.getElementsByTagName("DIV");
	for(var i=1;i<elAr.length;i++){
		if(elAr[i].style.display=='none' || elAr[i].block_fl){
			elAr[i].block_fl = true;
			if(fl)
				elAr[i].style.display = "";
			else
				elAr[i].style.display = "none";
		}
	}
}
function getWindowSize() {
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
	    myWidth = window.innerWidth;
	    myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	    //IE 6+ in 'standards compliant mode'
	    myWidth = document.documentElement.clientWidth;
	    myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	    //IE 4 compatible
	    myWidth = document.body.clientWidth;
	    myHeight = document.body.clientHeight;
	}
	return [ myWidth, myHeight ];
}
function getScrollXY() {
	var scrOfX = 0, scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	return [ scrOfX, scrOfY ];
}
// Opens a new window (if it is not there yet) and displays the message. Useful  for debugging
function log(message) {
    if (!log.window_ || log.window_.closed) {
        var win = window.open("", null, "width=400,height=200," +
                              "scrollbars=yes,resizable=yes,status=no," +
                              "location=no,menubar=no,toolbar=no");
        if (!win) return;
        var doc = win.document;
        doc.write("<html><head><title>Debug Log</title></head>" +
                  "<body></body></html>");
        doc.close();
        log.window_ = win;
    }
    var logLine = log.window_.document.createElement("div");
    logLine.appendChild(log.window_.document.createTextNode(message));
    log.window_.document.body.appendChild(logLine);
}
