$(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}$/);
	}, "Please specify a valid phone number");
	
	jQuery.validator.addMethod("zipcode", function(zip) {
		zip = zip.replace(/^\s+/, "");
		zip = zip.replace(/\s+$/, "");
		
		if(zip.length == 0) {
			return true;
		}
	
		if(zip.match(/^\d{5}([- ]?\d{4})?$/)) {
			return true;
		}
			return false;
		}, "Please specify a valid US ZIP code");
	
	jQuery.validator.addMethod("char70", function(char70) {
		if(char70.length > 70){return false} else {return true};								   
	}, "Only 70 characters allowed")
	
	jQuery.validator.addMethod("limit500", function(limit500) {
		if(limit500.length > 500){return false} else {return true};								   
	}, "Only 500 characters allowed")
	
	jQuery.validator.addMethod("limit5000", function(limit5000) {
		if(limit5000.length > 5000){return false} else {return true};								   
	}, "Only 5000 characters allowed")
	
	jQuery.validator.addMethod("shortUrl", function(shortUrl) {
		var shortUrlReg = /^(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(shortUrl);
		if(shortUrlReg){
			return true;
		} else {
			return false;
		};								   
	}, "Enter a valid URL without 'http://'")
						   
	$("#signup").validate({
	  rules: {
		field: {
		  required: true,
		  phoneUS: true
		}
	  }
	});
	
	$("#update").validate({
	  rules: {
		field: {
		  required: true,
		  phoneUS: true
		}
	  }
	});
	
	
	$("#company").keyup(function(){
		getCompanyCount();						 
	});
	
	$("#profile_short").keyup(function(){
		getShortProfileCount();						 
	});
	
	$("#profile_long").keyup(function(){
		getLongProfileCount();						 
	});
	
	function getCompanyCount(){
		var count = $("#company").val().length;
		$(".companyCount").find(".activeCount").html(count);
		if(count > 70){
			$(".companyCount").addClass("red");
		} else {
			$(".companyCount").removeClass("red");	
		};
	};
	
	function getShortProfileCount(){
		var count = $("#profile_short").val().length;
		$(".profileShortCount").find(".activeCount").html(count);
		if(count > 500){
			$(".profileShortCount").addClass("red");
		} else {
			$(".profileShortCount").removeClass("red");	
		};
	};
	
	function getLongProfileCount(){
		var count = $("#profile_long").val().length;
		$(".profileLongCount").find(".activeCount").html(count);
		if(count > 5000){
			$(".profileLongCount").addClass("red");
		} else {
			$(".profileLongCount").removeClass("red");	
		};
	};
	
	getCompanyCount();
	getShortProfileCount();
	getLongProfileCount();
});