// JavaScript Document
var data;

//
String.prototype.trim = function()
{
	a = this.replace(/^\s+/, '');
	return a.replace(/\s+$/, '');
};

//
String.prototype.replaceAll = function (strTarget, strSubString)
{
	var strText = this;
	var intIndexOfMatch = strText.indexOf(strTarget);
	 
	// Keep looping while an instance of the target string
	// still exists in the string.
	while (intIndexOfMatch != -1)
	{
		// Relace out the current instance.
		strText = strText.replace(strTarget, strSubString)
		// Get the index of any next matching substring.
		intIndexOfMatch = strText.indexOf(strTarget);
	}
	 
	// Return the updated string with ALL the target strings
	// replaced out with the new substring.
	return strText;
}

//
function populateEmdLabels(el)
{
	if (el.checked == true)
	{
		$('dlabel_1').value = 'Naam';
		$('dlabel_2').value = 'Voornaam';
		$('dlabel_3').value = 'Adres';
		$('dlabel_4').value = 'Geboortedatum';
		$('dlabel_5').value = 'Geslacht';
		$('dlabel_6').value = 'Telefoon';
		$('rdata_1').checked = true;
		$('rdata_2').checked = true;
	}
}

function populateEmdLabelsnl(el)
{
	if (el.checked == true)
	{
		$('dlabel_1').value = 'Naam';
		$('dlabel_2').value = 'Voornaam';
		$('dlabel_3').value = 'Adres';
		$('dlabel_4').value = 'Geboortedatum';
		$('dlabel_5').value = 'Geslacht';
		$('dlabel_6').value = 'Telefoon';
		$('rdata_1').checked = true;
		$('rdata_2').checked = true;
	}
}

function populateEmdLabelsen(el)
{
	if (el.checked == true)
	{
		$('dlabel_1').value = 'Surname';
		$('dlabel_2').value = 'Name';
		$('dlabel_3').value = 'Address';
		$('dlabel_4').value = 'Date of birth';
		$('dlabel_5').value = 'Sex';
		$('dlabel_6').value = 'Phone';
		$('rdata_1').checked = true;
		$('rdata_2').checked = true;
	}
}

function populateEmdLabelsde(el)
{
	if (el.checked == true)
	{
		$('dlabel_1').value = 'Name';
		$('dlabel_2').value = 'Vorname';
		$('dlabel_3').value = 'Adresse';
		$('dlabel_4').value = 'Geburtsdatum';
		$('dlabel_5').value = 'Geschlecht';
		$('dlabel_6').value = 'Telefon';
		$('rdata_1').checked = true;
		$('rdata_2').checked = true;
	}
}

function populateEmdLabelsfr(el)
{
	if (el.checked == true)
	{
		$('dlabel_1').value = 'Nom';
		$('dlabel_2').value = 'Prénom';
		$('dlabel_3').value = 'Adresse';
		$('dlabel_4').value = 'Date de naissance';
		$('dlabel_5').value = 'Sexe';
		$('dlabel_6').value = 'Téléphone ';
		$('rdata_1').checked = true;
		$('rdata_2').checked = true;
	}
}

//
function searchPatients(agendaID)
{
	clearPatient();
	clearSuggestions();
	
	var lastname = $F('data_0');
	var firstname = $F('data_1');
	var dateofbirth = $F('data_3');
	var timestamp = Math.round(new Date().getTime()/1000);
	
	lastname = lastname.trim();
	firstname = firstname.trim();
	dateofbirth = dateofbirth.trim();
	
	if (dateofbirth.length > 0)
	{
		var pattern = new RegExp("[0-3][0-9]-(0|1)[0-9]-(19|20)[0-9]{2}");
		if (pattern.test(dateofbirth) == false)
		{
			alert('Geboortedatum is niet correct (dd-mm-yyyy).');
			return;
		}
	}
	
	new Ajax.Request('/agenda_search_emd.php',
	{
		method:'get',
		parameters: {id:agendaID, lastname:lastname, firstname:firstname, dateofbirth:dateofbirth},
		onLoading: function()
		{},
		onSuccess: function(transport)
		{},
		onFailure: function()
		{
			alert('Zoekopdracht mislukt!');
		},
		onComplete: function(transport)
		{
			var response = transport.responseText || "no response text";
			//alert(response);

			data = response.evalJSON();
			
			if (data.length > 0)
			{
				var list = new Element('ul', {'id' : 'thelist'});
				
				for (var i=0; i<data.length; i++)
				{
					if(data[i].lastname == null) data[i].lastname = "";
					if(data[i].firstname == null) data[i].firstname = "";
					if(data[i].dateofbirth == null) data[i].dateofbirth = "";

					var lastname = data[i].lastname.replaceAll("'","&#39;");
					var firstname = data[i].firstname.replaceAll("'","&#39;");
					var dateofbirth = data[i].dateofbirth;

				    var item = new Element('li', {'id' : i});
				    item.observe('click', respondToClick);
				    item.update(lastname + ' ' + firstname + ' ' + dateofbirth);
				    list.insert(item);  
				}
				
				$('suggestions').insert(list);
			}
			else
				alert('De patient werd niet gevonden.\nWijzig a.u.b. uw zoekopdracht.');	
		}
	});
}

//
function respondToClick(event)
{
	var element = event.element();
	var elementId = element.id;
	
	var id = data[elementId].id;
	var autonumber = data[elementId].autonumber;
	var lastname = data[elementId].lastname;
	var firstname = data[elementId].firstname;
	var address = data[elementId].address;
	var dateofbirth = data[elementId].dateofbirth;
	var sex = data[elementId].sex;
	var phone = data[elementId].phone;
	
	$('patientID').value = id;
	$('autonumber').value = autonumber;
	$('data_0').value = lastname;
	$('data_1').value = firstname;
	$('data_2').value = address;
	$('data_3').value = dateofbirth;
	$('data_4').value = sex;
	$('data_5').value = phone;
}

//
function clearSuggestions()
{
	if ($('thelist')) $('thelist').remove();
}

//
function clearPatient()
{
	$('patientID').value = '';
	$('autonumber').value = '';
	//$('data_0').value = '';	
	//$('data_1').value = '';
	//$('data_2').value = '';
	//$('data_3').value = '';
	$('data_4').value = '';
}

