GiftCardHandler.prototype.clearErrors = GiftCardHandler_clearErrors;
GiftCardHandler.prototype.getSku = GiftCardHandler_getSku;
GiftCardHandler.prototype.renderPrice = GiftCardHandler_renderPrice;
GiftCardHandler.prototype.setSelection = GiftCardHandler_setSelection;
GiftCardHandler.prototype.validate = GiftCardHandler_validate;

function GiftCardHandler() {
    this.skuId = null;
}

function GiftCardHandler_clearErrors() {
}


function GiftCardHandler_getSku() {
    return this.skuId;
}

function GiftCardHandler_renderPrice(dropDown) {
    var selectedOption = dropDown.options[dropDown.selectedIndex];
    
    if (selectedOption) {
        
        var priceDisplay = "";
        if (selectedOption.value > "") {
            priceDisplay = selectedOption.text + ".00";
        } 
        
        document.getElementById("priceValue").innerHTML = priceDisplay;
    }
}

function GiftCardHandler_setSelection(dropDown) {
    var selectedOption = dropDown.options[dropDown.selectedIndex];
    
    if (selectedOption) {
        this.skuId = selectedOption.value;
        this.renderPrice(dropDown);
    }
}

function GiftCardHandler_validate() {
    var fields = [
        {fieldId:"giftCardTo", fieldType:"text"},
        {fieldId:"giftCardFrom", fieldType:"text"},
        {fieldId:"giftCardAmt", fieldType:"dropdown"}
    ];
    var anyErrors = false;

    for(var f=0; f<fields.length; ++f)
    {
        var field = fields[f];
        var el = document.getElementById(field.fieldId);
        var errorElementId = field.fieldId + "_Error";
        var hasError = false;
        if(field.fieldType == "text")
        {
            hasError = (el.value.match("^\\s*$") != null);
        }
        if(field.fieldType == "dropdown")
        {
            hasError = (el.value == 0);
        }
        if(hasError)
        {
            showErrorMessage("Please complete the selections marked below.");
            showElement(errorElementId);
            anyErrors = true;
        }
        else
        {
            hideElement(errorElementId);
        }
    }
    return !anyErrors;
}