// <Script>

function reFillSelect(poSelect, poSource, psListNode, psValueNode, psTextNode, psSelectedValue, pboHasEmptyRow, pboHasLastRow, psLastRowText){
	while (poSelect.options.length != 0){
		poSelect.options[0] = null;
	}
	var oOption = null;
	poSelect.selectedIndex = -1;
	if (typeof(pboHasEmptyRow) != "undefined" && pboHasEmptyRow == true){
		oOption = document.createElement("option");
		oOption.value = "";
		oOption.appendChild(document.createTextNode(""));
		oOption.selected = true;
		poSelect.options.appendChild(oOption);
	}
	var oList = poSource.selectNodes(psListNode)
	for (var oNode = oList.nextNode(); oNode != null; oNode = oList.nextNode()){
		oOption = document.createElement("option");
		oOption.value = oNode.selectSingleNode(psValueNode).text;
		oOption.appendChild(document.createTextNode(oNode.selectSingleNode(psTextNode).text));
		if (oOption.value == psSelectedValue){
			oOption.selected = true;
		}
		poSelect.options.appendChild(oOption);
	}
	if (typeof(pboHasLastRow) != "undefined" && pboHasLastRow == true){
		oOption = document.createElement("option");
		oOption.value = "";
		oOption.appendChild(document.createTextNode((typeof(psLastRowText) == "undefined")?(""):(psLastRowText)));
		if (oOption.value == psSelectedValue){
			oOption.selected = true;
		}
		poSelect.options.appendChild(oOption);
	}
	
	if (poSelect.selectedIndex < 0){
		poSelect.selectedIndex = 0;
	}

}

function trim(psSource){
	return (ltrim(rtrim(psSource)));
}

function ltrim(psSource){
	var sTemp = String(psSource)
	while (sTemp.length > 0 && sTemp.substr(sTemp.length-1,1) == " "){
		sTemp = sTemp.substr(0,	sTemp.length-1);
	}
	return (sTemp);
}

function rtrim(psSource){
	var sTemp = String(psSource)
	while (sTemp.length > 0 && sTemp.substr(0,1) == " "){
		sTemp = sTemp.substr(1,	sTemp.length-1);
	}
	return (sTemp);
}

var m_oXmlError = new ActiveXObject("MSXML.DOMDocument");

function checkError(psSource){
	m_oXmlError.loadXML(psSource);
	if (m_oXmlError.selectSingleNode("//error") != null){
		alert("Error: " + m_oXmlError.selectSingleNode("//error/description").text);
		return false;
	}
	if (trim(psSource) == ""){
		alert("Error: undefined");
		return false;
	}
	return true;
}

var m_oXmlResult = new ActiveXObject("MSXML.DOMDocument");

function showResult(psSource){
	var sResult = '';
	m_oXmlResult.loadXML(psSource);
	var oList = m_oXmlResult.selectNodes("//result")

	for (var oNode = oList.nextNode(); oNode != null; oNode = oList.nextNode()){
		sResult += oNode.selectSingleNode("./description").text;
		sResult += "\n";
	}
	if (sResult.length > 0) {
		alert(sResult);
		return true;
	} else {
		alert ("Result was not specified.");
		return false;
	}
}

function reInitXmlSrc(){
	var oSelectList = document.all.tags("SELECT")
	for (var i=0; i < oSelectList.length; i++){
		try {
			oSelectList[i].reInit();
			oSelectList[i].reInitXmlSrc();
		}  catch(e){}
	}
	
	var oInputList = document.all.tags("INPUT")
	for (var i=0; i < oInputList.length; i++){
		try {
			oInputList[i].reInitXmlSrc();
		}  catch(e){}
	}
}

function transDate2XML(pdtDate){
	return (formatDigest(pdtDate.getFullYear(),4) + '-' + formatDigest((pdtDate.getMonth()+1),2) + '-' + formatDigest(pdtDate.getDate(),2) + 'T' + formatDigest(pdtDate.getHours(),2) + ':' + formatDigest(pdtDate.getMinutes(),2) + ':' + formatDigest(pdtDate.getSeconds(),2));
}

//convert from YYYY-MM-DDThh:mm:ss string to Date
function transXML2Date(sDateTime){
	return new Date(
		getXMLYear(sDateTime),
		getXMLMonth(sDateTime),
		getXMLDay(sDateTime),
		getXMLHours(sDateTime),
		getXMLMinutes(sDateTime)
	);
}

//Date time function for convertion YYYY-MM-DDThh:mm:ss to selected attribute
function getXMLYear(pdtDate){
	var _aDateTime = String(pdtDate + "T").split("T");
	var _aDate = String(_aDateTime[0] + "---").split("-")
	return _aDate[0];
}
function getXMLMonth(pdtDate){
	var _aDateTime = String(pdtDate + "T").split("T");
	var _aDate = String(_aDateTime[0] + "---").split("-")
	if (!isNaN(_aDate[1])) _aDate[1]--;
	return _aDate[1];
}

function getXMLDay(pdtDate){
	var _aDateTime = String(pdtDate + "T").split("T");
	var _aDate = String(_aDateTime[0] + "---").split("-")
	return _aDate[2];
}

function getXMLHours(pdtDate){
	var _aDateTime = String(pdtDate + "T").split("T");
	var _aTime = String(_aDateTime[1] + "::").split(":")
	return _aTime[0];
}

function getXMLMinutes(pdtDate){
	var _aDateTime = String(pdtDate + "T").split("T");
	var _aTime = String(_aDateTime[1] + "::").split(":")
	return _aTime[1];
}

//Add additional '0' at the beginning of the Number, e.g. formatDigest(2,4) = 0002
function formatDigest(pnSource, pnDigestCount){
	var sTemp = String(pnSource);
	while (sTemp.length < pnDigestCount){
		sTemp = "0" + sTemp;
	}
	return(sTemp);
}

function getPageName(){
	var aLocation = String(window.location.pathname).split("/")
	return aLocation[aLocation.length -1 ]
}

function disableInputs(psSaveBtn){
	if (document.all.tags("INPUT")(psSaveBtn) == null){
		var oList = document.all.tags("INPUT")
		for (var i=0; i<oList.length; i++){
			if (typeof(oList[i].noDisable) == "undefined"){
				if (oList[i].type != "hidden"){
					oList[i].disabled = true;
				}
			}
		}

		var oList = document.all.tags("SELECT")
		for (var i=0; i<oList.length; i++){
			if (typeof(oList[i].noDisable) == "undefined"){
				oList[i].disabled = true;
			}
		}
	}
}

var m_nExecScriptCounter = 0;
function executeScript(psScript){
	try {
		window.execScript(psScript);
	} catch(e){}
	if (m_nExecScriptCounter++ < 6){
		window.setTimeout("executeScript('" + psScript + "')", 100*m_nExecScriptCounter);
		return;	
	}	
	m_nExecScriptCounter = 0;
}
function clearNodes(oNodeList){
	for (var i=0; i<oNodeList.length; i++) {
	    var oNode = oNodeList.item(i);
		if (oNode.childNodes.length > 0){
			clearNodes(oNode.childNodes);
		} else {
			var sName = String(oNode.parentNode.nodeName);
			if (sName == "budget_item_id" 
				|| sName == "budget_id" 
				|| sName == "working_budget_id" 
				|| sName == "budget_year" 
				|| sName == "financial_benefit_id" 
				|| sName.substr(0, 3) == "pe_"
				|| sName == "economic_id" 
				|| sName == "attachment_id" 
				|| sName == "cash_flow_id" 
				|| sName == "ts" 
				|| sName == "refinery_name" 
				|| sName == "topic"){
				//No Action for _id fields
				var i=0;
			} else {
				if (sName.substr(sName.length-3) == "_id"){
					oNode.text = "0";
				} else {
					oNode.text = "";
				}
			}
		}
	}
}

function MM_findObj(n, d) { 
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}
function MM_swapImage() { 
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() { 
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { 
 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
   var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
   if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

	function showPage(psLocation){
		window.location = psLocation;
		return false;
	}


