//

$(document).ready(function()
{
    $("#domainSearchTextfield").data("activated", false);

    $("#domainSearchTextfield").click(function()
    {
        var domainNameObj = $("#domainSearchTextfield");
        if (domainNameObj.data("activated"))
        {
            return false;
        }
        domainNameObj.val("");
        domainNameObj.css("color", "#333333");
        domainNameObj.data("activated", true);
        return false;
    });

    $("#submitSearchButton").click(function()
    {
        var domainNameObj = $("#domainSearchTextfield");
        var domainName = domainNameObj.val();
        if (!domainNameObj.data("activated"))
        {
            domainName = "";
        }
        window.location = "DomainLookup.fvnx?d=" + escape(domainName);
        return false;
    });

    $("#submitCouponCodeButton").click(function()
    {
        var couponCode = $("#couponCode").val();
        if (couponCode == "")
        {
            jAlert('error', 'Invalid coupon code', 'Add Coupon');
            return false;
        }
        $.ajax(
        {
            type: "POST",
            url: "AddCouponToBasket.fvnx?c=" + couponCode,
            dataType: "xml",
            success: function(xml)
            {
                var status = xmlValue(xml, "status");
                if (status == "ok")
                {
                    jAlert('success', 'Thank you! Your coupon has been added', 'Add Coupon');
                    window.location.reload(true);
                }
                else if (status == "coupon_not_found")
                {
                    jAlert('error', 'Invalid coupon code', 'Add Coupon');
                }
                else if (status == "coupon_none_remaining")
                {
                    jAlert('error', 'Sorry; no more coupons of this type are available', 'Add Coupon');
                }
                else if (status == "coupon_not_yet_active")
                {
                    jAlert('error', 'Sorry; no more coupons of this type available', 'Add Coupon');
                }
                else if (status == "coupon_expired")
                {
                    jAlert('error', 'Sorry; no more coupons of this type available', 'Add Coupon');
                }
                else if (status == "coupon_not_applicable")
                {
                    message = xmlValue(xml, "message");
                    if (message == null)
                    {
                        message = "This coupon cannot be used with the items currently in your basket";
                    }
                    jAlert('error', message, 'Add Coupon');
                }
                else
                {
                    alert("ERROR: Unexpected status: " + status);
                }
            }
        });
        return false;
    });
});


function removeItemFromBasket(pItemId)
{
    $.ajax(
    {
        type: "POST",
        url: "RemoveItemFromBasket.fvnx?i=" + pItemId,
        dataType: "xml",
        success: function(xml)
        {
            var status = xmlValue(xml, "status");
            if (status == "ok")
            {
                window.location.reload(false);
            }
            else
            {
                alert("ERROR: Unexpected status: " + status);
            }
        }
    });
}
