$(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");
    
$("#request-form-generic").validate({
	rules: {
		onfocusout: true,
		"00N40000001fWHX": {
			required: true
		},
		first_name: {
			required: true
		},
		last_name:{
			required: true
		},
		company: {
			required: true
		},
		title: {
			required: true
		},
		email: {
			required: true,
			email: true,	
			maxlength: 64
		},
		phone: {
			required: true,
			phone: true
		},
		street: {
			required: true
		},
		city: {
			required: true
		},
		state: {
			required: true
		},
		zip: {
			required: true
		},
		country: {
			required: true
		},
		comments: {
			required: true
		}
	},
	messages: {
		"00N40000001fWHX": {
			required: "Please make a selection."
		},
		first_name: {
			required: "Please enter your name."
		},
		last_name:{
			required: "Please enter your full name."
		},
		company: {
			required: "Please enter the name of your company."
		},
		title: {
			required: "Please enter your title."
		},
		email: {
			required: "Please enter a valid e-mail address."
		},
		phone: {
			required: "Please enter your phone number."
		},
		street: {
			required: "This field is required"
		},
		city: {
			required: "This field is required"
		},
		state: {
			required: "This field is required"
		},
		zip: {
			required: "This field is required"
		},
		country: {
			required: "Please select a country."
		},
		comments: {
			required: "This field is required"
		}
	},
	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>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>State:</label><!-- Default State Field  --><input type="text" id="state_freeform" name="state" />'); 
		}
	});

});