function getCookieVal(offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if(endstr == -1)
	  endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function FixCookieDate(date) {
	var base = new Date(0);
	var skew = base.getTime();				      //dawn of Unix time - should be 0
	if(skew > 0)
	  date.setTime(date.getTime() - skew);			  //except for Macs - ahead of its time
}

function GetCookie(name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while(i < clen) {
		var j = i + alen;
		if(document.cookie.substring(i,j) == arg)
			return getCookieVal(j);
			i = document.cookie.indexOf(" ", i) + 1;
		if(i == 0) { break; }
	}
	return null;
}

function SetCookie(name, value, expires,path,domain,secure) {
	document.cookie = name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path :"") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
}

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";
	}
}
	

function CheckCookie()
{
	//if the cookie does not exist, create an empty cookie
	/*
	var basket = "";
	if( (GetCookie(cart_cookie_name) == null) || (GetCookie(cart_cookie_name) == "") ){
	//	var expdate = new Date();
	//	FixCookieDate(expdate);			//fix date for Macs
	//	expdate.setTime(expdate.getTime() + (24 * 60 * 60 * 1000));		//24 hours from now
		SetCookie(cart_cookie_name,"",expdate,"/",null,false);
		cart_count = 0;

	} else {
		basket = GetCookie(cart_cookie_name);

		if(cart_count == ""){
			var products = basket.split("|");
			cart_count = 0;
			for(q=0; q<products.length; q++)
			{
				var item = products[q].split(",");
				cart_count += parseInt(item[1]);
			}
		}
	}
	*/
	if(cart_count == '')
		cart_count = 0;
		
	document.cart.cartCount.value = cart_count;
	document.cart.cookie_value.value = basket;
}



function resizeIFrame(name)
{
	var adjust = 0;

	//This is only for IE and Opera (who thinks they are IE at times)
	//Without this, if the IFRAME is set to a height greater than 0...
	//Then IE reports this specified height as document.body.offsetHeight
	//Which should only be a NS setting and kills our condition below
	//By setting it to 0, it undefs the offsetHeight for IE and uses the correct condition

	if(navigator.appName.match("Internet Explorer") || window.opera)
		parent.document.getElementById(name).style.height = '0px';


	if(navigator.userAgent.substr(navigator.userAgent.indexOf("Firefox")).split("/")[1] >= 0.1
	&& navigator.userAgent.substr(navigator.userAgent.indexOf("Firefox")).split("/")[1] < 2.0)
		adjust = 16;


	var height = calcTotalScreenHeight();
		
	//alert("Current_height: "+parent.document.getElementById(name).style.height+" New Height: "+height);
	if(parent.document.getElementById(name))
		parent.document.getElementById(name).style.height = (height + adjust) + 'px';
}


// This function will tell you in pixels how long the document is
// regardless of browser being used
// Tested on...
//   IE       5.0 - 6.00.2900
//   Netscape 6.2 - 8.0.3.3
//   Firefox  1.0 - 1.5.4
//   Opera    7.0 - 9.00 Beta
//   Mozilla  1.7
//   Epiphany 1.4.8
function calcTotalScreenHeight(p_screen)
{
	var doc = new Object;
	if(p_screen){
		doc = parent.document;
	} else
		doc = document;

	var docElmnt = doc.body;
	//This is needed to correctly get the scrollHeight in Opera
	//This call did not work with the Opera 9.0 beta... so simply checking against if the browser is opera
	//if(document.compatMode && document.compatMode != "BackCompat"){
	if(window.opera)
		docElmnt = document.documentElement;

	var height = 0;

	//This should only be used for Gecko Based browers and not MSIE or Opera
	if(docElmnt.offsetHeight && !navigator.userAgent.match("MSIE") || (window.opera && navigator.userAgent.match("9.00"))){		// ns 6+
		height = docElmnt.offsetHeight;

	} else if(docElmnt.scrollHeight) {	// ie 5+
		height = docElmnt.scrollHeight;

		//ie 5.0
		if(docElmnt.offsetHeight && docElmnt.clientHeight && !window.opera)
			height += (docElmnt.offsetHeight - docElmnt.clientHeight);
	}

	return(height);
}


function calcTotalScreenWidth(p_screen)
{
	var doc = new Object;
	if(p_screen)
		doc = top.document;
	else
		doc = document;


	var docElmnt = doc.body;
	//This is needed to correctly get the scrollHeight in Opera
	//This call did not work with the Opera 9.0 beta... so simply checking against if the browser is opera
	//if(document.compatMode && document.compatMode != "BackCompat"){
	if(window.opera)
		docElmnt = doc.documentElement;


	var width = 0;
	//This should only be used for Gecko Based browers and not MSIE or Opera
	if(docElmnt.offsetWidth && !navigator.userAgent.match("MSIE") || (window.opera && navigator.userAgent.match("9.00")))		// ns 6+
		width = docElmnt.offsetWidth;
	else if(docElmnt.scrollWidth)	// ie 5+
		width = docElmnt.scrollWidth;


	return(width);
}




// This function will tell you how far you are scrolled down in pixels
// regardless of brower being used
function calcScrollTopOffset()
{
	var pos = 0;

	if(top.document.documentElement.scrollTop) {
		pos = top.document.documentElement.scrollTop;
	} else if(top.window.pageYOffset) {
		pos = top.window.pageYOffset;	
	} else if(top.document.body.scrollTop) {
		pos = top.document.body.scrollTop;
	}
	return(pos);
}

// This function will tell you how far you are scrolled down in pixels
// regardless of brower being used
function calcScrollLeftOffset(p_screen)
{
	var pos = 0;

	if(document.body.scrollLeft){
		if(p_screen)
			pos = top.document.body.scrollLeft();
		else
			pos = document.body.scrollLeft();
	} else if(window.pageXOffset) {
		if(p_screen)
			pos = top.window.pageXOffset();	
		else
			pos = window.pageXOffset();	
	}
	
	return(pos);
}

// This function determines the x coords needed to center
// an item in the middle of the screen, leaving equal padding all around
function calcCenterScreenXPos(itm_w)
{
	itm_w = parseInt(itm_w);

	//var pg_w = window.screen.availWidth;
	var pg_w = calcTotalScreenWidth(true);
	var pg_offset = calcScrollLeftOffset(true);
	pg_w = (pg_w - pg_offset);
	
	var x = 0;

	//alert("pg_w: "+pg_w+"; pg_offset: "+pg_offset+"; itm_w: "+itm_w);

	if(pg_w > itm_w)
		x = ((pg_w / 2) - (itm_w / 2) + pg_offset);

	//alert("new x: "+x);
	
	return(x);
}

// This function determines the y coords needed to center
// an item in the middle of the screen, leaving equal padding all around
function calcCenterScreenYPos(itm_h)
{
	itm_h = parseInt(itm_h);

	//var pg_h = window.screen.availHeight;
	var pg_h = calcTotalScreenHeight(true);
	if(pg_h > window.screen.availHeight)
		pg_h = window.screen.availHeight;

	var pg_offset = calcScrollTopOffset(true);
	pg_h = (pg_h - pg_offset);
	
	var y = 0;

	//alert("pg_h: "+pg_h+"; pg_offset: "+pg_offset+"; itm_h: "+itm_h);

	if(pg_h > itm_h)
		y = ((pg_h - itm_h) / 2) + pg_offset;

	//alert("new y: "+y);

	return(y);
}


// This function moves an item into a specified location
// This is done by absolutely positioning the item
// and moving it to a specified x,y coordinate for
// the upper left corner of the item
function absPosItem(item,x,y)
{
	item.style.position = "absolute";
	
	//alert("x: "+x+", y: "+y);
	
	item.style.top = y+'px';
	item.style.left = x+'px';
}


function centerItemOnScreen(item)
{
	absPosItem(item,calcCenterScreenXPos(item.style.width),calcCenterScreenYPos(item.style.height));
}



function toggleCart(btn)
{
	var c = document.getElementById('cart_items');
	var cs = document.getElementById('cart_summary');

	if(c.className == 'cart_hide'){
		c.className = 'cart_viz';
		cs.className = 'cart_summary_tbl_details';
		btn.value = "Hide Details";
	} else {
		c.className = 'cart_hide';
		cs.className = 'cart_summary_tbl';
		btn.value = "View Details";
	}
	resizeIFrame('cart_details');
}





var IFrameObj; //our View Cart IFrame creation object
function callToServer(form)
{
	if(!document.createElement)
		return true;

	IFrameObj = new Object;
	var URL = "/cgi-bin/store_bagger.cgi" + buildQueryString(form) + "&iframe=1";

	if(!IFrameObj && document.createElement){
		// create the IFrame and assign a reference to the
		// object to our global variable IFrameObj.
		// this will only happen the first time
		// callToServer() is called
		
		try {
			var tempIFrame = document.createElement('iframe');
			tempIFrame.setAttribute('id','cart_details');
			tempIFrame.setAttribute('name','cart_details');
			tempIFrame.style.border = "0px";
			tempIFrame.style.width = "0px";
			tempIFrame.style.height = "0px";

			if(document.getElementById('cart_holder'))
				IFrameObj = document.getElementById('cart_holder').appendChild(tempIFrame);
			else
				IFrameObj = document.body.appendChild(tempIFrame);
		
			if(document.frames){
				// this is for IE5 Mac, because it will only
				// allow access to the document object
				// of the IFrame if we access it through
				// the document.frames array
				IFrameObj = document.frames['cart_details'];
			}
		} catch (exception) {
			// This is for IE5 PC, which does not allow dynamic creation
			// and manipulation of an iframe object.  Instead we fake
			// it up by creating our own object

			var iframeHTML = "<iframe id='cart_details' name='cart_details' style='>";
			iframeHTML += "border: 0px; ";
			iframeHTML += "width: 0px; ";
			iframeHTML += "height: 0px; ";
			iframeHTML += "'></iframe>";

			if(document.getElementById('cart_holder'))
				document.getElementById('cart_holder').innerHTML = iframeHTML;
			else
				document.body.innerHTML += iframeHTML;

			IFrameObj = new Object();
			IFrameObj.document = new Object();
			IFrameObj.document.location = new Object();
			IFrameObj.document.location.iframe = document.getElementById('cart_details');
			IFrameObj.document.location.replace = function(location){
				this.iframe.location;
			}
		}

		if(navigator.userAgent.indexOf('Gecko') != -1 && !IFrameObj.contentDocument){
			// we have to give NS6 a fraction of a section
			// to recognize the new IFrame
			setTimeout('callToServer(form)',10);
			return false;
		}

	} else if(document.getElementById && document.getElementById('cart_details')){
		//alert("Loading here");
		IFrameObj = document.getElementById('cart_details');
	}


	var IFrameDoc = new Object;
	if(IFrameObj.contentDocument){
		// For NS6
		IFrameDoc = IFrameObj.contentDocument;
	} else if(IFrameObj.contentWindow) {
		// For IE5.5 and IE6
		IFrameDoc = IFrameObj.contentWindow.document;
	} else if(IFrameObj.src) {
		// For IE5
		IFrameObj.src = URL;
		return(false);
	} else {
		return(true);
	}

	if(IFrameDoc.location.replace)
		IFrameDoc.location.replace(URL);
	else if(IFrameDoc.src)
		IFrameDoc.src = URL;
	else
		IFrameDoc.location = URL;
		
	return(false);
}

function buildQueryString(form)
{
	var qs = '';
	for(var e=0; e<form.elements.length;e++){
		if(form.elements[e].name != '' && !form.elements[e].disabled && (form.elements[e].type == 'hidden' || form.elements[e].checked)){
			qs += (qs=='')?'?':'&';
			qs += form.elements[e].name+'='+escape(form.elements[e].value);
		}
	}
//	alert(qs);
	return(qs);
}

function openCDRequest(gs_id,pids,ml_ids)
{
	if(!document.getElementById('cd_req_form_div'))
	{
		var base_height = 350;
		var url = "http://www.trackntrade.com/cgi-bin/processCDRequest.cgi?gs_id="+gs_id;
		//var url = "http://shop.geckosoftware.nrussell.webdev.gecko/cgi-bin/processCDRequest.cgi?gs_id="+gs_id;

		var pid_list = new Array;
		pid_list = pids.split(',');
		for(var a=0; a<pid_list.length; a++){
			url += "&pid="+pid_list[a];
			base_height += 20;
		}

		if(ml_ids != undefined)
		{
			base_height += 40;
			var ml_id_list = new Array;
			ml_id_list = ml_ids.split(',');
			for(var a=0; a<ml_id_list.length; a++){
				url += "&ml_id="+ml_id_list[a];
				base_height += 20;
			}
		}


		var div_holder = document.createElement("div");
		div_holder.setAttribute("id","cd_req_form_div");
		div_holder.setAttribute("name","cd_req_form_div");

		var title_bar = document.createElement("div");
		title_bar.setAttribute("name","cd_req_form_title");
		if(navigator.userAgent.match(/MSIE/))
			title_bar.setAttribute("className","cd_req_title_bar");
		else
			title_bar.setAttribute("class","cd_req_title_bar");

		var title_bar_txt = document.createElement("div");
		title_bar_txt.setAttribute("name","cd_req_form_title");
		if(navigator.userAgent.match(/MSIE/))
			title_bar_txt.setAttribute("className","cd_req_title");
		else
			title_bar_txt.setAttribute("class","cd_req_title");
		
		title_bar_txt.appendChild(document.createTextNode("Free CD Request Form"));
		title_bar.appendChild(title_bar_txt);

		var close_btn = document.createElement("button");
		close_btn.setAttribute("id","cd_req_form_close");
		close_btn.setAttribute("name","cd_req_form_close");
		if(navigator.userAgent.match(/MSIE/))
			close_btn.setAttribute("className","cd_req_close_btn");
		else
			close_btn.setAttribute("class","cd_req_close_btn");
			
		if(close_btn.attachEvent)
			close_btn.attachEvent("onclick",hideCDReqForm);
		else
			close_btn.setAttribute("onClick","document.getElementById('cd_req_form_div').className='cart_hide'");

		close_btn.appendChild(document.createTextNode("X"));
		title_bar.appendChild(close_btn);
		
		div_holder.appendChild(title_bar);
	
		var ifrm = document.createElement("iframe");
		ifrm.setAttribute("id","cd_req_form");
		ifrm.setAttribute("name","cd_req_form");
		ifrm.setAttribute("frameBorder","0");
		ifrm.setAttribute("scrolling","no");
		ifrm.setAttribute("allowTransparency","true");


		if(navigator.userAgent.match(/MSIE/) && !navigator.userAgent.match(/Opera/))
		{
			div_holder.appendChild(ifrm);
			document.body.appendChild(div_holder);

			var ifrmObj = document.frames['cd_req_form'];
			ifrmObj.location = url;

			document.getElementById("cd_req_form").style.height = (base_height+51)+"px";
			document.getElementById("cd_req_form").style.borderWidth = "0px";
		} else {
			ifrm.setAttribute("src", url);
		
			div_holder.appendChild(ifrm);
			document.body.appendChild(div_holder);
			
			document.getElementById("cd_req_form").style.height = (base_height-3)+"px";
		}
		document.getElementById("cd_req_form_div").style.width = "297px";
		document.getElementById("cd_req_form").style.width = "297px";
		
	} else {
		document.getElementById('cd_req_form_div').className='cart_viz';
	}
	//centerItemOnScreen(document.getElementById("cd_req_form"));
	absPosItem(document.getElementById("cd_req_form_div"),calcCenterScreenXPos(document.getElementById("cd_req_form").style.width),(100 + calcScrollTopOffset(true)));
}

function hideCDReqForm()
{
	document.getElementById('cd_req_form_div').className='cart_hide';
}
