var errors = new Array();
var errorIds = new Array();

function resetArrays() {
	errors = new Array();
	errorIds = new Array();
}

function addErrorId(id) {
	if ($.inArray(id, errorIds) == -1) {
		errorIds.push(id);
	}
}

function validate_future_date(month_id, year_id) {
	var month = jQuery("#" + month_id).val();
	var year = jQuery("#" + year_id).val();
	var ccDate = new Date();
	//credit cards are good until the end of the current month
	ccDate.setFullYear(year,month,1);
	var today = new Date();

	if (ccDate<today)
	{
	  errors.push("Please choose a date in the future.");
		addErrorId(month_id);
		addErrorId(year_id);
		return false;
	 } else {
		return true;
	}
	
}
//only works for email address for 
function validate_uniqueness_of(table, column, field) {
	var value = $("#" + field).val();
	if (value.length > 0) {
		var path = "/";
		if (table == "conf_registrations") {
			path = path + "conference";
		} else if (table == "claims_registrations") {
			path = path + "claims_school";
		}
		path = path + "/registrations/";
		var error_state = false;

		$.ajax({
	  	url: path + column + ".json?in_use=" + value,
	  	async: false,
	  	cache: false,
	  	dataType: "json",
	  	success: function(html){
	    	error_state = html
	  	}
	 	});
		if (error_state) {
			errors.push("The email address is already in use.");
			addErrorId(field);
			return false;
		} else {
			return true;
		}
	}
}

function validate_presence_of(field, message) {
	var value = $("#" + field).val();
	
	if (value == undefined) {
		errors.push(message);
		addErrorId(field);
		return false;
	} else {
		if (value.trim().length > 0) {
			return true;
		} else {
			errors.push(message);
			addErrorId(field);
			return false;
		}
	} 
}

function validate_length_of(field, min, max, fieldName) {
	var value = $("#" + field).val();
	if (value == undefined) {
		errors.push(fieldName + " must be greater than " + min.toString() + " characters");
		addErrorId(field);
		return false;
	}
	if (max < 0) { 
		if (value.length >= min) {
			return true;
		} else {
			errors.push(fieldName + " must be greater than " + min.toString() + " characters");
			addErrorId(field);
			return false;
		}
	} else {
		if ((value.length >= min) && (value.length <= max)) {
			return true;
		} else {
			errors.push(fieldName + " must be between " + min.toString() + " and " + max.toString() + " characters");
			addErrorId(field);
			return false;
		}
	}
}

function validate_format_of(field, regex, fieldName) {
	var value = $("#" + field).val();

	if (value == undefined) {
		errors.push(fieldName + " is not in the correct format.");
		addErrorId(field);
		return false;
	}
	var email_regex = /^\w(?:\w|-|\.(?!\.|@))*@\w(?:\w|-|\.(?!\.))*\.\w{2,3}/ 
		// index of -1 means "not found"
	if (regex.test(value)) {
		  return true;
	} else {
			errors.push(fieldName + " is not in the correct format.");
			addErrorId(field);
			return false;
	}	
}

function resetErrors() {
	$(":input").removeClass('error_signup').addClass('signup');
}

function showErrors() {
	resetErrors();
	if (errorIds.length > 0) {
		$.each(errorIds, function(key,value) {
			$('#' + value).removeClass('signup').addClass('error_signup');
		});
		
		var errorConcat = "";
		$.each(errors, function(key,value) {
			errorConcat = errorConcat + value + "<br />";
		});
		$("#error_spans").html("");
		$("#error_spans").html(errorConcat);
		$("#js_error").show();
		
		$("#" + errorIds[0]).focus();
		return false;
	}
	return true;
}