﻿var userID = ''; // this will keep the real user ID after login
var uniqID = ''; // users unique ID
var loggedin = false;
var userrole = '';
var  sid = '';
var _DEBUG_ = false;
$('<link rel="stylesheet" type="text/css" href=/si_lc/js/fancybox/jquery.fancybox-1.3.4.css \/>').appendTo('head');
$.ajaxSetup({async: false});
$.getScript('/si_lc/js/fancybox/jquery.mousewheel-3.0.4.pack.js');
$.getScript('/si_lc/js/fancybox/jquery.fancybox-1.3.4.pack.js');
$.ajaxSetup({async: true});

function ajax_login(userid, pass){
var url="/si_lc/login.php?r" + Math.random();
var params="userID="+userid+"&userPass="+encodeURIComponent(pass);
//alert(encodeURIComponent(params));
$.post(url, params, function(res){
	if (res == null || res == "") {
		alert("登錄失敗!");
		$('#userid').focus();
		return;
	} else if (res.indexOf('SUCCESS') == -1 && 
			   res.indexOf('成功') == -1) {
		 if (_DEBUG_)  alert(res);
		 var err_message = "登入失敗，請查明您的帳號密碼或是您尚未啟用帳號";
		 alert(err_message);
		//$('#error_message').html(res);
		$('#userpass').focus();
		return;
	}else{
		alert('登入成功!');
		
		if(res.indexOf('FORCE_CHANGEPW')!=-1){
			//確認是否強制修改密碼
			$('#member_login').after('<div style="display:none;"><a id="changepw" href="/si_lc/force_change_password.php" title="請更改密碼">請更改密碼</a></div>');
			$("#changepw").fancybox({
							'width'				: '75%',
							'height'			: '75%',
							'autoScale'			: false,
							'transitionIn'		: 'none',
							'transitionOut'		: 'none',
							'hideOnOverlayClick': false,
							'showCloseButton'	: false,
							'type'				: 'iframe'
						});
			$('#changepw').trigger('click');
		}
		
		var url="/phpbb3/ucp.php?mode=login";
		var params="username="+userid+"&password="+encodeURIComponent(pass);
		//alert(encodeURIComponent(params));
		$.ajax({
		   type: "POST",
		   url: url,
		   data: ({
			username: userid,
			password: pass,
			login: '登入'
		   }),
		   success: function(msg){
			   location.reload(true);
			   //alert(msg);
			 //document.write( msg );
			 //window.location.href='/si_lc/index.php';
		   }
		 });
		//window.location.href='/si_lc/index.php';
	}
	$('#member_login').hide();
	var retParams = res.split('#', 4);
	var retParamsArr = new Array(4);
	var tempArr = new Array(2);
	for (x in retParams) {
		tempArr = retParams[x].split('=', 2);
		retParamsArr[tempArr[0]] = tempArr[1];
	}
		
		
	var rolestr      = retParamsArr['USER_ROLE'];//res.substring(res.indexOf("USER_ROLE=")+10);
	var usernamestr  = retParamsArr['USER_NAME'];
	var userpointstr = retParamsArr['USER_POINT'];
	//alert("role : " + rolestr + ", uname : " + usernamestr + ", point : " + userpointstr);
	$('#' + rolestr).show();
	$('#' + rolestr + '_username').html(usernamestr);
	$('#user_points').html(userpointstr);
	  
	if ( rolestr == 'COMP_ADMIN')
				  
			{	  
			page_link = "/si_lc/companies/company_main.php";
			page_name = "企業會員專區";
			}
		else if ( rolestr == 'ORG_ADMIN')
				  
			{	  
			page_link = "/si_lc/external/main.php";
			page_name = "合作開班單位專區";
			}
		else if ( rolestr == 'TEACHER')
				  
			{	  
			page_link = "/si_lc/teachers/teacher_main.php";
			page_name = "專家老師專區";
			}
		else
				  
			{	  
			page_link = "/si_lc/members/members_main.php";
			page_name = "會員專區";
			}
		  
	
	$('#private_page_link').html(page_name);
	$('#private_page_link').attr('href', page_link);
	
	$('#success_message').html('登入成功! <a href="index.php">進入主頁</a>');				 
								 });
}

function ajax_logout(userid,sid) {
	var rno = Math.random();
	//var params="userid="+userid+"&randno=" + rno;
	var params;
	var url="/si_lc/logout.php";//alert(params)
	$.ajax({
		url: url,
		cache: false,
		type: 'GET',	//or POST
		data: {t: new Date().getTime()},
		error: function(xhr) {
		  alert('Ajax request 發生錯誤');
		},
		success: function(response) {
			if ($(document).attr('title') != "Login Panel") {
				rno = Math.random();
				window.location.href="/si_lc/index.php?" + rno; 
			}
		}
	});
	
	//$.get(url, function(res){
	//		if ($('#MEMBER')) {
	/*
	$('#MEMBER').hide();
	$('#COMP_ADMIN').hide();
	$('#ORG_ADMIN').hide();
	$('#TEACHER').hide();
	$('#member_login').show();
	*/
	//}					
	//});

	
	 
}


function login(){
var userid = $('#userid').val();
var userpass = $('#userpass').val();

if (userid == null || userid == "") {
alert("請輸入您的帳號!");
return false;
} else if (userpass == null || userpass == "") {
alert("請填寫密碼!");
return false;
}
//alert('ajax_login');
ajax_login(userid, userpass);
}

function logout(){
//alert("user " + userID + " is logging out ...");
ajax_logout(userID,sid);
}

function check_login(){
	userID = $('#userID').val();
	userrole = $('#userrole').val();
	uniqID = $('#uniqID').val();
	//loggedin = $('#loggedin').val();
	sid = $('#sid').val(); 
	if (sid != null && sid != "") 
	 loggedin = true; 
	 else if (userID != null && userID != '' && userrole != null &&
	 (userrole == 'SUPER_VISOR' || userrole == 'SYS_ADMIN' || userrole == 'COMP_ADMIN' || 
	  userrole == 'ORG_ADMIN' || userrole == 'TEACHER' || userrole == 'MEMBER' )) 
     loggedin = true;  
	 else
	 loggedin = false;
	 
	
	//alert("loggedin : " + loggedin);
	//alert("userrole : " + userrole);
	//alert("uid:" + uniqID);
	//alert('sid : ' + sid);
	//alert('userID : ' + userID);


if (loggedin && (userrole == 'SYS_ADMIN' || userrole == 'SUPER_VISOR') ) {
	//alert('System Administrators should use the Admin Control Panel. Redirecting ...');
	window.location.href='/acpl/index.php';
} else if (loggedin && userrole == 'COMP_ADMIN') {
  $('#member_login').hide();
  $('#' + userrole).show();
} else if (loggedin && userrole == 'ORG_ADMIN') {
  $('#member_login').hide();
  $('#' + userrole).show();
} else if (loggedin && userrole == 'TEACHER') {
  $('#member_login').hide();
  $('#' + userrole).show();
} else if (loggedin && userrole == 'MEMBER') {
  $('#member_login').hide();
  $('#' + userrole).show();

}  else {
	//alert("Unknown user role( " + userrole + " )! Please contact with the webmaster!");
							 return false;
}
}

$(document).ready(function(){
					  check_login();
					  });


