﻿function trim(stringToTrim) 
{
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function validateQuantity(source, arguments)
{    

    arguments.IsValid = true;    
    var quantity = document.getElementById(source.getAttribute("ClientId"));
    var maxValue = 0;
    var regExp = /^\d+$/;
        
    if(quantity == null || source.getAttribute("MaxValue") == null)
    {
        arguments.IsValid = false;
        return;               
    }
    else
    {  
        maxValue = parseInt(source.getAttribute("MaxValue"));
    }
    
    if (regExp.test(arguments.Value)) 
    {
        if(parseInt(arguments.Value) > maxValue)
        {
            arguments.IsValid = false;
            return;
        }
        else
        {
            if(parseInt(trim(arguments.Value)) == 0)
            {
                quantity.value = "1";
            }
        }
    }else
    {
        quantity.value = "1";
    }
}