function showcard()
{
	var oCard = document.getElementById("CCVCard");
	if (oCard.style.display=="") {
		oCard.style.visibility = "hidden";
		oCard.style.display = "none";
	} else {
		oCard.style.visibility = "visible";
		oCard.style.display = "";
	}
}


function updateCart(baseURL, id)
{					 
	var productID = id; //$('productID').value;
	var caseqty = $('caseqty_' + id).value;
	var unitqty = $('unitqty_' + id).value;
	var caseprice = $('pricePerDozen_' + id).value;
	var unitprice = $('pricePerUnit_' + id).value;
	var updatedMsg = $('updatedMsg_' + id).value;
	
	var maxBottlesAllowed = $('maxBottlesAllowed_' + id).value;
	if (maxBottlesAllowed == '') maxBottlesAllowed = '1000000';
				
	//Check quantity to see if it's blank
	if (unitqty == '') {
		unitqty = '0';
		$('unitqty_' + id).value = '0';
	}
	if (caseqty == '') {
		caseqty = '0';
		$('caseqty_' + id).value = '0';
	}
				
	if (isNaN(unitqty) || isNaN(caseqty)) {
		//alert("Please enter only numbers in the bottle/cases quantity fields");
		var faceboxMsg = updatedMsg+'<div class="msg">Please enter only numbers in the bottle and case quantity fields</div>';
		jQuery.facebox(faceboxMsg,'itemaddedtocartMsg');
	}
	else if ( ((Number(caseqty)*12) + Number(unitqty)) > maxBottlesAllowed) {
		var faceboxMsg = updatedMsg+'<div class="msg">There is a limit of '+maxBottlesAllowed+' bottles placed on this item. Please adjust your order accordingly.</div>';
		jQuery.facebox(faceboxMsg,'itemaddedtocartMsg');
	}
	else
	{
		//alert("updateCart("+productID+" caseqty:"+caseqty+" unitqty:"+unitqty+")");
		var url = baseURL + 'cart/update';
		var pars = 'productID=' + productID + '&caseqty=' + caseqty + '&unitqty=' + unitqty;
		var target = 'errorMsg';
		
		var myAjax = new Ajax.Updater(target, url, {
			method: 'post',
			parameters: pars,
			onComplete: function(myResults) {			
			
				var origRowTotal = $('rowTotal_' + id).innerHTML;				
				var newRowTotal = (caseprice*caseqty)+(unitprice*unitqty);	
				var prevSubtotal = $('grandtotal').innerHTML;				
				var newSubtotal = Number(prevSubtotal) - Number(origRowTotal) + Number(newRowTotal);
				
				var origCartbarQty = $('cartbarQty').innerHTML;
				var origRowQty = $('rowQty_' + id).value;
				var newRowQty = Number(unitqty) + Number(caseqty);
				
				$('rowTotal_' + id).innerHTML = newRowTotal;
				$('grandtotal').innerHTML = newSubtotal;
				
				$('rowQty_' + id).value = newRowQty;
				$('cartbarTotal').innerHTML = newSubtotal;
				$('cartbarQty').innerHTML = Number(origCartbarQty) - Number(origRowQty) + Number(newRowQty);
								
				var faceboxMsg = updatedMsg+'<div class="msg">Your cart has been updated</div>'; 
				jQuery.facebox(faceboxMsg,'itemaddedtocartMsg');
			}
		 });
	}
}

function addToCart(baseURL, id)
{					 
	var productID = id; //$('productID').value;
	var caseqty = $('caseqty_' + id).value;
	var unitqty = $('unitqty_' + id).value;
	var caseprice = $('pricePerDozen_' + id).value;
	var unitprice = $('pricePerUnit_' + id).value;
	var addedMsg = $('addedMsg_' + id).value;
			
	var maxBottlesAllowed = $('maxBottlesAllowed_' + id).value;
	if (maxBottlesAllowed == '') maxBottlesAllowed = '1000000';
	
	//Check quantity to see if it's blank
	if (unitqty == '') {
		unitqty = '0';
		$('unitqty_' + id).value = '0';
	}
	if (caseqty == '') {
		caseqty = '0';
		$('caseqty_' + id).value = '0';
	}
	
	if (isNaN(unitqty) || isNaN(caseqty)) {
		//alert("Please enter only numbers in the bottle/cases quantity fields");
		var faceboxMsg = addedMsg+'<div class="msg">Please enter only numbers in the bottle and case quantity fields</div>';
		jQuery.facebox(faceboxMsg,'itemaddedtocartMsg');
	}
	else if ( ((Number(caseqty)*12) + Number(unitqty)) > maxBottlesAllowed) {
		var faceboxMsg = addedMsg+'<div class="msg">There is a limit of '+maxBottlesAllowed+' bottles placed on this item. Please adjust your order accordingly.</div>';
		jQuery.facebox(faceboxMsg,'itemaddedtocartMsg');
	}
	else
	{
		//alert("addToCart("+productID+" caseqty:"+caseqty+" unitqty:"+unitqty+")");
		var url = baseURL + 'cart/add';
		var pars = 'productID=' + productID + '&caseqty=' + caseqty + '&unitqty=' + unitqty;
		var target = 'errorMsg';
		
		var myAjax = new Ajax.Updater(target, url, {
			method: 'post',
			parameters: pars,
			onComplete: function(myResults) {
				
				var origRowTotal = $('rowTotal_' + id).innerHTML;				
				var newRowTotal = (caseprice*caseqty)+(unitprice*unitqty);	
				var prevSubtotal = $('grandtotal').innerHTML;				
				var newSubtotal = Number(prevSubtotal) - Number(origRowTotal) + Number(newRowTotal);
				
				var origCartbarQty = $('cartbarQty').innerHTML;
				var origRowQty = $('rowQty_' + id).value;
				var newRowQty = Number(unitqty) + Number(caseqty);
				
				$('rowTotal_' + id).innerHTML = newRowTotal;
				$('grandtotal').innerHTML = newSubtotal;
				
				$('rowQty_' + id).value = newRowQty;
				$('cartbarTotal').innerHTML = newSubtotal;
				$('cartbarQty').innerHTML = Number(origCartbarQty) - Number(origRowQty) + Number(newRowQty);
						
				
				if ((unitqty == '0') && (caseqty == '0')) {
					var faceboxMsg = addedMsg+'<div class="msg">Has been removed from your cart</div>'; 
				} else {
					var faceboxMsg = addedMsg+'<div class="msg">Has been added to your cart</div>'; 
				}
				jQuery.facebox(faceboxMsg,'itemaddedtocartMsg');
			}
		 });
	}
}



function removeCartItem(id,baseURL)
{
	var pars = 'id=' + id;
	var url = baseURL + 'cart/remove';
	var target = 'miniCart';
	
	var myAjax = new Ajax.Updater(target, url, {
		method: 'post',
		parameters: pars,
		onComplete: function(myResults){
			updateCartBar(baseURL);
			fetchMiniCart(baseURL);
		}
	 });
}

function updateShipping(cost)
{
	var subCost = $('subCost').innerHTML;
	
	//Remove $ if any
	subCost = subCost.replace('$','');
	
	$('shipCost').innerHTML = '$' + cost;
	$('shippingT').value = cost;
	
	//Calculate New Total
	var total = parseFloat(subCost) + parseFloat(cost);
	
	//Round to 2 decimal places
	total = total.toFixed(2);
	
	$('totalCost').innerHTML = '$' + total;
}

function swatchShow(swatch){
	
	var swatchBig = $(swatch + '_100'); 
	var swatchSmall = $(swatch + '_50'); 
	
	//Effect.Shrink(swatchSmall);
	$(swatchBig).show();
	
	new Effect.Morph(swatchBig,{
  		style: 'width:100px; height:100px;', // CSS Properties
  		duration: 0.2 // Core Effect properties
	});

	return false;
}

function swatchHide(swatch){
	
	var swatchBig = $(swatch + '_100'); 
	var swatchSmall = $(swatch + '_50'); 
	
	new Effect.Morph(swatchBig,{
  		style: 'width:0px; height:0px;', // CSS Properties
  		duration: 0.5 // Core Effect properties
	});
	
	//$(swatchBig).hide();
	
	return false;
}


function jumpToProduct(baseURL)
{
	var productID = $('productListings').value;	
	if(productID != '')
	{
		window.location = baseURL + 'admin/products/edit/' + productID;
	}
}

function updateStates(countryObj) {
		
	var stateList = document.getElementById('state');
	var statesHTML = '<option value="">Choose State:</option>';
	
	if (($(countryObj).value != 'USA') && ($(countryObj).value != 'Australia') && ($(countryObj).value != 'Canada')) {
		stateList.hide();
	} else {
		
		switch($(countryObj).value) 
		{
			case 'USA' : 	var statesArr = Array('AL','AK','AZ','AR','CA','CO','CT','DE','FL','GA','HI','ID','IL','IN','IA','KS','KY','LA','ME','MD','MA','MI','MN','MS','MO','MT','NE','NV','NH','NJ','NM','NY','NC','ND','OH','OK','OR','PA','RI','SC','SD','TN','TX','UT','VT','VA','WA','WV','WI','WY'); break;
			
			case 'Australia' : 	var statesArr = Array('ACT','NSW','NT','QLD','SA','TAS','VIC','WA'); break;
			
			case 'Canada' : 	var statesArr = Array('AB','BC','MB','NB','NL','NT','NS','NU','ON','PE','QC','SK','YT'); break;			
		}
		
		
		for (var i=0; i<statesArr.length; i++) {
			statesHTML += '<option value="' + statesArr[i] + '">' + statesArr[i] + '</option>';
		}
		stateList.innerHTML = statesHTML;
		stateList.show();
	}
}

