/**
 * Set up behaviors for the UDM Form on page load
 * 
 * This is quick, dirty, and totally inelegant. But it works.
 */
jQuery( function() {
	
	// initialize the datepickers
	jQuery("input.date").datepicker( { changeYear: true } );
	
	// ...and give some extra juju to the DOB picker
	var endYear = new Date().getFullYear();
	var startYear = endYear - 100;
	jQuery("#claimant-dob")
		.datepicker( "option", "yearRange", startYear + ":" + endYear );
	
	// initialize the placeholders on the phone fields
	jQuery("input.phone").each( function() {
		blurPlaceholder( this );
	} );
	
	// ...and clear them on form submit
	jQuery("#udm-form").submit( function() {
		focusPlaceholder( document.getElementById("requestor-phone") );
		focusPlaceholder( document.getElementById("requestor-fax") );
		focusPlaceholder( document.getElementById("requestor2-phone") );
		focusPlaceholder( document.getElementById("requestor2-fax") );
		focusPlaceholder( document.getElementById("claimant-phone") );
		focusPlaceholder( document.getElementById("physician-phone") );
		focusPlaceholder( document.getElementById("physician-fax") );
	} );
	
	// form validation
	
	var states = [
		"al", "alabama",
		"ak", "alaska",
		"az", "arizona",
		"ar", "arkansas",
		"ca", "california",
		"co", "colorado",
		"ct", "connecticut",
		"de", "delaware",
		"dc", "district of columbia",
		"fl", "florida",
		"ga", "georgia",
		"hi", "hawaii",
		"id", "idaho",
		"il", "illinois",
		"in", "indiana",
		"ia", "iowa",
		"ks", "kansas",
		"ky", "kentucky",
		"la", "louisiana",
		"me", "maine",
		"md", "maryland",
		"ma", "massachusetts",
		"mi", "michigan",
		"mn", "minnesota",
		"ms", "mississippi",
		"mo", "missouri",
		"mt", "montana",
		"ne", "nebraska",
		"nv", "nevada",
		"nh", "new hampshire",
		"nj", "new jersey",
		"nm", "new mexico",
		"ny", "new york",
		"nc", "north carolina",
		"nd", "north dakota",
		"oh", "ohio",
		"ok", "oklahoma",
		"or", "oregon",
		"pa", "pennsylvania",
		"pr", "puerto rico",
		"ri", "rhode island",
		"sc", "south carolina",
		"sd", "south dakota",
		"tn", "tennessee",
		"tx", "texas",
		"ut", "utah",
		"vt", "vermont",
		"va", "virginia",
		"wa", "washington",
		"wv", "west virginia",
		"wi", "wisconsin",
		"wy", "wyoming"
	];	              

	// requestor 1
	
	jQuery("#requestor-firstName").blur( function() {
		if ( jQuery(this).val() === "" ) {
			jQuery("#requestor-firstName-error").html("First Name Required").show();
		} else {
			jQuery("#requestor-firstName-error").hide();
		}
	} );
	
	jQuery("#requestor-lastName").blur( function() {
		if ( jQuery(this).val() === "" ) {
			jQuery("#requestor-lastName-error").html("Last Name Required").show();
		} else {
			jQuery("#requestor-lastName-error").hide();
		}
	} );
	
	jQuery("#requestor-titlePosition").blur( function() {
		if ( jQuery(this).val() === "" ) {
			jQuery("#requestor-titlePosition-error").html("Title/Position Required").show();
		} else {
			jQuery("#requestor-titlePosition-error").hide();
		}
	} );
	
	jQuery("#requestor-Organization").blur( function() {
		if ( jQuery(this).val() === "" ) {
			jQuery("#requestor-Organization-error").html("Organization Required").show();
		} else {
			jQuery("#requestor-Organization-error").hide();
		}
	} );
	
	jQuery("#requestor-address1").blur( function() {
		if ( jQuery(this).val() === "" ) {
			jQuery("#requestor-address1-error").html("Address Required").show();
		} else {
			jQuery("#requestor-address1-error").hide();
		}
	} );
	
	jQuery("#requestor-city").blur( function() {
		if ( jQuery(this).val() === "" ) {
			jQuery("#requestor-city-error").html("City Required").show();
		} else {
			jQuery("#requestor-city-error").hide();
		}
	} );
	
	jQuery("#requestor-state").blur( function() {
		if ( jQuery(this).val() === "" ) {
			jQuery("#requestor-state-error").html("State Required").show();
		} else if ( !isState( jQuery(this).val() ) ) {
			jQuery("#requestor-state-error").html("Invalid State").show();
		} else {
			jQuery("#requestor-state-error").hide();
		}
	} );
	
	jQuery("#requestor-zipcode").blur( function() {
		if ( jQuery(this).val() === "" ) {
			jQuery("#requestor-zipcode-error").html("ZIP Code Required").show();
		} else if ( !isZIP( jQuery(this).val() ) ) {
			jQuery("#requestor-zipcode-error").html("Invalid ZIP Code").show();
		} else {
			jQuery("#requestor-zipcode-error").hide();
		}
	} );
	
	jQuery("#requestor-phone")
		.blur( function() {
			blurPlaceholder(this);
			validateRequestorPhone();
		} )
		.focus( function() {
			focusPlaceholder(this);
		} );
	
	jQuery("#requestor-phone-ext")
		.blur( function() {
			validateRequestorPhone();
		} );
	
	function validateRequestorPhone() {
		if ( jQuery("#requestor-phone").val() === "" || jQuery("#requestor-phone").val() === jQuery("#requestor-phone").attr("title") ) {
			jQuery("#requestor-phone-error").html("Phone Required").show();
		} else if ( !isPhone( jQuery("#requestor-phone").val() ) ) {
			jQuery("#requestor-phone-error").html("Invalid Phone Number").show();
		} else if ( jQuery("#requestor-phone-ext").val() !== "" && !isInt( jQuery("#requestor-phone-ext").val() ) ) { 
			jQuery("#requestor-phone-error").html("Invalid Extension").show();
		} else {
			jQuery("#requestor-phone-error").hide();
		}
	}

	jQuery("#requestor-fax")
		.blur( function() {
			blurPlaceholder(this);
			if ( jQuery(this).val() === "" || jQuery(this).val() === jQuery(this).attr("title") ) {
				jQuery("#requestor-fax-error").html("Fax Required").show();
			} else if ( !isPhone( jQuery(this).val() ) ) {
				jQuery("#requestor-fax-error").html("Invalid Fax Number").show();
			} else {
				jQuery("#requestor-fax-error").hide();
			}
		} )
		.focus( function() {
			focusPlaceholder(this);
		} );
	
	jQuery("#requestor-email").blur( function() {
		if ( jQuery(this).val() === "" ) {
			jQuery("#requestor-email-error").html("Email Required").show();
		} else if ( !isEmail( jQuery(this).val() ) ) {
			jQuery("#requestor-email-error").html("Invalid Email").show();
		} else {
			jQuery("#requestor-email-error").hide();
		}
	} );
	
	// requestor 2
	
	jQuery("#requestor2-firstName").blur( function() {
		if ( jQuery(this).val() === "" ) {
			jQuery("#requestor2-firstName-error").html("First Name Required").show();
		} else {
			jQuery("#requestor2-firstName-error").hide();
		}
	} );
	
	jQuery("#requestor2-lastName").blur( function() {
		if ( jQuery(this).val() === "" ) {
			jQuery("#requestor2-lastName-error").html("Last Name Required").show();
		} else {
			jQuery("#requestor2-lastName-error").hide();
		}
	} );
	
	jQuery("#requestor2-titlePosition").blur( function() {
		if ( jQuery(this).val() === "" ) {
			jQuery("#requestor2-titlePosition-error").html("Title/Position Required").show();
		} else {
			jQuery("#requestor2-titlePosition-error").hide();
		}
	} );
	
	jQuery("#requestor2-Organization").blur( function() {
		if ( jQuery(this).val() === "" ) {
			jQuery("#requestor2-Organization-error").html("Organization Required").show();
		} else {
			jQuery("#requestor2-Organization-error").hide();
		}
	} );

	jQuery("#requestor2-address1").blur( function() {
		if ( jQuery(this).val() === "" ) {
			jQuery("#requestor2-address1-error").html("Address Required").show();
		} else {
			jQuery("#requestor2-address1-error").hide();
		}
	} );
	
	jQuery("#requestor2-city").blur( function() {
		if ( jQuery(this).val() === "" ) {
			jQuery("#requestor2-city-error").html("City Required").show();
		} else {
			jQuery("#requestor2-city-error").hide();
		}
	} );
	
	jQuery("#requestor2-state").blur( function() {
		if ( jQuery(this).val() === "" ) {
			jQuery("#requestor2-state-error").html("State Required").show();
		} else if ( !isState( jQuery(this).val() ) ) {
			jQuery("#requestor2-state-error").html("Invalid State").show();
		} else {
			jQuery("#requestor2-state-error").hide();
		}
	} );
	
	jQuery("#requestor2-zipcode").blur( function() {
		if ( jQuery(this).val() === "" ) {
			jQuery("#requestor2-zipcode-error").html("ZIP Code Required").show();
		} else if ( !isZIP( jQuery(this).val() ) ) {
			jQuery("#requestor2-zipcode-error").html("Invalid ZIP Code").show();
		} else {
			jQuery("#requestor2-zipcode-error").hide();
		}
	} );
	
	jQuery("#requestor2-phone")
		.blur( function() {
			blurPlaceholder(this);
			validateRequestor2Phone();
		} )
		.focus( function() {
			focusPlaceholder(this);
		} );
	
	jQuery("#requestor2-phone-ext")
		.blur( function() {
			validateRequestor2Phone();
		} );
	
	function validateRequestor2Phone() {
		if ( jQuery("#requestor2-phone").val() === "" || jQuery("#requestor2-phone").val() === jQuery("#requestor2-phone").attr("title") ) {
			jQuery("#requestor2-phone-error").html("Phone Required").show();
		} else if ( !isPhone( jQuery("#requestor2-phone").val() ) ) {
			jQuery("#requestor2-phone-error").html("Invalid Phone Number").show();
		} else if ( jQuery("#requestor2-phone-ext").val() !== "" && !isInt( jQuery("#requestor2-phone-ext").val() ) ) { 
			jQuery("#requestor2-phone-error").html("Invalid Extension").show();
		} else {
			jQuery("#requestor2-phone-error").hide();
		}
	}

	jQuery("#requestor2-fax")
		.blur( function() {
			blurPlaceholder(this);
			if ( jQuery(this).val() === "" || jQuery(this).val() === jQuery(this).attr("title") ) {
				jQuery("#requestor2-fax-error").html("Fax Required").show();
			} else if ( !isPhone( jQuery(this).val() ) ) {
				jQuery("#requestor2-fax-error").html("Invalid Fax Number").show();
			} else {
				jQuery("#requestor2-fax-error").hide();
			}
		} )
		.focus( function() {
			focusPlaceholder(this);
		} );

	jQuery("#requestor2-email").blur( function() {
		if ( jQuery(this).val() === "" ) {
			jQuery("#requestor2-email-error").html("Email Required").show();
		} else if ( !isEmail( jQuery(this).val() ) ) {
			jQuery("#requestor2-email-error").html("Invalid Email").show();
		} else {
			jQuery("#requestor2-email-error").hide();
		}
	} );

	// claimant
	
	jQuery("#claimant-firstName").blur( function() {
		if ( jQuery(this).val() === "" ) {
			jQuery("#claimant-firstName-error").html("First Name Required").show();
		} else {
			jQuery("#claimant-firstName-error").hide();
		}
	} );	

	jQuery("#claimant-lastName").blur( function() {
		if ( jQuery(this).val() === "" ) {
			jQuery("#claimant-lastName-error").html("Last Name Required").show();
		} else {
			jQuery("#claimant-lastName-error").hide();
		}
	} );
	
	jQuery("#claimant-address1").blur( function() {
		if ( jQuery(this).val() === "" ) {
			jQuery("#claimant-address1-error").html("Address Required").show();
		} else {
			jQuery("#claimant-address1-error").hide();
		}
	} );
	
	jQuery("#claimant-city").blur( function() {
		if ( jQuery(this).val() === "" ) {
			jQuery("#claimant-city-error").html("City Required").show();
		} else {
			jQuery("#claimant-city-error").hide();
		}
	} );	

	jQuery("#claimant-state").blur( function() {
		if ( jQuery(this).val() === "" ) {
			jQuery("#claimant-state-error").html("State Required").show();
		} else if ( !isState( jQuery(this).val() ) ) {
			jQuery("#claimant-state-error").html("Invalid State").show();
		} else {
			jQuery("#claimant-state-error").hide();
		}
	} );	

	jQuery("#claimant-zipcode").blur( function() {
		if ( jQuery(this).val() === "" ) {
			jQuery("#claimant-zipcode-error").html("ZIP Code Required").show();
		} else if ( !isZIP( jQuery(this).val() ) ) {
			jQuery("#claimant-zipcode-error").html("Invalid ZIP Code").show();
		} else {
			jQuery("#claimant-zipcode-error").hide();
		}
	} );

	jQuery("#claimant-phone")
		.blur( function() {
			blurPlaceholder(this);
			if ( jQuery(this).val() !== "" && jQuery(this).val() !== jQuery(this).attr("title") && !isPhone( jQuery(this).val() ) ) {
				jQuery("#claimant-phone-error").html("Invalid Phone Number").show();
			} else {
				jQuery("#claimant-phone-error").hide();
			}
		} )
		.focus( function() {
			focusPlaceholder(this);
		} );


	jQuery("#claimNumber").blur( function() {
		if ( jQuery(this).val() === "" ) {
			jQuery("#claimNumber-error").html("Claim Number Required").show();
		} else {
			jQuery("#claimNumber-error").hide();
		}
	} );

	// physician
	
	jQuery("#physician-firstName").blur( function() { 
		if ( jQuery(this).val() === "" ) {
			jQuery("#physician-firstName-error").html("First Name Required").show();
		} else {
			jQuery("#physician-firstName-error").hide();
		}
	} );
	
	jQuery("#physician-lastName").blur( function() { 
		if ( jQuery(this).val() === "" ) {
			jQuery("#physician-lastName-error").html("Last Name Required").show();
		} else {
			jQuery("#physician-lastName-error").hide();
		}
	} );
	
	jQuery("#physician-npi").blur( function() { 
		if ( jQuery(this).val() !== "" && !isNPI( jQuery(this).val() ) ) {
			jQuery("#physician-npi-error").html("Invalid NPI").show();			
		} else {
			jQuery("#physician-npi-error").hide();
		}
	} );
	
	jQuery("#physician-address").blur( function() { 
		if ( jQuery(this).val() === "" ) {
			jQuery("#physician-address-error").html("Address Required").show();
		} else {
			jQuery("#physician-address-error").hide();
		}
	} );
	
	jQuery("#physician-city").blur( function() { 
		if ( jQuery(this).val() === "" ) {
			jQuery("#physician-city-error").html("City Required").show();
		} else {
			jQuery("#physician-city-error").hide();
		}
	} );
	
	jQuery("#physician-state").blur( function() { 
		if ( jQuery(this).val() === "" ) {
			jQuery("#physician-state-error").html("State Required").show();
		} else if ( !isState( jQuery(this).val() ) ) {
			jQuery("#physician-state-error").html("Invalid State").show();
		} else {
			jQuery("#physician-state-error").hide();
		}
	} );
	
	jQuery("#physician-zipcode").blur( function() { 
		if ( jQuery(this).val() !== "" && !isZIP( jQuery(this).val() ) ) {
			jQuery("#physician-zipcode-error").html("Invalid ZIP Code").show();
		} else {
			jQuery("#physician-zipcode-error").hide();
		}
	} );
	
	jQuery("#physician-phone")
		.blur( function() {
			blurPlaceholder(this);
			validatePhysicianPhone();
		} )
		.focus( function() {
			focusPlaceholder(this);
		} );
	
	jQuery("#physician-phone-ext")
		.blur( function() {
			validatePhysicianPhone();
		} );
	
	function validatePhysicianPhone() {
		if ( jQuery("#physician-phone").val() === "" || jQuery("#physician-phone").val() === jQuery("#physician-phone").attr("title") ) {
			jQuery("#physician-phone-error").html("Phone Required").show();
		} else if ( !isPhone( jQuery("#physician-phone").val() ) ) {
			jQuery("#physician-phone-error").html("Invalid Phone Number").show();
		} else if ( jQuery("#physician-phone-ext").val() !== "" && !isInt( jQuery("#physician-phone-ext").val() ) ) { 
			jQuery("#physician-phone-error").html("Invalid Extension").show();
		} else {
			jQuery("#physician-phone-error").hide();
		}
	}

	jQuery("#physician-fax")
		.blur( function() {
			blurPlaceholder(this);
			if ( jQuery(this).val() !== "" && jQuery(this).val() !== jQuery(this).attr("title") && !isPhone( jQuery(this).val() ) ) {
				jQuery("#physician-fax-error").html("Invalid Fax Number").show();
			} else {
				jQuery("#physician-fax-error").hide();
			}
		} )
		.focus( function() {
			focusPlaceholder(this);
		} );

	// utility functions
	
	function isState( state ) {
		return jQuery.inArray( state.toLowerCase(), states ) !== -1;
	}
	
	function isInt( int ) {
		return new RegExp("^[0-9]+$").test( int );
	}
	
	function isZIP( zip ) {
		return new RegExp("^[0-9]{5}$").test( zip );
	}
	
	function isNPI( npi ) {
		return new RegExp("^[0-9]{10}$").test( npi );
	}

	function isPhone( phone ) {
		return new RegExp("^[2-9][0-9]{2}-[2-9][0-9]{2}-[0-9]{4}$").test( phone );
	}
	
	function isEmail( email ) {
		var re = /^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i;
		return re.test( email ); 
	}
	
	function isDate( date ) {
		var valid = false;
		var re = new RegExp( "^(0[1-9]|1[0-2])/(0[1-9]|[1-2][0-9]|3[0-1])/(19|20)[0-9]{2}$" );
		if ( re.test( date ) ) {
			var dateParts = date.split("/");
			var m = dateParts[0];
			var d = dateParts[1];
			var y = dateParts[2];
			valid = m > 0 && m < 13 && y > 0 && y < 32768 && d > 0 && d <= (new Date(y, m, 0)).getDate();
		}
		return valid;
	}
	
	function focusPlaceholder( element ) {
		var $e = jQuery( element );
		if ( $e.val() === $e.attr("title") ) {
			$e.val("").removeClass("placeholder");
		}
	}
	
	function blurPlaceholder( element ) {
		var $e = jQuery( element );
		if ( $e.val() === "" ) {
			$e.val( $e.attr("title") ).addClass("placeholder");
		}
	}

} );