$(document).ready(function() {
	$("#REGISTER").ajaxForm(reg_options);
	$("#LOGIN").ajaxForm(login_options);
	$("#CONFIRM").ajaxForm(confirm_options);
	$("#EDITPROFILE").ajaxForm(editprofile_options);
	$("#logout").bind("click",logoutPortal);
	$("#editprofile").bind("click",editProfile);
	$("#ubahpassword").bind("click",ubahPassword);



});

/*================== Registrasi=====================*/
var reg_options = { 
		beforeSubmit:  function() {
					if(document.REGISTER.editprofile.value == "register")
					{
						if(validasi() != "OK")
						{
							goTab('REGISTER');					
							return false;
						}
					}
					else if(document.REGISTER.editprofile.value == "editprofile")
					{
						if(validasi_editprofile() != "OK")
						{
							goTab('REGISTER');					
							return false;
						}

					}
					else
					{
						if(validasi_ubahpassword() != "OK")
						{
							goTab('REGISTER');					
							return false;
						}

					}

		               },
		success:    SubmitRegisterSuccess,
		url: 'modules/?id=admuser&svr=s01',
		type: 'post',
		dataType: 'script',
		resetForm: false
    }; 

function SubmitRegisterSuccess(responseXML) {
	
	if(document.REGISTER.editprofile.value == "register")
	{
		if(validasi() == "OK")
		{
			if(registrasi_success == 0)
			{
				alert(message("registrasiSukses"));
				 outTab('REGISTERSTYLE');
			}
			else if (registrasi_success == 1)
			{
				alert(message("userExist"));
				goTab('REGISTER');
			}
			else
			{
				alert(message("kodeKonfirmasiSalah"));
				goTab('REGISTER');
			}
		}
		else
		{
			goTab('REGISTER');
		}
	}	
	else if(document.REGISTER.editprofile.value == "editprofile")
	{
		jQuery("#REGISTER").ajaxSubmit(editprofile_options);
	}
	else
	{
		jQuery("#REGISTER").ajaxSubmit(ubahpassword_options);
	}
}

function validasi()
{
	var t=document.REGISTER;
	var usernamex=t.username.value;
	var usernames=t.username;
	var namex=t.name.value;
	var names=t.name;
	var sexx=t.sex.value;
	var sexs=t.sex;
	var emailx=t.email.value;
	var emails=t.email;
	var new_passwordx=t.new_password.value;
	var new_passwords=t.new_password;
	var password_confirmx=t.password_confirm.value;
	var password_confirms=t.password_confirm;
	var hint_descx=t.hint_desc.value;
	var hint_descs=t.hint_desc;
	var hint_answerx=t.hint_answer.value;
	var hint_answers=t.hint_answer;
	var confirm_codex=t.confirm_code.value;
	var confirm_codes=t.confirm_code;

	var retval = "";
	if (usernamex.length==0)
	{
		alert(message("isiUserid"));
		usernames.focus();
	}
	else
	{
		if(emailx.length==0)
		{	
		alert(message("isiEmail"));
		emails.focus();
		}
		else
		{
			var x = emails.value;
			var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;	
			if (filter.test(x))
			{
				if(new_passwordx.length == 0)
				{
					alert(message("isiPassword"));
					new_passwords.focus();
				}	 
				else
				{
					if(password_confirmx.length == 0)
					{
						alert(message("isiKonfirmasi"));
						new_passwords.focus();
					}	 
					else
					{
						if(new_passwordx != password_confirmx)
						{
							alert(message("bedaPassword"));
							new_passwords.focus();
						}
						else
						{
							if(hint_answerx.length == 0)
							{
								alert(message("isiJawabanRahasia"));
								hint_answers.focus();
							}
							else
							{
								if(confirm_codex.length == 0)
								{
									alert(message("isiKodeVerifikasi"));
									confirm_codes.focus();
								}
								else
								{
									retval ="OK";
								}
							}
						}
					}		
				}
			}
			else
			{
				alert(message("salahTulisEmail")); 
				emails.focus();
			}
		}
	}
	return retval;
}

function module_admuser_register()
{
	var ctgrHandler = "modules/?id=admuser&svr=s05";
	var pars = "";
       jQuery.get(ctgrHandler,pars,function(data) {

		var userid = data;
		if((userid == " ") || (userid.length <= 1))
		{
			var ctgrHandler = "modules/?id=admuser&svr=s07";
			var pars = "";
		       jQuery.get(ctgrHandler,pars,function(data) {
				$("div#REGISTERSTYLE").html(data);
				goTabRegister();
				
				$("#REGISTER").ajaxForm(reg_options);
			});
		}		
		else
		{
			goTabEditprofile();
		}
		$("#REGISTER").ajaxForm(reg_options);
       });
	

}


/* ====================end Registrasi=================*/

/*========================Login============================*/
var login_options = { 
		beforeSubmit:  function() {
		               },
		success:    SubmitLoginSuccess,
		url: 'modules/?id=admuser&svr=s00',
		type: 'post',
		dataType: 'script',
		resetForm: true
    }; 

function SubmitLoginSuccess(responseXML ) {

	if(login_success == 0)
	{
		alert(message("unregUser"));
		goTab('LOGIN');
	}
	else
	{
	    outTab('LOGINSTYLE');
		var loginArr = responseXML.split("|");
	    var login = loginArr[0];
		var user = loginArr[1];
		var group = loginArr[2];

		var x = user.length;
		var xx = x-1;
		user = user.substring(1,xx);

		var y = group.length;
		var yy = y-1;
		group = group.substring(1,yy);

		var today = new Date();
		var expire = new Date();
		expire.setTime(today.getTime() + 1800000);

		$.cookie('userid', user, {expires: expire, path: '/', domain: 'bumn.go.id'});
		$.cookie('groupid', group, {expires: expire, path: '/', domain: 'bumn.go.id'});
		
		var userLogin = $.cookie('userid');

		var idLang = $.cookie('pbahasa');
		var ubahpassword = "";
		var ubahprofil = "";
		var portallama = "";

		if(idLang == "EN"){
			ubahpassword = "Change Password";
			ubahprofil = "Edit Profile";
			portallama = "Old Portal";
		}
		else {
			ubahpassword = "Ubah Password";
			ubahprofil = "Ubah Profile";
			portallama = "Portal Lama";
		}

		document.getElementById("LOGLOG").innerHTML="<a href=\"www1.bumn.go.id\" title=\"Portal Lama\">"+portallama+"</a> | "+
				"<a href=\"#ubahpassword\" id=\"ubahpassword\" title=\""+ubahpassword+"\" class=\"ico-profil\">"+ubahpassword+"</a> | "+
				"<a href=\"#editprofile\" id=\"editprofile\" title=\""+ubahprofil+"\" class=\"ico-profil\">"+ubahprofil+"</a> | "+
				"<a href=\"#logout\" id=\"logout\" title=\"Logout [ "+userLogin+" ]\" class=\"ico-logout\">Logout [ "+userLogin+" ]</a>";
	}	
	$("#LOGOUT").ajaxForm(logout_options);
	$("#logout").bind("click",logoutPortal);
	$("#REGISTER").ajaxForm(reg_options);
 	$("#editprofile").bind("click",editProfile);
	$("#ubahpassword").bind("click",ubahPassword);

}
/*======================end Login==========================*/

/*========================confirm============================*/
var confirm_options = { 
		beforeSubmit:  function() {
		               },
		success:    SubmitConfirmSuccess,
		url: 'modules/?id=admuser&svr=s02',
		type: 'post',
		dataType: 'script',
		resetForm: true
    }; 

function SubmitConfirmSuccess(responseXML ) {
	 var confirmArr = responseXML.split("|");
	  var confirm = confirmArr[0];
	  var newPasswd = confirmArr[1];
	
	if(validasi_confirm() == "OK")
	{
		if(confirm_success == 1)
		{
			alert(message("kirimPassword"));
			outTab('CONFIRMSTYLE');
		}
		else if(confirm_success == 2)
		{
			alert(message("gagalEmail"));
			goTab('CONFIRM');
		}
		else
		{
			alert(message("salahIdentitas"));
			goTab('CONFIRM');
	    	}	
	}
	else
	{
		goTab('CONFIRM');
	}
		
}

function validasi_confirm()
{
	var t=document.CONFIRM;
	var usernamex=t.username.value;
	var usernames=t.username;
	var emailx=t.email.value;
	var emails=t.email;
	var hint_descx=t.hint_desc.value;
	var hint_descs=t.hint_desc;
	var hint_answerx=t.hint_answer.value;
	var hint_answers=t.hint_answer;
	
	var retval = "";
	if (usernamex.length==0)
	{
		alert(message("isiUserid"));
		usernames.focus();
	}
	else
	{
		if(emailx.length==0)
		{	
			if(hint_answerx.length == 0)
			{
				alert(message("isiKeyLupaPassword"));
				hint_answers.focus();
			}
			else
			{
				retval = "OK";
			}
		}
		else
		{
			var x = emails.value;
			var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;	
			if (filter.test(x))
			{
				retval ="OK";

			}
			else
			{
				alert(message("salahTulisEmail")); 
				emails.focus();
			}
		}
	}
	return retval;
}
/*======================end confirm==========================*/
/*=====================Logout=====================*/
var logout_options = { 
		beforeSubmit:  function() {
		               },
		success:    SubmitLogoutSuccess,
		url: 'modules/?id=admuser&svr=s03',
		type: 'post',
		dataType: 'script',
		resetForm: true
    }; 

function SubmitLogoutSuccess(responseXML ) {

	if(data == '1')
	{
		$.cookie("userid",'',{Expires: -1, path: '/', domain: 'bumn.go.id'});
		$.cookie("groupid",'',{Expires: -1, path: '/', domain: 'bumn.go.id'});
		var userLogin = $.cookie('userid');
		if(userLogin == "")
		{
			userLogin = "Tamu";
		}
		document.getElementById("LOGLOG").innerHTML="<a href=\"javascript:goTabLogin()\" class=\"ico-logout\">Login</a>";
	}	
		
}

function logoutPortal()
{
	var ctgrHandler = "modules/?id=admuser&svr=s03";
	var pars = "";
       jQuery.get(ctgrHandler,pars,function(data) {

	  if(data == 1)
	  {
		$.cookie("userid",'',{Expires: -1, path: '/', domain: 'bumn.go.id'});
		$.cookie("groupid",'',{Expires: -1, path: '/', domain: 'bumn.go.id'});
		var userLogin = $.cookie('userid');
		
		var idLang = $.cookie('pbahasa');
		var ubahprofil ="";
		var portallama ="";

		if(idLang == "EN"){
			ubahprofil = "Edit Profile";
			portallama = "Old Portal";
		}
		else {
			ubahprofil = "Ubah Profile";
			portallama = "Portal Lama";
		}

		document.getElementById("LOGLOG").innerHTML="<a href=\"www1.bumn.go.id\" title=\"Portal Lama\">"+portallama+"</a> | "+
								"<a href=\"javascript:goTabLogin()\" class=\"ico-logout\">Login</a>";
	}
	
       });

}

/*====================end Logout==================*/
/*================== Edit Profile=====================*/
function editProfile()
{
	var ctgrHandler = "modules/?id=admuser&svr=s05";
	var pars = "";
       jQuery.get(ctgrHandler,pars,function(data) {

		var userid = data;
		getFormEditProfile(userid);
		$("#REGISTER").ajaxForm(reg_options);
       });
	

}

function getFormEditProfile(userLogin)
{
	var ctgrHandler = "modules/?id=admuser&svr=s06&userid="+userLogin;
	var pars = "";
       jQuery.get(ctgrHandler,pars,function(data) {
		$("div#REGISTERSTYLE").html(data);
		
              goTabEditprofile();
		eval("document.getElementById('regPanel').style.marginLeft= (((document.body.clientWidth-990)/2)+300)");

		$("#REGISTER").ajaxForm(reg_options);
	});
	

}

var editprofile_options = { 
		beforeSubmit:  function() {
					if(validasi_editprofile() != "OK")
					{
						goTab('REGISTER');
						return false;
					}
		               },
		success:    SubmitEditprofileSuccess,
		url: 'modules/?id=admuser&svr=s04',
		type: 'post',
		dataType: 'script',
		resetForm: true
    }; 

function SubmitEditprofileSuccess(data) {
	var valid = validasi_editprofile();
	if(valid  == "OK")
	{
		if(data == 0)
		{
			alert(message("ubahProfilSukses"));
		     outTab('REGISTERSTYLE');
		}
		else if (data == 1)
		{
			alert(message("ubahProfilGagal"));
			goTab('REGISTER');
		}
		else if(data == 2)
		{
			alert(message("salahPassword"));
			goTab('REGISTER');

		}
	}
	else
	{
		goTab('REGISTER');
	}
}
	
function validasi_editprofile()
{
	var t=document.REGISTER;
	var namex=t.name.value;
	var names=t.name;
	var sexx=t.sex.value;
	var sexs=t.sex;
	var emailx=t.email.value;
	var emails=t.email;
	var old_passwordx=t.old_password.value;
	var old_passwords=t.old_password;
	var new_passwordx=t.new_password.value;
	var new_passwords=t.new_password;
	var password_confirmx=t.password_confirm.value;
	var password_confirms=t.password_confirm;
	var hint_descx=t.hint_desc.value;
	var hint_descs=t.hint_desc;
	var hint_answerx=t.hint_answer.value;
	var hint_answers=t.hint_answer;
	

	if(emailx.length==0)
	{	
	alert(message("isiEmail"));
	emails.focus();
	}
	else
	{
		var x = emails.value;
		
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;	
		if (!filter.test(x))
		{
			alert(message("salahTulisEmail")); 
			emails.focus();
		}
		else
		{
			if(new_passwordx.length > 0)
			{
				if(old_passwordx.length > 0)
				{

					if(new_passwordx != password_confirmx)
					{
						alert(message("bedaPassword"));
						new_passwords.focus();
					}
					else
					{
						var retval = "OK";
						return retval;
					}
				}
				else
				{
					alert(message("isiPasswordLama")); 
					old_passwords.focus();
				}
			}
			else
			{
				var retval = "OK";
				return retval;
			}
		}
	}
	
}
	
function ubahPassword()
{
	var ctgrHandler = "modules/?id=admuser&svr=s05";
	var pars = "";
       jQuery.get(ctgrHandler,pars,function(data) {

		var userid = data;
		getFormUbahPassword(userid);
		$("#REGISTER").ajaxForm(reg_options);
       });
}

function getFormUbahPassword(userLogin)
{
	var ctgrHandler = "modules/?id=admuser&svr=s08&userid="+userLogin;
	var pars = "";
       jQuery.get(ctgrHandler,pars,function(data) {
		$("div#REGISTERSTYLE").html(data);
		
              goTabUbahPassword();
		eval("document.getElementById('regPanel').style.marginLeft= (((document.body.clientWidth-990)/2)+300)");

		$("#REGISTER").ajaxForm(reg_options);
	});
}

var ubahpassword_options = { 
		beforeSubmit:  function() {
					if(validasi_ubahpassword() != "OK")
					{
						goTab('REGISTER');
						return false;
					}
		               },
		success:    SubmitUbahPasswordSuccess,
		url: 'modules/?id=admuser&svr=s09',
		type: 'post',
		dataType: 'script',
		resetForm: true
    }; 

function SubmitUbahPasswordSuccess(data){
	var valid = validasi_ubahpassword();
	if(valid  == "OK")
	{
		if(data == 0)
		{
			alert(message("ubahPasswordSukses"));
		     outTab('REGISTERSTYLE');
		}
		else if (data == 1)
		{
			alert(message("ubahPasswordGagal"));
			goTab('REGISTER');
		}
		else if(data == 2)
		{
			alert(message("salahPassword"));
			goTab('REGISTER');
		}

	}
	else
	{
		goTab('REGISTER');
	}

}

function validasi_ubahpassword()
{
	var t=document.REGISTER;
	var old_passwordx=t.old_password.value;
	var old_passwords=t.old_password;
	var new_passwordx=t.new_password.value;
	var new_passwords=t.new_password;
	var password_confirmx=t.password_confirm.value;
	var password_confirms=t.password_confirm;

	if(old_passwordx.length < 0)
	{
		alert(message("isiPasswordLama"));
		new_passwords.focus();
	}
	else
	{
		if(new_passwordx != password_confirmx)
		{
			alert(message("bedaPassword"));
			new_passwords.focus();
		}
		else
		{
			var retval = "OK";
			return retval;
		}
	}
}


/* ====================end Edit Profile=================*/

  function today() {
    var days=new Array("Minggu", "Senin", "Selasa", "Rabu", "Kamis", "Jum'at", "Sabtu");
    var months=new Array("","Januari", "Februari", "Maret", "April", "Mei", "Juni", "Juli", "Agustus", "September", "Oktober", "November","Desember");
    var tdy=new Date();
    var d=tdy.getDate();
    var dy=tdy.getDay();
    var day=days[dy];
    if (d<10) d="0"+d;
    var m=tdy.getMonth()+1;
    var y=tdy.getYear();
    if (y<2000) y=y+1900;   
    var month=months[m];

    return ""+day+", "+d+" "+month+" "+y;
  }

  function logout() {
    advAJAX.get({
      url: "logout.php",
      onSuccess : function(obj) {
        document.getElementById("welcome").innerHTML="Selamat datang, Tamu";
        document.getElementById("LOGLOG").innerHTML="<a href=\"javascript:goTab('LOGIN')\">Login</a>";
        document.getElementById("CMSLINK").innerHTML="";
//        alert(obj.responseText);
      },
      onError : function(obj) {
        alert("Error: " + obj.status);
      }
    });
  }

  function opentab(menu) {
    if (document.all) {
      eval("document.all."+menu+"TABLE.width = document.body.clientWidth");
      if (menu == "BLOCK") {
        eval("document.all."+menu+"TABLE.height = document.body.scrollHeight");
      } 
      else {
        eval("document.all."+menu+"TABLE.height = document.body.clientHeight");
      }
      eval(menu+"STYLE.style.pixelLeft=0");
      eval(menu+"STYLE.style.pixelTop=0");
    } 
    else {
      eval("document.getElementById('"+menu+"TABLE').style.width = document.body.clientWidth");
      if (menu == "BLOCK") {
        eval("document.getElementById('"+menu+"TABLE').style.height = document.body.scrollHeight");
      }
      else {
        eval("document.getElementById('"+menu+"TABLE').style.height = document.body.clientHeight");
      }
      if ((!document.all) && (menu=="BLOCK")) eval("document.getElementById('"+menu+"TABLE').style.MozOpacity = 0.70");
      eval("document.getElementById('"+menu+"STYLE').style.left=0");
      eval("document.getElementById('"+menu+"STYLE').style.top=0");
    } 
//    if (menu != "BLOCK") eval("document."+menu+".elements[0].focus()");
      if (menu != "BLOCK"){
	 eval("document."+menu+".elements[0].focus()");
    
	 eval("document.getElementById('regPanel').style.marginLeft= (((document.body.clientWidth-990)/2)+300)");
        eval("document.getElementById('confirmPanel').style.marginLeft= (((document.body.clientWidth-990)/2)+300)");
	}

  }

  function closetab(style) {
    if (document.all) {
      eval(style+".style.pixelLeft=-1300");
      eval(style+".style.pixelTop=-1300");
    }
    else {
      eval("document.getElementById('"+style+"').style.left=-1300");
      eval("document.getElementById('"+style+"').style.top=-1300");
    }
  }

  function goTabLogin() {
    opentab("BLOCK");
    opentab("LOGIN");
    clearFormLogin();
  }

  function clearFormRegister()
  {
	eval ("document.REGISTER.username.value= ''");
	eval ("document.REGISTER.name.value= ''");
	eval ("document.REGISTER.email.value= ''");
	eval ("document.REGISTER.new_password.value= ''");
	eval ("document.REGISTER.password_confirm.value= ''");
	eval ("document.REGISTER.hint_answer.value= ''");
	eval ("document.REGISTER.confirm_code.value= ''");
  }  

  function clearFormLogin()
  {
	eval ("document.LOGIN.ID.value= ''");
	eval ("document.LOGIN.PWD.value= ''");
  }  

  function clearFormConfirm()
  {
	eval ("document.CONFIRM.username.value= ''");
	eval ("document.CONFIRM.email.value= ''");
	eval ("document.CONFIRM.hint_answer.value= ''");
  }

  function goTabRegister() {
    opentab("BLOCK");
    opentab("REGISTER");
    clearFormRegister();
  }

  function goTabEditprofile() {
    opentab("BLOCK");
    opentab("REGISTER");
  }

  function goTabUbahPassword() {
    opentab("BLOCK");
    opentab("REGISTER");
  }


  function goTabRegister1() {
    opentab("BLOCK");
    opentab("REGISTER1");
  }

  function goTab(menu) {
    opentab("BLOCK");
    opentab(menu);
  }

  function outTab(menu) {
    closetab("BLOCKSTYLE");
    closetab(menu);
  }


  function login() {
    if (document.LOGIN.ID.value == '') {
      document.LOGIN.ID.focus();
    }
    else {
      advAJAX.get({
        url: "login.php?ID="+document.LOGIN.ID.value+"&PWD="+document.LOGIN.PWD.value,
        onSuccess : function(obj) {
          var resl=obj.responseText.split("/");
          if (resl[1]=="ERROR") alert ("login error !");
          else {
            document.getElementById("welcome").innerHTML="Selamat datang, "+resl[0];
            document.getElementById("LOGLOG").innerHTML="<a href=\"javascript:logout()\">Logout</a>";
            if (resl[1]=="ADMIN") document.getElementById("CMSLINK").innerHTML="| <a href=\"/CMSMU\">CMS</a>";
          }
//          alert(obj.responseText);
        },
        onError : function(obj) {
          alert("Error: " + obj.status);
        }
      });
      outTab('LOGINSTYLE');
    }
  }

  function showScreen() {
    window.location.reload();
  }
  function getMouseXPos(e) {
    if (document.layers||ns6) {
      return parseInt(e.pageX+10)
    } else {
      return (parseInt(event.clientX+10) + parseInt(document.body.scrollLeft))
    }
  }
 
