var xmlHttp = null;
var ibrNS4 = (navigator.appName == "Netscape" && parseInt(navigator.appVersion) < 5);
var ibrIE = (navigator.userAgent.search(/MSIE/i) >= 0); 

function ucfTrim(str, chars)
{
	return ucfLtrim(ucfRtrim(str, chars), chars);
}
 
function ucfLtrim(str, chars)
{
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function ucfRtrim(str, chars)
{
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function ucfPutfocus(oObj)
{
	if (document.forms.length > 0)
	{
		oObj.focus();
		
		if (oObj.type == "text")
		{
			oObj.select();			
		}
	 }
}
	
// 08.05.2005 : PHM : Check valid email address
function ucfCheckemail(oObj)
{
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(oObj.value))
	{
		return (true);
	}
	else
	{
		alert(msg_invalid_email);
		ucfPutfocus(oObj);
		return (false);
	}
}

function ucfCheckURL(oItem)
{ 
	var v = new RegExp(); 

	v.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$"); 

	if (v.test(oItem.value))
	{ 
		return true; 
	}
	else
	{
		alert(msg_invalid_url); 
		return false; 
	}
} 	

function ucfLoginprocess(oForm)
{
	var bOk = true;
	
	if (bOk)
	{
		if (ucfTrim(oForm.login.value) == '')
		{
			alert(msg_missed_value);
			ucfPutfocus(oForm.login);
			bOk = false;
		}
	}
	
	if (bOk)
	{
		if (ucfTrim(oForm.password.value) == '')
		{
			alert(msg_missed_value);
			ucfPutfocus(oForm.password);
			bOk = false;
		}
	}
	
	return bOk;
}

// 08.05.2005 : PHM : Logout confirmation
function ucfLogoutprocess()
{
	if (confirm(msg_logout_confirm))
	{
		document.frmheader.action="logout_process.php";
		document.frmheader.submit();
	}
}

// 08.05.2005 : PHM : Language change action	
function ucfLangprocess()
{
	{
		document.frmheader.currenturl.value = document.URL;
		document.frmheader.action="lang_process.php";
		document.frmheader.submit();
	}
}

function ucfBookmarksite(title,url)
{
	// firefox
	if (window.sidebar)
	{
		window.sidebar.addPanel(title, url, "");
	}
	// opera
	else if(window.opera && window.print)
	{ 
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	} 
	// ie
	else if(document.all)
	{
		//alert(url + " - " + title);
		window.external.AddFavorite(url, title);
	}
}

// 07.06.2004 : PHM : Is input a number ?
function isitNumber(oObj, needed, zerocheck, deccheck, lminmaxonlycheck, lmin, lmax, rmin, rmax)
{
	var iCnt = 0;
	var iDecimal = 0;
	var bError = false;
	var iSize = 0;
	var sMsg = "";
	var sTmp = "";

	if ( (bError == false) && (oObj == null) )
	{
		bError = true;
		sMsg = "";
	}

	sTmp = oObj.value;
	iSize = String(sTmp).length;

	if (bError == false)
	{
		for (iCnt=0;iCnt<iSize;++iCnt)
		{
			if((sTmp.charAt(iCnt) >= "0" && sTmp.charAt(iCnt) <= "9") || sTmp.charAt(iCnt) == ".")
			{
				bError = false;
			}
			else
			{
				bError = true;
				sMsg = msg_invalid_data_format;
				break;
			}
		}
	}

	if ( (bError == false) && (zerocheck) )
	{
		if (sTmp < "1")
		{
			//alert("zero");
			bError = true;
			sMsg = msg_invalid_data_format;
		}
	}

	if ( (bError == false) && (deccheck == true) )
	{
		//alert(sTmp);
		(lmin == '*')? lmin = 0: lmin = parseInt(lmin);
		(lmax == '*')? lmax = '': lmax = parseInt(lmax);
		(rmin == '*')? rmin = 0: rmin = parseInt(rmin);
		(rmax == '*')? rmax = '': rmax = parseInt(rmax);

		//alert("lmin : " + lmin + " / " + "lmax : " + lmax + " / " + "rmin : " + rmin + " / " + "rmax : " + rmax);

		iDecimal = sTmp.indexOf(".");

		//alert(iDecimal);

		if (iDecimal > 0)
		{
			slValue = sTmp.substring(0,iDecimal);
			srValue = sTmp.substring(iDecimal+1);
		}
		else
		{
			slValue = sTmp;
			srValue = "";
		}

		//alert(slValue + " / " + srValue);

		if (slValue != "")
		{
			if ( (slValue < lmin) || (slValue > lmax) )
			{
				bError = true;
				sMsg = msg_invalid_data_format;
			}
		}

		if (srValue != "")
		{
			if ( (srValue < rmin) || (srValue > rmax) )
			{
				bError = true;
				sMsg = msg_invalid_data_format;
			}
		}
	}
	else
	{
		//alert(sTmp);
		if (lminmaxonlycheck && ((sTmp < lmin) || (sTmp > lmax)))
		{
			bError = true;
			sMsg = msg_invalid_data_format;
		}
	}

	//alert(sTmp);
	
	if ((sTmp.length == 0) && needed)
	{
		bError = true;
		sMsg = msg_required_data;
	}

	if (bError)
	{
		alert(msg_invalid_data_format);
		oObj.focus();
	}

	return !bError;
}

// 15.05.2002 : PHM : Is input a number (without message) ?
function lisitNumber(iNumber)
{
	var iCnt = 0;
	var bError = false;
	var thesize = String(iNumber).length;
	for (iCnt=0;iCnt<thesize;++iCnt)
	{
		if(iNumber.charAt(iCnt) < "0" || iNumber.charAt(iCnt) > "9")
		{
			bError = true;
		}
	}
	if (bError)
	{
	   return false;
	}
	else
	{
		return true;
	}
}


// 15.05.2002 : PHM : Is input a date ?
function isitDate(oObj, needed, dateformat)
{
	var sTmp = "";
	var tmpArr = new Array();
	var iCnt1 = 0;
	var iCnt2 = 0;
	var iCnt3 = 0;
	var bError = false;
	var sDelimitor = "";

//	alert(dateformat);

	if (oObj == null)	
	{
		return(true);
	}

	sTmp = oObj.value;

	if ((sTmp.length == 0) && needed)
	{
		alert(msg_required_data);
		oObj.focus();
		// oObj.select();
		return false;
	}
	else
	{
		if (sTmp != "")
		{
			sDelimitor = dateformat.substr(1,1);
	
			if (dateformat.substr(0,1) == "d")
			{
				iCnt1 = 0;
				iCnt2 = 1;			
				iCnt3 = 2;			
			}
			else if (dateformat.substr(0,1) == "m")
			{
				iCnt1 = 1;
				iCnt2 = 0;			
				iCnt3 = 2;			
			}
			else if (dateformat.substr(0,1) == "Y")
			{
				if (dateformat.substr(2,1) == "d")
				{
					iCnt1 = 1;
					iCnt2 = 2;			
					iCnt3 = 0;			
				}
				else if (dateformat.substr(2,1) == "m")
				{
					iCnt1 = 2;
					iCnt2 = 1;			
					iCnt3 = 0;			
				}
			}
	
			tmpArr = sTmp.split(sDelimitor);
	
			if (tmpArr[iCnt1] != null && lisitNumber(tmpArr[iCnt1]))
			{
				if ((tmpArr[iCnt1] > 31) || (tmpArr[iCnt1] < 1))
				{
					bError = true;
				}
			}
			else
			{
				bError = true;
			}
	
			if (tmpArr[iCnt2] != null && lisitNumber(tmpArr[iCnt2]))
			{
				if ((tmpArr[iCnt2] > 12) || (tmpArr[iCnt2] < 1))
				{
					bError = true;
				}
			}
			else
			{
				bError = true;
			}
	
			if (tmpArr[iCnt3] != null && lisitNumber(tmpArr[iCnt3]))
			{
				if (tmpArr[iCnt3].length < 4)
				{
					bError = true;
				}
			}
			else
			{
				bError = true;
			}
		}
	}	

	if (bError)
	{
		alert(msg_invalid_data_format);
		oObj.focus();
		// oObj.select();
		return false;
	}
	else
	{
		return true;
	}
}

// 07.06.2004 : PHM : Is input a required value ?
function checkValue(oObj, bNospacecheck, bNoexoticcheck)
{
	var bOk = true;

	if (bNospacecheck == null)
	{
		bNospacecheck = false;
	}

	if (bNoexoticcheck == null)
	{
		bNoexoticcheck = false;
	}

	if (bOk)
	{
		if (oObj == null)
		{
			bOk = false;
		}
	}
	
	sTmp = ucfTrim(oObj.value);

	if (bOk)
	{
		if (sTmp.length == 0)
		{
			alert(msg_required_data);
			oObj.focus();
			bOk = false;
		}
	}

	if (bOk)
	{
		if (bNospacecheck || bNoexoticcheck)		
		{
			if (sTmp.indexOf(" ") != -1)
			{
				alert(msg_no_space_required_data);
				oObj.focus();
				bOk = false;
			}
		}
	}

	if (bOk)
	{
		if (bNoexoticcheck)		
		{
			var iCnt = 0;
			var iSize = sTmp.length;
			for (iCnt=0;iCnt<iSize;++iCnt)
			{
				if(sTmp.charAt(iCnt) < "0" || sTmp.charAt(iCnt) > "9")
				{
					if(sTmp.charAt(iCnt) < "a" || sTmp.charAt(iCnt) > "z")
					{
						if(sTmp.charAt(iCnt) < "A" || sTmp.charAt(iCnt) > "Z")
						{
							alert(msg_no_exotic_required_data);
							oObj.focus();
							bOk = false;
							break;
						}
					}
				}
			}
		}
	}

	return bOk;
}

// 26.09.2002 : PHM : Check checked checkbox
function checkChkItems(frm ,chkPrefix, minNr, alertswitch)
	// 26.09.2002 : PHM : formInst : form nr
	// 26.09.2002 : PHM : ckkPrefix : selection criteria
{
//        var frm = document.forms[formInst];
	var i;
	var iCnt = 0;
	var iElem = frm.elements.length;
	var iPrefixLen = String(chkPrefix).length;

	if (alertswitch == null)
	{
		alertswitch = true;
	}

	// 26.09.2002 : PHM : Loop on items
	for (i=0;i<=iElem-1;i=i+1)
	{
		// 26.09.2002 : PHM : Checkbox and good id ?
		if ((frm.elements[i].type == "checkbox") & (frm.elements[i].id.substr(0,iPrefixLen) == chkPrefix))
		{
			// 26.09.2002 : PHM : Checked ?
			if (frm.elements[i].checked == true)
			{
				iCnt++;
			}
		}
	}

	// 12.06.2003 : PHM : Message for minimum nr to select
	if (minNr > 0)
	{
		// 26.09.2002 : PHM : Message for no selected item
		if (iCnt < minNr)
		{
			if (alertswitch)
			{
				alert(msg_min_selected_item +  " : " + minNr);
			}
		}
	}
	else if (minNr == 0)
	{
		// 26.09.2002 : PHM : Message for no selected item
		if (iCnt == 0)
		{
			if (alertswitch)
			{
				alert(msg_no_selected_item);
			}
		}
	}
	else if (minNr == -1)
	{
		iCnt = 1;
	}

	return iCnt;
}

function ucCheckRadio(frm, sId, sValue)
{
	var bOk = false;
	var i;
	var iCnt = 0;
	var iElem = frm.elements.length;
						
	// 26.09.2002 : PHM : Loop on items
	for (i=0;i<=iElem-1;i=i+1)
	{
		// 26.09.2002 : PHM : Checkbox and good id ?
		if ((frm.elements[i].type == "radio") & (frm.elements[i].id == sId) & (frm.elements[i].value == sValue))
		{
			// 26.09.2002 : PHM : Checked ?
			frm.elements[i].checked = true;
			break;
		}
	}
}

function showHideTreeItem(subText)
{
	var textBlock=document.getElementById(subText);
	var imgSign=document.getElementById('gif'+subText);
	if(textBlock.style.display=='block')
	{
		textBlock.style.display='none';
		imgSign.src='images/plus_micro.gif';
		imgSign.alt=cap_click_expand;
	}
	else
	{
		textBlock.style.display='block';
		imgSign.src='images/minus_micro.gif';
		imgSign.alt=cap_click_collapse;
	}
}

function toggleItemsAll(chkAll, titlename, questionname, answername, itemEntryCount)
{
	var sa = document.getElementById(chkAll);
	var ch = sa.checked;
	toggleItems(ch, chkAll, titlename, questionname, answername, itemEntryCount);
}

function toggleItems(checked, chkAll, titlename, itemname, detailname, itemEntryCount)
{
	var secCount = 1;

	for (var i=1; i<=itemEntryCount; i++)
	{
		var title = document.getElementById(titlename + i);

		if(checked == true)
		{
			expandItem(titlename + i, itemname + i, detailname + i);
		}
		else
		{
			collapseItem(titlename + i, itemname + i, detailname + i);
		}
	}

	if(checked == false)
	{
		var sa = document.getElementById(chkAll);
		sa.checked = false;
	}
}

function toggleItem(tName, iName, dName, chkAll)
{
	var title = document.getElementById(tName);
	if (title == null)
	{
		return;
	}

	var detail = document.getElementById(dName);
	if (detail == null)
	{
		return;
	}

	var iItem = document.getElementById(iName);
	if (detail.style.display == '')
	{
		if (iItem != null)
		{
			iItem.style.display = 'none';
		}

		detail.style.display = 'none';

        var sa = document.getElementById(chkAll);

		if (sa != null)
		{
	        sa.checked = false;
		}
	}
	else
	{
		if (iItem != null)
		{
			iItem.style.display = '';	
		}
		detail.style.display = '';
	}
}

function expandItem(tName, iName, dName)
{
	var title = document.getElementById(tName);
	if (title == null)
	{
		return;
	}

	var detail = document.getElementById(dName);
	if (detail == null)
	{
		return;
	}

	var iItem = document.getElementById(iName);
	if (iItem != null)
	{
		iItem.style.display = '';
	}
	detail.style.display = '';
}

function collapseItem(tName, iName, dName)
{
	var title = document.getElementById(tName);
	if (title == null)
	{
		return;
	}

	var detail = document.getElementById(dName);
	if (detail == null)
	{
		return;
	}

	var iItem = document.getElementById(iName);
	if (iItem != null)
	{
		iItem.style.display = 'none';
	}
	detail.style.display = 'none';

}

// 13.06.2004 : PHM : Popup window
function popupWin(mypage, myname, w, h, status, scrollbars, resizable, directories, toolbar, location, menubar, w_delta, h_delta)
{
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;

	if (status == null)
	{
		status = 'yes';
	}

	if (scrollbars == null)
	{
		scrollbars = 'yes';
	}

	if (resizable == null)
	{
		resizable = 'yes';
	}

	if (directories == null)
	{
		directories = 'yes';
	}

	if (toolbar == null)
	{
		toolbar = 'yes';
	}

	if (location == null)
	{
		location = 'yes';
	}

	if (menubar == null)
	{
		menubar = 'yes';
	}

	if (w_delta != null)
	{
		winl = winl + w_delta;
	}

	if (h_delta != null)
	{
		wint = wint + h_delta;
	}

	winprops ='height='+h+',width='+w+',top='+wint+',left='+winl+',status='+status+',scrollbars='+scrollbars+',resizable='+resizable+',directories='+directories+',toolbar='+toolbar+',location='+location+',menubar='+menubar;

	win = window.open(mypage, myname, winprops);

	win.window.focus();
}

// 13.06.2004 : PHM : Popup help window
function popupHelpWin(topic, width, height)
{
	if (width==null) width = 800;
	if (height==null) height = 500;

	var url = "help.php?uclayout=popup";
	
	if ((topic != null) && (topic != "")) url = url + "#" + topic;

	popupWin(url, 'ucWinHelp', width, height, 'no', 'yes', 'yes', 'no', 'no', 'no', 'no');

	//var attrs = 'width=' + width +',height=' + height + ',status=no,directories=no,toolbar=no,location=no,menubar=no,scrollbars=yes,resizable=yes,top=200,left=200';
	//pcWinHelp = window.open(url ,'pklWinHelp', attrs);
	//pcWinHelp.focus();
}	

// 19.02.2008 : PHM : Popup browser config window
function popupBrowserConfigWin(width, height)
{
	if (width==null) width = 850;
	if (height==null) height = 450;

	var url = "browser_configuration.php?uclayout=popup";
	
	popupWin(url, 'ucWinBrowserConfig', width, height, 'no', 'yes', 'yes', 'no', 'no', 'no', 'no');

	//var attrs = 'width=' + width +',height=' + height + ',status=no,directories=no,toolbar=no,location=no,menubar=no,scrollbars=yes,resizable=yes,top=200,left=200';
	//pcWinHelp = window.open(url ,'pklWinHelp', attrs);
	//pcWinHelp.focus();
}	

// 12.12.2007 : PHM : Popup search filter window
function popupSearchFilterWin(params, width, height)
{
	if (width==null) width = 700;
	if (height==null) height = 400;

	var url = "searchfilter.php?uclayout=popup";

	if ((params != null) && (params != "")) url = url + "&" + params;

	popupWin(url, 'ucWinSearchFilter', width, height, 'no', 'yes', 'yes', 'no', 'no', 'no', 'no');
}	

// 12.01.2008 : PHM : Popup lookup window
function popupLookupWin(url, params, width, height)
{
	if (width==null) width = 850;
	if (height==null) height = 650;

	url = url + "?uclayout=popup";

	if ((params != null) && (params != "")) url = url + "&" + params;

	popupWin(url, 'ucWinLookup', width, height, 'no', 'yes', 'yes', 'no', 'no', 'no', 'no');
}	

function ucfTextAreaLimit(oObj, maxL)
{
	if (oObj.value.length == maxL) return false;
	return true;
}

function ucfTextAreaCount(oObj, maxL, oCounter, extra)
{ 
	objVal = oObj.value;
	if (objVal.length > maxL) objVal = objVal.substring(0, maxL);
	if (oCounter) oCounter.value = String(maxL - objVal.length) + extra;
	return true;
}

function ucfAddOption(theSel, theText, theValue)
{
	var newOpt = new Option(theText, theValue);
	var selLength = theSel.length;
	theSel.options[selLength] = newOpt;
}

function ucfDeleteOption(theSel, theIndex)
{ 
	var selLength = theSel.length;

	if(selLength > 0)
	{
		theSel.options[theIndex] = null;
	}
}

function ucfMoveOptions(theSelFrom, theSelTo)
{
  
	var selLength = theSelFrom.length;
	var selectedText = new Array();
	var selectedValues = new Array();
	var selectedCount = 0;
	var i;
	
	// Find the selected Options in reverse order
	// and delete them from the 'from' Select.
	for(i=selLength-1; i>=0; i--)
	{
		if(theSelFrom.options[i].selected)
		{
			selectedText[selectedCount] = theSelFrom.options[i].text;
			selectedValues[selectedCount] = theSelFrom.options[i].value;
			ucfDeleteOption(theSelFrom, i);
			selectedCount++;
		}
	}
	
	// Add the selected text/values in reverse order.
	// This will add the Options to the 'to' Select
	// in the same order as they were in the 'from' Select.
	for(i=selectedCount-1; i>=0; i--)
	{
		ucfAddOption(theSelTo, selectedText[i], selectedValues[i]);
	}
	
	if(ibrNS4) history.go(0);
}

function ucfChangeAllOptionsState(selCtrl, lSelect)
{
	var selLength = selCtrl.length;
	var i;
	
	for(i=selLength-1; i>=0; i--)
	{
		selCtrl.options[i].selected = lSelect;
	}
}

function ucfHasOptions(obj)
{
	if(obj!=null && obj.options!=null)
	{
		return true;
	}
	return false;
}

function ucfSwapOptions(obj,i,j)
{
	var o = obj.options;
	var i_selected = o[i].selected;
	var j_selected = o[j].selected;
	var temp = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
	var temp2= new Option(o[j].text, o[j].value, o[j].defaultSelected, o[j].selected);

	o[i] = temp2;
	o[j] = temp;
	o[i].selected = j_selected;
	o[j].selected = i_selected;
}

function ucfMoveOptionUp(obj)
{
	if(!ucfHasOptions(obj))
	{
		return;
	}

	for(i=0;i<obj.options.length;i++)
	{
		if(obj.options[i].selected)
		{
			if(i != 0 && !obj.options[i-1].selected)
			{
				ucfSwapOptions(obj,i,i-1);
				obj.options[i-1].selected = true;
			}
		}
	}
}


function ucfMoveOptionDown(obj)
{
	if(!ucfHasOptions(obj))
	{
		return;
	}

	for(i=obj.options.length-1;i>=0;i--)
	{
		if(obj.options[i].selected)
		{
			if(i !=(obj.options.length-1) && ! obj.options[i+1].selected)
			{
				ucfSwapOptions(obj,i,i+1);
				obj.options[i+1].selected = true;
			}
		}
	}
}

function GetXmlHttpObject()
{
	var xmlHttp=null;

	try
 	{
 		// Firefox, Opera 8.0+, Safari
 		xmlHttp=new XMLHttpRequest();
 	}
	catch (e)
 	{
 		//Internet Explorer
 		try
  		{
  			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  		}
 		catch (e)
  		{
  			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  		}
 	}

	return xmlHttp;
}

function ucxmlHttp(url, divid, subsequentcall, ucClass)
{ 
	xmlHttp = GetXmlHttpObject();

	if (xmlHttp==null)
	{
 		alert (msg_http_not_supported);
 		return
 	}

	url=url+"&sid="+Math.random();

	xmlHttp.open("GET",url,true);

	xmlHttp.onreadystatechange = function()
	{
		if (xmlHttp.readyState == 4 || xmlHttp.readyState=="complete")
		{
			//alert("url : " + url + "/ response : " + xmlHttp.responseText);
			if (xmlHttp.status == 200)
			{
				if (divid)
				{
					document.getElementById(divid).innerHTML = xmlHttp.responseText;// + "test"; 

					if(ucClass)
					{
						document.getElementById(divid).className = ucClass;
					}

					if( subsequentcall )
					{
						subsequentcall();
					}
				}
			}
			else
			{
				alert(msg_http_ajax_error + " : " + xmlHttp.responseText);
			} 
		} 
	};

	xmlHttp.send(null);
}