// JavaScript Document

$(function(){
	var allFields = $([]).add($("#nameIn")).add( $("#newUserNameIn") ).add( $("#userPassIn") ).add( $("#userConfPassIn") ).add( $("#userEmailIn") );

/***************************
*
*		Configure the form UI Elements, and helping functions
*			-Help Buttons
*			-Confirmation Dialog
*			-Info Accordian
*			-Clear Form Button
*			-Join Form Button
*
****************************/


//First Help Button
	$("#helpButt-00").click(function() {
				$("#infoAccord").accordion('activate', 2);
				$("#content-03").animate({backgroundColor: '#cee6f3', color: '#063850'}, 500);
				$("#content-03").animate({backgroundColor: '#fff', color: '#000'}, 700);
				return false;
		})
		.hover(
			function(){ 
				$(this).addClass("ui-state-hover"); 
			},
			function(){ 
				$(this).removeClass("ui-state-hover"); 
			}
			).mousedown(function(){
				$(this).addClass("ui-state-active"); 
			})
			.mouseup(function(){
				$(this).removeClass("ui-state-active");
			});
	
//Reset Form Button
	$("#resetForm").click(function() {
			//$("#joinDial").dialog('open');
			resetForm();
			return false;
		})
		.hover(
			function(){ 
				$(this).addClass("ui-state-hover"); 
			},
			function(){ 
				$(this).removeClass("ui-state-hover"); 
			}
		).mousedown(function(){
			$(this).addClass("ui-state-active"); 
		})
		.mouseup(function(){
				$(this).removeClass("ui-state-active");
		});
	
	
	function resetForm(){
		allFields.removeClass('ui-state-error');
		$("#joinTips").removeClass('tipError');
		$("#joinTips").text( "**All fields are required" );
		
	}

		/*****
		
				Process Join Form, and prompt for Error, or Confirmation
		
								****/
								
	$("#joinButt").click(function() {
			allFields.removeClass('ui-state-error');
			$("#joinTips").removeClass("tipError");
			
			var bVal = checkLength( $("#firstNameIn"), 3, 16, $("#joinTips"), "First Name must be between 3 and 16 characters.");
			if(bVal == true){ bVal = checkRegExp( $("#firstNameIn") ,/^([a-zA-Z])+$/i, $("#joinTips"), "First Name Field can only accept a-z or A-Z."); }
			
			if(bVal == true){ bVal = checkLength( $("#midInitIn"), 1, 1, $("#joinTips"), "Middle Initials are always one letter."); }
			if(bVal == true){ bVal = checkRegExp( $("#midInitIn") ,/^([A-Z])+$/i, $("#joinTips"), "Please only capitol letters in for your middle initial."); }
			
			if(bVal == true){ bVal = checkLength( $("#lastNameIn"), 2, 20, $("#joinTips"), "Last Name must be between 2 and 20 characters."); }
			if(bVal == true){ bVal = checkRegExp( $("#lastNameIn") ,/^([a-zA-Z])+$/i, $("#joinTips"), "First Name Field can only accept a-z or A-Z."); }
			
			if(bVal == true){ bVal = checkLength( $("#newUserNameIn"), 8, 16, $("#joinTips"), "User Name must be between 8 and 16 characters."); }
			if(bVal == true){ bVal = checkRegExp( $("#newUserNameIn"),/^[a-z]([0-9a-z_])+$/i, $("#joinTips"), "User Name may consist of a-z, 0-9, underscores, begin with a letter."); }
			
			if(bVal == true){ bVal = checkLength( $("#userPassIn"), 8, 16, $("#joinTips"), "Password must be between 8 and 16 characters."); }
			if(bVal == true){ bVal = checkRegExp( $("#userPassIn"),/^([0-9a-zA-Z])+$/, $("#joinTips"), "Password field only allows : a-z 0-9"); }
			
			if(bVal == true){ bVal = checkPasswords($("#userPassIn"), $("#userConfPassIn")); }
			
			if(bVal == true){ bVal = checkRegExp($("#userEmailIn"),/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, $("#joinTips"), "Emails must be in the form of Example@SamsWare.com"); }
			
			if(bVal == true){	checkUserNameTaken(); }
			
			return false;
		})
	.hover(
		function(){ 
			$(this).addClass("ui-state-hover"); 
		},
		function(){ 
			$(this).removeClass("ui-state-hover"); 
		}
	).mousedown(function(){
		$(this).addClass("ui-state-active"); 
	})
	.mouseup(function(){
			$(this).removeClass("ui-state-active");
	});
	
	function checkUserNameTaken(){
	
		jQuery.post('../../lib/joinForm/checkFreeUserName.php', {'userName': $("#newUserNameIn").val() }, function(data) {
				var response = parseInt(data);
				
				switch(response){
					case 0:{
						presentForm();
					}break;
					case 1:{
						$("#newUserNameIn").addClass( "ui-state-error" );
						$("#joinTips").text(  "User Name " + $("#newUserNameIn").val() + " is already taken.  Please try again" ).effect("highlight",{},1500);
					}break;
					case 2:{
						updateJoinTips( "SQL Query Exception Occured.  Please try again.  If the problem persists, please contact SamsWare Admin." );
					}
				}
			});
		}

	function checkPasswords(pass, confPass){
		if(pass.val() != confPass.val()){
			pass.addClass("ui-state-error");
			confPass.addClass("ui-state-error");
			$("#joinTips").text( "Password does not match confirmation" ).effect("highlight",{},1500);
			return false;
		}else{
			return true;
		}
	}

// Information Accordian
	$("#infoAccord").accordion({
			icons: {
				header: "ui-icon-circle-arrow-e",
				headerSelected: "ui-icon-circle-arrow-s"
			}
		});

//Confirmation Dialog
	$("#joinDial").dialog({
		bgiframe: true,
		modal: true,
		autoOpen: false,

		buttons: {
			Confirm: function() {
				$(this).dialog('close');
				$("#joinForm").submit();
			}, 
			Cancel: function() {
				$(this).dialog('close');
			}
		}
	});
	
	function presentForm(){
		$("#joinDialList").empty();
		
		var nameString = "Name: " + $("#firstNameIn").val() + " " + $("#midInitIn").val() + ". " + $("#lastNameIn").val();
		var middleInitString = "Middle Initial: " + $("#midInitIn").val();
		var lastNameString = "Last Name: " + $("#lastNameIn").val();
		var userNameString = "User Name " + $("#newUserNameIn").val(); 
		var passwordString = "Password: " + $("#userPassIn").val(); 
		var emailString = "Email: " + $("#userEmailIn").val(); 
		
		$("#joinDialList").append( "<li>" + nameString + "</li>" );
		
		$("#joinDialList").append( "<li>" + userNameString + "</li>" );
		$("#joinDialList").append( "<li>" + passwordString + "</li>" );
		$("#joinDialList").append( "<li>" + emailString + "</li>" );
		
		$("#joinDial").dialog('open');
	}


});