
/**
* Contact form submit handler
*/
function ajax_form(options)
{
    $('.submit').attr('disabled', 'disabled');

    $.ajax({
        type: "POST",
        dataType: "json",
        url: (options['proxy_url']) ? options['proxy_url'] : options['url'],
        data: $('#'+options['form_id']).serialize() + '&form_id=' + options['form_id'] + '&url=' + options['url'],
        complete: function()
        {
            $('.submit').removeAttr('disabled');
        },
        success: function(response)
        {
            //console.log(response);
            if(response.errors)
            {
                $('#' + options['error_id']).html(response.errors).slideDown(300);
            }
            else if(response.thanks_url)
            {
                window.location = response.thanks_url;
            }
            else if(response.success)
            {
                $('#' + options['error_id']).slideUp(300);
                $('#' + options['form_id']).slideUp(300);
                $('#' + options['success_id']).slideDown(300);
            }
            else
            {
                $('#' + options['error_id']).html('Sorry an unexpected error has occured please try again later').slideDown(300);
            }
        }
    });
}


function ajax_postcode(options)
{
    $('.submit').attr('disabled', 'disabled');

    $('#postcode_no').hide();
    $('#postcode_results').html('Please Wait...').show();

    $.ajax({
        type: "POST",
        dataType: "json",
        url: options['proxy_url'],
        data: $('#'+options['form_id']).serialize() + '&site_id=' + options['site_id'] + '&url=' + options['url'],
        complete: function()
        {
            $('.submit').removeAttr('disabled');
        },
        success: function(response)
        {
            if(response == null)
            {
                $('#' + options['error_id']).html('Sorry an unexpected error has occured please try again later').slideDown(300);
            }
            else if(response.error)
            {
                $('#' + options['error_id']).html(response.error).slideDown(300);
            }
            else if(response.url)
            {
                window.location = response.url;
            }
            else if(response.locations)
            {
                // display results
                $('#postcode_results').html(response.locations).slideDown(300);
            }
            else
            {
                $('#' + options['error_id']).html('No locations have been found for the given postcode').slideDown(300);
            }
        }
    });
}

