$(document).ready(function() {
	
	new RegistrationForm({
		element: $('form#SubscriberForm'),
		hasDatepickers: true
	});
	
});


function RegistrationForm(options) {
	this.form = options.element; // jQuery form object 
	this.init(options.hasDatepickers);
}

RegistrationForm.prototype = {
	
	init: function(hasDates) {
	
		if ( hasDates ) {
			this.bindDatepickers();
		}
	
		this.form.validate({
			rules: {
				pf_SubscriberName: {
					required: true
				},
				pf_Email: {
					required: true,
					email: true
				}, 
				pf_DemographicField1: {              
					required: true
				},
				pf_DemographicField5: {              
					//required: true,
					customDateFormat: true
				},
				pf_DemographicField6: {              
					customDateFormat: true
				},
				pf_DemographicField7: {              
					customDateFormat: true
				},
				pf_DemographicField8: {              
					customDateFormat: true
				}
			},
			messages: {
				pf_SubscriberName: {
					required: "Fornavn må fylles ut"
				},
				pf_DemographicField1: {              
					required: "Etternavn må fylles ut"
				},
				pf_Email: {
					required: "Epost må fylles ut",
					email: "Epostadressen er ugyldig"
				},
				pf_DemographicField5: {
					//required: "Termindato må fylles ut",
					customDateFormat: "Datoformat: åååå-mm-dd"
				},
				pf_DemographicField6: {
					customDateFormat: "Datoformat: åååå-mm-dd"
				},
				pf_DemographicField7: {
					customDateFormat: "Datoformat: åååå-mm-dd"
				},
				pf_DemographicField8: {
					customDateFormat: "Datoformat: åååå-mm-dd"
				}
			},                                    
			errorElement: 'em',
			errorPlacement: function(error, element) {
				error.appendTo(element.parent());
				error.css({ left: element.outerWidth(true) + 10 })
			},
			submitHandler: function(form) {
				form.submit();
			},
			invalidHandler: function(event, validator) {
			
			}
		});
	
	}, 
	bindDatepickers: function() {
		this.form.find('.date input').datepicker({
			changeMonth: true,
			changeYear: true,
			dateFormat: 'yy-mm-dd',
			firstDay: 1,
			beforeShowDay: function(date) {            
				var className = '';
				// skip calculation for all days before the 27th
				if( date.getDate() > 27 && isLastDayOfMonth(date)) {
					className = 'lastDay';
				}
				return new Array(true, className, ''); 
			}
		});                                                     
	}
};


if ( $.validator ) {
	$.validator.addMethod('customDateFormat', function(value, element) {
		/* var re = new RegExp(/(?:0[1-9]|[12][0-9]|3[01])\.(?:0[1-9]|1[0-2])\.(?:\d{2})/); */
		var re = new RegExp(/(?:(19|20)\d{2})\-(?:0[1-9]|1[0-2])\-(?:0[1-9]|[12][0-9]|3[01])/);
		return re.test(value) || value == "";
	}, "Invalid date format.");	
}

if ( $.datepicker ) {
	/* Norwegian initialisation for the jQuery UI date picker plugin. */
	/* Written by Naimdjon Takhirov (naimdjon@gmail.com). */
	$.datepicker.regional['no'] = {
		closeText: 'Lukk',
	    prevText: '&laquo;Forrige',
		nextText: 'Neste&raquo;',
		currentText: 'I dag',
	    monthNames: ['Januar','Februar','Mars','April','Mai','Juni',
	    'Juli','August','September','Oktober','November','Desember'],
	    monthNamesShort: ['Jan','Feb','Mar','Apr','Mai','Jun',
	    'Jul','Aug','Sep','Okt','Nov','Des'],
		dayNamesShort: ['Søn','Man','Tir','Ons','Tor','Fre','Lør'],
		dayNames: ['Søndag','Mandag','Tirsdag','Onsdag','Torsdag','Fredag','Lørdag'],
		dayNamesMin: ['S','M','T','O','T','F','L'],
	    dateFormat: 'yy-mm-dd', firstDay: 0,
		isRTL: false};
	$.datepicker.setDefaults($.datepicker.regional['no']);	
}

