var host = 'http://street.volkrote.com';
//var host = 'http://localhost:8080/thestyleriot';

function addToBag(productID, color, size)
{
	setHost();
	new Ajax.Request(host + '/ajax/addToBag.jsp', { method: 'post',   parameters: {productID: productID, color: color, size: size}   }); 
	document.getElementById('header-checkout').style.display = 'block';
	document.getElementById('addToBagButtonWrapper').style.display = 'none';
	document.getElementById('addedToBagButtonWrapper').style.display = 'block';
	document.getElementById('addedNextSteps').style.display = 'block';

	incrementCounter(1);	
}

function removeFromBag(itemIndex)
{
	setHost();
	new Ajax.Request(host + '/ajax/removeFromBag.jsp', { method: 'post',   parameters: {indexOf: itemIndex}   });
	document.getElementById('bagItem' + itemIndex).style.display = 'none';

	incrementCounter(-1);	
}

function incrementCounter(count)
{
	var e = document.getElementById('checkoutItemCount');
	itemCount = parseInt(e.innerHTML);
	itemCount = itemCount + count;
	e.innerHTML = itemCount;
}

function setHost()
{
	var url = window.location.href;
	var http = url.split('//')[0];
	var nohttp = url.split('//')[1];
	if(nohttp.split('/')[0] == "localhost:8080")
	{
		host = http + "//" + nohttp.split('/')[0] + "/thestyleriot";
	}
	else
	{
		host = http + "//" + nohttp.split('/')[0];
	}
}

function toggleDiv(divID)
{
	var e = document.getElementById(divID);
	if(e.style.display == 'block')
		e.style.display = 'none';
	else
		e.style.display = 'block';
}

function applyDiscount(discountCode)
{
	setHost();
	//alert(discountCode);
	new Ajax.Request(host + '/ajax/applyDiscount.jsp', { method: 'post',   parameters: {discountCode: discountCode}, 
	onSuccess: function(transport) {
		updateForDiscount(transport.responseText);
	}
	});
}

function updateForDiscount(discountExtra)
{
	var discount = 0;

	var e = document.getElementById('discountRow');
	e.style.display = 'block';
	
	var f = document.getElementById('discountText');
	

	if(discountExtra.split(':')[1] > 0)
	{
		discount = discountExtra.split(':')[1];
		multiplier = (100 - discount) / 100;
		var origTotal = document.getElementById('shoppingTotal').innerHTML;
		origTotal = origTotal.replace("$", "");
		var newTotal = origTotal * multiplier;
		var discountText = document.getElementById('discountTotal');
		discountText.innerHTML = "$" + newTotal;
		f.innerHTML = "<font color=blue>* discount applied *</font>"
	} 
	else
	{
		f.innerHTML = "<font color=orange>* invalid code *</font>"
		discountText.innerHTML = "";
	}
	
	
}

