var detect = navigator.userAgent.toLowerCase();
var OS,browser,version,total,thestring;
var timer = 1700;
var tobject;
var gotolang;

if (checkIt('konqueror'))
{
	browser = "Konqueror";
	OS = "Linux";
}
else if (checkIt('safari')) browser = "Safari"
else if (checkIt('omniweb')) browser = "OmniWeb"
else if (checkIt('opera')) browser = "Opera"
else if (checkIt('webtv')) browser = "WebTV";
else if (checkIt('icab')) browser = "iCab"
else if (checkIt('msie')) browser = "Internet Explorer"
else if (!checkIt('compatible'))
{
	browser = "Firefox"
 	version = detect.charAt(8);
}
else browser = "An unknown browser";

if (!version) version = detect.charAt(place + thestring.length);

if (!OS)
{
  if (checkIt('linux')) OS = "Linux";
  else if (checkIt('x11')) OS = "Unix";
  else if (checkIt('mac')) OS = "Mac"
  else if (checkIt('win')) OS = "Windows"
  else OS = "an unknown operating system";
}

if (browser != 'Internet Explorer')
{
	document.write('<style>ul { list-style: circle; margin-left: -20px; color: #464646 }</style>');
}

function home() {
	top.location.href = '/index.html';
	return true;
}

function ConfirmDelete(sHref, sQuestion)
{
	if (confirm(sQuestion))
	{
		top.location.href = sHref;
	}
}

function Toggle(img, file)
{
	img.src = '/im/' + file;
}

function ToggleDivVisibility(divName)
{
	var item = document.getElementById(divName);
	if ( item )
	{
		if ( item.style.visibility == 'hidden' )
		{
			item.style.visibility = '';
			item.style.display = 'block';
		}
		else
		{
			item.style.visibility = 'hidden';
			item.style.display = 'none';
		}
	}
}

function Intro(byid, img, col)
{
	var str = 'img'+byid;
	var objImg = document.getElementById(str);
	if ( objImg )
	{
		objImg.src = '/im/intro/'+img;
	}
	str = 'txt'+byid;
	var objText = document.getElementById(str);
	if ( objText )
	{
		objText.style.color = col;
	}
}

function ToggleById(img, file)
{
	var obj = document.getElementById(img);
	obj.src = '/im/' + file;
}

function Back()
{
	history.back();
	return true;
}

function SetRightBar()
{
	var x = getAbsoluteTop('bottommenu');
	var divRight = document.getElementById('rightbar');

	divRight.style.height = (x - 100) + 'px';
}

function Logout()
{
	var strHref=document.location.href;
	var strlanguage;
	var arrTemp=new Array();
	arrTemp=strHref.split('?');
	var strItem = 'lang=0';
	if (arrTemp[1]) {
		strItem=arrTemp[1];
	}
	var strsite=arrTemp[0];
	var arrItems=new Array();
	arrItems=strItem.split('&');
	var arrsite=strsite.split('/');
	strsite=arrsite[2];
	for (var n = 0; n < arrItems.length; n++) {
		var arrSplit=new Array();
		strItem=arrItems[n];
		var placex = strItem.indexOf('lang') + 1;
		if (placex) {
			strlanguage=arrItems[n];
		}
	}
	document.location.href="https://"+strsite+"/cgi-bin/login/logout.cgi?"+strlanguage;
	return true;
}

function JsLogout() {
	var strHref=document.location.href;
	var strlanguage;
	var arrTemp=new Array();
	arrTemp=strHref.split('?');
	var strItem = 'lang=0';
	if (arrTemp[1]) {
		strItem=arrTemp[1];
	}
	var strsite=arrTemp[0];
	var arrItems=new Array();
	arrItems=strItem.split('&');
	var arrsite=strsite.split('/');
	strsite=arrsite[2];
	if (strsite=="kelvin.polarautomatisering.nl" || strsite=="www.polarautomatisering.nl") {
		strsite=strsite+":8084";
	}
	for (var n = 0; n < arrItems.length; n++) {
		var arrSplit=new Array();
		strItem=arrItems[n];
		var placex = strItem.indexOf('lang') + 1;
		if (placex) {
			strlanguage=arrItems[n];
		}
	}
	if (browser=="Internet Explorer") {
		document.execCommand("ClearAuthenticationCache");
		document.location.href="http://"+strsite+"/index.cgi?changebar=1&page=first&"+strlanguage;
		return true;
	}
	else {
		document.location.href="http://logout:logout@"+strsite+"/index.cgi?changebar=1&page=first&"+strlanguage;
		return true;
	}
}

function InitTimer()
{
	tobject = setTimeout('BijwerkenTimer()', 1000);
	var elm = document.getElementById('tmr');
	elm.style.width = '150px';
}

function BijwerkenTimer()
{
	timer = timer - 1;
	var elm = document.getElementById('tmr');
	if ( timer > 0 ) {
		elm.style.width = (150 * (timer / 1700)) + 'px';
		tobject = setTimeout('BijwerkenTimer()', 1000);
	}
	else {
		elm.style.width = '0px';
		Logout();
	}
}

function fncChange()
{
	var strHref=document.location.href;
	var newHref;
	var arrTemp=new Array();
	var strItem;
	arrTemp=strHref.split('?');

	var LANG=document.frmChange.ChangeLanguage.options[document.frmChange.ChangeLanguage.selectedIndex].value;
	if (LANG==""){
		return 1;
	}else{
		newHref=arrTemp[0]+'?lang='+LANG;
		if (arrTemp[1]) {
			strItem=arrTemp[1];
	  	var arrItems=new Array();
	  	arrItems=strItem.split('&');
	  	for (var n = 0; n < arrItems.length; n++) {
	  		var arrSplit=new Array();
	  		strItem=arrItems[n];
	  		var placex = strItem.indexOf('lang') + 1;
	  		if (!placex) {
	  			newHref=newHref+'&'+arrItems[n];
	  		}
			}
		}
		document.location.href=newHref;
	}
	return 1;
}

function ResizeWindow(intwidth, intheight)
{
	intwidth=parseInt(intwidth);
	intheight=parseInt(intheight);
	if(intwidth>0&&intheight>0) {
		window.resizeTo(intwidth,intheight);
	}
	return 1;
}

function fncFilter()
{
	var strHref=document.location.href;
	var newHref;
	var arrTemp=new Array();
	arrTemp=strHref.split('?');

	var LANG=document.frmFilterBy1.lang.value;
	var FILTER=document.frmFilterBy1.filterby.options[document.frmFilterBy1.filterby.selectedIndex].value;
	if (LANG=="" || FILTER==""){
		return 1;
	}else{
		newHref=arrTemp[0]+'?lang='+LANG+'&filterby='+FILTER;
		var x1=document.frmFilterBy1.onlylang.value;
		if (x1) {
			newHref=newHref+'&onlylang='+x1;
		}
		var x2=document.frmFilterBy1.ItemsPerPage.value;
		if (x2) {
			newHref=newHref+'&ItemsPerPage='+x2;
		}
		document.location.href=newHref;
	}
	return true;
}

function checkIt(string)
{
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}

function InStr(ZoekIn, string)
{
	return ZoekIn.indexOf(string) + 1;
}

function SetFocusByName(NameOfElement) {
	var ForFocus = document.getElementsByName(NameOfElement);
	for (var i = 0; i < ForFocus.length; i++) {
		ForFocus[i].focus();
		if (ForFocus[i].value) {
			ForFocus[i].select();
		}
	}
}

function SetFocusByID(IDOfElement)
{
	var ForFocus = document.getElementById(IDOfElement);
	ForFocus.focus();
	ForFocus.select();
}

function SetFocusByIDSelect(IDOfElement) {
	var ForFocus = document.getElementById(IDOfElement);
	ForFocus.focus();
}

function getAbsoluteTop(objectId)
{
	o = document.getElementById(objectId)
	oTop = o.offsetTop
	while(o.offsetParent!=null) {
		oParent = o.offsetParent
		oTop += oParent.offsetTop
		o = oParent
	}
	return oTop
}

function getAbsoluteLeft(objectId)
{
	o = document.getElementById(objectId)
	oLeft = o.offsetLeft
	while(o.offsetParent!=null) {
		oParent = o.offsetParent
		oLeft += oParent.offsetLeft
		o = oParent
	}
	return oLeft
}

function fncDisplayHide(byid)
{
	var item = document.getElementById(byid);
	item.style.display = 'none';
}

function fncDisplayShow(byid)
{
	var item = document.getElementById(byid);
	item.style.display = '';
}

function SelectAll(formname, prefix, chkreferer)
{
	var elem = document.getElementById(formname).elements;
	var objreferer = document.getElementById(chkreferer);
	for(var i = 0; i < elem.length; i++)
	{
		if ( InStr(elem[i].name, prefix) )
		{
			elem[i].checked = objreferer.checked;
		}
	}
}

function SizeToFit()
{
	var x = getAbsoluteTop('startx');
	var y = getAbsoluteTop('lastrow');
	var objImg = document.getElementById('filler');
	if (browser == 'Safari')
	{
		objImg.style.height = (y - x + 20)+'px';
	}
	else if (browser == 'Internet Explorer')
	{
		objImg.style.height = (y - x - 40)+'px';
	}
	else
	{
		objImg.style.height = (y - x - 40)+'px';
	}
}

function ShowRows(iSection)
{
	var TRow;
	var x;

	for (x = 1; x <= 4; x++)
	{
		TRow = document.getElementById('Extra_'+iSection+'_'+x);

		if ( TRow.style.display == 'none' || TRow.style.display == '' )
		{
			if ( browser == "Firefox" )
			{
				TRow.style.display = 'table-row';
			}
			else
			{
				TRow.style.display = 'block';
			}
		}
		else
		{
			TRow.style.display = 'none';
		}
	}
}

function ToggleRows(sPrefix, iSection, iItems)
{
	var TRow;
	var x;

	for (x = 1; x <= iItems; x++)
	{
		TRow = document.getElementById(sPrefix+'_'+iSection+'_'+x);

		if ( TRow )
		{
			if ( TRow.style.display == 'none' || TRow.style.display == '' )
			{
				if ( browser == "Firefox" )
				{
					TRow.style.display = 'table-row';
				}
				else
				{
					TRow.style.display = 'block';
				}
			}
			else
			{
				TRow.style.display = 'none';
			}
		}
	}
}

function SetVisibility(sName)
{
	var objYN = document.getElementById(sName);

	for (x = 1; x <= 6; x++)
	{
		TRow = document.getElementById('TR'+x);

		if ( TRow )
		{
			if ( objYN.value == 'N' )
			{
				if ( x < 5 )
				{
					if ( browser == "Firefox" )
					{
						TRow.style.display = 'table-row';
					}
					else
					{
						TRow.style.display = 'block';
					}
				}
				else
				{
					TRow.style.display = 'none';
				}
			}
			else
			{
				if ( x == 5 )
				{
					if ( browser == "Firefox" )
					{
						TRow.style.display = 'table-row';
					}
					else
					{
						TRow.style.display = 'block';
					}
				}
				else
				{
					TRow.style.display = 'none';
				}
			}
		}
	}
}

function PopUpClick()
{
	top.location.href = '/index.cgi?lang=' + gotolang;
}

function ShowMore(referer, popup)
{
  var item = top.document.getElementById(popup);
	var itemref = top.document.getElementById(referer);

	if ( item )
	{
	  item.style.top = (getAbsoluteTop(referer)) + 'px';
	  item.style.left = (getAbsoluteLeft(referer)) + 'px';
	  item.style.width = '275px';
	  item.style.visibility = 'visible';
		item.style.height = itemref.offsetHeight + 'px';
		item.style.width = itemref.offsetWidth + 'px';
	}
}

function HideAllBut(notitem)
{
	var x;
	var item;
	var itemname;
	for ( x = 1; x < 7; x++ )
	{
		itemname = 'ExtraInfo' + x;
		if ( itemname != notitem )
		{
			item = document.getElementById(itemname);
			if ( item )
			{
				MakeHidden(item);
			}
		}
	}
}

function MakeHidden(forObject)
{
	forObject.style.visibility = 'hidden';
}

function InitHttp(sScript, sVars, fnDone)
{
	var myConn = new XHConn();

	if (myConn)
	{
	  var fnWhenDone = function (oXML)
	  {
	  	return oXML.responseText;
	  }
	  myConn.connect(sScript, "POST", sVars, fnDone);
	}
}

function XHConn()
{
  var xmlhttp, bComplete = false;
  try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
  catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  catch (e) { try { xmlhttp = new XMLHttpRequest(); }
  catch (e) { xmlhttp = false; }}}
  if (!xmlhttp) return null;
  this.connect = function(sURL, sMethod, sVars, fnDone)
  {
    if (!xmlhttp) return false;
    bComplete = false;
    sMethod = sMethod.toUpperCase();

    try {
      if (sMethod == "GET")
      {
        xmlhttp.open(sMethod, sURL+"?"+sVars, true);
        sVars = "";
      }
      else
      {
        xmlhttp.open(sMethod, sURL, true);
        xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
        xmlhttp.setRequestHeader("Content-Type",
          "application/x-www-form-urlencoded");
      }
      xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState == 4 && !bComplete)
        {
          bComplete = true;
          fnDone(xmlhttp);
        }};
      xmlhttp.send(sVars);
    }
    catch(z) { return false; }
    return true;
  };
  return this;
}

function PrintContent() {
	var page = new String;
	if (top.content) {
		page = top.content.location.href;
	} else {
		page = top.location.href;
	}

	var x = page.match('\\?');
	if (x != '?') {
		page = page + '?print=1';
	} else {
		page = page + '&print=1';
	}

	OpenWindow(page, 700, 700);
}

function OpenWindow(urlStr, w, h)
{
	if ((top.Popup)&&(!top.Popup.closed)) top.Popup.close();
	top.Popup = window.open(urlStr, 'Popup', 'width='+w+', height='+h+', resizable, scrollbars=yes');
	top.Popup.print();
}

