//VALIDATE QUANTITY WHEN ADDING TO CART
function validateQuantity(index, minOrder, AddQty, StockControl, CartonQty)
{
	//alert('Qty Changed');
	// get controls
    quantityBox = document.getElementById(index);
    if (minOrder==0) minOrder=1;
	var quantitycheck;
	var allowSubmit;
	allowSubmit=true;
	minOrder=parseFloat(minOrder);
	AddQty=parseFloat(AddQty);
    // invalid if text box not found
    if (!quantityBox) {
			alert ('error');
            return false;
	} else {
            quantity = parseFloat(quantityBox.value);
            // only check further if have a value
            if (quantity) {
                    //check that quantity is a whole number
                 //   if (isNaN(quantity) || (parseInt(quantity) != quantity-0)) {
                 //           alert("You must enter a whole number!");
                //            quantityBox.value="";
                 //           quantityBox.focus();
                //            return false;
                //    }
                    //check that quantity is greater or equal to minorder
                    if ((quantity == 0) || (quantity<minOrder)) {
                            alert("This product has a minimum order of " + minOrder + "!\nPlease enter a new quantity or click the Update Cart button again to add the revised Quantity.");
                            quantityBox.value=minOrder;
                            quantityBox.focus();
							allowSubmit=false;
                            return false;
							
                    }  
					
					//check that quantity is a multiple of minorder
					if (AddQty>0) {
						if (AddQty==0) AddQty=1;
						quantitycheck=quantity-minOrder;
					   // if ((quantity == 0) || (quantity%AddQty != 0)) {
						  if (quantitycheck%AddQty != 0) {
								alert("This product can only be purchased in Additional lots of " + AddQty + "!\nPlease enter a new quantity.");
								quantityBox.value="";
								quantityBox.focus();
								allowSubmit=false;
								return false;
								
						}   
					}
					// Check if qty hits carton pricing
					if (CartonQty>0 & quantity>=CartonQty) {
							cartonremainintgitems=(quantity%CartonQty);
							totalcartonitems=Math.floor(quantity / CartonQty);
							totalcartonitems=totalcartonitems*CartonQty;
						alert(totalcartonitems+" items qualify for carton pricing\nSavings will be applied at checkout.");
					} else if (CartonQty>0 & (quantity/CartonQty >= .8) ) {
						extraforcarton=	CartonQty - quantity;
						alert("You would qualify for carton pricing if you ordered "+ extraforcarton +" more item(s).");
					}					
					
					
					if(StockControl!='OFF')  {
						if (quantity>StockControl) {
							alert("You cannot order more than we currently have in stock.\nPlease order a maximum quantity of "+StockControl);
							quantityBox.value="";
							quantityBox.focus();
							allowSubmit=false;
							return false;
							
						}
					}
            }
    }
	this.form.submit();
	alert("Item has been added to your cart.");
   	return true;
   // alert( allowSubmit );    
}

function MM_callJS(jsStr) { //v2.0
  return eval(jsStr)
}

jQuery.fn.center = function () { 
    this.css("position","absolute"); 
    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px"); 
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px"); 
    return this; 
} 

