function addValuationToComment(idNota, val)
{
    var formu=document.forms['valuation'+idNota];
    var logado=getCookie("cvjvisitor");
    if(logado!=null && logado!='' && formu.valorado.value=="0"){
        var spanActual=document.getElementById('numVecesActual'+idNota);
        spanActual.style.visibility="hidden";
        spanActual.style.display="none";
        var spanFuturo=document.getElementById('numVecesFuturo'+idNota);
        spanFuturo.style.visibility="visible";
        spanFuturo.style.display="inline";
        var spanValActual=document.getElementById('valActual'+idNota);
        spanValActual.style.visibility="hidden";
        spanValActual.style.display="none";
        if(val=="+1"){
            var spanValFuturo=document.getElementById('valFutura'+idNota);
            spanValFuturo.style.visibility="visible";
            spanValFuturo.style.display="inline";
        }else{
            var spanvalPasada=document.getElementById('valPasada'+idNota);
            spanvalPasada.style.visibility="visible";
            spanvalPasada.style.display="inline";
        }

        formu.valorado.value="1";
        var myConn = new XHConn();
        if (!myConn) alert(xmlhttp);
        var peticion = function (oXML) {}
        myConn.connect("/fichas/switchValoracion", "POST", "idNota="+idNota+"&val="+val, peticion);
        
    }else if(logado==null || logado==''){
        alert(registrateVal);
    }else{
        alert(yaValorado);
    }
    return false;
}
function masUds(idUds){
    if(document.getElementById(idUds)!=null){
        var uds=document.getElementById(idUds).value;
        if(uds!=null && uds!='undefined'){
            var unidades=eval(uds);
            unidades=unidades+1;
            document.getElementById(idUds).value=unidades;
        }
    }
}
function menosUds(idUds){
    var uds=document.getElementById(idUds).value;
    if(uds!=null && uds!='undefined'){
        var unidades=eval(uds);
        if(unidades>1){
            unidades=unidades-1;
            document.getElementById(idUds).value=unidades;
        }
    }
}
function envioComentComprador(idVendedor,form){
    var alias = form.alias.value;
    var email = form.email.value;
    var valoracion = form.starAction.value;
    var comentarios = form.comentarios.value;
    if(isEmpty(alias)){
       alert("Debe poner un alias");
       return false;
    }
    if(!isEmail(email)){
       alert("Debe poner un email válido");
       return false;
    }
    if(valoracion==0){
       alert("Debe poner una valoracion");
       return false;
    }
    if(isEmpty(comentarios)){
       alert("Debe poner un comentario");
       return false;
    }
    document.comentForm.action='/vendedor/guardarOpinionComprador?idVendedor='+idVendedor;
    document.comentForm.submit();
    return true;
}
function comprobarFavorito(productId, elementId){
    var favoritos=getCookie("favorites");
    while(favoritos!=null && favoritos.indexOf("|")!=-1){
        var i= favoritos.indexOf("|");
        if (i>-1) {
            var idFavorito= favoritos.substring(0,i);
            if(idFavorito!=null && productId==idFavorito){
                var imgId=document.getElementById(elementId);
                if(imgId!=null){
                    var img=imgId.src;
                    img=img.replace('_off', '_on');
                    imgId.src=img;
                }
            }
        }
        favoritos=favoritos.substring(i+1);
    }
}
function comprobarFavorito2(productId, elementId,hhFav,hhAdd,hhDel){
    var favoritos=getCookie("favorites");
    var imgId=document.getElementById(elementId);
    var favState=document.getElementById(hhFav);
    var txtAdd=document.getElementById(hhAdd);
    var txtDel=document.getElementById(hhDel);
    while(favoritos!=null && favoritos.indexOf("|")!=-1){
        var i= favoritos.indexOf("|");
        if (i>-1) {
            var idFavorito= favoritos.substring(0,i);
            if(idFavorito!=null && productId==idFavorito){
                if(imgId!=null){
                    if (txtDel != null)
                        imgId.innerHTML=txtDel.value;
                    if (favState != null)
                        favState.value = '_on';
                }
            }
        }
        favoritos=favoritos.substring(i+1);
    }
}
function switchFavoritesState2(productId, sender, heart,hhFav,hhAdd,hhDel)
{
    var favState=document.getElementById(hhFav);
    var txtAdd=document.getElementById(hhAdd);
    var txtDel=document.getElementById(hhDel);
    var logado=getCookie("cvjvisitor");
    if(logado!=null && logado!=''){
        var switchOff = (favState.value.indexOf('_on') != -1);
        var onfav='true';
        if(switchOff){
            onfav='false';
            favState.value = '_off';
            sender.innerHTML = txtAdd.value;
        }else{
            favState.value = '_on';
            sender.innerHTML = txtDel.value;
        }
        var myConn = new XHConn();
        if (!myConn) alert(xmlhttp);
        var peticion = function (oXML) {
            try{
                var rText=oXML.responseText;
                if (rText!=null && rText.length>0){
                    var respuesta=eval(rText);
                    if (respuesta>=0){
                        var spanFavorites=document.getElementById('spanFavorites');
                        if(spanFavorites!=null)
                            spanFavorites.innerHTML=respuesta;
                    }
                }
            }catch(ex){
            }
        };
        myConn.connect("/secciones/switchFavorites", "POST", "idproducto="+productId+"&onfavoritos="+onfav, peticion);

    }else{
        if(!heart)
            document.getElementById('smsFavoritos').style.visibility="visible";
        document.getElementById('smsFavoritos').style.display="block";
    }
    return false;
}
function switchFavoritesState(productId, sender, heart)
{
    var logado=getCookie("cvjvisitor");
    if(logado!=null && logado!=''){
        var switchOff = (sender.src.indexOf('_on') != -1);
        var img=sender.src;
        var onfav='true';
        if(switchOff){
            onfav='false';
            img=img.replace('_on', '_off');
        }else{
            img=img.replace('_off', '_on');

        }
        sender.src=img;
        var myConn = new XHConn();
        if (!myConn) alert(xmlhttp);
        var peticion = function (oXML) {
            try{
                var rText=oXML.responseText;
                if (rText!=null && rText.length>0){
                    var respuesta=eval(rText);
                    if (respuesta>=0){
                        var spanFavorites=document.getElementById('spanFavorites');
                        if(spanFavorites!=null)
                            spanFavorites.innerHTML=respuesta;
                    }
                }
            }catch(ex){
            }
        };
        myConn.connect("/secciones/switchFavorites", "POST", "idproducto="+productId+"&onfavoritos="+onfav, peticion);
        
    }else{
        if(!heart)
            document.getElementById('smsFavoritos').style.visibility="visible";
        document.getElementById('smsFavoritos').style.display="block";
    }
    return false;
}
function showPanel(idPanel){
    var panel=document.getElementById(idPanel);
    if(panel!=null){
        panel.style.visibility="visible";
        panel.style.display="block";
    }
}
function hidePanel(idPanel){
    var panel=document.getElementById(idPanel);
    if(panel!=null){
        panel.style.visibility="hidden";
        panel.style.display="none";
    }
}
function borrarPanel(idNota, idPanel){
    var pError=document.getElementById('errorAbuse'+idNota);
    pError.style.visibility="hidden";
    pError.style.display="none";
    var pGracias=document.getElementById('graciasAbuse'+idNota);
    pGracias.style.visibility="hidden";
    pGracias.style.display="none";
    hidePanel(idPanel);
}

function mostrarPanel(divId, idPanel, idNota, idProducto){
    var coment=document.getElementById(divId);
	if (coment.innerHTML==""){
		var myConn = new XHConn();
		if (!myConn) alert(xmlhttp);
		var peticion = function (oXML) {
			coment.innerHTML=oXML.responseText;
            coment.style.display='block';
		}
		myConn.connect("/fichas/reporteAbuso", "POST", "idproducto="+idProducto+"&idnota="+idNota, peticion);
	}else{
        var panel=document.getElementById(idPanel);
        panel.style.visibility="visible";
        panel.style.display='block';
		coment.style.display='block';
	}    
}

function abrirPanel(idNota, idPanel){
    var formu=document.forms['abuse'+idNota];
    showPanel(idPanel);
    if(formu.aceptarAbuse.disabled){
            var pGracias=document.getElementById('graciasAbuse'+idNota);
            pGracias.style.visibility="visible";
            pGracias.style.display="block";
    }    
}
function reportAbuse(idProducto, idNota){
    var formu=document.forms['abuse'+idNota];
    var pError=null;
    if(formu!=null){
        if(isEmpty(formu.reasonAbuse.value) || isEmpty(formu.nameAbuse.value) || isEmpty(formu.emailAbuse.value) || !isEmail(formu.emailAbuse.value)){
            pError=document.getElementById('errorAbuse'+idNota);
            pError.style.visibility="visible";
            pError.style.display="block";
        }else{
            pError=document.getElementById('errorAbuse'+idNota);
            pError.style.visibility="hidden";
            pError.style.display="none";
            var pGracias=document.getElementById('graciasAbuse'+idNota);
            pGracias.style.visibility="visible";
            pGracias.style.display="block";
            formu.aceptarAbuse.disabled=true;
            var myConn = new XHConn();
            if (!myConn) alert(xmlhttp);
            var peticion = function (oXML) {}
            myConn.connect("/fichas/enviarDenuncia", "POST", "idproducto="+idProducto+"&idnota="+idNota+"&nombreDenunciante="+formu.nameAbuse.value+"&mailDenunciante="+formu.emailAbuse.value+"&textoDenuncia="+formu.reasonAbuse.value, peticion);
        }
    }
}

// Precarga de imágenes
if (document.images) {
    var boton1_off = new Image();
    boton1_off.src = "/im/icon_star_off.png";
    var boton1_on = new Image();
    boton1_on.src = "/im/icon_star_on.png";
    var boton1_middle = new Image();
    boton1_middle.src = "/im/icon_star_middle.png";
}

// Carga de imagen cuando el ratón pasa por encima
function entra(formName, boton) {
    var botonString = boton;
    if(document.getElementById(formName).starAction.value=='0'){
        if (document.images) {
            if (botonString.indexOf('boton1')!=-1) {
                document.images[boton].src = boton1_on.src;
            }
            if (botonString.indexOf('boton2')!=-1) {
                document.images['boton1'].src = boton1_on.src;
                document.images[boton].src = boton1_on.src;
            }
            if (botonString.indexOf('boton3')!=-1) {
                document.images['boton1'].src = boton1_on.src;
                document.images['boton2'].src = boton1_on.src;
                document.images[boton].src = boton1_on.src;
            }
            if (botonString.indexOf('boton4')!=-1) {
                document.images['boton1'].src = boton1_on.src;
                document.images['boton2'].src = boton1_on.src;
                document.images['boton3'].src = boton1_on.src;
                document.images[boton].src = boton1_on.src;
            }
            if (botonString.indexOf('boton5')!=-1) {
                document.images['boton1'].src = boton1_on.src;
                document.images['boton2'].src = boton1_on.src;
                document.images['boton3'].src = boton1_on.src;
                document.images['boton4'].src = boton1_on.src;
                document.images[boton].src = boton1_on.src;
            }
        }
    }
}

// Carga de imagen cuando el ratón abandona el área de la imagen
function sale(formName, boton) {
    var botonString = boton;
    if(document.getElementById(formName).starAction.value=='0'){
        if (document.images) {
            if (botonString.indexOf('boton1')!=-1) {
                document.images[boton].src = boton1_off.src;
            }
            if (botonString.indexOf('boton2')!=-1) {
                document.images['boton1'].src = boton1_off.src;
                document.images[boton].src = boton1_off.src;
            }
            if (botonString.indexOf('boton3')!=-1) {
                document.images['boton1'].src = boton1_off.src;
                document.images['boton2'].src = boton1_off.src;
                document.images[boton].src = boton1_off.src;
            }
            if (botonString.indexOf('boton4')!=-1) {
                document.images['boton1'].src = boton1_off.src;
                document.images['boton2'].src = boton1_off.src;
                document.images['boton3'].src = boton1_off.src;
                document.images[boton].src = boton1_off.src;
            }
            if (botonString.indexOf('boton5')!=-1) {
                document.images['boton1'].src = boton1_off.src;
                document.images['boton2'].src = boton1_off.src;
                document.images['boton3'].src = boton1_off.src;
                document.images['boton4'].src = boton1_off.src;
                document.images[boton].src = boton1_off.src;
            }
        }
    }
}

function fijarValorStar(formName, val){
    document.getElementById(formName).starAction.value=val;
    switch(val){
        case "1":
            document.images['boton1'].src = boton1_on.src;
            document.images['boton2'].src = boton1_off.src;
            document.images['boton3'].src = boton1_off.src;
            document.images['boton4'].src = boton1_off.src;
            document.images['boton5'].src = boton1_off.src;
        break;
        case "2":
            document.images['boton1'].src = boton1_on.src;
            document.images['boton2'].src = boton1_on.src;
            document.images['boton3'].src = boton1_off.src;
            document.images['boton4'].src = boton1_off.src;
            document.images['boton5'].src = boton1_off.src;
        break;
        case "3":
            document.images['boton1'].src = boton1_on.src;
            document.images['boton2'].src = boton1_on.src;
            document.images['boton3'].src = boton1_on.src;
            document.images['boton4'].src = boton1_off.src;
            document.images['boton5'].src = boton1_off.src;
        break;
        case "4":
            document.images['boton1'].src = boton1_on.src;
            document.images['boton2'].src = boton1_on.src;
            document.images['boton3'].src = boton1_on.src;
            document.images['boton4'].src = boton1_on.src;
            document.images['boton5'].src = boton1_off.src;
        break;
        case "5":
            document.images['boton1'].src = boton1_on.src;
            document.images['boton2'].src = boton1_on.src;
            document.images['boton3'].src = boton1_on.src;
            document.images['boton4'].src = boton1_on.src;
            document.images['boton5'].src = boton1_on.src;
        break;
    }
}

function chequearAnonimo(){
    var formu=document.forms['comentForm'];
    if(eval("document.forms['comentForm'].anonimo.checked"))
        formu.name.value="anonimo";
    else
        formu.name.value="";
}

function envioComent(idProducto, idExterno){
    var formu=document.forms['comentForm'];
    var alertComent=null;
    var alertNota=null;
    var alertNombre=null;
    var alertEmail=null;
    if(formu!=null){
        alertNombre=document.getElementById('alertNombre');
        alertNombre.style.visibility="hidden";
        alertNombre.style.display="none";
        alertEmail=document.getElementById('alertEmail');
        alertEmail.style.visibility="hidden";
        alertEmail.style.display="none";
        alertComent=document.getElementById('alertComent');
        alertComent.style.visibility="hidden";
        alertComent.style.display="none";
        alertNota=document.getElementById('alertNota');
        alertNota.style.visibility="hidden";
        alertNota.style.display="none";
        if(isEmpty(formu.name.value)){
            alertNombre=document.getElementById('alertNombre');
            alertNombre.style.visibility="visible";
            alertNombre.style.display="block";
       }else if(isEmpty(formu.email.value) || !isEmail(formu.email.value)){
            alertEmail=document.getElementById('alertEmail');
            alertEmail.style.visibility="visible";
            alertEmail.style.display="block";
        }else if(isEmpty(formu.textComent.value)){
            alertComent=document.getElementById('alertComent');
            alertComent.style.visibility="visible";
            alertComent.style.display="block";
        }else if(formu.starAction.value=="0"){
            alertNota=document.getElementById('alertNota');
            alertNota.style.visibility="visible";
            alertNota.style.display="block";
        }else{
            //chequearAnonimo();
            var pGracias=document.getElementById('graciasComent');
            pGracias.style.visibility="visible";
            pGracias.style.display="block";
            formu.envioForm.disabled=true;
            var myConn = new XHConn();
            if (!myConn) alert(xmlhttp);
            var peticion = function (oXML) {}
            myConn.connect("/fichas/enviarComentario", "POST", "idproducto="+idProducto+"&idexterno="+idExterno+"&nombre="+formu.name.value+"&puntuacion="+formu.starAction.value+"&texto="+formu.textComent.value+"&email="+formu.email.value, peticion);
        }
    }
}
function irAOpinar(elemID, idProducto, idExterno){
    var offsetTrail = document.getElementById(elemID);
    var offsetLeft = 0;
    var offsetTop = 0;
    while (offsetTrail) {
    offsetLeft += offsetTrail.offsetLeft;
    offsetTop += offsetTrail.offsetTop;
    offsetTrail = offsetTrail.offsetParent;
    }
    if (navigator.userAgent.indexOf("Mac") != -1 && typeof document.body.leftMargin != "undefined" && navigator.appName=="Microsoft Internet Explorer" ) {
    offsetLeft += parseInt(document.body.leftMargin);
    offsetTop += parseInt(document.body.topMargin);
    }
    window.scrollTo(offsetLeft,offsetTop);
    mostrarOpinarProducto(idProducto,idExterno);
}
function mostrarOpinarProducto(idProducto, idExterno){
	var coment=document.getElementById('cajaComentario');
	if (coment.innerHTML==""){
		var myConn = new XHConn();
		if (!myConn) alert(xmlhttp);
		var peticion = function (oXML) {
			coment.innerHTML=oXML.responseText;
            var capaComentar=document.getElementById('cajaComentario');
            capaComentar.style.display='block';
			coment.style.display='block';
		}
		myConn.connect("/fichas/cajaComentario", "POST", "idproducto="+idProducto+"&idexterno="+idExterno, peticion);
	}else{
        var capaComentar=document.getElementById('cajaComentario');
		capaComentar.style.display='block';
		coment.style.display='block';
	}
}


function evalBoletin(nombreFormulario){
    var formu=document.forms[nombreFormulario];
    if(formu!=null){
       if(isEmpty(formu.email.value) || !isEmail(formu.email.value) || formu.email.value=="tunombre@dominio.com"){
            TINY.box.show(document.getElementById('errorBoletin').innerHTML,0,0,0,1);
        }else{
            var tipo="";
            if(nombreFormulario.indexOf("xxx")>-1)
                tipo="xxx";
            var myConn = new XHConn();
            if (!myConn) alert(xmlhttp);
            var peticion = function (oXML) {}
            myConn.connect("/otros/enviarBoletin", "POST", "email="+formu.email.value+"&tipo="+tipo, peticion);
            TINY.box.show(document.getElementById('graciasBoletin').innerHTML,0,0,0,1);
        }
    }
}

