$(document).ready(function() {

    //    alert('vid = ' + vid);
    hideDurchmesser();

    $('#order_article_material_id').focus();

    var sbheight = 560;
    sbheight = $('#deinpreis').height();
    $('#sidebar .sbdeinpreis').height(sbheight);

    //    $('#pdurchmesser').hide();

    $('#order_article_material_id').live('change', function() {
        isDisplayRebateTable = false;
        if ($('#calculator #rebatetable h3').hasClass('active')) {
            isDisplayRebateTable = true;
        }
        var $thisform = $('#calcform');
        var url = $thisform.attr('action');

        url = "/calc/" + $('#order_article_material_id').val() + "/1";

//        alert(url);

//        Unset the verarbeitung, so that no verarbeitung is selected, when a new material is selected
        $('#order_article_verarbeitung_id').val(null);

        var values = $('#calcform').serialize();
        $.ajax({
            type: "POST",
            url: url,
            cache: false,
            data: values,
            //                        data: "item_material_id=" + $(this).val(),
            success: function(j) {

                $('#calcformdiv').html(j),hideDurchmesser(),$('#order_article_material_id').focus();
                //                                $('.materialtypedescription').html(j);
                if (isDisplayRebateTable) {
                    $('#calculator #rebatetable h3').addClass('active');
                }
            }
        });
    })

    $('#order_article_anzahl').live('change', function() {
        checkMinVals();
        isDisplayRebateTable = false;
        if ($('#calculator #rebatetable h3').hasClass('active')) {
            isDisplayRebateTable = true;
        }
        var $thisform = $('#calcform');
        var url = $thisform.attr('action');
        url = "/calc/" + $('#order_article_material_id').val() + "/1";
        var values = $('#calcform').serialize();
        $.ajax({
            type: "POST",
            url: url,
            data: values,
            success: function(j) {
                $('#calcformdiv').html(j),hideDurchmesser(),$('#order_article_verarbeitung_id').focus();
                if (isDisplayRebateTable) {
                    $('#calculator #rebatetable h3').addClass('active');
                }
            }
        });
    })

    $('#order_article_breite_mm').live('change', function() {
        checkMinVals();
        isDisplayRebateTable = false;
        if ($('#calculator #rebatetable h3').hasClass('active')) {
            isDisplayRebateTable = true;
        }
        var $thisform = $('#calcform');
        var url = $thisform.attr('action');
        url = "/calc/" + $('#order_article_material_id').val() + "/1";
        var values = $('#calcform').serialize();
        $.ajax({
            type: "POST",
            url: url,
            data: values,
            success: function(j) {
                $('#calcformdiv').html(j),hideDurchmesser(),$('#order_article_hoehe_mm').focus();
                if (isDisplayRebateTable) {
                    $('#calculator #rebatetable h3').addClass('active');
                }
            }
        });
    })

    $('#order_article_hoehe_mm').live('change', function() {
        checkMinVals();
        isDisplayRebateTable = false;
        if ($('#calculator #rebatetable h3').hasClass('active')) {
            isDisplayRebateTable = true;
        }
        var $thisform = $('#calcform');
        var url = $thisform.attr('action');
        url = "/calc/" + $('#order_article_material_id').val() + "/1";
        var values = $('#calcform').serialize();
        $.ajax({
            type: "POST",
            url: url,
            data: values,
            success: function(j) {
                $('#calcformdiv').html(j),hideDurchmesser(),$('#order_article_anzahl').focus();
                if (isDisplayRebateTable) {
                    $('#calculator #rebatetable h3').addClass('active');
                }
            }
        });
    })

    $('#order_article_verarbeitung_id').live('change', function() {
        isDisplayRebateTable = false;
        if ($('#calculator #rebatetable h3').hasClass('active')) {
            isDisplayRebateTable = true;
        }
        var $thisform = $('#calcform');
        var url = $thisform.attr('action');
        url = "/calc/" + $('#order_article_material_id').val() + "/1";
        var values = $('#calcform').serialize();
        var vid = $(this).val();
        $.ajax({
            type: "POST",
            url: url,
            data: values,
            success: function(j) {
                $('#calcformdiv').html(j),hideDurchmesser(),$('#order_article_verarbeitung_id').focus();
                if (isDisplayRebateTable) {
                    $('#calculator #rebatetable h3').addClass('active');
                }
            }
        });
    })

    $('#order_article_verarbeitung_durchmesser_cm').live('change', function() {
        isDisplayRebateTable = false;
        if ($('#calculator #rebatetable h3').hasClass('active')) {
            isDisplayRebateTable = true;
        }
        var $thisform = $('#calcform');
        var url = $thisform.attr('action');
        url = "/calc/" + $('#order_article_material_id').val() + "/1";
        var values = $('#calcform').serialize();
        $.ajax({
            type: "POST",
            url: url,
            data: values,
            success: function(j) {
                $('#calcformdiv').html(j),$('#order_article_verarbeitung_durchmesser_cm').focus();
                if (isDisplayRebateTable) {
                    $('#calculator #rebatetable h3').addClass('active');
                }
            }
        });
    })

});

function hideDurchmesser() {
    var vid = $('#order_article_verarbeitung_id').val();
    if (vid >= 8 && vid <= 12) {
//        $('#pdurchmesser').fadeIn('slow');
        $('#pdurchmesser').show();
    }
    else {
//        $('#pdurchmesser').hide();
        $('#pdurchmesser').addClass('invisible');
    }
}

/**
 * Checks that width, height and units are within an appropriate range and adjusts them as necessary
 */
function checkMinVals() {
    var minwidth = 250;
    var minheight = 250;
    var minunits = 1;

    var widthval = parseInt($('#order_article_breite_mm').val());
    var heightval = parseInt($('#order_article_hoehe_mm').val());
    var unitsval = parseInt($('#order_article_anzahl').val());

    if (!widthval || widthval < minwidth) {
        $('#order_article_breite_mm').val(minwidth);
    }
    if (!heightval || heightval < minheight) {
        $('#order_article_hoehe_mm').val(minheight);
    }
    if (!unitsval || unitsval < minunits) {
        $('#order_article_anzahl').val(minunits);
    }
}

/*

 $('#calcform').load(
 $(this).parents('form').attr('action'),
 { query: '' },
 function() {
 $('#loader').hide();
 }
 );

 });
 */

/*
 $('#search_keywords').keyup(function(key) {
 if (this.value.length >= 3 || this.value == '') {
 $('#loader').show();
 $('#jobs').load(
 $(this).parents('form').attr('action'),
 { query: this.value + '*' },
 function() {
 $('#loader').hide();
 }
 );
 }
 });
 */



