// Following variables must be kept in synch with constants defined at top of /LandingPage/ExpressLane.ascx.vb
var ItemNumberFieldName = "ItemNumber";
var ItemQtyFieldName = "ItemQty";
var ItemProductIDFieldName = "ProductID";
var ItemCheckboxFieldName = "CheckBox";
var Action_FindItems = "FindItems";
var Action_Reset = "Reset";
var Action_AddLines = "AddLines";
var Action_AddToCart = "AddToCart";
var MaxFields = 300;
var HdnNumFields = "NumberOfFields";

function CartUpdate () {
	var ItemsChecked = false;

    if (CheckIDsAndQtys()) {
          with (document) {
            for (i = 1; i <= parseInt(getElementById(HdnNumFields).value); i++) {
				if (getElementById(ItemCheckboxFieldName + i).checked) {
					ItemsChecked = true;
					break;
				}
			}		
			if (!ItemsChecked) {
				alert("Please select at least one item to add to your cart.");
				return;
			}
            for (i = 1; i <= parseInt(getElementById(HdnNumFields).value); i++) {
                if (getElementById(ItemProductIDFieldName + i) != null) {
                    with (ExpressLaneForm) {
                        hdnAction.value = Action_AddToCart;
                        submit();
                        return;
                    }
                }
            }
        }
    }
}

function FindItems () {
    if (CheckIDsAndQtys()) {
        with (document.ExpressLaneForm) {
            hdnAction.value = Action_FindItems;
            submit();
        }
    }
}

function ResetForm () {
    with (document.ExpressLaneForm) {
        hdnAction.value = Action_Reset;
        submit();
    }
}

function AddLines () {
    with (document.ExpressLaneForm) {
        if (parseInt(NumberOfFields.value) < MaxFields) {
            hdnAction.value = Action_AddLines;
            submit();
            return;
        }
    }

    alert("Sorry, not more than " + MaxFields + " items allowed.");
}

function CheckIDsAndQtys () {
    var IDsOK = false;

    with (document) {
        for (i = 1; i <= parseInt(getElementById(HdnNumFields).value); i++) {
            if (trim(getElementById(ItemNumberFieldName + i).value) != "") {
                IDsOK = true;
                with (getElementById(ItemQtyFieldName + i)) {
                    if (isNaN(value) || parseInt(value) < 1) {
                        alert ("Please enter a value of one or more for the quantity.");
                        focus();
                        return false;
                    }
                }
            }
        }

        if (!IDsOK) {
            alert("Please enter at least one item to search for.");
            getElementById(ItemNumberFieldName + "1").focus();
            return false;
        }
    }
    
    return true;
}