
// Text routines...

function replaceText(el, text) {
  if (el != null) {
    clearText(el);
    var newNode = document.createTextNode(text);
    el.appendChild(newNode);
  }
}

function clearText(el) {
  if (el != null) {
    if (el.childNodes) {
      for (var i = 0; i < el.childNodes.length; i++) {
        var childNode = el.childNodes[i];
        el.removeChild(childNode);
      }
    }
  }
}

function getText(el) {
  var text = "";
  if (el != null) {
    if (el.childNodes) {
      for (var i = 0; i < el.childNodes.length; i++) {
        var childNode = el.childNodes[i];
        if (childNode.nodeValue != null) {
          text = text + childNode.nodeValue;
        }
      }
    }
  }
  return text;
}
// Ajax handler...
function AJAX(url,handler) {
	if (typeof XMLHttpRequest == "undefined") {
		XMLHttpRequest=function() {
			try { return new ActiveXObject("Msxml2.XMLHTTP.6.0") } catch(e) {}
			try { return new ActiveXObject("Msxml2.XMLHTTP.3.0") } catch(e) {}
			try { return new ActiveXObject("Msxml2.XMLHTTP") } catch(e) {}
			try { return new ActiveXObject("Microsoft.XMLHTTP") } catch(e) {}
			throw new Error( "This browser does not support XMLHttpRequest." )
		}
	}

	var req=new XMLHttpRequest();
	req.onreadystatechange=function() {
		//alert('response rec with readyState='+req.readyState+', text='+req.responseText);
		if (req.readyState==4) {
			//alert('response rec with readyState='+req.readyState+', status='+req.status);
			if (req.status==200) {
				//alert("forwarding page response to "+handler.name+" for "+req.responseText);
				handler(req.responseText);
			}
		}
	}
	req.open("GET",url,true);
	req.send(null);
}

/* -------------------------------------------------------------
	Cart routines...
	--------------------------------------------------------------- */

function addToCart(id) {
	//message='Added to cart.';
//	message='Added '+id+' to cart.';
	var cart=new Cookie(document,'cart',1);
	cart.load();
	// add 1 of this id to the cart
	var varname='id'+id;
	if (cart[varname]) {
		cart[varname]=(Number(cart[varname]))+1;
	} else
		cart[varname]=1;

	var num_items=cart.getNumberOfItems();
	var message='Cart contains '+num_items+' items.';
	displayStatusMessage(message);
	displayCartImage(num_items);
	cart.store();
}
function displayStatusMessage(message) {
	var element=document.getElementById('cartinfo');
	if (element!=null) {
		replaceText(element,message);
	}
}

function displayCartImage(num_items) {
	var element=document.getElementById('cartimage');
	if (element!=null) {
		//var image_html='<img src="images/cart.png" alt="add to cart" title="add to cart" />';
		var image_html='<img src="images/'+FindCartImage(num_items)+'" alt="add to cart" title="add to cart" />';
		//alert(image_html);
		element.innerHTML=image_html;
	}
}
function GetCartContentsMessage() {
	var num_items=CountItemsInCart();
	if (num_items==0)
		return 'Cart is empty.';
	if (num_items==1)
		return 'Cart contains 1 item.';
	return 'Cart contains '+num_items+' items.';
}

// similar to FindCartImage in ShoppingCartClass.php
function FindCartImage(num_items) {
	//var num_items=cart.getNumberOfCart();
	if (num_items==0)
		return 'cart.png';
	if (num_items==1)
		return 'cart_1item.png';
	if (num_items==2)
		return 'cart_2item.png';
	return 'cart_3item.png';
}
/*
function addToCookie(name,line) {
	days=3;
	var date=new Date();
	date.setTime(date.getTime ()+(days*24*60*60*1000));
	var expires="expires="+date.toGMTString();
	document.cookie='name='+name+'; '+expires+'; contents='+line+';';
}

function readCookie(name) {
	return document.cookie;
	var cookiename=name+"=";
	var ca=document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ')
			c=c.substring(1,c.length);
		if (c.indexOf(cookiename)==0)
			return c.substring(cookiename.length,c.length);
	}
	return null;
}
*/
/* -------------------------------------------------------------
 * Cookie routines from JavaScript - The Definitive Guide p.271
 * -------------------------------------------------------------
 */

function Cookie(document,name,hours,path,domain,secure) {
	// All predefined properties begin with $ to distinguish them
	// from other properties, which are the values to be stored in the cookie
	this.$document=document;
	this.$name=name;
	if (hours)
		this.$expiration=new Date((new Date()).getTime()+hours*3600000);
	else
		this.$expiration=null;
	if (path)
		this.$path=path;
	else
		this.$path=null;
	if (domain)
		this.$domain=domain;
	else
		this.$domain=null;
	if (secure)
		this.$secure=true;
	else
		this.$secure=false;
}

Cookie.prototype.getNumberOfItems=function() {
	total=0;
	for (var prop in this) {
		// ignore values with names that begin with $ and also methods
		if ((prop.charAt(0)=='$') || ((typeof this[prop])=='function'))
			continue;
		total+=Number(this[prop]);
	}
	return total;
}

Cookie.prototype.store=function() {
	// first form the value of the cookie's contents
	var cookieval="";
	for (var prop in this) {
		// ignore values with names that begin with $ and also methods
		if ((prop.charAt(0)=='$') || ((typeof this[prop])=='function'))
			continue;
		if (cookieval!="")
			cookieval+='&';
		cookieval+=prop+':'+escape(this[prop]);
	}

	// now build the entire cookie string
	var cookie=this.$name+'='+cookieval;
	if (this.$expiration)
		cookie+='; expires='+this.$expiration.toGMTString();
	if (this.$path)
		cookie+='; path='+this.$path;
	if (this.$domain)
		cookie+='; domain='+this.$domain;
	if (this.$secure)
		cookie+='; secure';

	// finally store the cookie
	this.$document.cookie=cookie;
}

Cookie.prototype.load=function() {
	var allcookies=this.$document.cookie;
	if (allcookies=="")
		return false;
	// extract named cookies from list
	var start=allcookies.indexOf(this.$name+'=');
	if (start==-1)
		return false;	// cookie not defined for this page
	start+=this.$name.length+1;	// skip the name and equals sign
	var end=allcookies.indexOf(';',start);
	if (end==-1)
		end=allcookies.length;
	var cookieval=allcookies.substring(start,end);

	// Now that we've extracted the value of the named cookie,
	// we must break that value down into individual state variables
	var a=cookieval.split('&');
	for (var i=0; i<a.length; i++)
		a[i]=a[i].split(':');		// each pair becomes an array

	// Now set the variable names
	for (var i=0; i<a.length; i++)
		this[a[i][0]]=unescape(a[i][1]);

	return true;
}

Cookie.prototype.remove=function() {
	var cookie;
	cookie=this.$name+'=';
	if (this.$path)
		cookie+='; path='+this.$path;
	if (this.$domain)
		cookie+='; domain='+this.$domain;
	cookie+='; expires=Fri, 02-Jan-1970 00:00:00 GMT';
	this.$document.cookie=cookie;
}