﻿function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}


	var http_request = false;

	
    function makeRequest(url,funcao,isAssincrono) {	
        http_request = false;
		if(!isAssincrono){
			isAssincrono = true;
		}
		http_request=getXmlHttp();

        if (!http_request) {			
            return false;
        }else{	
			
		}
		
		if(!funcao){
			funcao = defaultRequest;
		}		
        http_request.onreadystatechange = funcao;
        http_request.open('GET', url, isAssincrono);
        http_request.send(null);		
    }
	
 function getXmlHttp(){
	 if(window.XMLHttpRequest){
		 return new XMLHttpRequest();
	 } else if (window.ActiveXObject) {			 
		 var axO=['Microsoft.XMLHTTP','Msxml2.XMLHTTP','Msxml2.XMLHTTP.6.0','Msxml2.XMLHTTP.4.0','Msxml2.XMLHTTP.3.0'];
		 for(var i=0;i<axO.length;i++){ 
			try{ 
				return new ActiveXObject(axO[i]);
				}catch(e){
					
				} 
		}
	}
 }
 
 
 function geraQueryString(idForm){
	var frm = document.forms[idForm];
	var query = '';
	var cont = 0;
	for (var i = 0; i < frm.elements.length; i++) {
		if(frm.elements[i].type){
			if(frm.elements[i].type.indexOf('text') == 0){
				if (cont==0) {
					query = '?' + frm.elements[i].name + '=' + frm.elements[i].value;
				} else {
					query = query + '&' + frm.elements[i].name + '=' + frm.elements[i].value;
				}
				cont++;
			}else if(frm.elements[i].type.indexOf('checkbox')== 0){			
				if(frm.elements[i].checked){
					if (cont==0) {
						query = '?' + frm.elements[i].name + '=' + frm.elements[i].value;
					} else {
						query = query + '&' + frm.elements[i].name + '=' + frm.elements[i].value;
					}
					cont++;
				}			
			}else{
				if (cont==0) {
					query = '?' + frm.elements[i].name + '=' + frm.elements[i].value;
				} else {
					query = query + '&' + frm.elements[i].name + '=' + frm.elements[i].value;
				}
				cont++;		
			}
		}else{
			
		}
		
	}
	return (query);
}

function enviaForm(idForm){
	var url = geraQueryString(idForm).replace('?','&');
	func = function(){
		if (http_request.readyState == 4 && http_request.status == 200){ 
			var retorno = http_request.responseText;
			if(retorno == '1'){
				var objM = document.getElementById('mensagemForm');
				var objB = document.getElementById('blocoForm');
				var objMR = document.getElementById('mensagemRetorno');
				objM.style.display = 'none';
				objB.style.display = 'none';
				objMR.style.display = 'block';
			}else{
				alert('Erro ao enviar sua mensagem. Por favor tente mais tarde');
			}
		}
	}
	funcSucesso = function(urlInter){
		//makeRequest(urlInter+url,func);
		window.location = urlInter+url;
	}	
	funcErro = function(strErro){
		alert("Preencha corretamente o(s) campo(s):\n\n"+sapo_replace(',','\n',strErro));
	}
	enviaFormulario(idForm,funcSucesso,funcErro);
}



function enviaAvaliacao(idForm){
	var url = geraQueryString(idForm).replace('?','&');
	func = function(){
		if (http_request.readyState == 4 && http_request.status == 200){ 
			var retorno = http_request.responseText;
			if(retorno == '1'){
				alert("Avaliação enviada com sucesso.");
				limpaCampos(idForm);
				getNota(0);
			}else{
				alert('Erro ao enviar sua avaliação. Por favor tente mais tarde');
			}
		}
	}
	funcSucesso = function(urlInter){
		//alert(urlInter+url);
		//makeRequest(urlInter+url,func);
		document.location = urlInter+url;
	}	
	funcErro = function(strErro){
		alert("Preencha corretamente o(s) campo(s):\n\n"+sapo_replace(',','\n',strErro));
	}
	enviaFormulario(idForm,funcSucesso,funcErro);
}



function sapo_replace(strFind,newValue,str){
	while(str.indexOf(strFind)!=-1){
			str = str.replace(strFind,newValue); 
		} 
	return str;	
}

function enviaFormulario(idForm,funcSucesso,funcErro){	
	var frm = document.forms[idForm];
	var checagem = 1;
	var count = frm.elements.length;
	var str_campo = "";
	for(i=0; i<frm.elements.length; i++){
		if(msg[frm.elements[i].name]){
			if(vld[frm.elements[i].name] == 1){
				checagem = validaCampo(frm.elements[i]);
			}
			else if(vld[frm.elements[i].name] == 2){
				checagem = validaEmail(frm.elements[i]);
			}else if(vld[frm.elements[i].name] == 3){
				 checagem = validaData("d-m-y");
			}else if(vld[frm.elements[i].name] == 4){
				 checagem = validaData("d-m");
			}else if(vld[frm.elements[i].name] == 5){
				 checagem = validaData("m-y");
			}else if(vld[frm.elements[i].name] == 6){
				 checagem = validaData("y");
			}else if(vld[frm.elements[i].name] == 7){
				 checagem = validaDataFinal("d-m-y");
			}else if(vld[frm.elements[i].name] == 8){
				 checagem = validaDataFinal("d-m");
			}else if(vld[frm.elements[i].name] == 9){
				 checagem = validaDataFinal("m-y");
			}else if(vld[frm.elements[i].name] == 10){
				 checagem = validaDataFinal("y");
			}
			if(!checagem){
				str_campo += msg[frm.elements[i].name] + ",";
				count --;
			}
		}
	}
	if(count == frm.elements.length){
		if(funcSucesso){
			funcSucesso(destino_frm);
		}
	}else{	
		if(funcErro){
			funcErro(str_campo);
		}
		return false;
	}
}

function validaCampo(campo){
	return campo.value != "";
}

function validaEmail(campo){
	return (campo.value != "" && campo.value.indexOf("@") > 0 && campo.value.indexOf(".") > 0);
}

function apenasNum(campo){
	campo.value = campo.value.replace(/[^0-9]/g, '');
}

var idPagina;
function iniciaAvaliacao(){
	if(urlAvaliacao){
		//getIdPagina();
	}
	
}

function iniciaCampos(){
	var frm = document.formularioAvalie;
	var objlivro = frm.idLivro;
	
	var nova_opcao = document.createElement("option");
	var texto = document.createTextNode("Selecione"); 
	nova_opcao.setAttribute("value",''); 
	nova_opcao.appendChild(texto); 
	objlivro.appendChild(nova_opcao);
	var url = 'controle.php?acao=livro';
	var func = function(){
		if (http_request.readyState == 4 && http_request.status == 200){
			var retorno = http_request.responseXML;
			$(retorno).find('Livro').each(function(){
				var nome = $(this).find('nome').text();
				var id = $(this).find('id').text();
				var nova_opcao = document.createElement("option");
				var texto = document.createTextNode(nome); 
				nova_opcao.setAttribute("value",id); 
				nova_opcao.appendChild(texto); 
				objlivro.appendChild(nova_opcao);
			});
			
			getCategorias();
			return true;
		}
	}
	makeRequest(url,func);
}

function getCategorias(){	
	var frm = document.formularioAvalie;
	var objCategoria = frm.idCategoria;
	var url = 'controle.php?acao=categoria';
	var func = function(){
		if (http_request.readyState == 4 && http_request.status == 200){
			var retorno = http_request.responseXML;
			var nova_opcao = document.createElement("option");
			var texto = document.createTextNode("Selecione"); 
			nova_opcao.setAttribute("value",''); 
			nova_opcao.appendChild(texto); 
			objCategoria.appendChild(nova_opcao);
			$(retorno).find('Categoria').each(function(){
				var descricao = $(this).find('descricao').text();
				var id = $(this).find('id').text();
				var nova_opcao = document.createElement("option");
				var texto = document.createTextNode(descricao); 
				nova_opcao.setAttribute("value",id); 
				nova_opcao.appendChild(texto); 
				objCategoria.appendChild(nova_opcao);
			});	
			
			return true;
		}
	}
	makeRequest(url,func);
}


function getReceita(){
	var frm = document.formularioAvalie;
	var idCategoria = frm.idCategoria.value;
	var idLivro = frm.idLivro.value;
	var receita = frm.idReceita;
	display('boxAvalie','none');
	display('boxAvaliacoes','none');
	if(idCategoria.length > 0 && idLivro.length > 0){
		var textoCategoria = frm.idCategoria[frm.idCategoria.selectedIndex].text;
		var textoLivro = frm.idLivro[frm.idLivro.selectedIndex].text;
		$('#exibicaoAvalie').html(textoLivro+" | "+textoCategoria);
		$('#exibicaoAvalie2').html(textoLivro+" | "+textoCategoria);
		
		var url = 'controle.php?acao=receita&idCategoria='+idCategoria+'&idLivro='+idLivro;
		
		while(receita.length != 0){
			receita.remove(0);
		}
		var func = function(){
			if (http_request.readyState == 4 && http_request.status == 200){
				var retorno = http_request.responseXML;
				var nova_opcao = document.createElement("option");
				var texto = document.createTextNode("Selecione"); 
				nova_opcao.setAttribute("value",''); 
				nova_opcao.appendChild(texto); 
				receita.appendChild(nova_opcao);
				$(retorno).find('Receita').each(function(){
					var nome = $(this).find('nome').text();
					var id = $(this).find('id').text();
					var nova_opcao = document.createElement("option");
					var texto = document.createTextNode(nome); 
					nova_opcao.setAttribute("value",id); 
					nova_opcao.appendChild(texto); 
					receita.appendChild(nova_opcao);
				});					
				return true;
			}
		}
		makeRequest(url,func);
	}
}

function avaliarPagina(idObj){
	if(urlAvaliacao){
		var url = "gera_id_pagina.php?acao=avaliar&url="+urlAvaliacao;		
		func = function(){
			if (http_request.readyState == 4 && http_request.status == 200){ 
				var retorno = http_request.responseText;
				if(retorno){
					$('#'+idObj).attr('classe'+retorno);
					alert('Dados enviados com sucesso!'+retorno);
				}
			}
		}
	}
	makeRequest(url,func);
}

function display(idObj,valor){		
	obj = document.getElementById(idObj); 
	if(obj){
		if(valor){
			obj.style.display = valor;			
		}else{
			var dsp = obj;
		
			if(dsp.style.display == 'none' || dsp.style.display.length == 0){				
				dsp.style.display = 'block';
			}else{
				dsp.style.display = 'none';
			}
		}
	}
}


function displayAvaliar(){
	var frm = document.formularioAvalie;
	var idCategoria = frm.idCategoria.value;
	var idLivro = frm.idLivro.value;
	var idReceita = frm.idReceita.value;
	if(idReceita.length > 0 ){
		display('boxAvalie','block');
		display('boxAvaliacoes','none');
	}else{
		alert('Selecione uma Receita');
		display('boxAvalie','none');
		display('boxAvaliacoes','none');
	}	
}

function displayAvaliacoes(){
	var frm = document.formularioAvalie;
	var idCategoria = frm.idCategoria.value;
	var idLivro = frm.idLivro.value;
	var idReceita = frm.idReceita.value;
	if(idReceita.length > 0 ){
		/*display('boxAvaliacoes','block');
		display('boxAvalie','none');*/
		frm.action = "?acao=avaliacao&idReceita="+idReceita;
		frm.submit();
	}else{
		alert('Selecione uma Receita');
		display('boxAvalie','none');
		display('boxAvaliacoes','none');
	}
}


function desabilitaBoxAvalie(){
	var frm = document.formularioAvalie;	
	var textoReceita = frm.idReceita[frm.idReceita.selectedIndex].text;
	$('#nomeReceita').html(textoReceita);
	$('#nomeReceita2').html(textoReceita);
	display('boxAvalie','none');
	display('boxAvaliacoes','none');
	document.formularioAvaliacao.idReceita.value = frm.idReceita.value;
}

function validaMensagem(){
	var frm =  document.formularioAvaliacao;
	var mensagem = frm.mensagem;
	if(mensagem.value.length > 399){
		mensagem.value = mensagem.value.substring(0,399);
	}
}

var idultimoIndex = 0;
function getNota(indexNota){
	var frm =  document.formularioAvaliacao;
	frm.nota.value = indexNota;
	var isAnterior = false;
	if(idultimoIndex == (indexNota/2)-1){
		isAnterior = true;	
		frm.nota.value	= indexNota -2;	
	}
	idultimoIndex = (indexNota/2)-1;
	$('#boxAvalie SPAN A').each(function(index){		
		if(index <= idultimoIndex){
			$(this).attr('class','fixed');
		}else{
			$(this).attr('class','pont-laranja');
		}
		
		if(isAnterior){
			if(index == idultimoIndex){
				$(this).attr('class','pont-laranja');
				idultimoIndex = idultimoIndex-1;
			}
		}
	});
}
function limpaCampos(idForm,strCamposAux){
	var frm = document.forms[idForm];
	if(strCamposAux){
		var arr = strCamposAux.split(',');
		for(i = 0; i < arr.length; i++){
			for (var j = 0; j < frm.elements.length; j++) {
				if(arr[i] == frm.elements[j].id){
					frm.elements[j].value='';							
				}
			}
		}		
	}
	
	for (var i = 0; i < frm.elements.length; i++) {
		if(frm.elements[i].type){
			if(frm.elements[i].type.indexOf('text') == 0){
				frm.elements[i].value='';
			}else if(frm.elements[i].type.indexOf('checkbox')== 0){			
				frm.elements[i].checked = false;			
			}else if(frm.elements[i].type.indexOf('select')== 0){	
				var sele = frm.elements[i];	
				if(sele[0]){
					sele[0].selected = true;
				}
			}
		}		
	}	
}

$(document).ready(function() {
	$('.op5').hover(
	  function () {
        $('.cont-erratas').fadeIn('normal');
      }, 
      function () {
        $('.cont-erratas').fadeOut('normal');
      }	
	);
	$('.pav').hover(
	  function () {
        $('.cont-pav').fadeIn('normal');
      }, 
      function () {
        $('.cont-pav').fadeOut('normal');
      }	
	);
 });

