var xmlhttpComm = null;
var idNoticia   = 0;
var tipoGlobal  = "noticia";

function showMsg2(opcion, move, e)
{
    if(opcion == "nosession"){
        obj('nosessionDiv2').style.display = "block";
    }

    if(obj('dynBox')){
        obj('dynBox').style.display = "none";
    }
    obj('dynBox2').style.display = "block";
    
    return 0;
}

function hideMsg2(divId){
    obj(divId).style.display  = "none";
    obj('dynBox2').style.display = "none";

    return 0;
}

function commentsAction(tipo, action, id, e)
{
    tipoGlobal = tipo;
    if(action == "nosession"){
        showMsg2(action, "yes", e);
        return false;
    }
    if(action == "get"){
        loadComments(action, "/services/getCommentsNoSession.php?tipo=" + tipo + "&id=" + id + "&r=" + Math.floor(Math.random() * 100));
        idNoticia = id;
    }
    if(action == "getBlog"){
        loadComments(action, "/services/getCommentsNoSession.php?tipo=" + tipo + "&id=" + id + "&blog=1");
        idNoticia = id;
    }
    if(action == "put" || action == "putBlog"){
        if(obj('commentText')){
            if(obj('email').value != ""){
                if(!validaEmail(obj('email').value)){
                    alert("Escriba por favor un correo electrónico válido (puede omitirlo)");
                    return false;
                }
            }
            if(obj('nombreC').value != "" && obj('commentText').value != ""){
                //loadComments(action, "/services/putCommentNoSession.php?nombre=" + obj('nombreC').value + "&email=" + obj('email').value + "&tipo=" + tipo + "&id=" + id + "&texto=" + obj('commentText').value + "&seguir=" + (obj('seguir').checked ? 1 : 0));
                loadComments(action, "/services/putCommentNoSession.php", "nombre=" + obj('nombreC').value + "&email=" + obj('email').value + "&tipo=" + tipo + "&id=" + id + "&texto=" + obj('commentText').value + "&seguir=" + (obj('seguir').checked ? 1 : 0));
            }
            else{
                alert("Favor de escribir nombre y comentario");
                return false;
            }
        }
        else{
            alert("NO COMMENT FIELD");
        }
    }
    if(action == "del"){
        if(window.confirm("¿Estás seguro de querer eliminar éste comentario?")){
            loadComments(action, "/services/delCommentAdmin.php?tipo=" + tipo + "&idlog=" + id, "");
        }
    }
    
	return 0;
}

function loadComments(action, url, postParams)
{
	if(window.XMLHttpRequest){		// code for Mozilla, etc.
		if(xmlhttpComm){
			xmlhttpComm.abort;
		}
		xmlhttpComm = new XMLHttpRequest();
	}
	else{
		if(window.ActiveXObject){	// code for IE
            try{
                xmlhttpComm = new ActiveXObject("Msxml2.XMLHTTP");
            }
			catch(e){
                try{
                    xmlhttpComm = new ActiveXObject("Microsoft.XMLHTTP");
                }
				catch(e){
					alert("Name:\t" + e.name + "\nMessage:\t" + e.message);
				}
            }
		}
	}
    if(xmlhttpComm){
        if(action == "get"){
            xmlhttpComm.onreadystatechange = getComments;
        }
        if(action == "getBlog"){
            xmlhttpComm.onreadystatechange = getCommentsBlog;
        }
        if(action == "getNum"){
            xmlhttpComm.onreadystatechange = getCommentsNum;
        }
        if(action == "put" || action == "del"){
            xmlhttpComm.open("POST",url,true);
            xmlhttpComm.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xmlhttpComm.setRequestHeader("Content-length", postParams.length);
            xmlhttpComm.setRequestHeader("Connection", "close");
            xmlhttpComm.onreadystatechange = putComment;
			xmlhttpComm.send(postParams);
			return 0;
        }
        if(action == "putBlog" || action == "delBlog"){
            xmlhttpComm.onreadystatechange = putCommentBlog;
        }
        xmlhttpComm.open("GET",url,true);		//asynchronious false para que se detenga; false no funciona en Firefox en modo Asincrono
        xmlhttpComm.send(null);
    }
	else{
    	alert('Giving up :( Cannot create an XMLHTTP instance');
    	return false;
	}
	return 0;
}

function getComments()
{
	if(xmlhttpComm.readyState == 4){
  		if(xmlhttpComm.status == 200){
			obj('commentsDiv').innerHTML = xmlhttpComm.responseText;
            loadComments("getNum", "/services/getCommentsNum.php?tipo=" + tipoGlobal + "&id=" + idNoticia);
        }
    }
	return 0;
}

function getCommentsBlog()
{
	if(xmlhttpComm.readyState == 4){
  		if(xmlhttpComm.status == 200){
			obj('commentsDiv').innerHTML = xmlhttpComm.responseText;
        }
    }
	return 0;
}

function getCommentsNum()
{
	if(xmlhttpComm.readyState == 4){
  		if(xmlhttpComm.status == 200){
			obj('commentsNum').innerHTML = xmlhttpComm.responseText;
			if(obj('commentsNumTop')){
				obj('commentsNumTop').innerHTML = xmlhttpComm.responseText;
			}
        }
    }
	return 0;
}

function putComment()
{
	if(xmlhttpComm.readyState == 4){
  		if(xmlhttpComm.status == 200){
			var jsonObj = eval("(" + xmlhttpComm.responseText + ")");
			if(jsonObj.msgError == "YES"){
				commentsAction(tipoGlobal, "get", obj(tipoGlobal == 'editorial' ? 'idEditorial' : (tipoGlobal == 'reporte' ? 'idReporte' : (tipoGlobal == 'semanario' ? 'idSemanario' : 'idNoticia'))).value);
                //window.location.href = "#commentsDiv";
            }
            else{
                alert(jsonObj.msgError);
            }
        }
    }
	return 0;
}

function putCommentBlog()
{
	if(xmlhttpComm.readyState == 4){
  		if(xmlhttpComm.status == 200){
			var jsonObj = eval("(" + xmlhttpComm.responseText + ")");
			if(jsonObj.msgError == "YES"){
                commentsAction(tipoGlobal, "getBlog", obj(tipoGlobal == 'editorial' ? 'idEditorial' : (tipoGlobal == 'reporte' ? 'idReporte' : (tipoGlobal == 'semanario' ? 'idSemanario' : 'idNoticia'))).value);
                //window.location.href = "#commentsDiv";
            }
            else{
                alert(jsonObj.msgError);
            }
        }
    }
	return 0;
}

