function getFormData(form_id)
{
    var form = document.getElementById(form_id);
    var query_string = '';

    for(i=0; i<form.elements.length; i++)
    {
        if(i==0)
        {
            if(form.elements[i].name && form.elements[i].value)
            {
                // Check if element is a radio checkbox
                if(form.elements[i].type == 'radio')
                {
                    if(form.elements[i].checked == true)
                        query_string += form.elements[i].name + '=' + encodeURIComponent(form.elements[i].value);
                }
                else
                    query_string += form.elements[i].name + '=' + encodeURIComponent(form.elements[i].value);
            }
        }
        else
        {
            if(form.elements[i].name && form.elements[i].value)
            {
                // Check if element is a radio checkbox
                if(form.elements[i].type == 'radio')
                {
                    if(form.elements[i].checked == true)
                        query_string += '&' + form.elements[i].name + '=' + encodeURIComponent(form.elements[i].value);
                }
                else
                    query_string += '&' + form.elements[i].name + '=' + encodeURIComponent(form.elements[i].value);
            }
        }
    }

    return query_string;
}

function ajaxAction(uri, target_element, loading_image, form_id, data_string)
{
    if(form_id)
    {
        $.ajax({
            type: 'POST',
            url: uri,
            cache: false,
            data: getFormData(form_id),
            beforeSend: function()
            {
                $('#' + target_element).html('<img src="' + loading_image + '" alt=""/>');
            },
            success: function(html)
            {
                $('#' + target_element).html(html);
            }
        });
    }
    else if(data_string)
    {
        $.ajax({
            type: 'POST',
            url: uri,
            cache: false,
            data: data_string,
            beforeSend: function()
            {
                $('#' + target_element).html(' <img src="' + loading_image + '" alt="" id="loading_image" style="display: inline-block;"/> ');
            },
            success: function(html)
            {
                $('#' + target_element).html(html);
            }
        });
    }
    else
    {
        $.ajax({
            type: 'POST',
            url: uri,
            cache: false,
            beforeSend: function()
            {
                $('#' + target_element).html('<img src="' + loading_image + '" alt=""/>');
            },
            success: function(html)
            {
                $('#' + target_element).html(html);
            }
        });
    }
}

function animatedAjaxAction(uri, target_element, loading_image, form_id, data_string)
{
    if(form_id)
    {
        $.ajax({
            type: 'POST',
            url: uri,
            cache: false,
            data: getFormData(form_id),
            beforeSend: function()
            {
                $('#' + target_element).html(' <img src="' + loading_image + '" alt="" id="loading_image" style="display: inline-block;"/> ');
            },
            success: function(html)
            {
                $('#' + target_element).animate({
                    opacity: 0
                }, {
                    complete: function()
                    {
                        $('#' + target_element).animate({
                            opacity: 1
                        });
                        $('#' + target_element).html(html);
                    }
                });
            }
        });
    }
    else if(data_string)
    {
        $.ajax({
            type: 'POST',
            url: uri,
            cache: false,
            data: data_string,
            beforeSend: function()
            {
                $('#' + target_element).html(' <img src="' + loading_image + '" alt="" id="loading_image" style="display: inline-block;"/> ');
            },
            success: function(html)
            {
                $('#' + target_element).animate({
                    opacity: 0
                }, {
                    complete: function()
                    {
                        $('#' + target_element).animate({
                            opacity: 1
                        });
                        $('#' + target_element).html(html);
                    }
                });
            }
        });
    }
    else
    {
        $.ajax({
            type: 'POST',
            url: uri,
            cache: false,
            beforeSend: function()
            {
                $('#' + target_element).html(' <img src="' + loading_image + '" alt="" id="loading_image" style="display: inline-block;"/> ');
            },
            success: function(html)
            {
                $('#' + target_element).animate({
                    opacity: 0
                }, {
                    complete: function()
                    {
                        $('#' + target_element).animate({
                            opacity: 1
                        });
                        $('#' + target_element).html(html);
                    }
                });
            }
        });
    }
}

function addElementAjaxAction(url, target_element, loading_image, type_of_action, form_id, data_string)
{
    if(form_id)
    {
        $.ajax({
            type: 'POST',
            url: url,
            cache: false,
            data: getFormData(form_id),
            beforeSend: function()
            {
                switch(type_of_action)
                {
                    case 'prepend':
                        $('#' + target_element).prepend(' <img src="' + loading_image + '" alt="" id="loading_image" style="display: inline-block;"/> ');
                        break;
                    case 'append':
                        $('#' + target_element).append(' <img src="' + loading_image + '" alt="" id="loading_image" style="display: inline-block;"/> ');
                        break;
                }
            },
            success: function(html)
            {
                $('#loading_image').animate({
                    opacity: 0
                }, {
                    complete: function()
                    {
                        $('#loading_image').remove();
                        switch(type_of_action)
                        {
                            case 'prepend':
                                $('#' + target_element).prepend(html);
                                break;
                            case 'append':
                                $('#' + target_element).append(html);
                                break;
                        }
                    }
                });
            }
        });
    }
    else if(data_string)
    {
        $.ajax({
            type: 'POST',
            url: url,
            cache: false,
            data: data_string,
            beforeSend: function()
            {
                switch(type_of_action)
                {
                    case 'prepend':
                        $('#' + target_element).prepend(' <img src="' + loading_image + '" alt="" id="loading_image" style="display: inline-block;"/> ');
                        break;
                    case 'append':
                        $('#' + target_element).append(' <img src="' + loading_image + '" alt="" id="loading_image" style="display: inline-block;"/> ');
                        break;
                }
            },
            success: function(html)
            {
                $('#loading_image').animate({
                    opacity: 0
                }, {
                    complete: function()
                    {
                        $('#loading_image').remove();
                        switch(type_of_action)
                        {
                            case 'prepend':
                                $('#' + target_element).prepend(html);
                                break;
                            case 'append':
                                $('#' + target_element).append(html);
                                break;
                        }
                    }
                });
            }
        });
    }
    else
    {
        $.ajax({
            type: 'POST',
            url: url,
            cache: false,
            beforeSend: function()
            {
                switch(type_of_action)
                {
                    case 'prepend':
                        $('#' + target_element).prepend(' <img src="' + loading_image + '" alt="" id="loading_image" style="display: inline-block;"/> ');
                        break;
                    case 'append':
                        $('#' + target_element).append(' <img src="' + loading_image + '" alt="" id="loading_image" style="display: inline-block;"/> ');
                        break;
                }
            },
            success: function(html)
            {
                $('#loading_image').animate({
                    opacity: 0
                }, {
                    complete: function()
                    {
                        $('#loading_image').remove();
                        switch(type_of_action)
                        {
                            case 'prepend':
                                $('#' + target_element).prepend(html);
                                break;
                            case 'append':
                                $('#' + target_element).append(html);
                                break;
                        }
                    }
                });
            }
        });
    }
}

function jumpToNextInputField(self, target_element_id)
{
    if(document.getElementById(self.id).value.length == document.getElementById(self.id).maxLength)
    {
        document.getElementById(target_element_id).focus();
        document.getElementById(target_element_id).select();
    }
}

function hoverHighlight(action, target_element_id)
{
    // Action is either "in" or "out", where in means on hover and out means on out.
    if(action == 'in')
    {
        document.getElementById(target_element_id).style.backgroundColor = '#eeeeee';
        document.getElementById(target_element_id).style.cursor = 'pointer';
    }
    else if(action == 'out')
    {
        document.getElementById(target_element_id).style.backgroundColor = '#ffffff';
    }
}
