function publicar(id,escribe_mas,categoria){

    var comentario = document.getElementById("comment");
	var rating = document.getElementById("reviews_rating");

    if(comentario.value.length<15){
        alert(escribe_mas);
    }else{
        var existeimagen = document.getElementById("enviando");

        if(existeimagen!=null){ 
            alert('has already been sent');
        }else{
            if(id==0){
                var divComentarios = document.getElementById("nuevo");
            }else{
                var divComentarios = document.getElementById("responde_"+id);
            }
            var imagen = document.createElement('img');
            imagen.setAttribute('id', 'enviando');
            imagen.setAttribute('src', 'images/ajax-loader.gif');
            divComentarios.appendChild(imagen);
            var string="";
            if(categoria!=0){
                string='&categories_id='+categoria;
                if(products_id==0){
                    products_id=-1;
                }else{
                    products_id=0;
                }
            }
            $.post('comentarios.php', 'products_id=' + products_id + string + '&padre='+id+'&review='+comentario.value+'&reviews_rating='+rating.value,
		    function (datos){
		        var respuesta=eval("("+datos+")");
		        if(respuesta.error!=0){
		            alert("error: " + respuesta.error);
		        }else{
		            if(respuesta.nuevo==1){
		                $("#nuevo").html('');
		                $("#nuevo").html('<div class="comentarios"><a name="inicio"></a><div><h4 class="principal">'+respuesta.titulo+'<div style="float: left" id="titulo"></div></h4></div><p class="principal">'+comentario.value+'</p><div class=\"usuario\">'+respuesta.nombre+'</div></div>');
		            }else{
		                $("#responde_"+id).html('');
		                $("#responde_"+id).html('<div class="comentarios"><a name="inicio"></a><div><h4 class="principal">'+respuesta.titulo+'<div style="float: left" id="titulo"></div></h4></div><p class="principal">'+comentario.value+'</p><div class=\"usuario\">'+respuesta.nombre+'</div></div>');
		            }
		        }
		        $("#cancelar"+id).css({visibility:"hidden"});
		    });
	    }
    }
};
function eliminar(id){
    if(id==0){
        $("#nuevo").html('');
    }else{
        $("#responde_"+id).html('');
    }
    areadetexto=false;
    $("#cancelar"+id).css({visibility:"hidden"});
}
$(document).ready(function(){
    var areadetexto=false;
    var com=0;
    $(".cancelar").css({visibility:"hidden"});
     $(".comentar").click(function(){
        if($("*").index( $('#comment')[0] )==-1 && estalogeado==true){
	        var a = this;
	        var comentario = this.title;
	        com=comentario;
	        
			$('.nuevo-mensaje').css({display:"block"})
	        $("#cancelar"+com).css({visibility:"visible"});
	        
	        var textArea = document.createElement('TEXTAREA'); 
            textArea.appendChild(document.createTextNode(''));
            textArea.setAttribute('rows', '3');

            textArea.setAttribute('cols', '60');
            textArea.setAttribute('name', 'review');
			textArea.setAttribute('class', 'iText');
            textArea.setAttribute('id', 'comment');
			
			var vota = document.createElement('INPUT');
				vota.setAttribute('type','hidden');
				vota.setAttribute('name','reviews_rating');
				vota.setAttribute('id','reviews_rating');
				vota.setAttribute('value','5');
				vota.setAttribute('class','iHidden');
	        
			/* creamos la lista de valoracion */
			var elUL	= document.createElement('UL');
				elUL.setAttribute('class','rating');
			
			var elLIu	= document.createElement('LI');
			var elAhu	= document.createElement('A');
				elAhu.appendChild(document.createTextNode('1'));
				elAhu.setAttribute('href','javascript:;');
				elAhu.setAttribute('title','');
				elAhu.className = "una-estrella";
				elAhu.onmousedown = function() 
											{ 
												elAhd.className="dos-estrellas";
												elAht.className="tres-estrellas";
												elAhc.className="cuatro-estrellas";
												elAhf.className="cinco-estrellas";
												elAhu.className="una-estrella-s"; 
												vota.value = 1; 
											}
				elLIu.appendChild(elAhu);
			
			var elLId	= document.createElement('LI');
			var elAhd	= document.createElement('A');
				elAhd.appendChild(document.createTextNode('2'));
				elAhd.setAttribute('href','javascript:;');
				elAhd.setAttribute('title','');
				elAhd.className = "dos-estrellas";
				elAhd.onmousedown = function() 
											{ 
												elAhu.className="una-estrella";
												elAht.className="tres-estrellas";
												elAhc.className="cuatro-estrellas";
												elAhf.className="cinco-estrellas";
												elAhd.className="dos-estrellas-s"; 
												vota.value = 2; 
											}
				elLId.appendChild(elAhd);
			
			var elLIt	= document.createElement('LI');
			var elAht	= document.createElement('A');
				elAht.appendChild(document.createTextNode('3'));
				elAht.setAttribute('href','javascript:;');
				elAht.setAttribute('title','');
				elAht.className = "tres-estrellas";
				elAht.onmousedown = function() 
											{ 
												elAhu.className="una-estrella";
												elAhd.className="dos-estrellas";
												elAhc.className="cuatro-estrellas";
												elAhf.className="cinco-estrellas";
												elAht.className="tres-estrellas-s"; 
												vota.value = 3; 
											}
				elLIt.appendChild(elAht);
			
			var elLIc	= document.createElement('LI');
			var elAhc	= document.createElement('A');
				elAhc.appendChild(document.createTextNode('4'));
				elAhc.setAttribute('href','javascript:;');
				elAhc.setAttribute('title','');
				elAhc.className = "cuatro-estrellas";
				elAhc.onmousedown = function() 
											{ 
												elAhu.className="una-estrella";
												elAhd.className="dos-estrellas";
												elAht.className="tres-estrellas";
												elAhf.className="cinco-estrellas";
												elAhc.className="cuatro-estrellas-s"; 
												vota.value = 4; 
											}
				elLIc.appendChild(elAhc);
				
			var elLIf	= document.createElement('LI');
			var elAhf	= document.createElement('A');
				elAhf.appendChild(document.createTextNode('5'));
				elAhf.setAttribute('href','javascript:;');
				elAhf.setAttribute('title','');
				elAhf.className = "cinco-estrellas-s";
				elAhf.onmousedown = function() 
											{ 
												elAhu.className="una-estrella";
												elAhd.className="dos-estrellas";
												elAht.className="tres-estrellas";
												elAhc.className="cuatro-estrellas";
												elAhf.className="cinco-estrellas-s"; 
												vota.value = 5; 
											}
				elLIf.appendChild(elAhf);
			
			elUL.appendChild(elLIu);
			elUL.appendChild(elLId);
			elUL.appendChild(elLIt);
			elUL.appendChild(elLIc);
			elUL.appendChild(elLIf);
			
			
			
			/* fin de la lista de valoracion */
			
	        if(comentario==0){
	            var divComentarios = document.getElementById("nuevo");
                divComentarios.appendChild(textArea);
				divComentarios.appendChild(vota);
				divComentarios.appendChild(elUL);
	        }else{
		        var divComentarios = document.getElementById("responde_"+comentario);
			    divComentarios.appendChild(textArea);
				divComentarios.appendChild(vota);
				divComentarios.appendChild(elUL);
	        }
	        var enlacepublicar = document.createElement('a'); 
			var spanpublicar   = document.createElement('span');
		    spanpublicar.appendChild(document.createTextNode(textPublicar));
			enlacepublicar.appendChild(spanpublicar);
            enlacepublicar.setAttribute('class', 'publicar');
            enlacepublicar.setAttribute('id', 'publicar'+com);
            enlacepublicar.setAttribute('title', comentario);
            
			if(foro_categoria==true){
                enlacepublicar.setAttribute('href', 'javascript:publicar('+com+',\''+escribe_mas+'\',\''+categoria+'\')');
            }else{
                enlacepublicar.setAttribute('href', 'javascript:publicar('+com+',\''+escribe_mas+'\',0)');
            }
            divComentarios.appendChild(enlacepublicar);
            var comm=comentario;
		}else{
		  if($("*").index( $('#comment')[0] )!=-1){
		      alert(un_comentario);
		  }else if(estalogeado==false){
              alert(estar_logueado);
          }
		}
     });
     $(".publicar").click(function(){
        var a = this;
        var comentario = this.title;
        alert('publicar');
     });
     $("#comment").focus(function(){
        $("#comment").value='';
     });
    $('#comment').keyup(function() {
        $comment = $(this).val();
        alert($comment);
    });
});
