
// stores the reference to the XMLHttpRequest object
var xmlHttp = createXmlHttpRequestObject();

// Adiciona um item ao carrinho
function addBasket(id)
{
	// Captura caixa de texto com Qtd
	var objQtd = document.getElementById('txtQtd' + id);

	// Verifica a Qtd
	if (IsNumber(objQtd.value) && objQtd.value != "")
	{
		// Adicionando item ao Basket
		updateBasket(id, objQtd.value);
	}
	else
	{
		// Qtd inválida
		alert("Por favor, informe uma Qtd. válida");
		objQtd.focus();
	}
	return;
}

function updateBasket(id, qtd)
{
	// Adicionando/Removendo item ao Basket
	parent.frmBasket.location.href='carrinho.php?produto=' + id + '&qtd=' + qtd;
	return;
}

// Aumenta quantidade de um item
function upQtd(id){

	var objQtd = document.getElementById('txtQtd' + id);
	var qtde = objQtd.value;

	if (qtde != (qtde/1) ) {
		qtde = 0;
	}

	qtde++;

	if (qtde > 99){
		qtde--;
	}else if(qtde <= 0){
		qtde = 1;
	}

	objQtd.value = qtde;

	return qtde;
}

// Diminui quantidade de um item
function downQtd(id){

	var objQtd = document.getElementById('txtQtd' + id);
	var qtde = objQtd.value;

	if (qtde != (qtde/1) ) {
		qtde = 0;
	}

	qtde--;

	if (qtde > 99){
		qtde--;
	}else if(qtde <= 0){
		qtde = 1;
	}

	objQtd.value = qtde;

	return qtde;
}

//
// Funções Matemáticas
//

// Verifica se uma variável só possui números.
function IsNumber(numero)
{
   var i;
   var nPonto = 0;

   for (i = 0; i < numero.length; i++)
   {
      if (numero.charAt(i) < "0" || numero.charAt(i) > "9")
      {
	      return false;
      }
   }
   return true;
}

////////////////////////////////////////////////////////////////////////////////
// Ajax begins here (Daniel - 12/05/07)
////////////////////////////////////////////////////////////////////////////////

// retrieves the XMLHttpRequest object
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;
}

// make asynchronous HTTP request using the XMLHttpRequest object
function b(id)
{

  document.getElementById("janela").rows[0].cells[0].innerHTML = "<center><table style='background: #fff; padding: 10px; border: 1px solid #804000; width: 300px;'><tbody><tr><td><center><b>Carregando...</b></center></td></tr></tbody></table>";
	var sombra = parent.document.getElementById("sombra");
	if(window.ActiveXObject)
		sombra.style.display = "inline";
	else
		sombra.style.display = "table";
	var janela = parent.document.getElementById("janela");
	if(window.ActiveXObject)
		janela.style.display = "inline";
	else
		janela.style.display = "table";

	//parent.document.body.style.overflow = "hidden";
	sombra.style.zIndex = "1";
	janela.style.zIndex = "2";

  // proceed only if the xmlHttp object isn't busy
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
  {
    // execute the quickstart.php page from the server
    xmlHttp.open("GET", "info.php?id=" + id + "&qtd=" + document.getElementById('txtQtd' + id).value, true);
    // define the method to handle server responses
    xmlHttp.onreadystatechange = handleServerResponse;
    // make the server request
    xmlHttp.send(null);
  }
  else
    // if the connection is busy, try again after one second
    setTimeout('product_show(' + id + ')', 1000);

}

// executed automatically when a message is received from the server
function handleServerResponse()
{

  // move forward only if the transaction has completed
  if (xmlHttp.readyState == 4)
  {

    // status of 200 indicates the transaction completed successfully
    // status of 200 indicates the transaction completed successfully
    if (xmlHttp.status == 200)
    {
		document.getElementById("janela").rows[0].cells[0].innerHTML = xmlHttp.responseText;
    }
    // a HTTP status different than 200 signals an error
    else
    {
      alert("Ocorreu um erro ao acessar o servidor. Por favor, tente novamente. (Erro " + xmlHttp.statusText + ")");
    }
  }
}

function fechaJanela()
{
	var sombra = parent.document.getElementById("sombra");
	sombra.style.display = "";
	var janela = parent.document.getElementById("janela");
	janela.style.display = "";

}
