
// handle Filter response on Career board page
function handleFilterResponse(xml)
{
	window.location.href = '#';
	
	// reset jobs count in dropdown list
	resetJobCounter(disciplineList);
	resetJobCounter(locationList);
	resetJobCounter(titleList);
	resetJobCounter(employmentList);

	// for backend only
	if ('backend' == boardType) {
		resetJobCounter(priorityList);
		resetJobCounter(consultantList);
		resetJobCounter(employerList);
	}
	
	// count jobs in a specified discipline and location
	$("discipline", xml).each(function() {
		var key = $("discipline_id", this).text();
		if ('' != key && null != disciplineList[key]) {
			disciplineList[key]['count'] = $("discipline_count", this).text();
		}
	});
	
	$("location", xml).each(function() {
		var key = $("location_id", this).text();
		if ('' != key && null != locationList[key]) {
			locationList[key]['count'] = $("location_count", this).text();
		}
	});
	
	$("title", xml).each(function() {
		var key = $("title_id", this).text();
		if ('' != key && null != titleList[key]) {
			titleList[key]['count'] = $("title_count", this).text();
		}
	});
	
	$("employment", xml).each(function() {
		var key = $("employment_id", this).text();
		if ('' != key && null != employmentList[key]) {
			employmentList[key]['count'] = $("employment_count", this).text();
		}
	});

	// for backend only
	if ('backend' == boardType) {	
		
		$("priority", xml).each(function() {
			var key = $("priority_id", this).text();
			if ('' != key && null != priorityList[key]) {
				priorityList[key]['count'] = $("priority_count", this).text();
			}
		});

		$("consultant", xml).each(function() {
			var key = $("consultant_id", this).text();
			if ('' != key && null != consultantList[key]) {
				consultantList[key]['count'] = $("consultant_count", this).text();
			}
		});

		$("employer", xml).each(function() {
			var key = $("employer_id", this).text();
			if ('' != key && null != employerList[key]) {
				employerList[key]['count'] = $("employer_count", this).text();
			}
		});
	}
			
	// get jobs found count
	var total = $("total", xml).text();
	if ('' == total) {
		total = '0';
	}
				
	// update jobs found count
	$("#result #counter").html(total);

	// get jobs id set
	var jobIDSet = $("jobidset", xml).text();
	$("#idset")[0].value = jobIDSet;
	
	var jobListing = '<table id="career_table" class="tablesorter" border="0" cellpadding="0" cellspacing="1">';  
	var featuredCount = 0; 	
	
	// get jobs count
	var counter = $("counter", xml).text();
	if (counter > 0) {
		// for backend only
		if ('backend' == boardType) {
			jobListing = '<div><div id="highPriority"> = ' + $("in_two_weeks_msg", xml).text() + '</div><div id="priority"> = ' + $("priority_msg", xml).text() + '</div></div><div style="clear: both;"></div>' + jobListing;
		}
		
		// construct table header
		jobListing += "<thead><tr>";
		var columnIndex = 0;
		$("column", xml).each(function() {
			// for backend only
			if ('backend' != boardType) {
				if (columnIndex == 0){ 
					jobListing += '<th class=\"header headerSortDown\">' + $(this).text() + '</th>';
				} else {
					jobListing += '<th>' + $(this).text() + '</th>';
				}

			} else {
				switch (columnIndex) {
					case 0:
						currentCol = '<th class="header headerSortDown">' + $(this).text() + '</th>';
						break;
					case 1:
						currentCol = '<th class="ref">' + $(this).text() + '</th>';
						break;
						
					case 2:
						currentCol = '<th class="title">' + $(this).text() + '</th>';
						break;
					
					case 5:
						currentCol = '<th class="views">#</th>';
						break;
						
					case 7:
						currentCol = '<th class="action">' + $(this).text() + '</th>';
						break;
						
					
					default:
						currentCol = '<th>' + $(this).text() + '</th>';
						break;
				}
				jobListing += currentCol;		
				//alert(columnIndex + ' : ' + currentCol);
			}
			columnIndex++;						
		});
		jobListing += '</tr></thead>';
		  
		// construct table body
		jobListing += '<tbody>';
		$("job", xml).each(function() {
			jobListing += '<tr>';
			
			// check if this is a featured job
			var	featuredClass = 'normal';
			if (1 == $("featured", this).text()) {
				featuredClass = 'featured';
				featuredCount++;				
			}

			if (2 == $("featured", this).text()) {
				featuredClass = 'highFeatured';
				featuredCount++;				
			}
			
			// consturct the job link
			if ('backend' != boardType) {
				var linkHref = $("refid", this).text() + '-' + $("id", this).text();
				linkHref = linkHref.replace(/ /g, '-');			
				var jobLink = '<a href="' + linkHref + '">' + $("title", this).text() + '</a>';
				
				//jobListing += '<td class="' + featuredClass + '">' + $("date", this).text() + '</td>';
				jobListing += '<td>' + $("date", this).text() + '</td>';
				jobListing += '<td>' + $("refid", this).text() + '</td>';
				jobListing += '<td class="cellTitle">' + jobLink + '</td>';
				jobListing += '<td>' + $("discipline", this).text() + '</td>';
				jobListing += '<td>' + $("location", this).text() + '</td>';
			} else {
				var id = $("id", this).text();
				var linkHref = '/adminUser/career/view/' + id;
				linkHref = linkHref.replace(/ /g, '-');			
				var jobLink = '<a href="' + linkHref + '">' + $("title", this).text() + '</a>';
				
				// check if we need to employer name
				var employer = $("employer", this).text();
				if ('' != employer) {
					jobLink += ' (' + employer + ')';
				}
				
				jobListing += '<td class="' + featuredClass + '">' + $("date", this).text() + '</td>';
				jobListing += '<td class="ref">' + $("refid", this).text() + '</td>';
				jobListing += '<td class="cellTitle">' + jobLink + '</td>';
				jobListing += '<td>' + $("discipline", this).text() + '</td>';
				jobListing += '<td>' + $("location", this).text() + '</td>';
				jobListing += '<td class="center">' + $("views", this).text() + '</td>';
				jobListing += '<td>' + $("consultant", this).text() + '</td>';
				
				// for Status column
				var active = $("active", this).text();
				var activeLink = id;
				if (1 == active) {
					activeLink += ':deactive';
					activeLink = '<a class="active" name="' + activeLink + '" href="#">' + '&nbsp;&nbsp;' + '</a>';
				} else {
					activeLink += ':active';
					activeLink = '<a class="deactive" name="' + activeLink + '" href="#">' + '&nbsp;&nbsp;' + '</a>';
				}
				
				activeLink = '<span class="cellStatus" id="status">' + activeLink + '</span>';
				jobListing += '<td id="' + id + '">' + activeLink + '&nbsp;|&nbsp;';
				
				// for Edit column
				var editLink = '/adminUser/career/edit/id/' + id;
				editLink = '<a class="editLinkIcon" href="' + editLink + '">' + '&nbsp;&nbsp;' + '</a>';
				jobListing += editLink + '&nbsp|';
				var deleteLink = '/adminUser/career/delete/id/' + id;
				deleteLink = '<a class="deleteLinkIcon" href="' + deleteLink + '" onclick="return alertDelete()">' + '&nbsp;&nbsp;' + '</a>';
				jobListing += deleteLink;
				jobListing += '</td>';		
			}

			jobListing += '</tr>';
		});
		jobListing += '</tbody>';
		paginator = $("paginator", xml).text();
	} else {
		// job no found
		var errorMessage = $("error", xml).text();
		jobListing += "<tr><td>" + errorMessage + "</tr></td>";
		paginator = '';     	
	}
	jobListing += '</table>';

	// update job listing and paginator
	$("#itemlist").html(jobListing);
	$(".pagination").html(paginator);
	
	// check if we need to show priority icon
	if (featuredCount <= 0) {
		$("#itemlist #priority").addClass("hide");
		$("#itemlist #highPriority").addClass("hide");
	}
	
	$("#career_table").tablesorter({
		// striping looking
		widgets: ['zebra']	
	});
	
	// set table sorter for listing
	registerClickEvent();
	
	// re-populate dropdown discipline and location list
	repopulateList();
			
	// remove mask layer and sort table
	$("#shade").removeClass("mask");  
}
