/*
 * Project:     Ajax and JSON Script Login
 * File:        script.js
 *
 * This program demonstrates JSON & Ajax Login with jquery.
 * This is open source program.You can modify this script as you want. 
 *
 * If you want to use this script in your site,or project
 * You should have to let me know on my mail.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * For questions, help, comments, discussion, etc.,
 * please mail me at expertsinphp@gmail.com
 *
 * @author : Kumar Dhiraj (Delhi, India)
 * @package: Ajax and JSON
 */ 
$(document).ready(function(){
$("#rolling").slideDown('fast');
});
$(document).ready(function()  // $("#login_form").submit(function(){
 { //alert('aa');	     
	//$("#submit").click(function()
	$("#login_form").submit(function()							  
		{// alert('SUbmit Event Generated');
		if($("#uname").val()=="" || $("#pass").val()=="")
			{
				$("h6").fadeTo('fast','0.99');
				$("msg").hide();
				$("h6").fadeIn('fast',function(){$("h6").html("<span id='error'>Please enter username and password </span>");});
				return false;
			}
			else
			  { 	
				var uname = $("#uname").val();
				var pass = $("#pass").val();
				var course = $("#crs_id").val();
				var host = $("#host").val();
				var mid = $("#mid").val();
				var crs_path = $("#crs_path").val();
				var req_uri = $("#requestUri").val();
				//alert(crs_path);
				//alert(uname+'hii upper'+pass);
				//alert('shgdsadsadsadsds');
				//$("p").html('<span class="normal"><img src="loading.gif"></span>'); //alert(course);
				//alert(pass); 
				//alert('Uname='+uname+' and Pass='+pass);  //For Checking the Vars Value
				$.getJSON(host+"server.php",{username:uname,password:pass},function(json)
					 {  
						 //alert(uname+'hii'+pass);
						// Parse JSON data if json.response.error = 1 then login successfull					
						var jsonerrcode=json.response.error;					 
						 //jsonvarid=jsonvarid.split('-');
						 //var jsonuid=jsonvarid[1];
						  //var jsonerrcode=jsonvarid[0];		
						 // alert(jsonerrcode);
						 
					    if(jsonerrcode == "1")
						  {
							data = "<span id='msg'>Welcome "+uname+"</span>";							
							var urlSuccess=host+'index.php?mode=secureLogin';							
							  // alert('URL if Successfull='+urlSuccess+'&user='+uname+'&pass='+pass);
							   if(mid!=0)
							     {
									var urlSuccess=host+'index.php?mode=secureLogin&mid='+mid;	
									 //alert(urlSuccess);
									window.location.href=urlSuccess;
								 }
							   if(mid==0)
							    {
								  var urlSuccess=host+'index.php?mode=secureLogin';
								  //alert(urlSuccess);
								  window.location.href=urlSuccess;
								}
								if(course!='')
							      {
									var coursePayUrl=host+'index.php?mode=secureLogin&crs_id='+course;
							        window.location.href=coursePayUrl;
								 }
								if(crs_path!='')
							     {
									var coursePublicUrl=host+'index.php?mode=secureLogin&crs_path='+crs_path;
							        window.location.href=coursePublicUrl;
								 }
								if(req_uri!='')
							      {
									var coursePublicUrl=host+'index.php?mode=secureLogin&req_uri='+req_uri;
							        //alert(coursePublicUrl);
									window.location.href=coursePublicUrl;
								  }								 
								/*
								else
								{
								  window.location.href=urlSuccess;
								}
							*/									
						}
						
						// Login failed
						 if(jsonerrcode=="0")
						  {   //alert('KKKK Modi');   
							 window.location.href='index.php?mode=lfj&login_attempt=1'; // Invalid User Id or Password
							 // alert(window.location.href='index.php?mode=lfj&login_attempt=1');
							/*$("p").css('background','');
							$("p").css('border-bottom',' ');
							data = "<span id='error'>Provided details not matched, Please try again...</span>";
							$("p").fadeTo('slow','0.99');
							$("p").fadeIn('slow',function(){$("p").html("<span id='msg'>"+data+"</span>");});							
							*/
						  }
						 if(jsonerrcode == "2")
						  {	
						    var passHistPage=host+'index.php?mode=lfj';
						     window.location.href=passHistPage;
						  }						  
							/*
							$("p").fadeTo('slow','0.99');
							 if(json.response.error == "0"){
							    //alert("Hellooooo")
							   $("p").fadeIn('slow',function(){$("p").html("<span id='msg'>"+data+"</span>");});
							 } */
					});
				return false;
			}
		}
	);
  
	$("#uname").focus(function(){
			$("h6").fadeTo('slow','0.0',function(){$("h6").html('');});
		}
	 );
	$("#pass").focus(function(){
			$("h6").fadeTo('slow','0.0',function(){$("h6").html('');});
		}
	); 
});
