function calculate_price() {
    var csrftoken = $.cookie('csrftoken');

    function csrfSafeMethod(method) {
        // these HTTP methods do not require CSRF protection
        return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
    }
    function sameOrigin(url) {
        // test that a given url is a same-origin URL
        // url could be relative or scheme relative or absolute
        var host = document.location.host; // host + port
        var protocol = document.location.protocol;
        var sr_origin = '//' + host;
        var origin = protocol + sr_origin;
        // Allow absolute or scheme relative URLs to same origin
        return (url == origin || url.slice(0, origin.length + 1) == origin + '/') ||
            (url == sr_origin || url.slice(0, sr_origin.length + 1) == sr_origin + '/') ||
            // or any other URL that isn't scheme relative or absolute i.e relative.
            !(/^(\/\/|http:|https:).*/.test(url));
    }

    // Get the form values
    var title = $('input:radio[name=form1-title]:checked').val();
    var duration = $('input:radio[name=form1-duration]:checked').val();
    var shipping_method = $('input:radio[name=form1-shipping_method]:checked').val();
    var region1 = $("#id_form2-region").val();
    var postal_code1 = $("#id_form2-postal_code").val();
    var country_code1 = $("#id_form2-country").val();
    var region2 = $("#id_form3-region").val();
    var postal_code2 = $("#id_form3-postal_code").val();
    var country_code2 = $("#id_form3-country").val();
    var host = document.location.host;
    var store = "/store";

    $.ajax({
        type: 'POST',
        url: "//" + host + store + "/item/price/",
        data: {
            title: title, duration:duration, shipping_method:shipping_method,
            region1:region1, postal_code1:postal_code1,
            country_code1: country_code1, region2:region2,
            postal_code2:postal_code2, country_code2: country_code2
        },
        beforeSend:function(xhr, settings) {
            $('#ajax-panel').html('<p>Loading....</p>');

            if (!csrfSafeMethod(settings.type) && sameOrigin(settings.url)) {
                // Send the token to same-origin, relative URLs only.
                // Send the token only if the method warrants CSRF protection
                // Using the CSRFToken value acquired earlier
                xhr.setRequestHeader("X-CSRFToken", csrftoken);
            }
        },
        success:function(data){
            // successful request; do something with the data
            $('#ajax-panel').empty();
            $('#ajax-panel').append(data);
        },
        error:function(){
            // failed request; give feedback to user
            $('#ajax-panel').html('<p class="error"><strong>Oops!</strong> Try that again in a few moments.</p>');
        }
    });
}

function check_state() {
    var country_code1 = $("#id_form2-country").val();
    var country_code2 = $("#id_form3-country").val();
    var country_code3 = $("#id_form5-country").val();

    var foreign1 = country_code1 != 'US';
    var country_code2_is_empty = (typeof country_code2 == 'undefined' || country_code2 == '');
    var foreign2 = (country_code2 != 'US' && !country_code2_is_empty);
    var foreign3 = country_code3 != 'US';

    if (foreign1 == true) {
        $("#id_form2-region").val('0');
        $("#id_form2-region").prop('disabled', true);
	} else {
		$("#id_form2-region").prop('disabled', false);
	}
    
	if (foreign2 == true) {
		$("#id_form3-region").val('0');
		$("#id_form3-region").prop('disabled', true);
    } else {
		$("#id_form3-region").prop('disabled', false);
	}

    if (foreign3 == true) {
        $("#id_form5-region").val('0');
        $("#id_form5-region").prop('disabled', true);
    } else {
        $("#id_form5-region").prop('disabled', false);
    }

    if (foreign1 == true || foreign2 == true) {
        $('input[name=form1-duration][value=1]').prop("checked",true);
        $('input[name=form1-duration]').prop("disabled",true);

        $('input[name=form1-shipping_method][value=1]').prop("checked",true);
        $('input[name=form1-shipping_method]').prop("disabled",true);
    } else {
        $('input[name=form1-duration]').prop("disabled",false);
        $('input[name=form1-shipping_method]').prop("disabled",false);
    }
}

window.onload = check_state();
window.onload = calculate_price();
