// JavaScript Document

// laden van de loader die wordt gebruikt bij AJAX
loader=new Image();
loader.src="/assets/ajax-loader.gif";


// Ajax events using Jquery
var AjaxEvent = function( Options ) {
	// set (default) options
	var succesMethod = function(data) {
		alert("succes!");
		console.log("succes");
		console.log(data);
	};
	var Options = {
		url: Options.url + "?" + Math.random() || "",
		element: Options.element || "content",
		data: Options.data || "",
		type: Options.type ? "POST" : "GET",
		dataType: Option.dataType || "",
		error: Options.error || function(xhr, status) {
			alert('sorry, there was a problem!');
		},
		complete: Options.complete || null
	}
	// initiate ajax request
	$.ajax(Options).success(function(data) { 
//			 console.log("succes");
//			 console.log(data);
			 // insert data into element
			 $("#"+Options.element).html(data);
			 });
/*
	$.ajax({
		url: Options.url,
		data: Options.data,
		dataType: 'html',
		succes: function(data){ 
			alert("succes");
			console.log(data);
		},
		error: function(jqXHR){
			alert("error");
			console.log(jqXHR);
		},
		complete: function() {
		}
//			alert("succes: "+textStatus); 
//			console.log("succes"); 
//			console.log(textStatus);
//		},
//		error: function(jqXHR, textStatus, errorThrown) { console.log("error");},
//		complete: function (xhr, status) { alert('complete: '+status); console.log(xhr)
//		;}
//		complete: function() { 
//			console.log("complete"); 
//			console.log(xhr); 
//			$('#content').html(xhr.responseText)
//		;}

	}).success(function() { alert("success"); });
*/
}




// functie die bij een click alleen de content verandert:
function Click(dataSource, divID, get, noloader, Focus, Help)
{
	// bij demo aan dit meesturen naar content lader
	if(demo_switch.switcher)
	{
		get = 'demo=1&' + get;
	} 

	if(!Help) { Help = 2; }	
	GetData(dataSource, divID, get, noloader, Focus, Help);
}



// DMV GET informatie verzenden en ophalen via AJAX:

// dataSource: waar dient de informatie te worden opgehaald
// divID: naar welke div moet de opgehaalde info worden gestuurd
// get: wat moet via GET doorgegeven worden
// noloader: geeft aan dat de loader niet weergegeven dient te worden
// Focus: legt de focus op het eerste form item
// help: geeft link voor help info nodig indien aanwezig
// updateCheck: voorkomt dat de help link verdwijnt door de continue update_onlineheid checks
function GetData(dataSource, divID, get, noloader, Focus, Help, UpdateCheck)
{



var xmlhttp=false;

	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	// and security blocked creation of the objects.
 try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   xmlhttp = false;
  }
 }
@end @*/
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') 
	{
  	  xmlhttp = new XMLHttpRequest();  
	}


	  if(xmlhttp) 
	  {
	    var obj = document.getElementById(divID);
	    var laden = document.getElementById('laden-div');
		dataSource = dataSource + "?" + Math.random(); 
		if (get)
		{
		  dataSource = dataSource + "&" + get;
		}
		xmlhttp.open("GET", dataSource);

		xmlhttp.onreadystatechange = function()
		{
		  if (xmlhttp.readyState == 4 && 
		    xmlhttp.status == 200) 
		  {
		    obj.innerHTML = xmlhttp.responseText;
			delete xmlhttp;
			xmlhttp = null;

			// zorgen dat browser bovenaan komt
				var browser=navigator.appName;
				if (browser == 'Netscape')
				{
					obj.scrollTop = 900000000000;
				} else
				{
					var version = defineVersion();
					if (version != "8") // IE8, scrollen net als firefox
					{
						obj.scrollTop = -9000000000000;
					} else {
						obj.scrollTop = 9000000000000;
					}
				}

			// laden div cleanen
//			setTimeout("var laden = document.getElementById('laden-div');laden.innerHTML = ' '", 600);

			// help link weergeven?
			if(Help)
			{
				changeRule(6 ,'hide');
			}
			if(Help == 1)
			{
				changeRule(6 ,'visible');
			}
			//cursor in het eerste veld zetten:
			if (Focus)
			{
				putFocus(Focus-1,Focus-1);
			}
			// mootools datepicker
//			load_datepicker();

		  } else if ((xmlhttp.readyState == 1 || xmlhttp.readyState == 2 || xmlhttp.readyState == 3))
		  {
			if (!noloader)
			{
			  if(obj.innerHTML != "<b>Laden</b> <img src='/assets/ajax-loader.gif' />")
			  {
			  	obj.innerHTML = "<b>Laden</b> <img src='/assets/ajax-loader.gif' />";
			  }
			}
		  } else {
			  obj.innerHTML = '<center>Ophalen informatie niet gelukt</center>', xmlhttp.responseText;
		  }
		}
		xmlhttp.send(null);
	  }
}






// functie die voorkomt dat ik alle oude forms van creepmusic moet aanpassen
function postData(dataSource, divID, form, Focus)
{
	PostData(dataSource, divID, '', Focus, form);
}

// Functie om info via de POST te versturen via AJAX
// variabelen: de link en de info die via de post wordt verstuurd
// als ook de divID en eventueel focus
function PostData(dataSource, divID, Poststr, Focus, form, noloader)
{
var xmlhttp=false;

/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
 try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   xmlhttp = false;
  }
 }
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') 
{
  xmlhttp = new XMLHttpRequest();  
}

	  if(xmlhttp) 
	  {
	    var obj = document.getElementById(divID);
		xmlhttp.open("POST", dataSource, true);
		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		
		xmlhttp.onreadystatechange = function()
		{
		  if (xmlhttp.readyState == 4 && 
		    xmlhttp.status == 200) 
		  {
		    obj.innerHTML = xmlhttp.responseText;
			//cursor in het eerste veld zetten:
			if (Focus)
			{
				putFocus(0,0);
			}
			// mootools datepicker
			load_datepicker();
			
		  } else if (xmlhttp.readyState == 1 || xmlhttp.readyState == 2 || xmlhttp.readyState == 3 )
		  {
			if (!noloader)
			{
				if(obj.innerHTML != "<b>Laden</b> <img src='/assets/ajax-loader.gif' />")
				{
					obj.innerHTML = "<b>Laden</b> <img src='/assets/ajax-loader.gif' />";
				}
			}
		  } else {
			  obj.innerHTML = '<center>Ophalen informatie niet gelukt.</center>';
		  }
		}
    }


	//////// De informatie uit de formulieren halen: ////////////////////
		if(form == 'titel_aanpassen')
		{
			// variabelen ophalen:
			// verzenden van variabelen:
			xmlhttp.send("titel=" + escape(encodeURI(document.titel_aanpassen.titel.value)));


		} else if(form == 'songtext_aanpassen')
		{
			// variabelen ophalen:
			// verzenden van variabelen:
			xmlhttp.send("songtext=" + escape(encodeURI(document.songtext_aanpassen.songtext.value)));


		} else if(form == 'songpage_verwijderen')
		{
			var songpage_id = document.getElementById('songpage_id').
			    options[document.getElementById('songpage_id').selectedIndex].value;

			xmlhttp.send("songpage_id=" + songpage_id);


		} else if(form == 'songpage_verwijderen_bevestigd')
		{
			xmlhttp.send("songpage_id=" + escape(encodeURI(document.songpage_verwijderen.songpage_id.value)));


		// bericht_toevoegen:
		} else if(form == 'bericht_toevoegen')
		{
			// variabelen ophalen:
			if(typeof(document.book.elements['naam']) != 'undefined')
			{
			  // naam opgegeven:
			  var info = document.book.naam.value;
			} else {
				var info = "";
			}
			if(typeof(document.book.elements['email']) != 'undefined')
			{
			  // email opgegeven:
			  info += "&=&&&=&" + document.book.email.value;
			} else {
				info += "&=&&&=&";
			}
			if(typeof(document.book.elements['website']) != 'undefined')
			{
			  // naam opgegeven:
			  info += "&=&&&=&" + document.book.website.value;
			} else {
				info += "&=&&&=&";
			}
			if(typeof(FCKeditorAPI) == "object")
			{
			  var fckcontent = escape(FCKeditorAPI.GetInstance('message').GetXHTML());
			  info += "&=&&&=&" + fckcontent;
			} else if(typeof(document.book.elements['message']) != 'undefined')
			{
			  // bericht opgegeven:
			  info += "&=&&&=&" + document.book.message.value;

			} else {
				info += "&=&&&=&";
			}

			// verzenden van variabelen:
			xmlhttp.send("post=" + escape(encodeURI(info)));

			// bevestigen van het te plaatsen bericht. alle info zit in de hidden variabele post
		} else if(form == 'bericht_toevoegen_bevestigen')
		{
			var Sending = document.bericht_toevoegen.post.value;

			xmlhttp.send("post=" + escape(encodeURI(Sending)));


		} else if(form == 'titel_aanpassen')
		{
			// variabelen ophalen:
			// verzenden van variabelen:
			xmlhttp.send("titel=" + escape(encodeURI(document.titel_aanpassen.titel.value)));


		} else if(form == 'songtext_aanpassen')
		{
			// variabelen ophalen:
			// verzenden van variabelen:
			xmlhttp.send("songtext=" + escape(encodeURI(document.songtext_aanpassen.songtext.value)));


		} else if(form == 'songpage_verwijderen')
		{
			var songpage_id = document.getElementById('songpage_id').
			    options[document.getElementById('songpage_id').selectedIndex].value;

			xmlhttp.send("songpage_id=" + songpage_id);


		} else if(form == 'songpage_verwijderen_bevestigd')
		{
			xmlhttp.send("songpage_id=" + escape(encodeURI(document.songpage_verwijderen.songpage_id.value)));


		} else if(form == 'kalenderitem_add')
		{

			// ophalen informatie uit de verschillende velden:
			// dag:
			var dag = document.kalenderitem_toevoegen.dag.value;
			// maand:
			var maand = document.kalenderitem_toevoegen.maandID.value;
			// jaar:
			var jaar = document.kalenderitem_toevoegen.jaar.value;
			// tijd:
			var tijd = document.kalenderitem_toevoegen.tijd.value;
			// titel:
			var titel = document.kalenderitem_toevoegen.titel.value;
			// details:
			var details = document.kalenderitem_toevoegen.details.value;
			// jaarlijks:

			if(typeof(document.kalenderitem_toevoegen.elements['exp_dag']) != 'undefined')
			{
			  // exp_dag:
			  var exp_dag = document.kalenderitem_toevoegen.exp_dag.value;
			  // exp_maand:
			  var exp_maand = document.kalenderitem_toevoegen.exp_maandID.value;
			  // exp_jaar:
			 var exp_jaar = document.kalenderitem_toevoegen.exp_jaar.value;
			}


			for (var i=0; i < document.kalenderitem_toevoegen.jaarlijks.length; i++)
			{
			   if (document.kalenderitem_toevoegen.jaarlijks[i].checked)
			      {
			      var jaarlijks = document.kalenderitem_toevoegen.jaarlijks[i].value;
			      }
			}

			
			//array aanmaken met de gegevens:
			var kalenderitems;
			kalenderitems = dag;
			kalenderitems += "&=&&&=&" + maand;
			kalenderitems += "&=&&&=&" + jaar;
			kalenderitems += "&=&&&=&" + tijd;
			kalenderitems += "&=&&&=&" + titel;
			kalenderitems += "&=&&&=&" + details;
			kalenderitems += "&=&&&=&" + jaarlijks;
			
			if(typeof(document.kalenderitem_toevoegen.elements['exp_dag']) != 'undefined')
			{
			  kalenderitems += "&=&&&=&" + exp_dag;
			  kalenderitems += "&=&&&=&" + exp_maand;
			  kalenderitems += "&=&&&=&" + exp_jaar;
			}

			xmlhttp.send("post=" + escape(encodeURI(kalenderitems)));


		} else if(form == 'kalenderitem_addPost')
		{
			var Sending = document.kalenderitem_toevoegen.post.value;

			xmlhttp.send("post=" + escape(encodeURI(Sending)));

		} else if(form == 'dossier_verwijderen')
		{
			// ophalen informatie:
			// dossierID:
			var DossierID = document.getElementById('dossier_id').options[document.getElementById('dossier_id').selectedIndex].value;
			var Poststr = "dossier_id=" + DossierID;
			xmlhttp.send(Poststr);

		} else if(form == 'dossier_verwijderen_bevestigd')
		{
	
			var Poststr = "dossier_id=" + document.dossier_verwijderen_form.dossier_id.value;
			xmlhttp.send(Poststr);

		} else if(form == 'dossier_toevoegen')
		{
			// ophalen informatie uit de verschillende velden:
			// naam:
			var voornaam = "voornaam=" + document.dossier_toevoegen_form.voornaam.value;
			// naam:
			var tussenvoegsel = "&tussenvoegsel=" + document.dossier_toevoegen_form.tussenvoegsel.value;
			// naam:
			var naam = "&naam=" + document.dossier_toevoegen_form.naam.value;
			// adres:
			var adres = "&adres=" + document.dossier_toevoegen_form.adres.value;
			// postcode:
			var postcode = "&postcode=" + document.dossier_toevoegen_form.postcode.value;
			// woonplaats:
			var woonplaats = "&woonplaats=" + escape(document.dossier_toevoegen_form.woonplaats.value);
			// telnum1:
			var telnum1 = "&telnum1=" + document.dossier_toevoegen_form.telnum1.value;
			// telnum2:
			var telnum2 = "&telnum2=" + document.dossier_toevoegen_form.telnum2.value;
			// telnum3:
			var telnum3 = "&telnum3=" + document.dossier_toevoegen_form.telnum3.value;
			// geboortedag:
			var geb_dag = "&geb_dag=" + document.dossier_toevoegen_form.geb_dag.value;
			// geboortemaand:
			var geb_maand = "&geb_maand=" + document.dossier_toevoegen_form.geb_maandID.value;
			// geboortejaar:
			var geb_jaar = "&geb_jaar=" + document.dossier_toevoegen_form.geb_jaar.value;
			// vestiging:
			var vestiging = "&vestiging=" + document.dossier_toevoegen_form.vestiging.value;
			// testdag:
//			var test_dag = "&test_dag=" + document.dossier_toevoegen_form.test_dag.value;
			// testmaand:
//			var test_maand = "&test_maand=" + document.dossier_toevoegen_form.test_maandID.value;
			// testjaar:
//			var test_jaar = "&test_jaar=" + document.dossier_toevoegen_form.test_jaar.value;
			// geslacht
			var geslacht = "&geslacht=" + document.dossier_toevoegen_form.geslacht.value;
		
			var Poststr = voornaam + tussenvoegsel + naam + geslacht + geb_dag + geb_maand + geb_jaar + adres + postcode + woonplaats + telnum1 + telnum2 + telnum3 + vestiging;
			xmlhttp.send(Poststr);

		} else if(form == 'obs_opslaan')
		{
			// ophalen info uit /content/dossiers/tests/includes/observer.inc.php
			var observant = "observant=" + document.getElementById('observanten_sel').options[document.getElementById('observanten_sel').selectedIndex].value;
			var test_dag = "&test_dag=" + document.obs_opslaan_form.test_dag.value;
			// testmaand:
			var test_maand = "&test_maand=" + document.obs_opslaan_form.test_maandID.value;
			// testjaar:
			var test_jaar = "&test_jaar=" + document.obs_opslaan_form.test_jaar.value;

			var Poststr = observant + test_dag + test_maand + test_jaar;
			xmlhttp.send(Poststr);

		} else {
			xmlhttp.send(Poststr);
		}
}
