function cShop(){
	this.toggleItem = shopToggleItem;
	this.applyDiscount = shopApplyDiscount;
	this.checkout = shopCheckout;
	this.toggleFormat = shopToggleFormat;
	this.notLoggedIn = shopNotLoggedIn;
	this.getNumItems = shopGetNumItems;

	this.numItems = null;
	this.discountAmount = "0";
	this.cdCost = 20;
	this.cdText = " [CD]";

	this.removeCol = 4;
	this.addCol = 3;
	this.colsDifference = 4; //columns to add to get from "CD Add" to "eBook Add"
	this.selectedBgCol = "#7c7";
	this.selectedFgCol = "#000";
	this.unselectedBgCol = "#fff";
	this.unselectedFgCol = "#3D3D3D";
}

function shopNotLoggedIn(ret)
{
	switch(ret){
		case "checkout":
			qstring = "?r=c";
			break;
		default:
			qstring = "";
			break;
	}
	window.location="/user/user.php"+qstring;
}

function shopToggleFormat(row,type,opt)
{
	td = row.getElementsByTagName('td');

	toggleCol1 = (type == "physical") ? this.addCol + this.colsDifference : this.addCol;
	toggleCol2 = (type == "physical") ? this.removeCol + this.colsDifference : this.removeCol;

	if(opt == "addable")
	{
		td[toggleCol1].getElementsByTagName('img')[0].style.visibility ="visible";
		td[toggleCol1].getElementsByTagName('span')[0].style.visibility = "visible";
		td[toggleCol2].getElementsByTagName('img')[0].style.visibility = "hidden";
		td[toggleCol2].getElementsByTagName('span')[0].style.visibility = "hidden";
	}
	else if(opt == "removeable")
	{
		td[toggleCol2].getElementsByTagName('img')[0].style.visibility = "visible";
		td[toggleCol2].getElementsByTagName('span')[0].style.visibility = "visible";
		td[toggleCol1].getElementsByTagName('img')[0].style.visibility ="hidden";
		td[toggleCol1].getElementsByTagName('span')[0].style.visibility = "hidden";
	}
}

function shopApplyDiscount(srcObj)
{
	this.discountAmount = parseInt(document.getElementById('shoppingDiscount').innerHTML);

	ao = new ajaxObject("/shop/shoppinghandler.php", "shoppingDiscount",function(disc){});

	ao.update("action=discount&discountcode=" + srcObj.parentNode.getElementsByTagName('input')[0].value);

	ao = new ajaxObject("/shop/shoppinghandler.php", "shoppingTotal");
	ao.update("action=total");

}

function shopGetNumItems()
{
	var numItems = 0;
	var table = document.getElementById("basket_shoppingcarttable");
	var tableTRs = table.getElementsByTagName("TR");

	for(var i = 0; i < tableTRs.length; i++){
		if(tableTRs[i].className == "itemRow"){
			if(tableTRs[i].title == "Selected"){
				numItems++;
			}
		}
	}
	return numItems;
}

function shopCheckout(){
	this.numItems = this.getNumItems();
	if(this.numItems > 0){
		time = new Date();
		tid = time.getTime();
		document.getElementById("shoppingtransactionid").value = tid;
		ao = new ajaxObject("/shop/shoppinghandler.php", "temp",function(){window.location="/shop/shoppinghandler.php?action=paypalcheckout&customid="+tid;});
		ao.update("action=checkout&tid=" + tid);
	}
}


function shopToggleItem(id,action,srcObj,format, detail)
{
	var srcObjTD 	= srcObj.parentNode;
	var srcObjTR	= srcObjTD.parentNode;
	var tdCells 	= srcObjTR.getElementsByTagName('td');

	var isBasketPage = document.getElementById('shoppingDiscount') ? 1 : 0;

	if(!format){format = "electronic";}

	ao = new ajaxObject("/shop/shoppinghandler.php", "", function(jsonv){
		var shopValues = eval("("+jsonv+")");
		document.getElementById('shoppingTotal').innerHTML=shopValues.totalprice;

		if(disc = document.getElementById('shoppingDiscount')){
			disc.innerHTML='$'+shopValues.discount;
		}
	});
	ao.update("action=" + action + "&id=" + id + "&format=" + format + "&detail=" + detail);

	if(format == 'electronic')
	{
		var physicalIsSelected = (tdCells[this.removeCol+this.colsDifference].getElementsByTagName('img')[0].style.visibility != "hidden") ? 1 : 0;

		if(action == "add")
		{
			this.toggleFormat(srcObjTR,"electronic","removeable");
			colorizeObject(srcObjTR, this.selectedBgCol, this.selectedFgCol);
			if(isBasketPage)
			{
				tdCells[this.addCol+this.colsDifference].getElementsByTagName('img')[0].style.visibility = "hidden";
				tdCells[this.addCol+this.colsDifference].getElementsByTagName('span')[0].style.visibility = "hidden";
				//console.log(				tdCells[this.addCol+this.colsDifference-1]);
				//tdCells[this.addCol+this.colsDifference-1].getElementsByTagName('span')[0].style.visibility = "hidden";
			}
		}
		else
		{
			this.toggleFormat(srcObjTR,"electronic","addable");
			if(!physicalIsSelected){
				colorizeObject(srcObjTR, this.unselectedBgCol, this.unselectedFgCol);
			}

			if(isBasketPage)
			{
				if(tdCells[this.addCol+this.colsDifference].getElementsByTagName('img')[0].style.visibility == "hidden" && tdCells[this.removeCol+3].getElementsByTagName('img')[0].style.visibility == "hidden" )
				{
					tdCells[this.addCol+this.colsDifference].getElementsByTagName('img')[0].style.visibility = "visible";
					tdCells[this.addCol+this.colsDifference].getElementsByTagName('span')[0].style.visibility = "visible";
					//tdCells[this.addCol+this.colsDifference-1].getElementsByTagName('span')[0].style.visibility = "visible";
				}
			}

		}
	}
	else if(format == 'physical')
	{
		var electronicIsSelected = (tdCells[this.removeCol].getElementsByTagName('img')[0].style.visibility != "hidden") ? 1 : 0;
		if(action == "add")
		{
			this.toggleFormat(srcObjTR,"physical","removeable");
			colorizeObject(srcObjTR, this.selectedBgCol, this.selectedFgCol);
			if(isBasketPage)
			{
				tdCells[this.addCol].getElementsByTagName('img')[0].style.visibility = "hidden";
				tdCells[this.addCol].getElementsByTagName('span')[0].style.visibility = "hidden";
				//tdCells[this.addCol-1].getElementsByTagName('span')[0].style.visibility = "hidden";
			}
		}
		else
		{
			this.toggleFormat(srcObjTR,"physical","addable");
			if(!electronicIsSelected){
				colorizeObject(srcObjTR, this.unselectedBgCol, this.unselectedFgCol);
			}
			if(isBasketPage)
			{
				if(tdCells[this.addCol].getElementsByTagName('img')[0].style.visibility == "hidden" && tdCells[this.removeCol].getElementsByTagName('img')[0].style.visibility == "hidden" )
				{
					tdCells[this.addCol].getElementsByTagName('img')[0].style.visibility = "visible";
					tdCells[this.addCol].getElementsByTagName('span')[0].style.visibility = "visible";
					//tdCells[this.addCol-1].getElementsByTagName('span')[0].style.visibility = "visible";
				}
			}
		}
	}else if(format == 'subscription')
	{
		var subscriptionIsSelected = (tdCells[this.removeCol].getElementsByTagName('img')[0].style.visibility != "hidden") ? 1 : 0;

		if(action=='add'){
			colorizeObject(srcObjTR, this.selectedBgCol, this.selectedFgCol);
		}else{
			colorizeObject(srcObjTR, this.unselectedBgCol, this.unselectedFgCol);
		}
		if(isBasketPage){
			var addVis = (action =='add') ? 'hidden' : 'visible';
			var remVis = (action =='add') ? 'visible' : 'hidden';

			tdCells[this.addCol].getElementsByTagName('img')[0].style.visibility = addVis;
			tdCells[this.addCol].getElementsByTagName('span')[0].style.visibility = addVis;

			tdCells[this.removeCol].getElementsByTagName('img')[0].style.visibility = remVis;
			tdCells[this.removeCol].getElementsByTagName('span')[0].style.visibility = remVis;
		}
	}

	//calculate cost
	/*currentValue = parseInt(tdCells[1].innerHTML.substr(1));

	if(action == "add")
	{
		srcObjTR.title="Selected";
		if(physicalIsSelected && electronicIsSelected)
		{
			newValue = (currentValue * 2) + this.cdCost;
		}
		else if (physicalIsSelected)
		{
			newValue = currentValue + this.cdCost;
		}
		else
		{
			newValue = currentValue;
		}
	}
	else
	{
		srcObjTR.title="Not selected";
		if(physicalIsSelected && electronicIsSelected)
		{
			newValue = (currentValue - this.cdCost) /2;
		}
		else if (physicalIsSelected)
		{
			newValue = currentValue - this.cdCost;
		}
		else
		{
			newValue = currentValue;
		}
	}*/
}





