// Requires jQuery library, form to be id="contactForm", input field to id="access_code"
// If a form exists with an id="contactForm, handle captcha input conversion
$(document).ready(function() {
	if ( $("#contactForm").length ) {
		$('#access_code').keypress(
			//block on non-numbers from being entered
			function(e)
			{
				var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
				
				// UPPERCASE LETTERS or lowercase
				if ((key >= 65 && key <= 90) || (key >= 97 && key <= 122)) {
					return true;
				}
				
				// check for other keys that have special purposes
				if (
					key == 8 /* backspace */ ||
					key == 9 /* tab */ ||
					key == 13 /* enter */ ||
					key == 35 /* end */ ||
					key == 36 /* home */ ||
					key == 37 /* left */ ||
					key == 39 /* right */ ||
					key == 46 /* del , also '.' */
				)
				{
					return true;
				}
				// for detecting special keys (listed above)
				// IE does not support 'charCode' and ignores them in keypress anyway
				if(typeof e.charCode != "undefined")
				{
					// special keys have 'keyCode' and 'which' the same (e.g. backspace)
					if(e.keyCode == e.which && e.which != 0)
					{
						return true;
					}
					// or keyCode != 0 and 'charCode'/'which' = 0
					else if(e.keyCode != 0 && e.charCode == 0 && e.which == 0)
					{
						return true;
					}
				}
	
				return false;
			}
		).keyup(
			// convert lowercase to UPPERCASE
			function()
			{
				$(this).val( $(this).val().toUpperCase() );
			}
		); // end of $('#access_code')
		
			// validate signup form on keyup and submit
		$("#contactForm").validate({
			//event: "keyup", // when validator is fixed uncomment event
			rules: {
				first_name: "required",
				last_name: "required",
				email: {
					required: true,
					email: true
				},
				message: {
					required:  true
				},
				access_code: {
					required: true,
					minLength: 5,
					maxLength: 5
				}
			},
			messages: {
				first_name: "Please enter your first name",
				last_name: "Please enter your last name",
				email: "Please enter a valid email address",
				message: "Please enter a message",
				access_code: {
					required: "Please enter the letters from the box above",
					minLength: "Please enter exactly 5 letters",
					maxLength: "Please enter exactly 5 letters"
  				}
			}
		});

	} // end of if $("#contactForm").length
	
		
})
