jQuery.validator.setDefaults({success: "valid"});

jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
    phone_number = phone_number.replace(/\s+/g, ""); 
	return this.optional(element) || phone_number.length > 9 &&
		phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
}, "Please specify a valid phone number");

$(document).ready(function(){
	
	if(document.addEventListener) 
	document.addEventListener( 'DOMContentLoaded', cmxform, false);

	function cmxform(){
	  // Hide forms
	  $('form.cmxform').hide().end();

	  // Processing
	  $('form.cmxform').find('li/label').not('.nocmx').each( function( i ){
	    var labelContent = this.innerHTML;
	    var labelWidth = document.defaultView.getComputedStyle(this, '').getPropertyValue('width');
	    var labelSpan = document.createElement('span');
	        labelSpan.style.display = 'block';
	        labelSpan.style.width = labelWidth;
	        labelSpan.innerHTML = labelContent;
	    this.style.display = '-moz-inline-box';
	    this.innerHTML = null;
	    this.appendChild(labelSpan);
	  } ).end();

	  // Show forms
	  $('form.cmxform').show().end();
	}
	
	$(".error-box")
		.fadeIn()
		.animate({
			backgroundColor: "#FFFFCC",
			borderTopColor: "yellow",
			borderRightColor: "yellow",
			borderBottomColor: "yellow",
			borderLeftColor: "yellow"
		}, 3000
	);
	$(".error-box").click(function(){
		$(this)
			.stop()
			.animate({
				backgroundColor: "pink",
				borderTopColor: "#F00",
				borderRightColor: "#F00",
				borderBottomColor: "#F00",
				borderLeftColor: "#F00"
			}, 500
		);
	});
	
	$("#signupForm").validate({
		rules: {
			your_name: "required",
			your_affiliation: "required",
			your_email_address: {
				required: true,
				email: true
			},
			your_phone_number: {
				required: true,
				phoneUS: true
			},
			your_fax_number: {
				phoneUS: true
			},
			nominees_name: "required",
			nominees_affiliation: "required",
			nominees_email_address: {
				required: true,
				email: true
			},
			nominees_phone_number: {
				required: true,
				phoneUS: true
			},
			nominees_fax_number: {
				phoneUS: true
			},
			nominees_address: "required",
			nominees_city: "required",
			nominees_state: {
				required: true,
				rangelength: [2,2]
			},
			nominees_zip: {
				required:true,
				digits:true,
				rangelength: [5,5]
			},
			membership: "required",
			nominees_resume: {
				accept: "doc|docx|pdf|rtf|txt"
			}
		},
		messages: {
			your_name: "Please enter your name",
			your_affiliation: "Please enter your affiliation",
			your_email_address: "Please enter a valid email address",
			your_phone_number: "Please enter a valid phone number",
			your_fax_number: "This is not a valid fax number",
			nominees_name: "Please enter your nominee's name",
			nominees_affiliation: "Please enter your nominee's affiliation",
			nominees_email_address: "Please enter a valid email address",
			nominees_phone_number: "Please enter a valid phone number for your nominee",
			nominees_fax_number: "This is not a valid fax number",
			nominees_address: "Please enter your nominee's address",
			nominees_city: "Please enter your nominee's city",
			nominees_state: "Please enter a valid state (e.g. OK, TX, etc)",
			nominees_zip: "Please enter a 5-digit zip code",
			membership: "Please recommend a membership for your nominee",
			nominees_resume: "Only doc, docx, pdf, rtf, and txt are accepted file types."
		}
	});
});
