
/* Add/remove form elements using jQuery http://mohdshaiful.wordpress.com/ */
function addFormField() {
	var id = document.getElementById("id").value;

$("#additionalDates").append("<tr id='row" + id + "'><td><input id='start_date" + id + "' class='required date start_date' type='text' name='start_date[" + id + "]' /></td><td><input id='start_time" + id + "' class='required time start_time' type='text' name='start_time[" + id + "]' /></td><td><input id='end_time" + id + "' class='required time end_time' type='text' name='end_time[" + id + "]' /></td><td><input id='end_date" + id + "' type='text' class='required date end_date' name='end_date[" + id + "]' /></td><td><input id='all_day_event' type='checkbox' name='all_day_event[" + id + "]' /> <label style='display:inline' for='all_day_event'>All Day Event</label>&nbsp;&nbsp<span class='delete_event' onClick='removeFormField(\"#row" + id + "\");'>Remove</span></td></tr>");
	id = (id - 1) + 2;
	document.getElementById("id").value = id;
}

function removeFormField(id) {
	$(id).remove();
}

$(document).ready(function(){

/* Regular Expression Pattern for Matches 8am | 8 am | 8:00 am http://regexlib.com/Search.aspx?k=time (^([0-9]|[0-1][0-9]|[2][0-3]):([0-5][0-9])(\s{0,1})(AM|PM|am|pm|aM|Am|pM|Pm{2,2})$)|(^([0-9]|[1][0-9]|[2][0-3])(\s{0,1})(AM|PM|am|pm|aM|Am|pM|Pm{2,2})$)
*/

jQuery.validator.addMethod("time", function(value, element) { 
  return this.optional(element) || /(^([0-9]|[0-1][0-9]|[2][0-3]):([0-5][0-9])(\s{0,1})(AM|PM|am|pm|aM|Am|pM|Pm{2,2})$)|(^([0-9]|[1][0-9]|[2][0-3])(\s{0,1})(AM|PM|am|pm|aM|Am|pM|Pm{2,2})$)/.test(value); 
}, "Enter Time as 10:00 PM");

/* Custom Function for US Phone Number validation*/ 
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");

$("#eventForm").validate({
	
  rules: {
    /* This rule requires 200 characters or less for the Event Description field */
    event_desc: {
      required: false,
      rangelength: [0, 200]
    }
  }
});
});

/* This function calls the datepicker.  It is modified to work with dynamically created elements, thanks to Vance Lucas' post: http://www.vancelucas.com/blog/jquery-ui-datepicker-with-ajax-and-livequery/comment-page-1/#comment-271 */
$(function(){
	$('input.start_date').live('click', function() {
		$(this).datepicker({showOn:'focus'}).focus();
	});
});
$(function(){
	$('input.end_date').live('click', function() {
		$(this).datepicker({showOn:'focus'}).focus();
	});
});

