windows = Array('rijschoolhouders', 'instructeurs', 'administrators', 'leerlingen');
showing = '';

pages = new Array('home', 'voordelen', 'informatie', 'screenshots', 'aanmelden', 'kosten', 'contact', 'av', 'helpdesk', 'faq');

function showElementById(elName) {
	var theElemenet = document.getElementById(elName);
	if (theElemenet) {
		theElemenet.style.display = "block";
	}
}
function hideElementById(elName) {
	var theElemenet = document.getElementById(elName);
	if (theElemenet) {
		theElemenet.style.display = "none";
	}
}

function showPage(pageid){
	for(var i = 0; i < pages.length; i++){
		if (pages[i] != pageid){
			hideElementById('page_'+pages[i]);
			//console.log('hiding page_'+pages[i]);
		}
	}
	showElementById('page_'+pageid);
	//document.getElementById('focusgrabber2').focus();
}

function verzendAanmelding(){
	voornaam = escape(document.getElementById('voornaam').value);
	achternaam = escape(document.getElementById('achternaam').value);
	rijschoolnaam = escape(document.getElementById('rijschoolnaam').value);
	email = escape(document.getElementById('email').value);
	telefoon = escape(document.getElementById('telefoon').value);
	hoegehoord = escape(document.getElementById('hoegehoord').value);
	//kvk = escape(document.getElementById('kvk').value);
	//akkoord = escape(document.getElementById('akkoord').value);

	uri = "ajax/ajaxFunctions.php";
	params = "fn=verwerkAanmelding&hoegehoord="+hoegehoord+"&voornaam="+voornaam+"&achternaam="+achternaam+"&rijschoolnaam="+rijschoolnaam+"&email="+email+"&telefoon="+telefoon;
	callPhpFunctionPOST(uri, params, 'verwerkAanmelding', true);
	
	document.getElementById('verzendknop').innerHTML = '<img src="images/wachten.gif">';
}

function stateChanged_verwerkAanmelding(){
	if (xmlHttp && (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")){ 
		doc = createDomObject(xmlHttp.responseText);
		if (getElementFromDom('success', doc) == '1') {
			//document.getElementById('aanmeldform').reset();
			document.getElementById('aanmeldform').innerHTML = '<span style="color: #F06600; font-weight: bold; font-size: 15px;">Bedankt voor uw aanmelding!</span><br/>'+getElementFromDom('message', doc)
		}
		else{
			document.getElementById('aanmeldresponse').innerHTML = '<span style="color: red;  font-weight: bold; font-size: 15px;">Foutmelding!</span><br/>'+getElementFromDom('message', doc)
			document.getElementById('verzendknop').innerHTML = '<a href="javascript: verzendAanmelding();" class="buttonlink">Verzenden</a>';
		}
	}
}


function openScreenshot(Screenshotid){
	day = new Date();
	id = day.getTime();
	URL = "screenshot.php?s="+Screenshotid;
	window.open(URL, id, 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=600,height=600');
}

function verzendContact(){
	naam = escape(document.getElementById('naamcontact').value);
	email = escape(document.getElementById('emailcontact').value);
	telefoon = escape(document.getElementById('telefooncontact').value);
	bericht = escape(document.getElementById('berichtcontact').value);
	
	uri = "ajax/ajaxFunctions.php";
	params = "fn=verwerkContact&naam="+naam+"&email="+email+"&telefoon="+telefoon+"&bericht="+bericht;
	callPhpFunctionPOST(uri, params, 'verwerkContact', true);
	
	document.getElementById('verzendknopcontact').innerHTML = '<img src="images/wachten.gif">';
}


function stateChanged_verwerkContact(){
	if (xmlHttp && (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")){ 
		doc = createDomObject(xmlHttp.responseText);
		if (getElementFromDom('success', doc) == '1') {
			//document.getElementById('aanmeldform').reset();
			document.getElementById('contactform').innerHTML = '<span style="color: #F06600; font-weight: bold; font-size: 15px;">Uw bericht is verstuurd!</span><br/>'+getElementFromDom('message', doc)
		}
		else{
			//document.getElementById('aanmeldresponse').innerHTML = '<span style="color: red;  font-weight: bold; font-size: 15px;">Foutmelding!</span><br/>'+getElementFromDom('message', doc)
			document.getElementById('verzendknopcontact').innerHTML = '<a href="javascript: verzendAanmelding();" class="buttonlink">Verzenden</a>';
			alert(getElementFromDom('message', doc));
		}
	}
}


//=========================================================================================//
//===== Ajax functions ====================================================================//
//=========================================================================================//

function callPhpFunctionPOST(uri, params, functionName, async){
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp==null){
		alert ("Browser does not support HTTP Request");
		return;
	} 
	xmlHttp.open("POST", uri, async);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHttp.send(params);	
	xmlHttp.onreadystatechange=eval("stateChanged_"+functionName);
}	

function GetXmlHttpObject() {
  // Firefox, Opera 8.0+, Safari
  if (window.XMLHttpRequest){
    objXMLHttp=new XMLHttpRequest();
  }
  // Internet Explorer
  else if (window.ActiveXObject){
    try{
			objXMLHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e){
 			try{
 				objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
 			}
 			catch(e){};
		}
  }
    
  return objXMLHttp;
}



function createDomObject(xmltext){
	var parser = new DOMImplementation();
	var domDoc = parser.loadXML(xmltext);
	var docRoot = domDoc.getDocumentElement();
	return docRoot;
}

function getElementFromDom(tagName, docRoot){
	var temp = docRoot.getElementsByTagName(tagName).item(0).getFirstChild();
	
	if (temp) {
		return temp.getNodeValue();
	}
	else {
		console.log('Missing tagName: '+tagName);
		return '';
	}
}
