/*
Author: Pradeep Khodke
URL: http://www.codingcage.com/
*/

$('document').ready(function()
{ 

	//alert("hello there");
     //user login script
     /* validation */
	 $("#signin").validate({
      rules:
	  {
			password: {
			required: true
			},
			email: {
            required: true
            },
	   },
       messages:
	   {
            password:{
                      required: "Please enter your password"
                     },
            email: "Please enter your email address.",
       },
	   submitHandler: submitForm	
       });  
	   /* validation */
	   
	   /* login submit */
	   function submitForm()
	   {		
			var data = $("#signin").serialize();		
				
			$.ajax({
				
			type : 'POST',
			url  : 'config/login.php',
			data : data,
			beforeSend: function()
			{	
				$("#error").fadeOut();
				$("#login").html('<i class="fa fa-spin fa-spinner"></i> Checking ...');
			},
			success :  function(response)
			   {				//alert(response);
					if(response == 3){
						$("#login").html('<i class="fa fa-spin fa-spinner"></i> &nbsp; Signing In ...');
						setTimeout(' window.location.href = "user_accounts/editors/dashboard.php"; ',2000);
					}
					else if(response == 2){
						$("#login").html('<i class="fa fa-spin fa-spinner"></i> &nbsp; Signing In ...');
						setTimeout(' window.location.href = "user_accounts/admin/dashboard.php"; ',2000);
					}
					else if(response == 1){
						$("#login").html('<i class="fa fa-spin fa-spinner"></i> &nbsp; Setting Up Your Account ...');						
						setTimeout(' window.location.href = "first_time.php"; ',2000);
					}					
					else if(response == 4){	
						$("#error").fadeIn(1000, function(){						
					    $("#error").html('<div class="alert alert-warning"> <button type="button" class="close" data-dismiss="alert">×</button><span class="icon icon-user"></span> Your username/password is incorrect!</div>');
						$("#login").html('<span class="fa fa-sign-in"></span> &nbsp; Sign In');
									});//Your username/password entered doesn't match
						$("#error").delay(6000).fadeOut(function(){});
					}
					else if(response == 5){									
						$("#error").fadeIn(2000, function(){						
					    $("#error").html('<div class="alert alert-error"> <button type="button" class="close" data-dismiss="alert">×</button><span class="icon icon-lock"></span> Your account has been locked, contact the Systems Administrator to help you unlock the account!</div>');
						$("#login").html('<span class="fa fa-lock"></span> &nbsp; Account Locked');
									});//Your username/password entered doesn't match
						$("#error").fadeOut(180000,function(){});
					}

			  }
			});
				return false;
		}
	   /* login submit */
/*--------------------------------------------UnLock account-----------------------------------------------*/

     /* validation */
	 $("#contact").validate({
      rules:
	  {
			name: {
			required: true
			},
			email: {
            required: true
            },
            massage:{
            	required:true
            }
	   },
       messages:
	   {
            name:"Please enter your name.",
            email: "Please enter your email address.",
            massage: "Please enter your message."
       },
	   submitHandler: unlockAccount	
       });  
	   /* validation */
	   
	   /* login submit */
	   function unlockAccount()
	   {		
			var data = $("#contact").serialize();		
				
			$.ajax({
				
			type : 'POST',
			url  : 'send_email.php',
			data : data,
			beforeSend: function()
			{	
				$("#error").fadeOut();
				$("#send_message").html('<i class="fa fa-spin fa-spinner"></i>&nbsp; Checking Password ...');
				
			},
			success :  function(response)
			   {	//alert(response);				
					
					//alert(response);				
					/*if(response == 3){
						$("#unlock_account").html('<i class="fa fa-spin fa-spinner"></i> &nbsp; Unlocking Account...');
						setTimeout(' window.location.href = "users/eusers/dashboard.php"; ',2000);
					}
					else if(response == 2){
						$("#unlock_account").html('<i class="fa fa-spin fa-spinner"></i> &nbsp; Unlocking Account...');
						setTimeout(' window.location.href = "users/admin/dashboard.php"; ',2000);
					}*/
					if(response == 1){									
						$("#error").fadeIn(2000, function(){						
					    $("#error").html('<div class="alert alert-error"> <button type="button" class="close" data-dismiss="alert">×</button><span class="icon icon-unlock"></span>&nbsp; Your message has been sent, we will come back to you!</div>');
						$("#send_message").html('<span class="fa fa-unlock"></span> &nbsp; Sign In');
									});//Your username/password entered doesn't match
						$("#error").fadeOut(5000,function(){});
					}
					else{
						$("#unlock_account").html('<i class="fa fa-spin fa-spinner"></i> &nbsp; Unlocking Account...');
						setTimeout(' window.location.href = "'+response+'"; ',2000);
					}
			  }
			});
				return false;
		}
	   /* login submit */


    
     /* validation */
	 $("#call").validate({
      rules:
	  {
			name: {
			required: true
			},
			email: {
            required: true
            },
            massage:{
            	required:true
            }
	   },
       messages:
	   {
            name:"Please enter your name.",
            email: "Please enter your email address.",
            massage: "Please enter your message."
       },
	   submitHandler: unlockAccount	
       });  
	   /* validation */
	   
	   /* login submit */
	   function unlockAccount()
	   {		
			var data = $("#call").serialize();		
				
			$.ajax({
				
			type : 'POST',
			url  : 'send_email.php',
			data : data,
			beforeSend: function()
			{	
				$("#error").fadeOut();
				$("#send_message").html('<i class="fa fa-spin fa-spinner"></i>&nbsp; Checking Password ...');
				
			},
			success :  function(response)
			   {	//alert(response);				
					
					//alert(response);				
					/*if(response == 3){
						$("#unlock_account").html('<i class="fa fa-spin fa-spinner"></i> &nbsp; Unlocking Account...');
						setTimeout(' window.location.href = "users/eusers/dashboard.php"; ',2000);
					}
					else if(response == 2){
						$("#unlock_account").html('<i class="fa fa-spin fa-spinner"></i> &nbsp; Unlocking Account...');
						setTimeout(' window.location.href = "users/admin/dashboard.php"; ',2000);
					}*/
					if(response == 1){									
						$("#error").fadeIn(2000, function(){						
					    $("#error").html('<div class="alert alert-error"> <button type="button" class="close" data-dismiss="alert">×</button><span class="icon icon-unlock"></span>&nbsp; Your message has been sent, we will come back to you!</div>');
						$("#send_message").html('<span class="fa fa-unlock"></span> &nbsp; Sign In');
									});//Your username/password entered doesn't match
						$("#error").fadeOut(5000,function(){});
					}
					else{
						$("#unlock_account").html('<i class="fa fa-spin fa-spinner"></i> &nbsp; Unlocking Account...');
						setTimeout(' window.location.href = "'+response+'"; ',2000);
					}
			  }
			});
				return false;
		}
	   /* login submit */


    


    

	   	

/*-------------------------------------------------------------------------------------------------------*/
		
    

	// $('.btn_delete').on('click', function(){ alert("button has been clicled") });

	  
	 /*  $("#logout").click(function(){
     	alert("here we go therer rereerr rerehrer ererhee rer rer erer ere r");
       });*/

	 //Formulate LPO script
     /* validation */
     /*
	 $("#formulate-lpoo").validate({
      rules:
	  {
			cat: {
			required: true
			},
			check: {
            required: true
            },
            reason: {
            required: true
            },
            approve: {
            required: true
            },
	   },
       messages:
	   {
            cat:   "Please select the Qualified Supplier",
            check: "Please select the one to check the LPO",
            reason:"Please enter the reason why you have selected the Supplier",
            approve:"Please select the one to approve the LPO",
       },
	   submitHandler: submitLpo	
       });  
	   /* validation */
	   
	   /* login submit */
	   /*
	   function submitLpo()
	   {		
			var data = $("#formulate-lpo").serialize();		
				
			$.ajax({
				
			type : 'POST',
			url  : 'insert_script.php',
			data : data,
			beforeSend: function()
			{	
				$("#error").fadeOut();
				$("#send_requisition").html('<i class="fa fa-spin fa-spinner"></i> Posting an LPO ...');
			},
			success :  function(response)
			   {	//alert(response);				
					if(response == 1){
						$("#send_requisition").html('<i class="fa fa-spin fa-spinner"></i> &nbsp; Signing In ...');
						setTimeout(' window.location.href = "users/eusers/dashboard.php"; ',4000);
					}
					else if(response == 'no'){
						$("#send_requisition").html('<i class="fa fa-spin fa-spinner"></i> &nbsp; Signing In ...');
						setTimeout(' window.location.href = "users/admin/dashboard.php"; ',4000);
					}
					else if(response == 2){
						$("#send_requisition").html('<i class="fa fa-spin fa-spinner"></i> &nbsp; Setting Up Your Account ...');						
						setTimeout(' window.location.href = "first_time.php"; ',4000);
					}					
					else if(response == 4){									
						//$("#error").fadeIn(2000, function(){						
					   // $("#error").html('<div class="alert alert-error"> <button type="button" class="close" data-dismiss="alert">×</button><span class="icon icon-user"></span>Your username/password combination is invalid!</div>');
						//$("#send_requisition").html('<span class="fa fa-sign-in"></span> &nbsp; Sign In');
						//			});//Your username/password entered doesn't match
						//$("#error").fadeOut(5000,function(){});
					}
					else{
						sweetAlert({
                                  title: 'Success!',
                                  text: 'LPO has been formulated',
                                  type: 'success'
                                },
                                function () {
                                  setTimeout(' window.location.href = "process_lpo.php"; ',4000);
                                }
                          );
						//$("#error").fadeIn(2000, function(){						
					    //$("#error").html('<div class="alert alert-error"> <button type="button" class="close" data-dismiss="alert">×</button><span class="icon icon-user"></span>'+response+'</div>');
						//$("#send_requisition").html('<span class="fa fa-sign-in"></span> &nbsp; Post LPO');
						//			});//Your username/password entered doesn't match
						//$("#error").fadeOut(15000,function(){});
					}
			  }
			});
				return false;
		}*/
	   /* login submit */

	    /* create new user */
     /* validation */
     $("#add_donor").click(function(){
     	alert("here we go therer rereerr rerehrer ererhee rer rer erer ere r");
     });

      

	 $("#add-donor").validate({
     
      rules:
	  {
			donor: {
			required: true,
			minlength:6,
			}
	   },
       messages:
	   {
            donor:{
                      required: "please enter  password",
                      minlength: "Enter more than 6 letters"
                     }

       },
	   submitHandler: addUser	
       });  
	   /* validation */

	   /* inserting */
	   /* login submit */
	   function addUser()
	   {		
			var data = $("#add-donor").serialize();
				
			$.ajax({
				
			type : 'POST',
			url  : 'sql_statements.php',
			data : data,
			beforeSend: function()
			{	
				$("#error").fadeOut();
				$("#add_donor").html('<i class="fa fa-spin fa-spinner"></i> Creating user ...');
			},
			success :  function(response)
			   {						
					if(response == "ok"){
						//$("#save_user").html('<i class="fa fa-spin fa-spinner"></i> &nbsp; Saving Data ...');	
					
					    //
					    $("#error").fadeIn(1000, function(){						
					    $("#error").html('<div class="alert alert-success alert-dismissible fade show" role="alert"><strong><i class="fa fa-check"></i> </strong>A user account has been created successfuly!<button type="button" class="close" data-dismiss="alert" aria-lSource Sans Pro="Close"><span aria-hidden="true">&times;</span></button></div>');
						setTimeout(' window.location.href = "all-users.php"; ',4000);
									});

					}
					else{
									
						$("#error").fadeIn(1000, function(){						
					    $("#error").html('<div class="alert alert-danger alert-dismissible fade show" role="alert"><strong><i class="fa fa-exclamation-circle"></i> </strong>'+response+'!<button type="button" class="close" data-dismiss="alert" aria-lSource Sans Pro="Close"><span aria-hidden="true">&times;</span></button></div>');
						$("#add_donor").html('<span class="fa fa-user"></span> &nbsp; Create User');
									});
					}
			  }
			});
				return false;
		}





});