

















(function($) {
$.ti.account = $.prototype = {

    html: $([ '<div id="swapableContent">',
                    '<h2>Wachtwoord wijzigen</h2>',
                    '<p>Voer je gegevens in om je wachtwoord te wijzigen voor de Reisplanner van Ierland.nl</p>',
                    '<form method="" action="" id="changePassword">',
                        '<fieldset>',
							'<div class="passwordOverlay">',
								'<label class="password" for="oldPassword">Oud wachtwoord</label>',
								'<input type="password" id="oldPassword" value="" />',
							'</div>',
							'<div class="passwordOverlay">',
								'<label class="password" for="newPassword">Nieuw wachtwoord</label>',
								'<input type="password" id="newPassword" value="" />',
							'</div>',
							'<div class="passwordOverlay">',
								'<label class="password" for="confirmNewPassword">Bevestig je nieuwe wachtwoord</label>',
								'<input type="password" id="confirmNewPassword" value="" />',
							'</div>',
                        '</fieldset>',
                        '<input type="image" class="button" height="22" src="/shared/resource/images/itinerary_planner/nl/buttons/update.png" id="submitNewPassword" />',
                    '</form>',
                '</div>'
            ].join('')),

    init: function() {
        $.ti.popup.init({
			content: $.ti.account.html,
			callback: $.ti.account.events,
			width: "360px"
		});
    },

    events: function() {
        $('#submitNewPassword').click(function(event){
			event.preventDefault();
            $.ti.validate.check('#changePassword', {
                rules: {
                    oldPassword: {
                        "required"  : 'Dit is een verplicht veld'
                    },
                    newPassword: {
                        "required"  : 'Dit is een verplicht veld',
                        "minLength" : ["6", 'Het wachtwoord moet uit minimaal 6 tekens bestaan'],
                        "matchVal"  : ['#confirmNewPassword', 'Sorry, je wachtwoorden komen niet overeen. Om toegang te krijgen tot je Reisplanner, klik op &#39;wachtwoord vergeten&#39;.']
                    },
                    confirmNewPassword: {
                        "required" : 'Dit is een verplicht veld',
                        "minLength" : ["6", 'Het wachtwoord moet uit minimaal 6 tekens bestaan']
                    }
                },
                success: $.ti.account.submitNewRequest
             });
        });
    },

    submitNewRequest: function(){
        var resetRequest = {
            login: {
                id: $.ti.tripData.trip.login.id,
                username: $.ti.tripData.trip.login.username,
                oldPassword: $('#oldPassword').val(),
                password: $('#newPassword').val()
            }
        };
        $.ti.ajax.postData('/nl/trip/', resetRequest, $.ti.account.requestConfirmation);
    },

	requestConfirmation: function(response) {
		if(response !== 0) {
			$.ti.popup.close();
		} else {
			$.ti.validate.insertMessage('oldPassword', 'Onjuist oud wachtwoord');
		}
	}
};
})(jQuery);
