// JavaScript Document

function isEmail( string ) {
	if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) {
		return true;
	}
	else {
		return false;
	}
}


function validate_indique() {
	var pass = true;
	var error = '';
	
	if ($('#fromNome').val() == '') {
		pass = false;
		error = error + "- Nome do remetente é obrigatorio!\n";
	}
	
	if ($('#fromEmail').val() == '' || !isEmail( $('#fromEmail').val() )) {
		pass = false;
		error = error + "- Email do remetente é obrigatorio e deve conter um email valido!\n";
	}
	
	if ($('#toNome').val() == '') {
		pass = false;
		error = error + "- Nome do destinatario é obrigatorio!\n";
	}
	
	if ($('#toEmail').val() == '' || !isEmail( $('#toEmail').val() )) {
		pass = false;
		error = error + "- Email do destinatario é obrigatorio!\n";
	}
	
	if (!pass) {
		alert("Verifique os seguintes erros: \n" + error);
	}
	
	return pass;
	
}

$(document).ready(function(){
	
	$('#palavra_chave').focus(function(){
		$(this).removeAttr('value');
	});
	
	$("#estado").change(function(){
		if ($("#estado option:selected").val() != "") {
			$.ajax({
				url: 'ajax/empreendimentos.php',
				type: 'post',
				data: {
					acao: 'estados',
					estado: $("#estado option:selected").val()
				},
				success: function (xml) {
					if ($(xml).find('cidade').size() > 0) {
						$('#cidades').empty();
						$(xml).find('cidade').each(function(){
							var html = '<option value="' + $(this).attr('id') + '">' + $(this).attr('nome') + '</option>';
							$(html).appendTo('#cidades');
						});
						$('#cidades').prepend('<option>Cidades</option>');
						$('#cidades option:first').attr('selected', 'selected');
						$('#cidades').show();
					}
				}
			});
		}
		else {
			$('#cidades').empty();
			$('#cidades').hide();
		}
	});
	
	$('#indique').bind('submit', validate_indique);

});