// JavaScript Document

function getHTTPObject() { 
  var xmlhttp; 
  /*@cc_on 
@if (@_jscript_version >= 5) 
  try { 
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); 
  } catch (e) { 
    try { 
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
    } catch (E) { 
      xmlhttp = false; 
    }
  } 
@else 
  xmlhttp = false; 
@end @*/ 
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') { 
    try { 
      xmlhttp = new XMLHttpRequest(); 
    } catch (e) { 
      xmlhttp = false; 
    } 
  } 
  return xmlhttp; 
}

function addProductToCart(productId,customerSessionId,productClass)
{
  var d = new Date();
  var xmlhttp = getHTTPObject();
  url="cartAddProduct.php?productId=" + productId + "&customerSessionId=" + escape(customerSessionId) + "&productClass=" + productClass + "&unique=" + d.getTime();
  xmlhttp.open("GET",url,true);
  xmlhttp.onreadystatechange = function () 
  {
    if (xmlhttp.readyState==4 || xmlhttp.readyState=="complete") {
      if (xmlhttp.responseText == "loppu")
      {
        alert("Tuotetta ei ole valitettavasti enempää varastossa");
      }

      getCart(customerSessionId);

      $('#cartContainer').css('display','inline');
    }
  }
  xmlhttp.send(null);
  sessioniId = customerSessionId;
}

function getCart(customerSessionId)
{
  $.ajax({
    url: "cartGet.php?customerSessionId=" + customerSessionId,
    cache: false,
    success: function(data){
      $('#cartContainer').html(data);
      if (parseInt($('#numOfProductsInCart').attr('value')) > 0)
      {
        $('#shoppingCartTag').text('Ostoskori (' + parseInt($('#numOfProductsInCart').attr('value')) + ')');
      } else {
        $('#shoppingCartTag').text('Ostoskori');
        if ($('#cartContainer').css('display') == 'inline')
        {
          $('#cartContainer').css('display','none');
        }
      }
    }
  });
}

function emptyCart(customerSessionId)
{
  $.ajax({
    url: "cartEmpty.php?customerSessionId=" + customerSessionId,
    cache: false,
    success: function(data){
      getCart(customerSessionId);
    }
  });
}

function removeProductFromCart(customerSessionId, productId) {
  vastaus = confirm("Haluatko varmasti poistaa tuotteen ostoskorista?");
  if (vastaus) {
    $.ajax({
      url: "cartRemoveProduct.php?customerSessionId=" + customerSessionId + "&productId=" + productId,
      cache: false,
      success: function(data){
        getCart(customerSessionId);
      }
    });
  }
}

function tarkistaTilausTiedot() {
  if ((document.getElementById("email").value).indexOf(',') > 0)
  {
    alert ("Virhe, tarkista tiedot! Sähköpostissa virheellinen merkki.");
    return false;
  }
  if ((document.getElementById("Postinumero").value).length != 5)
  {
    alert ("Virhe postinumerossa, tarkista tiedot!");
    return false;
  }
  if (document.getElementById("Sukunimi").value == "" || document.getElementById("Etunimi").value == "")
  {
    alert ("Tarkista tiedot. Nimi on ilmoitettava!");
    return false;
  }
  if (document.getElementById("puhelin").value == "" || document.getElementById("email").value == "")
  {
    alert ("Tarkista tiedot. Puhelinnumero ja sähköpostiosoite on ilmoitettava!");
    return false;
  }
  return true;
}



/***
* Tilaustuotteiden funktiot
***/

function cartAddPreOrderProduct(TTID,customerSessionId,luokka,tuoteryhma)
{
  var d = new Date();
  var xmlhttp = getHTTPObject();
  url="cartAddPreOrderProduct.php?TTID=" + TTID + "&customerSessionId=" + customerSessionId + "&luokka=" + luokka + "&tuoteryhma=" + tuoteryhma + "&unique=" + d.getTime();
  //	alert(url);
  xmlhttp.open("GET",url,true);
  xmlhttp.onreadystatechange = function () 
  {
    if (xmlhttp.readyState==4 || xmlhttp.readyState=="complete") {
      // Nothing
      getCart(customerSessionId);
    }
  }
  xmlhttp.send(null);
}

function cartRemovePreOrderProduct(customerSessionId, TTID) {
  vastaus = confirm("Haluatko varmasti poistaa tuotteen ostoskorista?");
  if (vastaus) {
    $.ajax({
      url: "cartRemovePreOrderProduct.php?customerSessionId=" + customerSessionId + "&TTID=" + TTID,
      cache: false,
      success: function(data){
        getCart(customerSessionId);
      }
    });
  }
}
