﻿// JScript File

	function openPopup(pagePath,iWidth,iHeight,forPrint,useUnique,keepOpen){
			var iWinWidth = (iWidth) ? parseInt(iWidth) : 455;
			var iWinHeight = (iHeight) ? parseInt(iHeight) : 380;
			
			//pagePath = String(pagePath).toLowerCase();
			// if other arguments exist, the first after url overwrites width of window
			
			
			//if (iWidth) iWinWidth = iWidth;
			//if (iHeight) iWinHeight = iHeight;
						
			var iAvailWidth = screen.availWidth - 40;
			var iAvailHeight = screen.availHeight - 40;
			if (iAvailWidth < iWinWidth) iWinWidth = iAvailWidth;
			if (iAvailHeight < iWinHeight) iWinHeight = iAvailHeight;
			// also center position of window
			var iLeft = (iAvailWidth - iWinWidth)/2;
			var iTop = (iAvailHeight - iWinHeight)/2;
			
			// make a function to generate window name unique or non unique as requested
			// close or do not close opened windows
			// add window to global least level array in opened windows stack
						
			var newWinName = (useUnique) ? getWindowName() : 'newWindow'; 
			
			var GlobalArray = (!keepOpen) ?  getGlobalArray() : new Array();
			
			if (forPrint)
				GlobalArray[GlobalArray.length] = window.open(pagePath,newWinName,'width='+ iWinWidth +',left='+ iLeft +',top='+ iTop +',height='+ iWinHeight +',toolbar=0,status=0,menubar=1,resizable=0,scrollbars=1');
			else
				GlobalArray[GlobalArray.length] = window.open(pagePath,newWinName,'width='+ iWinWidth +',left='+ iLeft +',top='+ iTop +',height='+ iWinHeight +',toolbar=0,status=0,menubar=0,resizable=0,scrollbars=1');
				
			
			GlobalArray[GlobalArray.length-1].focus();	
			
			
			if (event)
			{
				event.cancelBubble = true;
				event.returnValue = false;			
			}
			
		}
		
		var MenuIsOpen = true;
		function openModal(pagePath,iWidth,iHeight,isModal,isNotResizable){
			var iWinWidth = (iWidth) ? parseInt(iWidth) : 455;
			var iWinHeight = (iHeight) ? parseInt(iHeight) : 380;
			
			//pagePath = String(pagePath).toLowerCase();
			
			var iAvailWidth = screen.availWidth;
			var iAvailHeight = screen.availHeight;
			if (iAvailWidth < iWinWidth) iWinWidth = iAvailWidth;
			if (iAvailHeight < iWinHeight) iWinHeight = iAvailHeight;
			/*
			// also center position of window
			var iLeft = (iAvailWidth - iWinWidth)/2;
			var iTop = (iAvailHeight - iWinHeight)/2;
			*/
			// open either a modal or modless dialog according to isModal
						
			var modalReturn;
			var resize = "yes";
			if (isNotResizable) resize = "no";
			
				
			if (isModal)
				modalReturn = showModalDialog(pagePath,window,'dialogHeight:'+ iWinHeight+ 'px;dialogWidth:'+ iWinWidth+'px ;center: yes ;resizable: '+ resize +' ;scroll: no;status:no');
			else{
				if (!MenuIsOpen) showModelessDialog(pagePath,window,'dialogHeight:'+ iWinHeight+ 'px;dialogWidth:'+ iWinWidth+'px ;center: yes ;resizable: '+ resize +' ;scroll: yes;help:no; status:no;');
				MenuIsOpen = true;
				// this line means i have to reset menuisopen upon closure of dialog!
				
			}
			
			if (event && isModal)
			{
				event.cancelBubble = true;
				event.returnValue = false;			
			}
			
			
			
		}
		
	var newWin = new Array();
	function getGlobalArray(){
		
		if (window.opener){
			
				if (window.opener.getGlobalArray ){
						alert(window.opener.getGlobalArray);
					return window.opener.getGlobalArray();
				}else
					return newWin;
		}else
			return newWin;
	}
	function getWindowName(){
		// keep searching for a new unique ID
		
		var newID = window.document.uniqueID;
		if (window.newID){
			newID = window.document.body.uniqueID;
		}
		return newID;
	}
	
	function unloadPopup(){
										  
		// loop thru all opened windows and close only if this is the main window
		if (!window.opener){
			var i = 0;
			var newWinX;
			
			for (i=0; i<newWin.length; i++){
				
				if (window.newWin[i])
					if (!window.newWin[i].closed)
						window.newWin[i].close();
				newWin[i] = null;
			
			}
		}
		
		
		
	}
	var onLoadFocus = true; // set this to false in individual documents
	window.onunload = unloadPopup;
	window.onload = loadPage;
	var loadBodyFunc;
	
	function loadPage(){
		
			
		if (loadBodyFunc){
			eval(loadBodyFunc);
		}
		
		
	}
	var calID;
	
	
	
	function changeDisabled(disabledState,id1,id2){
	
		var elem = document.getElementById(id1);
		if (!document.getElementById(id1) || !document.getElementById(id2)) return;
		if (disabledState) {
			document.getElementById(id1).style.display = "inline";
			document.getElementById(id2).style.display = "none";
		}else{
			document.getElementById(id2).style.display = "inline";
			document.getElementById(id1).style.display = "none";
		}
		
	
	}

	//var cssPath = "../../css/";
	//var imagesFolder = "../images/";
	
	// onresize reposition active layer
	window.onresize = handleResize;
	var gList;
	function handleResize(){
		if (gList){
			if(document.getElementById(gList)){
				document.getElementById(gList).positionElem();
			}
		}
	}
	
	
	function updateOpener(optionValue, optionValue2){
		// go to top, call update function
		
		window.parent.SendData(event.srcElement.innerText, optionValue, optionValue2);
		window.parent.close();
	}
	
	function Select(val,val2){
		if (event.srcElement.tagName == "SPAN") src = event.srcElement.parentElement;
		else src = event.srcElement;
		SendData(src.innerText,val,val2);
		self.close();
	}
	function SelectGrade(optionValue,gradeText,optionValue2){
		if (window.opener){
			selObj = window.opener.document.getElementById(window.opener.selID);
		}else{
			selObj = dialogArguments.document.getElementById(dialogArguments.selID);
		}
		if (selObj){
			selObj.setAttribute("gradeText",gradeText);
			
			selObj.UpdateFieldValue(event.srcElement.innerText, optionValue, optionValue2);
			self.close();
			selObj = null;
			
		}
	}
	function SelectUpdate(val,val2){
		SendData(event.srcElement.innerText,val,val2);
		refreshOpen();
		
	}
	function SendData(optionText,optionValue,optionValue2,optionElement){
		// do the refresh and send to database close window or whatever necessary
		//alert(dateValue.value);
		// send data to opener, or window object
		var selObj;
		
		if (window.opener){
			selObj = window.opener.document.getElementById(window.opener.selID);
		}else{
			selObj = dialogArguments.document.getElementById(dialogArguments.selID);
		}
		if (selObj){
			//pass back the firing element
			if (optionElement) selObj.Option = optionElement;
			else selObj.Option = event.srcElement;
			
			selObj.UpdateFieldValue(optionText, optionValue, optionValue2);
			// TODO: optionally close window
			//self.close();
			
			

			selObj = null;
			
		}
		selObj = null;
	
	}

	function SelectItem(val,text,val2){
		// do the same with the field selected (item number), then take the inner text and update another field
		var itemD;
		if (window.opener){
			itemD = window.opener.document.getElementById('itemD');
		}else{
			itemD = dialogArguments.document.getElementById('itemD');
		}
		if (itemD){
			itemD.value = event.srcElement.innerText;
		}
		
		SendData(text,val,val2);
		self.close();
		
	}
	function updateVoucher(val,text,val2){
		
		SendData(text,val,val2);
		self.close();
	}
	function SelAccount(accNo){
		SendData(accNo,accNo);
		self.close();
	}
	function clearItemId(){
		// clear value when item description changes
		// itemId is thename of text field
		if (document.getElementById('itemId')){
			document.getElementById('itemId').value = '';
		}
		// although there is a hidden input, but the field should be ignored
		
	}
	function showFind(bId){
		if (document.getElementById(bId)){
			if (event.type == "focus") document.getElementById(bId).style.display = 'inline';
			else if(event.type == "blur") {
				if (document.activeElement.id == bId) return;
				else document.getElementById(bId).style.display = 'none';
			}
		}
	}
	
	var gReadOnly = false;
	
	
	function payMethod(gName,elem){
		// show hide according to clicked item
		var gElem = document.getElementsByName(gName);
		var gRadio = document.getElementsByName(elem.name);
		
		if (gElem){
			for (var i=0; i<gElem.length; i++){
				if (gRadio[i].checked) gElem[i].style.display = '';
				else gElem[i].style.display = 'none';
			}
		}
	}
	
	
	function refreshOpen(){
		window.top.dialogArguments.location.reload();
		self.close();
	}
	function updateRate(txt){
		window.top.selObj.innerText = txt;
		window.dialogArguments.document.getElementById(window.top.selObj.hdnText).value = txt;
		setTimeout("self.close();",0); // the page oddly flickers that is why i set time out
	}
	
	
	
	function resetVoucher(){
		if(event.propertyName=='value') 
			setTimeout('ddVoucher.UpdateFieldValue("","");',500); // to make sure the object has loaded
	}
	
	// open select time dialog
	function selTime(){
		window.selID = event.srcElement.uniqueID;
		
		openModal('../select/time.htm',200,100,true);
	}
	
	//search for different voucher
	function showTypes(frameId,kidId,vTypeId){

		window.kidId = kidId; // save field id in window
		window.vTypeId = vTypeId; // hidden field
		window.frameId = frameId;
		
		var frameO = document.getElementById(frameId);
		if(frameO){
			// now show span
			frameO.style.display = 'inline';
			frameO.setActive();	
			
			// upon first call only, get list of types:
			if (!window.isLoaded) {
				frameO.src = frameO.iSrc;
				// handle key presses
				window.onscroll = handleFrame;
				
			}
			window.isLoaded = true;
		}
	}
	function handleFrame(){
		alert(document.activeElement.tagName);
	}
	
	function popSelection(typeId){
		// from the kidId find parent kidId
		
		top.document.getElementById(top.kidId).value = event.srcElement.innerText;
		top.document.getElementById(top.vTypeId).value = typeId;
		top.document.getElementById(top.frameId).style.display = 'none';
		
		//top.document.getElementById(top.kidId).blur();
		curIndex = event.srcElement.index;
		
		if (window.selO) document.getElementById(window.selO).className = '';
		window.selO = event.srcElement.uniqueID;
		event.srcElement.className = 'selected';
		
	}
	function hideTypes(ref){
		var refO = (ref == 0) ? window : window.top;
		var frameO = refO.document.getElementById(refO.frameId);
		
		if (refO.document.activeElement != frameO )
			frameO.style.display = 'none';
	}
	
	
	function changeHref(nameId,attName,obj){
		// raed the hdId and append to obj href
		
		var hdO = document.getElementById(nameId);
		if (hdO){
			// append the search query, start from empty string everytime
			
			obj.setAttribute("search",searchQ(attName,hdO.value,obj.Href));
			
		}
	}
	// function to add attribute to the passed url
	function searchQ(attName,attValue,href){
		// find the real href, append to search string passed
		
		var ihref = (href.indexOf("?") > -1) ? "&" : "?";
		ihref += attName + "=" + attValue;
		return ihref;
	}
	

	
	function handleRadio(obj,trGroupId){
		// for the arguments passed, show hide trCosting elements
		var trGroup = document.getElementsByName(trGroupId);
		
		if (!obj.checked) return;
		if (trGroup){
			var j=2;
			
			for (var i=0; i<trGroup.length; i++){
				
				if (arguments[j] == i){
					trGroup[i].style.display = '';
					j++;
				}else{
					trGroup[i].style.display = 'none';
				}
			}
		}
	}
	
	function clickRadio(obj){
		var gRadio = document.getElementsByName(obj.name);
		
		if (gRadio){
			
			// click the one already selected
			for (var i=0; i<gRadio.length; i++){
				//alert(i + " "+ gRadio[i].checked);
				gRadio[0].click();
				if (gRadio[i].checked){ 

					gRadio[i].click();
				}
			}
		}
	}
	function changeDur(obj,id,txt){
		if (obj.checked)
			if (document.getElementById(id))
				document.getElementById(id).innerText = txt;
	}
	function handlePeriod(id,iRec){
		// read second value to determine what fields to show or hide
		if (event.type == 'pageload'){
			// read second attribute for type of recurrency
			var rec = iRec;
		}else{
			var rec = event.srcElement.Option.rec;
		}
		var recs0 = document.getElementById(id+0);
		var recs1 = document.getElementById(id+1);
		if (recs0)
			recs0.style.display = (rec == 0) ? "" : "none";
		if (recs1)
			recs1.style.display = (rec == 0) ? "none" : "";
		
	}
	/* take out
	function handleRec(id,eSrc){
		// read selected option rec, and show id equivalent
		var opId = eSrc.options[eSrc.selectedIndex].rec;
		if (opId){
			var obj = document.getElementsByName(id);
			if (obj){
				for (var i=0; i<obj.length; i++){
					if (parseInt(opId) == i+1) 
						obj[i].style.display = '';
					else
						obj[i].style.display = 'none';
				}
			}
		}
	}
	*/
	
	function budgetEmpty(){
		document.getElementById(event.srcElement.blockId).children[0].value = '';
	}
	
	var oPayM;
	function handlePayments(cId){
		// accordint to the selected item, change the appearanc of the page
		// gid for id of tboyd to show, and oPayM is the object to hide
		// cur is the currency to write in cId
		var oCurr = document.getElementById(cId);
		var oOption = event.srcElement.options[event.srcElement.selectedIndex];
		
		if (oCurr) oCurr.innerText = oOption.cur;
		
		var oPay = document.getElementById(oOption.gid);
		
		if (oPayM){
			oPayM.style.display = 'none';
		}
		if (oPay){
			oPay.style.display = '';
			oPayM = oPay;
		}
		
	}
		
	
	
	
	
	
	
	
	//testing
	function testCustome(){
		var rElem = document.forms[0].ayyash2;
		// read value then redirc
		for (i=0;i<rElem.length;i++){
			if (rElem[i].checked){
				if (rElem[i].value == 'typeA')
					location.href = 'position.htm';
				if (rElem[i].value == 'typeB')
					location.href = 'jobA.htm';
				if (rElem[i].value == 'typeC')
					location.href = 'jobC.htm';
				if (rElem[i].value == 'typeD')
					location.href = 'jobD.htm';
			}
		}
	}
	

	
	
	function showRedirect(id){
		// show options, and take some ids and set in hidden inputs as required
		// get redirectArea
		var elem = document.getElementById("redirectArea");
		if (elem){
			elem.style.display = "block";
			// position element
			var aElemRect = event.srcElement.getBoundingClientRect();
			elem.style.pixelTop = aElemRect.bottom;
			elem.style.pixelLeft = aElemRect.left - 2;
		}
	}
	
	// testing
	function testBudget(){
		var rElem = document.forms[0].testbutton;
		if (rElem[0].checked) location.href = 'budgetcc.htm';
		if (rElem[1].checked) location.href = 'budgetperiod.htm';
	}
	
	// testing function:
	function doZoom(){
		if (event.srcElement.style.zoom != 3){
			event.srcElement.style.zoom = 3;
			event.srcElement.style.position = "absolute";
			event.srcElement.style.zIndex= "10000";
			event.srcElement.style.marginLeft = "-1px";
		}else{
			event.srcElement.style.zoom = 1;
			event.srcElement.style.position = "static";
			event.srcElement.style.marginLeft = "0px";
		}
	}
	
	
	
	
function showHideTreeNodes(targetObj,pointerImg,sPath,force,stle)
	{
	var targs = document.getElementsByName(targetObj);
	var i;
	if (targs)
	{
		for (i=0;i<targs.length;i++)
		{
			
			if (!force){ 
			if (targs[i].currentStyle.display == 'none' )
			{
				if (pointerImg)
				{
					document.getElementById(pointerImg).src = sPath + 'iMinus.gif';
				}
				targs[i].style.display = 'block';
			}
			else
			{
				if (pointerImg)
				{
					document.getElementById(pointerImg).src = sPath + 'iPlus.gif';
				}
				targs[i].style.display = 'none';
			}
			}
			else
			{if (stle == 'block')
			{
				document.getElementById(pointerImg).src = sPath + 'iPlus.gif';
				targs[i].style.display = 'none';
			}
			else
			{  
				document.getElementById(pointerImg).src = sPath + 'iMinus.gif';
				targs[i].style.display = 'none';
	
			}
			}
			showHideTreeNodes(targs[i].myid,targs[i].imgname,sPath,true,targs[i].style.display);
		}
	}
}

