// JavaScript Document

var xmlHttp = createXmlHttpRequestObject(); 

function createXmlHttpRequestObject() 
{	
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // if running Internet Explorer
  if(window.ActiveXObject)
  {
    try
    {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e) 
    {
      xmlHttp = false;
    }
  }
  // if running Mozilla or other browsers
  else
  {
    try 
    {
      xmlHttp = new XMLHttpRequest();
    }
    catch (e) 
    {
      xmlHttp = false;
    }
  }
  // return the created object or display an error message
  if (!xmlHttp)
 
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}

function adicionarVoto(id)
{
	idConteudo = id
	
	var codePage = document.getElementById('codePage').value
	
	for(i=0; i<document.form_2['cls'].length; i++)
	{
		if((document.form_2['cls'][i].checked) == true){
			valorVoto = document.form_2['cls'][i].value;
			break;
		}
		else
			valorVoto = 0
	}
	
	if(valorVoto > 0)
	{
		url = "?valorVoto=" + valorVoto + "&idConteudo=" + idConteudo + "&codePage=" + codePage
		pageView = 'modAjax/votacao.php' + url
		
		if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
		{		
			xmlHttp.open("GET", pageView, true);  
			// define the method to handle server responses		
			xmlHttp.onreadystatechange = ServerResponse;		 
			// make the server request
			xmlHttp.send(null);	
		}
		else
			setTimeout('adicionarVoto()', 1000);
			alert("Classificacão enviado com sucesso")
	}
	else
		alert("Não Seleccionou uma Classificacão")
}

function adicionarVotoTop10(id)
{
	idConteudo = id
	
	var codePage = document.getElementById('codePage').value
	
	for(i=0; i<document.form_2['cls'].length; i++)
	{
		if((document.form_2['cls'][i].checked) == true){
			valorVoto = document.form_2['cls'][i].value;
			break;
		}
		else
			valorVoto = 0
	}
	
	if(valorVoto > 0)
	{
		url = "?valorVoto=" + valorVoto + "&idConteudo=" + idConteudo + "&codePage=" + codePage
		pageView = 'modAjax/votacao.php' + url
		
		if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
		{		
			xmlHttp.open("GET", pageView, true);  
			// define the method to handle server responses		
			xmlHttp.onreadystatechange = ServerResponse;		 
			// make the server request
			xmlHttp.send(null);	
		}
		else
			setTimeout('adicionarVoto()', 1000);
	}
	else
		alert("Nao Seleccionou uma Classificacao")
}

function ServerResponse() 
{
   /*if (xmlHttp.readyState==1)
		document.getElementById('divLoad').style.display='block';
	*/		
			
  if (xmlHttp.readyState == 4) 
  {
    // status of 200 indicates the transaction completed successfully
    if (xmlHttp.status == 200) 
    {
      xmlResponse = xmlHttp.responseText;
	  for(i=0; i<document.form_2['cls'].length; i++)
	  {
		document.form_2['cls'][i].checked = false
	  }
	} 
    else 
    {
      alert("There was a problem accessing the server: " + xmlHttp.statusText);
    }
  }
}
