
PromotionCodeOnlineValidator = function(textBox)
{
   this.textBox = $(textBox);
   this.textBox.addEvent("change", this.updateState.handler(this));
   this.textBox.addEvent("keyup", this.updateState.handler(this));
}

PromotionCodeOnlineValidator.prototype =
{
    textBox: null,
    lastActiveDivId: null,
    timeoutId: null,
    
    showPromotionCodeImage: function(imgSrc)
    {
        var promotionCodeImage = $("promotionCodeImage");
        promotionCodeImage.src = imgSrc;
        promotionCodeImage.show();
        
        $("promotionCodeDiv").hide();
    },
    
    validate: function(promotionCode)
    {
        if(this.timeoutId)window.clearTimeout(this.timeoutId);
        
        if(promotionCode == '')return false;
        
        var azPattern = new RegExp("^[a-zA-Z0-9]+$");

        if(azPattern.test(promotionCode) == false)
            return false;
            
        this.promotionCode = promotionCode;
        this.timeoutId = window.setTimeout(this.checkPromotionCode.handler(this), 300);
    },
    
    checkPromotionCode: function()
    {
        var ajax = new AjaxClient();
        ajax.set("onSuccess", this.onCheckSuccess.handler(this));
        ajax.addParam("promotionCode", this.promotionCode)
        ajax.post(setting.checkPromotionCodeUrl);
    },
    
    onCheckSuccess: function(ajaxObj)
    {
        var response = ajaxObj.parseJSON();
        
        if(response.status == "ok")
        {                                        
            this.showPromotionCodeImage(response.imgSrc);
        }
    },
    
    updateState: function()
    {
        this.validate(this.textBox.value); 
    }
}
