// tailor.js
// Script Copyright (c) 2007 Appropriate Solutions, Inc. All rights reserved.
// Written as Work for Hire for lumino.us.
//

// Basic outline of function derived from JavaScript--The Definitive Guide.
// Send form information to a cgi script (which will email it) and then
// allow normal submit function to flow through.
// Routine could be a bit more terse, but left this way for easy future maintenance.
// Requires mootools.
(function() { 
    // initialize after document loads
    if(window.addEventListener) 
        window.addEventListener("load", init, false);
    else if(window.attachEvent) 
        window.attachEvent("onload", init);

    // define event handler for form
    function init() {
        // Loop through all the forms in the document
        // (in our instance, there is but one--if there was more than one, we'd 
        // need to check the name)
        for( var i=0; i<document.forms.length; i++) {
            var f = document.forms[i];
            f.onsubmit = sendOnSubmit;
        }
    }
    
    function sendOnSubmit() {
        // variable to track form data (fd)
        var fd = ""
        
        // Loop through all form elements and build a parameter string
        // to submit to the cgi script.
        
        // Since we're submitting to PayPal, we need to renumber all the 
        // items so they are sequential, starting at 1. 
        // Also need to renumber the associated amounts which appear immediately
        // after the radio. It is IMPORTANT that this physical arrangement
        // be maintained in all future web modifications, or this code will fail
        // and prices will not be synchronized with the item purchased.
        checked_id = 1;
        unchecked_id = 999;
        class_selected = false;     // assume no class is selected
        missing_required = false;   // assume no required fields are missing
        fix_next_amount = false;    // if true, we need to renumber the checked amount
        for( var i=0; i < this.elements.length; i++) {
            var e = this.elements[i];
            if( e.type == "text")
            { 
                // always pass the parameter, even field is empty.
                // makes for consistent emails.
                fd = fd + e.id + '=' + encodeURIComponent(e.value) + '&';
                
                // IF className contains required, check we have something. 
                // NOTE: this is a match, not an equality since the field
                // will have multiple class attributes.
                required = e.className.match('required');
                if( required) {
                    if( e.value.length == 0) {
                        missing_required = true;
                    }
                }
            }
            
            if( e.type == "radio") {
                if( e.checked ) {
                    // Should already be encoded if we pass HTML validation.
                    fd = fd + e.id + '=' + e.value + '&';
                    
                    // change the id to track along with us
                    e.name = 'item_name_' + checked_id;
                    fix_next_amount = true;
                    class_selected = true;
                } else
                {
                    // not checked, get the name out of the way
                    e.name = 'item_name_' + unchecked_id;
                    fix_next_amount = false;
                }
            }
            
            if( e.type == "hidden")
            {
                if( e.name.search(/amount_/) >= 0)
                {
                    if( fix_next_amount)
                    {
                        e.name = 'amount_' + checked_id++;
                        fix_next_amount = false;
                    } else
                    {
                        e.name = 'amount_' + unchecked_id++;
                    }
                }
            }
            
            if( e.type == "textarea") {
                fd = fd + e.id + '=' + encodeURIComponent(e.value) + '&';
            }
        }
        
        alert_string = 'Please ';
        if( missing_required) {
            alert_string += 'complete all required fields (marked with *)';
        }
        
        if( !class_selected) {
            if( missing_required) {
                alert_string += ' and ';
            }
            alert_string += 'select a class';
        }
        
        alert_string += '.';
        
        if( class_selected && (! missing_required)) {
            // XHR is from mootools.
            var myXHR = new XHR({method: 'get', async: false}).send('http://flamenco-vivo.org/cgi-bin/tailor', fd);
    //        alert(myXHR.response.text);   // debugging
            return true;
        } else 
        {
            alert(alert_string);
            return false;
        }
    }
})();
