// Javascript Library - Copyright Make IT Work (http://www.makeitwork.be)

// Usage: <script  language="javascript" src="scriptlib.js">

// The left() function doesn't exist, so we'll just create one...

function Left(str, n)
{
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}

// The right() function doesn't exist, so we'll just create one...
function Right(str, n)
{
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

// The replace() function doesn't exist, so we'll just create one...
function replace( str, from, to )
{
    var idx = str.indexOf( from );
    while ( idx > -1 ) {
        str = str.replace( from, to );
        idx = str.indexOf( from );
    }
    return str;
}

// Pop-up window with picture
function ShowPicture(fotonaam, fhoogte, fbreedte, src, naam)
{
        naam=fotonaam;
		len=naam.length;
		naam=replace(replace(Left(naam,naam.length - 4),"-"," "),"_"," ");
		margin = 180; // margin you need for the menu + your OS menu bar
		reshoogte = screen.height; // user resolution height
		resbreedte = screen.width; // user resolution width
		if (fhoogte > (reshoogte-margin))
		{ hoogte = reshoogte-margin; breedte = (fbreedte/fhoogte)*hoogte; }
	    else { hoogte = fhoogte; breedte = fbreedte; } // if the resolution is way bigger than the pic size
		// alle parameters doorgeven aan de windowNavigator, navigatie info
		strUrl = "windowNavigator.php?fotonaam=" + fotonaam + "&src=" + src + "&breedte=" + breedte 
		+ "&hoogte=" + hoogte + "&naam=" + naam;
		foto = window.open(strUrl,'','menubar=no,toolbar=no');
		foto.resizeTo(breedte+15,hoogte+margin-50); // count 50 pixels for the OS menu bar
        //foto.document.write("<html><head><title>Foto - " + naam + "</title></head>");
		//foto.document.write("<body leftmargin='0' topmargin='0'>");
        //foto.document.write("<center><img src=" + replace(src," ","%20") + replace(fotonaam," ","%20")
		//+ " height='" + hoogte + "' width='" + breedte + "'><br>"); 
        //foto.document.write("<font face='Arial, Helvetica, sans-serif' size='2'><b>"+naam+"</b></font><br>");
		//foto.document.write("<input type='submit' value=' Close ' onclick='JavaScript:window.close();'>");
		//foto.document.write("</center>");
        //foto.document.write("</body></html>");
}
// original
// function ShowPicture(fotonaam, hoogte, breedte, src)
//{
//        naam=fotonaam;
//		len=naam.length;
//		naam=replace(replace(Left(naam,naam.length - 4),"-"," "),"_"," ");
//		foto = window.open('','','menubar=no,toolbar=no');
//        foto.resizeTo(breedte+15,hoogte+30);
//        foto.document.write("<html><head><title>Foto - " + naam + "</title></head>");
//		foto.document.write("<body leftmargin='0' topmargin='0'>");
//        foto.document.write("<center><img src=" + replace(src," ","%20") + replace(fotonaam," ","%20") + "><br>");
//        foto.document.write("<font face='Arial, Helvetica, sans-serif' size='2'><b>"+naam+"</b></font><br>");
//		foto.document.write("<input type='submit' value=' Close ' onclick='JavaScript:window.close();'>");
//		foto.document.write("</center>");
//        foto.document.write("</body></html>");
//}

// Pop-up window
function open_me(url,w,h)
{
	w=w+15; h=h+15;
	window.open(url,'','location=0,resizable=0,status=0,titlebar=0,directories=0,toolbar=0,menubar=0,scrollbars=0,status=0,width='+w+',height='+h+',screenX='+(screen.width - w)/2+',screenY='+(screen.height - h)/2+',top='+(screen.height - h)/2+',left='+(screen.width - w)/2);
}

// Pop-up window with scroll bar
function open_me_scroll(url,w,h)
{
	w=w+15; h=h+15;
	window.open(url,'','location=0,resizable=0,status=0,titlebar=0,directories=0,toolbar=0,menubar=0,scrollbars=1,status=0,width='+w+',height='+h+',screenX='+(screen.width - w)/2+',screenY='+(screen.height - h)/2+',top='+(screen.height - h)/2+',left='+(screen.width - w)/2);
}

// Limit size of textarea
// HTML code needed:
// <form name="form1">
// <textarea name="textfield" onkeydown="textCounter(textfield,form1.remLen,100);" onkeyup="textCounter(form1.textfield,form1.remLen,100);">Hello</textarea>
// <input name="remLen" type="text" value="100" size="3" maxlength="3" readonly="readonly"> remaining characters...
// </form>
function textCounter(field, countfield, maxlimit)
{
	if (field.value.length > maxlimit) // if too long...trim it!
	field.value = field.value.substring(0, maxlimit);
	// otherwise, update 'characters left' counter
else 
	countfield.value = maxlimit - field.value.length;
}

// Age calculator
// HTML code needed:
// Stef is <script>calculate(17,9,1970)</ script> oud...

// needed for calculate()
function y2k(number)
{
	return (number < 1000) ? number + 1900 : number;
}

function calculate(day,month,year)
{		
	var today = new Date();
	var thisYear = y2k(today.getYear());
	var thisMonth = today.getMonth()+1;
	var thisDay = today.getDate();
	var yearsold = thisYear - year, monthsold = 0, daysold = 0, string = '';

    if (thisMonth >= month) monthsold = thisMonth - month;
    else { yearsold--; monthsold = thisMonth + 12 - month; }

    if (thisDay >= day)daysold = thisDay - day;
    else {
        if (monthsold > 0) monthsold--;
        else { yearsold--; monthsold+=11; }
        daysold = thisDay + 31 - day;
    }

    if (yearsold > 0) // toon dan enkel maanden & dagen
	{
		string = yearsold + ' jaar';
	}
	else // anders enkel jaren
	{
		if (monthsold > 0) {string += monthsold + ' maand ';}
		if (daysold > 0) {
			string += ' en ' + daysold + ' dag';
			if (daysold > 1) string += 'en';        
		}
	}
    document.write(string);
}

// Pop-up Divs
function toggleDiv(id,flagit)
{
	if (flagit=="1")
	{
		if (document.layers) document.layers[''+id+''].visibility = "show"
		else if (document.all) document.all[''+id+''].style.visibility = "visible"
			 else if (document.getElementById) document.getElementById(''+id+'').style.visibility = "visible"
    }
	else
		if (flagit=="0")
		{
		if (document.layers) document.layers[''+id+''].visibility = "hide"
			else if (document.all) document.all[''+id+''].style.visibility = "hidden"
		   	     else if (document.getElementById) document.getElementById(''+id+'').style.visibility = "hidden"
	}
}

// Expand/collapse a DIV
// HTML code needed:
// <div id="hiddentext" style="display:none;">
// This is hidden text</div>
// <a href="javascript:void(null);" onclick="expand('hiddentext', this);">
// <img src="pics/button-bottom.gif" border="0" align="absmiddle"></a>

function expand(thistag, tag)
{
	styleObj=document.getElementById(thistag).style;
	if (styleObj.display=='none')
	{
		styleObj.display='';
		tag.innerHTML = "<img src='pics/button-top.gif' align=absmiddle border=0>";
	}
	else {
		styleObj.display='none';
		tag.innerHTML = "<img src='pics/button-bottom.gif' align=absmiddle border=0>";
	}
}


function RTEsubmitForm()
{
	updateRTE('rte1');
	return true;
}

function DeleteWithConfirmation(deletepage_url,field_name,field_value,messagetext,querystring)
{
  if (confirm(messagetext)==1)
  {	
    location.href = eval('\"'+deletepage_url+'?'+field_name+'='+field_value+'&'+querystring+'\"');
  }
}

// Title: Timestamp picker
// Description: See the demo at url
// URL: http://us.geocities.com/tspicker/
// Script featured on: http://javascriptkit.com/script/script2/timestamp.shtml
// Version: 1.0
// Date: 12-05-2001 (mm-dd-yyyy)
// Author: Denis Gritcyuk <denis@softcomplex.com>; <tspicker@yahoo.com>
// Notes: Permission given to use this script in any kind of applications if
//    header lines are left unchanged. Feel free to contact the author
//    for feature requests and/or donations

function show_calendar(str_target, str_datetime) {
	var arr_months = ["January", "February", "March", "April", "May", "June",
		"July", "August", "September", "October", "November", "December"];
	var week_days = ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"];
	var n_weekstart = 1; // day week starts from (normally 0 or 1)

	var dt_datetime = (str_datetime == null || str_datetime =="" ?  new Date() : str2dt(str_datetime));

	var dt_prev_month = new Date(dt_datetime);
	dt_prev_month.setMonth(dt_datetime.getMonth()-1);
	var dt_next_month = new Date(dt_datetime);
	dt_next_month.setMonth(dt_datetime.getMonth()+1);
	var dt_firstday = new Date(dt_datetime);
	dt_firstday.setDate(1);
	dt_firstday.setDate(1-(7+dt_firstday.getDay()-n_weekstart)%7);
	var dt_lastday = new Date(dt_next_month);
	dt_lastday.setDate(0);
	
	// html generation (feel free to tune it for your particular application)
	// print calendar header
	var str_buffer = new String (
		"<html>\n"+
		"<head>\n"+
		"	<title>Calendar</title>\n"+
		"</head>\n"+
		"<body bgcolor=\"White\">\n"+
		"<table class=\"clsOTable\" cellspacing=\"0\" border=\"0\" width=\"100%\">\n"+
		"<tr><td bgcolor=\"#4682B4\">\n"+
		"<div class='calendar'>"+
		"<table cellspacing=\"1\" cellpadding=\"3\" border=\"0\" width=\"100%\">\n"+
		"<tr>\n	<td bgcolor=\"#4682B4\"><a href=\"javascript:window.opener.show_calendar('"+
		str_target+"', '"+ dt2dtstr(dt_prev_month)+"');\">"+
		"<img src=\"/lib-pics/navi-back.gif\" width=\"16\" height=\"16\" border=\"0\""+
		" alt=\"previous month\"></a></td>\n"+
		"	<td bgcolor=\"#4682B4\" colspan=\"5\">"+
		"<font color=\"white\" face=\"tahoma, verdana\" size=\"2\">"
		+arr_months[dt_datetime.getMonth()]+" "+dt_datetime.getFullYear()+"</font></td>\n"+
		"	<td bgcolor=\"#4682B4\" align=\"right\"><a href=\"javascript:window.opener.show_calendar('"
		+str_target+"', '"+dt2dtstr(dt_next_month)+"');\">"+
		"<img src=\"/lib-pics/navi-next.gif\" width=\"16\" height=\"16\" border=\"0\""+
		" alt=\"next month\"></a></td>\n</tr>\n"
	);

	var dt_current_day = new Date(dt_firstday);
	// print weekdays titles
	str_buffer += "<tr>\n";
	for (var n=0; n<7; n++)
		str_buffer += "	<td bgcolor=\"#87CEFA\">"+
		"<font color=\"white\" face=\"tahoma, verdana\" size=\"2\">"+
		week_days[(n_weekstart+n)%7]+"</font></td>\n";
	// print calendar table
	str_buffer += "</tr>\n";
	while (dt_current_day.getMonth() == dt_datetime.getMonth() ||
		dt_current_day.getMonth() == dt_firstday.getMonth()) {
		// print row heder
		str_buffer += "<tr>\n";
		for (var n_current_wday=0; n_current_wday<7; n_current_wday++) {
				if (dt_current_day.getDate() == dt_datetime.getDate() &&
					dt_current_day.getMonth() == dt_datetime.getMonth())
					// print current day
					str_buffer += "	<td bgcolor=\"#FFB6C1\" align=\"right\">";
				else if (dt_current_day.getDay() == 0 || dt_current_day.getDay() == 6)
					// weekend days
					str_buffer += "	<td bgcolor=\"#DBEAF5\" align=\"right\">";
				else
					// print working days of current month
					str_buffer += "	<td bgcolor=\"white\" align=\"right\">";

				if (dt_current_day.getMonth() == dt_datetime.getMonth())
					// print days of current month
					str_buffer += "<a href=\"javascript:window.opener."+str_target+
					".value='"+dt2dtstr(dt_current_day)+"'; window.close();\">"+
					"<font color=\"black\" face=\"tahoma, verdana\" size=\"2\">";
				else 
					// print days of other months
					str_buffer += "<a href=\"javascript:window.opener."+str_target+
					".value='"+dt2dtstr(dt_current_day)+"'; window.close();\">"+
					"<font color=\"gray\" face=\"tahoma, verdana\" size=\"2\">";
				str_buffer += dt_current_day.getDate()+"</font></a></td>\n";
				dt_current_day.setDate(dt_current_day.getDate()+1);
		}
		// print row footer
		str_buffer += "</tr>\n";
	}
	// print calendar footer
	 //str_buffer +=
		//"<form name=\"cal\">\n<tr><td colspan=\"7\" bgcolor=\"#87CEFA\">"+
		//"<font color=\"White\" face=\"tahoma, verdana\" size=\"2\">"+
		//"Time: <input type=\"text\" name=\"time\" value=\""+dt2tmstr(dt_datetime)+
		//"\" size=\"8\" maxlength=\"8\"></font>
		//"</td></tr>\n</form>\n";
		str_buffer += "</table>\n" +
		"</div>" +
		"</tr>\n</td>\n</table>\n" +		
		"</body>\n" +
		"</html>\n";

	var vWinCal = window.open("", "Calendar", 
		"width=200,height=190,status=no,resizable=yes,top=200,left=200");
	vWinCal.opener = self;
	var calc_doc = vWinCal.document;
	calc_doc.write (str_buffer);
	calc_doc.close();
}
// datetime parsing and formatting routimes. modify them if you wish other datetime format
function str2dt (str_datetime) {
	var re_date = /^(\d+)-(\d+)-(\d+)$/;
	if (!re_date.exec(str_datetime))
		return alert("Invalid Datetime format: "+ str_datetime);
	return (new Date (RegExp.$1, RegExp.$2-1, RegExp.$3, RegExp.$4, RegExp.$5, RegExp.$6));
}
function dt2dtstr (dt_datetime) {
	return (new String (
			dt_datetime.getFullYear()+"-"+(dt_datetime.getMonth()+1)+"-"+dt_datetime.getDate()));
}
function dt2tmstr (dt_datetime) {
	return (new String (
			dt_datetime.getHours()+":"+dt_datetime.getMinutes()+":"+dt_datetime.getSeconds()));
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- "'+nm+'" moet een geldig e-mail addres zijn.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- "'+nm+'" moet een nummer zijn.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- "'+nm+'" moet een nummer tussen '+min+' en '+max+' zijn.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- "'+nm+'" is een verplicht veld.\n'; }
  } if (errors) alert('Corrigeer de volgende fouten:\n\n'+errors+'\n\n');
  document.MM_returnValue = (errors == '');
}
