function admissionFeeFocus(input) {
	input.value = "[$$.$$] if any";
	input.select();
}
function admissionFeeBlur(input) {
	input.value = "";
}
function validateField(field, filter, message) {
	var fieldValue  = jQuery(field).attr('value');
	//ignore blanks, we can do that with a different function
	if (fieldValue == "")
		return true;
	if (filter.test(fieldValue)){
		/* I am taking the following out on 6/25 because I want to handle the plain www.something.xxx because they are prettier
		if (field == "moreInfoLink") {//check the url field, if it doesn't have 'http://" at the beginning, add it
			var httpFilter = /^(http:\/\/).*$/;
			if (!httpFilter.test(fieldValue))
				document.getElementById(field).value = "http://" + document.getElementById(field).value
		}*/
		return true;
	}
	alert(message);
	jQuery(field).focus();
	jQuery(field).select();
	return false;
}

function notBlank(field, errMsg) {
	var fieldValue  = jQuery(field).attr('value');
	if (fieldValue != "")
		return true;
	else if (field == "#endDate") {//fill the end-date field if the begginning date is valid
		jQuery("#endDate").attr("value", jQuery("#startDate").attr("value"));
		return true;
	}
	else {
		alert(errMsg);
		jQuery(field).focus();
		jQuery(field).select();
		return false;
	}
}
function checkCategory(){
	if(jQuery("#category").attr('selectedIndex') > 0) //check the category box too
		return true;
	else {
		alert("You forgot to select a category for this event, please go back and do so");
		jQuery("#category").focus();
		return false;
	}
}

jQuery.fn.addValue = function(s) {
    if (this.attr('value')) {
        var temp = this.attr('value');
        this.attr('value', temp + s);
    };
    return jQuery;
};

function checkTime(elementID) {
	var timeFilter = /^(0?[0-9]|1[012])(:([0-5][0-9]))?([ ]*)([AaPp][Mm]{0,1})$/;
	var timeFilterWithProperAMorPM = /^(0?[0-9]|1[012])(:([0-5][0-9]))?([ ]*)([AaPp][Mm])$/;

	//if they just used a or p, add the m
	if (timeFilter.test(jQuery(elementID).attr("value")) && !timeFilterWithProperAMorPM.test(jQuery(elementID).attr("value"))) {
		jQuery(elementID).addValue("m");
	}
	return validateField(elementID, timeFilter, "Incorrect time format. Must be hh:mm tt");
}

function checkDate(elementID) {
	var dateFilter = /^(0?[0-9]|1[012])\/(0?[0-9]|[012][0-9]|3[01])\/([0-9]{2,4})$/;
	
	return validateField(elementID, dateFilter, "Incorrect date format. Must be mm/dd/yy.");
}

// Validate the following information:
//  Not blank:
//  * Title 
//  * Start date 
//  * Start/End time
//  * Location
//  * Description
//  * Contact info
//  * Submitter info
//
//  Correct format:
//  * Start date
//  * End date if not blank
//  * Start/End Time

function validateForm()
{
	var emailFilter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	// JH (11/05/09): dateFilter checks for [M]M/[d]d/[yy]yy
	var dateFilter = /^(0?[0-9]|1[012])\/(0?[0-9]|[012][0-9]|3[01])\/([0-9]{2,4})$/;
	// JH (11/05/09): timeFitler checks for [h]h:[m]m[ ][AM|PM]
	var timeFilter = /^(0?[0-9]|1[012])(:([0-5][0-9]))?([ ]*)([AaPp][Mm]{0,1})$/;
	var timeFilterWithProperAMorPM = /^(0?[0-9]|1[012])(:([0-5][0-9]))?([ ]*)([AaPp][Mm])$/;
	var urlFilter = /^(http:\/\/|www.).*$/;
	var optionalFieldOkay = true
	var IsOneDayEvent = false
	var okay = true //boolean to keep track if the fields are valid format
	
	//if they just used a or p, add the m
	if (timeFilter.test(jQuery("#endTime").attr("value")) && !timeFilterWithProperAMorPM.test(jQuery("#endTime").attr("value"))) {
		jQuery("#endTime").addValue("m");
	}
	if (timeFilter.test(jQuery("#startTime").attr("value")) && !timeFilterWithProperAMorPM.test(jQuery("#startTime").attr("value"))) {
		jQuery("#startTime").addValue("m");
	}
	
	//if they entered anything into the optional date field, check their input
	if (jQuery("#endDate").attr("value") != "") {
		optionalFieldOkay = notBlank("#endDate", "You left the end date field blank, please go back and fill it in.") &&
		validateField("#endDate", dateFilter, "Invalid end date entered, be sure to use the format mm/dd/yy.")
	} 
	//else if they left it empty, assume it is a one day event
	else IsOneDayEvent = true;
	
		//check the chronological order of the dates
		if (jQuery("#endDate").attr("value") != "") {
			var beginDate1String = new String(jQuery("#startDate").attr("value"));
			var beginDate2String = new String(jQuery("#endDate").attr("value"));
		
			var beginDate1Month = beginDate1String.substring(0,beginDate1String.indexOf("/"));
			var beginDate1Day = beginDate1String.substring(beginDate1Month.length+1,beginDate1String.indexOf("/",beginDate1String.indexOf("/")+1));
			var beginDate1Year = beginDate1String.substr(beginDate1Month.length+beginDate1Day.length+2);
		
			//alert("making a new date for begindate1 with these parameters: " + beginDate1Month + ",  " + beginDate1Day + ",  " + beginDate1Year);
		
			var beginDate2Month = beginDate2String.substring(0,beginDate2String.indexOf("/"));
			var beginDate2Day = beginDate2String.substring(beginDate2Month.length+1,beginDate2String.indexOf("/",beginDate2String.indexOf("/")+1));
			var beginDate2Year = beginDate2String.substr(beginDate2Month.length+beginDate2Day.length+2);
		
			var beginDate1 = new Date(beginDate1Year,beginDate1Month,beginDate1Day);
			var beginDate2 = new Date(beginDate2Year,beginDate2Month,beginDate2Day);
		
			//compare our dates and make sure they are in chronological order
			if (beginDate2.getTime() < beginDate1.getTime()) {
				alert("Your range of search dates is out of chronological order (" + jQuery("#endDate").attr("value") + " is an earlier date than " + jQuery("#startDate").attr("value") + "), please fix this.");
				return false;
			}
		}
	//once the optional fields are checked and verified, then check the rest of the form
	if (optionalFieldOkay) {
		okay =	notBlank("#title","You left the title field blank, please go back and fill it in.") &&
		checkCategory() &&
		notBlank("#startDate","You left the title start date blank, please go back and fill it in.") &&
		notBlank("#startTime","You left the start time field blank, please go back and fill it in.") &&
		notBlank("#endDate","You left the end date field blank, please go back and fill it in.") &&
		notBlank("#endTime","You left the end time field blank, please go back and fill it in.") &&
		notBlank("#location","You left the location field blank, please go back and fill it in.") &&
		notBlank("#description","You left the description field blank, please go back and fill it in.") &&
		notBlank("#contactName","You left the contact name field blank, please go back and fill it in.") &&
		notBlank("#contactPhone","You left the contact phone field blank, please go back and fill it in.") &&
		notBlank("#contactEmail","You left the contact e-mail field blank, please go back and fill it in.") &&
		validateField("#startDate", dateFilter, "You mistyped the start date, be sure to use the format mm/dd/yy.") &&
		validateField("#startTime", timeFilter, "You mistyped the start time, be sure to use the format hh:mm am/pm.") &&
		validateField("#endTime", timeFilter, "You mistyped the end time, be sure to use the format hh:mm am/pm.") &&
		validateField("#contactEmail", emailFilter, "You mistyped the e-mail address for the event contact person, be sure to use the format name@somedomain.xxx") 
		//&& validateField("#moreInfoLink", urlFilter, "The web address for more information must begin with one of the following:\n-- \'www\' (e.g. www.google.com)\n-- \'http://\' (e.g. http://google.com)")
	} //if (the optional fields are okay)
	else {
	
		return false;
	}
//the following code fills in the boxes for the submitter info if they are empty with copies of the contact info
	if (okay){
		if (jQuery("#submitterEmail").attr("value") == "") {
			jQuery("submitterEmail").attr("value", jQuery("contactEmail").attr("value"));
		}
		else if (!validateField("#submitterEmail", emailFilter, "The e-mail address that you submitted for the submitter is mistyped, be sure to use the format name@somedomain.xxx, if you are both the submitter and the event contact person, leave this field blank")){
			return false;
		}
		if (jQuery("#submitterName").attr("value") == ""){
			jQuery("#submitterName").attr("value", jQuery("contactName").attr("value"));
		}
		if (jQuery("#submitterPhone").attr("value") == ""){
			jQuery("#submitterPhone").attr("value", jQuery("contactPhone").attr("value"));
		}
	}
	else {
		return false;
	}
	jQuery("#endDate").disabled(false);
}

function fillInSubmitter(fillFromField,fillToField) {
  if (jQuery(fillToField).attr("value") == "") {
    jQuery(fillToField).attr("value", jQuery(fillFromField).attr("value"));
  }
}
//this is an addition to the code, to make sure people don't enter html tags into the form.
function hasBrackets(theString){
	if (theString.indexOf('>') == -1 && theString.indexOf('<') == -1 )
		return false;
	else return true;
}
var endDateValue; //this is a global variable we will use to keep track of their entry into the endDate field if they change
					// mind and click on recurring it will become disabled and will have the same date as the start date,
					// then clicking not recurring will return the end date field to the value they had left in it.
function startDateChange(){
  if  (jQuery("#endDate").attr("disabled"))
    jQuery("#endDate").attr("value", jQuery("#startDate").attr("value"));
}


function yesRecurring () {
    jQuery("#recurringRow").show();
}
function noRecurring () {
    jQuery("#recurringRow").hide();
}

function load() {
	if (jQuery('#recurringyes').attr('checked')) {
		yesRecurring();
	}
}

// Automatically update the value of the specified text box with the selected time
// e.g. id = #[name]
//          refers to pull-down lists #[name]_Hour, #[name]_Minute, #[name]_AMPM
// JH (12/16/09)
function changeTime(id) {
    var h, m, d;
    h = $(id+"_Hour").val();
    m = $(id+"_Minute").val();
    d = $(id+"_AMPM").val();

    $(id).attr("value", h+":"+m+" "+d);
}


// When page is loaded completely, check if "recurring" checkbox is checked.
$( function() {
	$("#startDate, #endDate").datepicker();
    if ($("#recurringyes:checked").val()!= null) {
        yesRecurring();     // if checked, display recurring options
    }
    else
    {
        noRecurring();      // if not checked, hide recurring options
    }        
});
