//  Developed by Roshan Bhattarai 
//  Visit http://roshanbh.com.np for this script and more.
//  This notice MUST stay intact for legal use

$(document).ready(function()
{
	$("#login_form").submit(function()
	{
		//remove all the class add the messagebox classes and start fading
		$("#msgbox").removeClass().addClass('messagebox').text('Validating....').fadeIn(1000);
		//check the username exists or not from ajax
		$.post("/jcontent/ajaxlogin.php",{username:$('#username').val(),password:$('#password').val(),rand:Math.random() } ,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='/favorites.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 incorrect').addClass('messageboxerror').fadeTo(900,1);
			});		
          }
				
        });
 		return false; //not to post the  form physically
	});
	//now call the ajax also focus move from 
	$("#password").blur(function()
	{
		$("#login_form").trigger('submit');
	});
});



$(document).ready(function() { 
    // validate signup form on keyup and submit 
    var validator = $("#create_account").validate({ 
        rules: {
            emailaddress: { 
                required: true,
                email: true,
				minlength: 3,
                remote: "/jcontent/vemail.php"
            },
            npassword: {
                required: true,
                minlength: 5
            },
            npassword2: {
                required: true,
                minlength: 5,
                equalTo: "#npassword"
            }
        }, 
        messages: { 
            emailaddress: {
                required: "Please enter a valid email address",
                minlength: "Please enter a valid email address",
                remote: jQuery.format("{0} is already registered on cdhm.org")
            },
            npassword: {
                required: "Provide a password",
                rangelength: jQuery.format("Enter at least {0} characters")
            },
            npassword2: {
                required: "Repeat your password",
                minlength: jQuery.format("Enter at least {0} characters"),
                equalTo: "Enter the same password as above"
            }
        }, 
        // the errorPlacement has to take the table layout into account 
        errorPlacement: function(error, element) { 
                error.appendTo( $('#errors') ); 
        },
        // specifying a submitHandler prevents the default submit, good for the demo 
        submitHandler: function() { 
        	$.ajax({
    		url: '/jcontent/mkaccount.php',
    		type: 'POST',
			dataType: "text",
			data: {
				username : $('#emailaddress').val(),
				password : $('#npassword').val()
			},
    		success: function(result){	
        		alert(result);
    	}
	});
		return false;
    }
		
        // set this class to error-labels to indicate valid fields 
    });
}); 



$(document).ajaxError(function(){
    if (window.console && window.console.error) {
        console.error(arguments);
    }
});




