// Browser Detection
isMac = (navigator.appVersion.indexOf("Mac")!=-1) ? true : false;
NS4 = (document.layers) ? true : false;
IEmac = ((document.all)&&(isMac)) ? true : false;
IE4plus = (document.all) ? true : false;
IE4 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 4.")!=-1)) ? true : false;
IE5 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 5.")!=-1)) ? true : false;
ver4 = (NS4 || IE4plus) ? true : false;
NS6 = (!document.layers) && (navigator.userAgent.indexOf('Netscape')!=-1)?true:false;

function preencheNubente(){

	if(document.getElementById('nub_apresentante_sexo').value=="M"){
		
		document.getElementById('nub_apresentante_sexo').disabled = true;
		
//		document.getElementById('apresentante').className = 'NubInfo1 NubInfoSep';
		
//		document.getElementById('nub1_nome_proprio').value = document.getElementById('certificadoNome').value;
//		document.getElementById('nub1_nome_proprio').disabled = true;
//		document.getElementById('nub1_nome_apelido').value = document.getElementById('certificadoApelido').value;
//		document.getElementById('nub1_nome_apelido').disabled = true;
//		document.getElementById('nub1_bi_nr').value = document.getElementById('certificadoDocumento').value;
//		document.getElementById('nub1_bi_nr').disabled = true;
	

//		document.getElementById('apresentante').className = 'NubInfo2';
//		document.getElementById('convidado').className = 'NubInfo1';
		
		

		
		
//		//Oculta o form padrão da Noiva é o Form auxiliar do Noivo
//		document.getElementById("NubInfo2").style.visibility='hidden';
//		document.getElementById("NubInfo2").style.display='none';
//		document.getElementById("NubInfo1Aux").style.visibility='hidden';
//		document.getElementById("NubInfo1Aux").style.display='none';
//		//Mostra o form auxiliar da Noiva
//		document.getElementById("NubInfo2Aux").style.visibility='visible';
//		document.getElementById("NubInfo2Aux").style.display='block';
	
		
		/*document.getElementById('NubInfo1').style.position = 'absolute';
		document.getElementById('NubInfo1').style.top = '0';
		document.getElementById('NubInfo1').style.left = '0';
		document.getElementById('NubInfo2').style.position = 'absolute';
		document.getElementById('NubInfo2').style.top = '0';
		document.getElementById('NubInfo2').style.right = '0';
		document.getElementById("ocultaCampoNoiva").style.visibility='hidden';*/
	
	
	} else if(document.getElementById('nub_apresentante_sexo').value=="F"){
		
		document.getElementById('nub_apresentante_sexo').disabled = true;
		
//		document.getElementById('apresentante').className = 'NubInfo2 NubInfoSep';
//		document.getElementById('convidado').className = 'NubInfo1';
//		document.getElementById('msgNoivo').className = 'NubInfo2'; 

//		document.getElementById('nub2_nome_proprio').value = document.getElementById('certificadoNome').value;
//		document.getElementById('nub2_nome_proprio').disabled = true;
//		document.getElementById('nub2_nome_apelido').value = document.getElementById('certificadoApelido').value;
//		document.getElementById('nub2_nome_apelido').disabled = true;
//		document.getElementById('nub2_bi_nr').value = document.getElementById('certificadoDocumento').value;
//		document.getElementById('nub2_bi_nr').disabled = true;

	
	} else {
		
		document.getElementById('nub_apresentante_sexo').disabled = false;
		
		document.getElementById('nub1_nome_proprio').value = '';
		document.getElementById('nub1_nome_proprio').disabled = false;
		document.getElementById('nub1_nome_apelido').value = '';
		document.getElementById('nub1_nome_apelido').disabled = false;
		document.getElementById('nub1_bi_nr').value = '';
		document.getElementById('nub1_bi_nr').disabled = false;
		document.getElementById('nub2_nome_proprio').value = '';
		document.getElementById('nub2_nome_proprio').disabled = false;
		document.getElementById('nub2_nome_apelido').value = '';
		document.getElementById('nub2_nome_apelido').disabled = false;
		document.getElementById('nub2_bi_nr').value = '';
		document.getElementById('nub2_bi_nr').disabled = false;
	}	
}

function setSelectedIndex(id, value){
	
	sel = document.getElementById(id);
	if(sel != null){
		for (var i=0; i<sel.options.length; i++) {
			if (sel.options[i].value == value) {
				sel.selectedIndex = i;
			}
		}
	}
	
}

function generateCombo(changedCombo, zona) {

	var distritoId;
	var concelhoId;
	var freguesiaId;
	var concelhoIdDiv;
	var freguesiaIdDiv;
	if(zona=='1'){
		distritoId = "nub1_natural_distrito";
		concelhoId = "nub1_natural_concelho";
		freguesiaId = "nub1_natural_freguesia";
		concelhoIdDiv = "nub1_natural_concelho_div";
		freguesiaIdDiv = "nub1_natural_freguesia_div";
	} else if(zona=='2'){
		distritoId = "nub1_residencia_distrito";
		concelhoId = "nub1_residencia_concelho";
		freguesiaId = "nub1_residencia_freguesia";
		concelhoIdDiv = "nub1_residencia_concelho_div";
		freguesiaIdDiv = "nub1_residencia_freguesia_div";
	} else if(zona=='3'){
		distritoId = "nub2_natural_distrito";
		concelhoId = "nub2_natural_concelho";
		freguesiaId = "nub2_natural_freguesia";
		concelhoIdDiv = "nub2_natural_concelho_div";
		freguesiaIdDiv = "nub2_natural_freguesia_div";
	} else if(zona=='4'){
		distritoId = "nub2_residencia_distrito";
		concelhoId = "nub2_residencia_concelho";
		freguesiaId = "nub2_residencia_freguesia";
		concelhoIdDiv = "nub2_residencia_concelho_div";
		freguesiaIdDiv = "nub2_residencia_freguesia_div";
	} else if(zona=='5'){
		distritoId = "proc_casamento_civil_local_conservatoria_distrito";
		conservatoriaId = "proc_casamento_civil_local_conservatoria_nome_div";
	}  else if(zona=='6'){
		distritoId = "proc_conservatoria_distrito";
		conservatoriaId = "proc_conservatoria_nome_div";
	} else if(zona == '7'){
		distritoId = "codDistritoCasamento";
		concelhoId = "codConcelhoCasamento";
		freguesiaId = "nub1_natural_freguesia";
		concelhoIdDiv = "codConcelhoCasamento_div";
		freguesiaIdDiv = "codFreguesiaCasamento_div";
	} else if(zona == '8'){
		distritoId = "distrito";
		conservatoriaId = "conservatoria_div";
	}
	
	
	function errh(msg) {
		
		alert(msg);
	}
	
	dwr.engine.setErrorHandler(errh);	

	if(changedCombo == 'processoCasamento') {
		
		Remote.getConservatoriasCombo(document.getElementById(distritoId).options[document.getElementById(distritoId).selectedIndex].value, zona, function handleReturn(data) {
			
			document.getElementById(conservatoriaId).innerHTML = data;
			
		});
		
	} else if(changedCombo == 'processoDivorcio') {
		
		Remote.getConservatoriasCombo(document.getElementById(distritoId).options[document.getElementById(distritoId).selectedIndex].value, zona, function handleReturn(data) {
			
			document.getElementById(conservatoriaId).innerHTML = data;
			
		});
		
	} else {
		
		if(changedCombo == 'distritocombo') {
			
			Remote.getConcelhosCombo(document.getElementById(distritoId).options[document.getElementById(distritoId).selectedIndex].value, zona, function handleReturn(data) {
				
				document.getElementById(concelhoIdDiv).innerHTML = data;
				
			});		
		}
		
		Remote.getFreguesiasCombo(document.getElementById(distritoId).options[document.getElementById(distritoId).selectedIndex].value, document.getElementById(concelhoId).options[document.getElementById(concelhoId).selectedIndex].value, zona, function handleReturn(data) {	
			document.getElementById(freguesiaIdDiv).innerHTML = data;
			
		});
	}

}

function generateConservatorias(distrito, selectedConservatoria) {
	
	function errh(msg) {
		alert(msg);
	}
	
	dwr.engine.setErrorHandler(errh);	
		
	Remote.getConservatorias(distrito.options[distrito.selectedIndex].value, function handleReturn(data) {
		
		selectElementsRemoveAll("conservatoria");
		optionsLength = selectElementsFill("conservatoria", data);
		
		sel = document.getElementById('conservatoria');
		if(selectedConservatoria == null || selectedConservatoria == '') {
			var option = new Option('-- seleccionar o distrito --', '');
			sel.options.add(option);
			sel.selectedIndex = 0;
		} else {
			if(sel != null) {
		    	options = sel.options;
				for (var i = 0 ; i < optionsLength ; i++) {
					if (sel.options[i].value == selectedConservatoria) {
						sel.selectedIndex = i;
					}
				}
			}
		}
	});
}

function generateComboConcelho(changedCombo, idToGenerate) {

	function errh(msg) {
		
		alert(msg);
	}
	
	dwr.engine.setErrorHandler(errh);	

	if(changedCombo.name == 'distritocombo') {
		
		Remote.getConcelhosCombo(document.getElementById('distritocombo').options[document.getElementById('distritocombo').selectedIndex].value, 'concelhocombo', function handleReturn(data) {
			
			document.getElementById('concelho').innerHTML = data;
			
		});

		Remote.getFreguesiasCombo(document.getElementById('distritocombo').options[document.getElementById('distritocombo').selectedIndex].value, document.getElementById('concelhocombo').options[document.getElementById('concelhocombo').selectedIndex].value, 'freguesiacombo', function handleReturn(data) {
			
			document.getElementById('freguesia').innerHTML = data;
			
		});
		
	}

	if(changedCombo.name == 'concelhocombo') {
		
		Remote.getFreguesiasCombo(document.getElementById('distritocombo').options[document.getElementById('distritocombo').selectedIndex].value, document.getElementById('concelhocombo').options[document.getElementById('concelhocombo').selectedIndex].value, 'freguesiacombo', function handleReturn(data) {
			
			document.getElementById('freguesia').innerHTML = data;
			
		});
		
	}
}

function generateComboFreguesia(changedCombo, idToGenerate) {

	function errh(msg) {
		
		alert(msg);
	}
	
	dwr.engine.setErrorHandler(errh);	

	if(changedCombo.name == 'distritocombo') {
		
		Remote.getConcelhosCombo(document.getElementById('distritocombo').options[document.getElementById('distritocombo').selectedIndex].value, 'concelhocombo', function handleReturn(data) {
			
			document.getElementById('concelho').innerHTML = data;
			
		});

		Remote.getFreguesiasCombo(document.getElementById('distritocombo').options[document.getElementById('distritocombo').selectedIndex].value, document.getElementById('concelhocombo').options[document.getElementById('concelhocombo').selectedIndex].value, 'freguesiacombo', function handleReturn(data) {
			
			document.getElementById('freguesia').innerHTML = data;
			
		});
		
	}

	if(changedCombo.name == 'concelhocombo') {
		
		Remote.getFreguesiasCombo(document.getElementById('distritocombo').options[document.getElementById('distritocombo').selectedIndex].value, document.getElementById('concelhocombo').options[document.getElementById('concelhocombo').selectedIndex].value, 'freguesiacombo', function handleReturn(data) {
			
			document.getElementById('freguesia').innerHTML = data;
			
		});
		
	}
}

function initBody() {
	try
	{
		loadBody();
	}
	catch(err)
	{
	//Handle errors here
	}
}

function habilitaDeslocacaoConservador(){
	codAreaFuncional = "proc_casamento_civil_local_conservatoria_nome";
	data = "proc_casamento_data";
	hora = "proc_casamento_hora";
	minutos = "proc_casamento_min";
	retorno=false;
	codAreaFuncional = document.getElementById(codAreaFuncional).value;
	data = document.getElementById(data).value;
	hora = document.getElementById(hora).value;
	minutos = document.getElementById(minutos).value;
	tipoCasamento = document.getElementById('proc_casamento_tipo').options[document.getElementById('proc_casamento_tipo').selectedIndex].value;
	
//	alert('Opa');
//	alert('Codigo Area Funcional: '+codAreaFuncional);
//	alert('Data: '+data);
//	alert('Hora: '+hora);
//	alert('Minutos: '+minutos);
//	alert(retorno);
//	alert("ola "+retorno);
//	alert("&"+codAreaFuncional+"& data =&"+ data+"& | hora = &" +hora +"& | mimu = &"+ minutos+"&");
	if(codAreaFuncional != "" && data != "" && hora != "" &&  minutos != "" && tipoCasamento == '1'){
//		alert("entrou");
		Remote.isHorarioDeExpediente(codAreaFuncional, data, hora, minutos, function handleReturn(data){
			retorno = data;
			if(retorno == true){
				//jQComboComuteDesloc
				jQuery('.proc_casamento_civil_local_deslocacao_Box').show();
//				document.getElementById('deslocacao').className = 'FormBlock1 clearfix jQComboComuteDesloc';
//				document.getElementById('deslocacaoConservador').style.visibility = 'visible';
//				document.getElementById("deslocacaoConservador").style.display='block';
			}else{
				jQuery('.proc_casamento_civil_local_deslocacao_Box').hide();
//				document.getElementById('deslocacao').className = 'FormBlock1 clearfix proc_casamento_civil_local_deslocacao_Box DisplayOff';
			}
		});
	}
}

function selectElementsRemoveAll(selectId) {
	
	var selectBox = document.getElementById(selectId);
	
	for (var i = 0 ; i < selectBox.length ; i++) {
		selectBox.remove(i);
		i--;
	}
}	

function selectElementsFill(selectId, options) {
	
	var selectBox = document.getElementById(selectId);
	var optionsArray = options.split(':::::');
	
	for(var i = 0 ; i < optionsArray.length - 1 ; i++) {
		var optionValue = optionsArray[i].split(':::');
		var option = new Option(optionValue[1], optionValue[0]);
		selectBox.options.add(option);
	}
	
	return optionsArray.length - 1;
}