function updatePostcode(code) {	
	getStores_byPostcode(code, code, 0);
}

//function to retrieve stores close to a postcode
function getStores_byPostcode(pdid, pcode_element, reload) {
	if(reload == 'undefined'){reload = 0;}
	var postcode = "#postcodebox_" + pcode_element;
	var dropdown = "#Store_dropdown_" + pcode_element;
	var quantity = $('#quantitybox_' + pcode_element).val();
	$('.newListSelected.Store_dropdown_' + pcode_element).remove();
	var removeDD = ".newListSelected.Store_dropdown_"+ pcode_element;
	$(dropdown).html('<option>Loading stores...</option>');
	$(dropdown).sSelect();
	$.post('/default.aspx?action=checkstore_postcode&pdid=' + pdid + '&pcode=' + $(postcode).val() + '&quantity=' + quantity, '', function(data) {
	  //on success
	  $(removeDD).remove();
	  data.replace('&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;', '');
	  $(dropdown).html(data);
	  $(dropdown).html($(dropdown).html());
	  $(dropdown).sSelect();
	  });
	  }

//function to retrieve stores near a postcode, specific to the cart
function getStores_byPostcode_Cart(pdid, pcode_element) {
	var pcode_value = $("#postcodebox_" + pcode_element).attr("value");
	var dropdown = "Store_dropdown_Cart_" + pcode_element;
	var quantity = 'quantitybox_' + pcode_element;
	var quantity_value = $('#' + quantity).val();
	$('.newListSelected.Store_dropdown_Cart_' + pcode_element).remove();
	var removeDD = ".newListSelected.Store_dropdown_Cart_"+ pcode_element;
	$('#' + dropdown).html('<option>Loading stores...</option>');
	$('#' + dropdown).sSelect();
	$.post('/default.aspx?action=checkstore_postcode&pdid=' + pdid + '&pcode=' + pcode_value + '&quantity=' + quantity_value, '', function(data) {
		//on success
		$(removeDD).remove();
		data.replace('&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;', '');
		$("#" + dropdown).html(data);
		$("#" + dropdown).html($("#" + dropdown).html());
		$('#' + dropdown).sSelect();
	});
}

function getStores_byState(pdid, state_element, reload) {
	if(reload == 'undefined'){reload = 0;}
	var state = "#statebox_" + state_element;
	var dropdown = "#Store_dropdown_" + state_element;
	var quantity = $('#quantitybox_' + state_element).val();
	$('.newListSelected.Store_dropdown_' + state_element).remove();
	var removeDD = ".newListSelected.Store_dropdown_"+ state_element;
	$(dropdown).html('<option>Loading stores...</option>');
	$(dropdown).sSelect();
	$.post('/default.aspx?action=checkstore_state&id=' + pdid + '&state=' + $(state).val() + '&quantity=' + quantity, '', function(data) {
			//on success
		$(removeDD).remove();
		data.replace('&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;', '');
		$(dropdown).html(data);
		$(dropdown).html($(dropdown).html());
		$(dropdown).sSelect();
	});
}	

function getStores_byState_Cart(pdid, state_element, stateinput) {
	var state = stateinput;
	var dropdown = "Store_dropdown_Cart_" + state_element;
	var quantity = $('#quantitybox_' + state_element).val();
	$('.newListSelected.Store_dropdown_Cart_' + state_element).remove();
	var removeDD = ".newListSelected.Store_dropdown_Cart_"+ state_element;
	$('#' + dropdown).html('<option>Loading stores...</option>');
	$('#' + dropdown).sSelect();
	$.post('/default.aspx?action=checkstore_state&id=' + pdid + '&state=' + state + '&quantity=' + quantity, '', function(data) {
		//on success
		$(removeDD).remove();
		data.replace('&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;', '');
		$('#' + dropdown).html(data);
		$("#" + dropdown).html($("#" + dropdown).html());
		$('#' + dropdown).sSelect();
	});
}

function addToCartFromSearchPopup(URL, frm, controlID, option, store, id) {
	//process the actual adding to the cart
	//collect the parameters:
	//if the type is album or track, remove all other track or album form fields other than the one identified by ident
	data = $("#" + frm).serialize();

	$("#" + controlID).slideToggle("slow");

	$.post(URL, data, function(data) {
		$.post('/default.aspx?Z=C&action=setinstore&id=' + id + '&option=' + option, '', function(data) {
			$.post('/default.aspx?Z=C&action=setpickupstore&id=' + id + '&option=' + store + '&setall=' + '0', '', function(data) {
				$("#" + controlID).removeAttr("onclick");
				$("#" + controlID).removeAttr('href'); //remove the javascript addToCart function to prevent adding more than one track/album
				$("#" + controlID).find("input").attr('disabled', 'disabled');
				$("#" + controlID).removeClass();
				$("#" + controlID).addClass("availability_cartbutton");
				$("#" + controlID).find("input").attr("src", "/JB/images/in_cart.gif");
				$("#" + controlID).find("input").attr("title", "In Cart");
				$("#" + controlID).slideToggle("slow");
				$("#" + controlID).next().removeClass("add_to_cart_small");//replace classes
				$("#" + controlID).next().addClass("in_cart_small");
				displayMiniCart();
			});
		});

	});
	return false;
}

function addToCart_ListPopup(URL, frm,type,ident,controlID){
	//process the actual adding to the cart
	//if the type is album or track, remove all other track or album form fields other than the one identified by ident

	var data = $("#" + frm + " :input[value]")
			.not("#" + frm + " :input[name=DDL_track_APN][value!=" + ident + "]")
			.not("#" + frm + " :input[name=DDL_album_APN][value!=" + ident + "]")
	//the additional add album button needs further filtering
	if(type=='track'){
		data = data.not("#frmMain_AddAlbum :input[value]");
	}
	data = data.serialize();

	$("#" + controlID).slideToggle("slow");
	$.post(URL, data,function(data) {
		//on success
		//disable link, if already added to cart
		$("#" + controlID).removeAttr("href");
		$("#" + controlID).removeAttr("onclick");
		$("#" + controlID).find("input").attr('disabled', 'disabled');
		$("#" + controlID).removeClass();
		$("#" + controlID).addClass("availability_cartbutton");
		$("#" + controlID).find("input").attr("src", "/JB/images/in_cart.gif");
		$("#" + controlID).find("input").attr("title", "In Cart");
		$("#" + controlID).slideToggle("slow");
		$("#" + controlID).next().removeClass("add_to_cart_small");//replace classes
		$("#" + controlID).next().addClass("in_cart_small");
		
		//there are two 'Add Album To Cart' buttons, make sure the other one reflects the current status
		if(type=='album'){
			if(frm == 'frmMain_mp3'){
				$("#frmMain_AddAlbum .add_album").attr('href','javascript:void(0)'); //remove the javascript addToCart function to prevent adding more than one track/album
				$("#frmMain_AddAlbum .add_album").text('In Cart').hide();//new text, first hidden
				$("#frmMain_AddAlbum .add_album").text('In Cart').slideToggle("slow");//display new text with animation
			} else {
				$("#frmMain_mp3 a").attr('href','javascript:void(0)'); //remove the javascript addToCart function to prevent adding more than one track/album
				$("#frmMain_mp3 a").text('In Cart').hide();//new text, first hidden
				$("#frmMain_mp3 a").text('In Cart').slideToggle("slow");//display new text with animation								
			}
		}
	displayMiniCart();
});

}

function setStore(id, store, setall) {		
	//work out if to set pickup status for all items or just one
	var option = 5;
	if (setall == 1) {option = 3;}
	else if (setall == 0) {option = 1;}
	
	$.post('/default.aspx?Z=C&action=setinstore&id=' + id + '&option=' + option + '&newQty=-1', '', function(data) {
		$.post('/default.aspx?Z=C&action=setpickupstore&id=' + id + '&option=' + store + '&setall=' + setall, '', function(data) {
			location.reload(true);
		});
	});
	
}

function setStoreNoReload(id, store, setall, myUrl, newQty) {		
	//work out if to set pickup status for all items or just one
	var option = 5;
	myUrl = myUrl || "";
	newQty = newQty || -1;
	if (setall == 1) {option = 3;}
	else if (setall == 0) {option = 1;}
	
	$.post('/default.aspx?Z=C&action=setinstore&id=' + id + '&option=' + option + '&newQty=' + newQty, '', function(data) {
		$.post('/default.aspx?Z=C&action=setpickupstore&id=' + id + '&option=' + store + '&setall=' + setall, '', function(data) {
			if(myUrl.length != 0){location.href = myUrl;}
		});
	});
	
}

function setStoreDropdown(id, store, setall) {		
	//work out if to set pickup status for all items or just one
	var option = 5;
	if (setall == 1) {option = 3;}
	else if (setall == 0) {option = 1;}
	$('#btnCartGoCheckOut').attr('disabled', 'disabled');
	$('#btnCartUpdate').attr('disabled', 'disabled');
	$('#btnPaypalCheckout').attr('href', '#');
	$.post('/default.aspx?Z=C&action=setinstore&id=' + id + '&option=' + option + '&newQty=-1', '', function(data) {
		$.post('/default.aspx?Z=C&action=setpickupstore&id=' + id + '&option=' + store + '&setall=' + setall, '', function(data) {
			$('#btnCartGoCheckOut').removeAttr('disabled');
			$('#btnCartUpdate').removeAttr('disabled');
			$('#btnPaypalCheckout').attr('href', '/default.aspx?Z=C&action=pp_express');
		});
	});
	
}

function setPickupStatus(id, option, reload, newQty) {	
	if(reload == 'undefined'){reload = 0;}			
	newQty = newQty || -1;
	$.post('/default.aspx?Z=C&action=setinstore&id=' + id + '&option=' + option + '&newQty=' + newQty, '', function(data) {
		//alert(/%ERROR_SETPICKUPSTORESTATUS%/.match(data) + ' ' + /%ERROR_SETINSTORESTATUS%/.match(data));
		//if((data.indexOf('ERROR_SETPICKUPSTORESTATUS') == -1) || (data.indexOf('ERROR_SETINSTORESTATUS') == -1)) {
			location.href='/default.aspx?Z=C&action=' + '<xsl:value-of select="//QUERYSTRING/ACTION"/>';
		//}
	});
}
