﻿// JScript File

//functions to enforce minimum/maximum quantities
function EnforceMinimum(txtQtyID, hdnMaxID, hdnMinID, BackOrderBehavior, blnIsSample)
{
    //Constants
    var BACK_ORDER_ALLOW = 1
    var BACK_ORDER_WARN = 0
    var BACK_ORDER_DISALLOW = 2
        
    //Field objects
    var txtQty = document.getElementById(txtQtyID);
    var hdnMax = document.getElementById(hdnMaxID);
    var hdnMin = document.getElementById(hdnMinID); 
    
    //Variables
    var intCurrentQty = 0;
    var intMax = 0;
    var intMin = 0;
    
    //check for isNaN
    if(isNaN(txtQty.value))
    {
        //Quantity is not valid.
        alert("'" + txtQty.value + "' is not a valid quantity.");
        txtQty.value = 1;
        return false;
    }    
    else
    {
        //handles negative numbers--negative quantities should not occur.
        if(hdnMax.value < 0 || isNaN(hdnMax.value))
        {
            hdnMax.value = 0;
        }
        if(hdnMin.value < 0 || isNaN(hdnMin.value))
        {
            hdnMin.value = 0;
        }  
        
        //Fill in variables 
        intCurrentQty = parseInt(txtQty.value);
        intMax = parseInt(hdnMax.value);
        if(!blnIsSample)
        {
            intMin = parseInt(hdnMin.value);
        }
        else
        {
            //Min is 1 for samples
            intMin = 1;
        }   
           
        //Check for invalid quantity 
        if(intCurrentQty < intMin && (intMin < intMax || BackOrderBehavior != BACK_ORDER_DISALLOW))
        {
            //if quantity is less than minimum and less than maximum, reset to minimum and notify.
            txtQty.value = intMin;
            
            alert("You must order at least " + intMin + " of this product.");
            
            return false;
        }    
        else if(intCurrentQty > intMax)
        {
            if(BackOrderBehavior == BACK_ORDER_DISALLOW)
            {
                //if backorder is not allowed, set quantity to max.
                txtQty.value = intMax;
                
                if(hdnMax.value != 1)
                {
                    alert("We're sorry.  There are only " + Number(intMax) + " available.");
                }
                else
                {
                    alert("We're sorry.  There is only 1 available.");
                }
                return false;
            }        
            else if(BackOrderBehavior == BACK_ORDER_WARN)
            {            
            
                //if backorder is allowed but warn is on, do not change quantity but notify that
                //additional quantities will be backordered
                if(intMax != 1)
                {
                    alert("We're sorry.  There are only " + Number(intMax) + " available. Any more will be placed on back order.");
                }
                else
                {
                    alert("We're sorry.  There is only 1 available.  Any more will be placed on back order.");
                }       
            }
        }
    }
    
    //No errors flagged; allow submission to continue
    return true;    
}
