// basket javacript
function scConfirmDelete(itemType)
{
	var confirmMessage = removeFromCart;
	confirmMessage = confirmMessage.replace("\{item_type\}", itemType);
	return confirm(confirmMessage);
}

function scConfirmAllDelete(confirmMessage)
{
	return confirm(confirmMessage);
}

function scChangeListbox(cart_id, control, product_name, old_quantity, pbId)
{
	var newQty = control.options[control.selectedIndex].value;
	if(scConfirmChanges(cart_id, newQty, product_name, old_quantity)) {
		scUpdateQty(cart_id, newQty, pbId);
	} else {
		for (var i = 0; i < control.options.length; i++) {
			if (control.options[i].value == old_quantity) {
				control.options[i].selected = true;
			} else {
				control.options[i].selected = false;
			}
		}
	}
}

function scChangeTextbox(cart_id, control, product_name, old_quantity, pbId)
{
	var newQty = control.value;
	if(scConfirmChanges(cart_id, newQty, product_name, old_quantity)) {
		scUpdateQty(cart_id, newQty, pbId);
	}	else	{
		control.value = old_quantity;
	}
}

function scCheckChanges(e, cart_id, control, product_name, old_quantity)
{
	var key;
	if (window.event) {
		key = window.event.keyCode; //IE
	} else {
		key = e.which; //firefox
	}

	if (key == 13) {
		if (scConfirmChanges(cart_id, control.value, product_name, old_quantity)) {
			document.basket.submit();
		} else {
			control.value = old_quantity;
		}
		return false;
	} else {
		return true;
	}
}


function scConfirmChanges(cart_id, new_quantity, product_name, old_quantity)
{
	var isConfirm = false;
	var confirmMessage = "";
  if (new_quantity < 1)
  {
		confirmMessage = cartQtyZero;
		confirmMessage = confirmMessage.replace("\{old_quantity\}", old_quantity);
		confirmMessage = confirmMessage.replace("\{product_name\}", product_name);
		if (confirm(confirmMessage))
		{
			isConfirm = true;
		}
  }
  else
  {
		confirmMessage = alterCartQty;
		confirmMessage = confirmMessage.replace("\{old_quantity\}", old_quantity);
		confirmMessage = confirmMessage.replace("\{new_quantity\}", new_quantity);
		confirmMessage = confirmMessage.replace("\{product_name\}", product_name);
    if (confirm(confirmMessage))
		{
			isConfirm = true;
		}
  }
	return isConfirm;
}

function scUpdateQty(cartId, newQty, pbId)
{
	var cartParams = new Array();
	cartParams["blockParams"] = new Array();
	cartParams["blockParams"]["active_pb_id"] = pbId;
	var url = "cart_add.php?cart_id=" + encodeURIComponent(cartId) + "&cart=";
	if (newQty < 1) {
		url += "RM";
	} else {
		url += "QTY&new_quantity=" + encodeURIComponent(newQty);
	}
	callAjax(url, reloadCartBlocks, cartParams);
}


function scApplyCoupon(pbId)
{
	var couponObj = document.getElementById("sc_coupon_code");
	var couponCode= couponObj.value;
	var blockParams = new Array();
	blockParams["active_pb_id"] = pbId;
	blockParams["sc_coupon_code"] = couponCode;

	reloadBlock(pbId, blockParams);
}

function scRemoveCoupon(pbId, couponId)
{
	var blockParams = new Array();
	blockParams["active_pb_id"] = pbId;
	blockParams["sc_remove_coupon"] = couponId;

	reloadBlock(pbId, blockParams);
}

function scCheckShipping(pbId)
{
	// get objects
	var countryObj = document.getElementById("sc_country_id");
	var stateObj = document.getElementById("sc_state_id");
	var postalObj = document.getElementById("sc_postal_code");
	// show progress until we get response and diable controls
	countryObj.disabled = true;
	stateObj.disabled = true;
	postalObj.disabled = true;
	stopProgress = false;
	showShipingProcess();
	// get values
	var countryId = countryObj.options[countryObj.selectedIndex].value;
	var stateId = stateObj.options[stateObj.selectedIndex].value;
	var postalCode= postalObj.value;
	// pass values to block
	var blockParams = new Array();
	blockParams["active_pb_id"] = pbId;
	blockParams["sc_country_id"] = countryId;
	blockParams["sc_state_id"] = stateId;
	blockParams["sc_postal_code"] = postalCode;

	reloadBlock(pbId, blockParams);
}

var progressNumber = 0;
function showShipingProcess()
{
	var obj = document.getElementById("sc_shipping_cost");
	var prEl = new Array("\\", "|", "/", "&mdash;");
	if (obj && !stopProgress) {
		progressNumber++;
		var indexNumber = (progressNumber % 4);
		obj.innerHTML = prEl[indexNumber];

		setTimeout("showShipingProcess()", 100);
	}
}


function hideZipText()
{
	var postalObj = document.getElementById("sc_postal_code");
	var postalValue = postalObj.value;
	if (postalValue == "zipcode") {
		postalObj.value = "";
		postalObj.className = "field";
	}
}

function showZipText()
{
	var postalObj = document.getElementById("sc_postal_code");
	var postalValue = postalObj.value;
	if (postalValue == "") {
		postalObj.value = "zipcode";
		postalObj.className = "lightText";
	}
}

