//### Fisher Solutions PHP/MySQL Shopping Cart System (v1.0)
//*** Copyright August 2010 - Robert Fisher (Fisher Solutions), http://www.fisher-solutions.co.uk
//*** You may copy and redistribute this code, but please keep the copyright notice intact.
//  {Requires: prototype-1.6.0.2.js and fs_objfunctions_01.js and fs_objfunctions_02.js}

//init code:
dbLoc = "";

function refreshBasket(){
  var agent_url = dbLoc + 'includes/ajax-refreshbasket.php?&' + getuniquestring() + '&';
  
  var myAjax = new Ajax.Request(
    agent_url,
    {method: "get",
    parameters:'&',
    onComplete: processBasketXML
    }
  );
  return false;  
}

//Function to add to basket...  The Ajax call and subsequent handling should write the updated basket details into the current window without the need for a full window refresh.
function addtobasket(bname, ItemNo) {
  //bname is the button name (ie the NoteId of the product item)
  var prod_id = bname.slice(1);
  var fname = "F" + prod_id;
  var obj = xDOM(fname, 0) ;    // - From fs_objfunctions_01.js.
  var qty = obj.value;

  var agent_url = dbLoc + 'includes/ajax-addtobasket.php?&' + getuniquestring() + '&';

  if (qty=='') {
    qty = '1';
  } else if (qty=='0') {
    return('false');
  }
  
  var myAjax = new Ajax.Request(
    agent_url,
    {method: "get",
    parameters:'&U=' + prod_id + '&Q=' + qty + '&',
    onComplete: processBasketXML
    }
  );
  
  return false;
}

function processBasketXML(xmlD) {
	
	var xmltext = xmlD.responseText;
	//alert('xmltext: ' + xmltext);
	
        // Grab the xmlDoc object from the correct parser for this browser:
	try //Internet Explorer
  	{
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async="false";
		xmlDoc.loadXML(xmltext);
	}
	catch(e)
	{
		try //Firefox, Mozilla, Opera, etc.
		{
			parser=new DOMParser();
			xmlDoc=parser.parseFromString(xmltext,"text/xml");
		}
		catch(e) {alert(e.message)}
	}
	
	var qty = GetTextFromNode(xmlDoc.getElementsByTagName("all_qty")[0].childNodes[0]);
	var subtot = GetTextFromNode(xmlDoc.getElementsByTagName("subtot_GBP")[0].childNodes[0]);
	
	qty = parseInt(qty);
	
	//pl is the plural marker - ie anything other than 1 item needs an s on the end to make item[s].	
	var pl = '';
	if(qty != 1) { pl = 's'; }
	
	var html = '';
	html = '<h3>Your shopping basket</h3>';
	html = html + '<p>' + qty + '&nbsp;item' + pl + '&nbsp;|&nbsp;Subtotal:&nbsp;&pound;' + subtot + '</p>';
	html = html + "<a href='" + '' + "basket.php'>Click to review and checkout</a>";
	
        var basket = document.getElementById("minibasket");
	if (qty > 0) {
		basket.innerHTML = html;
		fadeIn('minibasket', getOpacity('minibasket'));
	} else { 
		fadeOut('minibasket', getOpacity('minibasket'));
		basket.innerHTML = html;
	}
}

function GetTextFromNode(node) {
  
  try {
    if (node.textContent) {
      return node.textContent;
    } else if (node.innerText) {
      return node.innerText;
    } else {
      return node.text;
    }
  } catch(e) {
    alert(e.message);
  }
}
