/*
 *
 * Copyright (c) 2008 George Bonnes (george@olm1.com)
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 * Version 0.9.1
 *
 * $LastChangedDate$
 * $Rev$
 *
 */


ajax_error = 0;

function ajax_update(container_id, request_url, success_callback) {
    
    container_id = container_id.replace(/^.*#/, '');
    
    $.ajax({
        url: request_url,
        type: 'GET',
        dataType: 'html',
        timeout: 20000,
        beforeSend: function(){
            ajax_error = 0;
            //alert('container ' + container_id + '\nurl ' + request_url);
        },                
        error: function(){
            ajax_error = 1;
            ajax_update_error();
        },
        success: function(xml){
            $('#' + container_id).html(xml)
                                 .css('display', 'block')
                                 .css('opacity', '100');
                                 
             if (typeof success_callback == 'function') {
                 success_callback();
             }
        }
    });  
    
}


// call within a $(document.ready() handler so that this initializes the form in the background
function ajax_form(form_id, container_id, success_callback) {
    
    container_id = container_id.replace(/^.*#/, '');
    var options = { 
        target:        '#' + container_id,   // target element(s) to be updated with server response 
        beforeSubmit: function(a,f,o) {
            ajax_error = 0;
        },                
        error: function(){
            ajax_error = 1;
            ajax_update_error();
        },
        success:        function(xml){
            // do something with xml
            $('#' + container_id).html(xml);
                                 
             if (typeof success_callback == 'function') {
                 success_callback();
             } else { alert('not function'); }
        },
        dataType:   'html',
        type:       'post',
        timeout:    30000 
 
        // other available options: 
        //url:       url         // override for form's 'action' attribute 
        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
        //clearForm: true        // clear all form fields after successful submit 
        //resetForm: true        // reset the form after successful submit 
 
    }; 
 
    // bind form using 'ajaxForm' 
    $('#' + form_id).ajaxForm(options);             
}   

function ajax_upload_form(form_id, container_id) {
    container_id = container_id.replace(/^.*#/, '');
    $(function() {
        $('#' + form_id).ajaxForm({
            dataType:   'html',
            timeout:    30000,
            beforeSubmit: function(a,f,o) {
                ajax_error = 0;
            },                
            error: function(){
                ajax_error = 1;
                ajax_update_error();
            },
            success: function(data) {
                var $out = $('#upload_output');
                //$out.html('Form success handler received: <strong>' + typeof data + '</strong>');
                if (typeof data == 'object' && data.nodeType)
                    data = elementToString(data.documentElement, true);
                else if (typeof data == 'object')
                    data = objToString(data);
                
                $('#' + container_id).html(data);
            }
        });    
    });    
}



// helper for ajax_upload_form
function objToString(o) {
    var s = '{\n';
    for (var p in o)
        s += '    ' + p + ': ' + o[p] + '\n';
    return s + '}';
}

// helper for ajax_upload_form
function elementToString(n, useRefs) {
    var attr = "", nest = "", a = n.attributes;
    for (var i=0; a && i < a.length; i++)
        attr += ' ' + a[i].nodeName + '="' + a[i].nodeValue + '"';

    if (n.hasChildNodes == false)
        return "<" + n.nodeName + "\/>";

    for (var i=0; i < n.childNodes.length; i++) {
        var c = n.childNodes.item(i);
        if (c.nodeType == 1)       nest += elementToString(c);
        else if (c.nodeType == 2)  attr += " " + c.nodeName + "=\"" + c.nodeValue + "\" ";
        else if (c.nodeType == 3)  nest += c.nodeValue;
    }
    var s = "<" + n.nodeName + attr + ">" + nest + "<\/" + n.nodeName + ">";
    return useRefs ? s.replace(/</g,'&lt;').replace(/>/g,'&gt;') : s;
};     




function ajax_update_error() {
    error_message = '<center>Error Communicating '
            + 'with the Server.<br>Please Try Again.<br>'
            + '<a href="javascript:void(0);" onClick="ajax_hide_error();">'
            + '(Close)</a></center>';                
    $('#loading').html('<div class="flash_message_error">' + error_message + '</div>').centerElement().vCenterElement();
}

function ajax_hide_error() {
    $('#loading').jqmHide();
}


