$.fn.validate = function() {
    var defaultMsg = 'This field is required';
    var selectors  = !arguments[0] ? ".required input[type='text'], .required select" : arguments[0];
    this.submit(function(e) {
    	var er = 0;
    	var hack = 0;
		$('.form-error',this).remove();
	    // Not empty validation
		$(selectors,this).each(function (i) {
			var valid = true; 
			var v = this.value;
			cls = $(this).attr('class');
			switch(true){
				case cls.match("validate-zip") !== null:
					valid = validate_zip(v);
					urlHack = isUrl(v);
					break;			
				case cls.match("validate-email") !== null:
					valid = validate_email(v);
					urlHack = isUrl(v);
					break;
				case cls.match("validate-checked") !== null:
					valid = validate_checked(this);
					urlHack = isUrl(v);
					break;
				default:
					valid = validate_empty(v);
					urlHack = isUrl(v);
					break;
			}
			if(!valid){
				var msg  = ($(this).attr('title') !== undefined) ? $(this).attr('title') : defaultMsg;
				var html = '<span class="form-error">'+msg+'</span>';
				$('#'+this.id).after(html);
				er++; 
			} else {
				$('#'+this.id).removeClass('form-error');
			}
			if(urlHack) {
				hack++;
			}
		});
		if (er) {
			alert('Please complete the fields in red!');
			e.preventDefault();
		}
		if (hack) {
			alert('You may not include any URLs in your message!');
			e.preventDefault();
		}
		/* validation methods */
		function validate_zip(v) {
			return (validate_numeric(v) && v.length == 5) ? true : false; 
		}		
		function validate_numeric(v){
			return (isNaN(v) == false && validate_empty(v)) ? true : false; 
		}
		function validate_email(v) {
			var reg = new RegExp('^[^@()<>,;:\\\\/"[\\]]+@[^@()<>,;:\\\\/"[\\]]+\\.[a-zA-Z0-9]{2,6}$','');
			return (v.match(reg)) ? true : false;
		}	
		function validate_empty(v) {
			return (v !== '') ? true : false; 
		}
		function validate_checked(obj) {
			return ($(obj)[0].checked == true) ? true : false; 
		}
		function isUrl(v) {
			var reg = new RegExp('^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$','');
			return (v.match(reg)) ? true : false;
		}
    });
}

$(document).ready(function(){
	$('.email').click(function() {
		var answer = confirm('Please keep in mind that merely contacting Buist, Byars & Taylor, LLC will not establish an attorney-client relationship. Buist, Byars & Taylor cannot represent you until the firm knows there would not be a conflict of interest, and the firm determines that it is otherwise able to accept the engagement. Accordingly, please do not send Buist, Byars & Taylor any information or documents until a formal attorney-client relationship has been established through an interview with an attorney and authorized in the form of an engagement letter from the firm. Any information or documents sent prior to your receipt of an engagement letter cannot be treated as confidences, secrets or protected information of any nature. Clicking "OK" acknowledges that you understand and agree with this notice.');
		if(answer) {
			return true;
		}
		else {
			return false;
		}
	});
	
	$('textarea.wysiwyg').wysiwyg();
	
	$("ul.media li:nth-child(odd)").addClass("alt");
});
