var project_phase_obj                  = null ;
var project_phase_rights_obj             = null ;
var project_phase_employee_obj         = null ;
var m_project_phase_rights_idx           = 0 ;
var m_project_phase_user_idx       = 0 ;
var project_phase_rights_id_array        = [];
var item_sort = new Array();

$(document).ready(function() {
	$('#btnProjectPhaseClear').button().bind('click',clearProjectPhase) ;
	$('#btnProjectPhaseUpdate').button().bind('click',updateProjectPhase) ;
	$('#btnProjectPhaseAdd').button().bind('click',addProjectPhase) ;
	
	$('#project-phase-group-tabs').tabs() ;
	
/*	var i;
	for(i = 0; i < document.getElementById('sbg-project-phase-group-table').rows[0].cells.length; i++){
		item_sort[i] = 1;
	}*/
	
	
	$(window).resize(resizeProjectPhaseGrid) ;
	resizeProjectPhaseGrid(false);
}) ;


function resizeProjectPhaseGrid(vis){
	var h = $("#sbg-center-panel").outerHeight() - 90;
	if (vis){
		h = h - $('#sbg-project-phase-group-entry').outerHeight();
	}
	$("div#sbg-project-phase-group-data").css("height", h +'px') ;
}
function clearProjectPhase() {
	$('#txtProjectPhaseId').val("Auto") ;
	$('#txtProjectPhaseDesc').val("") ;
	$('#txtProjectPhaseRef').val("") ;
    
	$('#txtProjectPhaseProj').val(0);
	
	$('#project_phase_err_mesg').text('') ;
	$('#project_phase_err_id').hide() ;
	$('#project_phase_err_desc').hide() ;
	
	clearProjectPhaseRights();
	clearProjectPhaseUser();
	if (project_phase_obj != null && project_phase_obj != undefined)
	{
		$($(project_phase_obj).closest("tr")).removeClass("sbg-row-selected") ;
	}
	project_phase_obj = null;
}
function clearProjectPhaseRights() {
	project_phase_rights_obj = null;
	project_phase_rights_id_array = [];
	$('#tblProjectPhaseRights').empty() ;
	m_project_phase_rights_idx = 0 ;
}
function clearProjectPhaseUser() {
	project_phase_employee_obj = null;
	$('#tblProjectPhaseUser').empty() ;
	m_project_phase_user_idx = 0 ;
}
function updateProjectPhase() {
	saveProjectPhase(C_UPDATE) ;
}
function addProjectPhase() {
	saveProjectPhase(C_ADD) ;
}
function saveProjectPhase(type) {
	if (validateProjectPhase() == false)
		return false;
		
	var data = { "type": type, 
					"project_phase_id": $('#txtProjectPhaseId').val(), 
					"project_id": $('#txtProjectPhaseProj').val(), 
                    "desc": $('#txtProjectPhaseDesc').val(), 
                    "ref": $('#txtProjectPhaseRef').val(), 
                    "rights": getProjectPhaseRights(), 
                    "user": getProjectPhaseUser()	
                };
	var url = "request.pzx?c=" + project_phase_url + "&d=" + new Date().getTime() ;
	callServer(url,"json",data,onProjectPhaseResponse,project_phase_obj) ;
}

function editProjectPhase(project_phase_id, obj) {
	var data = { "type": C_GET,"project_phase_id": project_phase_id} ;
	var url = "request.pzx?c=" + project_phase_url + "&d=" + new Date().getTime();		
	clearProjectPhase() ;
	project_phase_obj = obj ;
	$($(obj).closest("tr")).addClass("sbg-row-selected");
	callServer(url,"json",data,showProjectPhase,obj) ;
}

function deleteProjectPhase(id, obj) {
	if (confirm("Confirm you want to delete request approval group : -" + id + "?") == false)
		return false;
		
	var data = {"type": C_DELETE, "project_phase_id": id} ;
	var url = "request.pzx?c=" + project_phase_url + "&d=" + new Date().getTime() ;
	callServer(url,"json",data,onProjectPhaseResponse,obj) ;
}
function showProjectPhase(obj,resp) {
	if (resp.status != C_OK) {
		swal("System Message",resp.mesg) ;
		return false;
	}
		
	$("#cobWorkFlow").html(resp.data.workflows);
	$("#cobApprovalGroup").html(resp.data.approvalGroups);
	
	$('#txtProjectPhaseId').val(resp.data.id) ;
	$('#txtProjectPhaseDesc').val(resp.data.desc) ;
	$('#txtProjectPhaseRef').val(resp.data.ref) ;
    $('#txtProjectPhaseProj').val(resp.data.project_id);
	
	showProjectPhaseRights(resp.data.rights);
	
	showProjectPhaseUser(resp.data.user);
		
	$('#txtProjectPhaseDesc').focus() ;
	$('#btnProjectPhaseUpdate').show() ;
	//$('#btnProjectPhaseAdd').hide() ;
}
function onProjectPhaseResponse(obj,resp) {
    
	if (resp.status != C_OK) {
		swal("Error",resp.mesg) ;
		return false;
	}
		
	if (resp.type == C_ADD) {
		$('#sbg-project-phase-data tr:first').after("<tr id='project-phase-rows'>" +
			"<td>" + resp.data + "</td>" + 
			"<td>" + $('#txtProjectPhaseRef').val() + "</td>" + 
			"<td>" + $('#txtProjectPhaseDesc').val() + "</td>" + 
			"<td style='text-align:center'><a href='javascript:' onclick='editProjectPhase(" + resp.data + ",this)'><img src='image/edit_16.png' title='Edit'></img></a></td>" +
			"<td style='text-align:center'><a href='javascript:' onclick='deleteProjectPhase(" + resp.data + ",this)'><img src='image/delete_16.png' title='Delete'></img></a></td>" +
			"</tr>"); 
		clearProjectPhase();
	} else if (resp.type == C_UPDATE) {
		$($(obj).closest('tr')).children('td:eq(1)').html($('#txtProjectPhaseRef').val()) ;
		$($(obj).closest('tr')).children('td:eq(2)').html($('#txtProjectPhaseDesc').val()) ;
		clearProjectPhase();
	} else if (resp.type == C_DELETE) {
		clearProjectPhase();
		$($(obj).closest("tr")).remove() ;
	}else if (resp.type == C_GET + '_HEAD') {
		$("#cobUserRights").html(resp.data.userList);
	}else if (resp.type == C_GET + '_USER') {
		$("#cobUser").html(resp.data.userList);
	}
	if(resp.type == C_GET + '_USER' || resp.type == C_GET + '_HEAD' || resp.type == C_LIST){
	}else{
		$('#sbg-popup-box').html(resp.mesg) ;
		$('#sbg-popup-box').show() ;
		$('#sbg-popup-box').fadeOut(5000) ;
	}
}

function validateProjectPhase() {
	var allValid = true;
	$('#project_phase_err_mesg').text('') ;
		
	if ($('#txtProjectPhaseDesc').blank())
	{
		$('#project_phase_err_desc').show() ;
		$('#txtProjectPhaseDesc').focus() ;
		$('#project_phase_err_mesg').text($('#project_phase_err_mesg').text() + "Description can not be blank. ") ;
		allValid = false ;
	}
	else 
		$('#project_phase_err_desc').hide() ;
	
	return allValid ;
}

function getProjectPhaseRights() {
	var lines = "";
	var sep = "" ;
	var rights_id = "";
	var id = "" ;
	var order=0;
	
	$('#tblProjectPhaseRights tr').each(function() { 
		id = $(this).find("td").eq(0).html() ;
		rights_id = $('#txtUserRightsId'+id).val() ;
		
		lines = lines + sep + rights_id + '~' + order;
		sep = "|" ;
		order ++ ;
	}); 
	return lines;
}

function getProjectPhaseUser() {
	var lines = "";
	var sep = "" ;
	var user_id = "";
	var id = "" ;
	
	$('#tblProjectPhaseUser tr').each(function() { 
		id = $(this).find("td").eq(0).html() ;
		user_id = $('#txtUserId'+id).val() ;
		
		lines = lines + sep + user_id;
		sep = "|" ;

	}); 
	return lines;
}

function showProjectPhaseRights(lines) {
	$('#tblProjectPhaseRights').empty() ;
	if (lines == undefined || lines == null || lines == "") return ;
	var line = lines.split('|') ;
	var fld = "" ;
	m_project_phase_rights_idx = 0 ;
	for (var i = 0 ; i < line.length ; i++) {
		if (line[i] != "") {
			fld 	= line[i].split('~');
			m_project_phase_rights_idx++;
			$('#tblProjectPhaseRights').append("<tr id='trUserRights" + m_project_phase_rights_idx + "'><td style='width:36px;'>" +m_project_phase_rights_idx+ "</td>" +
			"<td style='width:50px;display:none'><input style='width:50px' id='txtUserRightsId"+m_project_phase_rights_idx+ "' value='"+ fld[0] +"'></input></td>" +
			'<td style="width:280px;"><input disabled style="width:270px" id="txtUserRights'+m_project_phase_rights_idx+ '" value="'+ fld[1] +'"></input></td>' +
			"<td style='width:40px;text-align:center;'><a href='javascript:' onclick='removeProjectPhaseRights(" + m_project_phase_rights_idx + ")'><img src='image/remove_16.png' title='Remove'></img></a></td></tr>"); 
		}
	}
}

function showProjectPhaseUser(lines) {
	$('#tblProjectPhaseUser').empty() ;
	if (lines == undefined || lines == null || lines == "") return ;
	var line = lines.split('|') ;
	var fld = "" ;
	m_project_phase_user_idx = 0 ;
	for (var i = 0 ; i < line.length ; i++) {
		if (line[i] != "") {
			fld = line[i].split('~') ;
			m_project_phase_user_idx++;
			$('#tblProjectPhaseUser').append("<tr id='trUser" + m_project_phase_user_idx + "'><td style='width:36px;'>" +m_project_phase_user_idx+ "</td>" +
			"<td style='width:50px;display:none'><input style='width:50px' id='txtUserId"+m_project_phase_user_idx+ "' value='"+ fld[0] +"'></input></td>" +
			'<td style="width:280px;"><input disabled style="width:270px" id="txtUser'+m_project_phase_user_idx+ '" value="'+ fld[1] +'"></input></td>' +
			"<td style='width:40px;text-align:center;'><a href='javascript:' onclick='removeProjectPhaseUser(" + m_project_phase_user_idx + ")'><img src='image/remove_16.png' title='Remove'></img></a></td></tr>"); 
		}
	}
}
function clearSelectedData() {
	$('#cobUserRights').val("") ;
	$('#cobUser').val("") ;
	
	clearProjectPhaseRights();
	clearProjectPhaseUser();
}

function populateComChange() {
	clearSelectedData();
}


function addProjectPhaseRights() {
	if ( $('#cobUserRights').val() == 0) {
		alert("You must select a User Rights.") ;
		return ;
	}
	m_project_phase_rights_idx++;
	$('#tblProjectPhaseRights').append("<tr id='trUserRights" + m_project_phase_rights_idx + "'><td style='width:36px;'>" +m_project_phase_rights_idx+ "</td>" +
		"<td style='display:none'><input style='width:50px' id='txtUserRightsId"+m_project_phase_rights_idx+ "' value='"+ $('#cobUserRights').val() +"'></input></td>" +
		"<td style='width:280px;'><input disabled style='width:270px' id='txtUserRights"+m_project_phase_rights_idx+ "' value='"+ $('#cobUserRights :selected').text()+"'></input></td>" +
		"<td style='width:40px;text-align:center;'><a href='javascript:' onclick='removeProjectPhaseRights(" + m_project_phase_rights_idx + ")'><img src='image/remove_16.png' title='Remove'></img></a></td></tr>"); 
}
function addProjectPhaseUser() {
	if ( $('#cobUser').val() == 0) {
		alert("You must select a user.") ;
		return ;
	}
	m_project_phase_user_idx++;
	$('#tblProjectPhaseUser').append("<tr id='trUser" + m_project_phase_user_idx + "'><td style='width:36px;'>" +m_project_phase_user_idx+ "</td>" +
		"<td style='display:none'><input style='width:50px' id='txtUserId"+m_project_phase_user_idx+ "' value='"+ $('#cobUser').val() +"'></input></td>" +
		"<td style='width:280px;'><input disabled style='width:270px' id='txtUser"+m_project_phase_user_idx+ "' value='"+ $('#cobUser :selected').text()+"'></input></td>" +
		"<td style='width:40px;text-align:center;'><a href='javascript:' onclick='removeProjectPhaseUser(" + m_project_phase_user_idx + ")'><img src='image/remove_16.png' title='Remove'></img></a></td></tr>"); 
}

function removeProjectPhaseRights(idx) {
	$('#trUserRights'+idx).remove() ;
}

function removeProjectPhaseUser(idx) {
	$('#trUser'+idx).remove() ;
}
