var emp_password_obj = null ;
var item_sort = new Array();
$(document).ready(function() 
{ 
	$('#emp-password-tabs').tabs() ;
	$('#btnResetEmpPwdClear').button().bind('click',clearPasswordEmp) ;
	$('#btnResetEmpPwd').button().bind('click',resetPasswordEmp) ;
	
	$('input').css('marginTop', 0); 
	$('input').css('marginBottom',0);
	$('select').css('marginTop',0) ;
	$('select').css('marginBottom',0) ;	
	$('#btnEmpPasswordList').button().bind('click',filterEmpPassword) ;
	$('#sbg-emp-password-entry').hide();
}) ;
function clearPasswordEmp(){
	$('#lblResetPwdEmpId').text("") ;
	$('#lblResetPwdEmpCode').text("") ;
	$('#lblResetPwdEmpName').text("") ;
	$('#lblResetPwdEmpLogin').text("") ;
	$('#btnResetEmpPwd').prop('disabled','disabled') ;
}
function sortEmpTable(elmt){
	var col = 0;
	var theader = document.getElementById('sbg-emp-password-header').rows[0];
	for(i=0;i<theader.cells.length;i++){
		var strHTML = theader.cells[i].innerHTML;
		if(elmt.innerHTML == strHTML){
			col = i;
		}
	}
	
	var asc = item_sort[col];
	var tbody = document.getElementById('sbg-emp-password-table');
	
	for(i = 0; i < tbody.rows[0].cells.length; i++){
		item_sort[i] = (col == i ? item_sort[i] * -1 : 1);
	}
	
    var rows = tbody.rows, rlen = rows.length, arr = new Array(), i, j, cells, clen;
	
    for(i = 1; i < rlen; i++){
		cells = rows[i].cells;
		clen = cells.length;
		arr[i-1] = new Array();
        for(j = 0; j < clen; j++){
			arr[i-1][j] = cells[j].innerHTML;
			cells[j].innerHTML = '';
        }
    }
    arr.sort(function(a, b){
		if(a[col] == b[col])
			return 0;
		if(!isNaN(a[col]) && !isNaN(b[col])){
			return ((parseInt(a[col]) > parseInt(b[col])) ? asc : -1*asc);
		}
        return ((a[col] > b[col]) ? asc : -1*asc);
    });
	
    for(i = 1; i < rlen; i++){
		cells = rows[i].cells;
		clen = cells.length;
        for(j = 0; j < clen; j++){
			cells[j].innerHTML = arr[i-1][j];
        }
    }

}
function filterEmpPassword() {
	var data = { "type": C_LIST,"company": $('#cobEmployeePasswordCompany').val(),"department": $('#cobEmployeePasswordDepartment').val()} ;
	var url = "request.pzx?c=" + emp_password_url + "&d=" + new Date().getTime() ;		
	callServer(url,"html",data,showEmpPasswordFilterList,emp_password_obj) ;
}

function showEmpPasswordFilterList(obj,resp) {
	var fr = '<tr>' +
				'<td style="width:50px;height:1px"></td>' +
				'<td style="width:100px"></td>' +
				'<td style="width:250px"></td>' +
				'<td style="width:250px"></td>' +
				'<td style="width:250px"></td>' +
				'<td style="width:25px"></td>' +
			'</tr>';
	$('#sbg-emp-password-table').html(fr + resp) ;
}
/*
function resizeEmpGrid(vis) {
	var h = $("#sbg-center-panel").outerHeight() - $("#emp-filter").outerHeight() - 55;
	if (vis)
		h = h - $('#sbg-emp-entry').outerHeight();
	$("div#sbg-emp-data").css("height", h +'px') ;
}*/

function editResetEmpPwd(id,obj) {
	var data = { "type": C_GET,"id": id} ;
	var url = "request.pzx?c=" + emp_password_url + "&d=" + new Date().getTime();		
	callServer(url,"json",data,showEmpPassword,null) ;
}
function resetPasswordEmp() {
	var data = { "type": C_UPDATE,"id": $('#lblResetPwdEmpId').text()} ;
	var url = "request.pzx?c=" + emp_password_url + "&d=" + new Date().getTime();		
	callServer(url,"json",data,showEmpPassword,null) ;
}
function showEmpPassword(obj,resp) {
	if (resp.status == C_OK) {
		if(resp.type == C_GET){
			$('#sbg-emp-password-entry').show();
			$('#lblResetPwdEmpId').text(resp.data.id) ;
			$('#lblResetPwdEmpCode').text(resp.data.code) ;
			$('#lblResetPwdEmpName').text(resp.data.name) ;
			$('#lblResetPwdEmpLogin').text(resp.data.login) ;
			$('#btnResetEmpPwd').prop('disabled','') ;
		} else {
			$('#sbg-emp-password-entry').hide();
			showDialog("System Message",resp.mesg) ;
		}
	}
	else {
		showDialog("System Message",resp.mesg) ;
	}
}

