var login=(function(){
	var tm=null;
	return{
		masuk:function(a,b){
			a=a || "box_loader";
			b=b || lokal+"member";
			var box=document.getElementById(a);
			
			var us=document.getElementById("username").value;
			var pw=document.getElementById("password").value;
			
			var err="";
			if(us==""){
				err+="Username belum diisi<br/>";
			}
			if(pw==""){
				err+="Password belum diisi<br/>";
			}

			box.innerHTML=err;
			if(tm){
				clearTimeout(tm);
			}
			setTimeout(function(){box.innerHTML="";},5000);
			if(err!="") return;
			
			mload.sh(a);
			
			var ik_data = {
				ajax: 1,
				mode: "plg",
				cl: "login",
				ikdata: {0:"uLogin",1:us,2:pw}
			};
			
			$.ajax({
				url: lokal+"man.ajax.php",
				type: "POST",
				dataType: "json",
				async:true,
				data: ik_data,
				success: function(e){
					mload.hd();
					if(e==1){
						document.location=b;
					}else if(e==50){
						box.innerHTML="Brute Attack !!!!! Banned";
						if(tm){
							clearTimeout(tm);
						}
						setTimeout(function(){box.innerHTML="";},5000);
					}else if(e==2){
						box.innerHTML="Password salah";
						if(tm){
							clearTimeout(tm);
						}
						setTimeout(function(){box.innerHTML="";},5000);
					}else if(e==3){
						box.innerHTML="Username tidak di temukan";
						if(tm){
							clearTimeout(tm);
						}
						setTimeout(function(){box.innerHTML="";},5000);
					}else{
						box.innerHTML="Error Login";
						if(tm){
							clearTimeout(tm);
						}
						setTimeout(function(){box.innerHTML="";},5000);
					}
					
				},
				error: function (e) {
		           box.innerHTML="Error Login";
					if(tm){
						clearTimeout(tm);
					}
					setTimeout(function(){box.innerHTML="";},5000);
		        }
			});
		},
		keluar:function(a){
			a=a|| document.location.href;
			var ik_data = {
				ajax:1,
				mode:"plg",
				cl:"login",
				ikdata:{0:"keluar"}
			};
			
			$.ajax({
				url: lokal+"man.ajax.php",
				type: "POST",
				async: false,
				dataType: "json",
				data: ik_data,
				success: function(e){
					if(e==1){
						document.location=a;
					}else{
						alert('Logout gagal');
					}
				}
			});
		},
		enter:function(e,b,a){
			a=a || document.location.href;

			if(e.which == 13 || e.keyCode == 13){
				login.masuk(b);
			}
		},
		lupa:function(a,b,c){
			a=a || "box_loader";
			b=b || lokal;
			c=c || "email";
			var em=document.getElementById(c).value;
			var err="";
			if(em==""){
				err+="Email kosong.";
			}
			pesan.load(err,a);
			if(err!="") return;
			mload.sh(a);
			var ik_data = {
				ajax:1,
				mode:"plg",
				cl:"login",
				ikdata:{0:"kirimToken",1:em}
			};
			
			$.ajax({
				url: lokal+"man.ajax.php",
				type: "POST",
				async: false,
				dataType: "json",
				data: ik_data,
				success: function(e){
					if(e==1){
						document.location=b;
					}else if(e==2){
						pesan.load("Email Salah",a);
					}else{
						pesan.load("error",a);
					}
				}
			});
		},
		reset:function(a,b,c){
			a=a || "box_loader";
			b=b || document.location.href;
			var tk=c;
			
			var pw1=document.getElementById("password1").value;
			var pw2=document.getElementById("password2").value;
			var err="";
			if(tk==""){
				err+="Error Token. ";
			}
			if(pw1==""){
				err+="Error Password. ";
			}
			if(pw1!=pw2){
				err+="Password tidak sama. ";
			}
			if(pw1.length<6){
				err+="Password kurang dari 6 character. ";
			}
			pesan.load(err,a);
			if(err!="") return;
			mload.sh(a);
			var ik_data = {
				ajax:1,
				mode:"plg",
				cl:"login",
				ikdata:{0:"resetPass",1:tk,2:pw1}
			};
			
			$.ajax({
				url: lokal+"man.ajax.php",
				type: "POST",
				async: false,
				dataType: "json",
				data: ik_data,
				success: function(e){
					if(e==1){
						document.location=b;
					}else{
						pesan.load("error",a);
					}
				}
			});
		}
	};
})();
