function addValuationToComment(idNota, val)
{
    var formu=document.forms['valuation'+idNota];
    var logado=getCookie("dvdgovisitor");
    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 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 switchFavoritesState(productId, sender, heart)
{
    var logado=getCookie("dvdgovisitor");
    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";
    }
    return false;
}

function comprobarFavorito(productId, elementId, elementTxtId){
    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;
                }
                if(elementTxtId!=null && elementTxtId!='undefined'){
                    var txtId=document.getElementById(elementTxtId);
                    if(txtId!=null){
                        txtId.innerHTML=quitarFav;
                    }
                }
            }
        }
        favoritos=favoritos.substring(i+1);
    }
}

function switchFavoritesState(productId, sender, heart, elementTxtId)
{
    var logado=getCookie("dvdgovisitor");
    if(logado!=null && logado!=''){
        var txtId=null;
        if(elementTxtId!=null && elementTxtId!='undefined'){
            txtId=document.getElementById(elementTxtId);
        }
        if(sender.src==null || sender.src=='undefined'){
            sender=document.getElementById(sender);
        }
        var switchOff = (sender.src.indexOf('_on') != -1);
        var img=sender.src;
        var onfav='true';
        if(switchOff){
            onfav='false';
            img=img.replace('_on', '_off');
            if(txtId!=null){
                txtId.innerHTML=anadirFav;
            }
        }else{
            img=img.replace('_off', '_on');
            if(txtId!=null){
                txtId.innerHTML=quitarFav;
            }
        }
        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";
    }
    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 = "/i/white_star.png";
    var boton1_on = new Image();
    boton1_on.src = "/i/black_star.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 insertarHistorial(idProd,idExt,idEstado,idFormato,titulo,precio,img,puntuacion,votos,pvpSinIva,iva,campoIds,campoValues,fecha,stock){
    var linea='';
    var producto='';
    if(idProd!=null && idExt!=null && idEstado!=null && idFormato!=null && titulo!=null && precio!=null && img!=null && puntuacion!=null && votos!=null && fecha!=null && stock!=null)
        producto=idProd+'~'+idExt+'~'+idEstado+'~'+idFormato;
    var cadenaCampos='';
    if(campoIds!=null && campoValues!=null && campoIds!='undefined' && campoValues!='undefined'){
        cadenaCampos=generarCadenaCampos(campoIds,campoValues);
    }
    var cookie=getCookie("dvdhist");
	if (cookie!=null && cookie!='') {
        var existe=true;
		if (cookie.indexOf(producto)<0){
            existe=false;
            var lineas= cookie.split('_');
            var limite=lineas.length;            
            if(eval(limite)>=10){
                limite=eval(9);
            }
            for (var i=0;i<limite;i++) {
                linea=linea+lineas[i]+'_';
            }
        }
        if(!existe && idProd!=null && idExt!=null && idEstado!=null && idFormato!=null && titulo!=null && precio!=null && img!=null && puntuacion!=null && votos!=null && pvpSinIva!=null && iva!=null && fecha!=null && stock!=null){
            linea=idProd+'~'+idExt+'~'+idEstado+'~'+idFormato+'~'+titulo+'~'+precio+'~'+img+'~'+puntuacion+'~'+votos+'~'+pvpSinIva+'~'+iva+'~'+cadenaCampos+'~'+fecha+'~'+stock+'_'+linea;
            setCookie("dvdhist",linea,"","/","","");
        }
    }else{
        if(idProd!=null && idExt!=null && idEstado!=null && idFormato!=null && titulo!=null && precio!=null && img!=null && puntuacion!=null && votos!=null && pvpSinIva!=null && iva!=null && fecha!=null && stock!=null && fecha!=null && stock!=null)
            linea=idProd+'~'+idExt+'~'+idEstado+'~'+idFormato+'~'+titulo+'~'+precio+'~'+img+'~'+puntuacion+'~'+votos+'~'+pvpSinIva+'~'+iva+'~'+cadenaCampos+'~'+fecha+'~'+stock+'_'+linea;
        setCookie("dvdhist",linea,"","/","","");
    }
}

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);
        }
    }
}

