/*	Please do not duplicate this document with out prior permission.
	This unique combination of styles, properties, font-families and poistioning are copyright of ohioseagrant.osu.edu */

/*	SL Contact Form
	This includes values to be inserted into the form
		
	George Oommen (oommen.6@osu.edu)
	February 24 2009 */

function limitChars(textid, limit, infodiv) {
	var text = $('#'+textid).val();	
	var textlength = text.length;
	if (textlength >= limit) {
		$('#' + infodiv).html('(0 characters left to use)');
		$('#' + textid).val(text.substr(0, limit));
		return false;
	}
	else {
		$('#' + infodiv).html('(' + (limit - textlength) + ' characters left to use)');
		return true;
	}
}

 $(document).ready( function() {
	
	$('form#sl-contact-form select').styledSelect();
} );

$(document).ready(function() {

	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}$/);
	}, "" );
	
	jQuery.each(jQuery.validator.messages, function(i) {
		jQuery.validator.messages[i] = "";
	} );

	$("#sl-contact-form").validate({
		rules: {
			field: {
				required: true,
				phoneUS: true,
				number: true,
				email: true
			}
		}
	} );
} );

$(document).ready(function() {
 	$('#message').keyup(function(){
 		limitChars('message', 650, 'info-limit');
 	})
});

