$(function() {
    function isInteger(s) {
        var i;
        for (i = 0; i < s.length; i++) { // Check that current character is number.
            var c = s.charAt(i);
            if (((c < "0") || (c > "9"))) return false;
        } // All characters are numbers.
        return true;
    }
    function stripCharsInBag(s, bag) {
        var i;
        var returnString = ""; // Search through string's characters one by one.
        // If character is not in bag, append to returnString.
        for (i = 0; i < s.length; i++) { // Check that current character isn't whitespace.
            var c = s.charAt(i);
            if (bag.indexOf(c) == -1) returnString += c;
        }
        return returnString;
    }
    
    $.validator.addMethod("phone",
    function(phone_number, element) {
        var digits = "0123456789";
        var phoneNumberDelimiters = "()- ext.";
        var validWorldPhoneChars = phoneNumberDelimiters + "+";
        var minDigitsInIPhoneNumber = 10;
        s = stripCharsInBag(phone_number, validWorldPhoneChars);
        return this.optional(element) || isInteger(s) && s.length >= minDigitsInIPhoneNumber;
    },
    "Please enter a valid phone number");
    
    $('#register-form-mini').validate({
		rules: {
		onfocusout: true,
		emailAddress: {
			required: true,
			email: true,
			maxlength: 64
		},
		phoneNumber:{
			required: true,
			phone: true,
			maxlength: 16
		},
		password: {
			required: true,
			minlength: 6,
			maxlength: 32
		},
		confirmPassword: {
			required: true,
			equalTo: "#password"
		},
		firstName: {
			maxlength: 32
		},
		lastName: {
			maxlength: 32
		},
		companyName: {
			maxlength: 32
		},
		address1: {
			maxlength: 64
		},
		city: {
			maxlength: 32
		},
		state: {
			maxlength: 32,
			required: true
		},
		postalCode: {
			minlength:5,
			maxlength: 12
		},
		terms: {
			required: true
		},
		country: "required"
	},
	messages: {
		emailAddress: {
			required: "Your email address is required",
			email: "Please enter a valid email address to continue.<br/>For example: info@jivox.com",
			maxlength: "Your email address cannot exceed 64 characters"
		},
		phoneNumber: {
			required: "Please enter a valid phone number to continue.",
			phone: "Please enter a valid phone number to continue. For example: For US: 1-650-577-7800<br/>or for outside of US: +91 80 40487000."
		},
		password: {
			required: "Please enter a password to continue.",
			minlength: "Passwords are Case Sensitive. Use 6 to 32 characters, no spaces.",
			maxlength: "Passwords are Case Sensitive. Use 6 to 32 characters, no spaces."
		},
		confirmPassword: {
			required: "Please confirm your password to continue.",
			equalTo: "Passwords do not match"
		},
		terms: {
			required: "Please read the Jivox Terms and Conditions. You must check the box and accept Terms and Conditions to continue."
		},
		state: {
			required: "Please enter or select a state."
		},
		country: {
			required: "Please select a country."
		}
	},
	highlight: function(element, errorClass) {
		$(element).addClass(errorClass);
	},
	unhighlight: function(element, errorClass) {
		$(element).removeClass(errorClass);
	},
	focusInvalid: true
    });

	$('#country').change( function(eventObject){
		var country = $('#country').val();
		if(country == 'United States') {
			jQuery("#state-field").html("").html('<label><sup>*</sup>State:</label><!-- This select box is shown when United States is selected from the country field  --><select id="state" name="state"><option value="" selected="selected">-Select One-</option><option value="AL">Alabama</option><option value="AK">Alaska</option><option value="AZ">Arizona</option><option value="AR">Arkansas</option><option value="CA">California</option><option value="CO">Colorado</option><option value="CT">Connecticut</option><option value="DE">Delaware</option><option value="DC">District of Columbia</option><option value="FL">Florida</option><option value="GA">Georgia</option><option value="HI">Hawaii</option><option value="ID">Idaho</option><option value="IL">Illinois</option><option value="IN">Indiana</option><option value="IA">Iowa</option><option value="KS">Kansas</option><option value="KY">Kentucky</option><option value="LA">Louisiana</option><option value="ME">Maine</option><option value="MD">Maryland</option><option value="MA">Massachusetts</option><option value="MI">Michigan</option><option value="MN">Minnesota</option><option value="MS">Mississippi</option><option value="MO">Missouri</option><option value="MT">Montana</option><option value="NE">Nebraska</option><option value="NV">Nevada</option><option value="NH">New Hampshire</option><option value="NJ">New Jersey</option><option value="NM">New Mexico</option><option value="NY">New York</option><option value="NC">North Carolina</option><option value="ND">North Dakota</option><option value="OH">Ohio</option><option value="OK">Oklahoma</option><option value="OR">Oregon</option><option value="PA">Pennsylvania</option><option value="RI">Rhode Island</option><option value="SC">South Carolina</option><option value="SD">South Dakota</option><option value="TN">Tennessee</option><option value="TX">Texas</option><option value="UT">Utah</option><option value="VT">Vermont</option><option value="VA">Virginia</option><option value="WA">Washington</option><option value="WV">West Virginia</option><option value="WI">Wisconsin</option><option value="WY">Wyoming</option></select>'); 
		} else {
			jQuery("#state-field").html("").html('<label><sup>*</sup>State:</label><!-- Default State Field  --><input type="text" id="state_freeform" name="state" />'); 
		}
	});

});