function getXMLHttp()
{
  var xmlHttp

  try
  {
    //Firefox, Opera 8.0+, Safari
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    //Internet Explorer
    try
    {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
      try
      {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch(e)
      {
        alert("Your browser does not support AJAX!")
        return false;
      }
    }
  }
  return xmlHttp;
}

function MakeRequest()
{
  var xmlHttp = getXMLHttp();
  
  xmlHttp.onreadystatechange = function()
  {
    if(xmlHttp.readyState == 4)
    {
      HandleResponse(xmlHttp.responseText);
    }
  }

  xmlHttp.open("GET", "/ajax.php", true); 
  xmlHttp.send(null);
}

function HandleResponse(response)
{
  //document.getElementById('ResponseDiv').innerHTML = response;

	var pairs=response.split('|'); 

	e=document.getElementById('votar');
	ee=e.getElementsByTagName('ul');
	ee[0].className='rated '+pairs[1];

	document.getElementById('votos').innerHTML='('+pairs[0]+') votos';

}

function votarPelicula(varIdPelicula,varEstrellas) {
  var xmlHttp = getXMLHttp();
  
  xmlHttp.onreadystatechange = function()
  {
    if(xmlHttp.readyState == 4)
    {
      HandleResponse(xmlHttp.responseText);
    }
  }

  xmlHttp.open("GET", "/ajax-votar-pelicula.php?idpelicula="+varIdPelicula+"&estrellas="+varEstrellas, true); 
  xmlHttp.send(null);
}


function HandleResponse2(response)
{
  //document.getElementById('ResponseDiv').innerHTML = response;

	
	//var pairs=response.split('|'); 

	//e=document.getElementById('votar');
	//ee=e.getElementsByTagName('ul');
	//ee[0].className='rated '+pairs[1];
	if (response == 'ok') {
		document.getElementById('peliculaFichaFavorita').innerHTML='Pelicula favorita OK';
	}
	else {
		document.getElementById('peliculaFichaFavorita').innerHTML='Necesita estar registrado';
	}	

}

function agregarPeliculaFavorita(varIdPelicula) {
  var xmlHttp = getXMLHttp();
  
  xmlHttp.onreadystatechange = function()
  {
    if(xmlHttp.readyState == 4)
    {
      HandleResponse2(xmlHttp.responseText);
    }
  }

  xmlHttp.open("GET", "/ajax-agregar-pelicula-favorita.php?idpelicula="+varIdPelicula, true); 
  xmlHttp.send(null);
}

function cavicCotizarPelicula(varIdPelicula) {

 	var error='';
	var varFormato=0;
	
	if (document.formularioCotizador.cantidad.value == '') {

		document.getElementById('cavicCotizadorPeliculaError').innerHTML='Ingrese cantidad';

	} else if (document.formularioCotizador.precio.value == '') {

		document.getElementById('cavicCotizadorPeliculaError').innerHTML='Ingrese precio';	

	} else {

		document.getElementById('cavicCotizadorPeliculaError').innerHTML='';


		if (document.formularioCotizador.formato.length == undefined) {
			varFormato=1;
		}
		else {

			var i=0;
			for (i=0; i<document.formularioCotizador.formato.length; i++) {

				if (document.formularioCotizador.formato[i].checked) {
			        	break; 
				}
			}
			varFormato=document.formularioCotizador.formato[i].value;
		}
	

		varPrecio=document.formularioCotizador.precio.value.replace(',','.');
		varPrecio=varPrecio.replace('$','');
		varCantidad=document.formularioCotizador.cantidad.value;

		var xmlHttp = getXMLHttp();
  
		xmlHttp.onreadystatechange = function() {
			if(xmlHttp.readyState == 4) {
				cavicCotizarPelicula2(xmlHttp.responseText);
			}
		}

		xmlHttp.open("GET", "/ajax-cavic-cotizar-pelicula.php?idpelicula="+varIdPelicula+"&formato="+varFormato+"&precio="+varPrecio+"&cantidad="+varCantidad, true); 
		xmlHttp.send(null);
		
	}
}

function cavicCotizarPelicula2(response)
{
	

	var pairs=response.split('|'); 
	//e=document.getElementById('votar');
	//ee=e.getElementsByTagName('ul');
	//ee[0].className='rated '+pairs[1];

	
	

	//window.alert('agregar ok= '+response);
	if (pairs[0] == 'agregarOk') {
		document.getElementById('cavicCotizadorPeliculaError').innerHTML='Pelicula agregada';
	} else {
		document.getElementById('cavicCotizadorPeliculaError').innerHTML=pairs[1];
	}
}
