// 1-3-2010	ka	nw maand


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


// 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.
	}
}


// display one anchor
function display_index_anchor (anchor, desc, disab, active)
{
	if (active == "a")
	{
		var href = "#" + anchor;
//		confirm (href);
		document.write("<td class='butt_text butt_mg' onMouseover='mo(this);' onMouseout='mg(this);' onmousedown='mc(this);' onClick='ref_to(this);' href='" + href + "' " + disab + ">" + desc + "</td>");
	}
	else
		document.write("<td class='dummy_anchor'>" + desc + "</td>");
}


function display_index_anchors (year, deze_mnd, desc) 
{
	var maand = new Array(13);
	maand[1]  = "Jan";
	maand[2]  = "Feb";
	maand[3]  = "Mar";
	maand[4]  = "Apr";
	maand[5]  = "May";
	maand[6]  = "Jun";
	maand[7]  = "Jul";
	maand[8]  = "Aug";
	maand[9]  = "Sep";
	maand[10] = "Oct";
	maand[11] = "Nov";
	maand[12] = "Dec";

	// actieve maanden in diverse jaren
	// bij een nieuwe maand:
	//	1) aanpassen ym[] hieronder
	//	2) text uit DB/home naar pages/happenings20yy-mm (index.php zoekt zelf alle maanden)
	//		- kies invoegen
	//		- PageKey leeglaten
	//		- Webpage = happenings20yy-mm
	//		- Text = relevante text uit DB/home
	//		- bovenaan komt: <script language="JavaScript">display_index_anchors("2010", "1", "January")</script> 
	//			OPM: januari=1 en niet 01!!  en geen <br> achter <script ...
	//		- Het blok tekst laten eindigen op <br><br><br><br>
	//	5) verwijder text uit DB/home. Nieuwe home text eindigt op <br><br>
	//  6) bij aanmaken januari van een nieuw jaar aanpassen:
	//		- $year in index.php
	//		- tpl/wh_tail.html (aanmaken nieuwe jaar regel en current wijzigen in [DISAB_HAPPENINGS20jj]
	var ym = new Array(100);
	ym[2010] = " aa3456789012 ";	// spatie voor substring (begint bij 0), a=active
	ym[2009] = " aaaaaaaaaaaa ";
	ym[2008] = " 12345a7aaaaa ";
	ym[2007] = " aaaa5a7a9a1a ";
	ym[2006] = " aaaaaaaaaaaa ";
	ym[2005] = " aaaaaaaaaaaa ";
	ym[2004] = " 1aaa5aaaaaaa ";
	ym[2003] = " 1234567890aa ";

	if (deze_mnd == 0)
		document.write("<strong class='site'>What's happening?</strong>");
	else
		document.write("<strong class='site'>Ace & Recent Developments: " + desc + " " + year + "</strong>");
	anchor = maand[deze_mnd] + year;
	document.write("<a name='" + anchor.toLowerCase() + "'></a>");
	document.write("<br><br><table width='100%' border='0'><tr>");

	for ( var mnd=1 ; mnd<13; mnd++ )
	{
		disab = "";
		if (mnd == deze_mnd) disab = "DISABLED";	// 0 kan ook (voor now), dan mag alles
		active = ym[year].substring(mnd, mnd+1);
		anchor = maand[mnd] + year;
		if (deze_mnd > 0)
			display_index_anchor (anchor.toLowerCase(), maand[mnd], disab, active);
	}

	if (deze_mnd > 0)
		display_index_anchor ("otheryears", "other years", "", "a");

	document.write("</tr></table><br>");
}


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


function display_results_subhead (nr, year) 
{
	document.write("<strong class='site'>Year # " + nr + ": " + year + "</strong>");
	document.write("<br><br>");
}


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

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

// 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_mg';
}


// 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_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
}

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

