$(document).ready(function() {

	/* Date formatting */
	Date.firstDayOfWeek = 7;
	Date.format = 'yyyy-mm-dd';
	
	/* Date widgets */
	$('#id_date_of_birth').datePicker({startDate:'1960-01-01'});
	$('#id_anticipated_grad_date').datePicker();	
	$('#id_p1_start_date').datePicker({startDate:'1960-01-01'});
	$('#id_p1_end_date').datePicker({startDate:'1960-01-01'});
	$('#id_p2_start_date').datePicker({startDate:'1960-01-01'});
	$('#id_p2_end_date').datePicker({startDate:'1960-01-01'});
	$('#id_p3_start_date').datePicker({startDate:'1960-01-01'});
	$('#id_p3_end_date').datePicker({startDate:'1960-01-01'});	

	/* Adjust width of education start/end dates */
	$('#id_p1_start_date,#id_p1_end_date,#id_p2_start_date,#id_p2_end_date,#id_p3_start_date,#id_p3_end_date').parent().css('width', '500px');


	/* Add a `remove()` function to Arrays */
	Array.prototype.remove = function(from, to) {
	  var rest = this.slice((to || from) + 1 || this.length);
	  this.length = from < 0 ? this.length + from : from;
	  return this.push.apply(this, rest);
	};


	/* Make form sections collapsable on the review screen */
	$('.review.section .title').click(function() {
		$(this).parent().find('table').toggle();
		$(this).parent().toggleClass('collapsed');
	});
	$('.review.section .title').click();
	
	
	/* Add form validation. */
	$('.field.required input').addClass('required');
	$('.field.required select').addClass('required');
	$('#id_attachment').addClass('required');
	$.metadata.setType("attr", "validate");
	var validationOptions = {
		errorElement: "em",
		errorPlacement: function(error, el) {
			if ( el[0].name == "same_address") {
				error.insertAfter($('#school_header'));
			}
			else if( el[0].name == "gender") {
				$(el).parent().parent().parent().before(error);
			} 
			else {
				$(el).parent().parent().after(error);
			}
		}	
	}
	$("form").validate(validationOptions);
	// TODO: Validate digit inputs on Finance form.
	// $('#application_form.finances .required input').validate({rules:{field: {required:true, digits:true }}});
	
	/* Allow user to copy permanent address into school address */
	/* This hash maps the fields */
	var addressMap = {
	    'id_school_email': 'id_email',
		'id_school_address': 'id_address',
		'id_school_address_2': 'id_address_2',
		'id_school_city': 'id_city',
		'id_school_province': 'id_province',
		'id_school_postal_code': 'id_postal_code',
		'id_school_telephone': 'id_telephone',
		'id_school_country': 'id_country'
	};
	/* Function to perform the copy */
	function copyPermAddress() {
		$.each(addressMap, function(key, value) {
			$("#" + key).attr('value', $("#" + value).attr('value'));
		});
	}
	$('#copy_address').click(copyPermAddress);
	
	/* Registration form: password strength meter. */
	$('#id_password1').keyup(function(){
		var result = passwordStrength($('#id_password1').val(), 6);
		var cls="short";
		switch(result) {
			case WEAK_PW:
				cls = "bad";
				break;
			case GOOD_PW:
				cls = 'good';
				break;
			case STRONG_PW:
				cls = 'strong';
				break;
		}
		var html = $('<p>' + result + '</p>');
		html.addClass(cls);
		$('#pw_result').children().remove();
		$('#pw_result').append(html);
	});
	$('#id_password2').keyup(function() {
		if( $.trim($('#id_password2').attr('value')).length < 1 ) { return; }
		$('#pwMatch').remove();
		if( $('#id_password1').attr('value') != $('#id_password2').attr('value') ) {
			$('#pw_result').append("<p class=\"nomatch\" id=\"pwMatch\"><strong>Passwords don't match.</strong></p>");
		} else {
			$('#pw_result').append("<p class=\"match\" id=\"pwMatch\"><strong>Passwords match!</strong></p>");
		}
	});
});