
function ajax() {
	try {
		xmlhttp = new XMLHttpRequest();
	}
	catch ( ee ) {
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				xmlhttp = new ActiveXObject("Msxml.XMLHTTP");
			}
			catch (E){
				xmlhttp = false;
			}
    	}
	}
	return xmlhttp;
}


function carregaLista(dados_xml, tag_dados, id_select) {

	var dataArray = dados_xml.getElementsByTagName(tag_dados);

	if (dataArray.length > 0) {

		for (var i = 0 ; i < dataArray.length ; i++) {

			var item = dataArray[i];

			var codigo    = item.getElementsByTagName('codigo')[0].firstChild.nodeValue;
			var descricao = item.getElementsByTagName('descricao')[0].firstChild.nodeValue;

			var novo   = document.createElement('option');
			novo.value = codigo;
			novo.text  = descricao;
			document.getElementById(id_select).options.add(novo);
		}
	}
	else {
		document.getElementById(id_select).options[0].innerHTML = 'dados não disponíveis';
	}	  
}


function limpa_input(campo) {
	if (campo.defaultValue == campo.value) {
		campo.value = "";
	}
	else if (campo.value == "") {
		campo.value = campo.defaultValue;
	}
}



function validaNews(){

	d = document.frmNewsletter;

	//validar nome
	if ((d.txtNome.value == "") || (d.txtNome.value == d.txtNome.defaultValue)){
		alert("O campo Nome deve ser preenchido!");
		limpa_input(d.txtNome);
		d.txtNome.focus();
		return false;
	}

	//validar email
	if (d.txtEmail.value == d.txtEmail.defaultValue){
		alert("O campo E-mail deve ser preenchido!");
		limpa_input(d.txtEmail);
		d.txtEmail.focus();
		return false;
	}
	//validar email(verificao de endereco eletronico)
	parte1 = d.txtEmail.value.indexOf("@");
	parte2 = d.txtEmail.value.indexOf(".");
	parte3 = d.txtEmail.value.length;
//	if (!(parte1 >= 3 && parte2 >= 6 && parte3 >= 9)) {
	if (!(parte1 >= 3 && parte3 >= 9)) {
		alert("O campo E-mail deve conter um endereco eletronico!");
		d.txtEmail.value = "";
		d.txtEmail.focus();
		return false;
	}

	//validar UF
	if (d.comboUF.value == ""){
		alert("O campo UF deve ser preenchido!");
		d.comboUF.focus();
		return false;
	}

	//validar cidade
	if ((d.txtCidade.value == "") || (d.txtCidade.value == d.txtCidade.defaultValue)){
		alert("O campo Cidade deve ser preenchido!");
		limpa_input(d.txtCidade);
		if (document.getElementById('inputCidade').style.display != 'none') {
			d.txtCidade.focus();
		}
		else {
			d.cidade_id.focus();
		}
		return false;
	}


	ajax();
	xmlhttp.open('POST', 'send.php', true);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

	var txtNome   = d.txtNome.value;
	var txtEmail  = d.txtEmail.value;
	var txtCidade = d.txtCidade.value;
	var comboUF   = d.comboUF.value;
	var cidade_id = d.cidade_id.value;

	var params   = 'txtNome='+txtNome+'&txtEmail='+txtEmail+'&txtCidade='+txtCidade+'&comboUF='+comboUF+'&cidade_id='+cidade_id;
	xmlhttp.send(params);
	
	xmlhttp.onreadystatechange = function() {

		if (xmlhttp.readyState == 4 ) {
			alert('Seu cadastro foi realizado com sucesso!');
			d.reset();
			document.getElementById('inputCidade').style.display  = 'inline';
			document.getElementById('selectCidade').style.display = 'none';
		}
	}
}


function validaCidade(cidade) {
	
	if (cidade != '') {
		document.getElementById('txtCidade').value = cidade;
	}
}


function listaCidadesRS(uf) {
	
	if (uf == 'RS') {

		document.getElementById('inputCidade').style.display  = 'none';
		document.getElementById('selectCidade').style.display = 'inline';


		ajax();
		xmlhttp.open('POST', 'xml/cidades.php', true);
		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
		xmlhttp.onreadystatechange = function() {
	
			if (xmlhttp.readyState == 4 ) {
	
				if (xmlhttp.responseXML) {
	
					document.getElementById('cidade_id').options.length = 0;
					carregaLista(xmlhttp.responseXML, 'cidade', 'cidade_id');
				}
			}
		}
		var params = 'uf='+uf;
		xmlhttp.send(params);
	}
	else {
		document.getElementById('inputCidade').style.display  = 'inline';
		document.getElementById('selectCidade').style.display = 'none';
	}
}
