function popup(url, width, height, scroll, resize) 
{
	newWindow = window.open(url,"newWindow",'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars='+scroll+',resizable='+resize+',left=80,width=' +width+ ',height=' +height+ ',top=50')
}
 
function fnImageNotAvailable(i)
{
	if (image[i] != undefined)
	{
		image[i].src="/web_images/bd/not_available.gif";
	}
	else
	{	
		image.src="/web_images/bd/not_available.gif";
	}
}

function getObject(objName)
{	
	if (document.all)
	{
		return document.all(objName);
	}
	else if (!document.all && document.getElementById)
	{
		return document.getElementById(objName);
	}	
	else if (document.layers)
	{
		return document.layers(objName);		
	}
	else
	{		
		return null;
	}
}

function addQtyToCart(sku, objName)
{
	var args = "sku=" + String(sku) + "&Q" + String(sku) + "=";
	var url = "http://www.bookdepot.ca/store/xt_additems_qty.asp";
	var qtyAdded = 0;
	var bSignedIn = isSignedIn();
	var response = "";
	var obj = false;
	var qty = 0;
		
	qSku = getObject(objName);
	if (qSku) {	
		qty = Math.round(qSku.value);
		if (isNaN(qty) || (qty == 0)) { qty = 1; }
		args = args + String(qty);
		qSku.value = ''; 
	}		
	
	// user must be logged in to add to cart
	if (!bSignedIn)
	{
		alert("Please sign in before adding items to your cart.");
		return false;
	}
	if (isNaN(qty))
	{
		alert("Quantity entered is not a valid whole number.");
		return false;
	}
	if (qty==0)
	{
		alert("Quantity to be added must be greater than 0.");
		return false;
	}
	
	response = request(url, args);
	if (response)
	{
		qtyAdded = response;		
		updateCartInfo('spanCookie');
		obj = getObject('spanQ'+String(sku));
		if (obj) 
		{ 
			var temp = qtyAdded.split("|");
			fadeText(obj, String(temp[0]) + " items added to cart. Subtotal: $" + String(temp[1]), 16, 1);
		}		
	}
}

var xmlHttp = false;
function request(url, args)
{
	var argLength = 0;

	// indicate to page the no redirect is needed
	xmlHttp = false;
	url = url + "?noRedirect=true";

	// code for Mozilla, etc.
	if (window.XMLHttpRequest)
  	{
		xmlHttp = new XMLHttpRequest();
  	}
	// code for IE
	else if (window.ActiveXObject)
  	{
  		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  	}

	if (xmlHttp)
	{
		if (args != null) {
			argLength = args.length;
		}
			
		xmlHttp.onreadystatechange = xmlHttpChange;
		xmlHttp.open("POST", url, false);
		xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xmlHttp.setRequestHeader("Content-Length", argLength);
        xmlHttp.setRequestHeader("Connection", "close");		
		xmlHttp.send(args);
		
		return xmlHttp.responseText;			
	}
	else 
	{
		alert("Error (1): Error performing request.\nYour security settings may be preventing us.");
		return null;
	}	
}

function xmlHttpChange()
{
	// if xmlhttp shows "loaded"
	if (xmlHttp.readyState==4)
  	{
  		// if "OK"
  		if (xmlHttp.status==200)
    	{
    		return true;
    	}
		else
		{
			alert("Error (2): Error performing request.\nYour security settings may be preventing us.");
		}
  	}
}

function updateCartInfo(objName)
{
	var temp = "";
	var nItems = "";
	var subTotal = "";
	var vals = getCookie("cart").split("&");
	var obj = false;
	
	temp = vals[1].split("=");
	nItems = temp[1];
	temp = vals[0].split("=");
	subTotal = temp[1];
	
	obj = getObject(objName);
	if (obj) 
	{ 
		obj.innerHTML = '<a href="/store/basket.asp" class="staticSubHdr">Cart</a>: $' + String(subTotal) + " Items: " + String(nItems);
	}
}

function isSignedIn()
{
	var session = getCookie("Session");
	
	if (session == null)
	{		
		return false;	
	}
	else if (session.indexOf("authenticated_user=True") == -1)
	{
		return false;
	}
	return true;
}

function getCookie(NameOfCookie)
{
	if (document.cookie.length > 0)
	{
		begin = document.cookie.indexOf(NameOfCookie+"=");
		if (begin != -1)
		{
			begin += NameOfCookie.length+1;
			end = document.cookie.indexOf(";", begin);
			if (end == -1) end = document.cookie.length;
			return unescape(document.cookie.substring(begin, end));
		}
		return null;
	}
}

function fadeText(obj, msg, step, fade)
{
	var delay = 250;
	var hex = '0123456789ABCDEF';
	var tFade;	
	var i = hex.substr((step - 1), 1)
	var color = '#' + String(i) + String(i) + String(i) + String(i) + String(i) + String(i);
	
	switch (fade)
	{
		case 1:
			obj.innerHTML = '<font style="font-size: x-small;" color="' + color + '">' + String(msg) + '</font>';
			tFade = setTimeout(function() { fadeText(obj, msg, step, fade); }, delay);
			if (step == 1) { fade = 2; } else { step -= 1; }
			break;
			
		case 2:			
			obj.innerHTML = '<font style="font-size: x-small;" color="' + color + '">' + String(msg) + '</font>';
			tFade = setTimeout(function() { fadeText(obj, msg, step, fade); }, delay);
			if (step == 16) { fade = 3; } else { step += 1; }
			break;

		default:
			clearTimeout(tFade);
			obj.innerHTML = '';	
			break;
	}
}

function getCartSummary2()
{
	var url = "http://www.bookdepot.ca/store/include/cart_summary.asp";
	var response = "";
	var bSignedIn = isSignedIn();

	// user must be logged in to add to cart	
	if (!bSignedIn)	{ return false;	}
		
	response = request(url);
	if (response)
	{
		document.all("cartSummary").title = response;
	}
}

function getCartSummary(objName)
{
	var url = "http://www.bookdepot.ca/store/include/cart_summary.asp";
	var response = "";
	var bSignedIn = isSignedIn();
	var obj = false;
	var obj2 = false;

	// user must be logged in to add to cart	
	if (!bSignedIn)	{ return false;	}
		
	response = request(url);
	if (response)
	{
		obj = getObject(objName);
		obj2 = getObject('divSort');
		if (obj) 
		{ 
			obj.innerHTML = response;
			obj.style.visibility = 'visible';
			if (obj2)
			{
				obj2[0].style.visibility = 'hidden';	// sort boxes near top of page
				obj2[1].style.visibility = 'hidden';	// sort boxes near bottom of page
			}
		}
	}
}

function hideCartSummary(objName) 
{
	var obj = false;
	var obj2 = false;
	
	obj = getObject(objName);
	obj2 = getObject('divSort');
	if (obj) 
	{ 
		obj.style.visibility = 'hidden';
		if (obj2)
		{
			obj2[0].style.visibility = 'visible';	// sort boxes near top of page
			obj2[1].style.visibility = 'visible';	// sort boxes near bottom of page
		}
	}
}
