// animal-alphabet-growth-chart.html
// double-charms-name-necklace-in-silver.html

$(document).ready( function() {
    var frm = $("form[action*='order.store.yahoo.net']");
    if (frm.length == 0)
    {
        frm = $("form[action*='cgi-bin/test-order']")
    }
    frm.submit( function()
    {
        var frm = this;
        var formRetval = true;
        
        $("textarea[maxlength]").each( function()
        {
            var maxlength = $(this).attr("maxlength");
            if ($(this).val().length > maxlength)
            {
                alert("Please enter at most " + maxlength + " characters in " + $(this).attr("name") + ".");
                formRetval = false;
                return(false);
            }
        });
        if (!formRetval)
        {
            return(false);
        }
        // find all select elements
        $(this).find("select").each( function() {
            // get the multi add prefix if exists
            var vwattr = "";
            var vwattrRe = /vwattr\d+_/;
            var vwattrM = this.name.match(vwattrRe);
            if (vwattrM)
            {
                vwattr = vwattrM[0];
            }
            // see if it starts with Add..
            if (this.name.substr(0,4 + vwattr.length).toLowerCase() == vwattr + 'add ')
            {
                // ok, check if there is at least one option that adds a price
                var markupFound = false;
                var optRe = /\(([\d\s\+\-\.\$]+)\)/;
                $(this).find("option").each( function() {
                    if (optRe.test( $(this).val() ) )
                    {
                        markupFound = true;
                    }
                });
                if (markupFound)
                {
                    // get the name of the corresponding optional text field
                    var fldname = this.name.substr(4 + vwattr.length);
                    var foundCustomized = false;

                    $(frm).find("input[fldname='" + vwattr + fldname + "'],input[name='" + vwattr + fldname + "'],textarea[fldname='" + vwattr + fldname + "'],textarea[name='" + vwattr + fldname + "']").each( function() {
                        if ($(this).val() != "")
                        {
                            foundCustomized = true;
                        }
                    });

                    if (optRe.test( $(this).val() ))
                    {
                        // if we have an option where a markup is selected, make sure that 
                        // there is a corresponding customization field filled out
                        if (!foundCustomized && $("input[fldname='" + vwattr + fldname + "'],input[name='" + vwattr + fldname + "'],textarea[fldname='" + vwattr + fldname + "'],textarea[name='" + vwattr + fldname + "']").length > 0 )
                        {
                            alert("Please enter your customization.");
                            formRetval = false;
                            return (formRetval);
                        }
                    }
                    else
                    {
                        // otherwise, if there is no customization option selected but 
                        // customization is entered, issue a warning.
                        if (foundCustomized)
                        {
                            alert("Please select a customization option");
                            this.focus();
                            formRetval = false;
                            return (formRetval);
                        }
                    }
                }
            }
        });
        return(formRetval);
    });
});
