// JavaScript Document
//Functions for processing products that are selected to be added to shopping cart or shopping list
function isvalidQty(qtyIndex, minOrderIndex, prodidIndex)
{
	var qty = document.ProductList.elements[qtyIndex].value;
	var minOrder = document.ProductList.elements[minOrderIndex].value;
	var prodid = document.ProductList.elements[prodidIndex].value;
	qty = parseInt(qty);
	minOrder = parseInt(minOrder);
	if (qty == 'NaN' || qty == 0)
	{
		alert('Invalid Quantity for Item #: ' + prodid);
		document.ProductList.elements[qtyIndex].focus();
		return false;
	} else {
		if (qty % minOrder != 0 )
		{
			alert('Item #: ' + prodid + ' must be ordered in multiples of ' + minOrder);
			document.ProductList.elements[qtyIndex].focus();
			return false;
		} else
		return true;
	}
}

function NotDiscontinued(qtyIndex, stockTypeIndex,prodidIndex)
{
	var stockType = document.ProductList.elements[stockTypeIndex].value;
	var prodid = document.ProductList.elements[prodidIndex].value;
	if (stockType == 'Discontinued')
	{
		alert('Item #: '+ prodid+ ' is discontinued and is not available for ordering. If you wish to see this item stocked in the future, please contact us.');
		document.ProductList.elements[qtyIndex].focus();
		return false;
	} else
		return true;
} 
	
function shoppingList(shoppingListPage) {
				var url = shoppingListPage;
				var index = 0;
				var selectedItems = 0;
				for (var i = 0; i < document.ProductList.length; i++)  {
					if (document.ProductList.elements[i].type == "checkbox") {					
						if (document.ProductList.elements[i].checked) {
								selectedItems++;

								if (isvalidQty(i-1, i+4,i+2) ==  false)
									return;

								if (NotDiscontinued(i-1, i+5,i+2) ==  false)
									return;

								url += '&OID' + index + '=' + document.ProductList.elements[i+1].value;								
								url += '&QUANTITY' + index + '=' + document.ProductList.elements[i-1].value;
							    index++;
							}//end of if checked
						}//end of if type is checkbox
					}//end of for
		
				url += "&totalitems=" + index;
				if (selectedItems > 0) {
					parent.location = url;
				} else {
					alert('Please select an item(s).');
					return;
				}
			}//end of function

			function shoppingCart(shoppingCartPage) {
			    var url = shoppingCartPage;
				var index = 0;
				var selectedItems = 0;
				
					for (var i = 0; i < document.ProductList.length; i++)  {
						if (document.ProductList.elements[i].type == "checkbox") {					
							if (document.ProductList.elements[i].checked) {
								selectedItems++;
																
								if (isvalidQty(i-1, i+4,i+2) ==  false)
									return;

								if (NotDiscontinued(i-1, i+5,i+2) ==  false)
									return;

								url += '&OID' + index + '=' + document.ProductList.elements[i+1].value;								
								url += '&QUANTITY' + index + '=' + document.ProductList.elements[i-1].value;
							    url += '&DISCOUNT' + index + '='+ document.ProductList.elements[i+3].value;
								index++;
							}//end of if checked
					   }//end of if checkbox
					}//end of for
				
				url += "&totalitems=" + index;
				if (selectedItems > 0) {
					parent.location = url;
				} else {
					alert('Please select an item(s).');
					return;
				}
			}//end of function
			
			function updateListItem(j, shoppingListPage) 
			{
				var url = shoppingListPage;
				var name = "QUANTITY"+j;
				for (var i = 0; i < document.ProductList.length; i++)  {
						if (name == document.ProductList.elements[i].name) 
						 {					
							url += '&QUANTITY=' + document.ProductList.elements[i].value;
							break;
						}//end of if
					}//end of for
	           parent.location = url;				
			 }

   function reOrderPastItems(shoppingCartPage) {
			    var url = shoppingCartPage;
				var index = 0;
				var selectedItems = 0;
				
					for (var i = 0; i < document.ProductList.length; i++)  {
						if (document.ProductList.elements[i].type == "checkbox") {					
							if (document.ProductList.elements[i].checked) {
								selectedItems++;
								
								if (isvalidQty(i-1, i+3,i+2) ==  false)
									return;

								url += '&OID' + index + '=' + document.ProductList.elements[i+1].value;								
								url += '&QUANTITY' + index + '=' + document.ProductList.elements[i-1].value;
							    index++;
							}//end of if checked
					   }//end of if checkbox
					}//end of for
				
				url += "&totalitems=" + index;
				if (selectedItems > 0) {
					parent.location = url;
				} else {
					alert('Please select an item(s).');
					return;
				}
			}//end of function
						
//Function added by Vadim 12-15-04 due to "SSL" implementation of "Check all boxes on My Order Detail Page"
//function code is placed on /non-ssl/shopping_util.js as well as on /ssl/shopping_util.js  (only on UT server)		
function CheckAllBx()
{
count = document.ProductList.elements.length;
    	for (i=0; i < count; i++) 
		{
		//in case we want to "reverse check":
		//if(document.ProductList.elements[i].checked == 1)
    		//document.ProductList.elements[i].checked = 0; 
	  //else 
  		document.ProductList.elements[i].checked = 1;
		}
}// end of the function