$(document).ready(function(){
	// team edit goal form validation
	if ($('#submit-btn-editgoal').length > 0) {
		$("#submit-btn-editgoal").click(function(){
			if (isValid) {
			  var userCreatedDate = $('#targetMonth').val()+'/'+$('#targetDay').val()+'/'+$('#targetYear').val();
			  if(!isDate(userCreatedDate)) {
			    $("#targetDateErrorMsg").addClass('error');
					$("#targetDateErrorMsg").html('Please enter a valid date.');
					return false;
			  } else {
			    $("#userCreatedDate").val(userCreatedDate);
  				var today = new Date();
  				var computedDate = new Date(userCreatedDate);
  				var result = today <= computedDate;
  				if (!result) {
  					$("#targetDateErrorMsg").addClass('error');
  					$("#targetDateErrorMsg").html('Please choose a date greater than today.');
  					return false;
  				}else {
  					$("#editTeamGoal").submit();
  				}
			  }
				
				
			}
		});
		
		var isValid = $("#editTeamGoal").validate({
			rules: {
				fundraisingGoalAmount: {required:true, digits: true, min: 1, max:999999999},
				targetMonth: 'required',
				targetDay: 'required',
				targetYear: 'required'
			},
			messages: {
				fundraisingGoalAmount: {required: 'Please enter a dollar goal for this team.',min:'Please enter between 1 and 999,999,999.',max:'Please enter between 1 and 999,999,999.'},
				targetMonth: 'A full date is required.',
				targetDay: 'A full date is required.',
				targetYear: 'A full date is required.'
			},
			groups: {
				fullDate: "targetMonth targetDay targetYear"
			},
			errorPlacement: function(error, element){
				if (element.attr("name") == "targetMonth" || element.attr("name") == "targetDay" || element.attr("name") == "targetYear") {
					error.appendTo("#targetDateErrorMsg");
				}
				if (element.attr("name") == "fundraisingGoalAmount") {
					error.appendTo("#fundraisingGoalAmountErrorMsg");
				}
			}
		});
	}
	
	// team write note form validation
	if ($('#submit-btn-postNotes').length > 0) {
		$("#submit-btn-postNotes").click(function(){
			if (isValid) {
				$("#writeNoteForm").submit();
			}
		});
		var isValid = $("#writeNoteForm").validate({
			rules: {
				noteTitle: 'required',
				personalNote: 'required'
			},
			messages: {
				noteTitle: 'Please enter a title for this note.',
				personalNote: 'Please enter the note to the team.'
			},
			errorPlacement: function(error, element){
				if (element.attr("name") == "noteTitle") {
					error.appendTo("#noteTitleErrorMsg");
				}
				if (element.attr("name") == "personalNote") {
					error.appendTo("#personalNoteErrorMsg");
				}
			}
		});
		// character counting and limiting on adding a message
		$("#personalNote").charCounter(750, {container: "#characterCounter"});
	}
	
	
	// team photo change form validation
	if ($('#changeTeamPhotoSubmit').length > 0) {
		$("#changeTeamPhotoSubmit").click(function(){
			if (isValid) {
				$("#teamChangePhoto").submit();
			}
		});
		
		var isValid = $("#teamChangePhoto").validate({
			rules: {
				photo: { accept: "jpg|jpeg|gif|bmp|tif|tiff|png",
						required:true
				},
				photoConfirm: {required: function(element){
								return $("#photo").val() != ''}
				}
			},	
			messages: {
				photo: 'Please select a photo (gif/jpg/jpeg/png/tif/tiff/bmp) to upload.',
				photoConfirm: 'Please confirm that you have permission to upload this photo.'
			},
			errorPlacement: function(error, element){
				if (element.attr("name") == "photo") {
					error.appendTo("#photoError");
				}
				if (element.attr("name") == "photoConfirm") {
					error.appendTo("#photoConfirmError");
				}
			}
		});
	}
	
	jQuery.fn.preventDoubleSubmit = function() {
	  jQuery(this).submit(function() {
	    if (this.beenSubmitted)
	      return false;
	    else
	      this.beenSubmitted = true;
	  });
	};

	
	// team write comment form validation
	if ($('form#writeComment').length > 0) {
		$("#writeComment").validate({
			rules: {
				commentInput: 'required'
			},	
			messages: {
				commentInput: 'Please enter your comment.'
			},
			errorPlacement: function(error, element){
				if (element.attr("name") == "commentInput") {
					error.appendTo("#commentInputErrorMsg");
				}
			}
		});
		// character counting and limiting on adding a message
		$("#commentInput").charCounter(750, {container: "#characterCounter"});
	}
	
	
	// team thank you note validation
	if ($('#submit-btn-editdonationsthankyou').length > 0) {
		// character counting and limiting on adding a message
		$("#thankyoumessage").charCounter(750, {container: "#characterCounter"});
	}
	
	// team edit mission validation
	if ($('#submit-btn-editmission').length > 0) {
		// character counting and limiting on adding a message
		$("#teamMission").charCounter(750, {container: "#characterCounter"});
	}
	
	// modal start a team mission statement validation
	if ($('#startTeamSubmit').length > 0) {
		// character counting and limiting on adding a message
		$("#missionStatement").charCounter(750, {container: "#characterCounter"});
	}
	
	//question mark tooltip
	//execute only on modal page
	if ($('#tooltip1').length > 0) {
		$('#tooltip1').qtip({
			api: {
				onFocus: function(){
					$(".qtip-active").css("z-index", 10000);
				}
   			},
			position: {
				corner: {
					target: 'rightTop',
					tooltip: 'bottomLeft'
				}
			},
			show: {
				effect: {
					type: 'grow',
					length: 400
				}
			},
			hide: {
				effect: {
					type: 'grow',
					length: 400
				}
			},
			style: {
				name: 'light',
				tip: true,
				border: {
					radius: 8,
					color: '#999'
				}
			},
			content: "Public teams appear in the search results and anyone can join the team.  The Private team is by invite only and will not appear in the search results."
		});
		$('#tooltip2').qtip({
			api: {
				onFocus: function(){
					$(".qtip-active").css("z-index", 10000);
				}
   			},
			position: {
				corner: {
					target: 'rightTop',
					tooltip: 'bottomLeft'
				}
			},
			show: {
				effect: {
					type: 'grow',
					length: 400
				}
			},
			hide: {
				effect: {
					type: 'grow',
					length: 400
				}
			},
			style: {
				name: 'light',
				tip: true,
				border: {
					radius: 8,
					color: '#999'
				}
			},
			content: "Photos will be resized to a square format. Please upload an image that works in that format and does not exceed 1MB in size. Photo uploader supports GIF, PNG, and JPG."
		});
		$('#tooltip3').qtip({
			api: {
				onFocus: function(){
					$(".qtip-active").css("z-index", 10000);
				}
   			},
			position: {
				corner: {
					target: 'rightTop',
					tooltip: 'bottomLeft'
				}
			},
			show: {
				effect: {
					type: 'grow',
					length: 400
				}
			},
			hide: {
				effect: {
					type: 'grow',
					length: 400
				}
			},
			style: {
				name: 'light',
				tip: true,
				border: {
					radius: 8,
					color: '#999'
				}
			},
			content: "Reasons why you are starting this team or what you want to accomplish, work great as mission statements. Example: Cancer is a terrible thing and we can no longer stand by and do nothing. Join this team and help put an end to cancer."
		});
	}
	
	$("#button_sendteamemail").colorbox({
		scrolling:"false", innerHeight: 490, innerWidth:720 
	});
	$(".inviteFriends").colorbox({
		scrolling:"false", innerHeight: 620, innerWidth:740 
	});
	$(".addMessage").colorbox({
		scrolling:"false", innerHeight: 400, innerWidth:720 
	});
	$(".addComment").colorbox({
		scrolling:"false", innerHeight: 325, innerWidth:720 
	});
	$("#editPhoto").colorbox({
		scrolling:"false", innerHeight: 370, innerWidth:720 
	});
	$("#button_editgoal").colorbox({
		scrolling:"false", innerHeight: 370, innerWidth:720 
	});
	
	$("#button_writefirstnote").colorbox({
		scrolling:"false", innerHeight: 400, innerWidth:720 
	});

	$("#manageTeam").colorbox({
		scrolling:"false", innerHeight: 420, innerWidth:720 
	});
	$("#editMission").colorbox({
		scrolling:"false", innerHeight: 350, innerWidth:720 
	});
	$("#editThankYou").colorbox({
		scrolling:"false", innerHeight: 350, innerWidth:720 
	});
	$("#viewInvites").colorbox({
		scrolling:"false", innerHeight: 470, innerWidth:720 
	});
	
	$("#viewInvites").colorbox({
		scrolling:"false", innerHeight: 470, innerWidth:720 
	});
	
	$('#cancel').click(function(){$.fn.colorbox.close();});
	
	
	$(".closeConfirmBox").click(function(){
		$(this).parent().fadeOut("slow");
	});	

});	
