Type.registerNamespace("Independer.Autoverzekering.InvoerQuickQuote");

Independer.Autoverzekering.InvoerQuickQuote = function (state){
    this._state = state;
    this._state.controls.autoSelectorGrid.onchange = Function.createDelegate(this, this.clearKenteken);
    $addHandler(window ,"load", Function.createDelegate(this, this.addHandlers));
}

// handles the kenteken validation. Calls the webService to get the type back.
Independer.Autoverzekering.InvoerQuickQuote.prototype = {
    validateKenteken : function(){        
        var kenteken = $get(this._state.controls.inputKenteken + "_input").value;
        var isvalidKenteken = true;
        var kentekenValidators = this.getValidators(this._state.controls.inputKenteken);
        for (var i=0; i< kentekenValidators.length; i++){
            // validate only the regex validator, check for valid format
            if(kentekenValidators[i].validationexpression){
                ValidatorValidate(kentekenValidators[i], null, null);
                isvalidKenteken = isvalidKenteken && kentekenValidators[i].isvalid;
            }
        }
        if(kenteken != "" && isvalidKenteken){
            Independer.Finance.Auto.Webservices.QuickQuoteService.ValidateKenteken(kenteken, Function.createDelegate(this, this.validateKentekenOnSuccess), Function.createDelegate(this, this.validateKentekenOnFailure)); // todo: set delegates
        }
        else{
            Independer.WebControls.Ajax.UI.setVisibility($get(this._state.controls.containerKentekenNietGevonden), false);
            Independer.WebControls.Ajax.UI.setVisibility($get(this._state.controls.invoerMerkEnModel), false);
        }
    },


// Handles a successful response from the service for a kentenken validation.
    validateKentekenOnSuccess : function (result){
        var checkbox;
        Independer.WebControls.Ajax.UI.setVisibility($get(this._state.controls.containerKentekenNietGevonden), !result.IsValid);
        Independer.WebControls.Ajax.UI.setVisibility($get(this._state.controls.invoerMerkEnModel), result.IsValid);
        if (result.IsValid){
            $get("merkModel").innerHTML = result.AutoType;
            checkbox = $get(this._state.controls.radioKnown);
        }
        else {
            checkbox = $get(this._state.controls.radioUnknown);
            checkbox.checked = true;
        }
        if(checkbox.ControlDependencyExtenderBehavior)
            checkbox.ControlDependencyExtenderBehavior.update(); // trigger het event op de andere radio buttons
    },


// Handles a failing response from the service for a kentenken validation.
    validateKentekenOnFailure : function (result){
        // ignore for now
    },


// returns a validators array for the selected control.
    getValidators : function (id){
        var vals = new Array();
        for(var i=  0; i< Page_Validators.length; i++){
            if(Page_Validators[i].controltovalidate == id){
                Array.add(vals, Page_Validators[i]);
            }
        }
        return vals;
    },

// gives focus to the Kenteken text box when radioKnown is checked
    checkKentekenBox : function (){
        var checkbox = $get(this._state.controls.radioKnown);
        if (!checkbox.checked){
            checkbox.checked = true; //steal focus
            $get(this._state.controls.inputKenteken + "_input").focus(); // give focus back
        }
    },


// Clears the kenteken text box when radioUnknown is checked
    clearKenteken : function (){
        var checkbox = $get(this._state.controls.radioKnown);
        if (!checkbox.checked){
            $get(this._state.controls.inputKenteken + "_input").value = "";
            Independer.WebControls.Ajax.UI.setVisibility($get(this._state.controls.containerKentekenNietGevonden), false);
            Independer.WebControls.Ajax.UI.setVisibility($get(this._state.controls.invoerMerkEnModel), false);
        }
    },
    
// Add event handlers for SVJ validation
    addHandlers : function () {
        // Als we een waarschuwing hebben dan verbergen we deze.
        var geenJavaWaarschuwing = $get(this._state.controls.geenJavaWaarschuwing);
        if ( geenJavaWaarschuwing != null )
            Independer.WebControls.Ajax.UI.setVisibility(geenJavaWaarschuwing,false);
        
        $addHandler($get(this._state.controls.txtGeboorteDatum), "change", Function.createDelegate(this, this.validateSVJ));
        // Function in svj control. Is not available when control is not loaded.
        if(typeof validateJarenVerzekerd == 'function') {            
            $addHandler($get(this._state.controls.txtGeboorteDatum), "change", validateJarenVerzekerd);
        }
        // Als we al een kenteken hebben tijdens het laden, kunnen we deze ook checken :)
        if ( $get(this._state.controls.inputKenteken + "_input").value != "" )
            this.validateKenteken();
            
    },

// Checks that the combination of birthdate and SVJ is valid.
    validateLeeftijdRijbewijs : function (obj, val){
        var today = new Date();
        var dateToValidate = new Date();
        
        var geboorteDatumControlValue = $get(this._state.controls.txtGeboorteDatum).value;
        var schadeVrijeJaren = val.Value;
        
        var geboorteDatumVal = Date.parseInvariant(geboorteDatumControlValue, 'dd-MM-yyyy');
        var yearsToAdd = Number(18) + Number(schadeVrijeJaren);
        
        if(geboorteDatumVal != null){
            dateToValidate = geboorteDatumVal;
            dateToValidate.setFullYear(geboorteDatumVal.getFullYear() + yearsToAdd);
        }            
        val.IsValid = dateToValidate <= today;            
    },       

// manualy validates the SVJ
    validateSVJ : function (e){
        var obj = $get(this._state.controls.valLeeftijdRijbewijs);
        var obj_sr = $get(this._state.controls.valLeeftijdRijbewijs_SR);
        ValidatorValidate(obj, "", e);
        ValidatorValidate(obj_sr, "", e);
    },

// prevents user to type more than maxlength chars in a textbox.
    checkLength : function (objTxtArea, maxlength, e){
        if(objTxtArea.value.length >= maxlength && e.keyCode != 8 && e.keyCode != 46){
            objTxtArea.value = objTxtArea.value.substr(0, maxlength);
            return false;
        }
    }
}

Independer.Autoverzekering.InvoerQuickQuote.registerClass("Independer.Autoverzekering.InvoerQuickQuote", null, Sys.IDisposable);

if (typeof(Sys) !== "undefined")
    Sys.Application.notifyScriptLoaded();
        

