        $(document).ready(function(){
		//First, make sure there aren't any leftover error messages or checked boxes
		    fixMailbox()
			
		//tooltipster stuff
		$('.acctname-help').tooltipster({
			contentAsHTML: true,
			maxWidth:400,
			functionInit: function(){
				return $('#acctname-description').html();
			},
			functionReady: function(){
				$('#acctname-description').attr('aria-hidden', false);
			},
			functionAfter: function(){
				$('#acctname-description').attr('aria-hidden', true);
			}
		});	
		$('.description-help').tooltipster({
			contentAsHTML: true,
			maxWidth:400,
			functionInit: function(){
				return $('#description-description').html();
			},
			functionReady: function(){
				$('#description-description').attr('aria-hidden', false);
			},
			functionAfter: function(){
				$('#description-description').attr('aria-hidden', true);
			}
		});				
		$('.owners-help').tooltipster({
			contentAsHTML: true,
			maxWidth:400,
			functionInit: function(){
				return $('#owners-description').html();
			},
			functionReady: function(){
				$('#owners-description').attr('aria-hidden', false);
			},
			functionAfter: function(){
				$('#owners-description').attr('aria-hidden', true);
			}
		});	
		$('.orgid-help').tooltipster({
			contentAsHTML: true,
			maxWidth:400,
			functionInit: function(){
				return $('#orgid-description').html();
			},
			functionReady: function(){
				$('#orgid-description').attr('aria-hidden', false);
			},
			functionAfter: function(){
				$('#orgid-description').attr('aria-hidden', true);
			}
		});	
		$('.sponsor-help').tooltipster({
			contentAsHTML: true,
			maxWidth:400,
			functionInit: function(){
				return $('#sponsor-description').html();
			},
			functionReady: function(){
				$('#sponsor-description').attr('aria-hidden', false);
			},
			functionAfter: function(){
				$('#sponsor-description').attr('aria-hidden', true);
			}
		});	
		$('.mailbox-help').tooltipster({
			contentAsHTML: true,
			maxWidth:400,
			functionInit: function(){
				return $('#mailbox-description').html();
			},
			functionReady: function(){
				$('#mailbox-description').attr('aria-hidden', false);
			},
			functionAfter: function(){
				$('#mailbox-description').attr('aria-hidden', true);
			}
		});	
		$('.forwardaddr1-help').tooltipster({
			contentAsHTML: true,
			maxWidth:400,
			functionInit: function(){
				return $('#forwardaddr1-description').html();
			},
			functionReady: function(){
				$('#forwardaddr1-description').attr('aria-hidden', false);
			},
			functionAfter: function(){
				$('#forwardaddr1-description').attr('aria-hidden', true);
			}
		});
		$('.forwardaddr2-help').tooltipster({
			contentAsHTML: true,
			maxWidth:400,
			functionInit: function(){
				return $('#forwardaddr2-description').html();
			},
			functionReady: function(){
				$('#forwardaddr2-description').attr('aria-hidden', false);
			},
			functionAfter: function(){
				$('#forwardaddr2-description').attr('aria-hidden', true);
			}
		});
		$('.forwardaddr3-help').tooltipster({
			maxWidth:400,
			contentAsHTML: true,
			functionInit: function(){
				return $('#forwardaddr3-description').html();
			},
			functionReady: function(){
				$('#forwardaddr3-description').attr('aria-hidden', false);
			},
			functionAfter: function(){
				$('#forwardaddr3-description').attr('aria-hidden', true);
			}
		});
		$('.addlinfo-help').tooltipster({
			contentAsHTML: true,
			maxWidth:400,
			functionInit: function(){
				return $('#addlinfo-description').html();
			},
			functionReady: function(){
				$('#addlinfo-description').attr('aria-hidden', false);
			},
			functionAfter: function(){
				$('#addlinfo-description').attr('aria-hidden', true);
			}
		});	
									$('.mailbox-prefill-help').tooltipster({
								contentAsHTML: true,
								maxWidth:400,
								functionInit: function(){
									return $('#mailbox-prefill-description').html();
								},
								functionReady: function(){
									$('#mailbox-prefill').attr('aria-hidden', false);
								},
								functionAfter: function(){
									$('#mailbox-prefill').attr('aria-hidden', true);
								}
							});	
		 //END OF TOOLTIPSTER

      
         	$("#acctname").change(function () {
         		checkName()
         		if(($("#mailbox").is(":checked")) || ($("span.mailbox-errors").hasClass( "error-span" ))){			
         			mailboxAddress();
         		}
         	});
         
                $('#mailbox').click(function(){
                   fixMailbox()
                });
				
				
			function fixMailbox(){
				
				if($('#mailbox').is(":checked")){			
         			mailboxAddress();
                    }
                    else if($('#mailbox').is(":not(:checked)")){
                        $(".forwardaddr1 td.text-label .field-label").html("Forwarding Address #1");
                        $(".forwardaddr1 td input").removeAttr('readonly').val("");
						$(".forwardaddr1-help").show();
 						$(".mailbox-prefill-help").hide(); 					
                    }
				
			}	
         	function mailboxAddress() {
         		var acctname = $('input#acctname').val();
         			if (!acctname || (acctname.length < 9) || ($("#acctname-errors").hasClass( "error-span" )) ){
         				$("span.mailbox-errors").html("Please enter a valid Shared Mailbox Name first").addClass("error-span");
         				$(".acctname").addClass("alert");
         				$(".mailbox").addClass("alert");
         				$('#mailbox').prop('checked', false);
         			}
         			else {
         				var mailbox = acctname + "@o365.stanford.edu";
         				$(".forwardaddr1 td.text-label  .field-label").html("Mailbox Address");
						$(".mailbox-prefill-help").insertAfter( ".forwardaddr1-help" ).show();
						$(".forwardaddr1-help").hide();
         				$(".forwardaddr1 td input").attr('readonly','readonly').val(mailbox);
						$('#mailbox').prop('checked', true);
         				$(".acctname").removeClass("alert");	
         				$("span.mailbox-errors").html("").removeClass("error-span");				
         			}
         	}
         	
         	function checkName() {
         	     var str = $("#acctname").val();
         		 var acctname = str.replace(/\s+/g, '').toLowerCase();
         		 $("#acctname").val(acctname);
         		 if (acctname.length < 9) {
         			$(".acctname").addClass("alert");
         			$(".acctname-errors").html("Name must be at least 9 characters long").addClass("error-span");
         		 } else if (acctname.length >= 9) {
         			$(".acctname").removeClass("alert");
         			$(".acctname-errors").html("").removeClass("error-span");
         			 var regexp = /^[a-zA-Z0-9-_]+$/;
         			 if (acctname.search(regexp) == -1) {
         				$(".acctname").addClass("alert");
         				$(".acctname-errors").html("Name can only contain numbers, letters, underscores and hyphens").addClass("error-span");
         			 }
         
         		 }
         
         
         	}

         	
            });