function HTMLDecode(encodedstring) {
    var tmp
    tmp = encodedstring
    tmp = tmp.replace( '&euro;', '€' );
    tmp = tmp.replace( '&lt;' , '<' );
    tmp = tmp.replace( '&gt;' , '>' );
    tmp = tmp.replace( "&amp;" , '&' );
    tmp = tmp.replace( '&quot;', '"' );
    tmp = tmp.replace( '&nbsp;', ' ' );
    for (var i=1; i<=255; i++)
		{
        tmp = tmp.replace('&#' + i + ';', String.fromCharCode(i));
		}    
    return tmp;
}

function vulUserForm()
{
	if (xmlHttp.readyState==4) {
		if(xmlHttp.responseText.length == 0) {
			return false;
		}
		var xmlDoc
		xmlDoc = parseXMLText(xmlHttp.responseText);
		var InputValueArray = xmlHttp.responseText.split("|")
		document.getElementById('xU_Sofinummer').disabled = true;	
		document.getElementById('xU_Voorletters').value  = InputValueArray[0];
		document.getElementById('xU_Voorletters').disabled = true;	
		document.getElementById('xU_Voorvoegsel').value  = InputValueArray[1];
		document.getElementById('xU_Voorvoegsel').disabled = true;	
		document.getElementById('xU_Naam').value  = HTMLDecode((InputValueArray[2]));
		document.getElementById('xU_Naam').disabled = true;		
		document.getElementById('xU_Meisjesnaam').value  = unescape(InputValueArray[3]);
		document.getElementById('xU_Meisjesnaam').disabled = true;
		setSelected('xU_Geslacht',InputValueArray[4]);
		document.getElementById('xU_Geslacht').disabled = true;		
		document.getElementById('xU_gebDatum').value  = InputValueArray[5];
		document.getElementById('xU_gebDatum').disabled = true;		
		setSelected('xIN_Aanvang_OplNiveau_ID',InputValueArray[6]);
		document.getElementById('xIN_Aanvang_OplNiveau_ID').disabled = true;
		document.getElementById('xZoekAO_ID').value  = InputValueArray[6];
		document.getElementById('xZoek_ID').value  = InputValueArray[7];
		alert('Reeds bekende deelnemer gevonden op sofinummer!\nDruk op opslaan om toe te voegen.');
	}
}	

function vulOpleidersXML(dropdownlijst)
{
	if (xmlHttp.readyState==4) {
		if( xmlHttp.responseText == "geen" ) {
			dropdownlijst.options.length = 1;
			return false;
		}
		var xmlDoc
		xmlDoc = parseXMLText(xmlHttp.responseText);

		var locid, locnaam;
		dropdownlijst.options.length = 1;
		for (var i=0; i<xmlDoc.getElementsByTagName("opleider").length;i++)
		{
			locid = xmlDoc.getElementsByTagName("opleider")[i].childNodes[0].childNodes[0].nodeValue
			locnaam = xmlDoc.getElementsByTagName("opleider")[i].childNodes[1].childNodes[0].nodeValue
			dropdownlijst.options[i+1] = new Option(locnaam,locid);
		}
		if (dropdownlijst.options.length == 2) {
			dropdownlijst.selectedIndex = 1;
			document.getElementById('xG_Startdatum').focus();
			document.getElementById('details').style.display = 'inline';
		}	
	}
}

function vulVariantenXML(dropdownlijst)
{
	if (xmlHttp.readyState==4) {
		if( xmlHttp.responseText == "geen" ) {
			dropdownlijst.options.length = 1;
			return false;
		}
		var xmlDoc
		xmlDoc = parseXMLText(xmlHttp.responseText);

		var locid, locnaam;
		dropdownlijst.options.length = 1;
		for (var i=0; i<xmlDoc.getElementsByTagName("opleider").length;i++)
		{
			locid = xmlDoc.getElementsByTagName("opleider")[i].childNodes[0].childNodes[0].nodeValue
			locnaam = xmlDoc.getElementsByTagName("opleider")[i].childNodes[1].childNodes[0].nodeValue
			dropdownlijst.options[i+1] = new Option(locnaam,locid);
		}
		if (dropdownlijst.options.length == 2) {
			document.getElementById('variant').style.display = 'none';
			dropdownlijst.selectedIndex = 1;
			document.getElementById('xG_Startdatum').focus();
			document.getElementById('details').style.display = 'inline';
		}
		if (dropdownlijst.options.length > 2) {
			document.getElementById('variant').style.display = 'inline';
		}
		if (dropdownlijst.options.length < 2) {
			document.getElementById('variant').style.display = 'none';
		}
		
	}
}

function getOpleiders(dropdownlijst, opleiding){
	geefAjaxValues2(function() { vulOpleidersXML(dropdownlijst) },"opleidersxml","&xOpleiding=" + opleiding);
}	

function getVarianten(dropdownlijst, opleidingnaam, bedrijfsid){
	geefAjaxValues2(function() { vulVariantenXML(dropdownlijst) },"variantenxml","&xOpl_Naam=" + opleidingnaam + "&xBedrijf_ID=" + bedrijfsid);
}	

function getUserBySofi(sofinr, groep_id, g_random){
	geefAjaxValues2(function() { vulUserForm() },"userbysofi","&xSofi=" + sofinr + "&xGroep_ID=" + groep_id + "&xG_Random=" + g_random);
}	

function chkNumber(obj) {
	if (obj.value.length > 0) {
		if (isNaN(obj.value) || obj.value.length == 0 || obj.value <= 0) {
			alert('Geen geldig aantal deelnemers ingevuld!');
			obj.focus();
		}
	}		
}

function IsDatumInPeriode(startPeriode, eindPeriode, controledatumID) {
	var start = '' + startPeriode.substr(3,2) +'/' + startPeriode.substr(0,2) + '/' + startPeriode.substr(6);
	var eind  = '' + eindPeriode.substr(3,2) +'/' + eindPeriode.substr(0,2) + '/' + eindPeriode.substr(6);
	var controle  = '' + document.getElementById(controledatumID).value.substr(3,2) +'/' + document.getElementById(controledatumID).value.substr(0,2) + '/' + document.getElementById(controledatumID).value.substr(6);
	if( Date.parse(controle) < Date.parse(start) || Date.parse(controle) > Date.parse(eind) ) {
	   alert('De startdatum van de opleiding ligt niet binnen de vereiste periode om in aanmerking te komen voor ESF.\n\n Deze periode loopt van ' + startPeriode + ' tot ' + eindPeriode + '.');
	   document.getElementById(controledatumID).focus();
	   return false;
	}
	return true
}

function ControleerStartEind(startdatumID, einddatumID) {
	var start = document.getElementById(startdatumID).value;
	var eind = document.getElementById(einddatumID).value;
   
	if (start.length > 0 && eind.length > 0) {
		// Datum format aanpassen
		start = '' + start.substr(3,2) +'/' + start.substr(0,2) + '/' + start.substr(6);
		eind = '' + eind.substr(3,2) + '/' + eind.substr(0,2) + '/' + eind.substr(6);
		if( Date.parse(start) > Date.parse(eind) ) {
		   alert('LET OP!: Einddatum is eerder dan de startdatum!');
		}
	}
}

function GetXmlHttpObject()
{
  var xmlHttp=null;
  try { // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
  }
  catch (e) { // Internet Explorer
    try {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  return xmlHttp;
}

function parseXMLText(tekst)
{
	var xmlDoc
	try //Internet Explorer
	{
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async="false";
		xmlDoc.loadXML(tekst);
	}
	catch(e)
	{
		try //Firefox, Mozilla, Opera, etc.
		{
			parser=new DOMParser();
			xmlDoc=parser.parseFromString(tekst,"text/xml");
		}
		catch(e)
		{
			alert(e.message);
			return;
		}
	}
	return xmlDoc;
}

function geefAjaxValues(functie, actie, extratekst){
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp!=null) {
		xmlHttp.onreadystatechange=functie;
		xmlHttp.open("POST","ajax.asp?actie="+actie+extratekst,false);
		xmlHttp.send(null);
	}
}

function geefAjaxValues2(functie, actie, extratekst){
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp!=null) {
		xmlHttp.onreadystatechange=functie;
		xmlHttp.open("GET","ajax.asp?actie="+actie+extratekst,true);
		xmlHttp.send(null);
	}
}
