function StartJobPostList() {
	var objMyRules = {
		'#RecruitersJobsJobPostListActionSelectBox' : function(objElement) {
			addEvent(objElement,'change',JobPostFormSubmit);
		}		
	};	
	Behaviour.register(objMyRules);	
	Behaviour.apply();
	
	var arrElements = document.getElementsBySelector('table.RecruitersJobsJobPostList tbody');
	var objTimeFrameElement = document.getElementById('TimeFrame');
	
	if(objTimeFrameElement != null){
		var strTimeFrame = objTimeFrameElement.value;		
		for(var i = 0;i < arrElements.length; i++){
			for(var j = 0;j < arrElements[i].childNodes.length;j++) {
				if(arrElements[i].childNodes[j].nodeType == 1) {
					var objLIElement = arrElements[i].childNodes[j];				
					var strParams = "intJobPostId="+objLIElement.id.split('-')[1]+"&strTimePeriod="+strTimeFrame;
					SendHTTPRequest("GET", "/Recruiters/GetStatsBeta.php", strParams, ShowStats);
				}			
			}		
		
		}
	}
	 
}

function JobPostFormSubmit(objEvent) {
	var bolAllJobs = document.getElementById('SelectAllItems').value;
	var arrCheckBoxElements = document.getElementsBySelector('input.ResourcesCommonGeneralListCheckBox');
	var intSelectedJobs = 0;
	
	for(var i = 0;i < arrCheckBoxElements.length; i++) {
		if(arrCheckBoxElements[i].checked == true) {
			intSelectedJobs++;
		}
	}
	
	if(intSelectedJobs == 0) {
		alert('Please select at least one job advert before applying an action.');
		document.getElementById('DefaultActionSelection').selected = true;
		return false;
	}
		
	var Action = document.getElementById('RecruitersJobsJobPostListActionSelectBox').value;
	
	if(Action == 'Resend') {
		HumanReadableAction = 'resend email applications for'
	} else {
		HumanReadableAction = Action.toLowerCase();
	}
	
	if(bolAllJobs == 1) {
		var ConfirmMessage = 'Are you sure you wish to '+HumanReadableAction+' all active jobs?';
	} else {
		var ConfirmMessage = 'Are you sure you wish to '+HumanReadableAction+' '+intSelectedJobs+' jobs?';
	}
	var bolConfirm = confirm(ConfirmMessage);
	if(bolConfirm == true) {
		document.getElementById('ResourcesCommonGeneralListActionForm').submit();
	} else {
		document.getElementById('DefaultActionSelection').selected = true;
	}

}

function ShowStats(objStats) {
	var strCellId  				= "Row"+objStats['intJobPostId']+"-Cell4";
	var objHTMLElement			= document.getElementById(strCellId);

	objHTMLElement.innerHTML	= "<div class='ResourcesCommonGeneralListItem'>Job Views: "+objStats['intJobViews']+"</div> <div class='ResourcesCommonGeneralListItem'>Applications: <a href=\"/Run.php?Module=Recruiters&Section=Jobs&Resource=ApplicationList&Context=HTML&Action=SearchApplications&Arguments=["+objStats['intJobPostId']+"]\">"+objStats['intJobApplications']+"</a></div>";
}


AddPageLoadFunction(StartJobPostList);

