function handleError() {
	console.log(textStatus);
	console.log(errorThrown);
	if(textStatus=="parsererror") {
		showAlert();
	} else {
		try {
			var json = JSON.parse(XMLHttpRequest.responseText);
			if(checkAndProcessErrors(json, e)) {
				console.log(json);
			} else {
				showAlert();
			}
		} catch (error) {
			console.log(json);
		}
	}
}
function checkAndProcessErrors(json, e) {
	if(json.actionErrors && json.actionErrors.length > 0) {
		highlightFieldError(json.actionErrors + ":" + json.actionErrors.length, e.target);
	} else if(json.fieldErrors.quantity) {
		highlightFieldError(json.fieldErrors.quantity, e.target);
	} else {
		return false;
	}

	return true;
}

function highlightFieldError(message, target) {
	alert(message);
	$(target).css({'background-color': '#FF99CC'});
}

function showAlert() {
	console.log(json);
	alert("Problem submitting quantity change, please use the 'update' button at the bottom of the page");
}

function currency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
		num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
		cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+
		num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + "£" + num + '.' + cents);
}
function currencyNoPound(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
		num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
		cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+
		num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + num + '.' + cents);
}
$(document).ready(function(){
	// Basket Overlay
	// display basket overlay if javascript is enabled
	$("#myBasket").css("display", "block");
    $("#closeBasketOverlay").click(function(){
			$("#myBasket").css("display", "none");
    });

	$("#myZoomProduct").css("display", "block");
//    $("#closeZoomProductOverlay").click(function(){
//			$("#myZoomProduct").css("display", "none");
//    });

	$("#myLargeProduct").css("display", "block");
//    $("#closeLargeProductOverlay").click(function(){
//			$("#myLargeProduct").css("display", "none");
//    });

    window.setTimeout(function(){
 	  $("#myBasket").css("display", "none");
    }, 6000);

    //print feature
    $(".print").bind("click", function(e){
    	e.preventDefault();
    	window.print();
    	return false;
    });


    //add to favourites
    $(".favourites").bind("click", function(e){
        e.preventDefault();
    	var url = this.href;
        var title = 'Mole Valley Farmers Homepage';
   	     if (window.sidebar) { // Mozilla Firefox Bookmark
             window.sidebar.addPanel(title, url, "");
         } else if( window.external ) { // IE Favorite
             window.external.AddFavorite( url, title);
         } else if(window.opera) { // Opera 7+
             return false; // do nothing - the rel="sidebar" should do the trick
         } else { // for Safari, Konq etc - browsers who do not support bookmarking scripts (that i could find anyway)
              alert('Unfortunately, this browser does not support the requested action,'
              + ' please bookmark this page manually.');
         }
    });

    //Home Page Rotating promotional images
    $('#homeHighlightLeft').cycle({
    	fx: 'fade',
        speed: 'fast',
        timeout: 5000, // 5 second delay between each promotion
        delay:  -2000,
        pause: true // true to enable "pause on hover"
	});

});

 /**
 * Opens popup.
 */
 function openPopup(url, name, h, w) {

  w += 32;
  h += 96;
  wleft = (screen.width - w) / 2;
  wtop = (screen.height - h) / 2;
  if (wleft < 0) {
    w = screen.width;
    wleft = 0;
  }
  if (wtop < 0) {
    h = screen.height;
    wtop = 0;
  }

  var opts = "height="+h+",width="+w+",left="+wleft+",top="+wtop+",location=no,menubar=no,status=no,toolbar=no,scrollbars=yes,resizable=yes";
  newwindow=window.open(url, "MoleValley", opts);
  newwindow.resizeTo(w, h);
  newwindow.moveTo(wleft, wtop);
  newwindow.focus();
  return false;
};