// JavaScript Document

//JSON City State Lookup
/*
$(document).ready(function() {
$("#Contact0PostalCode").blur(function() {
	var city = $("#Contact0City");
	var state = $("#Contact0State");
	if (!city.val() && $(this).valid()) {
		$.getJSON("http://www.geonames.org/postalCodeLookupJSON?&country=US&callback=?", {postalcode: this.value }, function(response) {
			if (!city.val() && response && response.postalcodes.length && response.postalcodes[0].placeName) {
				city.val(response.postalcodes[0].placeName);
				state.val(response.postalcodes[0].adminCode1);
			}
		})
	}
});
});
*/
//JSON Copy Email to Username
/*
$(document).ready(function() {
$("#Contact0Email").blur(function() {
	var email = $("#Contact0Email");
	var username = $("#Contact0Username");
	
	username.val(email.val());
});
});
*/

//Field Masks
jQuery(function($){
   $("#Contact0Phone1").mask("(999) 999-9999");
   $("#Contact0PostalCode").mask("99999",{placeholder:" "});
   $("#Contact0State").mask("aa",{placeholder:" "});
});
	
//Validation Customizations Order Form
$(document).ready(function() {
	// validate signup form on keyup and submit
	var validator = $("#aspnetForm").validate({
				
		//validation rules
		rules: {
			Contact0FirstName: "required",
			Contact0LastName: "required",
			Contact0Password: {
				required: true,
				minlength: 6
			},
			Contact0PasswordConfirm: {
				required: true,
				minlength: 6,
				equalTo: "#Contact0Password"
			},
			Contact0Email: {
				required: true,
				email: true
			},
			Contact0PostalCode: {
				required: true,
				digits: true
			},
			Checkbox0: "required"
			},
		
		//validation messages
		messages: {
			Contact0FirstName: "Enter your First Name",
			Contact0LastName: "Enter your Last Name",
			Contact0PasswordConfirm: {
				required: "Provide a password",
				rangelength: jQuery.format("Enter at least {0} characters")
			},
			Contact0PasswordConfirm: {
				required: "Repeat your password",
				minlength: jQuery.format("Enter at least {0} characters"),
				equalTo: "Password doesn't match"
			},
			Contact0Email: {
				required: "Please enter a valid email address",
				minlength: "Please enter a valid email address"
			},
			Contact0PostalCode: "Enter a valid zip code",
			Checkbox0: "Please agree to the Terms of Service"
		},
		
		//Error message at top of form
		invalidHandler: function(e, validator) {
			var errors = validator.numberOfInvalids();
			if (errors) {
				var message = errors == 1
					? 'There was 1 problem with your submission.<br />The error has been highlighted below.'
					: 'There were ' + errors + ' problems with your submission.<br />Errors have been highlighted below.';
				$("div.error span").html(message);
				$("div.error").show();
			} else {
				$("div.error").hide();
			}
		},
		//Groups Error Placement
		groups: {
    		fullname: "Contact0FirstName Contact0LastName",
			passwords: "Contact0Password Contact0PasswordConfirm",
			billaddress: "Contact0PostalCode Contact0City Contact0State",
			creditinfo: "CreditCard0CardType CreditCard0CardNumber"
  		},
		//Custom error element, default is label
		errorElement: "span",

		// the errorPlacement has to take the layout into account
		// Options error.insertAfter("#Contact0LastName"); error.appendTo( element.next().prev() );.children()http://docs.jquery.com/Traversing
		errorPlacement: function(error, element) {
			if (element.attr("name") == "Contact0FirstName" 
                	|| element.attr("name") == "Contact0LastName" )
			error.insertAfter("#Contact0LastName");			
			else if (element.attr("name") == "Contact0Password" 
                	|| element.attr("name") == "Contact0PasswordConfirm" )
			error.insertAfter("#Contact0PasswordConfirm");
			else if (element.attr("name") == "Contact0PostalCode" 
                	|| element.attr("name") == "Contact0City"
					|| element.attr("name") == "Contact0State" )
			error.insertAfter("#Contact0State");
			else if (element.attr("name") == "CreditCard0CardType" 
                	|| element.attr("name") == "CreditCard0CardNumber" )
			error.insertAfter("#CreditCard0CardNumber");
			
			else if ( element.is(":radio") )
				error.appendTo( element.parent().next().next() );
			else if ( element.is(":checkbox") )
				error.insertAfter( element.parent().next() );
			else
				error.insertAfter(element);
		},

		// set this class to error-labels to indicate valid fields
		success: function(label) {
			label.html("&nbsp;").addClass("checked");
		}
	});
});
//Form highlight
$(document).ready(function()
{
	$('#aspnetForm input, #aspnetForm textarea, #aspnetForm select').focus(function(){
		$(this).parents('fieldset').addClass("formHighlight");
	}).blur(function(){
		$(this).parents('fieldset').removeClass("formHighlight");
	});
});