var inputVal,sellVal,itemCode;

function constructAddToShoppingCartAJAX(oriObject) {
  //remember the rowID because when the AJAX call returns, 
  //it must be able to find the label
	basketID = rowID;
	
  qtyRow = document.getElementById("qty_" + rowID);
  itemRow = document.getElementById("row_" + rowID);
  custIdRow = getEl("custId_" + rowID);
  
  if (qtyRow == null || itemRow == null) {	
	  basketID = rowOrderID;
  
		qtyRow = document.getElementById("orderrow_qty_" + rowOrderID);
		itemRow = document.getElementById("orderrow_" + rowOrderID);
	}
	
	if (qtyRow != null && itemRow != null && oriObject.value > '') {	
    inputVal = oriObject.value.valueOf() * 1;
    sellVal = qtyRow.getAttribute("name").valueOf() * 1;
    itemCode = itemRow.getAttribute("name");
    
    if (inputVal == 0) {
      sendAddToShoppingCartAJAX(itemCode, inputVal);
      return true;
    }

    if (inputVal < 20 || (inputVal >= sellVal && inputVal % sellVal == 0)) {
      //add the value to the cart      
      sendAddToShoppingCartAJAX(itemCode, inputVal);
      return true;
    }
    else {
		if (sellVal == 3000) {
			if (inputVal % 250 == 0) {
				message = "The standard order quantity for custom printed labels is 3,000 and multiples thereof. \r\n"+
					"However special quantity multiples exist for non-pictorial labels such as \r\n"+
					"Descriptive Labels (250) and Point of Sale items such as posters and wobblers (500)";
				alert(message);
				sendAddToShoppingCartAJAX(itemCode, inputVal);
				return true;
			}
			else {
				message = "The standard order quantity for custom printed labels is 3,000 and multiples thereof. \r\n"+
					"However special quantity multiples exist for non-pictorial labels such as \r\n"+
					"Descriptive Labels (250) and Point of Sale items such as posters and wobblers (500) \r\n"+
					" \r\nPlease re-enter the quantity.";
			}
		}
		else {
			message = "These items must be ordered in multiples of " + sellVal +
				". \r\nPlease re-enter the quantity.";
		}
      
      alert(message);
      qtyRow.innerHTML = "";
      oriObject.value = "";
      return false;
    }
  }
  return false;
}

function validateAddToShoppingCartCallback(responseStr) {
  var basketImg = document.getElementById("basket_" + history_rowid);
  
  var pos = responseStr.indexOf("|");
  var contents = "";
  if (pos > 0) {
	 contents = responseStr.substring(pos+1);
    responseStr = responseStr.substring(0, pos-1);
  }
  
  if (responseStr == "insert" || responseStr == "update") {
    basketImg.innerHTML = "<img src=\"n_images/cart.gif\" style=\"padding: 0; margin: 0\"  />";
  }
  else if (responseStr == "delete") {
    if (getEl("qty_"+rowID) != null) {
      var orderQty = getEl("qty_"+rowID).innerText;
      if (orderQty != null && orderQty*1 > 0) {
        return;
      }
      
      basketImg.innerHTML = "&nbsp;";
    }
    
    if (document.getElementById("gotopage").value == "5" ||
				document.getElementById("gotopage").value == "7" ||
				document.getElementById("gotopage").value == "12" ||
				document.getElementById("gotopage").value == "13") {
			stayInCurrentPage();
		}
  }
  else if (responseStr == "backorder") {
	var bdetaildiv = getBackorderDetailDIV();
	var visible = bdetaildiv.style.visibility;
	bdetaildiv.innerHTML = contents;
	
  if (visible == "hidden") {
		if (bDetailDivVisible()) showDetail(false);
		 if (bNotesVisible()) showNotes(false);
			if (bPreferenceVisible()) showPreference(false);
			if (bLabelTypeVisible()) showLabelType(false);
			if (bImageTypeVisible()) showImageType(false);
		 if (bFavouriteDivVisible) showFavourite(false);
		 if (bPriceListDivVisible()) showPrice(false);
		 
		 showBackorderDetail(true);
	 }
  }
  else if (responseStr.indexOf("error:") == 0) {
    alert(responseStr.substr(6));
    getEl("qty_"+rowID).innerHTML = "";
  }
  
  var qtyObj = document.getElementById("txtfldQty");
  qtyObj.value = "";
}


function bBackorderDivVisible() {
  var backorderdiv = getBackorderDetailDIV();
  if (backorderdiv != null) {
    var visible = backorderdiv.style.visibility;
    return (visible == "visible") ? true : false;
  }
  else {
    return false;
  }
}

function showBackorderDetail(flag) {
	var bdetaildiv = getBackorderDetailDIV();
	if (bdetaildiv != null) {
		if (flag == true)	{
			var toolbar = document.getElementById("labellist");
			
			bdetaildiv.style.visibility = "visible";
			
			updatePopupPos();
		   
			
			bdetaildiv.style.left = (popupXpos + 265) + "px";
		   bdetaildiv.style.top = (popupYpos + getElementY(toolbar)) + "px";
			
		}
		else {
			var url = "processLabelinBasket.php?ignoreBackOrder=true&itemCode=" + itemCode + "&itemQty=" + inputVal + "&type=tagpic";
			var ajax = new AJAXAction(url, validateAddToShoppingCartCallback); 
			ajax.doGet();
  
			bdetaildiv.style.visibility = "hidden";
		}
	}
}

function orderAlternative(newItemCode, orderQty) {
	var bdetaildiv = getBackorderDetailDIV();
	bdetaildiv.style.visibility = "hidden";
	
	var url = "processLabelinBasket.php?ignoreBackOrder=true&itemCode=" + newItemCode + "&itemQty=" + orderQty + "&type=tagpic";
	var ajax = new AJAXAction(url, gotoOrderPage); 
	ajax.doGet();
}

function gotoOrderPage() {
	gotoPage(4);
}

function getBackorderDetailDIV() {
  return document.getElementById("backorderdiv");
}
