/******************   global variable declaration,  begin *****************/

/******************   global variable declaration,  end *******************/

/******************   class declaration,  begin *****************/

/******************   class declaration,  end *******************/

/***** init section, begin *****/
$(document).ready(
    function() {
        step3Init();
    });


function step3Init()
{
    step3BindEvents();

}

function step3BindEvents()
{
    ;
}

function verifyStep3()
{
    var checkResult = true;
    var tmpErrorCode = 0;

    if ($("#step3_km").val()>500000) {
        checkResult = false;
        tmpErrorCode = 1;
    }

    // check postcode
    if (checkResult) {
        if (!isInteger($("#step3_km").val())) {
            checkResult = false;
            tmpErrorCode = 2;
        }
    }

    if($("#step3_km").val().length==0 || $("#step3_km").val()==0) {
        checkResult = false;
        tmpErrorCode = 3;
    }

    //debugLog("trigger1:" + $("#step3_km").val() + " : " + $("#step3_km").val().length);

    if (!checkResult) {
        switch(tmpErrorCode) {
            case 0:
                break;
            case 1:
                showTips("Les kilom&eacute;trages sup&eacute;rieurs &agrave; 500 000 ne sont pas autoris&eacute;s.");
                break;
            case 2:
                showTips("Veuillez saisir un kilom&eacute;trage valide.");
                break;
            case 3:
                showTips("Merci de saisir le kilom&eacute;trage du v&eacute;hicule.");
                break;
            default:
                break;
        }
    }

    return checkResult;
}

function goStep3()
{
    var params = {
            "data_format" : 'json',
            "year" : $("#step2_year").val(),
            "month" : $("#step2_month").val(),
            "brand_name" : $("#step2_brand_name").val(),
            "model_name" : $("#step2_model_name").val(),
            "energy_name" : $("#step2_energy_name").val(),
            "bodywork_name" : $("#step2_bodywork_name").val(),
            "door_name" : $("#step2_door_name").val(),
            "motorization_name" : $("#step2_motorization_name").val(),
            "finish_name" : $("#step2_finish_name").val(),
            "gearbox_name" : $("#step2_gearbox_name").val(),
            "step" : 3
        };
    var submitUrl;

    showBlockUILoading("#eurotax_valuation_form" );

    submitUrl = BASE_URL + "eurotax/goStep3";

    $.ajax({
        url: submitUrl,
        global: true,
        type: "GET",
        data: params,
        dataType: "json",
        success: function(jsonData){
            goStep3CallBack(jsonData);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            //debugLog("goStep3 Error: " + $(this).dump()); // the options for this ajax request
        }
    });
}

function goStep3CallBack(jsonData)
{
    if(jsonData.info.status == operationStatus.Error) {
        showTips(jsonData.info.error_info);
    } else {
        if(jsonData['items']['vehicles'] == false) {
            formStatus.currentStep--;
            showTips("Merci de compl&eacute;ter tous les champs.");
        } else if (jsonData['items']['options'].length>0) {
            //debugLog("jsonData['items']['options'].length>0");
			
            for (i=0;i<jsonData['items']['options'].length;i++) {
               var tmpId = "step3_option_" + i;
               $('<div><label for="' + tmpId+ '"><input type="checkbox" name="step3_options[]" id="' + tmpId + '" value="' + jsonData['items']['options'][i].id + '" />' + jsonData['items']['options'][i].label + '</label></div>').appendTo("#step3_options");
			}
        }
        showStepForm(formStatus.currentStep);

    }
    hideBlockUI("#eurotax_valuation_form");
}

