//	28-02-2003	checkletter,rightclick disabled
//	03-03-2003	showObj, hideObj
//	28-04-2003	checkdigit
//	01-05-2003	sendform return
//	03-07-2003	checkemail
//	10-09-2003	checkphone
//	10-09-2003	print
//	18-09-2003	length check
//	18-09-2003	combobox parent warning
//	19-09-2003	wordWrapLines
//	22-09-2003	trimString
//	29-09-2003	getValue, openInfoPage
//	26-08-2004	tlSelect, bij 1 optie => gelijk invullen
//	08-03-2005	wordWrapLines, 1 regel ook wrappen
//	01-10-2005	date check
//	09-11-2005	get url
//	21-11-2005	get url value
//	30-11-2005	getCookie / setCookie
//	05-12-2005	addFunction
//	13-12-2005	checkemail improved
//	14-09-2006	getZip, getGemeente, getStraat
//      06-10-2006      multichar typing in select box
//  22-10-2006  printversie functie
 
var SEPERATOR = '~';

function hideObj(obj) {
	obj.style.display = "none";
}

function showObj(obj) {
	obj.style.display = "";
}

function click(e) {
	if (document.all) {
		if (event.button==2||event.button==3) {
			alert("Right Click Disabled");
			return false;
		}
	}
	if (document.layers) {
		if (e.which == 3) {
			alert("Right Click Disabled");
			return false;
		}
	}
}

function tlSelect(choice, nextList, idField){
	// clear the next select list
	if (nextList != null)
		clearList(nextList);
	//var array_label = choice.options[choice.selectedIndex].value;
	var array_label =  idField + SEPERATOR + choice.options[choice.selectedIndex].value;
	// if there is an array with name fo the passed in value
	if (listArray[array_label]){
		//if (listArray[array_label]){
		// set the newlist variable to hold the designated array
		var newList = listArray[array_label];
		if (newList.length == 2) {	//1 option only
			var choiceLab = newList[1];
			var choiceVal = newList[0];
			nextList.options[0] = new Option(choiceLab, choiceVal);
		} else {
			// set the first option box in the next select list
			nextList.options[0] = new Option("- Maak hier uw keuze -", "");
			// get each item from the array of values and create an option element with the same name and value
			var i = 1;
			var j = 1;
			while (i < newList.length) {
				var choiceLab = newList[i];
				var choiceVal = newList[i-1];
				nextList.options[j] = new Option(choiceLab, choiceVal);
				j++;
				i+=2;
			}
		}
		// set the selected index of the next select list
		nextList.selectedIndex = 0;
		return true;
//		showObj(nextList);
	} else { // it is the last list on the page so do what needs to be done.
//		hideObj(nextList);
		return false;
	}
}

//functions to do mutli-char inpunt in select box

var holdchars = ""

function tlSelectFocus(choice)
{
  holdchars = "";
}

function tlSelectKeyUp(choice,e)
{
  if(window.event) // IE
  {
    keynum = e.keyCode;
  }
  else if(e.which) // Netscape/Firefox/Opera
  {
    keynum = e.which;
  }
  keychar = String.fromCharCode(keynum);
  
  if (keychar < 32)
  {
  	return;
  }
  
  holdchars=holdchars + keychar.toUpperCase();
  // Nu zoeken in de lijst

  if (holdchars.length > 1)
  {  
    i = 1;
    while (i < choice.length)
    {
	  choicestr = choice.options[i].text;
	  choicestr = choicestr.substr(0,holdchars.length).toUpperCase();
	  
      if (choicestr == holdchars)
	  {
//        alert("c="+choicestr+" h="+holdchars);
	    choice.selectedIndex = i;
		i = choice.length;
		
	    if(window.event) // IE
        {
          e.keyCode = 0;
        }
        else if(e.which) // Netscape/Firefox/Opera
        {
          e.which = 0;
        }		
  	  }
	  i++;
    }
  }	
  if (holdchars.length==1)
  {
    setTimeout("holdchars='';",3000)
  }  
}


//function to fill textarea
function tlTextArea(choice, TA, idField){
	var array_lab = idField + SEPERATOR + choice.options[choice.selectedIndex].value;
	if (listArray[array_lab]){
		if (listArray[array_lab].length > 0) {
			TA.value = wordWrapLines(listArray[array_lab][1],TA);	//[1] waarde uit 2e kolom (= label)
//			TA.value = listArray[array_lab][1];	//[1] waarde uit 2e kolom (= label)
			//code for ta.rows (count \n)
			TA.rows = countLines(TA.value)+1;
//			showObj(TA);
			return true;
		}
	} else { 
		clearTextArea(TA);
//		hideObj(TA);
		return false;
	}
}

// function to clear the passed in selection box
function clearList(box){
	for (var i = box.options.length - 1; i >= 0; i--){
		box.options[i] = null;
	}
	box.options[0] = new Option("- Maak eerst een keuze in de vorige keuzelijst -","");
	box.selectedIndex = 0;
}

function clearTextArea(TA){
	TA.value = "";
	TA.rows = 1;
}

function countLines(str) {
	i = 0;
	f = 1;
	while (i < str.length) {
		if (str.charAt(i) == "\n") {
			f++;
		}
		i++;
	}
	return f;
}

function wordWrapLines(str,tA) {
	c = tA.cols;
	r = tA.rows;
	i = 0;	//index
	p = 0;	//previous \n
	result = "";
	while (i < str.length) {
		if (str.charAt(i) == "\n") {
			s = trimString(str.substring(p,i));
			if (s.length > c) {
				s = wordWrapLine(s,c);
			}	//if
			result = result + s + "\n";
			p = i + 1;	//+1 voor \n
		}	//if
		i++;
	}	//while
	if (result == "") {
		result = wordWrapLine(trimString(str),c);
	}
	else {
		s = trimString(str.substring(p,i));
		if (s.length > c) {
			s = wordWrapLine(s,c);
		}	//if
		result = result + s;
	}
	return result;
}

function wordWrapLine(str,maxstrlength) {
	if (str.length > maxstrlength) {
		j = maxstrlength;
		while ( (j < str.length) && (j > 0) ) {
			if (str.charAt(j) == " ") {
				a = str.substring(0,j);
				b = str.substring(j+1,str.length);
				str = trimString(a) + "\n" + trimString(b);
				j = j + maxstrlength;
			}
			else {
				j--;
			}	
		}
	}
	return str;
}

function trimString(str) {
	k = 0;
	while ((k < str.length) && (str.charAt(k)) == " ") {
		k++;
	}
	s = str.substring(k,str.length);
	k = s.length-1;
	while ((k > 0) && (s.charAt(k) == " ")) {
		k--;
	}
	return s.substring(0,k+1);
}

function checkDigit(str){
	i = 0;
	while ((i < str.length) && ((str.charAt(i) >= "0") && (str.charAt(i) <= "9"))){
		i++;
	}
	return ((i == str.length) && (str.length != 0));
}

function checkLetter(str){
	i = 0;
	s = str.toLowerCase();
	while ((i < s.length) && ((s.charAt(i) >= "a") && (s.charAt(i) <= "z"))){
		i++;
	}
	return ((i == s.length) && (s.length != 0));
}

function checkZip(TF){
	return ((TF.value.length == 6) && (checkDigit(TF.value.substring(0,4)) && checkLetter(TF.value.substring(4,6))));
}

function emailCheck(str){
	return ( (str.indexOf("@") > 0) && (str.indexOf(".") > 0) );
}

function checkEmail(emailStr) {
/*	The following pattern is used to check if the entered e-mail address
	fits the user@domain format.  It also is used to separate the username
	from the domain. */
	var emailPat=/^(.+)@(.+)$/
/*	The following string represents the pattern for matching all special
	characters.  We don't want to allow special characters in the address. 
	These characters include ( ) < > @ , ; : \ " . [ ]    */
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
/*	The following string represents the range of characters allowed in a 
	username or domainname.  It really states which chars aren't allowed. */
	var validChars="\[^\\s" + specialChars + "\]"
/*	The following pattern applies if the "user" is a quoted string (in
	which case, there are no rules about which characters are allowed
	and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
	is a legal e-mail address. */
	var quotedUser="(\"[^\"]*\")"
/*	The following pattern applies for domains that are IP addresses,
	rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
	e-mail address. NOTE: The square brackets are required. */
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
/*	The following string represents an atom (basically a series of non-special characters.) */
	var atom=validChars + '+'
/*	The following string represents one word in the typical username.
	For example, in john.doe@somewhere.com, john and doe are words.
	Basically, a word is either an atom or quoted string. */
	var word="(" + atom + "|" + quotedUser + ")"
	// The following pattern describes the structure of the user
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	/* The following pattern describes the structure of a normal symbolic
	domain, as opposed to ipDomainPat, shown above. */
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
/*	Finally, let's start trying to figure out if the supplied address is valid. */
/*	Begin with the coarse pattern to simply break up user@domain into
	different pieces that are easy to analyze. */
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) {
	/*	Too many/few @'s or something; basically, this address doesn't
		even fit the general mould of a valid e-mail address. */
		//alert("Email adres lijkt niet correct (controleer @ en .)")
		return false
	}
	var user=matchArray[1]
	var domain=matchArray[2]
	//	See if "user" is valid 
	if (user.match(userPat)==null) {
	//	user is not valid
		//alert("De naam lijkt niet correct.")
		return false
	}
/*	if the e-mail address is at an IP address (as opposed to a symbolic
	host name) make sure the IP address is valid. */
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
	//	this is an IP address
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				//alert("Doel IP adres is juist!")
				return false
			}
		}
		return true
	}
//	Domain is symbolic name
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		//alert("De domein naam lijkt niet correct.")
		return false
	}
/*	domain name seems valid, but now make sure that it ends in a
	three-letter word (like com, edu, gov) or a two-letter word,
	representing country (uk, nl), and that there's a hostname preceding 
	the domain or country. */
/*	Now we need to break up the domain to get a count of how many atoms it consists of. */
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || 
		domArr[domArr.length-1].length>3) {
		// the address must end in a two letter or three letter word.
		//alert("Het adres moet eindigen met 3 letters (voor een domein) of met 2 letters (voor een land).")
		return false
	}
	// Make sure there's a host name preceding the domain.
	if (len<2) {
		var errStr="De hostnaam ontbreekt bij het email adres!"
		//alert(errStr)
		return false
	}
//	If we've gotten this far, everything's valid!
	return true;
}


function checkPhone(s){
	i = 0;
	while ((i < s.length) && (((s.charAt(i) >= "0") && (s.charAt(i) <= "9")) || (s.charAt(i) == "-") || (s.charAt(i) == " ") || (s.charAt(i) == "/"))){
		i++;
	}
	return ((i == s.length) && (s.length > 2));
}

var dtCh= "-";
var minYear=2005;
var maxYear=2100;

function stripCharsInBag(s, bag){
	var i;
	var returnString = "";
	// Search through string's characters one by one.
	// If character is not in bag, append to returnString.
	for (i = 0; i < s.length; i++){   
		var c = s.charAt(i);
		if (bag.indexOf(c) == -1) returnString += c;
	}
	return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
	// EXCEPT for centurial years which are not also divisible by 400.
	return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
	}
	return this
}

function checkDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
//		alert("De datum is niet goed ingevoerd: dd-mm-jjjj")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
//		alert("Maand klopt niet")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
//		alert("Dag klopt niet")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
//		alert("Jaartal moet tussen "+minYear+" en "+maxYear+" liggen")
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || checkDigit(stripCharsInBag(dtStr, dtCh))==false){
//		alert("De datum is niet juist ingevoerd: dd-mm-jjjj")
		return false
	}
	return true
}

function printPage(){
	if (!window.print){
	   alert("NetScape 4.x of hoger nodig om te kunnen printen!");
	   return false;
	}
	else {
		 window.print();
		 return true;
	}
}

function getValue(key,nr,idField) {
	var array_label =  idField + SEPERATOR + key;
	if (listArray[array_label]) {
		return listArray[array_label][nr];
	}
	else {
		return "";
	}
}

function openInfoPage(URL,pageLabel,pWidth,pHeight) {
	if (URL != "") {
		s = "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no";
		if ((pWidth) && (pHeight)) {
			s = s + " width=" + pWidth + " height=" + pHeight;
		}
		window.open(URL,pageLabel,s);
		return true;
	}
	else {
		return false;
	}
}

function getUrl() {
	return document.URL;
}

function getUrlValue() {
	url = getUrl();
	if (url.length > 0) {
		if (url.indexOf("?") > 0) {
			return url.substring(url.indexOf("?")+1,url.length);	
		}
		else 
		{  
			return "";
		}
	}
        else 
	{  
	 	return "";
	}
}

function getCookie(c_name)
{
	if (document.cookie.length>0)
	{
		c_start=document.cookie.indexOf(c_name + "=")
		if (c_start!=-1)
		{
			c_start=c_start + c_name.length+1 
			c_end=document.cookie.indexOf(";",c_start)
			if (c_end==-1) c_end=document.cookie.length
			return unescape(document.cookie.substring(c_start,c_end))
		}
	}
	return null
}

function setCookie(c_name,value,expiredays)
{
	var exdate=new Date()
	exdate.setDate(expiredays)
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : "; expires="+exdate)
}

function getZip(gemcode,straatcode,huisnummer)
{
	for (x in postcodeArray)
	{
		if ((gemcode == postcodeArray[x][0]) && (straatcode == postcodeArray[x][1]))
		{
			if (huisnummer & 1)
			{	
				if (((postcodeArray[x][3] == "Beide") || (postcodeArray[x][3] == "Oneven")) && (huisnummer >= postcodeArray[x][4]) && (huisnummer <= postcodeArray[x][5]))
				{
					return postcodeArray[x][2]
				}
			}
			else
			{	
				if (((postcodeArray[x][3] == "Beide") || (postcodeArray[x][3] == "Even")) && (huisnummer >= postcodeArray[x][4]) && (huisnummer <= postcodeArray[x][5]))
				{
					return postcodeArray[x][2]
				}
			}
		}
	}
	return ""
}

function getGemeente(postcode)
{
	for (x in postcodeArray)
	{
		if (postcode == postcodeArray[x][2])
		{
			return postcodeArray[x][0]
		}
	}
	return ""
}

function getStraat(postcode)
{
	for (x in postcodeArray)
	{
		if (postcode == postcodeArray[x][2])
		{
			return postcodeArray[x][1]
		}
	}
	return ""
}

// Schrijf Title en Body van dit item zonder stylesheet in een pop-up venster
// zodat een printvriendelijke pagina ontstaat.
function printversie()
{
    var Title;
    var Body;

    Title=document.getElementById('myTitle').innerHTML;
    Body=document.getElementById('myBody').innerHTML;
    var PrintWin = window.open("", "Print" );

    PrintWin.document.write('<html>')
    PrintWin.document.write('<head>')

    PrintWin.document.write('<style>@media print{.printclass{display: none;}}</style>');

    PrintWin.document.write('</head>')
    PrintWin.document.write('<body>')

    PrintWin.document.write('<form><input type="button" value="Print" class="printclass" onClick="print();">');
    PrintWin.document.write('<input type="button" value="Sluit venster" class="printclass" onClick="window.close();"></form>');
    PrintWin.document.write('<h1 class="noclass">' + Title + '</h1>');
    PrintWin.document.write(Body);

    PrintWin.document.write('</body>')
    PrintWin.document.write('</html>')

}

