/*@cc_on @if (@_win32 && @_jscript_version >= 5) if (!window.XMLHttpRequest)
window.XMLHttpRequest = function() { return new ActiveXObject('Microsoft.XMLHTTP') }
@end @*/

/**
 * Transforms a XML Document into a String according a style sheet.
 * Parameters: xDoc=XML Document, oStyle=Stylesheet Object.
 * @access public
 * @return void
 **/
function xslTrans(xDoc, oStyle){
	var sResult = '';
	if (window.ActiveXObject) {
		sResult += xDoc.transformNode(oStyle);
	} else {
		var processor = new XSLTProcessor();
		processor.importStylesheet(oStyle);
		var oResult = processor.transformToFragment(xDoc, document);
	}
}

/**
 *  loads a Stylesheet for multiple browsers
 *	parameter: Url of Style Sheet
 * @access public
 * @return Style Object
 **/
function loadStyleSheet(sUrl){
	var oStyle;
	if (window.ActiveXObject) {
		oStyle = new ActiveXObject("Microsoft.XMLDOM");
	} else {
		oStyle = document.implementation.createDocument("","",null);
	}
	oStyle.async = false;
	oStyle.load(sUrl);
	return oStyle;
}

function evaluateXPath(aNode, sExpr) {
	var result, res, i;
	var aFound = [];
	if (window.ActiveXObject) {
		result = aNode.selectNodes(sExpr);
		for (i=0; i<result.length; i++) {
			res = result.item(i);
			aFound.push(res);
		}
	} else {
		var xpe = aNode.ownerDocument || aNode;
		var nsResolver = xpe.createNSResolver(aNode.ownerDocument === null ? aNode.documentElement : aNode.ownerDocument.documentElement);
		result = xpe.evaluate(sExpr, aNode, nsResolver, 0, null);
		while (res = result.iterateNext()) {
			aFound.push(res);
		}
	}
	return aFound;
}

/**
 * stringFormData
 * Parameter: Form Object
 * @access public
 * @return String of form name-value pairs
 **/
function stringFormData(oFrm){
	var i, s;
	s='';
	for (i=0; i<oFrm.length; i++) {
		if (oFrm[i].name) {
			switch(oFrm[i].type){
			 	case 'checkbox':
			 		s += oFrm[i].name + '=' + (oFrm[i].checked ? '1' : '0') + '&';
			 		break;
			 	case 'radio':
					if (oFrm[i].checked) {
						s += oFrm[i].name + '=' + encodeURIComponent(oFrm[i].value) + '&'
					}
			 		break;
			 	default:
			 		s += oFrm[i].name + '=' + encodeURIComponent(oFrm[i].value) + '&';
			 } // switch
		}
	}
	s = s.substr(0, s.length-1);
	return s;
}


function loadHTML(oObj, pUrl){
	oRq = new XMLHttpRequest();
	oRq.open("GET", pUrl, true);
	oRq.onreadystatechange = function () {
		if (oRq.readyState == 4) {
			if (oRq.status == 200) {
				oObj.innerHTML = oRq.responseText;
			}
		}
	}
	oRq.send(null);
}

function bcolorRestore(pForm){
	if (document.forms[pForm]) {
		var oButton = document.forms[pForm].x_save;
		if (oButton) {
			var s = oButton.className;
			s = s.replace(/ saveOk/, '');
			oButton.className = s;
		}
	}
}


function sendString(pString, pUrl, pDest, pFunc){
	var nPar = arguments.length;
	var oRq = new XMLHttpRequest();
	oRq.open("POST", pUrl, true);
	oRq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	oRq.onreadystatechange = function () {
		if (oRq.readyState == 4) {
			hideAni();
			if (oRq.status == 200) {
				var oDest = document.getElementById(pDest);
				oDest.innerHTML = oRq.responseText;
				if (pFunc) {
					pFunc();
				}
			}
		}
	}
	oRq.send(pString);
	showAni();
}

function sendForm(pForm, pUrl, pDest, pFunc){
	var fName = pForm.name; // save form name. AJAX will replace it.
	var s = stringFormData(pForm);
	sendString(s, pUrl, pDest, function() {
		oForm = document.forms[fName]; // get the new transmitted form
		if (oForm) {
			if (oForm.x_save) {
				// if there exists a save button, make it green
				oForm.x_save.className += ' saveOk';
				window.setTimeout("bcolorRestore('"+fName+"')", 5000);
			}
		}
		if (pFunc) {
			pFunc();
		}
	});
}

function getString(pString, pUrl) {
	var c = '';
	var oRq = new XMLHttpRequest();
	oRq.open("POST", pUrl, false);
	oRq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	oRq.send(pString);
	if (oRq.status == 200) {
		c = oRq.responseText;
	}
	return c;
}

function getLabel(pLabelName){
	return getString('label='+pLabelName, 'getlabel.php');
}

function showAni(){
	oImg = document.getElementById('animation');
	nWidth = document.documentElement.clientWidth / 2;
	if (oImg) {
		oImg.style.left = String(nWidth+455)+'px';
		oImg.style.visibility = 'visible';
		return;
	}
	oImg = document.createElement('img');
	oImg.id = 'animation';
	oImg.src = 'images/icons/activity.gif';
	oImg.style.position = 'absolute';
	oImg.style.top = '130px';
	oImg.style.left = String(nWidth+455)+'px';
	document.body.appendChild(oImg);
	return;
}

function hideAni(){
	oImg = document.getElementById('animation');
	oImg.style.visibility='hidden';
}

function getAbsoluteXpos(oEl){
	var x = 0;
	var o = oEl;
	while (o) {
		x += o.offsetLeft;
		o = o.offsetParent;
	}
	return x;
}

function getAbsoluteYpos(oEl){
	var y = 0;
	var o = oEl;
	while (o) {
		y += o.offsetTop;
		o = o.offsetParent;
	}
	return y;
}

function setPosAbsolute(pElement){
	var oEl = document.getElementById(pElement);
	var x = getAbsoluteXpos(oEl);
	var y = getAbsoluteYpos(oEl);
	with (oEl.style) {
		position = 'absolute';
		top = String(y)+'px';
		left = String(x)+'px';
	}
}

function dispHelp(oImg){
	var cHelp = 'h_'+oImg.id;
	var oHelp = document.getElementById(cHelp);
	if (oHelp) {
		oHelp.style.visibility='visible';
		return;
	}
	oHelp = document.createElement('div');
	oHelp.id = cHelp;
	oHelp.className = 'helpArea';
	document.body.appendChild(oHelp);
	oHelp.disp = true;
	sendString('help=' + oImg.id, 'showhelp.php', cHelp, function() {
		var cHx = 'hx_'+oImg.id;
		var oHx = document.getElementById(cHx);
		with (oHelp.style) {
			var x = getAbsoluteXpos(oImg) + Number(oHx.getAttribute('left'));
			var y = getAbsoluteYpos(oImg) + Number(oHx.getAttribute('top'));
			top = y + 'px';
			left = x + 'px';
			width = oHx.getAttribute('width') + 'px';
			height = oHx.getAttribute('height') + 'px';
		}
		if (oHelp.disp) {
			oHelp.style.visibility='visible';
		}
	});
}

function hideHelp(oImg){
	var cHelp = 'h_'+oImg.id;
	var oHelp = document.getElementById(cHelp);
	if (oHelp) {
		oHelp.disp = false;
		oHelp.style.visibility='hidden';
	}
}