/**
 * @version 20090114
 */

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

function obj(id)
{
    return document.getElementById(id);
}

function commentsAction(action, id)
{
    if(action == "get"){
        //alert("/services/getCommentsParques.php?&id=" + id + "&r=" + Math.floor(Math.random() * 100));
        loadComments(action, "/services/getCommentsParques.php?&id=" + id + "&r=" + Math.floor(Math.random() * 100), '');
        idNoticia = id;
    }
    if(action == "put"){
        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=parque&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?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 == "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('');
    }
	else{
    	alert('Giving up :( Cannot create an XMLHTTP instance');
    	return false;
	}
	return 0;
}

function getComments()
{
	if(xmlhttpComm.readyState == 4){
  		if(xmlhttpComm.status == 200){
			obj('comentarios').innerHTML = xmlhttpComm.responseText;
            loadComments("getNum", "/services/getCommentsNum.php?tipo=" + tipoGlobal + "&id=" + idNoticia, '');
        }
    }
	
	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("get", obj('idParque').value);
                //window.location.href = "#commentsDiv";
            }
            else{
                alert(jsonObj.msgError);
            }
        }
    }
	return 0;
}


