/**
 * Ajax Helper Function
 * Turn synchronous links into asynchronous links
 *
 *
 */

function popinAjaxLinks()
{
    $('.popin-full a[display=popin]').click(
        function(event)
        {
            $.ajax(
            {
                type: 'GET',
                url: $(this).attr('href'),
                success: function(data)
                {
                    $('.popin-full .pop-content').html(data);
                }
            });
            return false;
        }
    );
}


/**
 * Ajax Popin
 * Request Document Form
 * Request Tryout Form
 *
 */


function popinRequestForm(idform)
{
    $('.popin-full form' + idform).submit(
        function(event)
        {
            // get form fields and select
            var dataString = '';
            var form_selector = $('.popin-full form'+ idform);
            var inputs = form_selector.find('input[type=text], input[type=hidden], input:checked');
            var inputs_selected = form_selector.find(':selected').parent();

            inputs.each
            (
                function()
                {
                    if ($(this).val() != '' )
                    {
                        dataString = dataString + $(this).attr('name') + '=' + $(this).val() + '&';
                    }
                }
            )
            inputs_selected.each
            (
                function()
                {
                    if ($(this).val() != '' )
                    {
                        dataString = dataString + $(this).attr('name') + '=' + encodeURIComponent($(this).val()).replace(/%20/g, '+') + '&';
                    }
                }
            )


            dataString = dataString.substr(0, dataString.length - 1);

            // submit
            $.ajax(
            {
                type: 'POST',
                url: form_selector.attr('action'),
                data: dataString,
                success: function(data)
                {
                    //form validated, returned confirm url
                    // RegExp Object sucks ! test and exec method fail
                    if (data.substr(0,7) == 'http://')
                    {
                        $.ajax(
                        {
                            type: 'GET',
                            url: data,
                            success: function(confirm_data)
                            {
                                $('.popin-full .pop-content').html(confirm_data);
                            }
                        });
                    }

                    else
                    {
                         $('.popin-full .pop-content').html(data);
                    }
                }
            });

            return false;
        }
    );
}

function popinRequestFormSelectDealer(idform)
{
    $('.popin-full form'+ idform +' input[name=dealer_id]').change( function()
    {
            // get form fields
            var dataString = '';
            var inputs_checked = $('.popin-full form'+ idform+' input:checked');

            inputs_checked.each
            (
                function()
                {
                    if ($(this).val() != '' )
                    {
                        dataString = dataString + $(this).attr('name') + '=' + $(this).val() + '&';
                    }
                }
            )

            dataString = dataString.substr(0, dataString.length - 1);

            // submit
            $.ajax(
            {
                type: 'POST',
                url: $('.popin-full form'+ idform ).attr('action'),
                data: dataString,
                success: function(data)
                {
                    if (data.substr(0,7) == 'http://')
                    {
                        $.ajax(
                        {
                            type: 'GET',
                            url: data,
                            success: function(confirm_data)
                            {
                                $('.popin-full .pop-content').html(confirm_data);
                            }
                        });
                    }

                    else
                    {
                         $('.popin-full .pop-content').html(data);
                    }
                }
            });
    });
}