// JavaScript Document

/* The following function creates an XMLHttpRequest object... */
function createRequestObject_cart_js()
{
  var request_o; //declare the variable to hold the object.
  var browser = navigator.appName; //find the browser name
  if(window.ActiveXObject){
    request_o = new ActiveXObject("Microsoft.XMLHTTP");
  }else{
    request_o = new XMLHttpRequest();
  }
  return request_o; //return the object
}
var http_add2cart = createRequestObject_cart_js();
var http = createRequestObject_cart_js();


function add_to_cart(product_id,item_id)
{
	if(http_add2cart)
	{
		try
		{
			http_add2cart.open('get', '/cart_ajax.php?action=add_to_cart&product_id='+product_id+'&item_id='+item_id, true);
			http_add2cart.onreadystatechange = handle_add_to_cart;
			http_add2cart.send(null);
		}
		catch(e)
		{
			alert("Can't connect to server:\n" + e.toString());
		}
	}
}
function handle_add_to_cart()
{
	if(http_add2cart.readyState == 4){
		if (http_add2cart.status == 200){
			var response = http_add2cart.responseText;
			document.getElementById('div_product_added').style.display = "none";
			document.getElementById('div_product_added').innerHTML = response;
			document.ajaxInProgress = false;
			update_header_totals();
			show_add2cart()
		} else {
			alert ( "Ajax Error - "+http_add2cart.status );
		}
	}
}

function add_to_cart2(product_id,item_id,hidecart)
{
	if(http_add2cart)
	{
		new Ajax.Request('/cart_ajax.php?action=add_to_cart&product_id='+product_id+'&item_id='+item_id, {
	  	method: 'get',
	  	onSuccess: function(transport) {
				if(hidecart==true)
				{
				}else{
					update_header_totals();
					showPopWin('/my_cart_overlay.php?action=add', 800, 275, null, true, 'Items in your cart.');
				}
		  }
		});
	
	}
}


var update_header = '';

function update_header_totals()
{
	new Ajax.Request('/cart_ajax.php?action=update_header_totals', {
		method: 'get',
		onSuccess: function(transport) {
			var response = transport.responseXML;
			var xmlroot = response.documentElement;
			var items_array = xmlroot.getElementsByTagName("header_items");
			var totals_array = xmlroot.getElementsByTagName("header_total");
			var total_array = xmlroot.getElementsByTagName("total_items");
			try{document.getElementById("header_items").innerHTML = items_array.item(0).firstChild.data;}catch(err){}
			try{document.getElementById("header_total").innerHTML = totals_array.item(0).firstChild.data;}catch(err){}
			if(total_array.item(0).firstChild.data>0)
				$('header_cart_total').style.display='block'
			else
				$('header_cart_total').style.display='none'
		}
	});
}
	
function update_header_totalsOLD()
{
	if(http)
	{
		try
		{
			http.open('get', '/cart_ajax.php?action=update_header_totals', true);
			http.onreadystatechange = handle_update_header_totals;
			http.send(null);
		}
		catch(e)
		{
			alert("Can't connect to server:\n" + e.toString());
		}
	}
}

function handle_update_header_totals()
{
	if(http.readyState == 4){
		var response = http.responseXML;
		var xmlroot = response.documentElement;
		var items_array = xmlroot.getElementsByTagName("header_items");
		var totals_array = xmlroot.getElementsByTagName("header_total");
		var total_array = xmlroot.getElementsByTagName("total_items");
		try{document.getElementById("header_items").innerHTML = items_array.item(0).firstChild.data;}catch(err){}
		try{document.getElementById("header_total").innerHTML = totals_array.item(0).firstChild.data;}catch(err){}
		if(total_array.item(0).firstChild.data>0)
			$('header_cart_total').style.display='block'
		else
			$('header_cart_total').style.display='none'
	}
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}


