/******************   global variable declaration,  begin *****************/

/******************   global variable declaration,  end *******************/

/******************   class declaration,  begin *****************/

/******************   class declaration,  end *******************/

/***** init section, begin *****/
$(document).ready(
    function() {
        step4Init();
    });


function step4Init()
{
    step4BindEvents();

}

function step4BindEvents()
{
    $("#step4_tel").bind('keydown', function(evt)
    {
        if(evt.keyCode == 32) {
            return false;
        }
    });
    
}

function verifyStep4()
{
    //debugLog("verifyStep4");
    var checkResult = true;
    var tmpErrorCode = 0;

    // check mandatory
    $("input[name*='step4']").each(function(){
        if ($(this).val()=="NULL" || $(this).val()=="") {
            checkResult = false;
            tmpErrorCode = 1;
        }
    })
    // check civility
    if (checkResult) {
        if (!$("input[name='step4_civility']:checked").val()) {
            checkResult = false;
            tmpErrorCode = 1;
        }
    }
    
    // check email
    if (checkResult) {
        var emailPtn = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
        if (!emailPtn.test($("#step4_email").val())) {
            checkResult = false;
            tmpErrorCode = 2;
        }
    }

    // check postcode
    if (checkResult) {
        if ($("#step4_postcode").val().length>5) {
            checkResult = false;
            tmpErrorCode = 3;
        }
    }

    // check postcode
    if (checkResult) {
        if (!isInteger($("#step4_postcode").val())) {
            checkResult = false;
            tmpErrorCode = 4;
        }
    }

    // check postcode
    if (checkResult) {
        var blackPostcodes = ["00000","11111","22222","33333","44444","55555","66666","77777","88888","99999"];

        if ($.inArray($("#step4_postcode").val(), blackPostcodes) != -1) {
            checkResult = false;
            tmpErrorCode = 4;
        }
    }

    // check postcode
    if(checkResult) {
        if ($("#step4_postcode").val() >= 96000 ) {
            checkResult = false;
            tmpErrorCode = 4;
        }
    }
    
    // check telphone, length
    $("#step4_tel").attr("value", $("#step4_tel").val().replace(/ /g, ""));
    if (checkResult) {
        if ($("#step4_tel").val().length>10) {
            checkResult = false;
            tmpErrorCode = 5;
        }
    }

    // check telphone, blacklist
    if (checkResult) {
        var blackTelNos = ["0123456789","0101010101","0202020202","0303030303","0404040404","0505050505","0606060606",
        "0707070707","0808080808","0909090909","1234567890","1111111111","1212121212","1111111111","0000000000"];
    
        if ($.inArray($("#step4_tel").val(), blackTelNos) != -1) {
            checkResult = false;
            tmpErrorCode = 6;
        }
    }

    // check telphone, leading char
    if (checkResult) {
        var telPtn = new RegExp(/^((0|\+)?\d{9})$/i);
        if (!telPtn.test($("#step4_tel").val())) {
            checkResult = false;
            tmpErrorCode = 7;
        }
    }
    
    if (!checkResult) {
        switch (tmpErrorCode) {
            case 0:
                break;
            case 1:
                showTips("Tous les champs sont obligatoires.");
                break;
            case 2:
                showTips("Veuillez saisir une adresse email valide.");
                break;
            case 3:
                showTips("Le code postal de doit pas faire plus de 5 caractères.");
                break;
            case 4:
                showTips("Veuillez saisir un code postal valide.");
                break;
            case 5:
                showTips("Veuillez saisir une numéro de téléphone valide.");
                break;
            case 6:
                showTips("Veuillez saisir une numéro de téléphone valide.");
                break;
            case 7:
                showTips("Veuillez saisir une numéro de téléphone valide.");
                break;
            default:
                break;
        }
    }

//    return false;
    return checkResult;
}

function goStep4()
{
    var params = {
        "data_format" : 'json',
        "km" : $("#step3_km").val(),
        "state" : $("input:radio[name*='step3_state']:checked").val(),
        "step" : 4
    };

    if ($("div#step3_options input:checkbox:checked").length > 0) {
        $(params).attr("options[]", $("div#step3_options input:checkbox:checked").map(function(){return $(this).val()}).get().join(",").split(","));
    }
    
    var submitUrl;

    showBlockUILoading("#eurotax_valuation_form" );

    submitUrl = BASE_URL + "eurotax/goStep4";

    $.ajax({
        url: submitUrl,
        global: true,
        type: "GET",
        data: params,
        dataType: "json",
        success: function(jsonData){
            goStep4CallBack(jsonData);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            //debugLog("goStep4 Error: " + $(this).dump()); // the options for this ajax request
        }
    });
}

function goStep4CallBack(jsonData)
{
    if (jsonData.info.status == operationStatus.Error) {
        showTips(jsonData.info.error_info);
    } else {
        showStepForm(formStatus.currentStep);
    }
    hideBlockUI("#eurotax_valuation_form");

}

