// JavaScript Document
function validaForm(f){
  var msgP = "llene correctamente lo(s) campo(s):\n\n";
  var msg1 = "";
  var msg2 = "";
  var msg3 = "";
  
  if(f==1){
    if(document.frmBusca.busca.value==""){
	   alert('Llene el campo de busca.');
	   document.frmBusca.busca.focus();
	   return false;
	}
	document.frmBusca.submit();
	limpaCampos(1);	
  }
  else if (f==2){
	msg1 = "";
    msg2 = "";   
	if(document.frmCadastro.nomeCad.value==""){
	   msg1 += " - Nome";
	   document.frmCadastro.nomeCad.focus();
	}
	if(document.frmCadastro.emailCad.value==""){
	   if(msg1!=""){
	      msg2 += "\n - E-mail";
	   }else{
	      msg2 += " - E-mail";
		  document.frmCadastro.emailCad.focus();
	   }
	}
	
	if (checkEmail2(document.frmCadastro.emailCad.value) == false){
		alert("Por favor llene el campo “E-mail” correctamente");
		return false;
		}
	if(msg1!="" || msg2!=""){
	   alert(msgP + msg1 + msg2);
	   return false;
	}else{
		document.frmCadastro.submit();
		limpaCampos(2);
	}
  }
  else if (f==3){
	msg1 = "";
    msg2 = "";  
	if(document.frmContato.nomeCont.value==""){
	   msg1 += " - Nome";
	   document.frmContato.nomeCont.focus();
	}
	if(document.frmContato.emailCont.value==""){
	   if(msg1!=""){
	      msg2 += "\n - E-mail";
	   }else{
	      msg2 += " - E-mail";
		  document.frmContato.emailCont.focus();
	   }
	}

	if (checkEmail2(document.frmContato.emailCont.value) == false){
		alert("Por favor llene el campo “E-mail” correctamente");
		return false;
	}

	
	if(document.frmContato.msg.value==""){
	   if(msg1!="" || msg2!=""){
	      msg3 += "\n - Mensagem";
	   }else{
	      msg3 += " - Mensagem";
		  document.frmContato.msg.focus();
	   }
	}
	if(msg1!="" || msg2!="" || msg3!=""){
	   alert(msgP + msg1 + msg2 + msg3);
	   return false;
	}else{
		document.frmContato.submit();
		limpaCampos(3);
	}	
  }
}
function limpaCampos(f){
  if(f==1){
    document.frmBusca.busca.value = "";
  }
  else if (f==2){
    document.frmCadastro.nomeCad.value = "";
	document.frmCadastro.emailCad.value = "";
  }
  else if (f==3){
    document.frmContato.nomeCont.value = "";
	document.frmContato.emailCont.value = "";
	document.frmContato.msg.value = "";	
  }
}
function alertaUser(f){
  if (f==2){
	alert('Suscripción realizada con éxito');
  }
  else if (f==3){
    alert('Mensaje enviado con éxito.');
  }
}

// Função de cadastro dos comentários

function cadcoment(id){
	
  if(document.getElementById('nome'+id).value == ""){
    alert("Por favor llene el campo Nombre.");
    document.getElementById('nome'+id).focus();
  }else if(document.getElementById('email'+id).value == ""){
    alert("Por favor llene el campo E-mail");
    document.getElementById('email'+id).focus();
  }else if(checkEmail(id) == false){
    alert("Por favor llene el campo “E-mail” correctamente"); 	  
  }else if(document.getElementById('comentario'+id).value == ""){
    alert("Favor preencher o campo Comentario");
    document.getElementById('comentario'+id).focus();
  }else if(document.getElementById('mailing1'+id).checked == false && document.getElementById('mailing2'+id).checked == false){
    alert(" Por favor elegir la opción de Mailing."); 
  }else{
	var url = "lib/ajax/inserir_coments.php";
    var pars = "nome=" + document.getElementById('nome'+id).value;
	pars = pars + "&email=" + document.getElementById('email'+id).value;
	pars = pars + "&comentario=" + document.getElementById('comentario'+id).value;
	if(document.getElementById('mailing1'+id).checked == true){
		pars = pars + "&mailing=" + document.getElementById('mailing1'+id).value;
	}else{
		pars = pars + "&mailing=" + document.getElementById('mailing2'+id).value;		
	}
	pars = pars + "&idpost="+ id;
	$(id).innerHTML = "<BR><BR>Inscribiendo comentario...";	
    //var meuAjax = new Ajax.Updater(DIV, url, {method: 'post', parameters: pars});
	var myAjax = new Ajax.Request( url, {method: 'post', parameters: pars, onComplete: LoadComents} );	
  }
}


//Função que atualiza div dos comentários
function LoadComents(or1){
	temp1 = or1.responseText;
	ArrFinal = temp1.split('-');
	var tmp1 = trim(ArrFinal[0]);
	var id = ArrFinal[1];
	var DIV = ArrFinal[2];
	if(tmp1 == "ok"){
		var url = "lib/ajax/load_coments.php";
		var pars = "idpost=" + id;
		$(id).innerHTML = "<center><span class=\"titulosComent\"><b><BR><BR>Cargando comentario...</b></span></center>";	
		document.getElementById('spanComents'+DIV).innerHTML = parseInt(document.getElementById('spanComents'+DIV).innerHTML) + 1;
		var meuAjax = new Ajax.Updater(id, url, {method: 'post', parameters: pars});
 	}
}
// Cadastro de Comentários



// Exibe Div comentários
function showHide (ID) {
if (document.getElementById(ID).style.display == "none") {
   document.getElementById(ID).style.display= "";
}
else {
   document.getElementById(ID).style.display = "none";
}
}

function checkEmail(id) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.getElementById('email'+id).value)){
	return (true)
	}
//	alert("Invalid E-mail Address! Please re-enter.")
	return (false)
}

function checkEmail2(id) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(id)){
	return (true)
	}
//	alert("Invalid E-mail Address! Please re-enter.")
	return (false)
}


function trim(s) {
   var temp = s;
   return temp.replace(/^\s+/,'').replace(/\s+$/,'');
}