// v1.01 ka	sep 10 2007	Initial version


//	******* ALL ENTRIES ARE SORTED ALPHABETICAL ***********/

// Add files to a Select box based upon AJAX call to find values from the directory in question
function add_files(id, value, table, tabkey, tabval)
{
//	alert(id+":"+value+":"+table+":"+tabkey+":"+tabval);
	if( id != '') 
		getDataReturnText("get_files.php"+	// call get_options.php 
		 "?id=" +escape(id)+				// add id
		 "&value="+escape(value)+			// input id value
		 "&table="+escape(table)+			// passed table name
		 "&tabkey="+escape(tabkey)+			// passed table key
		 "&tabval="+escape(tabval)+			// passed table value
		 "&rnd="+Math.random(),				// random to prevent caching
		 fill_files );						// which function to call with the output
}


// Add Options to a Select box based upon AJAX call to find values from the database table
function add_options(id, key, value)
{
//	alert(id + " | " + key + " | " + value);
	if( key != '') 
		getDataReturnText("get_options.php" +	// call get_options.php 
		 "?id=" + escape(id) +					// add id
		 "&key=" + escape(key) +				// the data value
		 "&value=" + escape(value) +			// input id value
		 "&rnd=" + Math.random(),				// random to prevent caching
		 fill_options );						// which function to call with the output
}


function build_adm_url(butt_name)
{
	if(butt_name != 'add')
	{
		select	= document.getElementById('choose');		// get the choose selectbox
		index	= select.selectedIndex;						// get number of selected entry
		key		= select.options[index].value;				// get the value for that entry
		tekst	= select.options[index].text;				// get the text for that entry
	}
	if(butt_name == 'add')
	{
		if(url_basename.toLowerCase() == 'agenda') 
			window.location.href  = 'change_agenda.php?action=I&record= ';
		else window.location.href = "adm_maintain_" + url_basename.toLowerCase() + ".php";
	}
	if(butt_name == 'change')
	{
		if(url_basename.toLowerCase() == 'agenda') 
			window.location.href = 'change_agenda.php?action=C&record='+escape(tekst);
		else 
			window.location.href = "adm_maintain_" + url_basename.toLowerCase() + ".php?" + url_basename + "Key=" + key;
	}
	if(butt_name == 'delete')
	{
		if(url_basename.toLowerCase() == 'agenda') 
			window.location.href = 'delete_agenda.php?record='+escape(tekst);
		else 
		{
			sure = confirm("Do you really want to delete this " + url_basename + " record: " + tekst + "?");
			if(sure == true) window.location.href = "adm_delete_" + url_basename.toLowerCase() + ".php?" + url_basename + "Key=" + key;
		}
	}
}


// wijzig blokkleur
function change_bgcolor()
{
	var a = document.getElementById('blokkleur1');
	var c = a.title;
	a.style.backgroundColor = c;
	var a = document.getElementById('blokkleur2');
	var c = a.title;
	a.style.backgroundColor = c;
}


// check form elements for valid entries
function check_form(form_to_check)
{
	var how_many	= form_to_check.length;					// how many form elements are there
	var msg			= '';									// set empty message
	for(element=0;  element < how_many; element++)			// traverse over them
	{
		var reg_exp		= null;								// set to null object
		var item		= form_to_check[element];			// get input element
		var required	= item.getAttribute("required");	// See if it is a required element (null if not)
		if( required   != null )							// to catch a value we don't have yet
		{
			reg_exp		= new RegExp("^.+$");				// anything
			extra		= " is verplicht";
		}
		if(required	   == "any") 
		{
			reg_exp		= new RegExp("^.+$");				// anything
			extra		= " is verplicht";
		}
		if(required	   == "num") 
		{
			reg_exp		= new RegExp("^[0-9]+$");			// numeric
			extra		= " is verplicht en moet numeriek zijn";
		}
		if(required	   == "alfa") 
		{
			reg_exp		= new RegExp("^[a-zA-Z]+$");		// alpha
			extra		= " is verplicht en mag alleen letters bevatten";
		}
		if(required	   == "email")			
		{
			reg_exp		= new RegExp("^.+@.+\.[a-zA-Z]+$");	// email
			extra		= " is verplicht en moet een geldig email adres zijn";
		}
		if(required	   == "dropdown")							
		{	// if first option is selected, we have an error.
			if(item.selectedIndex == 0) reg_exp = new RegExp("^@$");	
			extra		= " geen keuze gemaakt ";
		}
		if(reg_exp	   != null)
		{
			var val		= item.value.trim();					// get the value of the element and trim it
			item.value	= val;									// put trimmed value back in form element
			var tag		= item.parentNode.parentNode.firstChild;
			if(tag.tagName != "TD") tag = tag.nextSibling;	// Netscape has extra child in here
			if(!reg_exp.test(val) ) msg += stripHTML(tag.innerHTML)+extra+"<br>";	
		}	// als niet ingevuld, haal veldnaam op en zeg dat verplicht is.
	}
	if(msg	!= "")
	{
		document.getElementById("msg").style.display = "";  // maak rij zichtbaar
		document.getElementById("msg").innerHTML	 = msg;	// als er errors zijn, gooi die op het scherm
		return false;
	}
	else
	{
//		alert ("end routine");
		return true;							// anders kunnen we dit naar PHP gooien.
	}
}


// fill the options for the select box with id passed from PHP, use values in he array, and select the one that matches value
// data returns as follows: id^value|file|file|file|file etc
function fill_files(tekst)
{
//	alert(tekst);
	if( tekst != "")
	{
		arr		= tekst.split("|");			// split on element pairs
		input	= arr[0].split("^");		// split data values
		select  = document.getElementById(input[0]);	// get select box that needs to be filled
		aantal  = arr.length;				// how many elements are there?
		select.options.length = 1;			// clean all input from the select box
		for( i=1; i<aantal; i++ )			// loop over element pairs
		{
			opts	= arr[i];				// get data
			select.options[i] = new Option(opts,opts); // add option to select box as: text,value
			if( opts == input[1] )			// if this value is equal to the data from input
				select.selectedIndex = i;	// then set that as the selected value.
		}
		if (select.selectedIndex >= 1) a=1;
		else select.selectedIndex = 0;		
	}
}


// fill the options for the select box with id passed from PHP, use values in he array, and select the one that matches value
// data returns as follows: id^value˙optvalue^opttext˙optvalue^opttext˙optvalue^opttext˙optvalue^opttext etc
function fill_options(tekst)
{
//	alert(tekst);
	if( tekst != "")
	{
		arr		= tekst.split("|");			// split on element pairs
		input	= arr[0].split("^");		// split data values
		select  = document.getElementById(input[0]);	// get select box that needs to be filled
// alert(tekst);
		aantal  = arr.length;				// how many elements are there?
		select.options.length = 0;			// clean all input from the select box
		for( i=1; i<aantal; i++ )			// loop over element pairs
		{
			opts	= arr[i].split("^");	// get data
			select.options[i] = new Option(opts[1],opts[0]);	// add option to select box as: text, value
			if( opts[0] == input[1] )		// if this value is equal to the data from input
				select.selectedIndex = i;	// then set that as the selected value.
		}
	}
}

//  getDataReturnText(url, callback) 
//    ** Uses the GET method to get text from the server. **
//    Gets text from url, calls function named callback with that text.
//    Use when you just want to get data from an URL, or can easily
//    encode the data you want to pass to the server in an URL, such as 
//    "http://localhost/script.php?a=1&b=2&c=hello+there".
//    Example: getDataReturnText("http://localhost/data.txt", doWork); 
//    Here, the URL is a string, and doWork is a function in your own 
//    script.
function getDataReturnText(url, callback)
{ 
  var XMLHttpRequestObject = false; 

  if (window.XMLHttpRequest) {
    XMLHttpRequestObject = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    XMLHttpRequestObject = new
     ActiveXObject("Microsoft.XMLHTTP");
  }

  if(XMLHttpRequestObject) {
    XMLHttpRequestObject.open("GET", url);

    XMLHttpRequestObject.onreadystatechange = function()
    { 
      if (XMLHttpRequestObject.readyState == 4 &&
        XMLHttpRequestObject.status == 200) { 
          callback(XMLHttpRequestObject.responseText);
          delete XMLHttpRequestObject;
          XMLHttpRequestObject = null;
      } 
    } 

    XMLHttpRequestObject.send(null);
  }
}


// glow een stuk tekst
function glow()
{
	var col = new Array  ('#000000', '#001111', '#112222', '#223333', '#334444', '#445555', '#556666', '#667777', '#778888', '#889999', '#99aaaa', '#aabbbb', '#bbcccc', '#ccdddd', '#ddeeee', '#eeffff', '#ddeeee', '#ccdddd', '#bbcccc', '#aabbbb', '#99aaaa', '#889999', '#778888', '#667777', '#556666', '#445555', '#334444', '#223333', '#112222', '#001111' );
	var a = document.getElementById('glow');
	var b = a.lang;
	var c = a.title;
	var d = col.length;
	if ( c=='up' )
	{
		b++;
		if( b>d )
		{
			b = d;
			a.title = 'down';
		}
	}
	if ( c=='down' )
	{
		b--;
		if ( b<0 )
		{
			b = 0;
			a.title = 'up';
		}
	}
	a.style.backgroundColor = col[b];
	a.lang = b;
	setTimeout('glow()',100);
}

function mc(a)
{
	a.className = 'butt_text butt_mc';
}

function mc2(a)
{
	a.className = 'butt_text2 butt_mc2';
}

// give button the button stylesheet, and the mouse_out style sheet
function mg(a)
{
	a.className = 'butt_text butt_mg';
}


function mg2(a)
{
	a.className = 'butt_text2 butt_mg2';
}


// give button the button stylesheet, and the mouse_over style sheet
function mo(a)
{
	a.className = 'butt_text butt_mo';
}


function mo2(a)
{
	a.className = 'butt_text2 butt_mo2';
}


function ref_to(a)
{
	var b = a.getAttribute('href'); // Mozilla fix
	window.location = b;
}

// loop through the options under a selectbox and select the one that matches value
function select_option( id, value)
{
	var select  = document.getElementById(id);		// get the select box
	if(value != '')
	{
		var len		= select.options.length;			// how many options are there?
		select.options.selectedIndex = 0;				// preset option 0
		for(i=0; i<len; i++)
		{
			if ( select.options[i].value == value )		// loop until one matches
			{
				select.options.selectedIndex = i;		// set that as selected
				break;
			}
		}
	}
	else select.options.selectedIndex = 0;				// set option 0, when no value
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

function stripHTML(oldString)								// Nodig voor controleer functie!!!
{
	var newString	= "";
	var inTag		= false;
	for(var i = 0; i < oldString.length; i++) 
	{
        if(oldString.charAt(i) == "<") inTag		= true;
        if(oldString.charAt(i) == ">") inTag		= false;
        else if(!inTag)					newString  += oldString.charAt(i);
	}
	return newString;
} 

function suggest_password(id)
{
	var tekst	= 'abcdefghijklmnopqrstuvwxyz';
	var special = '!@#$%^&*';

	var pword	= tekst.charAt(Math.floor(Math.random()*26));	// letter
		pword  += tekst.charAt(Math.floor(Math.random()*26));	// letter
		pword  += Math.floor(Math.random()*10);					// number
		pword  += special.charAt(Math.floor(Math.random()*8));	// special
		pword  += tekst.charAt(Math.floor(Math.random()*26)).toUpperCase();	// Uppercase letter
		pword  += Math.floor(Math.random()*10);					// number
		pword  += Math.floor(Math.random()*10);					// number

	document.getElementById(id).value = pword;
}

