//Valida o formulario de cadastro de politicos
function validaForm(form) {
	nome = form.nome;
	email = form.email_profissional;
	estado = form.id_uf;
	cidade = form.id_cidade;
	if(email.value == '') {
		alert('Por favor, informe seu E-mail!');
		email.focus();
		return false;
	} else {
		var reg = /^[a-zA-Z0-9]([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if(!reg.test(email.value)) {
			alert("O E-mail informado é inválido!");
			email.focus();
			return false;
		}
	}
	if(nome.value == '') {
		alert('Preencha o campo nome!');
		nome.focus();
		return false;
	}
	if(form.pais.value == '') {
		alert('Escolha seu pais!');
		form.pais.focus();
		return false;
		
	}
	if(form.pais.value == 'Brasil') {
		if(estado.selectedIndex == 0) {
			alert('Selecione seu estado!');
			estado.focus();
			return false;
		}

		if(cidade.selectedIndex == 0) {
			alert('Selecione sua cidade!');
			cidade.focus();
			return false;
		}
	}
	return true;
}

//Chama o php que traz as cidades a partir de um estado
function getCidades(id_estado) {
	jsrsExecute('/resources/jsrs/cidades.php', jsrsreturn, 'getcidades', id_estado,0);
}

function changePais(id_pais) {
	if (id_pais != "Brasil"){
		document.getElementById('id_uf').value = 28;
		document.getElementById('id_uf').disabled = "disabled";
		
		var lkp = document.getElementById('id_cidade');
		lkp.options.length = 0;
		lkp.options[0] = new Option("Exterior","0");
		lkp.value = 0;
		lkp.disabled = "disabled";
	}
	else{
		document.getElementById('id_uf').disabled = "";
		document.getElementById('id_uf').value = 0;

		var lkp = document.getElementById('id_cidade');
		lkp.options.length = 0;
		lkp.options[0] = new Option("Selecione um estado primeiro","0");
		lkp.value = 0;
		lkp.disabled = "";
	}
}


//Trata o retorno do JSRS de cidades
function jsrsreturn(returnString) {
	var lkp = document.getElementById('id_cidade');
	lkp.options.length = 0;
	returnString = "~ Selecione sua cidade|" + returnString;
	arr = returnString.split('|');
	for(i = 0; i< arr.length; i++) {
		temp = arr[i].split('~');
		lkp.options[i] = new Option(temp[1], temp[0]);
	}
}

//Chama o php que traz os cargos a partir de um id_poder
function getCargos(id_poder) {
	jsrsExecute('/resources/jsrs/cargos.php', jsrsreturnCargos, 'getcargos', id_poder,0);
}

//Trata o retorno do JSRS de cargos
function jsrsreturnCargos(returnString) {
	var lkp = document.getElementById('id_cargo');
	lkp.options.length = 0;
	returnString = "~ Selecione seu cargo|" + returnString;
	arr = returnString.split('|');
	for(i = 0; i< arr.length; i++) {
		temp = arr[i].split('~');
		lkp.options[i] = new Option(temp[1], temp[0]);
	}
}

function mudouPais(pais) {
	if(pais.value  != 'Brasil') {
		pais.form.id_uf.selectedIndex = 28;
		pais.form.id_cidade.selectedIndex = pais.form.id_cidade.length-1;
	} else {
		pais.form.id_uf.selectedIndex = 0;
		pais.form.id_cidade.selectedIndex = 0;
	}
}

