$(document).ready(function(){
	
	$('input.loginname').focus(function(){
		$(this).select();
	});
	//submit base64 encoded data
	$('#loginForm input').each(function(){
		$(this).keyup(function(event) {
			if (event.which == 13){
				$('#loginForm').submit();
				$('#LogInHelp').addClass('hidden');
			}
		});
	});

	//Login form (appears when required)
	$('form#login1').submit(function(event) {
		event.preventDefault();
		//settings for login
		var password = $('input#password').val();
		if (password){
			$('#login1').addClass('hidden');
			$('#login2').removeClass('hidden');
			var base64password = Base64.encode(password);
			$('input#password').val('');
			$('input#base64password').val(base64password);
		}
		//Get all of the data from the input fields
		var getInputs = $(this).find('input').serialize() + "&Ajax=1";
		//perform ajax post to retrieve success / failure response
		$.ajax({
			type: "POST",
			url: scriptName + afterLogin,
			data: getInputs,
			dataType:"json",
			success: function(data){
				if(data.result===true){
					//re-direct browser to url retrieved from json data
					document.location=data.url;
				} else if (data.popup) {
					$('.login1').after('<div id="dialog"></div>');
					$('#dialog').html(data.popup).attr('title','Select a reader').dialog({
						width: 540,
						height: 320,
						buttons: {
							"Close": function() {
								$(this).dialog("close");
								$('input.loginname').focus();
							},
							"OK" : function() {
								$('.logincheckbox input').each(function(){
									if($(this).attr("checked") == true){
										var rdrid = $(this).attr("title");
										rdrid = rdrid.substring(1,rdrid.length);
										$('form#login1 .loginname').val(rdrid);
										$(this).dialog("close");
										$('input.loginname').focus();
										$('form#login1').submit();
									}
								});
							}
						}
					});
					$('.sellogin tr.logindata:odd').addClass('oddRow');
					$('.SingleSelectRow').parent().parent().each(function(){
						$(this).find('input[type="checkbox"]').click(function(event){
							var checkedStatus = $(this).attr('checked');
							if(checkedStatus){
								$(this).attr('checked',null);
							} else {
								$(this).attr('checked','checked');
							}
						});
						$(this).click(function(event){
							//event.preventDefault();
							var clickedId = $(this).find('input[type="checkbox"]').attr('title');
							var checkedStatus = $(this).find('input[type="checkbox"]').attr('checked');
							if(checkedStatus){
								$(this).find('input[type="checkbox"]').attr('checked',null);
							} else {
								$(this).find('input[type="checkbox"]').attr('checked','checked');
							}
							//uncheck all other checkboxes
							$('.SingleSelectRow').each(function(){
								if($(this).attr('title') != clickedId){
									$(this).attr('checked',null);
								}
							});
						});
					});
				} else {
					//if failed display error message
					Regexp2 = new RegExp("\[[A-za-z]{1,6}[0-9]{1,3}\]|\[[A-za-z]{1,6}[0-9]{1,3}-S\]");
					var regMatch = data.message.match(Regexp2);
					messageText = data.message.replace(Regexp2, '');
					//strip out ý from messageText
					Regexp3 = new RegExp("&#253;", "gim");
					var regMatch2 = messageText.match(Regexp3);
					messageText = messageText.replace(Regexp3, '');
					$('.login1').after('<div id="dialog"></div>');
					$('#login1').removeClass('hidden');
					$('#login2').addClass('hidden');
					$('#dialog').html(messageText).attr('title','Error encountered - '+ regMatch).dialog({
						buttons: {
							"Ok": function() {
								$(this).dialog("close");
								$('input.loginname').focus();
							}
						}
					});
					//reset focus to the username input
					$('input.loginname').focus();
				}
			}
		});
		return false;
	});
	
	//Side bar login
	$('form#SLogin1').submit(function(event) {
		event.preventDefault();
		//settings for login
		var password = $('input#password').val();
		if (password){
			var base64password = Base64.encode(password);
			$('input#password').val('');
			$('input#base64password').val(base64password);
		}
		
		$('#SLogin1').addClass('hidden');
		$('#login2').removeClass('hidden');
		//Get all of the data from the input fields
		var getInputs = $(this).find('input').serialize() + "&Ajax=1";
		
		//perform ajax post to retrieve success / failure response
		$.ajax({
			type: "POST",
			url: scriptName + "/login2",
			data: getInputs,
			dataType:"json",
			success: function(data){
				if(data.result===true){
					//$('#login2').html('Reader logged in successfully');
					//re-direct browser to url retrieved from json data
					document.location=location.href;
					
				} else if (data.popup) {
					$('.SLogin1').after('<div id="dialog"></div>');
					$('#dialog').html(data.popup).attr('title','Select a reader').dialog({
						width: 540,
						height: 320,
						buttons: {
							"Close": function() {
								$(this).dialog("close");
								$('input.loginname').focus();
							},
							"Ok" : function() {
								$('.logincheckbox input').each(function(){
									if($(this).attr("checked") == true){
										var rdrid = $(this).attr("title");
										rdrid = rdrid.substring(1,rdrid.length);
										
										$('form#SLogin1 .loginname').val(rdrid);
										$(this).dialog("close");
										$('input.loginname').focus();
										$('form#SLogin1').submit();
										$(this).dialog("close");
									}
								});
							}
						}
					});
					$('.sellogin tr.logindata:odd').addClass('oddRow');
					$('.SingleSelectRow').parent().parent().each(function(){
						$(this).find('input[type="checkbox"]').click(function(event){
							var checkedStatus = $(this).attr('checked');
							if(checkedStatus){
								$(this).attr('checked',null);
							} else {
								$(this).attr('checked','checked');
							}
						});
						$(this).click(function(event){
							//event.preventDefault();
							var clickedId = $(this).find('input[type="checkbox"]').attr('title');
							var checkedStatus = $(this).find('input[type="checkbox"]').attr('checked');
							if(checkedStatus){
								$(this).find('input[type="checkbox"]').attr('checked',null);
							} else {
								$(this).find('input[type="checkbox"]').attr('checked','checked');
							}
							//uncheck all other checkboxes
							$('.SingleSelectRow').each(function(){
								if($(this).attr('title') != clickedId){
									$(this).attr('checked',null);
								}
							});
						});
					});
				} else {
					//if failed display error message
					Regexp2 = new RegExp("\[[A-za-z]{1,6}[0-9]{1,3}\]|\[[A-za-z]{1,6}[0-9]{1,3}-S\]");
					var regMatch = data.message.match(Regexp2);
					messageText = data.message.replace(Regexp2, '');
					//strip out ý from messageText
					Regexp3 = new RegExp("&#253;", "gim");
					var regMatch2 = messageText.match(Regexp3);
					messageText = messageText.replace(Regexp3, '');
					$('.SLogin1').after('<div id="dialog"></div>');
					$('#SLogin1').removeClass('hidden');
					$('#login2').addClass('hidden');
					$('#dialog').html(messageText).attr('title','Error encountered - '+ regMatch).dialog({
						buttons: {
							"Ok": function() {
								$(this).dialog("close");
								$('input.loginname').focus();
							}
						}
					});
					//reset focus to the username input
					$('input.loginname').focus();
				}
			}
		});
		return false;
	});
});