   
function ajaxSaveVote(strURL, strSubmit, strResultFunc) {
    /*
      Función que realiza llamada AJAX para grabar en la base de datos el voto
    */
    
    var xmlHttpReq = false;
 
    if (BrowserDetect.browser == 'Explorer'){
	xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }else {  
	xmlHttpReq = new XMLHttpRequest();
	xmlHttpReq.overrideMimeType('text/xml');
    }

    xmlHttpReq.open('GET', strURL + strSubmit, true);
    xmlHttpReq.setRequestHeader('Content-Type', 
				'application/x-www-form-urlencoded');
    xmlHttpReq.onreadystatechange = function() {
	if(xmlHttpReq.readyState == 4) {
	    if(xmlHttpReq.status == 200){
		divVot = document.getElementById('estrellas');
		pVot = document.getElementById("vota-actual");		

		var s = xmlHttpReq.responseText;
		var pText;
		if(idioma == 'E'){
		    pText = document.createTextNode((s.match(/\d+/g).pop() + " votos"));
		}else{
		    pText = document.createTextNode((s.match(/\d+/g).pop() + " votes"));
		}
                var newP = document.createElement('p');
		newP.setAttribute("class","votos_txt_vota2");
		newP.setAttribute("id", "vota-actual");
                newP.appendChild(pText);			
		divVot.removeChild(pVot);
		divVot.appendChild(newP);
		stars = Math.floor(sumatoria/votacion);
		rmainder = Math.floor(Math.abs((stars - (sumatoria/votacion))*10));
		averageUpdate();
	    }
	}
    }
    xmlHttpReq.send(null);
}

function sendVote(voto, anex)
{
    /*
      Envía el   voto
    */
    var cDay = new Date();
    
    var nameCookie = "BNAv" + anex;

    if (getCookie(nameCookie) == "")
	{
	    cDay.setFullYear(cDay.getYear()+1,cDay.getMonth(),cDay.getDay());
	    setCookie(nameCookie, "vote=yes", cDay.getYear(), cDay.getMonth(),cDay.getDay());
	   // strPrueba = "?documento="+$("noticia").value+"&idioma="+$("idioma").value+"&tipo_documento=1&action=save&voto=" + voto;
	    strPrueba = "?documento="+  g_noticia +"&idioma="+ g_idioma +"&tipo_documento=1&action=save&voto=" + voto;
	    strUrlPrueba = "/votacion.jsp";
	    result = "";
	    votacion = votacion + 1;
            sumatoria = sumatoria + parseInt(voto);	   
	//alert(strUrlPrueba + strPrueba );
	    ajaxSaveVote(strUrlPrueba, strPrueba, result);	    
	}
    else
	{
	    if (idioma == 'E'){
		alert("Estimado Usuario:\n Usted ya ha votado esta historia");
	    }else {

		alert("Dear User:\n You have already voted for this story.");
	    }
	}
}






/*
  Funciones para mini-sistema de votación
*/

function averageUpdate(){
/*
  Actualiza las estrellas que muestran el promedio de votacion de la nota
*/ 


  if($('estrellas')){ 
  
    var current;
    var assign = "false";

    // alert("Stars: "  + stars);
    for(var i = 1; i <= stars; i++){
	current = document.getElementById('star' + i);
	current.src = "/images/ico_vot_ok.gif";
    }
    current = document.getElementById('star' + i);
    //alert("Remainder: "  + rmainder);
    switch (rmainder)
	{
	case 1:
	current.src = "/images/ico_vot_no.gif";
	break;
	case 2:
	current.src = "/images/ico_vot_no.gif";
	break;
	case 3:
	current.src = "/images/ico_vot_md.gif";
	assign = "true";
	break;
	case 4:
	current.src = "/images/ico_vot_md.gif";
	assign = "true";
	break;
	case 5:
	current.src = "/images/ico_vot_md.gif";
	assign = "true";
	break;
	case 6:
	current.src = "/images/ico_vot_md.gif";
	assign = "true";
	break;
	case 7:
	current.src = "/images/ico_vot_md.gif";
	assign = "true";
	break;
	case 8:
	current.src = "/images/ico_vot_ok.gif";
	break;
	case 9:
	current.src = "/images/ico_vot_ok.gif";
	break;
	}
    i++;
    for(var j = i; j <= 5; j++){
	current = document.getElementById('star' + j);
	current.src = "/images/ico_vot_no.gif";
    }
  }
    
}

function setCookie ( name, value, exp_y, exp_m, exp_d, path, domain, secure ){
	/*	  
	  Graba la cookie que impide que la persona vote más de
	  una vez por la noticia.

	  name:  Nombre de la Cookie
	  value: Valor de la Cookie
	  exp_y: Año de Expiración
	  exp_m: Mes de Expiracióm
	  exp_d: Dia de Expiración
	  path:  Opcional, Path donde aplica la cookie
	  domain: Opcional, Dominio de la cookie, nosotros lo usamos
	  secure: Opcional, indica que se debe usar SSL. No la usamos (¿deberíamos?)
	*/

    var cookie_string = name + "=" + escape ( value );
    
    if ( exp_y ){
	var expires = new Date ( exp_y, exp_m, exp_d );
	cookie_string += "; expires=" + expires.toGMTString();
    }
    if ( path ){
        cookie_string += "; path=" + escape ( path );
    }
    if ( domain ){
        cookie_string += "; domain=" + escape ( domain );
    }
    if ( secure ){
        cookie_string += "; secure";
    }
    document.cookie = cookie_string;
}


function getCookie(name){

    if(name == '')
	return('');
    name_index = document.cookie.indexOf(name + '=');
    if(name_index == -1)
	return('');
    cookie_value =  document.cookie.substr(name_index + name.length + 1, 
					   document.cookie.length);
    
    //Todos los nombres de las cookies terminan con ;, menos la última
    end_of_cookie = cookie_value.indexOf(';');
    if(end_of_cookie != -1)
	cookie_value = cookie_value.substr(0, end_of_cookie);
    //Restaura los blancos
    space = cookie_value.indexOf('+');
    while(space != -1)
	{ 
	    cookie_value = cookie_value.substr(0, space) + ' ' + 
		cookie_value.substr(space + 1, cookie_value.length);
	    space = cookie_value.indexOf('+');
	}
    return(cookie_value);
}

