$(function(){
	$("#username").keyup(function(){
		var illegalChars = /\W/;
		$(this).val(($(this).val().replace(illegalChars, "")).toLowerCase());
	});
	$("#username").keypress(function(){
		var illegalChars = /\W/;
		$(this).val(($(this).val().replace(illegalChars, "")).toLowerCase());
	});
	$.validator.setDefaults({		submitHandler: function()
		{
			//remove all the class add the messagebox classes and start fading
			$("#msgbox").removeClass().addClass('messagebox').html('Saving Information....<br/><img src="../../images/regloader.gif" alt="Loading..."/>').fadeIn(1000);
			
			var username = $("#username").val();
			var password = $("#password").val();
			var firstname = $("#firstname").val();
			var lastname = $("#lastname").val();
			var email = $("#email").val();
			var city = $("#city").val();
			var state = $("#state").val();
			var country = $("#country").val();
			var weddate = $("#wedday").val() + "-" + $("#wedmonth").val() + "-" + $("#wedyear").val();
			var bdate = $("#bday").val() + "-" + $("#bmonth").val() + "-" + $("#byear").val();
			var rndnum = Math.random(); //Spoof the Cache

			$.post("processor.php",{ username:username, password:password, firstname:firstname, lastname:lastname, email:email, city:city, state:state, weddate:weddate, bdate:bdate, rand:rndnum, country:country } ,function(data)
   	     	{
   	     		data = (data).replace(/^\s*|\s*$/g,'');
		  		if(data=='yes') //if correct login detail
		  		{
		  			$("#msgbox").fadeTo(200,0.1,function()  //start fading the messagebox
					{ 
			  			//add message and change the class of the box and start fading
			  			$(this).html('Logging in.....').addClass('messageboxok').fadeTo(900,1);
			  
					});
	  	 			//redirect to secure page
	  	 			//REDIRECT HERE
		 			document.location='../userpanel/index.php';
		  		}
		  		else 
		  		{
		  			$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
					{ 
			  			//add message and change the class of the box and start fading
			  			$(this).html('Registration Failed...' + data).addClass('messageboxerror').fadeTo(900,1);
					});		
          		}
				
        	});
 			return false; //not to post the  form physically
		}
	});
	
	$("#registrationForm").validate({
		rules: {
   	 		password: {
   	 			required: true,
   	 			minLength: 8
    		},
    		confirmpassword: {
    			equalTo: "#password"
    		},
    		username: {
    			required: true,
    			minLength: 8,
    			remote: "validate.php?action=username"
    		},
    		email: {
    			required: true,
    			remote: "validate.php?action=email"
    		},
    		captcha: {
    			required: true,
    			remote: "validate.php?action=captcha"
   	 		}		
    	},
    	
    	messages: {			firstname: "Please enter your First Name.",			lastname: "Please enter your Last Name.",			username: {				required: "Please enter a Username.",				minLength: "Your username must consist of at least 8 characters.",
				remote: "Username already exists."			},			password: {				required: "Please provide a password.",				minLength: "Your password must be at least 8 characters long."			},			confirmpassword: {
				equalTo: "Please enter the same password as above."
			},			email: {
				required: "Please enter a valid email address.",
				remote: "Email address already registered."
			},
			city: "Please enter your City.",
			state:  "Please enter your State."		}
    });
    
    $("#login_form").submit(function()
	{
		$("#msgbox").removeClass().addClass('messagebox').text('Validating....').fadeIn(1000);
		//check the username exists or not from ajax
		var username = $("#txtUsername").val();
		var password = $("#txtPass").val();
		var rndnum = Math.random(); //Spoof the Cache
		
		$.post("../../processors/login.php",{ username:username, password:password ,rand:rndnum } ,function(data)
        {
		  if(data=='yes') //if correct login detail
		  {
		  	$("#msgbox").fadeTo(200,0.1,function()  //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html('Logging in.....').addClass('messageboxok').fadeTo(900,1,
              function()
			  { 
			  	 //redirect to secure page
				 document.location='../userpanel/index.php';
			  });
			  
			});
		  }
		  else 
		  {
		  	$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html('Login Failed...').addClass('messageboxerror').fadeTo(900,1);
			});		
          }
				
        });
 		return false; //not to post the  form physically
	});
	//now call the ajax also focus move from 
	$("#txtPass").blur(function()
	{
		$("#login_form").trigger('submit');
	});
	
	$('a#forgotpass').facebox({        	loadingImage : '../../css/facebox/loading.gif',        	closeImage   : '../../css/facebox/closelabel.gif'
    });	
	$('#frmforgotpass').livequery("submit", function(){
		var emailAddress = $('#txtEmail').val();
		$.facebox(function(){
			$.post("../../processors/forgotpass.php", {emailAddress:emailAddress}, function(data){
				$.facebox(data);
			});
		});
		return false;
	});


});