// JavaScript Document

//Tenta criar o objeto xmlHTTP
try{
    xmlhttp = new XMLHttpRequest();
}catch(ee){
    try{
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }catch(e){
        try{
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }catch(E){
            xmlhttp = false;
        }
    }
}

/*
if (xmlhttp.overrideMimeType) {
	xmlhttp.overrideMimeType('text/xml; charset=iso-8859-1');
} else alert('fdx');
 */

//Fila de conexões
var fila = [];
var ifila = 0;

//Carrega via XMLHTTP a url recebida e coloca seu valor
//no objeto com o id recebido
//function ajaxHTML(id, url){
function loadurl_ajax_simples(id, url){
	//Carregando...
	try{
		//document.getElementById(id).innerHTML = "a carregar";
		document.getElementById(id).innerHTML = "";
	} catch(ee1) {
		//alert('Elemento não existe - ' + id);
	}
	
	//Adiciona à fila
	fila[fila.length] = [id, url];
	//Se não há conexões pendentes, executa
	if((ifila + 1) == fila.length)
		ajaxRun();
}

//Executa a próxima conexão da fila
function ajaxRun(){
	//Abre a conexão
	xmlhttp.open("GET", fila[ifila][1], true);
	//xmlhttp.open("POST", fila[ifila][1], true);
	
	
	//Função para tratamento do retorno
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4){
			//Mostra o HTML recebido
			
			//retorno = unescape(xmlhttp.responseText.replace(/\+/g," "));
			retorno = unescape(xmlhttp.responseText.replace(/\+/g,"+"));
			
//xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1");
			
			
			document.getElementById(fila[ifila][0]).innerHTML = retorno;
			//Roda o próximo
			ifila++;
			if(ifila < fila.length)
				setTimeout("ajaxRun()", 20);
		}
	}
	//Executa
	xmlhttp.send(null);
}

function abre_seccao(id, dados) {
	if (dados != '')
		st_dados = '?' + dados;
	else st_dados = ''
	
	//alert('mask_' + id + '.php' + st_dados);
	
	loadurl_ajax_simples('principal', 'mask_' + id + '.php' + st_dados);
	
	for (i = 0 ; i < 20 ; i++) {
		if (document.getElementById('link_menu_' + i)) {
			document.getElementById('link_menu_' + i).style.color = "#FFFFFF";
			//alert('link_menu_' + i);
		}
		//else break;
	}
	if (document.getElementById('link_menu_' + id))
		document.getElementById('link_menu_' + id).style.color = "#999999";
	
	while (true) {
		rand_no = (Math.round((Math.random()*(num_rand_max - 1) )) + 1);
		//alert(rand_no);
		
		if (num_imagem != rand_no) {
			num_imagem = rand_no;
			break;
		}
	}
	
	//document.getElementById("img_topo").src = "files/pic_top_0" + num_imagem + ".jpg";
	document.getElementById("img_topo").src = image_arr[num_imagem];
}

function show_hide(nome_div, mode) {
//function show_hide(nome_div) {
	var div = document.getElementById(nome_div);
	//if (div.style.display != "block")
	if (mode)
		div.style.display = "block";
	else div.style.display = "none";
}

function show_hide_alimentos(nome_div) {
	var div = document.getElementById(nome_div);
	if (div.style.display != "block")
		div.style.display = "block";
	else div.style.display = "none";
}





function sendData () {
	if (document.getElementById('email')) {
		if (validateEmailv2(document.getElementById('email').value)) {
			var url = 'email_mailing.php';
			var pars = Form.serialize('form_email');
			
			var myAjax = new Ajax.Request( url, {method: 'get', parameters: pars, onLoading: showLoad, onComplete: showResponse} );
		}
		else {
			alert('Email inválido');
			document.getElementById('email').focus();
		}
	}
}				
function showLoad () {
	//$('load').style.display = 'block';
	$('status').innerHTML = "A executar...";
}
function showResponse (originalRequest) {
	var newData = originalRequest.responseText;
	//$('load').style.display = 'none';
	$('status').innerHTML = newData;
}

var id_envio_link = 0;

function sendLink(id) {
	id_envio_link = id;
	//alert('weeeeeee 0 ');
	if ((document.getElementById('ed_teu_email_' + id)) && (document.getElementById('ed_amigo_email_' + id))) {
		//alert('weeeeeee 1 ');
		var url = 'email_link.php';
		var pars = Form.serialize('form_email_' + id);
		//alert('weeeeeee 2 ');
		
		faz_enviar_link = true;
		
		if (validateEmailv2(document.getElementById('ed_teu_email_' + id).value)) {
			
		} else {
			faz_enviar_link = false;
			alert('Email inválido');
			document.getElementById('ed_teu_email_' + id).focus();
			return;
		}
		
		//alert('weeeeeee 3 ');
		
		if (validateEmailv2(document.getElementById('ed_amigo_email_' + id).value)) {
			
		}
		else {
			faz_enviar_link = false;
			alert('Email inválido');
			document.getElementById('ed_amigo_email_' + id).focus();
			return;
		}
		
		if (faz_enviar_link) {
			var myAjax = new Ajax.Request( url, {method: 'get', parameters: pars, onLoading: showLoad_link, onComplete: showResponse_link} );
		}
	}
}
function showLoad_link () {
	//$('load').style.display = 'block';
	$('status_' + id_envio_link).innerHTML = "A executar...";
}
function showResponse_link (originalRequest) {
	var newData = originalRequest.responseText;
	//$('load').style.display = 'none';
	$('status_' + id_envio_link).innerHTML = newData;
}














