/*
product_validation.js

This file contains the scripts to validate product quantities with the cart functionality

Author: Nathan Fernau
*/

var ProductValidator;

if (!ProductValidator) ProductValidator = {};

ProductValidator.isErrorVisible = false;
ProductValidator.showErrorObject = null;
ProductValidator.highlightColor = "#ffcccc";
ProductValidator.itemArray = new Array();

ProductValidator.addArrayItem = function(item)
{
	ProductValidator.itemArray[ProductValidator.itemArray.length] = item;
};

ProductValidator.validate = function(qtyID, qtyOnHand)
{
	var qty = MM_findObj(qtyID)
	var val, val2;
	if (qty) {
		val = convertValue(qty.value, "int");
		val2 = convertValue(qtyOnHand, "int");
		if (val > val2)
		{
			return false;
		}
	}
	else
		alert("ERROR: Problem accessing quantity value in order to validate!");
	return true;
};

ProductValidator.showError = function(itemID, containerID)
{
	//this.showErrorObject = new Spry.Effect.Fade(containerID, {duration: 50, from: 0, to: 100, toggle:false});
	//this.isErrorVisible = true;
	showElement(containerID);
};

ProductValidator.hideError = function(itemID, containerID)
{
	if (this.showErrorObject) {
		hideElement(containerID);
		this.isErrorVisible = false;
		this.showErrorObject = null;
		hideElement(containerID);
	}
};

ProductValidator.showCartError = function(itemID, qtyID, containerID)
{
	this.showErrorObject = new Spry.Effect.Fade(containerID, {duration: 50, from: 0, to: 100, toggle:false});
	this.showErrorObject.start();
};

ProductValidator.hideCartError = function(qtyID, containerID)
{
	if (this.showErrorObject) {
		this.showErrorObject = null;
	}
	this.highlightElement(qtyID, false);
	hideElement(containerID, true);
};

ProductValidator.restoreValue = function(itemID, qtyID)
{
	var qty = MM_findObj(qtyID);
	var lastqty = MM_findObj("lastqty"+itemID);
	if (qty && lastqty)
		qty.value = lastqty.value;
};

ProductValidator.selectElement = function(qtyID, highlight)
{
	var qty = MM_findObj(qtyID);
	if(!highlight)highlight=false;
	if (qty)
	{
		if (highlight)
			this.highlightElement(qtyID, highlight);
		//qty.focus();
		qty.select();
		
	}
};

ProductValidator.highlightElement = function(qtyID, bHighlight)
{
	var qty = MM_findObj(qtyID);
	if (bHighlight == null)
		bHighlight = true;
	if (qty)
	{
		if(qty.style)qty=qty.style;
		if (bHighlight)
			qty.backgroundColor = ProductValidator.highlightColor;
		else
			qty.backgroundColor = "";
	}
};

function validateProductQty(itemID, formID, qtyID, qtyOnHand, messageContainerID, optionalHideContainerID)
{
	if (!optionalHideContainerID || optionalHideContainerID == null)
		optionalHideContainerID = "";
	// clear any existing error that is displayed
	if (ProductValidator.isErrorVisible && ProductValidator.showErrorObject)
		ProductValidator.hideError(itemID, messageContainerID);
	
	// validate the data
	if (ProductValidator.validate(qtyID, qtyOnHand))
	{
		if (optionalHideContainerID != "") {
			showElement(optionalHideContainerID, true);
			hideElement(messageContainerID, true);
		}
		return true;
	}
	else
	{
		//ProductValidator.restoreValue(itemID, qtyID);
		ProductValidator.selectElement(qtyID);
		if (optionalHideContainerID != "")
			hideElement(optionalHideContainerID, true);
		ProductValidator.showError(itemID, messageContainerID);
		return false;
	}
}

function validateShoppingViewItem(itemID, qtyID, qtyOnHand, messageContainerID)
{
	// validate the data
	if (ProductValidator.validate(qtyID, qtyOnHand))
	{
		ProductValidator.hideCartError(qtyID, messageContainerID);
		return true;
	}
	else
	{
		//ProductValidator.restoreValue(itemID, qtyID);
		ProductValidator.highlightElement(qtyID, true);
		ProductValidator.showCartError(itemID, qtyID, messageContainerID);
		return false;
	}
}

function shoppingViewUpdate()
{
	var returnVal = true;
	var arr;
	var i;
	if (ProductValidator.itemArray.length > 0) {
		for (i = 0; i < ProductValidator.itemArray.length; i++) {
			arr = ProductValidator.itemArray[i];
			if(!validateShoppingViewItem(arr[0], arr[1], arr[2], arr[3]))
				returnVal = false;
		}
	}
	return returnVal;
}

function setProductQuantity(qtyID, qtyOnHand, containerID)
{
	MM_findObj(qtyID).value = qtyOnHand;
	ProductValidator.hideCartError(qtyID, containerID);
}