//--------------------------------------------------------------------
//	val_tools_products_submit.js
//--------------------------------------------------------------------

function validateToolsProductSubmit(form)
{
	//alert("start validateToolsProductSubmit"); 
	//alert(btnWhichButton.value); 

	// attempt to extract the product id from the submit button name:
	var prodIdIdx = (btnWhichButton.value).lastIndexOf("_");
	var prodId = (btnWhichButton.value).substring(prodIdIdx + 1);

	var isMulti = "false";

	// check if user submitted for a multi-item:
	// (btnWhichButton is a global javascript var defined in toolsProduct2)
	if((btnWhichButton.value).search('multi') != -1)
	{
		isMulti = "true";
		//alert("this is a multi"); 
	}

	//alert("start validateToolsProductSubmit 2"); 

	if(isMulti == "true")
	{
		// format the name of associated select box using the previously extracted product id:
		var multi_select_name = "multi_select_" + prodId;

		// get a ref to the select box:
		var sel = document.getElementById(multi_select_name);
		
		// then get the selected option:
		var selOption = sel.options[sel.selectedIndex].value;

		// check if selected item is a POA:
		if(selOption.search('POA') != -1)
		{
			alert('Please call us for a price for the item you have selected');
			return false;
		}

		// check if selected item is out of stock:
		if(selOption.search('OOS') != -1)
		{
			alert('The item you have selected is out of stock');
			return false;
		}
	}


	//alert("start validateToolsProductSubmit 3"); 
	//alert((btnWhichButton.value).substring(6)); 
	var qtyId = "qty" + (btnWhichButton.value).substring(6);
	//alert(qtyId); 

	var itemQty = document.getElementById(qtyId);
	//alert(itemQty.value); 

	//check if something was entered in quantity field:
   if(isEmpty(itemQty.value)) 
   { 
      alert('You have not entered a quantity'); 
      itemQty.focus(); 
      return false; 
   } 

	// check if only numbers 0->9 were entered in quantity field:
   if (!IsNumeric(itemQty.value)) 
   { 
      alert('Please enter a whole number with no decimals in the quantity field'); 
      itemQty.focus(); 
      return false; 
	}
	
	// check if 0 was entered in quantity field:
	var num = parseInt(itemQty.value);
	if(num == 0)
	{
      alert('Please enter a non-zero amount in the quantity field'); 
	  itemQty.focus(); 
      return false; 
	}


	return true;
}


function IsNumeric(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;

 
   for (var i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}

function isEmpty(field)
{
	return ((field == null) || (field.length == 0)) 
}

function stripSpaces(myForm)
{
	var x = myForm.quantity.value;
	while (x.substring(0,1) == ' ') x = x.substring(1);
	while (x.substring(x.length-1,x.length) == ' ') x = x.substring(0,x.length-1);
	myForm.quantity.value = x;
}


