
/* core functions */

function getListItems(listType) {
	$('#loading').css('display','block');

	clearListObject(listType, 'selItemObj');
	$('#'+listType+'_selItemTotals').text('');

	printListMsg(listType, 1, '');

	if (document.getElementById("uc").value == 1) { // read cookie
		var sort = setListState(listType);
	}

	// get internal (int) and personal public page settings
	var isInternal = (document.getElementById('int') ? document.getElementById('int').value : 0);

	var ajaxObj = new ilincAjax('instant_getListItems', 'instant_getListItemsCallback');
	ajaxObj.addParam('listType', listType);
	ajaxObj.addParam('eventType', 'instant');
	ajaxObj.addParam('eUserId', (document.getElementById('eUserId') ? document.getElementById('eUserId').value : null));
	ajaxObj.addParam('isInternal', isInternal);
	ajaxObj.addParam('selStatus', 'active');
	if (sort) ajaxObj.addParam('sortBy', sort[0]);
	if (sort) ajaxObj.addParam('sortDir', sort[1]);
	ajaxObj.send();
}

function instant_getListItemsCallback(retObj) {
	var listType = retObj.listType;
	var eventType = retObj.eventType;

	// initialize list object and arrays
	clearListArray(listType, 'itemAry');
	clearListArray(listType, 'headAry');
	clearListObject(listType, 'selItemObj');

	listObj[listType].itemAry = retObj.itemAry;
	listObj[listType].headAry = retObj.headAry;

	printListItems(1, listType);

	delCookie('ilcListState','/',cookieDomain,useSSLCookie);
	document.getElementById("uc").value = 0;

	$('#loading').css('display','none');
}

// status object needs to be global to be used by sort routines
var statusObj = {
	d: {id: 2821, title:'Denied', style:'stat-denied'},
	e: {id: 2817, title:'Registered', style:'stat-registered'},
	p: {id: 2356, title:'Pending', style:'stat-pending'},
	r: {id: null, title:'&nbsp;', style:'stat-unregistered'},
	f: {id: 2819, title:'Full', style:'stat-full'},
	j: {id: 2348, title:'Join', style:'stat-join'},
	l: {id:  900, title:'Launch', style:'stat-launch'}
}

function printListItems(pageNum, listType) {
	var eventType = 'join';
	var isInternal = document.getElementById('int').value;

	if (listObj[listType].itemAry.length > 0) {
		printListHead(listType);
		$('#'+listType+'_listPagination').css('display','');
	} else {
		printListMsg(listType, 0, ilcTrans(5631,"There are no items to display"));	
		return false;
	}

	var a = setListPagination(pageNum, listType);
	var startIdx = a[0];
	var endIdx = a[1];
	var checkAll = 1;

	// track number of chckable items
	var checkableCount = 0;

	removeAllChildren(listType+'_listTableBody');

	var selTable = document.getElementById(listType+'_listTableBody');
	for (var i=startIdx; i<=endIdx; i++) {
		var newTr = document.createElement('tr');
		newTr.setAttribute('id', listType+"_"+listObj[listType].itemAry[i].id+"Tr");

		//Add this if your first column is a checkbox, selectable list
		var newTd = document.createElement('td');
		$(newTd)
			.attr('id', listType+"_"+listObj[listType].itemAry[i].id+"Td1");

		// join column
		var anchor = document.createElement("a");
		$(anchor)
			.attr('href','#')
			.attr('id', "joinAct_"+listObj[listType].itemAry[i].id)
			.bind('click',{'id':listObj[listType].itemAry[i].id,'block_type':listObj[listType].itemAry[i].block_type,'e_type':listObj[listType].itemAry[i].e_type,'showWL':listObj[listType].itemAry[i].showWL}, function(evt) { actionJoin(evt);return false; })
			.addClass('inlinebutton')
			.css('text-transform', "uppercase")
			.text(ilcTrans(2348,"Join"));
		newTd.appendChild(anchor);
		newTr.appendChild(newTd);

		// type column
		newTd = document.createElement('td');
		$(newTd)
			.attr('id', listType+"_"+listObj[listType].itemAry[i].id+"Td4")
			.attr('style', 'text-align: center');
		// span for holding class
		newSpan = document.createElement('span');
		$(newSpan)
			.attr('id', listType+"_"+listObj[listType].itemAry[i].id+"Td4sp")
			.attr('title', listObj[listType].itemAry[i]['type'])
			.addClass(listObj[listType].itemAry[i].au_type)
			.html('&nbsp;');
		newTd.appendChild(newSpan);
		newTr.appendChild(newTd);

		// session name column
		newTd = document.createElement('td');
		$(newTd)
			.attr('id', listType+"_"+listObj[listType].itemAry[i].id+"Td2")
			.attr('style','width:50%');
		var anchor = document.createElement("a");
		$(anchor)
			.attr('href', "#")
			.bind('click',{'id':listObj[listType].itemAry[i].id,'listType':listType,'isInternal':isInternal}, function(evt) { actionMoreInfo(evt);return false; })
			.text(listObj[listType].itemAry[i].name);
		newTd.appendChild(anchor);
		newTr.appendChild(newTd);
	
		// date/time column
		newTd = document.createElement('td');
		$(newTd)
			.attr('id', listType+"_"+listObj[listType].itemAry[i].id+"Td3")
			.attr('style','width:40%')
			.addClass('nowrap')
			.text(listObj[listType].itemAry[i].start_time);
		if (listObj[listType].itemAry[i].recur_type > 2) {
			$(newTd)
				.append(
					$(document.createElement('span'))
						.text(" | ")
						.get(0)
				)
				.append(
					$(document.createElement('span'))
						.addClass('tooltipText')
						.attr('title',listObj[listType].itemAry[i].date_desc)
						.text(ilcTrans(644, 'Recurring'))
						.get(0)
				);
		}
		newTr.appendChild(newTd);

		if (eventType == 'reg') {
			// status column
			newTd = document.createElement('td');
			$(newTd)
				.attr('id', listType+"_"+listObj[listType].itemAry[i].id+"Td5")
				.attr('style','width:20%')
				.addClass('nowrap')
				.addClass(statusObj[listObj[listType].itemAry[i].pStLt].style)
				.html(ilcTrans(statusObj[listObj[listType].itemAry[i].pStLt].id, statusObj[listObj[listType].itemAry[i].pStLt].title));
			newTr.appendChild(newTd);

			// seats column
			newTd = document.createElement('td');
			$(newTd)
				.attr('id', listType+"_"+listObj[listType].itemAry[i].id+"Td6")
				.attr('style','width:20%')
				.addClass('nowrap')
				.text(listObj[listType].itemAry[i].seats);
			newTr.appendChild(newTd);
		}

		// actions column
		newTd = document.createElement('td');
		$(newTd)
			.attr('id', listType+"_"+listObj[listType].itemAry[i].id+"Td7");

		var anchor = document.createElement("a");
		var img = document.createElement("img");
		$(anchor)
			.attr('href', "#")
			.bind('click',{'id':listObj[listType].itemAry[i].id,'listType':listType,'isInternal':isInternal}, function(evt) { actionMoreInfo(evt);return false; });
		$(img)
			.attr('title', ilcTrans(5564,"More Info"))
			.attr('src', ilcCCDocsDir+"/images/icon_info.gif");
		anchor.appendChild(img);
		newTd.appendChild(anchor);
		newTd.appendChild(document.createTextNode(" "));

		newTr.appendChild(newTd);

		selTable.appendChild(newTr);
	}
	selTable = null;

	$('#'+listType+"_allSelChk").attr('checked', (checkAll ? "checked" : ''));

	// activate button and check all only if there are checkable items
	if (checkableCount > 0) {
		$('#'+listType+"_allSelChk").attr('disabled', 0);
		$('#statusChangeBtns').css('display','block');
	} else {
		$('#'+listType+"_allSelChk").attr('disabled', 1);
		$('#statusChangeBtns').css('display','none');
	}
}

/* supporting functions */

function actionJoin(evt) {
	var eActId      = evt.data.id;
	var eBlockType  = evt.data.block_type;
	var eEnrollType = evt.data.e_type;
	var eShowWL     = evt.data.showWL;

	var showTB = 1;
	var eUsrId = (document.getElementById('eUserId') ? document.getElementById('eUserId').value : null);
	var isInternal = (document.getElementById('int') ? document.getElementById('int').value : null);

	if (isInternal) {
		if (eBlockType == 'supportlinc' && eEnrollType == '1' && eShowWL == 1) { // only supporlinc leaders
			createForm('waitingListForm',ilcProductSuiteRelPath+'/lms/vc_waiting_list.pl','WaitingList',
				{'activity_id':eActId,'user_id':eUsrId,'int':isInternal});
			openWinPost('waitingListForm','WaitingList',675,465,'yes');	
		}

		if (eBlockType == 'supportlinc' && eEnrollType == '0' && eShowWL == 1) { // only supporlinc users
			showTB = 0;
		}

		joinSession(eActId, '', showTB, 0);
	} else {
		document.location = ilcProductSuiteRelPath+'/lms/vc_launch.pl?activity_id='+eActId;
	}
}

function sortByStatusDown(a,b) {
	var x = statusObj[a.pStLt].title.toLowerCase();
	var y = statusObj[b.pStLt].title.toLowerCase();
	return ((x < y) ? -1 : ((x > y) ? 1 : sortByNameDown(a,b)));
}
function sortByStatusUp(a,b) {
	var x = statusObj[b.pStLt].title.toLowerCase();
	var y = statusObj[a.pStLt].title.toLowerCase();
	return ((x < y) ? -1 : ((x > y) ? 1 : sortByNameDown(a,b)));
}
