// ajax_comobo_boxes.js

/*  This page does all the Ajax to a 
 *  "double or triple or n.. related combo boxes" 
 *  The selected option data is sent to a PHP 
 *  script using the GET method.
 *  The PHP script sends back a response in XML format.
 */

// Have a function run after the page loads:
//window.onload = init;
window.onload = getTipoEntidadeEspecifica;


	// Selecciona os tipos de entidades especicifcas correspondentes ao tipo de entidade seleccionado
	function getTipoEntidadeEspecifica()
		{
			var tipoEntidadeID = document.formEntidade.tipoEntidadeID.value;
			if(tipoEntidadeID > 0)
				{
					// Retorna os concelhos correspondentes ao distrito seleccionado
					var Ajax = getXMLHttpRequestObject();
					if (!Ajax)
						{
							alert('Ajax inoperacional');
							return;
						}
					Ajax.onreadystatechange = function()
						{
							showTipoEntidadeEspecifica(Ajax);
						}
					Ajax.open('GET', 'http://localhost:8888/omd/module/tipoEntidade.php?tipoEntidadeID=' + tipoEntidadeID, true);
					Ajax.send(null);
				}
			else
				{
					return;
					//alert('Por favor, seleccione o tipo de entidade pretendido.');
				}
		}
		
	// Apresenta numa combo-box/select os tipos de entidades especificas correspondentes ao tipo de entidade seleccionado
	function showTipoEntidadeEspecifica(Ajax)
		{
			if(Ajax.readyState == 4)
				{
					if (Ajax.status == 200 || Ajax.status == 304)
						{
							// Recebe XML com lista de tipos de entidades especificas
							var xmldoc = Ajax.responseXML;
							if (xmldoc.hasChildNodes())
								{
									var nos 			= xmldoc.getElementsByTagName('tipoEntidadeEspecificaID');
									var comp = nos.length;
									var tipoEntidadeID 	= document.formEntidade.tipoEntidadeID.value;
									var tipoEntidadesEspecificas 	= document.formEntidade.Especificar;
									tipoEntidadesEspecificas.options.length = 0;
									var opt 			= new Option('Seleccione um tipo de entidade especifico', '-1');
									tipoEntidadesEspecificas.add(opt, null);
									for(var i=0;i<nos.length;i++)
										{
											var no = nos[i];
											var cod = no.childNodes[0].firstChild.nodeValue;
											var des = no.childNodes[1].firstChild.nodeValue;
											var opt = new Option(des, cod);
											tipoEntidadesEspecificas.add(opt, null);
										}
								}
						}
					else
						{
							alert('Erro: ' + Ajax.statusText);
						}
				}
		}

	// Function to show/hide field yes qual?
	function showHideFieldEscola(registo)
		{ 	
			// Define var to handle div id reference
			var block1 = document.getElementById('showHideFieldEscola');
				// Check if parameter registo has assigned the value "Outra"
				if (registo == 'Sim' || registo == '2')
					{
						// If previous condition is true display switch to block and shows on the form id div
						block1.style.display = 'block';
					}
				else
					{
						// If not, switch state display to none and omits the form id div
						block1.style.display = 'none';
					}
				return true;
		}

	// Function to show/hide field no qual?
	function showHideFieldClube(registo)
		{ 	
			// Define var to handle div id reference
			var block1 = document.getElementById('showHideFieldClube');
				// Check if parameter registo has assigned the value "Outra"
				if (registo == 'Sim' || registo == '2')
					{
						// If previous condition is true display switch to block and shows on the form id div
						block1.style.display = 'block';
					}
				else
					{
						// If not, switch state display to none and omits the form id div
						block1.style.display = 'none';
					}
				return true;
		}

	// Function to show/hide field Pais residencia
	function showHideFieldPais(registo)
		{ 	
			// Define var to handle div id reference
			var block1 = document.getElementById('showHideFieldDistrito');
			var block2 = document.getElementById('showHideFieldEstrangeiro');
				// Check if parameter registo has assigned the value "Outra"
				if (registo == 'Portugal' || registo =='1')
					{
						// If previous condition is true display switch to block and shows on the form id div
						block1.style.display = 'block';
						block2.style.display = 'none';
					}
				else if (registo == 'Estrangeiro' || registo == '3')
					{
						// If previous condition is true display switch to block and shows on the form id div
						block1.style.display = 'none';
						block2.style.display = 'block';
					}
				else
					{
						// If not, switch state display to none and omits the form id div
						block1.style.display = 'none';
						block2.style.display = 'none';
					}
				return true;
		}

	// Selecciona os concelhos correspondentes ao distrito seleccionado
	function getConcelhos()
		{
			var codigoDistrito = document.formEntidade.codigoDistrito.value;
			if(codigoDistrito > 0)
				{
					// Retorna os concelhos correspondentes ao distrito seleccionado
					var Ajax = getXMLHttpRequestObject();
					if (!Ajax)
						{
							alert('Ajax inoperacional');
							return;
						}
					Ajax.onreadystatechange = function ()
						{
							showConcelhos(Ajax);
						}
					Ajax.open('GET', 'http://localhost:8888/omd/module/concelho.php?codigoDistrito=' + codigoDistrito, true);
					Ajax.send(null);
				}
			else
				{
					alert('Por favor, seleccione o Distrito pretendido.');
				}
		}

	// Apresenta numa combo-box/select os concelhos correspondentes ao distrito seleccionado
	function showConcelhos(Ajax)
		{
			if(Ajax.readyState == 4)
				{
					if (Ajax.status == 200 || Ajax.status == 304)
						{
							// Recebe XML com lista de Concelhos
							var xmldoc = Ajax.responseXML;
							if (xmldoc.hasChildNodes())
								{
									var nos 			= xmldoc.getElementsByTagName('codigoConcelho');
									// var comp = nos.length;
									var codigoDistrito 	= document.formEntidade.codigoDistrito.value;
									var concelhos 	= document.formEntidade.Concelho;
									concelhos.options.length = 0;
									var opt 			= new Option('Seleccione um Concelho', '-1');
									concelhos.add(opt, null);
									for(var i=0;i<nos.length;i++)
										{
											var no = nos[i];
											var cod = no.childNodes[0].firstChild.nodeValue;
											var des = no.childNodes[1].firstChild.nodeValue;
											var opt = new Option(des, cod);
											concelhos.add(opt, null);
										}
								}
						}
					else
						{
							alert('Erro: ' + Ajax.statusText);
						}
				}
		}

	// Selecciona os concelhos correspondentes ao distrito seleccionado
	function getFreguesias()
		{
			var codigoConcelho = document.formEntidade.codigoConcelho.value;
			if(codigoConcelho > 0)
				{
					// Retorna os concelhos correspondentes ao distrito seleccionado
					var Ajax = getXMLHttpRequestObject();
					if (!Ajax)
						{
							alert('Ajax inoperacional');
							return;
						}
					Ajax.onreadystatechange = function()
						{
							showFreguesias(Ajax);
						}
					Ajax.open('GET', 'http://localhost:8888/omd/module/freguesia.php?codigoConcelho=' + codigoConcelho, true);
					Ajax.send(null);
				}
			else
				{
					alert('Por favor, seleccione o Concelho pretendido.');
				}
		}

	// Apresenta numa combo-box/select os concelhos correspondentes ao distrito seleccionado
	function showFreguesias(Ajax)
		{
			if(Ajax.readyState == 4)
				{
					if (Ajax.status == 200 || Ajax.status == 304)
						{
							// Recebe XML com lista de Concelhos
							var xmldoc = Ajax.responseXML;
							if (xmldoc.hasChildNodes())
								{
									var nos 			= xmldoc.getElementsByTagName('codigoFreguesia');
									// var comp = nos.length;
									var codigoConcelho 	= document.formEntidade.codigoConcelho.value;
									var freguesias 	= document.formEntidade.Freguesia;
									freguesias.options.length = 0;
									var opt 			= new Option('Seleccione uma Freguesia', '-1');
									freguesias.add(opt, null);
									for(var i=0;i<nos.length;i++)
										{
											var no = nos[i];
											var cod = no.childNodes[0].firstChild.nodeValue;
											var des = no.childNodes[1].firstChild.nodeValue;
											var opt = new Option(des, cod);
											freguesias.add(opt, null);
										}
								}
						}
					else
						{
							alert('Erro: ' + Ajax.statusText);
						}
				}
		}
