//funciones ajax
var pathtotal="http://www.generaccion.com/";

function getXmlHttpObject(){
    var xmlhttp;
    /*@cc_on
    @if (@_jscript_version >= 5)
    try{
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e){
    try{
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e){
    xmlhttp = false;
    }
    }
    @else
    xmlhttp = false;
    @end @*/
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined'){
        try{
            xmlhttp = new XMLHttpRequest();
        }
        catch (e){
            xmlhttp = false;
        }
    }
    return xmlhttp;
}

function cargaPagina(_pagina,valorget,valorpost,capa){ 
    var http = getXmlHttpObject();
    if(valorpost!=""){
        http.open("POST", _pagina+"?"+valorget+"&tiempo="+new Date().getTime(),true);
    } else {
        http.open("GET", _pagina+"?"+valorget+"&tiempo="+new Date().getTime(),true);
    }
    http.onreadystatechange=function(){
        if (http.readyState==1){
            document.getElementById(capa).innerHTML = "<img src='" + pathtotal + "img/preload.gif' align='center'> Aguarde por favor...";
        }
        if (http.readyState==4){
            if(http.status==200)
            {document.getElementById(capa).innerHTML = http.responseText;}
            else if(http.status==404)
            {
                capa.innerHTML = "La direccion no existe";
            }
            else
            {
                capa.innerHTML = "Error: ".http.status;
            }
        }
    }
    if(valorpost!=""){
        http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        http.send(valorpost);
    } else {
        http.send(null);
    }
}

function loguear(){
	var usr=document.getElementById("txt_usr").value;
	var pwd=document.getElementById("txt_pwd").value;
	if(usr==""){alert("Ingrese el mail correspondiente"); return;}
	if(pwd==""){alert("Ingrese la contrasena correspondiente"); return;}
	enviaLogueo(usr, pwd);
}

function enviaLogueo(usr, pwd){
	var httpLog = getXmlHttpObject();
	httpLog.open("POST", pathtotal+"includes/validar.php", true);
	httpLog.onreadystatechange = function(){
	  if(httpLog.readyState==1){
      	document.getElementById("Mensaje").innerHTML = "<img src='" + pathtotal + "img/preload.gif' align='center'> Aguarde por favor...";}
	  if(httpLog.readyState==4){
	  	if(httpLog.responseText=="0"){
	  	  document.location.href=pathtotal+"publica/?usr="+usr; return;
	  	}else{document.getElementById("Mensaje").innerHTML = "El mail o la contrase&ntilde;a no coinciden";}
	  }
	}
	httpLog.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	httpLog.send("usr="+usr+"&pwd="+pwd);
}

function validaContacto(){
	var nom = document.getElementById("txt_nom");
	var mai = document.getElementById("txt_mai");
	var msg = document.getElementById("txt_msg");
	if(nom.value==""){alert("Por favor, ingrese su nombre"); nom.focus(); return;}
	if(mai.value==""){alert("Por favor, ingrese su correo electronico"); mai.focus(); return;}
	if(!isEmailAddress(mai)){return;}
	if(msg.value==""){alert("Por favor, sus comentarios son importantes"); msg.focus(); return;}
	var tel = document.getElementById("txt_tel").value;
	enviaMailContacto(nom.value, mai.value, tel, msg.value)
}

function enviaMailContacto(nom, mai, tel, msg){
	var httpContact = getXmlHttpObject();
	httpContact.open("POST", pathtotal+'includes/enviomail_contacto.php', true);
	httpContact.onreadystatechange = function(){
	  if(httpContact.readyState==1){
      	document.getElementById("FormContacFormFrm").innerHTML = "<img src='" + pathtotal + "imagenes/ajax-loader.gif' align='center'> Enviando mail, Aguarde por favor...";}
	  if(httpContact.readyState==4){
		document.getElementById("FormContacFormFrm").innerHTML = httpContact.responseText;
		alert("Su mensaje de correo ha sido enviado. Gracias por usar este servicio.");
	  }
	}
	httpContact.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	httpContact.send("nom="+nom+"&mai="+mai+"&tel="+tel+"&msg="+msg);
}

function ValidArtUrs(){
	if(document.getElementById("cbo_seccion").value==""){
		alert("Seleccione una seccion."); document.getElementById("cbo_seccion").focus(); return;
	}
	if(document.getElementById("txt_tit").value == ""){
		alert("Ingrese el titulo."); document.getElementById("txt_tit").focus(); return;
	}
	document.getElementById("frm_artusr").submit();
}

function ValidaMailUsr(tpo){ //tpo si es de un articulo de usuario o revista o noticia
	var nom=document.getElementById("txt_nomenvia");
	var nommgo=document.getElementById("txt_nomamigo");
	var maimgo=document.getElementById("txt_maiamigo");
	if(!isEmailAddress(maimgo)){return;}
	var id=document.getElementById("idart");
	enviaMail(nom.value, nommgo.value, maimgo.value, id.value, tpo);
}

function enviaMail(nom, nommgo, maimgo, id, tpo){
	var httpMail = getXmlHttpObject();
	var ruta="";
	if(tpo=='usuario'){
		ruta='http://www.generaccion.com/includes/enviomail_usr.php';
	}else if(tpo=='revista'){
		ruta='http://www.generaccion.com/includes/enviomail.php';
	}else if(tpo=='noticia'){
		ruta='http://www.generaccion.com/includes/enviomail_not.php';
	}
	httpMail.open("POST", ruta, true);
	httpMail.onreadystatechange = function(){
	  if(httpMail.readyState==1){
      	document.getElementById("FormRecom").innerHTML = "<img src='" + pathtotal + "imagenes/ajax-loader.gif' align='center'> Enviando mail, Aguarde por favor...";}
	  if(httpMail.readyState==4){
		if(httpMail.status==200){
			document.getElementById("FormRecom").innerHTML = httpMail.responseText;
			alert("Su mensaje de correo ha sido enviado. Gracias por usar este servicio.");
		}else if(httpMail.status==404){
			enviaMail(nom, nommgo, maimgo, id, tpo);
		}else{
			enviaMail(nom, nommgo, maimgo, id, tpo);
		}
		
	  }
	}
	httpMail.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	httpMail.send("nom="+nom+"&nommgo="+nommgo+"&maimgo="+maimgo+"&id="+id);
}

function ValidaCmtRvt(tpo){
	var nom = document.getElementById("txt_nom");
	var mai = document.getElementById("txt_mai");
	var com = document.getElementById("txt_com");
	if(nom.value==""){alert("Por favor, ingrese su nombre"); nom.focus(); return;}
	if(mai.value==""){alert("Por favor, ingrese su correo electronico"); mai.focus(); return;}
	if(!isEmailAddress(mai)){return;}
	if(com.value==""){alert("Su comentario es muy importante"); com.focus(); return;}
	var id=document.getElementById("id").value;
	if(tpo=='usuario'){
		ruta='includes/gcomentarios_usr.php';
	}else if(tpo=='revista'){
		ruta='includes/gcomentarios.php';
	}else if(tpo=='noticia'){
		ruta='includes/gcomentarios_not.php';
	}
	var gasttip='';
	try{gasttip=document.getElementById("gasttip").value;}catch(e){gasttip='';}finally{}
	
	//alert("nom="+nom.value+"&mai="+mai.value+"&com="+com.value+"&id="+id+"&gasttip="+gasttip);
	cargaPagina(pathtotal+ruta, "", "nom="+nom.value+"&mai="+mai.value+"&com="+com.value+"&id="+id+"&gasttip="+gasttip, "dondeCargaAjax");
	alert("Tus comentarios han sido recibidos. Gracias por participar en Generaccion");
	nom.value=""; mai.value=""; com.value=""; nom.focus();
}

function isVideo(){
	var cbo=document.getElementById("cbo_imgvid").value;
	if(cbo=="img"){
		document.getElementById("imgVidcarga").innerHTML='<div>Imagen:</div><div id="Ctrls"><input type="file" name="img_imagen" id="img_imagen" class="txt_controles" size="40" /><br />Recomendado (ancho: 300px;alto: 200px).</div>';
	}else{
		document.getElementById("imgVidcarga").innerHTML='<div>URL Video Youtube:</div><div id="Ctrls"><input type="text" name="txt_video" id="txt_video" class="txt_controles" size="40" /><br />Ejemplo: <b>http://www.youtube.com/watch?v=EXQufqst7lC</b></div>';
	}
}

function ValidaRegistro(){
	var nom=document.getElementById("txt_nom");
	var dia=document.getElementById("cbo_dia"); var mes=document.getElementById("cbo_mes");
	var anio=document.getElementById("cbo_anio");
	var mai=document.getElementById("txt_mai"); if(!isEmailAddress(mai)){return;}
	var pwd=document.getElementById("txt_pwd");
	if(nom.value==""){alert("Por favor, ingrese su nombre"); nom.focus(); return;}
	if(dia.value=="0"){alert("Por favor, ingrese el dia de su nacimiento"); dia.focus(); return;}
	if(mes.value=="0"){alert("Por favor, ingrese el mes de su nacimiento"); mes.focus(); return;}
	if(anio.value=="0"){alert("Por favor, ingrese el aņo de su nacimiento"); anio.focus(); return;}
	if(mai.value==""){alert("Por favor, ingrese el mail de registro"); mai.focus(); return;}
	if(pwd.value==""){alert("Por favor, ingrese la contraseņa de registro"); pwd.focus(); return;}
	
	var ape=document.getElementById("txt_ape").value;
	var sex=document.getElementById("opt_sex").value;
	var pai=document.getElementById("cbo_pais").value;
	var ciu="";
	if(pai=="170"){ciu=document.getElementById("cbo_dpto").value;}else{ciu=document.getElementById("txt_ciudad").value;}
	var tel=document.getElementById("txt_tel").value;
	
	var cad="nom="+nom.value+"&ape="+ape+"&sex="+sex+"&dia="+dia.value+"&mes="+mes.value+"&anio="+anio.value+"&pai="+pai+"&ciu="+ciu+"&tel="+tel+"&mai="+mai.value+"&pwd="+pwd.value;
	cargaPagina(pathtotal+"includes/gregistro.php", "", cad, "FormForm");
}

function isEmailAddress(elElemento){
	var s = elElemento.value;
	var filter=/^[A-Za-z][A-Za-z0-9_\-.]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;
	if (s.length == 0 ) return true;
	if (filter.test(s))
	return true;
	else
	alert("Ingrese una direccion de correo valida");
	elElemento.focus();
	return false;
}

function getWordsFromInput(e, id){
	idInputSmartSearch = id;
	var keyCode;
	if (window.event) keyCode = window.event.keyCode;
	else if(e) keyCode = e.which;
	if(!keyCode) return;
	if(keyCode==defKEYESC){
		setFocusInput(); return;
	}
	if(keyCode!=defKEYENTER && keyCode!=defKEYBACKSPACE){
		clearTimeout(intTimeOutGetDataId);
		intTimeOutGetDataId = setTimeout("getListSmartSearch("+keyCode+")",intTimeOutGetData);
		return;
	}else{
		setValueSmartSearch(); 
		return;
	}
}

function setIndexSmartSearch(e, id){
	idInputSmartSearch = id;
	if (window.event) keyCode = window.event.keyCode;
	else if (e) keyCode = e.which;
	if (keyCode==defKEYDOWN || keyCode==defKEYUP){
		setFocusDivSmartSearchByKey(keyCode); return;
	}
	if(keyCode==defKEYBACKSPACE){
		clearTimeout(intTimeOutGetDataId);
		intTimeOutGetDataId = setTimeout("getListSmartSearch("+keyCode+")",intTimeOutGetData); return;
	}
	if (keyCode==defKEYTAB){
		setValueSmartSearch(); return;
	}
	if (keyCode==defKEYESC){
		setFocusInput(); return;
	}
}

function showNewPage(estado){
	if(estado=='2'){
		cargaPagina(pathtotal+"imagenes/newpropuesta/banner_2.php", "", "", "NewImagen");
		document.getElementById("NewPrimero").innerHTML="<a href='javascript:showNewPage(\"1\");'><img src='"+pathtotal+"imagenes/newpropuesta/banner_1.jpg' border='0' /></a>";
		document.getElementById("NewEstado").innerHTML="<a href='javascript:showNewPage(\"1\");'><img src='http://www.generaccion.com/imagenes/newpropuesta/up.jpg' border='0' /></a>";
	}else if(estado=='1'){
		document.getElementById("NewImagen").innerHTML="";
		document.getElementById("NewPrimero").innerHTML="<a href='javascript:showNewPage(\"2\");'><img src='"+pathtotal+"imagenes/newpropuesta/banner_1.jpg' border='0' /></a>";
		document.getElementById("NewEstado").innerHTML="<a href='javascript:showNewPage(\"2\");'><img src='http://www.generaccion.com/imagenes/newpropuesta/down.jpg' border='0' /></a>";	
	}
}

///////////////////////////////////////

function validaCom(usuario, identif, tpo){
	var nom=document.getElementById("txt_nom");
	var mai=document.getElementById("txt_mai");
	var com=document.getElementById("txt_Com");
	if(usuario==""){
		if(nom.value==""){
			document.getElementById("msgalgo").innerHTML="<div id='msgerror'>Ud. no est&aacute; logueado, debe ingresar su nombre</div>"; nom.focus();
			return false;
		}
		if(mai.value==""){
			document.getElementById("msgalgo").innerHTML="<div id='msgerror'>Ud. no est&aacute; logueado, debe ingresar su mail</div>"; mai.focus();
			return false;
		}
		if(!isEmailAddress(mai)){return false;}
	}
	if(com.value==""){
		document.getElementById("msgalgo").innerHTML="<div id='msgerror'>Ingrese un comentario</div>"; com.focus(); return false;
	}
	
	if(tpo=="articulo"){
		cargaPagina(pathtotal+"secciones/gastronomia/articulos/guardacomentario.php", "usr="+usuario+"&id="+identif+"&nom="+nom.value+"&mai="+mai.value+"&com="+com.value, "", "dondecargacom");
	}else if(tpo=="video"){
		cargaPagina(pathtotal+"secciones/gastronomia/media/guardacomentariovid.php", "usr="+usuario+"&id="+identif+"&nom="+nom.value+"&mai="+mai.value+"&com="+com.value, "", "dondecargacom");
	}else if(tpo=="imagen"){
		cargaPagina(pathtotal+"secciones/gastronomia/media/guardacomentarioimg.php", "usr="+usuario+"&id="+identif+"&nom="+nom.value+"&mai="+mai.value+"&com="+com.value, "", "dondecargacom");
	}
	nom.value=""; mai.value=""; com.value=""; nom.focus();
	document.getElementById("msgalgo").innerHTML="<div id='msgbien'>Gracias por participar con su comentario";
}

function validaEnviaMail(tpo,identif){ //tpo si es de un articulo de usuario o revista o noticia
	var nom=document.getElementById("txtnom");
	var nommgo=document.getElementById("txtnommgo");
	var maimgo=document.getElementById("txtmaimgo");
	if(nom.value=="" || nom.value=="Su nombre"){alert("Ingrese su nombre"); nom.focus(); return;}
	if(nommgo.value=="" || nommgo.value=="Nombre del amigo"){alert("Ingrese el nombre de su amigo"); nommgo.focus(); return;}
	if(maimgo.value=="" || maimgo.value=="Mail del amigo"){alert("Ingrese el mail de su amigo"); maimgo.focus(); return;}
	if(!isEmailAddress(maimgo)){return;}
	enviaMail(nom.value, nommgo.value, maimgo.value, identif, tpo);
}

function enviaMail(nom, nommgo, maimgo, identif, tpo){
	var httpMail = getXmlHttpObject();
	var ruta="";
	if(tpo=='articulo'){
		ruta=pathtotal+'magazine/includes/enviomail_arti.php';
	}else if(tpo=='infografia'){
		ruta=pathtotal+'magazine/includes/enviomail_info.php';
	}
	httpMail.open("POST", ruta, true);
	httpMail.onreadystatechange = function(){
	  if(httpMail.readyState==1){
      	document.getElementById("frmfrm").innerHTML = "<img src='" + pathtotal + "img/preload.gif' align='center'> Enviando mail, Aguarde por favor...";}
	  if(httpMail.readyState==4){
		if(httpMail.status==200){
			document.getElementById("frmfrm").innerHTML = httpMail.responseText;
			alert("Su mensaje de correo ha sido enviado. Gracias por usar este servicio.");
		}else if(httpMail.status==404){
			enviaMail(nom, nommgo, maimgo, identif, tpo);
		}else{
			enviaMail(nom, nommgo, maimgo, identif, tpo);
		}
		
	  }
	}
	httpMail.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	httpMail.send("nom="+nom+"&nommgo="+nommgo+"&maimgo="+maimgo+"&identif="+identif+"&tpo="+tpo);
}

//capturar el valor del radio button
function getRadioButtonSelectedValue(ctrl){
    for(i=0;i<ctrl.length;i++){
        if(ctrl[i].checked) return ctrl[i].value;
	}
}

function votar(valor, capa, id){
	if(valor=="v"){
	  cargaPagina('http://www.generaccion.com/secciones/gastronomia/includes/mostrarencuesta.php','id='+id,'',capa);
	}else{
	  cargaPagina('http://www.generaccion.com/secciones/gastronomia/includes/votar.php','val='+valor+'&id='+id,'',capa);
	}
	
}

//cambiar el tamano del texto de un elemento
function zoomText(Accion,Elemento){
	var obj=document.getElementById(Elemento);
	var max = 200;
	var min = 70;
	if(obj.style.fontSize==""){
		obj.style.fontSize="100%";
	}
	actual=parseInt(obj.style.fontSize);
	incremento=10;

	if(Accion=="reestablecer"){
		obj.style.fontSize="100%";
	}
	if(Accion=="aumentar" && ((actual+incremento) <= max )){
		valor=actual+incremento;
		obj.style.fontSize=valor+"%";
	}
	if(Accion=="disminuir" && ((actual+incremento) >= min )){
		valor=actual-incremento;
		obj.style.fontSize=valor+"%";
	}
}

//imprimir una capa
function imprSelec(nombre){
  var ficha = document.getElementById(nombre);
  var ventimp = window.open('', 'popimpr');
  ventimp.document.write(ficha.innerHTML);
  ventimp.document.close();
  ventimp.print();
  ventimp.close();
}

function imprimir(capa, id){
  var bName = navigator.appName;
  var bVer = parseFloat(navigator.appVersion);
  var contenido = document.getElementById(capa).innerHTML;
  ventana=window.open("print.php?id="+id,"ventana","width=560,scrollbars=1");
  ventana.document.open();
  ventana.document.write(contenido);
  ventana.document.close();
  ventana.print();
  ventana.focus();
}

function redireccionar(donde){
	location.href=donde;
}

//////////////////////////////////////////////////////////////////
function agregar_comentario(_pagina,valorget,valorpost,capa,id_r,tip){ 
    var http = getXmlHttpObject();
    if(valorpost!=""){
        http.open("POST", _pagina+"?"+valorget+"&tiempo="+new Date().getTime(),true);
    } else {
        http.open("GET", _pagina+"?"+valorget+"&tiempo="+new Date().getTime(),true);
    }
    http.onreadystatechange=function(){
        if (http.readyState==1){
            document.getElementById(capa).innerHTML = "<img src='" + pathtotal + "img/preload.gif' align='center'> Aguarde por favor...";
        }
        if (http.readyState==4){
            if(http.status==200){
				document.getElementById(capa).innerHTML = '<div class="uncaption"><div class="caption">Nombres:</div><div><input type="text" name="txtnom" id="txtnom" class="txt" /></div></div><div class="uncaption"><div class="caption">Mail:</div><div><input type="text" name="txtmai" id="txtmai" class="txt" /></div></div><div class="flotante"></div><div class="caption">Comentario:</div><div><textarea name="txtcom" id="txtcom" class="txtarea" /></textarea></div><div id="btncaption"><input type="button" value=" " class="btn" onclick="agregar_comentario_receta(\''+ id_r +'\',\''+ tip +'\')" /></div>';
				if(tip=="receta"){
					cargaPagina('http://www.generaccion.com/secciones/gastronomia/includes/comentarios_receta.php','id='+id_r,'','mostrarcom');
				}else if(tip=="articulo"){
					cargaPagina('http://www.generaccion.com/secciones/gastronomia/includes/comentarios_articulo.php','id='+id_r,'','mostrarcom');
				}else if(tip=="video"){
					cargaPagina('http://www.generaccion.com/secciones/gastronomia/includes/comentarios_video.php','id='+id_r,'','mostrarcom');
				}else if(tip=="galeria"){
					cargaPagina('http://www.generaccion.com/secciones/gastronomia/includes/comentarios_galeria.php','id='+id_r,'','mostrarcom');
				}
				
			}
            else if(http.status==404)
            {
                capa.innerHTML = "La direccion no existe";
            }
            else
            {
                capa.innerHTML = "Error: ".http.status;
            }
        }
    }
    if(valorpost!=""){
        http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        http.send(valorpost);
    } else {
        http.send(null);
    }
}

function agregar_comentario_receta(id_r,tip){
	var nom = document.getElementById('txtnom');
	var mai = document.getElementById('txtmai');
	var com = document.getElementById('txtcom');
	
	if(nom.value == ''){alert('Tienes que ingresar tu Nombre'); nom.focus(); return false;}
	if(mai.value == ''){alert('Tienes que ingresar tu Correo'); mai.focus(); return false;}
	if(com.value == ''){alert('Tienes que ingresar tu Comentario'); com.focus(); return false;}
	
	var comen = com.value.replace('&','$amp;');
	
	var lapagina="";
	if(tip=="receta"){
		lapagina='http://www.generaccion.com/secciones/gastronomia/includes/agregar_comentarios_receta.php';
	}else if(tip=="articulo"){
		lapagina='http://www.generaccion.com/secciones/gastronomia/includes/agregar_comentarios_articulo.php';
	}else if(tip=="video"){
		lapagina='http://www.generaccion.com/secciones/gastronomia/includes/agregar_comentarios_video.php';
	}else if(tip=="galeria"){
		lapagina='http://www.generaccion.com/secciones/gastronomia/includes/agregar_comentarios_galeria.php';
	}
	agregar_comentario(lapagina,'nom='+nom.value+'&mai='+mai.value+'&com='+comen+'&id='+id_r,'','enviarcomMed',id_r,tip);
}

function isBusqueda(){
	var opt=document.getElementById("cbo_por").value;
	if(opt=="1"){
		document.getElementById("medsupesprig").innerHTML="<input type='text' id='txt_nom' name='txt_nom' style='font:11px verdana;padding:2px;width:200px;' /><input type='hidden' id='hdn_forsearch' name='hdn_forsearch' value='1' />";
	}else{
		cargaPagina(pathtotal+"secciones/gastronomia/directorios/loadataopt.php", "opt="+opt, "", "medsupesprig");
	}
}

function cargarDirectorios(){
	var hdn=document.getElementById("hdn_forsearch").value;
	var val="";
	if(hdn=='1'){
		val=document.getElementById("txt_nom").value;
	}else{
		val=document.getElementById("cbo_tipo").value;
	}
	location.href=pathtotal+"secciones/gastronomia/directorios/?tip="+hdn+"&val="+val;
}