$(function() {
	var loginAttempts = 0
	$('#btnLoginProcess').button().click(function(e){
	
		
		// validate
		
		if ($('#txtUsername').val == '' || $('#txtPassword').val() == '') {
			alert("Username and password are both required to login.")
			$('#txtUsername').focus()
			
			
			return false;	
		}
		
	
			//ajax call to process
			$.ajax({
				cache:false,
				data: {username: $('#txtUsername').val(),password: $('#txtPassword').val()},
				type: "POST",
				dataType: "html" ,
				url: "/includes/AJAX_VerifyLogin.asp",
				error: function() {
					$('#divLoginArea').hide() //hide the verifying text on error
					},
				success: function(returnData, returnMessage){

					//hide the loading
					$('#divLoginArea').hide()
					$('#divLoginStatus').hide()
					$('#divLoginGood').hide()
					
					switch (returnData) {
					
					case "bad":	
						loginAttempts += 1
						$('#divLoginStatus').show()
						$('#txtPassword').val('')
						$('#txtPassword').focus()
						
						if (loginAttempts == 3) {
							checkForgot = confirm("It appears you are having trouble logging in. Click \'OK\' to be directed to the forgot your password page. Otherwise click \'Cancel\' and you can try again.")
							if (checkForgot == true) {
								window.location = '/forgot_pass.asp?username='	 + $('#txtUsername').val()	
							}
						}
						
						if (loginAttempts == 6) {
							
							window.location = '/forgot_pass.asp?username='	 + $('#txtUsername').val()
							return false;
						}
						
						
						
						break;
					
					case "good":
						//success! - clear the login box and then close it and then open a browser window
						$('#divLoginGood').show()
						//$('#txtUsername').val('')
						//$('#txtPassword').val('')
						
						window.location  = '/includes/recordsession.asp'
						
						//close the dialog
						//$( "#divLoginForm" ).dialog('close')
						
						//url = "/includes/recordsession.asp"
						//windowName = "studentContainer"
						//windowOptions = "status=1,toolbar=1,location=1, menubar=1, resizable=1, scrollbars=1, directories=1"
	
						
	
						//var nw = window.open(url, windowName,windowOptions);
						//if (window.focus) {nw.focus()}
						
						break;
					}
	
				},
				statusCode: {
					404: function() { alert("The Page was not found. Please trying again in a few moments.")},		
					500: function() {alert("There was an error processing your request. Please try again in a few moments.")}
				},
				beforeSend: function() {
					$('#divLoginArea').show() // show the verifying text
					}
				
			})
		})
		
		$( "#divLoginForm" ).dialog({
			show: 'fade',
			hide: 'fade',
			autoOpen: false,
			resizable: false,
			modal: true,
			closeOnEscape: true,
			open: function() {
				$('#txtUsername').val('');
				$('#txtPassword').val('');
					//reset 
				$('#divLoginStatus').hide()
				$('#divLoginArea').hide()
				},
			draggable: false,
			width: 360
		});

		$( "#aShowLoginForm, #btnLogBackIn" ).click(function() {
			$( "#divLoginForm" ).dialog( "open" );
			$('#divLoginArea').hide()
					$('#divLoginStatus').hide()
					$('#divLoginGood').hide()
			return false;
		});
		$('#txtUsername, #txtPassword').bind('keypress', function(event) {
			
			if ($( "#divLoginForm" ).dialog( "isOpen" ) == true) {
				event.which = event.charCode || event.keyCode;
				if (event.which == 13) {
					$('#btnLoginProcess').trigger("click")
				}
			}
			
			});

		

	});
