// JavaScript Document

var dataChanged = false;
function doSellTitleLookup(ca,order,page) {
    if (checkDataChanged()) {
        window.location = "search.jsp?sellTitleLookup=true&catalogNumber="+ca+"&orderId="+order+"&currentPage="+page;
    }
}
function doSellIsbnLookup() {
    if (checkDataChanged()) {
		document.main.searchDetailPage.value = true;
		document.main.sellIsbnLookup.value = true;
		document.main.searchWord.value = document.main.addNewIsbn.value;
		document.main.action = "search.jsp?sellDetail=true";
		document.main.submit();
   }
}
function createPackingList() {
    document.main.pageAction.value = "addToOrder";
	document.main.submit();
}
function updatePackingList() {
	if (!validateForm())
		return false;
    document.main.pageAction.value= "update";
    document.main.submit();
}
function closePackingList() {
    if (confirm("Are you sure you want to close this packing list?")) {
        document.main.pageAction.value = "close";
        document.main.submit();
    }
}
function generatePL() {
	window.alert("You must obtain an authorization code before generating a Packing List.");
}
function addNewIsbnFn() {
   	document.main.pageAction.value = "addNewIsbn";
	document.main.submit();
}
function doOSSellEnterKey(orderId) {
	window.alert("order id: "+orderId);
    if (event.keyCode == 13) {
        clickedEnterKey = true;
		window.alert("order id: "+orderId);
        if (searchCriteriaIsEmpty()) {
            if (document.main.quantityList) {
                if (document.main.quantityList.value != "" || document.main.newQuantityList.value != "") {
                    goODFS(orderId);
                } else {
					window.location = "sellDetail.jsp?orderId="+orderId;
                }
            } else {
                window.location = "sellDetail.jsp?orderId="+orderId;
            }
       	}
    }
}
function deletePackingList() {
    if (confirm("Are you sure you want to delete this packing list?")) {
        document.main.pageAction.value = "delete";
        document.main.submit();
    }
}
function goSellDetail(x) {
    window.location = "sellDetail.jsp?orderId="+x;
}
function goSellDetailFromLink(x) {
	window.location = "sellDetail.jsp?orderId="+x;
}
function createPackListName() {
	var packListName = "";
	packListName = window.prompt("Please supply a name for the packing list you are creating:", "");
	document.main.packListName.value = packListName;
	createPackingList();
}
function doAddMoreIsbn(x) {
    document.main.pageAction.value = "addMore";
    document.main.isbnListCount.value = x;
    document.main.submit();
}
function doAutoTab(l,o,x) {
    if (l == 10 && o.value.substring(0,3) == "978") {
        if (o.value.length < 13) {
            return;
        }
    }
    if (o.value.length >= l && event.keyCode >= 48 && event.keyCode <= 105) {
        if (x) {
            x.focus();
        }
    }
}

function validateForm() {
	/* Check to see if we have a used quantity length.  Will not have one if there is only 1 used quantity. */
	if (document.main.usedQtyList == undefined) {
		return false;
	}
	if (document.main.usedQtyList.length != undefined) {
		for (var i = 0; i < document.main.usedQtyList.length; i++) {
			if (!(document.main.usedQtyList[i].value >= 0 && document.main.usedQtyList[i].value <= 999)) {
				window.alert("Used quantity should be between 0 and 999.");
				document.main.usedQtyList[i].value = 0;
				document.main.usedQtyList[i].focus();
				return false; 
			}
			if (!(document.main.newQtyList[i].value >= 0 && document.main.newQtyList[i].value <= 999)) {
				window.alert("New quantity should be between 0 and 999.");
				document.main.newQtyList[i].value = 0;
				document.main.newQtyList[i].focus();
				return false; 
			}
			if (!(document.main.usedPriceList[i].value >= 0 && document.main.usedPriceList[i].value <= 999.99)) {
				window.alert("Used price should be between 0 and 999.99.");
				document.main.usedPriceList[i].value = 0;
				document.main.usedPriceList[i].focus();
				return false; 
			}
			if (!(document.main.newPriceList[i].value >= 0 && document.main.newPriceList[i].value <= 999.99)) {
				window.alert("New price should be between 0 and 999.99.");
				document.main.newPriceList[i].value = 0;
				document.main.newPriceList[i].focus();
				return false; 
			}
		}	
	} else {
		if (!(document.main.usedQtyList.value >= 0 && document.main.usedQtyList.value <= 999)) {
			window.alert("Used quantity should be between 0 and 999.");
			document.main.usedQtyList.value = 0;
			document.main.usedQtyList.focus();
			return false; 
		}
		if (!(document.main.newQtyList.value >= 0 && document.main.newQtyList.value <= 999)) {
			window.alert("New quantity should be between 0 and 999.");
			document.main.newQtyList.value = 0;
			document.main.newQtyList.focus();
			return false; 
		}
		if (!(document.main.usedPriceList.value >= 0 && document.main.usedPriceList.value <= 999.99)) {
			window.alert("Used price should be between 0 and 999.99.");
			document.main.usedPriceList.value = 0;
			document.main.usedPriceList.focus();
			return false; 
		}
		if (!(document.main.newPriceList.value >= 0 && document.main.newPriceList.value <= 999.99)) {
			window.alert("New price should be between 0 and 999.99.");
			document.main.newPriceList.value = 0;
			document.main.newPriceList.focus();
			return false; 
		}
	}
    return true;
}
