
GiftCertificate.prototype.addToCart = GiftCertificate_addToCart;
GiftCertificate.prototype.isValid = GiftCertificate_isValid;
GiftCertificate.prototype.isRecipientNameValid = GiftCertificate_isRecipientNameValid;
GiftCertificate.prototype.isRecipientEmailValid = GiftCertificate_isRecipientEmailValid;
GiftCertificate.prototype.isMessageValid = GiftCertificate_isRecipientMessageValid;
GiftCertificate.prototype.isAmountValid = GiftCertificate_isAmountValid;
GiftCertificate.prototype.isSenderNameValid = GiftCertificate_isSenderNameValid;
GiftCertificate.prototype.getFieldText = GiftCertificate_getFieldText;
GiftCertificate.prototype.setFocus = GiftCertificate_setFocus;
GiftCertificate.prototype.clearErrors = GiftCertificate_clearErrors;
GiftCertificate.prototype.showError = GiftCertificate_showError;

function GiftCertificate()
{
}

function GiftCertificate_addToCart()
{
    this.clearErrors();
    if(this.isValid())
    {
        var el = document.getElementById("gcForm");
        el.submit();
    }
}

function GiftCertificate_isValid()
{
    if(this.isRecipientNameValid() &&
            this.isRecipientEmailValid() &&
            this.isMessageValid() &&
            this.isAmountValid() &&
            this.isSenderNameValid())
    {
        return true;
    }
    return false;
}

function GiftCertificate_isRecipientNameValid()
{
    if(!this.getFieldText("recipientName"))
    {
        this.showError("recipientName");
        return false;
    }
    return true;
}

function GiftCertificate_isRecipientEmailValid()
{
    var str = this.getFieldText("recipientEmail");
    if(!str)
    {
        this.showError("recipientEmail-empty");
        return false;
    }
    if(!str.match("[^@\\s]+@\\w+\\.\\w+"))
    {
        this.showError("recipientEmail-invalid");
        return false;
    }
    return true;
}

function GiftCertificate_isRecipientMessageValid()
{
    var str = this.getFieldText("message");
    if(str)
    {
        if(str.length > 120)
        {
            this.showError("message");
            return false;
        }
    }
    return true;
}

function GiftCertificate_isAmountValid()
{
    var str = this.getFieldText("amount");
    if(!str || str.match("^([2-9][0-9]|[1-4][0-9][0-9]|500)$") == null)
    {
        this.setFocus("amount");
        this.showError("amount");
        return false;
    }
    return true;
}

function GiftCertificate_isSenderNameValid()
{
    var str = this.getFieldText("senderName");
    if(!str)
    {
        this.setFocus("senderName");
        this.showError("senderName");
        return false;
    }
    return true;
}

function GiftCertificate_getFieldText(id)
{
    var el = document.getElementById(id);
    if(el)
    {
        var str = el.value;
        if(str == null || str.length == 0 || str.match("^\\s+$") != null)
        {
            return null;
        }
        return str;
    }
    return null;
}

function GiftCertificate_setFocus(id)
{
    var el = document.getElementById(id);
    if(el)
    {
        el.focus();
    }
}

function GiftCertificate_clearErrors()
{
    hideElement("giftcert-error-language");
    hideElement("giftcert-error-recipientName");
    hideElement("giftcert-error-recipientEmail-empty");
    hideElement("giftcert-error-recipientEmail-invalid");
    hideElement("giftcert-error-message");
    hideElement("giftcert-error-amount");
    hideElement("giftcert-error-senderName");
    hideElement("giftcert-errors");
}

function GiftCertificate_showError(name)
{
    showElement("giftcert-error-" + name);
    showElement("giftcert-errors");
}
