// Highslide options (remove the registerOverlay call to disable the control bar)
hs.registerOverlay({thumbnailId: null,overlayId: 'controlbar',position: 'top right',hideOnMouseOut: true});
hs.graphicsDir = '/images/highslide/';
hs.outlineType = 'rounded-white';

// Checks to make sure the quantity in each field is a valid integer
function validateQtys(zero_ok)
{
	var qtySelected = 0;
	for(i = 0; i < document.orderForm.elements.length; i++)
	{
		var partNum = document.orderForm.elements[i].name;
		partNum = partNum.replace(/qty\[/,'');
		partNum = partNum.replace(/\]/,'');
		if (document.orderForm.elements[i].name.substr(0,3) == "qty")
		{
			if ((isNaN(document.orderForm.elements[i].value) && document.orderForm.elements[i].value != '') || (!isNaN(document.orderForm.elements[i].value) && document.orderForm.elements[i].value < 0))
			{
				alert("Part # '" + partNum +  "' has an invalid quantity of '" + document.orderForm.elements[i].value + "'. Please fix this and try again.");
				return false;
			}
			else if (document.orderForm.elements[i].value != '')
			{
				qtySelected = 1;
			}
		}
	}
	if (qtySelected == 0 && !zero_ok)
	{
		alert("You do not have any items selected to add to your order. If you just want to view your current order, please click the 'View Current Order' button.");
		return false;
	}
	return true;
}

// Confirms with the user that they want to submit the order
function validateOrder()
{
	if (validateQtys())
	{
		if (confirm('This will complete your order and processing will begin immediately. Once an order is completed, it can no longer be changed. To complete this order, click \'OK\'. Otherwise, click \'Cancel\' to go back and continue editing your order.'))
		{
			return true;
		}
	}
	else
	{
		return false;
	}
	return false;
}

// Increment all quantities by either -1 or 1
function incrementQtys(option)
{
	for(i = 0; i < document.orderForm.elements.length; i++)
	{
		if (document.orderForm.elements[i].name.substr(0,3) == 'qty')
		{
			var qty = 0;
			if (!isNaN(parseInt(document.orderForm.elements[i].value)))
			{
				if (option == '+')
				{
					qty = parseInt(document.orderForm.elements[i].value) + 1;
				}
				else if (option == '-')
				{
					if (parseInt(document.orderForm.elements[i].value) > 0)
					{
						qty = parseInt(document.orderForm.elements[i].value) - 1;
					}
				}
			}
			else if (document.orderForm.elements[i].value == '')
			{
				if (option == '+')
				{
					qty = 1;
				}
				else if (option == '-')
				{
					qty = 0;
				}
			}
			else if (isNaN(document.orderForm.elements[i].value))
			{
				if (option == '+')
				{
					qty = 1;
				}
				else if (option == '-')
				{
					qty = 0;
				}
			}
			document.orderForm.elements[i].value = qty;
		}
	}
}

// Process the form to view an order
function viewOrder(orderID)
{
	document.getElementById('viewOrderID').value = orderID;
	document.viewOrderForm.submit();
}

// Process the form to delete an order
function deleteOrder(orderID)
{
	if (confirm('This will delete all items in this order. Are you sure you want to delete this order? Click \'OK\' to delete it, or \'Cancel\' to keep it.'))
	{
		document.getElementById('deleteOrderID').value = orderID;
		document.deleteOrderForm.submit();
	}
}

// Change the file type description
function changeFileTypeDesc(spanID,description)
{
	document.getElementById(spanID).innerHTML = description;
}

// Fields to export
function exportFields(showHide)
{
	if (showHide == 'show')
	{
		document.getElementById('exportFields').style.display = 'block';
	}
	else if (showHide == 'hide')
	{
		document.getElementById('exportFields').style.display = 'none';
	}
}