//  ================================================================================
//  FORM FUNCTIONS
//  --------------------------------------------------------------------------------
//  Author: Michael Maw
//  Last Modified: 2008/04/28
//  ================================================================================

//  FUNCTION LIST
//  ================================================================================
//  01) submitForm(formID, domID)
//  02) populateSelect(selectID, url, sHeader, bSelectAll)
//  03) multiSelectAll(selectID)
//  04) showFormMsg(domID, sMsg, sType)


//	GLOBAL VARIABLES
//	================================================================================
    var loadingImagePath = "/images/animated/loading.gif";
    var activeMsgID;
    
    
//  PRELOAD IMAGES
//	================================================================================
    var loadingImage = new Image();
    loadingImage.src = loadingImagePath;
   
   
//	01) Submit form using AJAX and populate an element with the response
//	================================================================================
//  Usage: submitForm('formID', 'domID', true);
//  ================================================================================
    function submitForm(formID, domID, showMsg) {
        try {
                // Display a "loading" image
                if (!showMsg) { $(domID).update("<div style='width:100%; text-align:center;'><img src='" + loadingImagePath + "' /></div>"); }

                $(formID).request({
                    onSuccess: function(transport) {
                        var sType = 'Success';
                        var sResponse = transport.responseText;
                    
                        // Show a Form Message
                        if (showMsg) {
                            if (sResponse.search(/Error/g) != -1) { sType = 'Failure'; }
                            showFormMsg(domID, sResponse, sType);
                        // OR Update the Element
                        } else {
                            $(domID).update(sResponse);
                        }
                    },
                    onFailure: function(transport) {
                        // Show a Form Error Message
                        if (showMsg) { 
                            showFormMsg(domID, transport.responseText, 'Failure');
                        } else {
                            alert("ERROR: " + e.message);
                        }
                    }
                });
                return true;
        } catch (e) {
            alert("ERROR: " + e.message);
            return false;
        }
    }
    
//	02) Populate a SELECT object via an AJAX request
//	================================================================================
//  selectID    = DOM ID of the <select> object to be populated
//  url         = URL of the AJAX script which returns a list of <option> values
//  sHeader     = List items to insert at the top of the list (HTML format)
//  bSelectAll  = Automatically select all list items after populating (true/false)
//  --------------------------------------------------------------------------------
//  Usage:      populateSelect("selectID", "/ajax/sql_formlist.aspx", 
//              "<option value=''>Select from list...</option>", false);
//  Note:       Sets the "populated" property of the SELECT object to TRUE when complete
//  ================================================================================
    function populateSelect(selectID, url, sHeader, bSelectAll) {
        try {
                $(selectID).update("<option value=''>Loading...</option>");
                $(selectID).populated = false;

                new Ajax.Request(url, {
                    method: 'get', 
                    onSuccess: function(transport) {
                        // Error Checking
                        if ((transport.responseText == "Error") || (transport.responseText == "")) {
                            $(selectID).update("<option value=''>Not Available</option>");
                            $(selectID).populated = true;
                        }
                        else {                          
                            // Update the SELECT object
                            $(selectID).update(sHeader + transport.responseText);
                            
                            // If bSelectAll is true, select all options
                            if (bSelectAll) { multiSelectAll(selectID); }
                            $(selectID).populated = true;
                        }
                    }
                });
                return true;
        } catch (e) {
            alert("ERROR: " + e.message);
            return false;
        }
    }

//	03) Selects all options in a SELECT object with a "multiple" attribute
//	================================================================================
    function multiSelectAll(selectID) {
        try {
            // Cycle backwards through all <option> elements and select them
            for (i=$(selectID).length-1; i>-1; i--) {
                $(selectID).options[i].selected="selected";
            }
            return true;
        } catch (e) {
            alert("ERROR: " + e.message);
            return false;
        }
    }
    
//	04) Show a Form Message (ie. Success or Failure)
//	================================================================================
//  Usage: showFormMsg('msgID', 'Insert message here', 'Error');
//  ================================================================================
    function showFormMsg(domID, sMsg, sType) {
        try {
            activeMsgID = domID;
            
            // Toggle CSS Class
            if (sType == 'Success') {
                $(activeMsgID).removeClassName('form_error');
                $(activeMsgID).addClassName('form_success');
            } else {
                $(activeMsgID).removeClassName('form_success');
                $(activeMsgID).addClassName('form_error');
            }
            
            // Add custom message string
            if (Object.isString(sMsg)) { $(activeMsgID).update(sMsg); }
            
            // Show the Message
            new Effect.Appear(activeMsgID);
            
            // Hide the Message
            setTimeout("new Effect.Fade(activeMsgID);", 3000);
            return true;
        } catch (e) {
            alert("ERROR: " + e.message);
            return false;
        }
    }