
//function to get object from id
  function getObj(sId){
		try{
			obj = document.getElementById(sId)
			return obj
		}catch(e){
			alert("Inget objekt \nMeddelande = "+ e.description)
		}
	}
//function to show IFRAME and load PAGE into it  
	function showIframe(sFrmId, sPage){
		document.frames(sFrmId).location = sPage;
		document.getElementById(sFrmId).style.visibility = "visible";
	}
//function to close IFRAME and load PAGE into it  
	function closeIFrame(sFrmId, sPage){
		parent.document.getElementById(sFrmId).style.visibility = "hidden";
		parent.document.frames[sFrmId].location.href = sPage;
	}

//function to resize Iframe with height and width from Iframe content
function resizeIframe(sIframeId, sReferenceTblId){
	parent.getObj(sIframeId).style.width  = getObj(sReferenceTblId).offsetWidth
	parent.getObj(sIframeId).style.height = getObj(sReferenceTblId).offsetHeight
}
function resizeDialog(sReferenceTblId){
	parent.dialogWidth = getObj(sReferenceTblId).offsetWidth + 9 +"px"
	parent.dialogHeight = getObj(sReferenceTblId).offsetHeight + 26 +"px"
}

//* Functions for the new way of displaying details windows *****************
//***************************************************************************
function CloseDetailsWindow(){
	SaveDetailsWindowPosition()
	opener.top.bolDetailsWindowIsOpen = false;
	opener.top.oDetailsWindowRef = null
	window.close()
}
function CloseOpenerIFrameFromDetailsWindow(sFrmId, sPage){
	opener.document.getElementById(sFrmId).style.visibility = "hidden";
	opener.document.frames[sFrmId].location.href = sPage;
}
function OpenDetailsWindow(sURL, iWidth, iHeight, iLeft, iTop, bResizable, bScrollbars, bStatus){
	CloseAnyDetailsWindow()
	
	var intWidth, intHeight, intLeft, intTop, strResizable, strStatus, strScrollbars;
	try{
		intWidth  = (iWidth != undefined) ? iWidth : 300;
		intHeight = (iHeight != undefined) ? iHeight : 200;
		intLeft   = (iLeft != undefined) ? iLeft : top.arrAddLeftTop[0] - 3;
		intTop    = (iTop != undefined) ? iTop : top.arrAddLeftTop[1] - 22;
		strResizable = (bResizable != undefined) ? "yes" : "no";
		strStatus    = (bStatus != undefined) ? "yes" : "no";
		strScrollbars    = (bScrollbars != undefined) ? "yes" : "no";

		//JT:20040212:Added HTTP_REFERER parameter
		//****************************************
		//** Add the HTTP_REFERER parameter to the querystring, for use in VB.NET!
		if ((sURL.indexOf(".aspx?", 0) > 0) || (sURL.indexOf(".htm?", 0) > 0) || (sURL.indexOf(".html?", 0) > 0)) {
			sURL = sURL + "&";
		}
		else {
			sURL = sURL + "?";
		}
		sURL = sURL + "HTTP_REFERER=" + document.location.pathname; //** old faulty way >>> document.location; //** add the HTTP_REFERER variabel
		//***************************************
		
		//JT:20040218:Added PRIREFERER parameter
		//****************************************
		//** Add the PRIREFERER parameter to the querystring, for use in VB.NET!
		if ((sURL.indexOf(".aspx?", 0) > 0) || (sURL.indexOf(".htm?", 0) > 0) || (sURL.indexOf(".html?", 0) > 0)) {
			sURL = sURL + "&";
		}
		else {
			sURL = sURL + "?";
		}
		sURL = sURL + "PRIREFERER=DETAIL"; //** add the PRIREFERER variabel
		//***************************************
		
		top.oDetailsWindowRef = window.open(sURL,"PRIWebDetails","top="+ intTop +",left="+ intLeft +",width="+ intWidth +",height="+ intHeight +",location=no,menubar=no,resizable="+ strResizable +",status="+ strStatus +",scrollbars="+ strScrollbars +",titlebar=no")
		
		top.bolDetailsWindowIsOpen = true;
	}catch(e){
		if(top.oDetailsWindowRef != null){
			top.oDetailsWindowRef.close();
			top.bolDetailsWindowIsOpen = false;
		}
	}
}
function SaveDetailsWindowPosition(){
	try{
		opener.top.bolDetailsWindowIsOpen = false;
		opener.top.arrAddLeftTop[0] = window.screenLeft
		opener.top.arrAddLeftTop[1] = window.screenTop
	}catch(e){
	
	}
}
function RefreshOpenerUsingShowPageBtn(){
	opener.document.forms["Form1"].PRIGridUserControl1_btnShowPage.click();
}
function resizeWindow(sReferenceTblId){
	document.title = "PRI Web"
	opener.top.bolDetailsWindowIsOpen = true;
	var x = getObj(sReferenceTblId).offsetWidth + opener.top.arrAddWidthHeight[0]
	var y = getObj(sReferenceTblId).offsetHeight + opener.top.arrAddWidthHeight[1]
	window.resizeTo(x, y)
	if(TestClientWidthAndHeight(x, y) == false){
		ChangeClientWidthAndHeight(x, y)
	}
}
function TestClientWidthAndHeight(iWidth, iHeight){
	var x = window.document.body.clientWidth + opener.top.arrAddWidthHeight[0]
	var y = window.document.body.clientHeight + opener.top.arrAddWidthHeight[1]
	if((iWidth == x) && (iHeight == y)){
		return true
	}else{
		return false
	}
}
function ChangeClientWidthAndHeight(iWidth, iHeight){
	var xNew, yNew;
	var x = window.document.body.clientWidth
	var y = window.document.body.clientHeight
	if(iWidth != x){
		xNew = iWidth - x;
	}
	if(iHeight != y){
		yNew = iHeight - y;
	}
	window.resizeBy(xNew, yNew)
	opener.top.arrAddWidthHeight[0] = xNew
	opener.top.arrAddWidthHeight[1] = yNew
}
function RefreshDetailsMatrix(){
	document.forms["Form1"].PRIGridUserControl1_btnShowPage.click();
}
//*****************************************************************************

//creates timestamp for DB
function getTimeStamp(){
	strTimeStamp = new Date().getTime();
	document.forms["Form1"].PRIGridUserControl1_txtTimeStamp.value = strTimeStamp;
	document.forms["Form1"].btnSave.click();
}
//show list when layout is set
	function showGridTbl(sId){
    getObj(sId).style.visibility = "visible"
	}
//show list header when layout is set
	function showGridTblHeader(sId){
		getObj(sId).width = getObj("listHead").offsetWidth
    getObj(sId).style.visibility = "visible"
	}

//show list when layout is set
	function hideTable(sId){
    getObj(sId).style.visibility = "hidden"
	}
//show list when layout is set
	function showTable(sId){
    getObj(sId).style.visibility = "visible"
	}

//refreshes parent page
	function refreshParentUsingShowPageBtn(){
		CloseAnyDetailsWindow()
		parent.document.forms["Form1"].PRIGridUserControl1_btnShowPage.click();
	}
	function RefreshMatrix(){
		CloseAnyDetailsWindow()
		parent.document.frames("FS3_MIDDLE").location.reload()
		document.forms["Form1"].PRIGridUserControl1_btnShowPage.click();
	}
	function CloseAnyDetailsWindow(){
		try{
			top.oDetailsWindowRef.close()
		}catch(e){}
	}

	function RefreshJobLog(intRefreshTheJobLog,intShowMessage, Message){ //JT:20041007:added
		try{
			var sValue = getObj("txtRefreshJobLog").value;
			var arrRefreshSplit = sValue.split("#prisep#");
			try{
				if(intRefreshTheJobLog == null){
					//Use the value from txtRefreshJobLog.
					intRefreshTheJobLog = arrRefreshSplit[0];
				}
			}catch(e){}
			try{
				if(intShowMessage == null){
					//Use the value from txtRefreshJobLog.
					intShowMessage = arrRefreshSplit[1];
				}
			}catch(e){}
			try{
				if(Message == null){
					//Use the value from txtRefreshJobLog.
					Message = arrRefreshSplit[2];
				}
			}catch(e){}
			
			if(intRefreshTheJobLog == "1"){
				RefreshWorkViewer();
				//show message if needed.
				if(intShowMessage == "1"){
					if(Message == null){
						EMsg("Jobbet lagt i k&ouml;.");
					}else{
						EMsg(Message);
					}
				}
			}
		}catch(e){}
	}	
	
	function RefreshJobLogFromControl(){
		try{
			var oHiddenField = getObj("txtRefreshJobLogFromControl");
			
			if(oHiddenField != null){
				if(oHiddenField.value == "1"){
					EMsg("Jobbet lagt i k&ouml;.");
					RefreshWorkViewer();
				}
			}
		}catch(e){
		}
	}	

	function RefreshWorkViewer(){
		var test
		try{
			test = opener.top
		}catch(e){
			test = null
		}
		if(test == null){
			top.frames("FS3_MIDDLE").location.reload()
		}else{
			opener.top.frames("FS3_MIDDLE").location.reload()
		}
	}

//handles delete button in DETAILS popup
	function deleteThisDetail(){
		if(confirm("Ta bort denna post?")){
			__doPostBack('btnDelete','')
		}
	}
//handles checkbox settings at create new and copy 
	function setChkChoices(obj){
		strChkId = obj.id
		bolChecked = obj.checked
		//alert(strChkId + ", " + bolChecked)
		if(strChkId == "chk1" && bolChecked == false){
			document.forms["Form1"].chk2.checked = false
		}else if(strChkId == "chk2" && bolChecked == true){
			document.forms["Form1"].chk1.checked = true
		}
	}
//Handles Error messages
	function EMsg(strMessage, bolRefreshWindow){
		iW = document.body.clientWidth / 2 - (300/2);
		iH = document.body.clientHeight / 2 - (250/2);
		if(bolRefreshWindow == true){
			//alert(strMessage)
			//alert(parent.refreshParentUsingShowPageBtn)
			strHTML = '<TABLE id="tblMsg" width="300" height="250" style="border:solid 2px;border-color:#c6cfde #003366 #003366 #c6cfde ;background-color:#336699;padding:1px;" id="tblReplace" cellSpacing="1" cellPadding="2" border="0"><TR><TD vAlign="top" align="right" height="22"><table style="border:solid 2px;border-color:#FFFFFF #404040 #404040 #ffffff;height:19px;background-color:#D4D0C8;padding:2px 5px 2px 5px;cursor:default;" cellSpacing="2" cellPadding="0" width="100%" border="0"><tr><td style="font-family:Verdana;font-size:12px;color:black;font-weight:bold;">Meddelande</td><td align="right"><A href="#" onclick="parent.oPopupErrorMsgBox.hide()"><IMG height="16" src="../images/scrollbar/btnClose.gif" width="17" border="0"></A></td></tr></table></TD></TR><tr><td valign="top" height="184"><TABLE width="100%" height="184" style="border:solid 1px;border-color:#FFFFFF;height:16px; background-color:#D4D0C8;padding:2px 5px 2px 5px;cursor:default;" cellSpacing="2" cellPadding="0" border="0" width="100%"><TR><TD height="174" valign="top" style="padding:4px 6px 4px 6px;text-align:left;color:#000000;font-family:verdana;font-weight:normal;font-size:10px">'+ strMessage +'</TD></TR></TABLE></td></tr><tr><td align="right" height="22"><INPUT id="btnClose" type="button" value="Ok" name="btnClose" style="font-family:verdana; font-size:11px;color:black;background-color:#D6E7EF;border:2px solid;border-color:#FFFFFF #3F3F3F #3F3F3F #FFFFFF;margin:1px;height:20px;width:70px;" onclick="parent.refreshParentUsingShowPageBtn();parent.oPopupErrorMsgBox.hide();"></td></tr></TABLE>';
		}else{
			strHTML = '<TABLE id="tblMsg" width="300" height="250" style="border:solid 2px;border-color:#c6cfde #003366 #003366 #c6cfde ;background-color:#336699;padding:1px;" id="tblReplace" cellSpacing="1" cellPadding="2" border="0"><TR><TD vAlign="top" align="right" height="22"><table style="border:solid 2px;border-color:#FFFFFF #404040 #404040 #ffffff;height:19px;background-color:#D4D0C8;padding:2px 5px 2px 5px;cursor:default;" cellSpacing="2" cellPadding="0" width="100%" border="0"><tr><td style="font-family:Verdana;font-size:12px;color:black;font-weight:bold;">Meddelande</td><td align="right"><A href="#" onclick="parent.oPopupErrorMsgBox.hide()"><IMG height="16" src="../images/scrollbar/btnClose.gif" width="17" border="0"></A></td></tr></table></TD></TR><tr><td valign="top" height="184"><TABLE width="100%" height="184" style="border:solid 1px;border-color:#FFFFFF;height:16px; background-color:#D4D0C8;padding:2px 5px 2px 5px;cursor:default;" cellSpacing="2" cellPadding="0" border="0" width="100%"><TR><TD height="174" valign="top" style="padding:4px 6px 4px 6px;text-align:left;color:#000000;font-family:verdana;font-weight:normal;font-size:10px">'+ strMessage +'</TD></TR></TABLE></td></tr><tr><td align="right" height="22"><INPUT id="btnClose" type="button" value="Ok" name="btnClose" style="font-family:verdana; font-size:11px;color:black;background-color:#D6E7EF;border:2px solid;border-color:#FFFFFF #3F3F3F #3F3F3F #FFFFFF;margin:1px;height:20px;width:70px;" onclick="parent.oPopupErrorMsgBox.hide()"></td></tr></TABLE>';
		}
		oPopupErrorMsgBox = window.createPopup();
		oPopupErrorMsgBox.document.body.innerHTML = strHTML; 
		oPopupErrorMsgBox.show(iW, iH, 300, 250, document.body);
	}

	function GetCurrentPageNumber_CPN(){
		strQuery = "&CPN="+ document.forms["Form1"].PRIGridUserControl1_txtPageToDisplay.value
		return strQuery
	}	
	function GetActiveRow_AR(){
		strQuery = "&AR="+ activeRow.index
		return strQuery
	}	
	function GetSupplierId_SUPID(strFormFieldName){
		strQuery = "&SUPID="+ document.forms["Form1"].elements[strFormFieldName].value
		return strQuery
	}	
	function GetActiveRowIndex(){
		return activeRow.index
	}	
	function GetDGLeft(){
		return getObj("PRIGridUserControl1_DGLeft")
	}
	function GetDGRight(){
		return getObj("PRIGridUserControl1_DGRight")
	}

	function SetBGColorForDetailsPage(sID){
		//getObj(sID).style.backgroundColor = "#aaaaaa"
		getObj(sID).style.backgroundColor = "#6699bb"
	}
	function SetShadowForDetailsPage(sID, bolSetShadow){
		if(bolSetShadow == true){
			parent.getObj(sID).style.filter = "progid:DXImageTransform.Microsoft.Shadow(color='#111111', Direction=135, Strength=10)"
		}else{
			parent.getObj(sID).style.filter = ""
		}
	}
	function GetActiveRowId(){
		try{
			sRowId = "nr"+ activeRow.index;
			RowId = getObj(sRowId).sId;
			return RowId
		}catch(e){
			EMsg("Det finns inga rader i matrisen.")
			return ""
		}
	}
	function MatrixHasActiveRow(bolDontShowStandardEMsg){
		try{
			sRowId = "nr"+ activeRow.index;
			RowId = getObj(sRowId).sId;
			return true
		}catch(e){
			if(bolDontShowStandardEMsg == undefined || bolDontShowStandardEMsg == false){
				EMsg("Det finns inga rader i matrisen.")
				return false
			}else{
				return false
			}
		}
	}
	function MatrixHasSelectedRows(bolDontShowStandardEMsg){
		try{
			if(!oFrm.sSelectedRows.length > 0 && !oFrm.bIsInverted){
				if(bolDontShowStandardEMsg == undefined || bolDontShowStandardEMsg == false){
					EMsg("Du har inte markerat n&aring;gon/n&aring;gra rader.")
				}
				return false
			}else{
				return true
			}
		}catch(e){
			EMsg("Fel uppstod i funktion <b>MatrixHasSelectedRows</b>.")
			return false
		}
	}	
	function GetMatrixFormValue(strCbo, strType){
		var o = getObj(strCbo)
		var strValue = ""
		if(strType == "cbo"){
			strValue = o.options[o.selectedIndex].value
		}else if(strType == "txt"){
			strValue = o.value
		}
		return strValue
	}
	function GetActiveRowAndCurrentPage(){
		var strValue = ""
		try{
			var AR = activeRow.index
			var CP = GetMatrixFormValue("PRIGridUserControl1_txtPageToDisplay", "txt")
			strValue = "&AR="+ AR +"&CP="+ CP	
			return strValue
		}catch(e){
			return strValue
		}
	}
	function SetRowIdToDeleteListIds(strRowId){
		document.forms["Form1"].txtDeleteListIds.value = strRowId +"###"
	}
	
	function OpenSpecificHelpPage(){
		try{
			var strCompleteURL = document.location.toString();
			var strQueryString = document.location.search;
			var strURL = strCompleteURL.replace(strQueryString, "");
			var arrURLSplit = strURL.split("/");
			var iLength = arrURLSplit.length - 1;
			var strPageName = arrURLSplit[iLength];
			var strURLtoOpen = "../HELP/GetSpecificHelp.aspx?PageName="+ strPageName;
			window.open(strURLtoOpen, "_blank", "top=100,left=100,width=550,height=400,scrollbars=yes,location=no,menubar=no,resizable=yes,status=yes,titlebar=no")
		}catch(e){
			EMsg("Fel uppstod vid hämtning av hjälpsidan.<br>"+ e.description)
		}
	}
	function OpenHelpPageForService(ServiceId){
		try{
			var strURLtoOpen = "../HELP/GetSpecificHelp.aspx?ServiceId="+ ServiceId;
			window.open(strURLtoOpen, "_blank", "top=100,left=100,width=550,height=400,scrollbars=yes,location=no,menubar=no,resizable=yes,status=yes,titlebar=no")
		}catch(e){
			EMsg("Fel uppstod vid hämtning av hjälpsidan.<br>"+ e.description)
		}
	}

//function to handle print from Matrix
	function PrintMatrix(){
		var oForm, ModalVariable, arrModalVariables, SelectionType, MatrixId
		
		try{
		
			oForm = document.forms["Form1"]
			
			if(MatrixHasSelectedRows(true)){
				SelectionType = "?stype=1"
			}else if(MatrixHasActiveRow(true)){
				SelectionType = "?stype=2"
			}else{
				SelectionType = "?stype=2"
			}
		
			MatrixId = "&mid="+ oForm.PRIGridUserControl1_txtMatrixId.value
			
			ModalVariable = window.showModalDialog("../PrintTemplateManagement/SelectPrintTemplate.aspx"+ SelectionType + MatrixId,"","dialogHeight:150px; dialogWidth:370px; center:yes; resizable:no; scroll:no; status:no; help:no; unadorned:no")
			
			if(ModalVariable != undefined && ModalVariable != ""){
				oForm.PRIGridUserControl1_txtPrintModalVariables.value = ModalVariable;
				
				ModalVariable = ModalVariable.toString();
				arrModalVariables = ModalVariable.split("#:#");
				SelectionType = arrModalVariables[1];
				
				if(SelectionType == "1"){
					// Selected rows
					oForm.PRIGridUserControl1_txtPrintSelectedRows.value = oFrm.sSelectedRows;
					oFrm.sSelectedRows = "";
					oFrm.bIsInverted = false;
					oForm.PRIGridUserControl1_btnPrint.click();
				}else if(SelectionType == "2"){
					// Activ row exists
					oFrm.sSelectedRows = "";
					oFrm.bIsInverted = false;
					RowId = GetActiveRowId();
					oForm.PRIGridUserControl1_txtPrintSelectedRows.value = RowId +"###";
					oForm.PRIGridUserControl1_btnPrint.click();
				}else if(SelectionType == "3"){
					oFrm.sSelectedRows = "";
					oFrm.bIsInverted = false;
					oForm.PRIGridUserControl1_txtPrintSelectedRows.value = "";
					oForm.PRIGridUserControl1_btnPrint.click();
				}
			}
		}catch(e){
			alert(e.description)
		}
	}
	
	function EditMatrix(){
		var sQueryString, oForm;
		try{
			oForm = document.forms["Form1"];
			
			sQueryString = "../PRIAdmin/MatrixStandardListDetails.aspx";
			sQueryString += "?RowId="+ oForm.PRIGridUserControl1_txtMatrixId.value +":";
			sQueryString += "&action=edit";
			
			OpenDetailsWindow(sQueryString)

		}catch(e){
			alert(e.description)
		}
	}

	function SetFocusToField(){
		try{
			// Set focus on specified field
			strFieldName = document.getElementById("lblFieldNameToFocus").innerText
			document.getElementById(strFieldName).focus()
		}catch(e){
		} 
	}
		
	var oPRIToolTip = {
		intTimeBeforeHide : 1000,
		intTimeOutId			: null,
		oToolTipPopup			: null, 
		oSpan							: null, 
		oDivContainer			: null,
		
		Show : function(o){
			try{
				if(this.intTimeOutId != null){ 
					window.clearTimeout(this.intTimeOutId)
				};
				
				var strPRIToolTip = o.PRIToolTip;
				var intTop   = event.clientY;
				var intLeft  = event.clientX;
				this.oSpan = document.getElementById("spnToolTipJs");
				
				if(this.oSpan == null){
					//Create hidden container element to document body
					this.oDivContainer								  = document.createElement("DIV");
					this.oDivContainer.id							  = "divToolTipContainer";
					this.oDivContainer.style.visibility = "hidden";
					this.oDivContainer.style.width			= "200px";
					document.body.appendChild(this.oDivContainer);
					
					//Create actual ToolTip to be shown and add element into container
					this.oSpan											 = document.createElement("SPAN");
					this.oSpan.id										 = "spnToolTipJs";
					this.oSpan.style.border					 = "solid 1px black";
					this.oSpan.style.backgroundColor = "white";
					this.oSpan.style.padding				 = "2px 5px 2px 3px";
					this.oSpan.style.fontFamily			 = "verdana";
					this.oSpan.style.fontSize				 = "10px";
					this.oDivContainer.appendChild(this.oSpan);
				}
				
				this.oSpan.innerText  = strPRIToolTip;
				
				this.oToolTipPopup = window.createPopup();
				this.oToolTipPopup.document.body.style.border					 = "solid 1px black";
				this.oToolTipPopup.document.body.style.backgroundColor = "white";
				this.oToolTipPopup.document.body.style.padding				 = "2px 5px 2px 3px";
				this.oToolTipPopup.document.body.style.fontFamily			 = "verdana";
				this.oToolTipPopup.document.body.style.fontSize				 = "10px";

				this.oToolTipPopup.document.body.innerHTML = strPRIToolTip;
				iWidth  = o.offsetWidth;
				iHeight = o.offsetHeight;
				this.oToolTipPopup.show(50, 25, this.oSpan.offsetWidth, this.oSpan.offsetHeight, o);

			}catch(e){
				alert(e.description)
			}
		},
		
		Hide : function(){
			try{
				if(this.oToolTipPopup != null){
					if(this.oToolTipPopup.isOpen){
						this.intTimeOutId = window.setTimeout("oPRIToolTip.oToolTipPopup.hide()", this.intTimeBeforeHide)
					}
				}
			}catch(e){
				alert(e.description)
			}
		}
	}
