﻿// AJAX functions:
function loadXMLDoc(content, target_id)
{
	function createXMLHttp()
	{
		if (typeof XMLHttpRequest != 'undefined')
		{
			return new XMLHttpRequest();
		}
		else if (window.ActiveXObject)
		{
			var avers = ["Microsoft.XmlHttp", "MSXML2.XmlHttp", "MSXML2.XmlHttp.3.0", "MSXML2.XmlHttp.4.0", "MSXML2.XmlHttp.5.0"];
			for (var i = avers.length -1; i >= 0; i--)
			{
				try
				{
					httpObj = new ActiveXObject(avers[i]);
					return httpObj;
				}
				catch(e) {}
			}
		}
		throw new Error('XMLHttp (AJAX) not supported');
	}
	function processReqChange() 
	{
		if (req.readyState == 4) // only if req shows "complete"
		{
			if (req.status == 200) // only if "OK"
			{
				function replacenewlines(stringdata) // Replace new lines with linebreak instruction, <br />
				{
					var returnindex = stringdata.search(/\r/);
					var newlineindex = stringdata.search(/\n/);
					if (returnindex >= 0)
					{
						stringdata = stringdata.replace(/\r/g, "<br />");
					}
					else
					{
						stringdata = stringdata.replace(/\n/g, "<br />");
					}
					if (returnindex == 0 || newlineindex == 0) // Remove leading linebreaks
					{
						stringdata = stringdata.substr(6);
					}
					return stringdata;
				}
				function inserthref(stringdata, name, link, targetclass) // Insert href anchors
				{
					newdata =  '<a href="'+link+'" class="'+targetclass+'" id="'+name+'">'+name+'</a>';
					stringdata = stringdata.replace(name, newdata);
					return stringdata; 
				}
				
				var returndata = req.responseXML.documentElement;
				var text1 = returndata.getElementsByTagName('text')[0].firstChild.nodeValue;
				if (returndata.getElementsByTagName('head')[0].firstChild) //Only process head data if it exists
				{
					var head1 = returndata.getElementsByTagName('head')[0].firstChild.nodeValue;
					head1 = replacenewlines(head1);
				}
				text1 = replacenewlines(text1);
				var href = returndata.getElementsByTagName('href'); //Get specified href anchors
				for (var i = 0; i < href.length; i++)
				{
					var data = href[i].childNodes;
					for (var j = 0; j < data.length; j++)
					{
						if (data[j].nodeType == 1)
						{
							if (data[j].tagName == 'name')
							{
								var hrefname = data[j].childNodes[0].nodeValue;
							}
							if (data[j].tagName == 'link')
							{
								var hreflink = data[j].childNodes[0].nodeValue;
							}
							if (data[j].tagName == 'targetclass')
							{
								var hreftargetclass = data[j].childNodes[0].nodeValue;
							}
						}
					}
					text1 = inserthref(text1, hrefname, hreflink, hreftargetclass);
					//if (currentfile+'.html' == hreflink) // Identify id of current menu item, put it in variable for later use
					if ('?'+currentlocation ==  hreflink) // Identify id of current menu item, put it in variable for later use
					{	
						menuhrefname = hrefname;  //Global variable so no 'var' used !
					}
				}
				document.getElementById(target_id).innerHTML = text1;
				if (document.getElementById(target_id+'head')) //Only place head if the id exists:
				{
					document.getElementById(target_id+'head').innerHTML = head1;
				}
				var img = returndata.getElementsByTagName('img'); //Only place images if specified
				var img1 = '';
				for (var i = 0; i < img.length; i++)
				{
					var data = img[i].childNodes;
					for (var j = 0; j < data.length; j++)
					{
						if (data[j].nodeType == 1)
						{
							if (data[j].tagName == 'file')
							{
								var imgfile = data[j].childNodes[0].nodeValue;
							}
							if (data[j].tagName == 'alt')
							{
								var imgalt = data[j].childNodes[0].nodeValue;
							}
						}
					}
					img1 = img1+'<img alt="'+imgalt+'" src="img/'+imgfile+'" class="imgspacing" /> ';
					document.getElementById(target_id).innerHTML = img1;
					//if (document.getElementById(target_id+'img')) //Only place images if the id exists
					//{
					//document.getElementById(target_id+'img').innerHTML = img1;
					//}
				}
				//if (currentfile == content) // Make current menuitem bold, this is only possible after drawing of the menu itself, so this  is done when the centercontent is loaded, which must be specieified after the menucontent
				if (currentlocation == content) // Make current menuitem bold, this is only possible after drawing of the menu itself, so this  is done when the centercontent is loaded, which must be specieified after the menucontent
				{
					var menu = document.getElementById(menuhrefname);
					menu.style.fontWeight = 'bold';
				}
			}
    	}
	}
	var req = createXMLHttp();
	req.onreadystatechange = processReqChange;
	var URL = "xml/"+content+".xml";
	req.open('GET', URL, true);
	req.send(null);	
}
// END AJAX functions
//
// -------------------------------------------------------------------
//
// Event handlers:
var started = 0;
function init()
{
	currentlocation = location.search; //Global variable so no 'var' used !
	currentlocation = currentlocation.replace('?', ''); //remove leading ?
	if (currentlocation == '') //Load index when site is accessed with domainnname only
	{
		currentlocation = 'index';
	}
	currentfile = location.pathname; //Global variable so no 'var' used !
	currentfile = currentfile.replace('/veenhartkerk/', '');
	currentfile = currentfile.replace('/veenhartkerk2/', '');
	currentfile = currentfile.replace('.shtml', '');
	//if (currentfile == '') //Load index.html when site is access bij domainnname without index.html
	//alert("currentfile= " + currentfile);
	if (currentfile == '/opnames' || currentfile == 'opnames')
		{
		var content = 'opnames';
		}
	else
		{
		var content = 'calender';
		}
	//{
	//	currentfile = 'index';
	//}
 	if (started === 0)
	{
		//var content = 'menu';
		//loadXMLDoc(content, content);
		var targetid = 'calender';
 		loadXMLDoc(content, targetid);
 		//document.write("Test"+currentfile);
		//var content = 'contact';
 		//loadXMLDoc(content, content);
 		//var content = 'centerpage';
 		//loadXMLDoc(currentfile, content);
 		//loadXMLDoc(currentlocation, content);
		//var content = 'footer';
 		//loadXMLDoc(content, content);
 		started = 1;
 	}
}
window.onload = init;
