/* core functions */

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

	if (eventType == null)
		eventType = document.getElementById("div_view").value;

	// switch highlighted tab
	var eventTypeStrs = {reg:'Register',join:'Join'};
	var eventTypeLong = eventTypeStrs[eventType];
	if (!$('#main'+eventTypeLong).hasClass('selected')) {
		var typeAry = [ 'Register','Join' ];
		for (var i in typeAry) {
			var jEl = $('#main'+typeAry[i]+'Btn');
			if (eventTypeLong.indexOf(typeAry[i]) == -1) {
				jEl.removeClass('selected');
			} else {
				jEl.addClass('selected');
			}
		}

		// switch instructional text
		$('#instr_reg').css('display', (eventType == 'reg' ? 'inline' : 'none') );
		$('#instr_join').css('display', (eventType == 'join' ? 'inline' : 'none') );
	}

	// set the current list type
	var eventTypeEl = document.getElementById('div_view');
	eventTypeEl.value = eventType;

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

	printListMsg(listType, 1, '');

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

	// if eUserID is blank check to see if external event_user_id is set
	var eUserId = (document.getElementById('eUserId') ? document.getElementById('eUserId').value :
 		(document.getElementById('event_user_id') ? document.getElementById('event_user_id').value : null) )

	// get internal (int) and personal public page settings
	var isInternal = (document.getElementById('int') ? document.getElementById('int').value : 0);
	var ppOwnerId  = (document.getElementById('pp_owner_id') ? document.getElementById('pp_owner_id').value : '');
	var ppUserName = (document.getElementById('pp') ? document.getElementById('pp').value : '');
	var sId = (document.getElementById('sid') ? document.getElementById('sid').value : 0);

	var ajaxObj = new ilincAjax('public_getListItems', 'public_getListItemsCallback');
	ajaxObj.addParam('listType', listType);
	ajaxObj.addParam('eventType', eventType);
	ajaxObj.addParam('eUserId', eUserId);
	ajaxObj.addParam('isInternal', isInternal);
	if (ppOwnerId)
		ajaxObj.addParam('ppOwnerId', ppOwnerId);
	if (ppUserName)
		ajaxObj.addParam('pp', ppUserName);
	if (sId)
		ajaxObj.addParam('eParentActivityId',sId);
	ajaxObj.addParam('selStatus', 'active');
	if (document.getElementById(listType+'_selSearch').value != ilcTrans(4466,'Search')) ajaxObj.addParam('selSearch', document.getElementById(listType+'_selSearch').value);
	if (sort) ajaxObj.addParam('sortBy', sort[0]);
	if (sort) ajaxObj.addParam('sortDir', sort[1]);
	ajaxObj.send();
}

function public_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:  632, title:'Open', style:'stat-launch'}
}

function printListItems(pageNum, listType) {
	// clear and hide select all message div
	$('#'+listType+'_SelectAllMsg').empty().hide();

	var eventType = document.getElementById('div_view').value;
	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');

	/* track personal public page owner */
	var ppUserName = (document.getElementById('pp') ? document.getElementById('pp').value : '');
	var evtUserId = (document.getElementById('event_user_id') ? document.getElementById('event_user_id').value : '');

	var selTable = document.getElementById(listType+'_listTableBody');
	var chkAry = new Array();
	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")
			.css('text-align', "center");

		// join/reg checkbox column
		if (eventType == 'reg') {
			if (listObj[listType].itemAry[i].pStLt == 'r' || listObj[listType].itemAry[i].pStLt == 'd') {
				var val = getSelItemValue(listObj[listType].itemAry,i).value;
				var chk = document.createElement("input");
				$(chk)
					.attr('type', "checkbox")
					.attr('id', listType+"_"+listObj[listType].itemAry[i].id+"SelChk")
					.attr('value', val)
					.bind('click', {'id':listObj[listType].itemAry[i].id,'listType':listType}, function(evt) { selItemChecked(evt); });
				newTd.appendChild(chk);
				// need to do this after all appends for IE
				if (listObj[listType].selItemObj[val])
					chkAry.push(chk);
	
				checkableCount++;
			}
		} else { // for join
			var linkText;
			var sizeByType;
			if (listObj[listType].itemAry[i].au_type.match(/ll_/)) {
				linkText = ilcTrans(2348,"Join");
				sizeByType = 'sync';
			} else {
				linkText = ilcTrans(623,"Open");
				sizeByType = 'async';
			}

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

		if (! listObj[listType].selItemObj[val])
			checkAll = 0;

		// 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,'eventType':eventType,'isInternal':isInternal,'ppUserName':ppUserName,'evtUserId':evtUserId}, 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,'eventType':eventType,'isInternal':isInternal,'ppUserName':ppUserName,'evtUserId':evtUserId}, 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;

	for (var i in chkAry) {
		$(chkAry[i]).attr("checked","checked");
	}

	$('#'+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');
	}
}

function getSelItemValue(tempAry, itemNumber) {
	var itemAry = tempAry;
	var i = itemNumber;

	var val = itemAry[i].id;

	return {'value':val};
}

/* supporting functions */

function actionPublicJoin(evt) {
	var eActId      = evt.data.id;
	var eSizeByType = evt.data.sizeByType;
	var eBlockType  = evt.data.block_type;
	var eEnrollType = evt.data.e_type;
	var eShowWL     = evt.data.showWL;
	var eWinWidth   = evt.data.winWidth;
	var eWinHeight   = evt.data.winHeight;

	var showTB = 1;
	var eUsrId = $('#eUserId').val();
	var isInternal = $('#int').val();

	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});
		openWinPost('waitingListForm','WaitingList',675,465,'yes');	
	}

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

	if (isInternal && eSizeByType != 'async') {
		//joinSession(eActId, '', showTB, 0);
		var jObj = { aID:eActId, wodLevel:'', tb:showTB, noWin:0 };
		joinSession(jObj);
	} else {
		var win = joinEvent(eActId, eUsrId, eSizeByType, isInternal, eWinWidth, eWinHeight);
		if (eWinWidth == 1 && eWinHeight == 1) {
			win.moveTo(0,0);
			win.resizeTo(screen.availWidth,screen.availHeight);
		}
	}
}

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)));
}
