
function validateForm(source)
	{
	    if(typeof(JT_showText) != 'function')
	    {
	        alert('You must include a reference to JTip and its CSS');
	        return false;
	    }
	    
	    var returnValue = true;
	    
	    var _isShoppingCart = source["hdnIsShoppingCartPage"] ? true : false;
	    var _isQuickOrder = source["QuickOrder"] ? true : false;
	    
	    // validation for the shoppingcart
	    if(_isShoppingCart == true)
	    {
	        $("input:text", source).each(
                function(i)
                {
                    if(returnValue == true)
                    {
                    
                        if(_isQuickOrder == true)
                        {
                            // only validate if there is an entry, empty value are OK
                            if(this.value.length > 0)
                            {
                                if(isNaN(this.value))
			                    {  
			                        JT_showText("• Please enter a valid quantity.", this.id, 'Incorrect Quantity');
			                        this.focus();
			                        returnValue = false;
			                    }
			                    else
			                    {
			                        // validate the stock quantity
			                        var _productid = parseInt(this.id.split('_')[1]);
			                        var _stocklimit = parseInt(source["hdnNumStock_" + _productid].value);
			                        var _quantity = parseInt(this.value);
			                        
			                        if(_stocklimit <= _quantity)
        				            {
        				                JT_showText("• Our in-stock supplies are limited to " + _stocklimit + ".", this.id, 'Unavailable Quantity');
        				                this.focus();
		                                returnValue = false;
        				            }
			                    }
                            }
                        }
                        else
                        {
                    
                            if(isNaN(this.value) || this.value.length < 1)
			                {				
				                // add the error alert to the textbox
				                JT_showText("• Please enter a valid quantity.", this.id, 'Incorrect Quantity');
				                this.focus();
			                    returnValue = false;
			                }								
			                else
			                {
			                    var _quantity = parseInt(this.value);
    				            
				                if(_quantity > 0)
				                {		
			                        if (source["hdnOptionStock_" + this.id])
			                        {
				                        StockLimit = parseInt(source["hdnOptionStock_" + this.id].value);
			                        }
			                        else
			                        {
				                        StockLimit = parseInt(source["hdnNumStock_" + this.id].value);
			                        }
                    				
        				            if(StockLimit == -1 || _quantity <= StockLimit)
        				            {
        				            }
        				            else
                                    {
        				                JT_showText("• Our in-stock supplies are limited to " + StockLimit + ".", this.id, 'Unavailable Quantity');
        				                this.focus();
		                                returnValue = false;
        				            }

            				    }
            				    else
                                {
        				            JT_showText("• Quantity cannot be zero or less.", this.id, 'Incorrect Quantity');
        				            this.focus();
		                            returnValue = false;
        				        }
    		                }
    		            }
			         }
                 }
			 );
	    }
        // run this validation if the product has options
        else if(source["hdnHasOptions"].value == "True")
        {
            var _txtQuantity = $("input:text", source).get(0);	            
            var _stock = parseInt(source["stock"].value);
            var _productid = parseInt(source["pid"].value);
	           
	        var _purchaseQuantity = 0;
	        
	        // check the stock level of the product
            if(!isNaN(_stock) && !isNaN(_txtQuantity.value))
            {
                var _purchaseQuantity = parseInt(_txtQuantity.value);
                if(_purchaseQuantity > 0)
                {
                    if(_stock == -1) // unlimited
                    {
                        // do nothing
                    }
                    else if(_stock < _purchaseQuantity)
                    {
                        JT_showText("• Our in-stock supplies are limited to " + _stock + ".", _txtQuantity.id, 'Unavailable Quantity');
                        this.focus();
			            returnValue = false;
                    }
                }
                else
                {
                    JT_showText("• Quantity cannot be zero or less...", _txtQuantity.id, 'Incorrect Quantity');
                    this.focus();
			        returnValue = false;
                }
            }
            else
            {
                JT_showText("• Invalid quantity.", _txtQuantity.id, 'Quantity Error');
                this.focus();
                returnValue = false;
            }
            
            if(returnValue == true)
            {
            
                // check the stock level of all the options
                $("select", source).each(
                    function(i)
                    {
                        if(returnValue == true && !isNaN(this.value))
                        {
                            var optionID = parseInt(this.value);
                            var hdnMandatory = source["hdnoptreq_" + this.name];
                            var mandatory = (hdnMandatory) && (hdnMandatory.value == 'True');                            
                            if(optionID != -1)
                            {
                                var optionStockLevel = document.getElementById("hdnOptionStockLevel_" + optionID).value;
                                var _optionRepeaterLevel = document.getElementById("hdnOptionReapterLevel_" + optionID).value;
                                
                                var _optionQuantityBox = document.getElementById("optqty_" + _productid + "_" + _optionRepeaterLevel);
                                
                                if(_optionQuantityBox)
                                {
                                    _purchaseQuantity = _optionQuantityBox.value;
                                }
                                
                                if(_purchaseQuantity > 0)
                                {
                                    if(!isNaN(optionStockLevel))
                                    {
                                        if(parseInt(optionStockLevel) >= parseInt(_purchaseQuantity) || parseInt(optionStockLevel) == -1)
                                        {
                                            // do nothing
                                        }
                                        else
                                        {
                                            JT_showText("• Our in-stock supplies are limited to " + optionStockLevel + ".", this.id, 'Unavailable Quantity');
                                            this.focus();
			                                returnValue = false;
                                        }
                                    }
                                    else
                                    {
                                        JT_showText("• The stock level for this product has been corrupted.", this.id, 'Stock Error');
                                        this.focus();
			                            returnValue = false;
                                    }
                                }
                                else
                                {
                                    JT_showText("• Quantity cannot be zero or less.", this.id, 'Incorrect Quantity');
                                    this.focus();
			                        returnValue = false;
                                }
                                   
                            }
                            else if (mandatory == true)
                            {
	                            JT_showText("• Please select an option.", this.id, 'Option Required');
	                            this.focus();
			                    returnValue = false;
                            }
                        }
                        else 
                        {
                           returnValue = false; 
                        }
                    }
                );
            }
        }
        else if(source["hdnHasVariants"].value == "True") // check the stock levels of all the variants
        {
            //alert($("input:text", source).length);
            
            var totalItems = 0;
            
            $("input:text", source).each(
                function(i)
                {
                    //alert(i);
                    if(returnValue == true)
                    {
                        var _quantity = this.value;
                        
                        if(!isNaN(_quantity))
                        {
                        
                            totalItems += _quantity;
                        
                            if(_quantity > 0)
                            {
                                var _idProperties = this.id.split('_');
                                var _pid = parseInt(_idProperties[1]);
                                var _variantStock = parseInt(document.getElementById('stock_qty_' + _pid + '_' + _idProperties[2]).value);
                                if(_variantStock < parseInt(_quantity))
                                {
                                    JT_showText("• Our in-stock supplies are limited to " + _variantStock + ".", this.id, 'Unavailable Quantity');
                                    this.focus();
			                        returnValue = false;
                                }
                            }
                            else
                            {
                                //JT_showText("• Quantity cannot be zero or less.", this.id, 'Incorrect Quantity');
                                //this.focus();
			                    //returnValue = false;
                            }                 
                        }
                        else
                        {
                            JT_showText("• Invalid quantity.", this.id, 'Quantity Error');
                            this.focus();
                            returnValue = false;
                        }
                   }
                }
            );
            
            
            if(totalItems == 0)
            {
                JT_showText("• Invalid quantity.", $("input:submit", source).get(0).id, 'Quantity Error');
                returnValue = false;
            }
        }
        else
        {
            var _txtQuantity = $("input:text", source).get(0);	
            var _stock = parseInt(source["stock"].value);
            var _productid = parseInt(source["pid"].value);
	           
	        if(!isNaN(_txtQuantity.value))
	        {
	            var _purchaseQuantity = parseInt(_txtQuantity.value);
	            
                if(!isNaN(_stock))
                {
                    if(_purchaseQuantity > 0)
                    {
                        if(_stock == -1)
                        {
                            // do nothing
                        }
                        else if(_stock < _purchaseQuantity)
                        {
							JT_showText("• Our in-stock supplies are limited to " + _stock + ".", _txtQuantity.id, 'Quantity Error');
							this.focus();
			                returnValue = false;
                        }
                    }
                    else
                    {
                        JT_showText("• Quantity cannot be zero or less.", _txtQuantity.id, 'Incorrect Quantity');
                        this.focus();
			            returnValue = false;
                    }
                }
                else
                {
                    JT_showText("• Invalid quantity.", _txtQuantity.id, 'Stock Error');
                    this.focus();
                    returnValue = false;
                }
            }
            else
            {
                JT_showText("• Invalid quantity.", _txtQuantity.id, 'Incorrect Quantity');
                this.focus();
                returnValue = false;
            }
        }
	    
	    return returnValue;
}