// # v1.01 ka	oct 10 2008	init 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')
	{
		window.location.href = "adm_maintain_" + url_basename.toLowerCase() + ".php";
	}
	if(butt_name == 'change')
	{
		window.location.href = "adm_maintain_" + url_basename.toLowerCase() + ".php?" + "k=" + key;
	}
	if(butt_name == 'delete')
	{
		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?" + "k=" + key;
	}
}


// 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 mandatory";
		}

		if(required	   == "any") 
		{
			reg_exp		= new RegExp("^.+$");				// anything
			extra		= " is mandatory";
		}
		if(required	   == "num") 
		{
			reg_exp		= new RegExp("^[0-9]+$");			// numeric
			extra		= " is mandatory and has to be numeric";
		}
		if(required	   == "ymd") 
		{
			reg_exp		= new RegExp("^[0-9]{2,4}[-/][0-9]{1,2}[-/][0-9]{1,2}$");	// 2007-04-31 or 07/4/5
			extra		= " is mandatory and has to be a date in format (yy)yy/(m)m/(d)d";
		}
		if(required	   == "alfa") 
		{
			reg_exp		= new RegExp("^[a-zA-Z]+$");		// alpha
			extra		= " is mandatory and can contain only letters";
		}
		if(required	   == "email")			
		{
			reg_exp		= new RegExp("^.+@.+\.[a-zA-Z]+$");	// email
			extra		= " is mandatory and has to be a valid email address";
		}
		if(required	   == "dropdown")							
		{	// if first option is selected, we have an error.
			if(item.selectedIndex == 0) reg_exp = new RegExp("^@$");	
			extra		= " no choice made";
		}
		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 += "<i>" + stripHTML(tag.innerHTML) + "</i>" + 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);
  }
}


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

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


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


// jump to page on same website
function ref_to(a)
{
	var b = a.getAttribute('href'); // Mozilla fix
	window.location = b;
}

// jump to page on other website
function ref_to_target(a)
{
	var b = a.getAttribute('href');
//	confirm(b);
	window.open(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
}

// display a picture with dropshadow
function shadowpic (picnaam, width, height, border, shadow , center, target_name, target_page)
{
	if (shadow == "" ) shadow = "10";
	if (border == "" ) border = "1";
	document.write("<td STYLE='filter:progid:DXImageTransform.Microsoft.Shadow(color=#333333, direction=135, strength=" + shadow + ");'>");
	if (center != "" )	document.write("<center>");
	if (target_name != "") 
	{
		target = "";
		if (target_page != "") target = " target='" + target_page + "'";	// _blank
		document.write("<a href='" + target_name + "'" + target + ">");
	}
	document.write("<img src='" + picnaam + "'");
	if (width != "")	document.write(" width='" + width + "'");
	if (height != "")	document.write(" height='" + height + "'");
	document.write(" border='" + border + "'>");
	if (center != "" ) document.write("</center>");
	document.write("</td>");
}

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;
}
