var calificacion=0;
var WEB_ROOT="";
var ID = 0;

function EnviarRectificacion(){
	var articuloid=document.getElementById("hdaid").value;
	var RectificarNombre=document.getElementById("RectificarNombre").value;
	var RectificarEmail=document.getElementById("RectificarEmail").value;	
	var RectificarMotivo=document.getElementById("RectificarMotivo").value;	
	var RectificarMensaje=document.getElementById("RectificarMensaje").value;
	var hdaurl=document.getElementById("hdaurl").value;
	var hdatitulo=document.getElementById("hdatitulo").value;	
	
	if(RectificarNombre!="" && validarEmail(RectificarEmail) && RectificarMotivo!="" && RectificarMensaje !="" ){	
		xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null)
		{
			alert ("Browser does not support HTTP Request");
			return;
		}		
		document.getElementById("RectificarMensajeError").innerHTML="Por favor espere...";
		var url=WEB_ROOT+"/webservices/noticia.aspx";				
		url=url+"?opcion=EnviarRectificacion";		
		url=url+"&RectificarNombre="+RectificarNombre;	
		url=url+"&RectificarEmail="+RectificarEmail;	
		url=url+"&RectificarMotivo="+RectificarMotivo;	
		url=url+"&RectificarMensaje="+RectificarMensaje;	
		url=url+"&url="+hdaurl;	
		url=url+"&titulo="+hdatitulo;	
		
		xmlHttp.onreadystatechange=stateChangedEnviarRectificacion;
		xmlHttp.open("GET",url,true);
		//alert(url);
		xmlHttp.send(null);
	}else{
		if(RectificarNombre=="" || RectificarEmail=="" || RectificarMotivo=="" || RectificarMensaje==""){	
			document.getElementById("RectificarMensajeError").innerHTML="Debe ingresar los campos obligatorios.";
		}else{
			if( !validarEmail(RectificarEmail) ){	
				document.getElementById("RectificarMensajeError").innerHTML="Debe ingresar un email v&aacute;lido.";
			}
		}
	}
}
function stateChangedEnviarRectificacion() 
{ 		
	if(xmlHttp.readyState == 4 || xmlHttp.readyState=="complete")
	{		
		if(xmlHttp.status == 200){		
			xml = xmlHttp.responseText;						
			//alert(xml);
			ManejarRespuestasRectificacion()
		}					
	}else{		
	}	
}
function ManejarRespuestasRectificacion(){
	if(xml=="OK"){
		document.getElementById("RectificarMensajeError").innerHTML="Gracias, su informaci&oacute;n ha sido enviada para su revisi&oacute;n.";
		document.getElementById("formRectificacion").reset();
	}else{
		alert(xml);
	}
}

/***********************************************************/

function EnviarCorreo(){
	var articuloid=document.getElementById("hdaid").value;
	var CorreoNombreRemitente=document.getElementById("CorreoNombreRemitente").value;
	var CorreoEmailRemitente=document.getElementById("CorreoEmailRemitente").value;
	var CorreoNombreDestinatario=document.getElementById("CorreoNombreDestinatario").value;
	var CorreoEmailDestinatario=document.getElementById("CorreoEmailDestinatario").value;
	var CorreoMensaje=document.getElementById("CorreoMensaje").value;
	var hdaurl=document.getElementById("hdaurl").value;
	var hdatitulo=document.getElementById("hdatitulo").value;
	
	if(CorreoNombreRemitente!="" && validarEmail(CorreoEmailRemitente) && CorreoNombreDestinatario!="" && validarEmail(CorreoEmailDestinatario)){	
		xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null)
		{
			alert ("Browser does not support HTTP Request");
			return;
		}		
		document.getElementById("CorreoMensajeError").innerHTML="Por favor espere...";
		var url=WEB_ROOT+"/webservices/noticia.aspx";				
		url=url+"?opcion=EnviarCorreo";		
		url=url+"&CorreoNombreRemitente="+CorreoNombreRemitente;	
		url=url+"&CorreoEmailRemitente="+CorreoEmailRemitente;	
		url=url+"&CorreoNombreDestinatario="+CorreoNombreDestinatario;	
		url=url+"&CorreoEmailDestinatario="+CorreoEmailDestinatario;	
		url=url+"&CorreoMensaje="+CorreoMensaje;			
		url=url+"&url="+hdaurl;	
		url=url+"&titulo="+hdatitulo;	
		url=url+"&articuloid="+articuloid;
		
		xmlHttp.onreadystatechange=stateChangedEnviarCorreo;
		xmlHttp.open("GET",url,true);
		//alert(url);
		xmlHttp.send(null);
	}else{
		if(CorreoNombreRemitente=="" || CorreoEmailRemitente=="" || CorreoNombreDestinatario=="" || CorreoEmailDestinatario==""){	
			document.getElementById("CorreoMensajeError").innerHTML="Debe ingresar los campos obligatorios.";
		}else{
			if( !validarEmail(CorreoEmailRemitente)  ||  !validarEmail(CorreoEmailDestinatario) ){	
				document.getElementById("CorreoMensajeError").innerHTML="Debe ingresar un email v&aacute;lido.";
			}
		}
	}
}

function stateChangedEnviarCorreo() 
{ 		
	if(xmlHttp.readyState == 4 || xmlHttp.readyState=="complete")
	{		
		if(xmlHttp.status == 200){		
			xml = xmlHttp.responseText;						
			//alert(xml);
			ManejarRespuestasCorreo()
		}					
	}else{		
	}	
}
function ManejarRespuestasCorreo(){
	if(xml=="OK"){
		document.getElementById("CorreoMensajeError").innerHTML="Gracias por recomendar este ar&iacute;culo.";
		document.getElementById("formEnviarCorreo").reset();
	}else{
		alert(xml);
	}
}

function EnviarCorreoPost(id) {
    //alert(id);
    ID = id;
    var articuloid = document.getElementById("hdaid" + id).value;
    var CorreoNombreRemitente = document.getElementById("CorreoNombreRemitente" + id).value;
    var CorreoEmailRemitente = document.getElementById("CorreoEmailRemitente" + id).value;
    var CorreoNombreDestinatario = document.getElementById("CorreoNombreDestinatario" + id).value;
    var CorreoEmailDestinatario = document.getElementById("CorreoEmailDestinatario" + id).value;
    var CorreoMensaje = document.getElementById("CorreoMensaje" + id).value;
    var hdaurl = document.getElementById("hdaurl" + id).value;
    var hdatitulo = document.getElementById("hdatitulo" + id).value;

    if (CorreoNombreRemitente != "" && validarEmail(CorreoEmailRemitente) && CorreoNombreDestinatario != "" && validarEmail(CorreoEmailDestinatario)) {
        xmlHttp = GetXmlHttpObject()
        if (xmlHttp == null) {
            alert("Browser does not support HTTP Request");
            return;
        }
        document.getElementById("CorreoMensajeError" + id).innerHTML = "Por favor espere...";
        var url = WEB_ROOT + "/webservices/noticia.aspx";
        url = url + "?opcion=EnviarCorreo";
        url = url + "&CorreoNombreRemitente=" + CorreoNombreRemitente;
        url = url + "&CorreoEmailRemitente=" + CorreoEmailRemitente;
        url = url + "&CorreoNombreDestinatario=" + CorreoNombreDestinatario;
        url = url + "&CorreoEmailDestinatario=" + CorreoEmailDestinatario;
        url = url + "&CorreoMensaje=" + CorreoMensaje;
        url = url + "&url=" + hdaurl;
        url = url + "&titulo=" + hdatitulo;
        url = url + "&articuloid=" + articuloid;

        xmlHttp.onreadystatechange = stateChangedEnviarCorreoPost;
        xmlHttp.open("GET", url, true);
        //alert(url);
        xmlHttp.send(null);
    } else {
        if (CorreoNombreRemitente == "" || CorreoEmailRemitente == "" || CorreoNombreDestinatario == "" || CorreoEmailDestinatario == "") {
            document.getElementById("CorreoMensajeError" + id).innerHTML = "Debe ingresar los campos obligatorios.";
        } else {
            if (!validarEmail(CorreoEmailRemitente) || !validarEmail(CorreoEmailDestinatario)) {
                document.getElementById("CorreoMensajeError" + id).innerHTML = "Debe ingresar un email v&aacute;lido.";
            }
        }
    }
}
function stateChangedEnviarCorreoPost() {
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
        if (xmlHttp.status == 200) {
            xml = xmlHttp.responseText;
            //alert(xml);
            ManejarRespuestasCorreoPost()
        }
    } else {
    }
}
function ManejarRespuestasCorreoPost() {
    if (xml == "OK") {
        document.getElementById("CorreoMensajeError" + ID).innerHTML = "Gracias por recomendar este ar&iacute;culo.";
        document.getElementById("formEnviarCorreo"+ID).reset();
    } else {
        alert(xml);
    }
}

/***********************************************************/
function setearCalificacion(valor)
{	
	calificacion=valor;
	for(var i=1; i<=5; i++)
	{
		$("#opcionEstrella"+i).removeClass("inactivo");
		$("#opcionEstrella"+i).removeClass("activo");		
		if(i<=valor){
			$("#opcionEstrella"+i).addClass("activo"); 
		}
	}
}
function CalificarEstrellas()
{
	var articuloid=document.getElementById("hdaid").value;
	xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null)
		{
			alert ("Browser does not support HTTP Request");
			return;
		}		
		var url=WEB_ROOT+"/webservices/noticia.aspx";				
		url=url+"?opcion=Calificar";		
		url=url+"&calificacion="+calificacion;	
		url=url+"&articuloid="+articuloid;		
		xmlHttp.onreadystatechange=stateChangedCalificar;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
}
function stateChangedCalificar() 
{ 		
	if(xmlHttp.readyState == 4 || xmlHttp.readyState=="complete")
	{		
		if(xmlHttp.status == 200){		
			xml = xmlHttp.responseText;						
			//alert(xml);
			ManejarRespuestasCalificar()
		}					
	}else{		
	}	
}
function ManejarRespuestasCalificar(){
	if(xml=="OK"){
		alert("Gracias por calificar este artículo.");
	}else{
		alert("Gracias por calificar este artículo.asdasd sd");
	}
}

/***********************************************************/
function fontSize(op) {
	var target="#noticiaInternaContenido";	
	if(op==0)	{
		curSize = parseInt($(target).css("font-size"));
		newSize = curSize - 1;
		if (newSize >= 9) {
			$(target).css('font-size', newSize);
		} 
	}
	if(op==1){
		curSize = parseInt($(target).css("font-size"));
		newSize = curSize + 1;
		if (newSize <= 30) {
			$(target).css('font-size', newSize);
		} 
	}
	if(op==-1){		
		$(target).css('font-size', 12);
		 
	}
}
function ComentarNoticia()
{
	document.getElementById("mensajeContacto").innerHTML="";
	if ($(".comentarNoticia").is(":hidden")) {
        $(".comentarNoticia").slideDown("slow");
      } else {
        $(".comentarNoticia").hide();
      }

}

function registrarContacto()
{
	var nombre=document.getElementById("txtnombre").value;
	var email=document.getElementById("txtemail").value;
	var comentario=document.getElementById("txtcomentario").value;
	var articuloid=document.getElementById("hdaid").value;
		
	if(nombre!="" && validarEmail(email) && comentario!=""){		
		xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null)
		{
			alert ("Browser does not support HTTP Request");
			return;
		}
		$("#mensajeContacto").slideDown("slow");
		document.getElementById("mensajeContacto").className="mensaje";
		document.getElementById("mensajeContacto").innerHTML="Por favor espere...";
		var url=WEB_ROOT+"/webservices/noticia.aspx";		
		
		url=url+"?opcion=Comentario";		
		url=url+"&nombre="+nombre;		
		url=url+"&email="+email;
		url=url+"&comentario="+comentario;
		url=url+"&articuloid="+articuloid;
		xmlHttp.onreadystatechange=stateChanged;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
		//alert(url);		
			
	}else{
		document.getElementById("mensajeContacto").className="mensaje-error";
		if(nombre=="" || email=="" || comentario==""){	
			document.getElementById("mensajeContacto").innerHTML="Debe ingresar todos los campos.";
		}else{
			if(!validarEmail(email)){	
				document.getElementById("mensajeContacto").innerHTML="Debe ingresar un email v&aacute;lido.";
			}
		}
	}
}
function stateChanged() 
{ 		
	if(xmlHttp.readyState == 4 || xmlHttp.readyState=="complete")
	{		
		if(xmlHttp.status == 200){		
//			xml = xmlHttp.responseXML;			
			xml = xmlHttp.responseText;						
			//alert(xml);
			ManejarRespuestas()
		}					
	}else{		
	}	
}
function ManejarRespuestas(){	
	if(xml=="OK"){
		document.getElementById("formComentario").reset();
		document.getElementById("mensajeContacto").innerHTML="Gracias por participar, su comentario ser&aacute; revisado y publicado lo m&aacute;s pronto nos sea posible.";
		setTimeout("ComentarNoticia()",2000);		
	}else{
		document.getElementById("mensajeContacto").innerHTML="Se produjo un error por favor intentelo más tarde. Gracias";
	}	
}
function MaxComentario(contenido, max) {
   ;
    if (document.getElementById(contenido) != null) {
        var textbox = document.getElementById(contenido);
        if (textbox.value.length > max) {
            textbox.value = textbox.value.substring(0, max);            
            textbox.scrollTop = textbox.scrollTop + textbox.scrollHeight;
        }
    }
}
function validarEmail(valor) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor)){	
		return (true)
	} else {
		return (false);
	}
}
function OcultarMensajeComentario(){
	$("#mensajeContacto").fadeOut("slow");
}


/********/

function CargarCaricatura(edicion) {
   // alert(edicion);

    xmlHttp = GetXmlHttpObject()
    if (xmlHttp == null) {
        alert("Browser does not support HTTP Request");
        return;
    }
    document.getElementById("CaricaturaNoticia").innerHTML = "Cargando...";
    var url = WEB_ROOT + "/webservices/noticia.aspx";
    url = url + "?opcion=caricatura";
    //url = url + "&id=" + id;
    url = url + "&edicion=" + edicion;    
    xmlHttp.onreadystatechange = stateChangedCargarCaricatura;
    xmlHttp.open("GET", url, true);
    //alert(url);
    xmlHttp.send(null);

}
function stateChangedCargarCaricatura() {
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
        if (xmlHttp.status == 200) {            
            xml = xmlHttp.responseText;
            ManejarRespuestasCargarCaricatura()
        }
    } else {
    }
}
function ManejarRespuestasCargarCaricatura() {
    if (xml != "Error") {
        document.getElementById("CaricaturaNoticia").innerHTML =xml;
    } else {
        //document.getElementById("CaricaturaNoticia").innerHTML = "Se produjo un error por favor intentelo más tarde. Gracias";    
    }
}

