// JavaScript Document

function now() {
	var currentTime = new Date()
	var year = currentTime.getFullYear();
	var month = currentTime.getMonth() + 1;
	var day = currentTime.getDate();
	var hours = currentTime.getHours();
	var minutes = currentTime.getMinutes();
	var seconds = currentTime.getSeconds();
	var time = year + '/' + month + '/' + day + '-' + hours + ':' + minutes + ':' + seconds;
	return time;
}

function datenow() {
	var currentTime = new Date()
	var year = currentTime.getFullYear();
	var month = currentTime.getMonth() + 1;
	var day = currentTime.getDate();
	var time = month + '/' + day + '/' + year;
	return time;
}

$(function() {
	$('.phone').mask('(999) 999-9999? x99999');
	$('.num').mask('9?9', {placeholder:''});

// http://code.google.com/p/dyndatetime/wiki/Home
	$('.dateField').datepicker({ dateFormat: 'mm/dd/y - DD, MM d' });
//.dynDateTime({
//		ifFormat: "%m/%d/%y - %a, %b %e",
//		electric: true,
//		singleClick: false
//	});

	$('#prefdate1, #prefdate2').change(function(){
		var val = $(this).val();
		var found = false;
		if (val.search('Monday') > 0) found = true;
		if (val.search('Wednesday') > 0) found = true;
		if (val.search('Friday') > 0) found = true;
		if (!found) {
			$(this).val('');
			alert('Please choose Monday, Wednesday, or Friday.');
		}
	});

	$('#tourrequest').validate({
		errorPlacement: function(error, element) {
			error.appendTo( element.parents('div.element') );
		},
		rules: {
			group: 'required',
			contactname: 'required',
			phone: 'required',
			fax: 'required',
			email: {
				required: true,
				email: true
			},
			numstudents: {
				required: true,
				max: 20
			},
			numadvisors: 'required',
			grades: 'required',
			disability: 'required',
			programs: {
				maxlength: 3
			},
			prefdate1: 'required',
			preftime1: 'required',
			agree: 'required',
			signature: 'required',
			datesigned: 'required'
		},
		messages: {
			group: 'Required',
			contactname: 'Required',
			phone: 'Required',
			fax: 'Required',
			email: {
				required: 'Required',
				email: 'Invalid Email Format'
			},
			numstudents: {
				required: 'Required',
				max: 'No more than 20!'
			},
			numadvisors: 'Required',
			grades: 'Required',
			disability: 'Required',
			prefdate1: 'Required',
			preftime1: 'Required',
			programs: 'Please select no more than 3 programs.',
			agree: 'Please read and agree to the "San Juan College Tour Agreement"',
			signature: 'Please type your name to sign.',
			datesigned: 'Required'
		}
	});

	$('#signature, #datesigned')
		.attr('disabled', 'disabled')
		.css('background-color', 'gray');
	
	$('#agree').click(function() {
		if ($('#agree').is(':checked')) {
			$('#signature')
				.removeAttr('disabled')
				.css('background-color', '');
		} else {
			$('#signature, #datesigned')
				.attr('disabled', 'disabled')
				.css('background-color', 'gray')
				.val('');
		}
	});
	
	$('#signature').keyup(function() {
		if ($(this).val() != '') {
			$('#datesigned')
				.removeAttr('disabled')
				.css('background-color', '');
		} else {
			$('#datesigned')
				.attr('disabled', 'disabled')
				.css('background-color', 'gray')
				.val('');
		}
	});
	
	$('#datesigned').focus(function() {
		$(this).val(datenow())
	});
	
	$('#reset').click(function() {
		window.location = url;
	});
});

