$(document).ready(function()
{
$('#username').keyup(function(){$('#result').html(passwordStrength($('#password').val(),$('#username').val()))})
		$('#password').keyup(function(){$('#result').html(passwordStrength($('#password').val(),$('#username').val()))})
	$("#username").blur(function()
	{
		var uname = document.signup.username.value;
		$("#msgboxusername").removeClass().addClass('signmessagebox').text('Checking...').fadeIn("slow");
		$.post("user_avail.php",{ username:uname } ,function(data)
        {
		  if(data == 'no')
		  {
		  	$("#msgboxusername").fadeTo(200,0.1,function()
			{ 
			  $(this).html('This User name Already exists').addClass('signmessageboxerror').fadeTo(900,1);
			});		
          }
		  else if(data == 'empty')
		  {
		  	$("#msgboxusername").fadeTo(200,0.1,function()
			{ 
			  $(this).html('Please type a username').addClass('signmessageboxerror').fadeTo(900,1);
			  return false;
			});		
          }
		  else
		  {
		  	$("#msgboxusername").fadeTo(200,0.1,function()
			{ 
			  $(this).html('Username available to register').addClass('signmessageboxok').fadeTo(900,1);	
			});
		  }
        });		
	});
$("#rpassword").blur(function()
	{
var password = document.signup.password.value;
var rpassword = document.signup.rpassword.value;
		$("#msgboxpassword").removeClass().addClass('signmessagebox').text('Checking...').fadeIn("slow");
		  if(!rpassword)
		  {
		  	$("#msgboxpassword").fadeTo(200,0.1,function()
			{ 
			  $(this).html('Repeat your password, this is to ensure you typed correctly').addClass('signmessageboxerror').fadeTo(900,1);
			});		
          }
		  else if(rpassword != password)
		  {
		  	$("#msgboxpassword").fadeTo(200,0.1,function()
			{ 
			  $(this).html('Password does not match').addClass('signmessageboxerror').fadeTo(900,1);
			  return false;
			});		
          }
		  else
		  {
		  	$("#msgboxpassword").fadeTo(200,0.1,function()
			{ 
			  $(this).html('Password matches').addClass('signmessageboxok').fadeTo(900,1);	
			});
		  }
        });
$("#email").blur(function()
	{
		$("#msgboxemail").removeClass().addClass('signmessagebox').text('Checking...').fadeIn("slow");
		$.post("email_check.php",{ email:$(this).val() } ,function(data)
        {
		  if(data=='no')
		  {
		  	$("#msgboxemail").fadeTo(200,0.1,function()
			{ 
			  $(this).html('An user with this email allready exists').addClass('signmessageboxerror').fadeTo(900,1);
			});		
          }
		  else if(data=='empty')
		  {
		  	$("#msgboxemail").fadeTo(200,0.1,function()
			{ 
			  $(this).html('Your email address, activation code will be sent here. We do not endorce spamming and will not give/sell your email to anyone').addClass('signmessageboxerror').fadeTo(900,1);
			  return false;
			});		
          }
		  else if(data=='invalid')
		  {
		  	$("#msgboxemail").fadeTo(200,0.1,function()
			{ 
			  $(this).html('Invalid email address, format is user@host.tld').addClass('signmessageboxerror').fadeTo(900,1);
			  return false;
			});		
          }
		  else
		  {
		  	$("#msgboxemail").fadeTo(200,0.1,function()
			{ 
			  $(this).html('Email is valid').addClass('signmessageboxok').fadeTo(900,1);	
			});
		  }
        });
	});
$("#remail").blur(function()
	{
var email = document.signup.email.value;
var remail = document.signup.remail.value;
		$("#msgboxremail").removeClass().addClass('signmessagebox').text('Checking...').fadeIn("slow");
		  if(!remail)
		  {
		  	$("#msgboxremail").fadeTo(200,0.1,function()
			{ 
			  $(this).html('Repeat your email, this is to ensure you have typed correctly').addClass('signmessageboxerror').fadeTo(900,1);
			});		
          }
		  else if(remail != email)
		  {
		  	$("#msgboxremail").fadeTo(200,0.1,function()
			{ 
			  $(this).html('Email does not match').addClass('signmessageboxerror').fadeTo(900,1);
			  return false;
			});		
          }
		  else
		  {
		  	$("#msgboxremail").fadeTo(200,0.1,function()
			{ 
			  $(this).html('Email matches, ok').addClass('signmessageboxok').fadeTo(900,1);	
			});
		  }
        });
$("#accept").click(function()
	{
var accept = document.signup.accept.value;
		$("#msgboxaccept").removeClass().addClass('signmessagebox').text('Checking...').fadeIn("slow");
		  if (accept != 'on')
		  {
		  	$("#msgboxaccept").fadeTo(200,0.1,function()
			{ 
			  $(this).html('You must accept').addClass('signmessageboxerror').fadeTo(900,1);
			});		
          }
		  else
		  {
		  	$("#msgboxaccept").fadeTo(200,0.1,function()
			{ 
			  $(this).html('ok').addClass('signmessageboxok').fadeTo(900,1);	
			});
		  }
        });
function users()
{
 var username = document.getElementById('msgboxusername').innerHTML;
if(username != 'Username available to register') { return false; }
else
{
  return true;
}
}
function password()
{
  var password = document.getElementById('result').innerHTML;
  if(password == 'Password strong enough') { return true; }
  else if(password == 'Strong password, Great!') { return true; }
  else { return false; }
}
function rpassword()
{
  var rpassword = document.getElementById('msgboxpassword').innerHTML;
  if (rpassword == 'Password matches') { return true; }
  else { return false; }
}
function email()
{
  var email = document.getElementById('msgboxemail').innerHTML;
  if (email == 'Email is valid') { return true; }
  else { return false; }
}
function remail()
{
  var remail = document.getElementById('msgboxremail').innerHTML;
  if (remail == 'Email matches, ok') { return true; }
  else { return false; }
}
function accept()
{
  var accept = document.signup.accept.value;
  if (accept == 'on') { return true; }
  else { return false; }
}
function checkform()
{
if(users() && password() && rpassword() && email() && remail() && accept())
  {
	alert("true");
  }
  else
  {
	alert("false");
  }
}
$("#lsignup").click(function()
	{
var ausername = document.signup.username.value;
var apassword = document.signup.password.value;
var aemail = document.signup.email.value;;
  if(users() && password() && rpassword() && email() && remail() && accept())
  {
		$.post("sup.php",{ 'username': ausername, 'password': apassword, 'email': aemail } ,function(data)
        {
		  if(data=='created')
		  {
			  document.getElementById('message').innerHTML = 'Successfull... please wait';
			  setTimeout("document.getElementById('page').innerHTML = 'User created, you must first use the confirmation link in your email before you can use your account'",1250);
		  }	
		  if(data=='error')
		  {
			  document.getElementById('message').innerHTML = 'Error';
		  }		
          });
  }
  else
  {
	document.getElementById('message').innerHTML = 'Please fill in ALL fields, and check above for errors';
  }
});
$("#llogin").click(function()
{
  var lusername = document.login.lusername.value;
  var lpassword = document.login.lpassword.value;
  		$("#loginstatus").removeClass().addClass('signmessagebox').text('Checking...').fadeIn("slow");
		$.post("login.php",{ username:lusername, password:lpassword } ,function(data)
        {
		  if(data=='yes')
		  {
		  	$("#loginstatus").fadeTo(200,0.1,function()
			{ 
			  $(this).html('Logging in... Please wait').addClass('signmessageboxok').fadeTo(900,1);
			});		
			  setTimeout("document.getElementById('page').innerHTML = 'You are now logged in.. Please wait'",750);
			  setTimeout("javascript:history.go(-0)",1750);

          }
		  else
		  {
		  	$("#loginstatus").fadeTo(200,0.1,function()
			{ 
			  $(this).html('Invalid username/password combination').addClass('signmessageboxerror').fadeTo(900,1);	
			});
		  }
        });
});

//capture the return key
$("#login").bind("keydown", function(e) {
if (e.keyCode == 13) {
var lusername = document.login.lusername.value;
  var lpassword = document.login.lpassword.value;
  		$("#loginstatus").removeClass().addClass('signmessagebox').text('Checking...').fadeIn("slow");
		$.post("login.php",{ username:lusername, password:lpassword } ,function(data)
        {
		  if(data=='yes')
		  {
		  	$("#loginstatus").fadeTo(200,0.1,function()
			{ 
			  $(this).html('Logging in... Please wait').addClass('signmessageboxok').fadeTo(900,1);
			});		
			  setTimeout("document.getElementById('page').innerHTML = 'You are now logged in.. Please wait'",750);
setTimeout("javascript:history.go(-0)",1750);
          }
		  else
		  {
		  	$("#loginstatus").fadeTo(200,0.1,function()
			{ 
			  $(this).html('Invalid username/password combination').addClass('signmessageboxerror').fadeTo(900,1);	
			});
		  }
        });
return false; //prevent default behaviour
}
});

$("#signup").bind("keydown", function(e) {
if (e.keyCode == 13) {
var ausername = document.signup.username.value;
var apassword = document.signup.password.value;
var aemail = document.signup.email.value;;
  if(users() && password() && rpassword() && email() && remail() && accept())
  {
		$.post("sup.php",{ 'username': ausername, 'password': apassword, 'email': aemail } ,function(data)
        {
		  if(data=='created')
		  {
			  document.getElementById('message').innerHTML = 'Successfull... please wait';
			  setTimeout("document.getElementById('page').innerHTML = 'User created, you must first use the confirmation link in your email before you can use your account'",1250);
		  }	
		  if(data=='error')
		  {
			  document.getElementById('message').innerHTML = 'Error';
		  }		
          });
  }
  else
  {
	document.getElementById('message').innerHTML = 'Please fill in ALL fields, and check above for errors';
  }
return false; //prevent default behaviour
}

});


});