// JavaScript Document
function createSelect(obj,path,act,newSelect,selected){
	$newSelect = $('#'+newSelect);
	var txtBegin = $newSelect.children('option:eq(0)').text();
	$newSelect.html('<option value="">Carregando ...</option>');
	$.ajax({
		contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
		type: "GET",
		url: path,
		data: "acao="+act+"&value="+$(obj).val()+"&dummy="+new Date().getTime(),
		dataType: "json",
		success: function(ret){
			if (ret.length > 0){
				$newSelect.attr('disabled',false);
				$newSelect.html('<option value="">-- Selecione --</option>');
				
				var val = null;
				$(ret).each(function(i){
					if (selected == ret[i].value)
						$newSelect.append('<option value="'+ret[i].value+'" selected>'+ret[i].text+'</option>');
					else
						$newSelect.append('<option value="'+ret[i].value+'">'+ret[i].text+'</option>');
				});
			}else{
				$newSelect.html('<option value="">Nenhum registro encontrado</option>');
				setTimeout(function(){
					$newSelect.html('<option value="">'+txtBegin+'</option>');
					$newSelect.attr('disabled',true);
				},1000);
			}
		}
	});
}

function replaceAll(string,char,newchar) {
	while (string.indexOf(char) != -1){
   		string = string.replace(char,newchar);
 	}
	return string;
}

function tiraAcento(text) {
	text = text.replace(new RegExp('[áàãâ]','gi'), 'a');
	text = text.replace(new RegExp('[ÁÀÂÃ]','gi'), 'A');
	text = text.replace(new RegExp('[éèê]','gi'), 'e');
	text = text.replace(new RegExp('[ÉÈÊ]','gi'), 'E');
	text = text.replace(new RegExp('[íìî]','gi'), 'i');
	text = text.replace(new RegExp('[ÍÌÎ]','gi'), 'I');
	text = text.replace(new RegExp('[óòõô]','gi'), 'o');
	text = text.replace(new RegExp('[ÓÒÔÕ]','gi'), 'O');
	text = text.replace(new RegExp('[úùû]','gi'), 'u');
	text = text.replace(new RegExp('[ÚÙÛ]','gi'), 'U');
	text = text.replace(new RegExp('[ç]','gi'), 'c');
	text = text.replace(new RegExp('[Ç]','gi'), 'C');
	return text;
} 


// JavaScript Document

function addNews(){
	$obj = $('#retNews');
	$ipt = $('.newsletter input');
	$bt = $('.newsletter button');
	var email = $ipt.val();
	if (email != ''){
		
		var reEmail =/^[\w-]+(\.[\w-]+)*@(([\w-]{2,63}\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
		if(reEmail.test(email)){
			$ipt.attr('disabled',true);
			$bt.attr('disabled',true);
			$obj.html('Enviando ...');
			$.ajax({
				contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
				type: "POST",
				url: "/assincronous/tools.php",
				data: "acao=addNews&email="+email+"&dummy="+new Date().getTime(),
				success: function(ret){
					setTimeout(function(){
						if (ret == "1"){
							$obj.html('Cadastro efetuado!');
							setTimeout(function(){
								$obj.html('Obrigado por se cadastrar!');
								setTimeout(function(){
									$obj.html('');
									$ipt.attr('disabled',false);
									$bt.attr('disabled',false);
								},2000);
							},2000);
						}else{
							$obj.html('Erro ao cadastrar! Tente novamente');
							setTimeout(function(){
								$obj.html('');
								$ipt.attr('disabled',false);
								$bt.attr('disabled',false);
							},2000);
						}
					},1000);
				}
			});
		}else{
			$obj.html('E-mail inv&aacute;lido!');
		}
	}else{
		$obj.html('Informe o e-mail');
	}
}

