/*
 * Scripts for callbacks for main system in topapply version 2.0
 */

//vars for use within the site
var idholder;
var staticURL = 'http://V2static.topapply.com';
var recipientHolder = [];
var broadcastMessage;



//dashboard scripts
function updateStatusOnDashBoard(field, interviewId){
	url = "../../../dashboard/dashboard/updatestatus/?statusId=" + field.value + "&interviewId=" + interviewId + "&dashboard=1";
	getNewPage(url, 'interview' + interviewId);
	
	
}

//hult script
function postCallbackSecStatusDate(field, id){
	var dated = $(field + '_year').value + '-' + $(field + '_month').value + '-' + $(field + '_day').value;
	var url = '../../status/updatenotified/?id=' + id + '&date=' + dated;
	getNewPage(url, 'scipoStatusArea' + id);
}

//telesales scripts
function updateReminderTelesales(field,reminderid, id){
		var url = '../../telesales/updatereminder/?id=' + id + '&reminderid=' + reminderid + '&status=' + field.value;
		getNewPage(url, 'actionArea');
}

function updateTelesalesCourse(field, id){
	var url = '../../telesales/updatecourse/?id=' + id + '&course=' + field.value;
	getNewPage(url, 'courseArea');	
}
//message center function

function newRecipientDropDown(field){
	
	var selectCode;
	var breakCode = '';
	if($(field).value != "dist|All"){
		if(recipientHolder.length == 0) {
			$('newRecipients').innerHTML = "<br><br><a href=\"javascript:void(0)\" onClick=\"deleteFromRecipientList(" + $(field).value + ")\"><img border=\"0\" src=\"" + staticURL + "/images/smallcross.gif\" align=\"absmiddle\" alt=\"Delete recipient\" title=\"Delete recipient\"></a> " + arrItems1[$(field).value] + ", ";
		}else{
			$('newRecipients').innerHTML += "<a href=\"javascript:void(0)\" onClick=\"deleteFromRecipientList(" + $(field).value + ")\"><img border=\"0\" src=\"" + staticURL + "/images/smallcross.gif\" align=\"absmiddle\" alt=\"Delete recipient\" title=\"Delete recipient\"></a> " + arrItems1[$(field).value] + ", ";
		}
		recipientHolder.push($(field).value);
		broadcastMessage = 0;
		$(field).value = "Start";
	}else{
		$('newRecipients').innerHTML = "<br><br>Broadcast to all admin users";
		$(field).value = "Start";
		broadcastMessage = 1;
		recipientHolder = [];
	}
}

function deleteFromRecipientList(id){
	var holder = [];
		for(var i = 0;i < recipientHolder.length;i++){
			if(recipientHolder[i] != id) holder.push(recipientHolder[i])
		}
		recipientHolder = [];
		for(var i = 0;i < holder.length;i++){
			recipientHolder.push(holder[i])
		}
		$('newRecipients').innerHTML = '';
		
		if(recipientHolder.length > 0) $('newRecipients').innerHTML = "<br><br>";
		for(var i = 0;i < recipientHolder.length;i++){
			$('newRecipients').innerHTML += "<a href=\"javascript:void(0)\" onClick=\"deleteFromRecipientList(" + recipientHolder[i] + ")\"><img border=\"0\" src=\"" + staticURL + "/images/smallcross.gif\" align=\"absmiddle\" alt=\"Delete recipient\" title=\"Delete recipient\"></a> " + arrItems1[recipientHolder[i]] + ", ";
		}
}

function createNewMessage(formId, urlCall, area, callBackId){
	if(broadcastMessage == 1){
		$('recipientIds').value = 'broadcast';
	}else{
		$('recipientIds').value = recipientHolder.join();
	}
	var showmsg = 0;
	var msg = 'The following fields are empty.\n';
	if($('recipientIds').value == ''){
		showmsg = 1;
		msg += 'Recipients\n';
	}
	if($('f_messageSubject').value == ''){
		showmsg = 1;
		msg += 'Subject\n';
	}
	if($('f_messageText').value == ''){
		showmsg = 1;
		msg += 'Message\n';
	}
	if(showmsg == 1){
		alert(msg);
	}else{
	postCallback(formId, urlCall, area, callBackId);
	}
}

function addReplyRecipients(id){
	recipientHolder = [];
	var holder = id.split(",");
	for(var i = 0;i < holder.length;i++){
		recipientHolder.push(holder[i]);
	}
	deleteFromRecipientList(99999999);
}

/*
 * interview javascript
 */

function updateInterviewStatus(type, field,interviewId,callBackId, applicantId){
	if(type == 'prospect' || type == 'schedule'){
		url = '../../../interviews/candidateinterview/updatestatus/?prospect=1&applicantId=' + applicantId + '&span=' + callBackId + '&interviewId=' + interviewId + '&statusId=' + field.value;	
	}else{
	url = '../../interviews/candidateinterview/updatestatus/?applicantId=' + applicantId + '&span=' + callBackId + '&interviewId=' + interviewId + '&statusId=' + field.value;
	}
	new Ajax.Updater(callBackId, url, { method: 'get', evalScripts: true});
	
	//reallocate slot?
	if(field.value==4 || field.value == 3){
		var panel = "interviewupdatereallocate" + applicantId;
		
		if(type == 'prospect' || type == 'schedule'){
			url = '../../../interviews/candidateinterview/updatestatusreallocate/?prospect=1&applicantId=' + applicantId + '&span=' + panel + '&interviewId=' + interviewId + '&statusId=' + field.value;	
		}else{
			url = '../../interviews/candidateinterview/updatestatusreallocate/?applicantId=' + applicantId + '&span=' + panel + '&interviewId=' + interviewId + '&statusId=' + field.value;
		}
		
		new Ajax.Updater(panel, url, { method: 'get', evalScripts: true});
	}
}


/*
 * The following function deal with the ajax calls
 */
//standard POST call
function postCallback(formId, urlCall, area, callBackId){
	var holder = $(callBackId).value;
	$(callBackId).value = 'Updating...';
	new Ajax.Updater(area, urlCall, { method: 'post', parameters: $(formId).serialize(true), evalScripts: true });
	$(callBackId).value = holder;
}

//periodical checker for ajax
function getnewPageTimed(url, callBackId, time, decayer){
	new Ajax.PeriodicalUpdater
	(callBackId, url,   {     method: 'get',  frequency: time,     decay: decayer,  evalScripts: true   });
}

function stopUpdater(){
	Ajax.PeriodicalUpdater.stop();
}

function standardPostCallback(formId, urlCall, area){
	new Ajax.Updater(area, urlCall, { method: 'post', parameters: $(formId).serialize(true), evalScripts: true });
}

//satndard GET call used from most links with  in site
function getNewPage(url, callBackId){
	//alert(callBackId)
	new Ajax.Updater(callBackId, url, { method: 'get', evalScripts: true});
	
}

function getNewPageSetTarget(url, callBackId, field){
	//alert(callBackId)
	var urlNew = url + '&type=' + field.value;
	new Ajax.Updater(callBackId, urlNew, { method: 'get', evalScripts: true});
	
}

function getNewPageRotman(url, callBackId, fields){
	//alert(callBackId)
	url = '&type=' + fields.value;
	new Ajax.Updater(callBackId, url, { method: 'get', evalScripts: true});
	
}

//callback from button and to include fields from site and change button
function getNewPageFields(url, callBackId, button, fields){

	$(button).value = 'Updating...';
	var holder = fields.split("|");
	var params = '';		
			for(var i = 0; i < holder.length; i++ ){
				params += "&" + holder[i] + "=" + document.getElementById(holder[i]).value;
				
			}
	url += params;

	new Ajax.Updater(callBackId, url, { method: 'get', evalScripts: true});
	$(button).value = 'Update'
}

//callback from button and to include fields from site and change button
function getNewPageFieldsNoButton(url, callBackId, fields){

	var holder = fields.split("|");
	var params = '';		
			for(var i = 0; i < holder.length; i++ ){
				params += "&" + holder[i] + "=" + document.getElementById(holder[i]).value;
				
			}
	url += params;

	new Ajax.Updater(callBackId, url, { method: 'get', evalScripts: true});
}



//GET from a form call
function getCallback(fields, callBackId, controller, action){
	
	$('actions_functions').innerHTML = '<div class="actions clear"><p class="sort-by"><img src="' + staticURL + '/images/spinner.gif" align="absmiddle"> Updating Results...</p></div>'
	var holder = fields.split("|");
	var params = '';		
			for(var i = 0; i < holder.length; i++ ){
				params += "&" + holder[i] + "=" + document.getElementById(holder[i]).value;
			}
	
	var url = '../../' + controller + '/' + action + '/?profile=1' + params;
	new Ajax.Updater(callBackId, url, { method: 'get', evalScripts: true});
	
}



//process multi update of reminders
function reminderCheckBoxUpdate(type, callBackId, checkboxes, dropdown){
	$(callBackId).innerHTML = '<div style="width:100%;height:200px;text-align:center;vertical-align:center;font-size:10pt"><br><br><br><br><br><br><img src="' + staticURL + '/images/spinner.gif" align="absmiddle"> &nbsp;&nbsp;&nbsp;Updating Reminders...</div>';
		alert(checkboxes);
		for(var i = 0; i < $(checkboxes).length; i++){
			//alert($(checkboxes).value)
		}
}

//process multi update of reminders
function messageMultiDelete(){
		var deleted = 'deleteMessage'
		alert(document.getElementById(deleted).value)
		//for(var i = 0; i < $('deleteMessage').length; i++){
		//	alert($('deleteMessage').value)
		//}
}

//performs search on database from search page
function performSearch(formId, urlCall, area, callBackId){
	//alert(urlCall);
	$('actions_functions').innerHTML = '<div class="actions clear"><p class="sort-by"><img src="' + staticURL + '/images/spinner.gif" align="absmiddle"> Updating Results...</p></div>'
	$(callBackId).value = 'Searching...';
	new Ajax.Updater(area, urlCall, { method: 'post', parameters: $(formId).serialize(true), evalScripts: true });
	$(callBackId).value = 'Search >';
	TabbedPanels1.showPanel(0);
}

//do a search using the saved search field
function getSavedSearch(field){
	//alert(field.value)
	var url = "../../results/getsearch/?reset=1&id=" + field.value;
	$('actions_functions').innerHTML = '<div class="actions clear"><p class="sort-by"><img src="' + staticURL + '/images/spinner.gif" align="absmiddle"> Updating Results...</p></div>'
	new Ajax.Updater('Results', url, { method: 'get', evalScripts: true});
	$('99_savedsearch').value = 0;
	TabbedPanels1.showPanel(0);
}

//do a search using the group function
function getGroupSearch(field){
	//alert(field.value)
	var url = "../../results/getgroup/?reset=1&group_id=" + field.value;
	$('actions_functions').innerHTML = '<div class="actions clear"><p class="sort-by"><img src="' + staticURL + '/images/spinner.gif" align="absmiddle"> Updating Results...</p></div>'
	new Ajax.Updater('Results', url, { method: 'get', evalScripts: true});
	$('99_groupsearch').value = 0;
	TabbedPanels1.showPanel(0);
}

function getGroupSearchLink(field){
	//alert(field.value)
	var url = "../../results/getgroup/?reset=1&group_id=" + field;
	$('actions_functions').innerHTML = '<div class="actions clear"><p class="sort-by"><img src="' + staticURL + '/images/spinner.gif" align="absmiddle"> Updating Results...</p></div>'
	new Ajax.Updater('Results', url, { method: 'get', evalScripts: true});
	$('99_groupsearch').value = 0;
	TabbedPanels1.showPanel(0);
}
//do a search using the group link

//used when deleting an item and refreshing a list on the page
function getNewPageConfirmdelete(url, callBackId){
	
	if(confirm('Are you sure you wish to delete this item')){
		new Ajax.Updater(callBackId, url, { method: 'get', evalScripts: true});
	}
	
}

// no idea
function getResults(url, callBackId){
	
	new Ajax.Updater(callBackId, url, { method: 'get', evalScripts: true});
	TabbedPanels1.showPanel(0);
}

// print front sheet for selected applicants from result page
function printFrontSheetBatch(){
	idholder = selectedID.join(',');
	var url_print = "../../../custom/stgallen/printfrontsheet/?id=" + idholder;
	window.open(url_print);
}

//manages interaction with dropdown list for actioning selected records in search results
function manageSelectField(showNoOfTabs){
	showTabNo = (typeof(showNoOfTabs) != 'undefined' && showNoOfTabs) ? showNoOfTabs : false;
    if($('SelectField').value == 0){
		alert('You must choose an option from the drop down list')
	}else if($('SelectField').value == 1){
		viewmultiple(0, 999999, 1, showTabNo);
	}
	else if($('SelectField').value == 2){
		viewall(0, 999999, 1, showTabNo);
	}
	else if($('SelectField').value == 7){
		if (selectedID.length == 0) {
			alert('You have not selected any records');
		}
		else {
			getNewPage('../../results/group/?type=1&full=0', 'searchmanagement');
		}
	}
	else if($('SelectField').value == 8){
		getNewPage('../../results/group/?type=2&full=0', 'searchmanagement');
	}
	else if($('SelectField').value == 9){
		getNewPage('../../results/savesearch/', 'searchmanagement');
	}
	
	else if($('SelectField').value == 10){
		if (selectedID.length == 0) {
			alert('You have not selected any records');
		}
		else {
			getNewPage('../../results/allocaterecruiter/', 'searchmanagement');
		}
	}
	
	else if($('SelectField').value == 16){
		if (selectedID.length == 0) {
			alert('You have not selected any records !!!!');
		}
		else {
			printFrontSheetBatch();
		}
	}
	
	else if($('SelectField').value == 13){
		if (selectedID.length == 0) {
			alert('You have not selected any records');
		}
		else {
			idholder = selectedID.join(',');
			var urlCall = "&id=" + idholder
			getNewPage('../../results/archive/?type=1' + urlCall, 'searchmanagement');
		}
	}
	

	else if($('SelectField').value == 14){
		if (selectedID.length == 0) {
			alert('You have not selected any records');
		}
		else {
			idholder = selectedID.join(',');
			var urlCall = "&id=" + idholder
			getNewPage('../../results/archive/?type=2' + urlCall, 'searchmanagement');
		}
	}
	
	
	else if($('SelectField').value == 11){
		if (selectedID.length == 0) {
			alert('You have not selected any records');
		}
		else {
			getNewPage('../../watchlist/multiallocate/', 'searchmanagement');
		}
	}
	
	else if($('SelectField').value == 6){
		idholder = allID.join(',');
			//var urlCall = "&idToUse=" + idholder
			location.href = '../../../reporting/ggsb/extraction/?buffering=on&complete=1&selectType=2&selectGroup=0&selectCourse=0&usesessionsql=1';
	}
	
	else if($('SelectField').value == 15){
		idholder = allID.join(',');
			//var urlCall = "&idToUse=" + idholder
			location.href = '../../../reporting/tisch/extraction/?buffering=on&complete=1&selectType=2&selectGroup=0&selectCourse=0&usesessionsql=1';
	}
}
/*
 * GROUP FUNCTIONS
 */
//checks if the dropdown in saving group has asked for a new name

function checkgroupdropdown(field, type, target, id){

	if(field.value == 999999){
		if (type == 3) {
			getNewPage('../../applicant/callbackreturn/?showarea=other&full=1&type=' + type + '&layout=callback&id=' + id, target);
		}else{
			getNewPage('../../results/group/?type=' + type + '&full=1&id=' + id, target);
		}
	}
}

function postCallbackGroup(formId, urlCall, area, callBackId){
	//alert(area);
	if($('grouptype').value == 1){
		idholder = selectedID.join(',');
		urlCall += "&id=" + idholder
	}
	$(callBackId).value = 'Updating...';
	new Ajax.Updater(area, urlCall, { method: 'post', parameters: $(formId).serialize(true), evalScripts: true });
}

//deal with allocating recruiters
function postCallbackAllocate(formId, urlCall, area, callBackId){
	//alert(area);
	idholder = selectedID.join(',');
	urlCall += "?id=" + idholder
	$(callBackId).value = 'Updating...';
	new Ajax.Updater(area, urlCall, { method: 'post', parameters: $(formId).serialize(true), evalScripts: true });
}

//function allocating a recruiter to an individual
function updateRecruiter(field, id, target){
	var url = "../../applicant/updaterecruiter/?id=" + id + "&recruiter=" + field.value + "&complete=1"
	getNewPage(url, target)
}


//marketing source update
function updateMarketingSource(field, id, target){
	var url = "../../marketing/updatemarketingsourcereturn/?id=" + id + "&source=" + field.value;
	getNewPage(url, target)
}


/*
 * next functions deal with the viewing and selecting of multiple records in search results
 */

//shows a single record in tab view
function normalview(name,id,type){
$('appdetails0').innerHTML = '<div class="actions clear"><p class="sort-by" style="text-align:center;font-size:10pt"><img src="' + staticURL + '/images/spinner.gif" align="absmiddle"> Finding applicant</p></div>';
$('app0name').innerHTML = name;
Element.show('tabdetails0');
var url = '../../applicant/index/?layout=Applicant&id=' + id + '&type=' + type;
getNewPage(url, 'appdetails0')
TabbedPanels1.showPanel(5);

}


function showtab(name,id,type,showNoOfTabs){
	
	var showpanel = (typeof(showNoOfTabs) != 'undefined' && showNoOfTabs)  ? showNoOfTabs : 5;
	if(type == 5){ showpanel = 1;}
	var show = 0;
	var found = 0;
	for (x=0;x<tabcheck.length;x++){
		if(tabcheck[x] == id){
			show = 1;
			found = 1;
			TabbedPanels1.showPanel(x + showpanel);
			break;
		}
		
	}
	if(found == 0){
		for (x=0;x<tabcheck.length;x++){
			if(tabcheck[x] == 0){
				
				show = 1;
				$('app' + x  + 'name').innerHTML = name;
				Element.show('tabdetails'+ x);
				
				TabbedPanels1.showPanel(x + showpanel);
				var url = '../../applicant/index/?layout=Applicant&id=' + id + '&type=' + type;
			    var target = 'appdetails'+ x;
			    getNewPage(url, target)
			    tabcheck[x] = id;  
				break;
			}
		}
	}
		
	if(show == 0){
		alert("All tabs are open. \n You need to close an applicant details before you may open another tab.")
	}
   

}

//this function will make the function above showtab() to be deprecated
function showtabApplicant(name,id,type){
	
	var showpanel = 5;
	if(type == 5){ showpanel = 1;}
	var show = 0;
	var found = 0;
	for (x=0;x<tabcheck.length;x++){
		if(tabcheck[x] == id){
			show = 1;
			found = 1;
			TabbedPanels1.showPanel(x + showpanel);
			break;
		}
		
	}
	if(found == 0){
		for (x=0;x<tabcheck.length;x++){
			if(tabcheck[x] == 0){
				
				show = 1;
				$('app' + x  + 'name').innerHTML = name;
				Element.show('tabdetails'+ x);
				
				TabbedPanels1.showPanel(x + showpanel);
				var url = '../../../applicants/details/index/?layout=Applicant&id=' + id + '&type=' + type;
			    var target = 'appdetails'+ x;
			    getNewPage(url, target)
			    tabcheck[x] = id;  
				break;
			}
		}
	}
		
	if(show == 0){
		alert("All tabs are open. \n You need to close an applicant details before you may open another tab.")
	}
   

}




function closetab(tab){

Element.hide('tabdetails'+tab);
$('appdetails' + tab).innerHTML = '';	 
setTimeout("TabbedPanels1.showPanel(0)",5);
	
tabcheck[tab] = 0;

}

function showtabsingle(name,id,type,tsm){
	
	var found = 0;
	   if(tabcheck[0] == id){
			found = 1;
			TabbedPanels1.showPanel(1);
		}
	if(found == 0){
				show = 1;
				$('app0name').innerHTML = name;
				Element.show('tabdetails0');
				TabbedPanels1.showPanel(1);
				var url = '../../telesales/getindividual/?id=' + id;
				if(tsm)
					url += '&tsm=true';
			    var target = 'appdetails0';
			    getNewPage(url, target)
			   tabcheck[0] = id;  
	}

}

//used to select or deslect IDS of records in to the selectedID javascript variable
function selectApplicantIds(chosenField){
	 	
	 	if(chosenField.checked == true){
			var s = 0;
			for(var h=0;h<selectedID.length;h++){
				if(selectedID[h] == chosenField.value){
					s = 1;
					break;
				}
			}
			if (s == 0) {
				selectedID.push(chosenField.value)
			}
		}else{
			for(var h=0;h<selectedID.length;h++){
				if(selectedID[h] == chosenField.value){
					selectedID.splice(h,1);
					break;
				}
			}
		}
		
}

function selectApplicantIdsDirect(chosenField){
 	
 	selectedID.push(chosenField)
}

//select all checkboxes on a form with the same name
//same as above but from select all check box. If check box is unselected it empties the ids from the selectedID var
function selectallcheckboxes(fieldname, records){
if (records > 0){	
		for(var i = 1;i <= records;i++){
			document.getElementById(fieldname + i).checked = true;
			selectApplicantIds(document.getElementById(fieldname + i))
		}
	} else {	
	document.getElementById(fieldname + records).checked = true;

	}
	
}

//sets up the id variable for the multi watchlist allocation
function watchlistMultiCallback(formId, urlCall, area, callBackId){
	idholder = selectedID.join(',')
	urlCall = urlCall + '?id=' + idholder;
	//alert(urlCall)
	$(callBackId).value = 'Updating...';
	new Ajax.Updater(area, urlCall, { method: 'post', parameters: $(formId).serialize(true), evalScripts: true });
	
}
//sets up variable to send to the results action then calls the function to select individual candidate  within the selectedID var
function viewmultiple(position, list, check,showNoOfTabs){
	multiShowTabNo = (typeof(showNoOfTabs) != 'undefined' && showNoOfTabs) ? showNoOfTabs : false;
	if(check == 1 && selectedID.length == 0) {
		alert('You have not selected any records');
	}
	else {
		idholder = selectedID.join(',')
		multipleview(position, selectedID.length, 1, list, multiShowTabNo)
	}
}

function viewmultiplePassed( ids){
	
	["John", "Bob", "Sue"]
		idholder = ids.join(',')
		multipleview(0, ids.length, 1, 999999)

}


function viewall(position, list, check, showNoOfTabs){
    multiViewShowPanelAll = (typeof(showNoOfTabs) != 'undefined' && showNoOfTabs) ? showNoOfTabs : false;
	if (check == 1 && allID.length == 101) {
		alert('You have either not found any records\n or the records found exceeds the 100 limit on this function');
	}
	else {
		idholder = allID.join(',')
		multipleview(position, allID.length, 0, list, multiViewShowPanelAll)
	}
}

//sends url to action script to get each individual record based in position in the selectedID array
function multipleview(position, number, type, list, showNoOfTabs){
	multiViewShowPanel = (typeof(showNoOfTabs) != 'undefined' && showNoOfTabs) ? showNoOfTabs : 5;
	$('appdetails0').innerHTML = '<div class="actions clear"><p class="sort-by" style="text-align:center;font-size:10pt"><img src="' + staticURL + '/images/spinner.gif" align="absmiddle"> Finding applicant</p></div>';
	$('app0name').innerHTML = number + ' Records';
	Element.show('tabdetails0');
	var url = '../../applicant/index/?rand=' + list + '&type=' + type + 'layout=Applicant&id=' + idholder +'&position=' + position;
	getNewPage(url, 'appdetails0')
	TabbedPanels1.showPanel(multiViewShowPanel);
}

//sends url to action script to get each individual record based in position in the selectedID array a hack until main area rewritten
function multipleviewDirect(position, number, type, list, ids){
	$('appdetails0').innerHTML = '<div class="actions clear"><p class="sort-by" style="text-align:center;font-size:10pt"><img src="' + staticURL + '/images/spinner.gif" align="absmiddle"> Finding applicant</p></div>';
	$('app0name').innerHTML = number + ' Records';
	Element.show('tabdetails0');
	var url = '../../applicant/index/?rand=' + list + '&type=' + type + 'layout=Applicant&id=' + ids +'&position=' + position;
	getNewPage(url, 'appdetails0')
	TabbedPanels1.showPanel(5);
}


//gets an applicant direct used for drop down when mutliple applicants selected
function applicantDirect(list, check){
	//alert(list);
	var id = $('multiapplicantValues').value;
	viewmultiple(id, list, check);
}

function applicantDirectAll(){
	var id = $('multiapplicantValues').value;
	viewall(id);
}


//deselects all named checkboxes
function deselectallcheckboxes(fieldname, records){
if (records > 0){	
		for(var i = 1;i <= records;i++){
			document.getElementById(fieldname + i).checked = false;
			selectApplicantIds(document.getElementById(fieldname + i))
		}
	} else {	
	document.getElementById(fieldname + records).checked = false;

	}

}

//decides wether to use deslect or select checkboxes
function select_deselectall(records, checkedfield, fieldname){
if (checkedfield.checked == true){
	selectallcheckboxes(fieldname, records);	

}else{
		deselectallcheckboxes(fieldname, records);

}
}

//Tab Functions









function clearArea(id){

	$(id).innerHTML = '';
}

function clearIframeParent(id){
	parent.document.getElementById(id).innerHTML = '';
}
/*
 * gets the email message that will go with a status change and populates the field.
 */
function getStatusMessage(area,id, values){
	$(area).innerHTML = 'Retrieving message...';
	var url = '../../applicant/getstatusmessage/?statusid=' + values.value + '&id=' + id;
	//alert(url)
	new Ajax.Updater(area, url, { method: 'get', evalScripts: true});

}
//get free rooms
function getFreeRoom(area,id, values){
	$(area).innerHTML = 'Retrieving seats...';
	var url = '../../exam/getroomselect/?roomID=' + values.value + '&id=' + id;
	//alert(url)
	new Ajax.Updater(area, url, { method: 'get', evalScripts: true});
}

function getRoomAllocation(area, section, venue, room){
	$(area).innerHTML = 'Retrieving rooms...';
	var url = '../../eventmanagement/getfreerooms/?venueId=' + venue.value + '&sectionId=' + section + '&room=' + room;
	new Ajax.Updater(area, url, { method: 'get', evalScripts: true});
}

//change row colors for status table
function changeRowColor(row){
	//alert($('newTable').rows.length)
	
	for(var i = 1; i <= $('newTable').rows.length - 1; i++ ){
		//alert('1')
		//$("statrow" + i).style.backgroundColor = '#FFFFFF';	
	}
	
	if (row > 0) {
		//$("statrow" + row).style.backgroundColor = '#CCCCCC';
	}
	
}

//show courselist if status attached to only one
function showStatusCourses(course){
	if($('courses').value == 1){
		getNewPage('../../tools/coursecheckboxes/?id=' + course, 'choosecourses');
	}else{
		clearArea('coursecheckboxarea')
	}
}

//this is school specific stuff
//ROTMAN
function updateCommissionStatus(id, field){
	getNewPage('../../rotman/updateappcommissionstatus/?id=' + id + '&status=' + field.value, 'rotmanStatusArea' + id);
}

function updateTotal(fields, totalField){
	var total = parseInt(0);
	var holder = fields.split("|");
	for(var i = 0; i < holder.length;i++){
		total = (parseInt(total) + parseInt($(holder[i]).value));
	}
	$(totalField).innerHTML = total;
}

//science po specific

function updateSecondStatus(field, id){
	getNewPage('../../status/updatesecondstatus/?id=' + id + '&status=' + field.value, 'scipoStatusArea' + id);
}

//wmt specific
function selectAllWMTCandidates(fieldname, records){
 	if (records > 0){	
		for(var i = 0;i < records;i++){
			document.getElementById(fieldname + i).checked = true;
		}
	} else {	
	document.getElementById(fieldname + records).checked = true;
	}
}

//clear spans
function clearspanTimed(span, time){
	setTimeout('document.getElementById(\'' + span + '\').innerHTML = \'\'', time);
}

//functions for dialog boxes

function checkGroupForm(groupid, newgroup){
	show = 0;
	msg = "You must complete the following fields";
	if(groupid == '0'){ show = 1; msg += '\n Group'; }
	if(groupid == 'new' && newgroup == ''){ show = 1; msg += '\n New group'; }
	if(show == 1){ alert(msg) }else{ submitForm('applicants/tag', 'create-group', 'staticDialog_content', 'mainTaggedApplicantForm') }
}
