﻿// Developed by FdO - VCS Sleeuwijk
// last updated 20061224
// advanced javascripting and xhtml
var xmlhttp = false;
// fotoalbum
var fotocounter = 0;
var fotoimg;
var fotoimgmini;
var fotoimgalt;
var fotoi = 0;


try 
{
    //If the Javascript version is greater than 5.
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
    //If not, then use the older active x object.
    try
    {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (E)
    {
        xmlhttp = false;
    }
}

if (!xmlhttp)
{
    xmlhttp = new XMLHttpRequest();
}

function makerequest(serverPage, objID)
{
  var obj = document.getElementById(objID);
	// add a timestamp to the url to make sure the page is reloaded from the server
	var re = new RegExp('\\?', 'i');

	if (serverPage.match(re))
	    xmlhttp.open("GET", serverPage + "&timestamp=" + new Date().getTime() );
	else
		xmlhttp.open("GET", serverPage + "?timestamp=" + new Date().getTime() );
    xmlhttp.onreadystatechange = function()
    {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
        {
	        obj.innerHTML = xmlhttp.responseText;
			urchinTracker(serverPage);
        }
    }
    xmlhttp.send(null);
}

function makePOSTRequest(url, parameters, objID) 
{
    var obj = document.getElementById(objID);

    xmlhttp.open('POST', url, true);
    xmlhttp.onreadystatechange = function()
    {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
        {
	        obj.innerHTML = xmlhttp.responseText;
  		    urchinTracker(url);
        }
    }
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.setRequestHeader("Content-length", parameters.length);
    xmlhttp.setRequestHeader("Connection", "close");
    xmlhttp.send(parameters);
 }


function openfotoalbum(album, objID)
{
  var obj = document.getElementById(objID);
	// add a timestamp to the url to make sure the page is reloaded from the server
	var re = new RegExp('\.jpg$', 'i');

		xmlhttp.open("GET", album + "?timestamp=" + new Date().getTime() );
    xmlhttp.onreadystatechange = function()
    {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
        {
					var xmldoc = xmlhttp.responseXML;

					var albumtext = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n';
					albumtext += '<html xmlns="http://www.w3.org/1999/xhtml">\n';
					albumtext += '<head>\n';
					albumtext += '<meta http-equiv="cache-control" content="no-cache" />\n';
					albumtext += '</head>\n';
					albumtext += '<body>\n';
					albumtext += '<a id="fototop"></a>\n';
					// get title from xml
					albumtext += '<h1>' + xmldoc.getElementsByTagName('title')[0].childNodes[0].nodeValue + '</h1>\n';
					// construct arrays with foto information
				  var fotoos = xmldoc.getElementsByTagName('foto');
				  fotoi = 0;
				  fotocounter = 0;
				  fotoimg = new Array(fotoos.length);
				  fotoimgmini = new Array(fotoos.length);
					fotoimgalt = new Array(fotoos.length);
				  while (fotoi < fotoos.length)
				  {
								if (fotoos[fotoi].getElementsByTagName('url'))
								{
					 			 	 fotoimg[fotoi] = fotoos[fotoi].getElementsByTagName('url')[0].childNodes[0].nodeValue;
					 			 	 if (fotoos[fotoi].getElementsByTagName('miniurl'))
					 			 	 {
									 		try
											 { fotoimgmini[fotoi] = fotoos[fotoi].getElementsByTagName('miniurl')[0].childNodes[0].nodeValue;
											 }catch(e){}

											 if (fotoimgmini[fotoi] == null)
 										 		 fotoimgmini[fotoi] = fotoimg[fotoi].replace(re, 'm.jpg');
					 	 			 }
					 	 			 else
					 	 			 {
									 	 fotoimgmini[fotoi] = fotoimg[fotoi].replace(re, 'm.jpg');
					 	 			 }
									 if (fotoos[fotoi].getElementsByTagName('tekst'))
									 {
                   		try
											 {fotoimgalt[fotoi] = fotoos[fotoi].getElementsByTagName('tekst')[0].childNodes[0].nodeValue;
										}catch(E){}
									 						 if (fotoimgalt[fotoi] == null)
									 						 {
															 	 fotoimgalt[fotoi] = '';
															 	}
		               }
		               else
		               {
									 		 fotoimgalt[fotoi] = '';
		 	      			 }

									 fotoi++;
					      }
					}
					albumtext += '<img id="mainfoto" src="' + fotoimg[0] + '" alt="' + fotoimgalt[0] + '" title="' + fotoimgalt[0] + '" border="0"  width="720" style="border: solid; display: block; margin-left: auto; margin-right: auto;" />\n';
					albumtext += '<table border="0" align="center">\n';
					albumtext += '<tr>\n';
					albumtext += '<td width="60">\n';
					albumtext += '<img id="prevklik" src="../../img/leftdisabled.gif" border="0" style="cursor: pointer; cursor: hand; border: none; margin-left: auto; margin-right: auto;"\n';
					albumtext += 'alt="Previous" onclick="fotocounter--;showfoto();" />\n';
					albumtext += '</td>\n';
					albumtext += '<td width="60">\n';
					albumtext += '<img id="nextklik" src="../../img/rightenabled.gif" border="0" style="cursor: pointer; cursor: hand; border: none; margin-left: auto; margin-right: auto;"\n';
					albumtext += 'alt="Next" onclick="fotocounter++;showfoto();" />\n';
					albumtext += '</td>\n';
					albumtext += '</tr>\n';
					albumtext += '</table>\n';

					albumtext += '<div id="minifotos" style="text-align:center">\n'
					// create miniatures
					fotoi = 0;
					while (fotoi < fotoimg.length)
					{
						albumtext += '<img src="' + fotoimgmini[fotoi] + '" alt="' + fotoimgalt[fotoi] + '" title="' + fotoimgalt[fotoi] + '" onclick="fotocounter=' + fotoi + '; showfoto(); location.href=\'#fototop\';" style="cursor: pointer; cursor: hand;" />&nbsp\n';
						if (((fotoi+1) % 8) == 0)
      						albumtext += '<br />\n';
						fotoi++;
					}
					albumtext += '</div>\n';
					albumtext += '</body>\n';
					albumtext += '</html>\n';
					//showfoto();
					obj.innerHTML = albumtext;
					urchinTracker(album);


        }
    }
    xmlhttp.send(null);
}

function showfoto()
{
	if (fotocounter > fotoimg.length-1)
		fotocounter = fotoimg.length-1;

	if (fotocounter < 0)
		fotocounter = 0;

	if (fotocounter == fotoimg.length-1)
	{
	 	// hide next button;
	 	document.getElementById('nextklik').src = '../../img/rightdisabled.gif';
	}
	else
	{
	 	document.getElementById('nextklik').src = '../../img/rightenabled.gif';
	}

	if (fotocounter == 0)
	{
		 // hide previous button;
		 document.getElementById('prevklik').src = '../../img/leftdisabled.gif';
	}
	else
	{
		 document.getElementById('prevklik').src = '../../img/leftenabled.gif';
	}

	var fobj = document.getElementById('mainfoto');
	fobj.src = fotoimg[fotocounter];
	fobj.alt = fotoimgalt[fotocounter];
	fobj.title = fotoimgalt[fotocounter];
}


function validatecontactform()
{
	var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
	var regex = new RegExp(emailReg);

	if (document.getElementById("contactnaam").value.length == 0)
	{
		alert('Uw naam moet ingevuld zijn');
		return false;
	}
	
	if (document.getElementById("contactemail").value.length == 0)
	{
		alert('Uw e-mail adres moet ingevuld zijn');
		return false;
	}
	
	if (!regex.test(document.getElementById("contactemail").value))
	{
		alert('Vul een geldig e-mail adres in');
		return false;
	}
  	
	return true;
}

function submitcontactform(objID)
{
	var poststr = "naam=" + encodeURI( document.getElementById("contactnaam").value ) +
		"&adres=" + encodeURI( document.getElementById("contactadres").value ) +
		"&postcode=" + encodeURI( document.getElementById("contactpostcode").value ) +
		"&woonplaats=" + encodeURI( document.getElementById("contactwoonplaats").value ) +
		"&telefonnummer=" + encodeURI( document.getElementById("contacttelefoonnummer").value ) +
		"&email=" + encodeURI( document.getElementById("contactemail").value ) +
		"&bericht=" + encodeURI( document.getElementById("contactbericht").value );
		

	makePOSTRequest('/content/sendmail.php', poststr, objID);
}

function GetParameterValue(name)
{
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var tmpURL = window.location.href;
    var results = regex.exec( tmpURL );
    if( results == null )
        return "";
    else
        return results[1];
}

function LoadInitialPage(initialpage, objID)
{
	var tmppage = GetParameterValue("pageid")
	if (tmppage == "")
		makerequest(initialpage, objID);
	else
		makerequest(tmppage, objID);
}

function PrintPage()
{
   var sOption="toolbar=no,location=no,directories=no,menubar=no,"; 
         sOption+="scrollbars=yes,width=600,height=300"; 
   var winprint=window.open("/scripts/print.html","Print", sOption); 
   winprint.focus();
}
