// JavaScript Document
var path_gastenboek = "/gastenboek/"

function gastenboek_load(pagina, sEvent) {
	switch(sEvent) {
		case "geladen": {
			break;
		}
		default: {
			document.getElementById("spnAllEntry").innerHTML = "<span id='spnLoading_entry' class='spnLoading_entry'>Bezig met laden...</span>";
			document.getElementById("spnAllEntry").style.display = "block";
			gastenboek_data_get(path_gastenboek + "data/load_entry.asp?pagnum=" + pagina, "spnAllEntry", "gastenboek_load('" + pagina + "','geladen')");
			break;
		}
	}
}

function deleteEntry(id, currentPage,sEvent) {
	switch(sEvent) {
		case "geladen": {
			gastenboek_load(currentPage);
			break;
		}
		default: {
			if (confirm("Ben je zeker dat je deze ingave wilt offline plaatsen?")) {
				gastenboek_data_get(path_gastenboek + "data/del_entry.asp?del=" + id, "", "deleteEntry('" + id + "','" + currentPage + "','geladen')");
			}
			break;
		}
	}
}

function closeReactie(id) {
	document.getElementById("spnReact" + id).style.display = "none";
}

function saveReactie(id, sEvent) {
	switch(sEvent) {
		case "geladen": {
			alert("Seef ok");
			break;
		}
		default: {
			var reactie_content = document.getElementById("txtReactie" + id).value;
			gastenboek_data_get(path_gastenboek + "data/mod_react.asp?open=" + id + "&save=" + id + "&r=" + encodeURI(reactie_content), "spnReact" + id, "saveReactie('" + id + "','geladen')");
				break;
				}
	}
}

function openReactie(id, currentPage,sEvent) {
	switch(sEvent) {
		case "geladen": {
			break;
		}
		default: {
			document.getElementById("spnReact" + id).style.display = "block";
			document.getElementById("spnReact" + id).innerHTML = "<span id='spnLoading_entry" + id + "' class='spnLoading_entry'>Bezig met laden...</span>";
				gastenboek_data_get(path_gastenboek + "data/mod_react.asp?open=" + id, "spnReact" + id, "openReactie('" + id + "','" + currentPage + "','geladen')");
				break;
				}
	}
}

function openNewEntry(sEvent) {
	switch(sEvent) {
		case "geladen": {
			break;
		}
		default: {
			document.getElementById("spnWelcome").style.display = "none";
			document.getElementById("spnNewEntry").innerHTML = "<span id='spnLoading_entry' class='spnLoading_entry'>Bezig met laden...</span>";
			document.getElementById("spnNewEntry").style.display = "block";
			gastenboek_data_get(path_gastenboek + "data/new_entry.asp", "spnNewEntry", "openNewEntry('geladen')");
			break;
		}
	}
}

function closeNewEntry() {
	document.getElementById("spnNewEntry").style.display = "none";
	document.getElementById("spnNewEntry").innerHTML = "";
	document.getElementById("spnWelcome").style.display = "block";
}

function clearNewEntry() {
	closeNewEntry();
	gastenboek_load('1');
}

function saveNewEntry(sEvent) {
	var gb_naam = document.getElementById("gb_naam").value;
	var gb_mail = document.getElementById("gb_email").value;
	var gb_site = document.getElementById("gb_website").value;
	var gb_beri = document.getElementById("gb_bericht").value;
	var sErr    = "";
	if (gb_naam=="") sErr += "\n- Geen naam ingevuld.";
	if (gb_beri=="") sErr += "\n- Geen bericht ingevuld.";
	if (gb_mail!="") { if (!gastenboek_checkmail(gb_mail)) {sErr += "\n- Ongeldig e-mailadres ingevuld.";} }
	if (sErr!="") {
		alert("De volgende problemen deden zich voor:\n" + sErr); } 
	else {
		document.frmGastenboek.submit();
	}
}


var xmlhttp;

function gastenboek_data_get(url, spanControl, js_loadfunctie) {
	xmlhttp = false;
	// branch for native XMLHttpRequest object
	if(window.XMLHttpRequest) {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch(e) {
			xmlhttp = false;
		}
		// branch for IE/Windows ActiveX version
	} 
	else if(window.ActiveXObject) {
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				xmlhttp = false;
			}
		}
	}
	if(xmlhttp) {
		xmlhttp.onreadystatechange = function() {
 			if (xmlhttp.readyState==4) {
   				if (spanControl!="") document.getElementById(spanControl).innerHTML = xmlhttp.responseText;
				if (js_loadfunctie!="") eval(js_loadfunctie);
  			}
		}
		xmlhttp.open("GET", url, true);
		xmlhttp.send("");
	}
}

function gastenboek_data_sendlist_get(url, spanControl, js_loadfunctie, sendlist) {
	xmlhttp = false;
	// branch for native XMLHttpRequest object
	if(window.XMLHttpRequest) {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch(e) {
			xmlhttp = false;
		}
		// branch for IE/Windows ActiveX version
	} 
	else if(window.ActiveXObject) {
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				xmlhttp = false;
			}
		}
	}
	if(xmlhttp) {
		xmlhttp.onreadystatechange = function() {
 			if (xmlhttp.readyState==4) {
   				if (spanControl!="") document.getElementById(spanControl).innerHTML = xmlhttp.responseText;
				if (js_loadfunctie!="") eval(js_loadfunctie);
  			}
		}
		xmlhttp.open("GET", url, true);
		xmlhttp.send(sendlist);
	}
}

function gastenboek_data_post(url, spanControl, js_loadfunctie, sendlist) {
	xmlhttp = false;
	// branch for native XMLHttpRequest object
	if(window.XMLHttpRequest) {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch(e) {
			xmlhttp = false;
		}
		// branch for IE/Windows ActiveX version
	} 
	else if(window.ActiveXObject) {
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				xmlhttp = false;
			}
		}
	}
	if(xmlhttp) {
		xmlhttp.onreadystatechange = function() {
 			if (xmlhttp.readyState==4) {
   				if (spanControl!="") document.getElementById(spanControl).innerHTML = xmlhttp.responseText;
				if (js_loadfunctie!="") eval(js_loadfunctie);
  			}
		}
		xmlhttp.open("POST", url, false);
		xmlhttp.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");
		xmlhttp.send(sendlist);
	}
}

function trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}

function gastenboek_checkmail(sEmail) { 
  var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
  return regex.test(sEmail);
}

