/***** init section, begin *****/
$(document).ready(
    function() {
        step6Init();
    }
    );


function step6Init()
{
    step6BindEvents();
}

function step6BindEvents()
{

}

function verifyStep6()
{
	var checkResult = true;
    var tmpErrorCode = 0;
    if ($('input:radio[name=opinion_estimate]:checked').val() == null && $('#verif_step').val()== 1) {
        checkResult = false;
        tmpErrorCode = 1;
    }

    if($('#verif_step').val()== 0) {
    	$('input:radio[name=opinion_estimate]').filter('[value=nettement-inferieure]').attr('checked', true);
    }

    if (!checkResult) {
        switch(tmpErrorCode) {
            case 0:
                break;
            case 1:
                showTips("Votre avis sur cette estimation est obligatoire");
                break;
            default:
                break;
        }
    }
    return checkResult;
}

function goStep6()
{
    var params = {
        "type" : $("input:radio[name*=step5]:checked").val(),
        "brand_name" : $("#step5_brand_name").val(),
        "model_name" : $("#step5_model_name").val(),
        "beta_model_id" : $("#step5_beta_model_id").val(),
        "data_format" : 'json',
        "step" : 6
    };
    var submitUrl;

    showBlockUILoading("#eurotax_valuation_form" );

    submitUrl = BASE_URL + "eurotax/goStep6";

    $.ajax({
        url: submitUrl,
        global: true,
        type: "GET",
        data: params,
        dataType: "json",
        success: function(jsonData){
            goStep6CallBack(jsonData);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            //debugLog("goStep6 Error: " + $(this).dump()); // the options for this ajax request
        }
    });


}

function goStep6CallBack(jsonData)
{
    // on cache le boton suivant
    $("#step_form_btn").hide();

    //alert(jsonData['items'].aramisValuation.value.min);
    if (jsonData.info.status == operationStatus.Error) {
        //$("#step6_content").html('<br><p>'+jsonData.info.error_info+'</p>');
        $("#verif_step").val(0);
        $("#step6_default_content").hide();
        $("#step_form_btn").hide();
        $("#cotisation_300").show();
         
    } else {        
        
        if(jsonData['items'].aramisValuation.value.min < 300) {

            $("#verif_step").val(0);
            $("#step6_default_content").hide();
            $("#cotisation_300").show();
        }
        else 
        {
            $("#verif_step").val(1);
            $("#step6_min_price").html(jsonData['items'].aramisValuation.value.min);
            $("#step6_max_price").html(jsonData['items'].aramisValuation.value.max);
            $("#vehicle_estimation_rdv").val('De ' + jsonData['items'].aramisValuation.value.min + ' ? ' + jsonData['items'].aramisValuation.value.max);
            $("#step6_brand").html(jsonData['items'].estimateParams.brand);
            $("#step6_model").html(jsonData['items'].estimateParams.model);
            trackConfirmation();
        }
        
    }

    $("#tag_keyade").html(jsonData.tag.keyade);
    $("#tag_matiro").html(jsonData.tag.matiro);

    //tag for loading page
    showStepForm(formStatus.currentStep);

    // ecran tempo market    
    $("#eurotax_valuation_step_6_before").stop().fadeIn('slow').delay(15000).fadeOut('slow');
    $("#eurotax_valuation_step_6").stop().fadeOut('slow').delay(15000).fadeIn('slow', function() { 
            if (typeof(omniture_s_is_enable) != 'undefined' && omniture_s_is_enable == 1) 
            {
                //tag for estimate page
                ajaxCommondRepriseVar('6', jsonData['tag']['omniture']);
            }
            
            // si le passage a etape suivante valide on affiche le bouton suivant
            $("#step_form_btn").show();
    });    

    hideBlockUI("#eurotax_valuation_form");
}
