$(function() {
    
    // ------------------------------------------------------------------------
    // Add submission Form processing
    // ------------------------------------------------------------------------
    $(".submissionForm").each(function(){
        
        var formURL = $("input[name=_processingFormURL]").attr('value');
        var formID = this.id;
        
        $("form", this).submit(function(){
        
            // Get all the form fields and for each one get the entered
            // value
            var fields = [];
            
            $(":input", this).each(function() {
                fields.push(this.name + '=' + escape(this.value));
            });
            
            // Build the ajax request
            jQuery.ajax({
                type: "POST",
                data: fields.join('&'),
                url: formURL,
                
                error: function(){
                    console.log("Failed to submit");
                },
                
                success: function(r){
                    //alert(r); 
                    if (r == "OK"){
                        //alert("#" + formID);
                        $("#" + formID).css({"display": "none"});
                        $("#" + formID + "_message").css({"display": "block"});
                    }
                    else{
                        //alert(r);
                    }
                }
            });
                
            return false;
            
        })
    })
    
    
    // ------------------------------------------------------------------------
    // Submission admin: on clicking a checkbox, the checkbox state is saved
    // at the corresponding article submission field.
    // ------------------------------------------------------------------------
    $("div[ @id ^='submission_']").each(function(){
        var x = this.id.split("_");
        var articleID = x[1];
        var updateURL = $(":hidden[ @name=_processingFormURL ]", this).attr('value');
        var collection = $(":hidden[ @name=_collection ]", this).attr('value');
        
        // Get all the checkboxes
        $(":checkbox", this).each(function(){
            $(this).click(function(){
                
                
                //alert("ID: " + articleID + " Value: " + this.value + ": " + this.checked + " To be processed by: " + updateURL);
                
                var fields = [];
                
                fields.push("articleID=" + articleID);
                fields.push("field=" + this.value);
                fields.push("checked=" + this.checked);
                fields.push("collection=" + collection)
                var request = fields.join('&')
                
                // Build the ajax request
                jQuery.ajax({
                type: "POST",
                data: request,
                url: updateURL,
                
                error: function(){
                    console.log("Failed to submit");
                },
                
                success: function(r){
                    alert(r);
                }
            });
                
            });
        });
        
    });
    
  
    // TODO: to delete...
    /*var parentID;
    
    // Reply Submission: on click displays add form on a pop-up
    $(".submissionReply").each(function(){
        
        parentID = this.id;
        
        $(this).click(function(){
            window.open("http://myguide/Complete/main/seed3/" + parentID,
                        "Adicionar Resposta",
                        "menubar=0, resizable=1, width=350, height=250");
            });
        })*/
})

