jQuery(function ($) {
    // Add the json format param to each form
    $('form.regionSearch').each(function () {
        $(this).append('<input type="hidden" name="format" value="json">');
    });
    
    $('form.regionSearch').submit(function (e) {
        e.preventDefault();
        var form = $(this);
		var form_type = form.find('input.form-type').val()
        $.get(form.attr('action'), form.serialize(), function (data) {
            var result = $(".regionResult", form);
            if (data.twittername && form_type == 'twitter') {
                result.html('Your region is <a href="http://twitter.com/' + data.twittername + '">' + data.title + '</strong>');
			} else if (form_type == 'rss') {
				result.html('Your region is <a href="/who-we-are/working-in-the-regions/' + data.title.replace(' ','-').toLowerCase() + '">' + data.title + '</strong>');
            } else {
                result.html('Your region could not be found');
            }
        }, 'json');
    });
});
