/* browser detection code from:
http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html
Copyright Netscape
*/


var agt=navigator.userAgent.toLowerCase();
var is_nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
                && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
                && (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));
		
var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));


/* capture screen and window parameters so that the shopping cart can be
   loaded so that it does not overlap with the main prodcut list.
*/
var cX=0;
var cY=360;
var cW=750;
var cH=180;

var pX=0;
var pY=0;
var pW=750;
var pH=360;

// Get the screen height & width
var sH=550, sW=750; // use some defaults
if(window.screen)
{
	sH = window.screen.availHeight;
	sW = window.screen.availWidth;
}
if (document.all) {
	pY = window.screenTop
	pX = window.screenLeft;
}
else if (document.layers) {
	pX = window.screenX;
	pY = window.screenY;
}
cW = window.outerWidth; // make the cart width the same as the parent window
pX = 0;
pY = 0;
pW = sW;
cW = pW;
if(is_ie)
{
	pH = ((sH / 3) * 2) -50; // the main window should cover 2/3 of the screen
	cH = sH / 3; // the child windows should cover 1/3 of the screen.
	cY = pH;
}
else
{
	pH = ((sH / 3)*2) -70; // the main window should cover 1/2 of the screen (could have multiple tool/task bars
	cH = sH / 3 - 10; // the child windows should cover 1/3 of the screen.
	cY = pH + 30;
}

/* ------------------------------------------------------------------

function addToCart()
	
	Call this fucntion to add a prodcut to the shopping cart. It
	will load a small child window and display the cart contents
	in the new child window.
	
Parameters:

	id: This is the product id that you want to add to the shopping 
	cart.

Example:

	onclick="addToCart(1);"

	This example adds one item of prodict ID 1 to the shopping 
	cart. If the cart window has not been opened, it is opened
	at this time.	
------------------------------------------------------------------ */

var cartWnd;
var first = 1;
function addToCart(id)
{
	var url = "/cgi-bin/cart/cadd.pl?id=" + id + "&qty=1";
	var op = "toolbar=no,scrollbars=yes,width=" + cW + ",height=" + cH;
	cartWnd = window.open(url,'cart_window',op);
	if(first){
		cartWnd.moveTo(cX, cY);
		window.moveTo(pX, pY);
		window.resizeTo(pW, pH);
		first =0;
	}
}


/* -------------------------------------------------------------------------------------------------------
	
	Alternate form of adding products to the shopping cart. This method modifies the shopping cart
	cookie directly using JavaScript rather than sending a request to a CGI.

	Functions:
	addToCart2() => Adds the product to the cart
	removeItem() => Removes the product from the cart
	viewCart() => Displays the shopping cart
	
 -------------------------------------------------------------------------------------------------------*/
function addToCart2(id)
{
	// Add an item by setting it's value to 1
	var cstr = getCookie('cart',false);
	var val = parseCookie( cstr );
	if(val)
	{
		var carray = val;
	}
	else
	{
		var carray = new Array();
	}
	
	carray = setCookieItem(carray, id, '1');
	cstr = getCookieString(carray);
	var now = new Date();
	now.setTime(now.getTime() + 60 * 60 * 1000); // expires in one hour	
	setCookie('cart', cstr, false, now, '/');
}
function removeItem(id)
{
	// To remove an item, set it's value to 0.
	var cstr = getCookie('cart');
	var carray = parseCookie( cstr );
	carray = setCookieItem(carray, id, '0');
	cstr = getCookieString(carray);
	var now = new Date();
	now.setTime(now.getTime() + 60 * 60 * 1000); // expires in one hour	
	setCookie('cart', cstr,false, now, '/');
	
}
function viewCart()
{
	// This works just like the oringinal addToCart() function but it does not add a new item to the cart
	var url = "/cgi-bin/cart/cadd.pl";
	var op = "toolbar=no,scrollbars=yes,width=" + cW + ",height=" + cH;
	cartWnd = window.open(url,'cart_window',op);
	if(first){
		cartWnd.moveTo(cX, cY);
		window.moveTo(pX, pY);
		window.resizeTo(pW, pH);
		first =0;
	}
	
}
function selectMe(obj,id)
{
	
	if(obj.checked) // adding the item
	{
		addToCart2(id);
	}
	else // adding the item
	{
		removeItem(id);
	}
		
}
/* -------------------------------------------------------------------------------------------------------
	My JavaScript helpper functions. These parse the cookie data, set the value of a specific cookie
	and return a reassembled cookie string.
	
	Functions:
	parseCookie() => parses a cookie string and returns an array of cookie elements
	setCookieItem() => sets the value of a specific cookie item.
	getCookieString() => builds a cookie string from an array of cookie values.
	
-------------------------------------------------------------------------------------------------------*/
function parseCookie(str)
{
	if(str){
		return str.split('&');
	}
	return str;
}
function setCookieItem(carray, id, value)
{
	if(carray)
	{
		for(var i=0; i<carray.length; i=i+2)
		{1
			if(carray[i] == id)
			{
				carray[i+1] = value;
				return carray;
			}
		}
	}
	// the existing cookie item was not found, so add it new.
	carray.push(id);
	carray.push(value);
	return carray;
}
function getCookieString(carray)
{
	// join does a nasty thing and adds the delimiter to the beginning
	// of the string. This is not what we want.
	//return carray.join('&');
	var t;
	for(var i=0;i<carray.length; i++){
		if(!t) // first element
		{
			t = carray[i];
		}
		else // the rest of the elements
		{
			t = t + '&' + carray[i];
		}
	}
	return t;
}

/* -------------------------------------------------------------------------------------------------------

	'Low Leve' cookie functions. These perform the actual work of reading and writing cookies from
	the browser.
	
	 Public domain cookie code was taken from:
	 http://www.webreference.com/js/column8/functions.html
	 but which was orriginally written by Bill Dortch who's website and the orriginal reference is no 
	 longer on-line.
 
 -------------------------------------------------------------------------------------------------------*/
 
// name - name of the cookie
// value - value of the cookie
// esc - escape the value of the cookie (true or false)
// [expires] - expiration date of the cookie (defaults to end of current session)
// [path] - path for which the cookie is valid (defaults to path of calling document)
// [domain] - domain for which the cookie is valid (defaults to domain of calling document)
// [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
// * an argument defaults when it is assigned null as a placeholder
// * a null placeholder is not required for trailing omitted arguments
function setCookie(name, value, esc, expires, path, domain, secure) {
	
  var v2 = "";
  if(esc){
	  v2 = escape(value);
  }else{
	  v2 = value;
  }
	  
  var curCookie = name + "=" + v2 +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

// name - name of the desired cookie
// esc - escape the value of the cookie (true or false)
// * return string containing value of specified cookie or null if cookie does not exist
function getCookie(name,esc) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  if(esc){
	  return unescape(dc.substring(begin + prefix.length, end));
  }else{
	  return dc.substring(begin + prefix.length, end);
  }
}

// name - name of the cookie
// [path] - path of the cookie (must be same as path used to create cookie)
// [domain] - domain of the cookie (must be same as domain used to create cookie)
// * path and domain default if assigned null or omitted if no explicit argument proceeds
function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" + 
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"
function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}
