// DEVENTIT 2006
//globale scripts die door alle applicaties gebruikt worden.

function GetApproute()
{
	var route = paramByName("app_route");
	if (route == null)
		route = 0;
	return route;
}

function AddAppRoute(anchor)
{
	anchor.href = anchor.href + "&app_route="+ GetApproute();
	//alert(anchor.href);
}

function AutoTab(input, toFocusedInputId)
{
 
 if(input != null && input.getAttribute){
 
   var maxLenght = input.getAttribute('maxLength');
   if(input.value.length == maxLenght){
      var elementToFocus = document.getElementById(toFocusedInputId);
      elementToFocus.focus();
   }
 }   
}


//validatie scripts
function ValidateDate(theForm)
{
  if (wnd)
  {  if (true!=wnd.closed)
    wnd.close(); }

  var checkOK = "0123456789-";
  var checkStr = theForm.vandag.value;
  var allValid = true;
  var decPoints = 0;
  var allNum = "";
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
    allNum += ch;
  }
  if (!allValid)
  {
    alert("Voer alleen cijfers in.");
    theForm.vandag.focus();
    return (false);
  }

  var chkVal = allNum;
  var prsVal = parseInt(allNum);
  if (chkVal != "" && !(prsVal >= "0" && prsVal <= "31"))
  {
    alert("Voer een waarde tussen 1 en 31 in.");
    theForm.vandag.focus();
    return (false);
  }

  var checkOK = "0123456789-";
  var checkStr = theForm.vanmaand.value;
  var allValid = true;
  var decPoints = 0;
  var allNum = "";
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
    allNum += ch;
  }
  if (!allValid)
  {
    alert("Voer alleen cijfers in.");
    theForm.vanmaand.focus();
    return (false);
  }

  var chkVal = allNum;
  var prsVal = parseInt(allNum);
  if (chkVal != "" && !(prsVal >= "0" && prsVal <= "12"))
  {
    alert("Voer een waarde tussen 1 en 12 in.");
    theForm.vanmaand.focus();
    return (false);
  }

  var checkOK = "0123456789-";
  var checkStr = theForm.vanjaar.value;
  var allValid = true;
  var decPoints = 0;
  var allNum = "";
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
    allNum += ch;
  }
  if (!allValid)
  {
    alert("Voer alleen cijfers in.");
    theForm.vanjaar.focus();
    return (false);
  }

  var chkVal = allNum;
  var prsVal = parseInt(allNum);
  if (chkVal != "" && !(prsVal >= "0"))
  {
    alert("Voer een waarde groter dan 0 in.");
    theForm.vanjaar.focus();
    return (false);
  }

  var checkOK = "0123456789-";
  var checkStr = theForm.totdag.value;
  var allValid = true;
  var decPoints = 0;
  var allNum = "";
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
    allNum += ch;
  }
  if (!allValid)
  {
    alert("Voer alleen cijfers in.");
    theForm.totdag.focus();
    return (false);
  }

  var chkVal = allNum;
  var prsVal = parseInt(allNum);
  if (chkVal != "" && !(prsVal >= "0" && prsVal <= "31"))
  {
    alert("Voer een waarde tussen 1 en 31 in.");
    theForm.totdag.focus();
    return (false);
  }

  var checkOK = "0123456789-";
  var checkStr = theForm.totmaand.value;
  var allValid = true;
  var decPoints = 0;
  var allNum = "";
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
    allNum += ch;
  }
  if (!allValid)
  {
    alert("Voer alleen cijfers in.");
    theForm.totmaand.focus();
    return (false);
  }

  var chkVal = allNum;
  var prsVal = parseInt(allNum);
  if (chkVal != "" && !(prsVal >= "0" && prsVal <= "12"))
  {
    alert("Voer een waarde tussen 1 en 12 in.");
    theForm.totmaand.focus();
    return (false);
  }

  var checkOK = "0123456789-";
  var checkStr = theForm.totjaar.value;
  var allValid = true;
  var decPoints = 0;
  var allNum = "";
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
    allNum += ch;
  }
  if (!allValid)
  {
    alert("Voer alleen cijfers in.");
    theForm.totjaar.focus();
    return (false);
  }

  var chkVal = allNum;
  var prsVal = parseInt(allNum);
  if (chkVal != "" && !(prsVal >= "0"))
  {
    alert("Voer een waarde groter dan 0 in.");
    theForm.totjaar.focus();
    return (false);
  }
  return (true);
}

function checkField(theField)
{
  if (theField.value == "")
  {
    alert("Veld: '"+theField.name+"' moet ingevuld zijn.");
    theField.value = "*";
    return (false);
  }
  return (true);
}

function validateBestelling(theForm)
{
	if ((theForm.naam.value == "") || (theForm.adres.value == "") || (theForm.postcode.value == "") || (theForm.woonplaats.value == "") || (theForm.email.value == ""))
	{
		alert("Naam, adres, postcode, woonplaats en email moeten ingevuld zijn!");
		return false;
	}
	else
		return checkSelecties(theForm);
}

function checkSelecties(form)
{
  var aantal_selecties = 0;
  for (i=0,n=form.elements.length;i<n;i++)
  {
      if (form.elements[i].name.indexOf('selectie') !=-1)
      {
  		if(form.elements[i].checked)
  			aantal_selecties ++;
  	}
  }
  if (aantal_selecties <= 0)
  {
  	alert ("Er zijn geen documenten geselecteerd!");
  	return false;
  }
  return true;
}


//scripts voor openen popuppagina's
var wnd =null;
function ShowToelichting(PAGE)
{
  wnd = window.open(PAGE,'Toelichting', "left=15,top=15,height=420,width=400,channelmode=no,directories=no,status=no,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes");
  wnd.focus();
}

function OpenSelectionPopup(PAGE)
{
  wnd = window.open(PAGE,'Selecteer', "left=15,top=15,height=380,width=500,channelmode=no,directories=no,status=no,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes");
  wnd.focus();
}

function OpenDetailPopup(PAGE)
{
  var wnd = window.open(PAGE,'Detail', "left=30,top=30,height=620,width=804,channelmode=no,directories=no,status=no,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes");
  wnd.focus();
}

function OpenArchiefPopup(PAGE)
{
  wnd = window.open(PAGE,'Archief', "left=5,top=5,height=620,width=804,channelmode=no,directories=no,status=no,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes");
  wnd.focus();
}

//scripts voor illustraties
function vergroten()
{
	if(document.atlantisimage)
	{
		document.atlantisimage.width = (document.atlantisimage.width * 3)/2; 
	}
	else if(document.getElementById('atlantisimage'))
	{
		document.getElementById('atlantisimage').width = (document.getElementById('atlantisimage').width * 3)/2; 
	}
		//de hoogte wordt automatisch door de explorer berekend
		//document.atlantisimage.height = document.atlantisimage.height;
}

function verkleinen()
{
	if(document.atlantisimage)
	{
		if(document.atlantisimage.width > 45)
		{
			document.atlantisimage.width = (document.atlantisimage.width *2)/3;
		}
	}
	else if(document.getElementById('atlantisimage'))
	{
		if(document.getElementById('atlantisimage').width > 45)
			document.getElementById('atlantisimage').width = (document.getElementById('atlantisimage').width * 2)/3; 
	}
		//de hoogte wordt automatisch door de explorer berekend
		//document.atlantisimage.height=(document.atlantisimage.height*2)/3;
}

// =======================================================================
// ==                         PARAM Ripper                              == 
// ==                    (c) acdhirr@trilobiet.nl                        ==
// ==                        www.trilobiet.nl                            ==
// =======================================================================
// ==  Puts params added to the url in document.userparams              ==   
// ==  These params can be accessed by their name.                      ==
// ==                                                                   == 
// ==  call as:                                                         ==
// ==  <A HREF="doc.htm?param1=value1&param2=value2&param3=value3">     ==
// ==                                                                   == 
// ==  param values can be accessed as:                                 == 
// ==  a = paramByName('whatevername');                                 == 
// ==                                                                   == 
// =======================================================================


// =======================================================================
// create new param object
//
function userParam(nName, vValue)
{
  this.name = nName;
  this.value = vValue;
}
// =======================================================================


// =======================================================================
// return value of param named nName
//
function paramByName(nName)
{

  for (i=0; i<document.userparams.length; i++)
  {
     if (document.userparams[i].name == nName) return document.userparams[i].value; 
  }

  return null;

}
// =======================================================================


// =======================================================================
// main
function ripParams()
{

   // rip entire param string from url:
   cAll = location.search.substring(1,location.search.length);

   // break into pieces
   arParamset = cAll.split('&');

   // create array for user params.
   document.userparams = new Array();

   // assign names and values to document.userparams
   for (i=0; i<arParamset.length; i++)
   {
      // create paramnames as properties of the document
      // and set their values as given

      thisParam = arParamset[i].split('=');
      thisParamname = thisParam[0];
      thisParamvalue = thisParam[1];

      document.userparams[i] = new userParam(thisParamname, thisParamvalue);     
     
   } 

   return document.userparams;
}
// =======================================================================


function NavigatieLinks(_huidige, _totaal, _aantal_pp, _aantal_links, function_name)
{	
// _totaal is het totale aantal records
// _huidige is recordnummer van 1e record op de huidige pagina

// deze foutsituatie kan eigelijk niet voorkomen
	if (_huidige < 0) 
		_huidige = 0;

// als er maar 1 pagina met resultaten is, dan geen links weergeven
	if (_totaal /1 < _aantal_pp /1)
		return;

// zet de aantallen om naar paginanummers
	_huidige = _huidige /_aantal_pp;
	_totaal = _totaal / _aantal_pp;
	

// 1e link bepalen	
	var kleinste_link = _huidige - _aantal_links; //de kleinste navigatielink bepalen
// als je nog meerdere pagina's terug kan
	if (kleinste_link > 0) 
	{
		//toon '<<' 
		document.write("<a class=\"navlink\" href=\"javascript:OpenResult("+((kleinste_link-1) * _aantal_pp)+")\">&laquo;</a>");
	}
	
// Als we niet op de 1e pagina staan, toon de de knop om 1 pagina terug te gaan		
	if (_huidige > 0)
	{
		//toon '<' 
		document.write(" <a class=\"navlink\" href=\"javascript:OpenResult("+(_huidige-1) * _aantal_pp+")\">&lsaquo;</a>");	
	}
	
// Toon de paginanummers die voor de huidige liggen
	if (kleinste_link < 0)
		kleinste_link = 0;
	for (; kleinste_link < _huidige; kleinste_link++)
	{
		document.write(" <a class=\"navlink\" href=\"javascript:OpenResult("+kleinste_link * _aantal_pp+")\">"+(kleinste_link +1)+"</a>");
	}

// Toon de huidige link (_huidige begint bij 0, maar dat is pagina 1)
	var toon_huidige = (_huidige + 1);
	document.write(" <span class=\"navlink_current\">"+ toon_huidige +"</span> ");

// De grootste navigatielink bepalen
	var grootste_link = _huidige + _aantal_links +1; 
	if (grootste_link > _totaal)
		grootste_link = _totaal;

// Toon de links na de huidige		
	var first_link = "";
	for (var t = _huidige+1; t < grootste_link; t++)
	{
		// de > knop verwijst naar huidige+1
		if (t == _huidige+1)
		{
// Maak de link voor '>'
			first_link = " <a class=\"navlink\" href=\"javascript:OpenResult("+t * _aantal_pp+")\">&rsaquo;</a>";
		}
		document.write(" <a class=\"navlink\" href=\"javascript:OpenResult("+t * _aantal_pp+")\">"+(t+1)+"</a>");
	}
// Toon '>'
	if (first_link != "")
		document.write(first_link);

	
// Als er nog pagina's zijn	
	if (grootste_link < _totaal)
	{
		//toon '>>'
		var maximum = (grootste_link * _aantal_pp);
		document.write(" <a class=\"navlink\" href=\"javascript:OpenResult("+maximum+")\">&raquo;</a>");
	}
}

function RemoveLetters(string)
{
    for (var i=0, output='', valid="1234567890"; i<string.length; i++)
	if (valid.indexOf(string.charAt(i)) != -1)
        	output += string.charAt(i)
    return output;
}


function swapTreeNode(node)
{
	var x = node.childNodes
	for (var i=0;i<x.length;i++)
	{
		if (x[i].className == "Treenode")
		{
			//klap een node open of dicht.
			if(x[i].style.display == 'block')
				x[i].style.display = 'none';
			else
				x[i].style.display = 'block';
		}
		if (x[i].className == "Treenodeimg")
		{
			//verander het plaatje van de node.
			if(x[i].src.indexOf(x[i].close)>-1)
				x[i].src = x[i].open;
			else
				x[i].src = x[i].close;
		}
		// bij de volgende tags moeten de childnodes van deze tags ook 'geswapt' worden.
		// Zo kan er opmaak in de tree geregeld worden.
		if (	x[i].tagName == 'TR' || 
				x[i].tagName == 'TD' || 
				x[i].tagName == 'TABLE' || 
				x[i].tagName == 'TBODY')
			swapTreeNode(x[i]);
		
	}
}


var wachtrij = new Array(0);
var working = false;
	
function loadHTML(anchor)
{
	wachtrij.push(anchor);
	handleWachtrij();
}

function handleWachtrij() 
{	
	if (!working)
	{
		working = true;
		while(wachtrij.length > 0)
		{
			var anchor = wachtrij.shift();
			if (anchor != null)
			{
				//de div ophalen waarin deze anchor zit.
				var div = anchor;
				while (div.parentNode)
				{
					div = div.parentNode;
					if (div.tagName == 'DIV')
						break;
				}
				//uit deze div de eerste subdiv halen.
				//in deze div komen namelijk de geladen objecten
				var x = div.childNodes;
				for (var i=0;i<x.length;i++)
				{
					if (x[i].className == "Treenode")
					{
						div = x[i];
						break; 
					}
				}
				//als het item gecached is niet meer inladen.
				if (div.cached == 'false')		
				{
					loadXMLDoc(anchor.href, 'writeHTML', div);
				}
			}
		}
		working = false;
	}
}
	
function writeHTML(nStatus, htmlobject) 
{	
	if (nStatus == 200) 
	{	
		if (htmlobject.style.display == 'block')
			htmlobject.outerHTML = unescape(req.responseText);
		else
			htmlobject.cached = 'false';
		req = false;
	}
}	

var req = false;
var evalResult;
var htmlObject;

function loadXMLDoc(url, pReqChange, htmlobject) 
{
	evalResult = pReqChange;
	htmlObject = htmlobject;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest)
    {
    	try 
    	{
			req = new XMLHttpRequest();
        }
        catch(e) 
        {
			req = false;
			eval(evalResult + "(0);");
        }
		
    } 
    else if(window.ActiveXObject) // branch for IE/Windows ActiveX version
    {
       	try 
       	{
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} 
      	catch(e) 
      	{
        	try 
        	{
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} 
        	catch(e) 
        	{
          		req = false;
				eval(evalResult + "(0);");
        	}
		}
    }
	if(req) 
	{
		req.onreadystatechange = processReqChange;
		req.open("GET", url, true);
		req.send("");
	}
}

function processReqChange() 
{
    // only if req shows "loaded"
    if (req.readyState == 4) 
    {
		eval(evalResult + "(" + req.status + " , htmlObject);"); 
    }
   
}

function HiresObject(_volgorde, _hiresurl, _waregrootte ,_bestelref, _nummer)
{
  this.volgorde = _volgorde;
  this.hiresurl = _hiresurl;
  this.bestelref= _bestelref;
  this.waregrootte = _waregrootte;
  this.nummer = _nummer;
}


function AddHiresWithNumber(volgorde, hiresurl, waregrootte, bestelref, nummer)
{
	hiresObjects[hiresObjects.length] = new HiresObject(volgorde, hiresurl, waregrootte, bestelref, nummer);
}

function AddHires(volgorde, hiresurl, waregrootte, bestelref)
{
	hiresObjects[hiresObjects.length] = new HiresObject(volgorde, hiresurl, waregrootte, bestelref, volgorde);
}

function vorige()
{
	setImage(--huidige_volgorde);
}

function volgende()
{
	setImage(++huidige_volgorde);
}

function setImage(volgorde)
{
    huidige_volgorde = volgorde;
	document.getElementById('hirescontainer').innerHTML = "";
	if (hiresByVolgorde(volgorde))
		document.getElementById('hirescontainer').innerHTML = hiresByVolgorde(volgorde);
	document.getElementById('volgendeimg').style.visibility = (volgorde < hiresObjects.length ? "visible" : "hidden" )
	document.getElementById('vorigeimg').style.visibility = (volgorde > 1 ? "visible" : "hidden" )
	document.getElementById('afbeeldingsnummer').innerText = 'Nummer: ' + nummerByVolgorde(volgorde);
	if (bestelRefByVolgorde(volgorde))
		document.getElementById('bestellink').innerHTML = bestelRefByVolgorde(volgorde);
	if (wareGrootteRefByVolgorde(volgorde))
		document.getElementById('waregroottecontainer').innerHTML = wareGrootteRefByVolgorde(volgorde);
	setInitialSize();
}

function hiresByVolgorde(_volgorde)
{
  for (i=0; i<hiresObjects.length; i++)
  {
	 if (hiresObjects[i].volgorde == _volgorde)
		return hiresObjects[i].hiresurl; 
  }
  return null;
}

function bestelRefByVolgorde(_volgorde)
{
  for (i=0; i<hiresObjects.length; i++)
  {
	 if (hiresObjects[i].volgorde == _volgorde)
		return hiresObjects[i].bestelref; 
  }
  return null;
}

function wareGrootteRefByVolgorde(_volgorde)
{
  for (i=0; i<hiresObjects.length; i++)
  {
	 if (hiresObjects[i].volgorde == _volgorde)
		return hiresObjects[i].waregrootte; 
  }
  return null;
}

function nummerByVolgorde(_volgorde)
{
  for (i=0; i<hiresObjects.length; i++)
  {
	 if (hiresObjects[i].volgorde == _volgorde)
		return hiresObjects[i].nummer; 
  }
  return null;
}

function setInitialSize()
{
	if(document.getElementsByName('atlantisimage')[0])
	{
		document.getElementsByName('atlantisimage')[0].width = 500; 
	}
}
