// User Level Control Messages.

function upgrade(msg) {
	alert('This feature is not available to you on your current subscription level.');
	return;
	if (msg) {
		if (confirm(msg)) {
			document.location = '/cgi-bin/signup.pl';
		}
	} else {
		if (confirm('This feature is not available to you on your current subscription level. Press OK to upgrade or Cancel to carry on.')) {
			document.location = '/cgi-bin/signup.pl';
		}
	}
}

function notInBeta() {
	alert('This feature is not implemented for Beta.');
}

// Does we does or does we don't take access ?? '

var isNav = (navigator.appName.indexOf("Netscape") != -1);
var isIE = (navigator.appName.indexOf("Microsoft") != -1);
var isOpera = (navigator.appName.indexOf("Opera") != -1);
var isMac = (navigator.appVersion.indexOf("Macintosh") != -1);

// 4th generation browsers. Doncha just love em? Grrrr.

// See if this browser supports array.push ...

var test = new Array;
var noPush = true;
if (test.push) noPush = false;

// A small wrapper to simulate the push method for old browsers
function myArray() {
	if (noPush) {
		var i = null;
		var a = new Array;
		a.push = function() {
			var i;
			for(i=0;i<arguments.length;i++) {
				this[this.length] = arguments[i];
			}
		};
		for(i=0;i<arguments.length;i++) {
			a[a.length] = arguments[i];
		}
		return a;
	} else {
		var a = new Array;
		for(i=0;i<arguments.length;i++) {
			a[a.length] = arguments[i];
		}
		return a;
	}
}

// An array to hold the preloaded image objects
// Initialise it here to give old slow browsers time before we use it
var imageArray = new myArray;

// Some magic to simulate getElementById for old browsers
// nda = no DOM Array, nd = no DOM

// nda = the object whose properties are those elements with an id string
var nda;
// no_nda = do we still need to initialise the nda object
var no_nda = true;

// constructor function for the nda object
function nd() {
	var i = null;

	if (isIE) {
		imgs = document.all;
	} else {
		imgs = document.images;
	}
	for(i=0;i<imgs.length;i++) {
		if (imgs[i].id) {this[imgs[i].id] = imgs[i]}
	}
	no_nda = false;
}

var noDOM = true;
if (document.getElementById) {
	noDOM = false;
} else {
	document.getElementById = function(id) {
		if (no_nda) nda = new nd();
		return nda[id];
	};
}

// End of wrappers for dain bramaged gen4 slappers

/*
 * Most form submission happens as the result of pressing a button
 * which invokes some javascript eventually resulting in f.submit().
 * This means we need some magic for onSubmit processing. Here it is.
 * Any onSubmit handler needs to be assigned in JavaScript as an anonymous
 * function which accepts a single object as an argument. This argument has
 * 4 properties: o.ret which is set to true to indicate the form should be
 * submitted, false if not. The second property is o.msg, which is the error
 * message displayed when o.ret is false. The third property allows us to
 * merely decline to submit the form without giving a reason, perhaps because
 * the reason has been given already as the result of a
 * "return(confirm('Are you sure'))" type thingy. The fourth will cause any
 * uploading / updating div to be made visible if set to true.
 */

function ret() {this.ret = ''; this.msg = 'Please check the values in the form and try again.'; this.showMsg = true; this.showUploadDiv = false; }

/* 
 * As remarked, forms are generally submitted by invoking f.submit(). This
 * means there is no handy submit parameter which indicates that the form
 * has actually been submitted, rather than it just being a call to show
 * the first page in the process. For this we use the 'submitme' parameter.
 * This function below sets submitme if appropriate, but also does the
 * onSubmit processing too.
 *
 * The second of these 2 functions is identical to the first, but it doesn't
 * set the f.submitme parameter to 1. Use it when you want form validation
 * but don't want the submitme parameter.
 *
 * NB: Keep these two functions synchronised eg if the ret object changes
 */

function formSubmit() {
	var f=document.surveyForm;
	var to = typeof f.onsubmit;
	if (to == 'function') {
		var r = new ret();
		f.onsubmit(r);
		if (r.ret) {
			f.submitme.value = 1;
			f.submit();
			if (r.showUploadDiv) {
				showUploadDiv();
			}
		} else {
			if (r.showMsg) {
				alert(r.msg);
			}
		}
	} else {
		f.submitme.value = 1;
		f.submit();
	}
}

function formSubmitNoSM() {
	var f=document.surveyForm;
	var to = typeof f.onsubmit;
	if (to == 'function') {
		var r = new ret();
		f.onsubmit(r);
		if (r.ret) {
			f.submit();
			if (r.showUploadDiv) {
				showUploadDiv();
			}
		} else {
			if (r.showMsg) {
				alert(r.msg);
			}
		}
	} else {
		f.submit();
	}
}

function showUploadDiv(n) {
	// n is the name of the div to display, for those pages where we have 2
	// if n is not passed in, it will default to 'uploading'
	if (n == '' || n == null) {
		n = 'uploading';
	}
	var u = document.getElementById(n);
	var p = document.getElementById('page');
	if (u && p) {
/*
		if (!isOpera) {
			p.style.visibility = 'hidden';
		}
*/
		p.style.height = 0;
		p.style.overflow = 'hidden';
		window.scrollTo(0,0);
		u.style.visibility = 'visible';
	}
}

// The form cancel function. Just sets the cancel input to true and submits
// the form.

function formCancel() {
	f=document.surveyForm;
	f.cancel.value = 1;
	f.submit();
}

function customFormCancel() {
	f=document.surveyForm;
	f.action = "/sarge";
	f.cancel.value = 1;
	f.target = "_self";
	f.submit();
}

/*
 * Utility functions
 */

function padZeroLeft(n,l) {
	// Left pads a number with zeroes to the required length
	var n1 = parseInt(n);
	var n2 = n;
	if (n1 >= 0 && n1 < Math.pow(10,l)) {
		n2 = n.toString();
		while (n2.length < l) n2='0'+n2;
	}
	return n2;
}

/*
 * A number of functions to manipulate form controls.
 * Some of these are identical, but I like to avoid manipulating checkboxes
 * by calling a function called clickRadio ;-) Also a selected dropdown
 * option needs to have the selected attribute true, not the checked
 * attribute.
 */

// Selects a checkbox. Used to init previous answers.

function clickCheckbox(c,v) {
	// c = name of the checkbox group, v = value of option to be clicked
	var a,i;
	var a = eval('document.surveyForm.'+c);
	if (a.length) {
		for(i=0;i<a.length;i++) {
			if (a[i].value == v) a[i].checked = true;
		}
	} else {
		a.checked = true;
	}
}

// Selects the required option in a drop down. Used to init previous answers.

function clickDropdown(d,v) {
	// d = name of the dropdown element, v = value of option to be clicked
	var a,i;
	eval('a = document.surveyForm.'+d);
	if (a.length) {
		for(i=0;i<a.length;i++) {
			if (a[i].value == v) a[i].selected = true;
		}
	} else {
		a.checked = true;
	}
}

// Two functions to manipulate radio button groups. Note the difference in
// the first argument of each. For clickRadio, supply a radio button group
// name, for getRadioValue, supply an array of radio objects.

// Auto click a radio button if any of it's dependent options are used. '
// Requires the onChange attr to be set for those dependents

function clickRadio(r,v) {
	// r = name of the radio group, v = value of option to be clicked
	var a;
	eval('a = document.surveyForm.'+r);
	if (a == null) return;
	if (a.length) {
		for(i=0;i<a.length;i++) {
			if (a[i].value == v) {
				a[i].checked = true;
				previous[a[i].name] = a[i].id;
			}
			stateName[a[i].id] = a[i].name;
			stateValue[a[i].id] = a[i].value;
			state[a[i].id] = a[i].checked;
		}
	} else {
		a.checked = true;
	}
}

// Returns the value of the currently selected radio button, if any

function getRadioValue(r) {
	// r = array of radio objects
	var i;
	for(i=0;i<r.length;i++) {
		if (r[i].checked == true) return r[i].value;
	}
}

function isRadioGroupChecked(r) {
	// r = array of radio objects
	var i;
	for(i=0;i<r.length;i++) {
		if (r[i].checked == true) return 1;
	}
	return 0;
}

// General test for number-ness of a value

function isNotNum(val) {
	if (isNaN(parseInt(val,10)) || parseInt(val,10) != val) {
		return true;
	} else {
		return false;
	}
}

// Sets the direction when a next / previous button is pushed

function qnNav(dirn,jmp_pid) {
	f=document.surveyForm;
	
	if (jmp_pid) {
		if (isNotNum(jmp_pid)) return;
	}
	
	f.dirn.value = dirn;
	if (jmp_pid) f.jmp_pid.value = jmp_pid;
	if (f.already_submitted) {
		return;
	}
	if (dirn == 'bwd') {
		formSubmit();
		f.already_submitted = 1;
	} else if (dirn == 'finish') {
		formSubmit();
		f.already_submitted = 1;
	} else {
		if (checkRequiredFields()) {
			formSubmit();
			f.already_submitted = 1;
		}
	}
}

/*
 * Button Drivers
 * ==============
 *
 * The next umpteen functions are all invoked by buttons in various pages.
 * They are run by JavaScript because this allows us to only have one form,
 * and we can set the action of the form depending on the button pushed,
 * rather than having to have one form for each button.
 *
 * NB These button drivers submit the form. There are 3 ways to do it:
 * formSubmit() - does onsubmit processing and sets submitme param on success
 * formSubmitNoSM() - same but no submitme parameter
 * f.submit() - no onsubmit processing, no submitme parameter
 */

/* Functions specific to individual whitelabels */

/* IIP */

function iipHome(id, version) {
	var f = document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'show_projects';
	f.version.value = version;
	f.target = '_self';
	f.project_id.value = id;
	f.submit();
}

function iipDataCapturePDF(id, version) {
	var f = document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'data_capture_pdf';
	f.version.value = version;
	f.project_id.value = id;
	f.submit();
}

function iipViewResults(prjid, version) {
	f=document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'iip_view_results';
	f.project_id.value = prjid;
	if (version == 2) {
		f.target = '_self';
	} else {
		f.target = '_blank';
	}
	f.submit();
}

function iipResultFilters(version) {
	f=document.surveyForm;
	f.action = '/sarge';
	if (version == 2) {
		f.target = '_self';
	} else {
		f.target = '_blank';
	}
	f.command.value = 'iip_result_filters';
	f.submit();
}

function iipDownloadReport() {
	f=document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'iip_download_report';
	f.submit();
}

function iipDownloadAdminReport() {
	f=document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'iip_download_admin_report';
	f.submit();
}

function iipDeleteProject(prjid) {
	f=document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'iip_delete_project';
	f.project_id.value = prjid;
	f.submit();
}

function iipTogglePaused(prjid) {
	f=document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'iip_toggle_paused';
	f.project_id.value = prjid;
	f.submit();
}

function iipToggleClosed(prjid) {
	f=document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'iip_toggle_closed';
	f.project_id.value = prjid;
	f.submit();
}

function iipDataCapture(prjid) {
	f=document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'data_capture';
	f.project_id.value = prjid;
	f.submit();
}

function iipInvitations(prjid) {
	f=document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'invitation_management';
	if (prjid) f.project_id.value = prjid;
	f.submit();
}

function iipRemoveEmailFromQueue(prjid) {
	f=document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'iip_remove_email_from_queue';
	f.project_id.value = prjid;
	f.submit();
}

function iipManageContactList() {
	f=document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'manage_contact_list';
	f.submit();
}

function iipAddContacts() {
	f=document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'add_contacts';
	f.submit();
}

function iipExportContacts() {
	f=document.surveyForm;
	var cmd = f.command.value;
	f.action = '/sarge';
	f.command.value = 'export_contacts';
	f.submit();
	f.command.value = cmd;
}

function iipDeleteContact(ab_eid) {
	f=document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'delete_contact';
	f.ab_eid.value = ab_eid;
	f.submit();
}

function iipClearResult(ab_eid) {
	f=document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'iip_clear_results_by_respondent';
	f.note.value = 'invitation_management';
	f.ab_eid.value = ab_eid;
	f.submit();
}

function iipEditContact(ab_eid) {
	f=document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'edit_contact';
	f.ab_eid.value = ab_eid;
	f.submit();
}

function iipDeleteAllContacts() {
	f=document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'delete_all_contacts';
	//f.submit();
}

function iipRecsPerPageAddressBook(r) {
	f=document.surveyForm;
	f.recs_per_page.value = r;
	f.command.value = 'iip_set_recs_per_page_address_book';
	f.action = '/sarge';
	f.submit();
}

function iipPageAddressBook(dirn) {
	f=document.surveyForm;
	f.command.value = 'iip_page_' + dirn + '_address_book';
	f.action = '/sarge';
	f.submit();
}

function iipFindRecipients() {
	f=document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'iip_find_recipients';
	f.submit();
}

function iipSendEmailToLinks() {
	f=document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'iip_send_email_to_links';
	f.submit();
}

function iipPreviewEmail() {
	f=document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'iip_preview_email';
	f.submit();
}

function iipViewDistributionList() {
	f=document.surveyForm;
	f.command.value = 'view_distribution_list';
	f.action = '/sarge';
	f.submit();
}

function iipExportReminderList() {
	f=document.surveyForm;
	var cmd = f.command.value;
	f.command.value = 'iip_export_reminder_list';
	f.action = '/sarge';
	f.submit();
	f.command.value = cmd;
}

function iipExportDistributionList() {
	f=document.surveyForm;
	var cmd = f.command.value;
	f.command.value = 'export_distribution_list';
	f.action = '/sarge';
	f.submit();
	f.command.value = cmd;
}

function iipClearResultsByRespondent(ab_eid) {
	f=document.surveyForm;
	f.ab_eid.value = ab_eid;
	f.command.value = 'iip_clear_results_by_respondent';
	f.action = '/sarge';
	f.submit();
}

function iipViewSavedSelections() {
	f=document.surveyForm;
	f.command.value = 'view_distribution_list';
	if (f.view_saved_selections.value == 1) {
		f.view_saved_selections.value = 0;
	} else {
		f.view_saved_selections.value = 1;
	}
	f.action = '/sarge';
	f.submit();
}

function iipRecsPerPageEmailDetail(r) {
	f=document.surveyForm;
	f.recs_per_page.value = r;
	f.command.value = 'iip_set_recs_per_page_email_detail';
	f.action = '/sarge';
	f.submit();
}

function iipPageEmailDetail(dirn) {
	f=document.surveyForm;
	f.command.value = 'iip_page_' + dirn + '_email_detail';
	f.action = '/sarge';
	f.submit();
}

function iipReminderEmail(select_how) {
	f=document.surveyForm;
	f.command.value = 'iip_compose_reminder';
	if (select_how && f.select_recipients) f.select_recipients.value = select_how;
	f.action = '/sarge';
	f.submitme.value = 0;
	f.submit();
}

function iipPreviewReminder() {
	f=document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'iip_preview_reminder';
	f.submit();
}

/* /IIP */

/* Sodexho */

function sdxhReportPicker() {
	f=document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'report_picker';
	f.submit();
}

function sdxhReportSchedule() {
	f=document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'report_schedule';
	f.submit();
}

function sdxhCommitToSchedule() {
	f=document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'commit_to_schedule';
	f.submit();
}

function sdxhDeleteFromSchedule() {
	f=document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'delete_from_schedule';
	f.submit();
}

function sdxhCommitToDistribute() {
	f=document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'commit_to_distribute';
	f.submit();
}

function sdxhAddRecipient(stid) {
	f=document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'sdxh_add_recipient';
	f.stid.value = stid;
	f.submit();
}

function sdxhDeleteRecipient(rrid) {
	f=document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'sdxh_delete_recipient';
	f.rrid.value = rrid;
	f.submit();
}

function sdxhEditUser(uid) {
	f=document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'sdxh_user_maint';
	f.target_uid.value = uid;
	f.submit();
}

/* /Sodexho */

/* STAP */

function stapReminder(ab_emid) {
	f=document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'diagnostic_reminder';
	f.ab_emid.value = ab_emid;
	f.submit();
}

function stapDownloadReport(qnid) {
	f=document.surveyForm;
	f.action = '/customreports';
	f.command.value = 'report_index';
	f.qnid.value = qnid;
	f.submit();
}

function stapEdit360Recipients(qnid) {
	f=document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'edit_360_recipients';
	f.qnid.value = qnid;
	f.submit();
}

function stapEdit360Recipient(ab_eid) {
	f=document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'edit_360_recipient';
	f.ab_eid.value = ab_eid;
	f.submit();
}

function stapAdd360Recipients(qnid) {
	f=document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'add_360_recipient';
	f.qnid.value = qnid;
	f.submit();
}

function stapToggle360Candidate(ab_eid,sense) {
	f=document.surveyForm;
	if(confirm('Are you sure you want to '+sense+' this recipient?')) {
		f.command.value = 'toggle_360_candidate';
		f.ab_eid.value = ab_eid;
		f.action = '/sarge';
		f.submit();
	}
}

function stapDelete360Recipient(ab_eid) {
	f=document.surveyForm;
	if(confirm('Are you sure you want to delete this recipient?')) {
		f.command.value = 'delete_360_recipient';
		f.ab_eid.value = ab_eid;
		f.action = '/sarge';
		f.submit();
	}
}

function stapCommit360(qnid) {
	f=document.surveyForm;
	if (confirm('Commit will send your questionnaire to your selected recipients. This is the point of no return. Do you wish to continue?')) {
		f.action = '/sarge';
		f.command.value = 'commit_360';
		f.qnid.value = qnid;
		f.submit();
	}
}

function stapEdit360Email(qnid) {
	f=document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'edit_360_email';
	f.qnid.value = qnid;
	f.submit();
}

function stapEdit360Settings(qnid) {
	f=document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'edit_360_settings';
	f.qnid.value = qnid;
	f.submit();
}

function changeQnType() {
	f=document.surveyForm;
	f.action = '/sarge';
	f.submit();
}

/* /STAP */

/* HSE javascript button drivers */
/* HSE */

function hseHome(id, version) {
	var f = document.surveyForm;
	f.action = '/hseuser';
	f.command.value = 'show_projects';
	f.version.value = version;
	f.target = '_self';
	f.project_id.value = id;
	f.submit();
}

function hseProjectOptions(id, version) {
	var f = document.surveyForm;
	f.action = '/hseuser';
	f.command.value = 'hse_show_options';
	f.version.value = version;
	f.target = '_self';
	f.project_id.value = id;
	f.submit();
}

function hseAdmin(id, version) {
	var f = document.surveyForm;
	f.action = '/admin';
	f.command.value = 'hse_admin_tools';
	f.version.value = version;
	f.target = '_self';
	f.project_id.value = id;
	f.submit();
}

function hseDataCapturePDF(id, version) {
	var f = document.surveyForm;
	f.action = '/hseuser';
	f.command.value = 'hse_data_capture_pdf';
	f.version.value = version;
	f.project_id.value = id;
	f.submit();
}

function hseViewResults(prjid, version) {
	f=document.surveyForm;
	f.action = '/hseuser';
	f.command.value = 'hse_view_results';
	f.project_id.value = prjid;
	f.target = '_self';
	f.submit();
}

function hseEditCategories(prjid, rids) {
	f=document.surveyForm;
	if (rids > 0) {
		var okay = confirm("You can edit your categories if you wish.\n\nIf you do this ALL your responses for this project WILL BE CLEARED.\n\n!!!This is a permanent action and can not be reversed!!!\n\nDo you really want to do this, click OK to continue or Cancel or abort this process?");
		if (okay == true) {
			f.action = '/hseuser';
			f.command.value = 'hse_category_builder';
			f.project_id.value = prjid;
			f.mode.value = 'protected';
			f.target = '_self';
			f.submit();
		} else {
			alert("Process aborted. Click OK to continue.");
		}
	} else {
		f.action = '/hseuser';
		f.command.value = 'hse_category_builder';
		f.project_id.value = prjid;
		f.target = '_self';
		f.submit();
	}

}

function hseResultFilters(version) {
	f=document.surveyForm;
	f.action = '/hseuser';
	f.target = '_self';
	f.command.value = 'hse_result_filters';
	f.submit();
}

function hseDownloadReport() {
	f=document.surveyForm;
	f.action = '/hseuser';
	f.command.value = 'hse_download_report';
	f.submit();
}

function hseDownloadAdminReport() {
	f=document.surveyForm;
	f.action = '/hseuser';
	f.command.value = 'hse_download_admin_report';
	f.submit();
}

function hseDeleteProject(prjid) {
	f=document.surveyForm;
	f.action = '/hseuser';
	f.command.value = 'hse_delete_project';
	f.project_id.value = prjid;
	f.submit();
}

function hseTogglePaused(prjid) {
	f=document.surveyForm;
	f.action = '/hseuser';
	f.command.value = 'hse_toggle_paused';
	f.project_id.value = prjid;
	f.submit();
}

function hseToggleClosed(prjid) {
	f=document.surveyForm;
	f.action = '/hseuser';
	f.command.value = 'hse_toggle_closed';
	f.project_id.value = prjid;
	f.submit();
}

function hseShowOptions(prjid) {
	f=document.surveyForm;
	f.action = '/hseuser';
	f.command.value = 'hse_show_options';
	f.project_id.value = prjid;
	f.submit();
}

function hseDataCapture(prjid) {
	f=document.surveyForm;
	f.action = '/hseuser';
	f.command.value = 'hse_data_capture';
	f.project_id.value = prjid;
	f.submit();
}

function hseInvitations(prjid) {
	f=document.surveyForm;
	f.action = '/hseuser';
	f.command.value = 'hse_invitation_management';
	if (prjid) f.project_id.value = prjid;
	f.submit();
}

function hseAccount(mode) {
	f=document.surveyForm;
	var okay = true;
	f.action = '/hseuser';
	f.command.value = 'hse_account';
	f.mode.value = mode;
	if (mode == "update") {
		if (f.password.value != f.reEnterPassword.value) {
			alert("The passwords in both password fields must match.");
			okay = false;
		}
	}
	if (okay == true) f.submit();

}

function hseLogout() {
	f=document.surveyForm;
	f.action = '/hseuser';
	f.command.value = 'logout';
	f.submit();
}

function hseRemoveEmailFromQueue(prjid) {
	f=document.surveyForm;
	f.action = '/hseuser';
	f.command.value = 'hse_remove_email_from_queue';
	f.project_id.value = prjid;
	f.submit();
}

function hseManageContactList() {
	f=document.surveyForm;
	f.action = '/hseuser';
	f.command.value = 'hse_manage_contact_list';
	f.submit();
}

function hseAddContacts() {
	f=document.surveyForm;
	f.action = '/hseuser';
	f.command.value = 'hse_add_contacts';
	f.submit();
}

function hseExportContacts() {
	f=document.surveyForm;
	var cmd = f.command.value;
	f.action = '/hseuser';
	f.command.value = 'hse_export_contacts';
	f.submit();
	f.command.value = cmd;
}

function hseDeleteContact(ab_eid) {
	f=document.surveyForm;
	f.action = '/hseuser';
	f.command.value = 'hse_delete_contact';
	f.ab_eid.value = ab_eid;
	f.submit();
}

function hseEditContact(ab_eid) {
	f=document.surveyForm;
	f.action = '/hseuser';
	f.command.value = 'hse_edit_contact';
	f.ab_eid.value = ab_eid;
	f.submit();
}

function hseDeleteAllContacts() {
	f=document.surveyForm;
	f.action = '/hseuser';
	f.command.value = 'hse_delete_all_contacts';
	f.submit();
}

function hseRecsPerPageAddressBook(r) {
	f=document.surveyForm;
	f.recs_per_page.value = r;
	f.command.value = 'hse_set_recs_per_page_address_book';
	f.action = '/hseuser';
	f.submit();
}

function hsePageAddressBook(dirn) {
	f=document.surveyForm;
	f.command.value = 'hse_page_' + dirn + '_address_book';
	f.action = '/hseuser';
	f.submit();
}

function hseFindRecipients() {
	f=document.surveyForm;
	f.action = '/hseuser';
	f.command.value = 'hse_find_recipients';
	f.submit();
}

function hseComposeEmail() {
	f=document.surveyForm;
	f.action = '/hseuser';
	f.command.value = 'hse_compose_email';
	f.submit();
}

function hseSendEmailToLinks() {
	f=document.surveyForm;
	f.action = '/hseuser';
	f.command.value = 'hse_send_email_to_links';
	f.submit();
}

function hsePreviewEmail() {
	f=document.surveyForm;
	f.action = '/hseuser';
	f.command.value = 'hse_preview_email';
	f.submit();
}

function hseViewDistributionList() {
	f=document.surveyForm;
	f.command.value = 'hse_view_distribution_list';
	f.action = '/hseuser';
	f.submit();
}

function hseExportReminderList() {
	f=document.surveyForm;
	var cmd = f.command.value;
	f.command.value = 'hse_export_reminder_list';
	f.action = '/hseuser';
	f.submit();
	f.command.value = cmd;
}

function hseExportDistributionList() {
	f=document.surveyForm;
	var cmd = f.command.value;
	f.command.value = 'hse_export_distribution_list';
	f.action = '/hseuser';
	f.submit();
	f.command.value = cmd;
}

function hseClearResultsByRespondent(ab_eid) {
	f=document.surveyForm;
	f.ab_eid.value = ab_eid;
	f.command.value = 'hse_clear_results_by_respondent';
	f.action = '/hseuser';
	f.submit();
}

function hseViewSavedSelections() {
	f=document.surveyForm;
	f.command.value = 'hse_view_distribution_list';
	if (f.view_saved_selections.value == 1) {
		f.view_saved_selections.value = 0;
	} else {
		f.view_saved_selections.value = 1;
	}
	f.action = '/hseuser';
	f.submit();
}

function hseRecsPerPageEmailDetail(r) {
	f=document.surveyForm;
	f.recs_per_page.value = r;
	f.command.value = 'hse_set_recs_per_page_email_detail';
	f.action = '/hseuser';
	f.submit();
}

function hsePageEmailDetail(dirn) {
	f=document.surveyForm;
	f.command.value = 'hse_page_' + dirn + '_email_detail';
	f.action = '/hseuser';
	f.submit();
}

function hseReminderEmail(select_how) {
	f=document.surveyForm;
	f.command.value = 'hse_compose_reminder';
	if (select_how && f.select_recipients) f.select_recipients.value = select_how;
	f.action = '/hseuser';
	f.submitme.value = 0;
	f.submit();
}

function hsePreviewReminder() {
	f=document.surveyForm;
	f.action = '/hseuser';
	f.command.value = 'hse_preview_reminder';
	f.submit();
}

function hsePasswordReminder() {
	f=document.loginForm;
	if (f.email.value == "") {
		alert("Please enter your email address in the field labeled 'Email Address' and try again.");
	} else {
		f.action = '/hseuser';
		f.state.value = 'hse_password_reminder';
		f.submit();
	}
}

function hseResendActivationEmail() {
	f=document.loginForm;
	if (f.email.value == "") {
		alert("Please enter your email address in the field labeled 'Email Address' and try again.");
	} else {
		f.action = '/hseuser';
		f.state.value = 'hse_resend_activation';
		f.submit();
	}
}

function hseRegisterCancel() {
	var f=document.registerForm;
	f.action = '/hseuser';
	f.state.value = 'login';
	f.submit();
}

function hseRegister(runAs) {
	var okay = true;
	var f = document.registerForm;
	
	if (runAs == "show") {
		f = document.loginForm;
		if (f.password.value != "") f.password.value = "";
	}
	if (f.mode.value == "process") {
		if (f.name.value == "") {
			alert("Please supply your name.");
			okay = false;
		} else if (f.emailNew.value == "") {
			alert("Please supply an email address - this will be used as your username.");
			okay = false;
		} else if (f.passwordNew.value == "") {
			alert("Please supply a password.");
			okay = false;
		} else if (f.passwordNew.value.length < 6) {
			alert("Your password should be at least 6 characters long.");
			okay = false;
		} else if (f.telephone.value == "") {
			alert("Please supply a telephone number.");
			okay = false;
		} else if (f.companyName.value == "") {
			alert("Please supply your company name.");
			okay = false;
		} else if (f.typeOrg.value == "") {
			alert("Please select the type of organisation you belong to.");
			okay = false;
		} else if (f.reEnterPasswordNew.value != f.passwordNew.value) {
			alert("The passwords in both password fields must match.");
			okay = false;
		}
	}
	f.action = '/hseuser';
	f.state.value = 'hse_register';
	if (okay == true) f.submit();
}

function hseCreateProject(status) {
	var okay = true;
	f=document.surveyForm;
	if (status == "qnid") f.mode.value = "";
	if (f.mode.value == "process") {
		if (f.organisationName.value == "") {
			alert("Please supply an organisation name.");
			okay = false;
		} else if (f.organisationSize.value == "") {
			alert("Please supply and organisation size.");
			okay = false;
		} else if (f.turnover.value == "") {
			alert("Please select a turnover value.");
			okay = false;
		} else if (f.organisationIdentity.value == "") {
			alert("Please supply and organisation identity.");
			okay = false;
		} else if (f.organisationSector.value == "") {
			alert("Please supply and organisation sector.");
			okay = false;
		} else if (f.geographicRegion.value == "") {
			alert("Please supply a geographic region.");
			okay = false;
		} else if (f.advisorName.value == "") {
			alert("Please supply advisor name.");
			okay = false;
		} else if (f.advisorRegNo.value == "") {
			alert("Please supply advisor reg number.");
			okay = false;
		} else if (f.modeOfUse.value == "") {
			alert("Please supply a mode of use.");
			okay = false;
		} else if (f.surveyLimit.value == "") {
			alert("Please supply survey limit.");
			okay = false;
		} else if (f.advisorEmail.value == "") {
			alert("Please supply advisor email address.");
			okay = false;
		}
	}
	f.action = '/hseuser';
	f.state.value = 'hse_create_project';
	if (okay == true) f.submit();
}

/* RWG javascript button drivers */
/* RWG */

function rwgHome(id, version) {
	var f = document.surveyForm;
	f.action = '/rwguser';
	f.command.value = 'show_projects';
	f.version.value = version;
	f.target = '_self';
	f.project_id.value = id;
	f.submit();
}

function rwgAdmin(id, version) {
	var f = document.surveyForm;
	f.action = '/admin';
	f.command.value = 'rwg_admin_tools';
	f.version.value = version;
	f.target = '_self';
	f.project_id.value = id;
	f.submit();
}

function rwgDataCapturePDF(id, version) {
	var f = document.surveyForm;
	f.action = '/rwguser';
	f.command.value = 'rwg_data_capture_pdf';
	f.version.value = version;
	f.project_id.value = id;
	f.submit();
}

function rwgViewResults(prjid, version) {
	f=document.surveyForm;
	f.action = '/rwguser';
	f.command.value = 'rwg_view_results';
	f.project_id.value = prjid;
	f.target = '_self';
	f.submit();
}

function rwgEditCategories(prjid, rids) {
	f=document.surveyForm;
	if (rids > 0) {
		f.mode.value = 'protected';
	} else {
		f.mode.value = '';
	}
	f.action = '/rwguser';
	f.command.value = 'rwg_category_builder';
	f.project_id.value = prjid;
	f.target = '_self';
	f.submit();
}

function rwgResultFilters(version) {
	f=document.surveyForm;
	f.action = '/rwguser';
	f.target = '_self';
	f.command.value = 'rwg_result_filters';
	f.submit();
}

function rwgDemoFilters(version) {
	f=document.surveyForm;
	f.action = '/rwguser';
	f.target = '_self';
	f.command.value = 'rwg_demo_filters';
	f.submit();
}

function rwgDemoClusterFilters() {
	f=document.surveyForm;
	f.action = '/rwguser';
	f.target = '_self';
	f.command.value = 'rwg_demo_cluster_filters';
	f.submit();
}

function rwgDownloadReport() {
	f=document.surveyForm;
	f.action = '/rwguser';
	f.command.value = 'rwg_download_report';
	f.submit();
}

function rwgDownloadAdminReport() {
	f=document.surveyForm;
	f.action = '/rwguser';
	f.command.value = 'rwg_download_admin_report';
	f.submit();
}

function rwgDeleteProject(prjid) {
	f=document.surveyForm;
	f.action = '/rwguser';
	f.command.value = 'rwg_delete_project';
	f.project_id.value = prjid;
	f.submit();
}

function rwgTogglePaused(prjid) {
	f=document.surveyForm;
	f.action = '/rwguser';
	f.command.value = 'rwg_toggle_paused';
	f.project_id.value = prjid;
	f.submit();
}

function rwgToggleClosed(prjid) {
	f=document.surveyForm;
	f.action = '/rwguser';
	f.command.value = 'rwg_toggle_closed';
	f.project_id.value = prjid;
	f.submit();
}

function rwgDataCapture(prjid) {
	f=document.surveyForm;
	f.action = '/rwguser';
	f.command.value = 'rwg_data_capture';
	f.project_id.value = prjid;
	f.submit();
}

function rwgInvitations(prjid) {
	f=document.surveyForm;
	f.action = '/rwguser';
	f.command.value = 'rwg_invitation_management';
	if (prjid) f.project_id.value = prjid;
	f.submit();
}

function rwgAccount(mode) {
	f=document.surveyForm;
	var okay = true;
	f.action = '/rwguser';
	f.command.value = 'rwg_account';
	f.mode.value = mode;
	if (mode == "update") {
		if (f.password.value != f.reEnterPassword.value) {
			alert("The passwords in both password fields must match.");
			okay = false;
		}
	}
	if (okay == true) f.submit();

}

function rwgLogout() {
	f=document.surveyForm;
	f.action = '/rwguser';
	f.command.value = 'logout';
	f.submit();
}

function rwgRemoveEmailFromQueue(prjid) {
	f=document.surveyForm;
	f.action = '/rwguser';
	f.command.value = 'rwg_remove_email_from_queue';
	f.project_id.value = prjid;
	f.submit();
}

function rwgManageContactList() {
	f=document.surveyForm;
	f.action = '/rwguser';
	f.command.value = 'rwg_manage_contact_list';
	f.submit();
}

function rwgAddContacts() {
	f=document.surveyForm;
	f.action = '/rwguser';
	f.command.value = 'rwg_add_contacts';
	f.submit();
}

function rwgExportContacts() {
	f=document.surveyForm;
	var cmd = f.command.value;
	f.action = '/rwguser';
	f.command.value = 'rwg_export_contacts';
	f.submit();
	f.command.value = cmd;
}

function rwgDeleteContact(ab_eid) {
	f=document.surveyForm;
	f.action = '/rwguser';
	f.command.value = 'rwg_delete_contact';
	f.ab_eid.value = ab_eid;
	f.submit();
}

function rwgEditContact(ab_eid) {
	f=document.surveyForm;
	f.action = '/rwguser';
	f.command.value = 'rwg_edit_contact';
	f.ab_eid.value = ab_eid;
	f.submit();
}

function rwgDeleteAllContacts() {
	f=document.surveyForm;
	f.action = '/rwguser';
	f.command.value = 'rwg_delete_all_contacts';
	f.submit();
}

function rwgRecsPerPageAddressBook(r) {
	f=document.surveyForm;
	f.recs_per_page.value = r;
	f.command.value = 'rwg_set_recs_per_page_address_book';
	f.action = '/rwguser';
	f.submit();
}

function rwgPageAddressBook(dirn) {
	f=document.surveyForm;
	f.command.value = 'rwg_page_' + dirn + '_address_book';
	f.action = '/rwguser';
	f.submit();
}

function rwgFindRecipients(page) {
	f=document.surveyForm;
	f.action = '/rwguser';
	f.command.value = 'rwg_find_recipients';
	f.run_as.value = 'compose_email';
	f.submit();
}

function rwgFindReminderRecipients(page) {
	f=document.surveyForm;
	f.action = '/rwguser';
	f.command.value = 'rwg_find_reminder_recipients';
	f.run_as.value = 'compose_reminder';
	f.submit();
}

function rwgComposeEmail() {
	f=document.surveyForm;
	f.action = '/rwguser';
	f.command.value = 'rwg_compose_email';
	f.submit();
}

function rwgSendEmailToLinks() {
	f=document.surveyForm;
	f.action = '/rwguser';
	f.command.value = 'rwg_send_email_to_links';
	f.submit();
}

function rwgPreviewEmail() {
	f=document.surveyForm;
	f.action = '/rwguser';
	f.command.value = 'rwg_preview_email';
	f.submit();
}

function rwgViewDistributionList() {
	f=document.surveyForm;
	f.command.value = 'rwg_view_distribution_list';
	f.action = '/rwguser';
	f.submit();
}

function rwgExportReminderList() {
	f=document.surveyForm;
	var cmd = f.command.value;
	f.command.value = 'rwg_export_reminder_list';
	f.action = '/rwguser';
	f.submit();
	f.command.value = cmd;
}

function rwgExportDistributionList() {
	f=document.surveyForm;
	var cmd = f.command.value;
	f.command.value = 'rwg_export_distribution_list';
	f.action = '/rwguser';
	f.submit();
	f.command.value = cmd;
}

function rwgClearResultsByRespondent(ab_eid) {
	f=document.surveyForm;
	f.ab_eid.value = ab_eid;
	f.command.value = 'rwg_clear_results_by_respondent';
	f.action = '/rwguser';
	f.submit();
}

function rwgViewSavedSelections() {
	f=document.surveyForm;
	f.command.value = 'rwg_view_distribution_list';
	if (f.view_saved_selections.value == 1) {
		f.view_saved_selections.value = 0;
	} else {
		f.view_saved_selections.value = 1;
	}
	f.action = '/rwguser';
	f.submit();
}

function rwgRecsPerPageEmailDetail(r) {
	f=document.surveyForm;
	f.recs_per_page.value = r;
	f.command.value = 'rwg_set_recs_per_page_email_detail';
	f.action = '/rwguser';
	f.submit();
}

function rwgPageEmailDetail(dirn) {
	f=document.surveyForm;
	f.command.value = 'rwg_page_' + dirn + '_email_detail';
	f.action = '/rwguser';
	f.submit();
}

function rwgReminderEmail(select_how) {
	f=document.surveyForm;
	f.command.value = 'rwg_compose_reminder';
	if (select_how && f.select_recipients) f.select_recipients.value = select_how;
	f.action = '/rwguser';
	f.submitme.value = 0;
	f.submit();
}

function rwgPreviewReminder() {
	f=document.surveyForm;
	f.action = '/rwguser';
	f.command.value = 'rwg_preview_reminder';
	f.submit();
}

function rwgPasswordReminder() {
	f=document.loginForm;
	if (f.email.value == "") {
		alert("Please enter your email address in the field labeled 'Email Address' and try again.");
	} else {
		f.action = '/rwguser';
		f.state.value = 'rwg_password_reminder';
		f.submit();
	}
}

function rwgResendActivationEmail() {
	f=document.loginForm;
	if (f.email.value == "") {
		alert("Please enter your email address in the field labeled 'Email Address' and try again.");
	} else {
		f.action = '/rwguser';
		f.state.value = 'rwg_resend_activation';
		f.submit();
	}
}

function rwgRegisterCancel() {
	var f=document.registerForm;
	f.action = '/rwguser';
	f.state.value = 'login';
	f.submit();
}

function rwgRegister(runAs) {
	var okay = true;
	var f = document.registerForm;
	
	if (runAs == "show") {
		f = document.loginForm;
		if (f.password.value != "") f.password.value = "";
	}
	if (f.mode.value == "process") {
		if (f.name.value == "") {
			alert("Please supply your name.");
			okay = false;
		} else if (f.emailNew.value == "") {
			alert("Please supply an email address - this will be used as your username.");
			okay = false;
		} else if (f.passwordNew.value == "") {
			alert("Please supply a password.");
			okay = false;
		} else if (f.passwordNew.value.length < 6) {
			alert("Your password should be at least 6 characters long.");
			okay = false;
		} else if (f.telephone.value == "") {
			alert("Please supply a telephone number.");
			okay = false;
		} else if (f.companyName.value == "") {
			alert("Please supply your company name.");
			okay = false;
		} else if (f.typeOrg.value == "") {
			alert("Please select the type of organisation you belong to.");
			okay = false;
		} else if (f.reEnterPasswordNew.value != f.passwordNew.value) {
			alert("The passwords in both password fields must match.");
			okay = false;
		}
	}
	f.action = '/rwguser';
	f.state.value = 'rwg_register';
	if (okay == true) f.submit();
}

function rwgCreateProject(status) {
	var okay = true;
	f=document.surveyForm;
	if (status == "qnid") f.mode.value = "";
	if (f.mode.value == "process") {
		if (f.modeOfUse.value == "") {
			alert("Please supply a mode of use.");
			okay = false;
		}
	}
	f.action = '/rwguser';
	f.state.value = 'rwg_create_project';
	if (okay == true) f.submit();
}


/* CentralContact */

function createNewContactSchedule() {
	f=document.surveyForm;
	f.actually_do_it.value = 1;
	formSubmit();
}

/* /CentralContact */

/* Stress */

function adminAddCMSCategory() {
	f=document.surveyForm;
	f.command.value = 'cms_add_category';
	formSubmit();
}

function adminToggleCMSCategory(cmscatid) {
	f=document.surveyForm;
	f.command.value = 'cms_toggle_category';
	f.cmscatid.value = cmscatid;
	f.submit();
}

function adminEditCMSCategory(cmscatid) {
	f=document.surveyForm;
	f.command.value = 'cms_edit_category';
	f.cmscatid.value = cmscatid;
	f.submit();
}

function adminDeleteCMSCategory(cmscatid) {
	if (confirm('Are you sure?')) {
		f=document.surveyForm;
		f.command.value = 'cms_delete_category';
		f.cmscatid.value = cmscatid;
		f.submit();
	}
}

function adminEditCMSArticle(cmsartid) {
	f=document.surveyForm;
	f.command.value = 'cms_edit_article';
	f.cmsartid.value = cmsartid;
	f.submit();
}

function adminAuthCMSArticle(cmsartid) {
	f=document.surveyForm;
	f.command.value = 'cms_auth_article';
	f.cmsartid.value = cmsartid;
	f.submit();
}

function adminDeleteCMSArticle(cmsartid) {
	if (confirm('Are you sure?')) {
		f=document.surveyForm;
		f.command.value = 'cms_delete_article';
		f.cmsartid.value = cmsartid;
		f.submit();
	}
}

function adminDownloadCMSArticle(cmsartid) {
	f=document.surveyForm;
	f.command.value = 'cms_download_article';
	f.cmsartid.value = cmsartid;
	f.submit();
}

function toggleCMSCategory(cmscatid) {
	f=document.surveyForm;
	f.command.value = 'cms_toggle_category';
	f.cmscatid.value = cmscatid;
	f.submit();
}

function showCMSArticles(cmscatid) {
	f=document.surveyForm;
	f.command.value = 'cms_show_articles';
	f.submit();
}

/* /Stress */

/* Skillwatch */

function swCITBDrillDown(skill_area) {
	f=document.surveyForm;
	f.skill_area.value = skill_area;
	f.command.value = 'report_custom';
	f.action = '/customreports';
	formSubmit();
}

function swCITBDrillUp() {
	f=document.surveyForm;
	f.command.value = 'report_custom';
	f.drill_up.value = 1;
	f.action = '/customreports';
	formSubmit();
}

/* /Skillwatch */

/* Admin functions start */

function adminExportQn() {
	f=document.surveyForm;
	f.command.value = 'export_survey';
	formSubmit();
}

function adminImportQn() {
	f=document.surveyForm;
	f.command.value = 'import_survey';
	formSubmit();
}

function adminImportExportQn() {
	f=document.surveyForm;
	f.command.value = 'importexport_survey';
	formSubmit();
}

function adminCompareSurveys() {
	f=document.surveyForm;
	f.command.value = 'compare_surveys';
	formSubmit();
}

function adminUpdateQntype() {
	f=document.surveyForm;
	f.command.value = 'update_qntype';
	formSubmit();
}

function adminDeleteQn() {
	f=document.surveyForm;
	if(confirm('Are you sure you want to delete these surveys?')) {
		f.command.value = 'delete_survey';
		f.action = '/admin';
		formSubmit();
	}
}

function adminMoveSurvey() {
	f=document.surveyForm;
	f.command.value = 'move_survey';
	formSubmit();
}

function adminCopySurvey() {
	f=document.surveyForm;
	f.command.value = 'copy_survey';
	formSubmit();
}

function adminCreateUser(uid) {
	f=document.surveyForm;
	f.command.value = 'create_user';
	f.submit();
}

function adminDeleteUser(uid) {
	f=document.surveyForm;
	if(confirm('Delete user: Are you sure?')) {
		f.target_uid.value = uid;
		f.command.value = 'delete_this_user';
		f.action = '/admin';
		f.submit();
	}
}

function adminToggleUserLevel(uid) {
	f=document.surveyForm;
	if(confirm('Change user level: Are you sure?')) {
		f.target_uid.value = uid;
		f.command.value = 'toggle_user_level';
		f.action = '/admin';
		f.submit();
	}
}

function adminToggleSuspended(uid) {
	f=document.surveyForm;
	if(confirm('Change suspended: Are you sure?')) {
		f.target_uid.value = uid;
		f.command.value = 'toggle_suspended';
		f.action = '/admin';
		f.submit();
	}
}

function adminToggleFree(uid) {
	f=document.surveyForm;
	if(confirm('Change free: Are you sure?')) {
		f.target_uid.value = uid;
		f.command.value = 'toggle_free';
		f.action = '/admin';
		f.submit();
	}
}

function adminToggleShowPwrBySS(uid) {
	f=document.surveyForm;
	if(confirm('Change Show Powered By SurveyShack: Are you sure?')) {
		f.target_uid.value = uid;
		f.command.value = 'toggle_pwr_by_ss';
		f.action = '/admin';
		f.submit();
	}
}

function adminToggleExportInfo(uid) { 
	f=document.surveyForm;
	if(confirm('Export full respondent info: Are you sure?')) {
		f.target_uid.value = uid;
		f.command.value = 'full_export_info';
		f.action = '/admin';
		f.submit();
	}
}

function adminToggleCustomData(uid) {
	f=document.surveyForm;
	if(confirm('Show all Custom Data fields: Are you sure?')) {
		f.target_uid.value = uid;
		f.command.value = 'with_custom_data';
		f.action = '/admin';
		f.submit();
	}
}

function adminToggleReseller(uid) {
	f=document.surveyForm;
	if(confirm('Change reseller: Are you sure?')) {
		f.target_uid.value = uid;
		f.command.value = 'toggle_reseller';
		f.action = '/admin';
		f.submit();
	}
}

function adminToggleAdmin(uid) {
	f=document.surveyForm;
	if(confirm('Change admin status: Are you sure?')) {
		f.target_uid.value = uid;
		f.command.value = 'toggle_admin';
		f.action = '/admin';
		f.submit();
	}
}

function adminToggleExpire(uid) {
	f=document.surveyForm;
	if(confirm('Change expiry status: Are you sure?')) {
		f.target_uid.value = uid;
		f.command.value = 'toggle_expire';
		f.action = '/admin';
		f.submit();
	}
}

function adminToggleDowngrade(uid) {
	f=document.surveyForm;
	if(confirm('Change downgrade status: Are you sure?')) {
		f.target_uid.value = uid;
		f.command.value = 'toggle_downgrade';
		f.action = '/admin';
		f.submit();
	}
}

function adminToggleActivated(uid) {
	f=document.surveyForm;
	if(confirm('Change activation status: Are you sure?')) {
		f.target_uid.value = uid;
		f.command.value = 'toggle_activated';
		f.action = '/admin';
		f.submit();
	}
}

function adminRefundProtxPayment(vendortx) {
	f=document.surveyForm;
	if(confirm('Refund payment: Are you sure?')) {
		f.vendortx.value = vendortx;
		f.command.value = 'refund_protx_payment';
		f.action = '/admin';
		formSubmit();
	}
}

function adminLoginAs(url) {
	lwin = window.open(url,'lwin','location=1,menubar=1,resizable=1,scrollbars=1,status=1,titlebar=1,toolbar=1');
	document.adminWin = 1;
}

function adminShowSurveysForUser(uid) {
	f=document.surveyForm;
	f.target_uid.value = uid;
	f.command.value = 'show_surveys_for_user';
	f.action = '/admin';
	f.submit();
}

function adminShowReportClassesForUser(uid) {
	f=document.surveyForm;
	f.target_uid.value = uid;
	f.command.value = 'show_rtype';
	f.action = '/admin';
	f.submit();
}

function adminAddCustomReportRules() {
	f=document.surveyForm;
	f.command.value = 'wl_add_qncrid';
	f.action = '/admin';
	f.submit();
}

function adminDeleteCustomReportRules() {
	f=document.surveyForm;
	f.command.value = 'wl_delete_qncrid';
	f.action = '/admin';
	f.submit();
}

function adminNotifyCustomReportRules(sense) {
	f=document.surveyForm;
	f.command.value = 'wl_notify_qncrid';
	f.sense.value = sense;
	f.action = '/admin';
	f.submit();
}

function adminCreateOffer() {
	f=document.surveyForm;
	f.command.value = 'create_offer';
	f.action = '/admin';
	f.submit();
}

function adminEditOffer(oc) {
	f=document.surveyForm;
	f.target_oc.value = oc;
	f.command.value = 'edit_offer';
	f.action = '/admin';
	f.submit();
}

function adminDeleteOffer(oc) {
	f=document.surveyForm;
	if(confirm('Delete offer code: Are you sure?')) {
		f.target_oc.value = oc;
		f.command.value = 'delete_offer';
		f.action = '/admin';
		f.submit();
	}
}

function adminCreateInvoice(aid) {
	f=document.surveyForm;
	f.target_aid.value = aid;
	f.command.value = 'create_invoice';
	f.action = '/admin';
	f.submit();
}

function adminPreviewInvoice() {
	f=document.surveyForm;
	self.name = 'adwin';
	var winw=window.open('','invoice','width=640,height=480,left=50,top=50,scrollbars=yes,toolbar=no,statusbar=no');
	f.target = 'invoice';
	f.action = '/admin';
	f.submit();
	f.target = 'adwin';
}

function adminInvoicePaid(inv) {
	f=document.surveyForm;
	f.invoice_no.value = inv;
	f.command.value = 'invoice_paid';
	f.action = '/admin';
	f.submit();
}

function adminCreditInvoice(inv) {
	f=document.surveyForm;
	f.invoice_no.value = inv;
	f.command.value = 'credit_invoice';
	f.action = '/admin';
	f.submit();
}

function adminUnCreditInvoice(inv) {
	f=document.surveyForm;
	f.invoice_no.value = inv;
	f.command.value = 'uncredit_invoice';
	f.action = '/admin';
	f.submit();
}

function adminShowInvoicesForUser(uid) {
	f=document.surveyForm;
	f.target_uid.value = uid;
	f.command.value = 'show_invoices_for_user';
	f.action = '/admin';
	f.submit();
}

function adminShowPaymentsForUser(uid) {
	f=document.surveyForm;
	f.target_uid.value = uid;
	f.command.value = 'show_payments_for_user';
	f.action = '/admin';
	f.submit();
}

function adminShowProtxForUser(uid) {
	f=document.surveyForm;
	f.target_uid.value = uid;
	f.command.value = 'show_protx_for_user';
	f.action = '/admin';
	f.submit();
}

function adminRefundPayment(pno) {
	f=document.surveyForm;
	f.payment_no.value = pno;
	f.command.value = 'refund_payment';
	f.action = '/admin';
	f.submit();
}

function adminSubscriberDetailReport(ul,set) {
	f=document.surveyForm;
	f.user_level.value = ul;
	f.set.value = set;
	f.command.value = 'report_subscriber_detail';
	f.action = '/admin';
	f.submit();
}

function adminQnList(type,arg1,arg2,arg3) {
	// type = user ==> arg1 is list of qnids, arg2 is status code (or 'all'), arg3 is uid
	// type = user_level ==> arg1 is user_level, arg2 is status code
	f=document.surveyForm;
	f.command.value = 'report_qn_list';
	if (type == 'user') {
		if (arg1) {
			f.qnids.value = arg1;
			f.scode.value = arg2;
			f.target_uid.value = arg3;
			f.submit();
		} else {
			alert('No qnids');
		}
	} else if (type == 'user_level') {
		f.user_level.value = arg1;
		f.scode.value = arg2;
		f.submit();
	} else {
		alert("Unrecognised list option");
	}
}

function adminUserDetail(uid,email) {
	f=document.surveyForm;
	if (! (email || uid)) {
		alert('No user');
	} else {
		if (uid) f.target_uid.value = uid;
		if (email) f.target_email.value = email;
		f.command.value = 'show_user_detail'
		f.submit();
	}
}

function adminPreviewSurvey(l) {
	f=document.surveyForm;
	winp=window.open('/cgi-bin/s?l='+l,'previewWin','width=700,height=500,left=30,top=20,scrollbars=yes,toolbar=no,statusbar=yes');
}

function adminUserLimits() {
	f=document.surveyForm;
	f.command.value = 'user_limits';
	f.submit();
}

function adminEditWhitelabel(site_id) {
	f=document.surveyForm;
	f.command.value = 'edit_whitelabel';
	f.target_site.value = site_id;
	f.submitme.value = '';
	f.submit();
}

function adminDirectLogin(site_id, uid) {
	f=document.surveyForm;
	f.command.value = 'admin_direct_login';
	f.target_site.value = site_id; 
	f.target_uid.value = uid;
	f.submitme.value = "1";
	f.target = "_blank";
	f.submit();
}

function directLogin(uid) {
	f=document.surveyForm;
	f.command.value = 'direct_login';
	f.target_uid.value = uid;
	f.submitme.value = "1";
	f.target = "_blank";
	f.submit();
}

function directLoginFinder(uid) {
	f=document.surveyForm;
	f.command.value = 'direct_login_finder';
	f.target_uid.value = uid;
	f.submitme.value = "1";
	f.target = "_blank";
	f.submit();
}

function adminShowReportClassesForWhitelabelSurvey() {
	f=document.surveyForm;
	f.command.value = 'wl_report_class_survey';
	formSubmitNoSM();
}

function adminShowReportClassesForWhitelabel() {
	f=document.surveyForm;
	f.command.value = 'wl_rtypes';
	formSubmitNoSM();
}

function adminShowFolderTypesForWhitelabel() {
	f=document.surveyForm;
	f.command.value = 'wl_show_folder_types';
	formSubmitNoSM();
}

function adminShowRtypesForQnid(qnid) {
	f=document.surveyForm;
	f.command.value = 'show_rtypes_for_qnid';
	f.submit();
}

function adminShowDashboardProjectsForWhitelabel() {
	f=document.surveyForm;
	f.command.value = 'wl_show_dashboard_projects';
	formSubmitNoSM();
}

function adminMenuManagementForWhitelabel() {
	f=document.surveyForm;
	f.command.value = 'wl_menu_management';
	formSubmitNoSM();
}

function adminAdminUserForWhitelabel() {
	f=document.surveyForm;
	f.command.value = 'wl_manage_admin_user';
	formSubmitNoSM();
}

function adminDeleteDashboardProject(prjfid) {
	f=document.surveyForm;
	f.command.value = 'wl_delete_dashboard_project';
	f.project_fid.value = prjfid;
	f.submit();
}

function adminBulkResultsImportExport() {
	f=document.surveyForm;
	f.command.value = "survey_data_import_export";
	f.submit();
}
/* Admin functions end */

/* Whitelabel admin functions start */

function wlAdminEditDetailsPw() {
	f=document.surveyForm;
	f.action = '/admin';
	f.command.value = 'edit_details_pw';
	f.submit();
	
}

function wlAdminSetPassword() {
	f=document.surveyForm;
	f.action = '/admin';
	f.command.value = 'edit_user';
	if (f.password.value != "") {
		if (sl == "0") {
			f.new_password.value = f.password.value;
		} else if (sl == "1") {
			f.new_password.value = Sha256.hash(f.password.value, false);
		} else if (sl == "2") {
			f.new_password.value = Sha256.hash(f.password.value, false);
		}
		f.password.value = "";
	}
	f.submitme.value = '1';
   	f.submit();
	
}

function adminWLUserSetPassword() {
	f=document.surveyForm;
	f.action = '/admin';
	f.command.value = 'wl_manage_admin_user';
	if (f.password.value != "") {
		if (sl == "0") {
			f.new_password.value = f.password.value;
		} else if (sl == "1") {
			f.new_password.value = Sha256.hash(f.password.value, false);
		} else if (sl == "2") {
			f.new_password.value = Sha256.hash(f.password.value, false);
		}
		f.password.value = "";
	}
	f.submitme.value = '1';
   	f.submit();
	
} 

function setPassword() {
	f=document.loginForm;
	if (f == null) {
		f=document.surveyForm;
	}
	if (f.enter_password.value != "") {
		if (sl == "0") {
			f.password.value = f.enter_password.value;
		} else if (sl == "1") {
			f.password.value = Sha256.hash(f.enter_password.value, false);
		} else if (sl == "2") {
			f.password.value = Sha256.hash(f.enter_password.value, false);
		} else {
			f.password.value = f.enter_password.value;
		}
		f.enter_password.value = "";
	}
	if (f.enter_confirm_password != null) {
		if (f.enter_confirm_password.value != "") {
			if (sl == "0") {
				f.confirm_password.value = f.enter_confirm_password.value;
			} else if (sl == "1") {
				f.confirm_password.value = Sha256.hash(f.enter_confirm_password.value, false);
			} else if (sl == "2") {
				f.confirm_password.value = Sha256.hash(f.enter_confirm_password.value, false);
			} else {
				f.confirm_password.value = f.enter_confirm_password.value;
			}
			f.enter_confirm_password.value = "";
		}
	}
}

function wlAdminLookNFeel() {
	f=document.surveyForm;
	f.action = '/admin';
	f.command.value = 'look_and_feel';
	f.submit();
}

function wlAdminCreateTemplateCat() {
	f=document.surveyForm;
	f.command.value = 'create_template_cat';
	f.action = '/admin';
	formSubmit();
}

function wlAdminDeleteTemplateCat() {
	f=document.surveyForm;
	f.del_catid.blur();
	f.command.value = 'delete_template_cat';
	f.action = '/admin';
	formSubmit();
}

function wlAdminShowThemes() {
	f=document.surveyForm;
	f.action = '/admin';
	f.method = 'POST';
	f.command.value = 'show_themes';
	f.submit();
}

function wlAdminUpdateThemes() {
	f=document.surveyForm;
	f.action = '/admin';
	f.command.value = 'show_themes';
	formSubmit();
}

function wlAdminCreateTheme() {
	f=document.surveyForm;
	f.action = '/admin';
	f.command.value = 'create_theme';
	formSubmit();
}

function wlAdminEditTheme() {
	f=document.surveyForm;
	f.action = '/admin';
	f.command.value = 'edit_theme';
	formSubmitNoSM();
}

function wlAdminDeleteTheme() {
	f=document.surveyForm;
	f.action = '/admin';
	f.command.value = 'delete_theme';
	formSubmitNoSM();
}

function wlAdminDefaultTheme() {
	f=document.surveyForm;
	f.action = '/admin';
	f.command.value = 'default_theme';
	formSubmitNoSM();
}

function wlAdminAllocateThemes() {
	f=document.surveyForm;
	f.action = '/admin';
	f.command.value = 'allocate_themes';
	formSubmitNoSM();
}

function wlAdminAllocateTemplates() {
	f=document.surveyForm;
	f.action = '/admin';
	f.command.value = 'allocate_templates';
	formSubmitNoSM();
}

function wlAdminEditUser(uid) {
	f=document.surveyForm;
	f.action = '/admin';
	f.target_uid.value = uid;
	f.command.value = 'edit_user';
	f.submitme.value = "";
	formSubmitNoSM();
}

function wlAdminShowResponses() {
	f=document.surveyForm;
	f.action = '/admin';
	f.command.value = 'show_responses';
	formSubmit();
}
/* Whitelabel admin functions end */

/* User functions start */

function clickIt(url) {
	if (! url) return;
	document.location = url;
}

function crosstab() {
	var f=document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'crosstab';
	f.submit();
}

function exportCrosstab() {
	var f=document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'export_crosstab';
	f.submit();
	f.command.value = 'crosstab';
}

function exportCrosstabToPDF() {
	var f=document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'export_crosstab_to_pdf';
	f.submit();
	f.command.value = 'crosstab';
}

var runner;
function customReport() {
	var f=document.surveyForm;
	f.action = '/customreports';
	f.command.value = 'report_custom';
	formSubmit();
	//var runner = window.setTimeout("backToResults()", 1000);
	//showUploadDiv();
}

function backToResults() {
	window.clearTimeout(runner)
	runner = null;
	var f = document.backToResults;
	f.submit();
}

function customReportIndex(qnid) {
	var f=document.surveyForm;
	f.action = '/customreports';
	f.command.value = 'report_index';
	f.target = "_self";
	//if (qnid && f.qnid) f.qnid.value = qnid;
	formSubmit();
}

function resumeRegistration() {
	f=document.surveyForm;
	f.command.value = 'resume_reg';
	f.submitme.value = 1;
	f.submit();
}

function jumpRegistration(p) {
	f=document.surveyForm;
	f.command.value = p;
	f.jumpto.value = 1;
	f.submitme.value = 1;
	f.submit();
}

function jumpShop(p) {
	f=document.surveyForm;
	f.command.value = p;
	f.jumpto.value = 1;
	f.submitme.value = 1;
	f.submit();
}

function mailInvoice(inv) {
	f=document.surveyForm;
	f.action = '/sarge';
	f.invoice_no.value = inv;
	f.command.value = 'mail_invoice';
	f.submit();
}

function payInvoice(inv) {
	f=document.surveyForm;
	f.action = '/sarge';
	f.invoice_no.value = inv;
	f.command.value = 'pay_invoice';
	f.submit();
}

function previewQn(qnid,cc,o) {
	f=document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'preview_survey';
	f.qnid.value = qnid;
	if (cc) f.cancel_command.value = cc;
	if (o) f.o.value = o;
	f.submit();
	showUploadDiv('loading');
}

function editGatheringOptions(qnid) {
	f=document.surveyForm;
	f.qnid.value = qnid;
	f.command.value = 'show_gather_options';
	f.action = '/sarge';
	f.submit();
}

function editCollectionOptions(qnid) {
	f=document.surveyForm;
	f.qnid.value = qnid;
	f.command.value = 'edit_opts';
	f.action = '/sarge';
	f.submit();
}

function toggleClosed(qnid) {
	f=document.surveyForm;
	f.qnid.value = qnid;
	f.command.value = 'toggle_closed';
	f.action = '/sarge';
	f.submit();
}

function togglePaused(qnid) {
	f=document.surveyForm;
	f.qnid.value = qnid;
	f.command.value = 'toggle_paused';
	f.action = '/sarge';
	f.submit();
}

function doneEditingQn(qnid) {
	f=document.surveyForm;
	f.qnid.value = qnid;
	f.command.value = 'done_editing';
	f.action = '/sarge';
	f.submit();
}

function toggleFolder(fid) {
	f=document.surveyForm;
	f.fid.value = fid;
	f.command.value = 'toggle_folder';
	f.action = '/sarge';
	f.submit();
}

function openAllFolders() {
	f=document.surveyForm;
	f.command.value = 'open_all_folders';
	f.action = '/sarge';
	f.submit();
}

function closeAllFolders() {
	f=document.surveyForm;
	f.command.value = 'close_all_folders';
	f.action = '/sarge';
	f.submit();
}

function moveToFolder(qnid) {
	if (document.getElementById('move_to_'+qnid).value == 'doh') return;
	f=document.surveyForm;
	f.qnid.value = qnid;
	f.command.value = 'move_to_folder';
	f.action = '/sarge';
	f.submit();
}

function moveFolderToFolder(fid) {
	if (document.getElementById('move_folder_to_'+fid).value == 'doh') return;
	f=document.surveyForm;
	f.fid.value = fid;
	f.command.value = 'move_folder_to_folder';
	f.action = '/sarge';
	f.submit();
}

function manageFolders() {
	f=document.surveyForm;
	f.command.value = 'manage_folders';
	f.action = '/sarge';
	f.submit();
}

function addFolder(fid) {
	f=document.surveyForm;
	f.fid.value = fid;
	f.command.value = 'add_folder';
	f.action = '/sarge';
	f.submit();
}

function renameFolder(fid) {
	f=document.surveyForm;
	f.fid.value = fid;
	f.command.value = 'rename_folder';
	f.action = '/sarge';
	f.submit();
}

function deleteFolder(fid,title) {
	f=document.surveyForm;
	f.fid.value = fid;
	f.command.value = 'delete_folder';
	f.action = '/sarge';
	f.submit();
}

function sortFolder(fid,field) {
	f=document.surveyForm;
	f.fid.value = fid;
	f.sort_field.value = field;
	f.command.value = 'sort_folder';
	f.action = '/sarge';
	f.submit();
}

function adminSortBy(field) {
	f=document.surveyForm;
	f.sort_field.value = field;
	f.command.value = 'show_surveys';
	f.action = '/admin';
	f.submit();
}

function listQn() {
	f=document.surveyForm;
	f.command.value = 'list_qn';
	f.action = '/sarge';
	f.submit();
}

function deleteQn(qnid,title) {
	f=document.surveyForm;
	if(confirm('Are you sure you want to delete the survey titled "'+title+'"?')) {
		f.qnid.value = qnid;
		f.command.value = 'delete_qn';
		f.action = '/sarge';
		f.submit();
	}
}

function designQn(qnid,live) {
	f=document.surveyForm;
	f.qnid.value = qnid;
	f.command.value = 'design_qn';
	f.action = '/sarge';
	if (live) {
		if (confirm('Your survey\'s status is currently LIVE. If you continue and edit your survey it will be moved to PAUSED status, which will prevent new respondents from entering the survey. You will need to resume the survey manually once you are finished editing it. When a survey is paused you will find the resume button in the Survey Status column on the My Surveys screen.\n\nThis will not affect respondents who are already taking the survey. Would you like to continue and edit your survey?')) {
			f.submit();
			showUploadDiv('loading');
		}
	} else {
		f.submit();
		showUploadDiv('loading');
	}
}

function showResults(qnid) {
	f=document.surveyForm;
	if (qnid != '' && qnid != null) f.qnid.value = qnid;
	if (f.report_user_results != null) {
		if (f.report_user_results.value != "") {
			f.command.value = 'show_results_report_user';
		} else {
			f.command.value = 'show_results';
		}
	} 	else {
		f.command.value = 'show_results';
	}
	f.action = '/sarge';
	f.submit();
	showUploadDiv();
}

function showResultsReportUser(qnid) {
	f=document.surveyForm;
	if (qnid != '' && qnid != null) f.qnid.value = qnid;
	f.command.value = 'show_results_report_user';
	f.action = '/sarge';
	f.submit();
	showUploadDiv();
}

function clearResults(qnid,title) {
	f=document.surveyForm;
	if(confirm('Are you sure you want to clear the results from the questionnaire titled "'+title+'"? This will delete all of your data. Are you sure?')) {
		f.qnid.value = qnid;
		f.command.value = 'clear_results';
		f.action = '/sarge';
		f.submit();
		showUploadDiv('uploading');
	}
}

function exportResults() {
	f=document.surveyForm;
	f.command.value = 'export_results';
	f.action = '/sarge';
	f.submit();
}

function shareExportResults(type) {
	f=document.surveyForm;
	f.type.value = type;
	f.command.value = 'share_export_results';
	f.action = '/sarge';
	f.submit();
}

function createExport() {
	f=document.surveyForm;
	f.command.value = 'export_results';
	f.action = '/sarge';
	formSubmit();
}

function createExportPlain() {
	f=document.surveyForm;
	f.command.value = 'export_plain_results';
	f.action = '/sarge';
	formSubmit();
}

function exportReport() {
	f=document.surveyForm;
	f.command.value = 'export_report';
	f.action = '/sarge';
	formSubmit();
}

function showGraph(cid,eid1,eid2,format) {
	self.name = 'resultsWin';
	f=document.surveyForm;
	f.cid.value = cid;
	if (eid1 != '' && eid1 != null) f.eid1.value = eid1;
	if (eid2 != '' && eid2 != null) f.eid2.value = eid2;
	if (format != '' && format != null) f.format.value = format;
	f.command.value = 'show_graph';
	f.action = '/sarge';
	if (isMac) {
		f.submit();
	} else {
		var graphWin = window.open('','graphWin','width=800,height=600,left=100,top=100,scrollbars=yes,toolbar=no,statusbar=yes,resizable=yes');
		graphWin.focus();
		f.target = 'graphWin';
		f.submit();
		f.target = self.name;
		f.cid.value = '';
		f.eid1.value = '';
		f.eid2.value = '';
	}
}

function showQGraph(cid) {
	self.name = 'resultsWin';
	f=document.surveyForm;
	f.cid.value = cid;
	f.command.value = 'show_q_graph';
	f.action = '/sarge';
	if (isMac) {
		f.submit();
	} else {
		var graphWin = window.open('','graphWin','width=800,height=600,left=100,top=100,scrollbars=yes,toolbar=no,statusbar=yes,resizable=yes');
		graphWin.focus();
		f.target = 'graphWin';
		f.submit();
		f.target = self.name;
		f.cid.value = '';
		f.eid1.value = '';
		f.eid2.value = '';
	}
}

function zoomGraph(zoom_dirn,cid,eid1,eid2) {
	f=document.surveyForm;
	f.zoom_dirn.value = zoom_dirn;
	f.cid.value = cid;
	if (eid1 != null && eid1 != 0) f.eid1.value = eid1;
	if (eid2 != null && eid2 != 0) f.eid2.value = eid2;
	f.command.value = 'zoom_graph';
	f.action = '/sarge';
	f.submit();
}

function zoomQGraph(zoom_dirn,cid) {
	f=document.surveyForm;
	f.zoom_dirn.value = zoom_dirn;
	f.cid.value = cid;
	f.command.value = 'zoom_q_graph';
	f.action = '/sarge';
	f.submit();
}

function viewIndividualResponses(type,cid,qnum,eid1,eid2) {
	self.name = 'resultsWin';
	f=document.surveyForm;
	f.cid.value = cid;
	f.qnum.value = qnum;
	f.qtype.value = type;
	f.action = '/sarge';
	if (eid1) f.eid1.value = eid1;
	if (eid2) f.eid2.value = eid2;
	if (type == 'name_and_address') {
		f.command.value = 'view_name_and_address_responses';
	} else if (type == 'element_header') {
		f.command.value = 'view_element_header_results';
	} else {
		f.command.value = 'view_text_responses';
	}
	if (isMac) {
		f.submit();
	} else {
		var textWin = window.open('','textWin','width=800,height=400,left=30,top=30,scrollbars=yes,toolbar=no,statusbar=yes');
		textWin.focus();
		textWin.closeMe = 1;
		f.target = 'textWin';
		f.submit();
		f.target = self.name;
	}
}

function deleteResultSet() {
	if (confirm('Are you sure you want to delete this result set?')) {
		f=document.surveyForm;
		f.command.value = 'delete_result_set';
		f.submit();
	}
}

function viewDetail(seq) {
	if (document.haveResults == 0) return;
	f=document.surveyForm;
	f.command.value = 'show_detail';
	// Force this brain-dead excuse for a language to parse a number as a ...
	// ... number. How radical is that!!
	seq = parseInt(seq,10);
	var maxseq = parseInt(f.maxseq.value,10);
	var currentseq = parseInt(f.currentseq.value,10);
	if (seq == currentseq) return;
	if (seq > maxseq) {
		alert('Can\'t move beyond the last record');
	} else if (seq < 1) {
		alert('Can\'t move beyond the first record');
	} else {
		f.newseq.value = seq;
		if (window.opener) f.target = window.opener.name;
		f.action = '/sarge';
		f.submit();
		if (window.opener && window.closeMe == 1) window.close();
		showUploadDiv();
	} 
}

function viewDetailByRID() {
	if (document.haveResults == 0) return;
	f=document.surveyForm;
	f.command.value = 'show_detail';
	var maxseq = parseInt(f.maxseq.value,10);
	var seq = parseInt(f.currentseq.value,10);
	if ((f.goto_rid.value > 0) && (f.goto_rid.value != null)) {
		f.newseq.value = seq;
		if (window.opener) f.target = window.opener.name;
		f.action = '/sarge';
		f.submit();
		if (window.opener && window.closeMe == 1) window.close();
		showUploadDiv();
	} else {
		alert("Please supplied a valid RID");
	}
}

function viewDetailShare(seq) {
	f=document.surveyForm;
	f.command.value = 'share_detail';
	// Force this brain-dead excuse for a language to parse a number as a ...
	// ... number. How radical is that!!
	seq = parseInt(seq,10);
	var maxseq = parseInt(f.maxseq.value,10);
	var currentseq = parseInt(f.currentseq.value,10);
	if (seq == currentseq) return;
	if (seq > maxseq) {
		alert('Can\'t move beyond the last record');
	} else if (seq < 1) {
		alert('Can\'t move beyond the first record');
	} else {
		f.newseq.value = seq;
		if (window.opener) f.target = window.opener.name;
		f.action = '/sarge';
		f.submit();
		if (window.opener) {
			if (window.opener.document.adminWin != 1) window.close();
		}
		showUploadDiv();
	} 
}

function editRid(rid) {
	f=document.surveyForm;
	f.rid.value = rid;
	f.command.value = 'edit_rid';
	f.action = '/sarge';
	f.submit();
}
	
function updateFilters() {
	f=document.surveyForm;
	f.command.value = 'update_filters';
	f.action = '/sarge';
	f.submit();
	showUploadDiv();
}
	
function fR(type,cid,eid1,eid2,eid3) {
	// type = 1d, 2d, 3d, text ...
	// cid, eid1, eid2, eid3 = question, component, element ids
	
	f=document.surveyForm;
	f.command.value = 'create_filter';
	f.filter_type.value = type;
	f.cid.value = cid;
	f.eid1.value = eid1;
	if (eid2 != null && eid2 != '') {
		f.eid2.value = eid2;
	} else {
		f.eid2.value = '';
	}
	if (eid3 != null && eid3 != '') {
		f.eid3.value = eid3;
	} else {
		f.eid3.value = '';
	}
	f.action = '/sarge';
	f.submit();
	showUploadDiv();
}

function editMetaDataFilters() {
	f=document.surveyForm;
	f.command.value = 'edit_meta_data_filters';
	f.action = '/sarge';
	f.submit();
	showUploadDiv();
}

function toggleFilters(qnid) {
	f=document.surveyForm;
	f.qnid.value = qnid;
	f.command.value = 'toggle_filters';
	f.action = '/sarge';
	f.submit();
}

function qnNavChange(type) {
	f=document.surveyForm;
	if (type == 'edit') {
		f.command.value = 'design_qn';
		f.action = '/sarge';
	} else if (type == 'results') {
		if (f.report_user_results != null) {
			if (f.report_user_results.value != "") {
				f.command.value = 'show_results_report_user';
			} else {
				f.command.value = 'show_results';
			}
		} else {
			f.command.value = 'show_results';
		}
		f.action = '/sarge';
	} else if (type == 'results_detail') {
		f.command.value = 'show_detail';
		f.action = '/sarge';
	}
	f.submit();
	showUploadDiv();
}

function linkForEmail() {
	f=document.surveyForm;
	f.command.value = 'link_for_email';
	f.action = '/sarge';
	f.submit();
}

function quota_management_v2(qnid) {
	f=document.surveyForm;
	f.camefrom.value = f.command.value;
	f.command.value = 'quota_management_v2';
	f.action = '/sarge';
	f.target = '_self';
	if (qnid) {
		var e = document.createElement("input");
		e.type = "hidden";
		e.name = "qnid";
		e.value = qnid;
		f.appendChild(e);
	}
	f.submit();
}

function linkForWebsite() {
	f=document.surveyForm;
	f.command.value = 'link_for_website';
	f.action = '/sarge';
	f.submit();
}

function linkForDataEntry() {
	f=document.surveyForm;
	f.command.value = 'link_for_data_entry';
	f.action = '/sarge';
	f.submit();
}

function linkForResults() {
	f=document.surveyForm;
	f.command.value = 'link_for_results';
	f.action = '/sarge';
	f.submit();
}

function linkForPreview() {
	f=document.surveyForm;
	f.command.value = 'link_for_preview';
	f.action = '/sarge';
	f.submit();
}

function linkForPopup() {
	f=document.surveyForm;
	f.command.value = 'link_for_popup';
	f.action = '/sarge';
	f.submit();
}

function previewTemplate() {
	f=document.surveyForm;
	if (f.qnid.value && f.qnid.value != 'doh' && f.qnid.value != '') {
		var previewWin = window.open('/sarge?command=preview_template&qnid='+f.qnid.value,'previewWin','width=700,height=500,left=30,top=20,scrollbars=yes,toolbar=no,statusbar=yes');
		previewWin.focus();
	} else {
		alert('You haven\'t chosen a template yet.');
	}
}

function emailOptOut(type) {
	f=document.surveyForm;
	if (type == 'decline') {
		f.decline.value = 1;
	} else if (type == 'demur') {
		f.demur.value = 1;
	} else if (type == 'delete_me') {
		f.delete_me.value = 1;
	}
	f.action = '/sarge';
	f.submit();
}

/*
 *	CentralContact
 */

function showContactEmails() {
	f=document.surveyForm;
	f.command.value = 'show_contact_emails';
	f.action = '/sarge';
	f.submit();
}

function editContactEmail(ceid) {
	f=document.surveyForm;
	f.command.value = 'edit_contact_email';
	f.ceid.value = ceid;
	f.action = '/sarge';
	f.submit();
}

function deleteContactEmail(ceid) {
	f=document.surveyForm;
	f.command.value = 'delete_contact_email';
	f.ceid.value = ceid;
	f.action = '/sarge';
	if (confirm('Are you sure you want to delete this email?')) {
		f.submit();
	}
}

function newContactEmail() {
	f=document.surveyForm;
	f.command.value = 'new_contact_email';
	f.action = '/sarge';
	f.submit();
}

function showContactSchedules() {
	f=document.surveyForm;
	f.command.value = 'show_contact_schedules';
	f.action = '/sarge';
	f.submit();
}

function newContactSchedule() {
	f=document.surveyForm;
	f.command.value = 'new_contact_schedule';
	f.action = '/sarge';
	f.submit();
}

function editContactSchedule(csid) {
	f=document.surveyForm;
	f.command.value = 'edit_contact_schedule';
	f.csid.value = csid;
	f.action = '/sarge';
	f.submit();
}

function deleteContactSchedule(csid) {
	f=document.surveyForm;
	f.command.value = 'delete_contact_schedule';
	f.csid.value = csid;
	f.action = '/sarge';
	if (confirm('Are you sure you want to delete this invitation?')) {
		f.submit();
	}
}

/*
 *	/CentralContact
 */

function addressBook() {
	f=document.surveyForm;
	f.command.value = 'address_book';
	f.action = '/sarge';
	f.submit();
}

function changeAddressBook() {
	f=document.surveyForm;
	f.command.value = 'change_address_book';
	f.action = '/sarge';
	f.submit();
}

function sortAddressBook(ab_lid,field) {
	f=document.surveyForm;
	f.sort_field.value = field;
	f.command.value = 'sort_address_book';
	f.action = '/sarge';
	f.submit();
}

function importToAddressBook(ab_lid) {
	f=document.surveyForm;
	f.command.value = 'import_emails';
	f.action = '/sarge';
	f.submit();
}

function exportAddressBook(ab_lid) {
	f=document.surveyForm;
	f.command.value = 'export_emails';
	f.action = '/sarge';
	f.submit();
}

function editFromAddressBook(ab_eid) {
	f=document.surveyForm;
	f.command.value = 'edit_from_address_book';
	f.ab_eid.value = ab_eid;
	f.action = '/sarge';
	f.submit();
}

function pwdReminderFromAddressBook(ab_eid) {
	f=document.surveyForm;
	f.command.value = 'password_reminder_from_address_book';
	f.ab_eid.value = ab_eid;
	f.action = '/sarge';
	f.submit();
}

function deleteFromAddressBook(i,n) {
	if (confirm('Do you really want to delete \''+n+'\' from this list?')) {
		f=document.surveyForm;
		f.command.value = 'delete_from_address_book';
		f.ab_eid.value = i;
		f.action = '/sarge';
		f.submit();
	}
}

function deleteAllFromAddressBook() {
	if (confirm('Do you really want to delete all contacts from this list?')) {
		f=document.surveyForm;
		f.command.value = 'delete_all_from_address_book';
		f.action = '/sarge';
		f.submit();
	}
}

function deleteByFilterFromAddressBook() {
	f=document.surveyForm;
	f.command.value = 'delete_by_filter_from_address_book';
	f.action = '/sarge';
	f.submit();
}

function recsPerPageAddressBook(r) {
	f=document.surveyForm;
	f.recs_per_page.value = r;
	f.command.value = 'set_recs_per_page_address_book';
	f.action = '/sarge';
	f.submit();
}

function pageAddressBook(dirn) {
	f=document.surveyForm;
	f.command.value = 'page_' + dirn + '_address_book';
	f.action = '/sarge';
	f.submit();
}

function createEmailList() {
	f=document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'create_email_list';
	f.submit();
}

function editEmailList() {
	f=document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'edit_email_list';
	f.submit();
}

function deleteEmailList() {
	f=document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'delete_email_list';
	o=document.surveyForm.ab_lid;
	title=o.options[o.selectedIndex].text;
	if(title == '') {
		alert('No list selected');
	} else {
		if (confirm('Do you really want to delete your email list called \''+title+'\'?')) {
			f.submit();
		}
	}
}

function viewEmails() {
	f=document.surveyForm;
	f.command.value = 'view_emails';
	f.action = '/sarge';
	f.submit();
}

function sortEmails(field) {
	f=document.surveyForm;
	f.sort_field.value = field;
	f.command.value = 'sort_emails';
	f.action = '/sarge';
	f.submit();
}

function recsPerPageEmails(r) {
	f=document.surveyForm;
	f.recs_per_page.value = r;
	f.command.value = 'set_recs_per_page_emails';
	f.action = '/sarge';
	f.submit();
}

function pageEmails(dirn) {
	f=document.surveyForm;
	f.command.value = 'page_' + dirn + '_emails';
	f.action = '/sarge';
	f.submit();
}

function viewEmailDetail(ab_emid) {
	f=document.surveyForm;
	f.ab_emid.value = ab_emid;
	f.command.value = 'view_email_detail';
	f.action = '/sarge';
	f.submit();
}

function exportEmailDetail() {
	f=document.surveyForm;
	f.command.value = 'export_email_detail';
	f.action = '/sarge';
	f.submit();
}

function sortEmailDetail(ab_emid,field) {
	f=document.surveyForm;
	f.sort_field.value = field;
	f.command.value = 'sort_email_detail';
	f.action = '/sarge';
	f.submit();
}

function recsPerPageEmailDetail(r) {
	f=document.surveyForm;
	f.recs_per_page.value = r;
	f.command.value = 'set_recs_per_page_email_detail';
	f.action = '/sarge';
	f.submit();
}

function pageEmailDetail(dirn) {
	f=document.surveyForm;
	f.command.value = 'page_' + dirn + '_email_detail';
	f.action = '/sarge';
	f.submit();
}

function clearResultsByRespondent(ab_eid) {
	f=document.surveyForm;
	f.command.value = 'clear_results_by_respondent';
	f.action = '/sarge';
	f.ab_eid.value = ab_eid;
	f.submit();
}

function viewImportedDataSets() {
	f=document.surveyForm;
	f.command.value = 'view_imported_data_sets';
	f.action = '/sarge';
	f.submit();
}

function verifySurveyLink() {
	// Can't use onsubmit processing 'cos preview email does something different
	// if submitme is set
	// XXX Yes we can: move these checks into the page and use formSubmitNoSM()
	var f=document.surveyForm;
	var html = false;
	var send = false;
	
	if (f.sending_html) {
		if (f.sending_html.value == 1) return false;
	}
	if (f.vsl != null) {
		if (f.vsl.value == "0") return false; 
	}
	if (f.html_body != null) {
		var hb = f.html_body.value;                  
		hb = hb.replace(/\<\/{0,1}[^>]+\>/gi, "");
		if (hb != "") {
			if (f.compose_email_page && f.html_body.value.search(/\[#SURVEYLINK#\]/) == -1) {
				send = true;
			} else {
				send = false;
			}
		} else {
			if (f.compose_email_page && f.body.value.search(/\[#SURVEYLINK#\]/) == -1) {
				send = true;
			} else {
				send = false;
			}
		}
	}
	return send;
}

function sendEmail(html) {
	if (html && document.surveyForm.html_email) document.surveyForm.html_email.value = '1';
	if (verifySurveyLink() ) {
		alert('You must have the survey link place marker (\'[#SURVEYLINK#]\') in the email body');
	} else {
		f=document.surveyForm;
		f.command.value = 'find_recipients';
		f.action = '/sarge';
		formSubmitNoSM();
	}
}

function previewEmail() {
	if (verifySurveyLink() ) {
		alert('You must have the survey link place marker (\'[#SURVEYLINK#]\') in the email body');
	} else {
		f=document.surveyForm;
		f.command.value = 'preview_email';
		f.action = '/sarge';
		formSubmitNoSM();
	}
}

function previewReminder() {
	if (verifySurveyLink() ) {
		alert('You must have the survey link place marker (\'[#SURVEYLINK#]\') in the email body');
	} else {
		f=document.surveyForm;
		f.command.value = 'preview_reminder';
		f.action = '/sarge';
		formSubmitNoSM();
	}
}

function sendEmailToQueue() {
	if (confirm('Your survey will now be sent out to the recipients you have selected. This is the last stage at which you can make any changes before the email is sent out.\n\nIf you want to continue click OK, or else click Cancel')) {
		f=document.surveyForm;
		f.command.value = 'send_email_to_queue';
		f.action = '/sarge';
		f.submit();
	}
}

function sendReminderEmailToQueue() {
	if (confirm('Your survey will now be sent out to the recipients you have selected. This is the last stage at which you can make any changes before the email is sent out.\n\nIf you want to continue click OK, or else click Cancel')) {
		f=document.surveyForm;
		f.command.value = 'reminder_email';
		f.stage.value = 'send';
		f.action = '/sarge';
		f.submit();
	}
}

function removeEmailFromQueue(ab_emid) {
	f=document.surveyForm;
	f.ab_emid.value = ab_emid;
	f.command.value = 'remove_email_from_queue';
	f.action = '/sarge';
	f.submit();
}

function sendEmailToLinks() {
	if (confirm('Your survey will now be finalised (but no emails will be sent). This is the last stage at which you can make any changes.\n\nIf you want to continue click OK, or else click Cancel')) {
		f=document.surveyForm;
		f.command.value = 'send_email_to_links';
		f.action = '/sarge';
		f.submit();
	}
}

function pwdReminderFromQnLogin() {
	f=document.surveyForm;
	f.command.value = 'password_reminder_from_qn_login';
	f.action = '/sarge';
	f.submit();
}

function reminderEmail(ab_emid,select_how,stage) {
	f=document.surveyForm;
	f.command.value = 'reminder_email';
	if (select_how && f.select_recipients) f.select_recipients.value = select_how;
	f.action = '/sarge';
	f.ab_emid.value = ab_emid;
	f.submitme.value = 0;
	f.stage.value = stage; 
	f.submit();
}

function setQsRequired(req) {
	f=document.surveyForm;
	f.command.value = 'set_qs_required';
	if (req == 'on') {
		f.ans_req.value = 1;
	} else {
		f.ans_req.value = 0;
	}
	f.action = '/sarge';
	f.submit();
}

function searchNReplace(qnid) {
	f=document.surveyForm;
	f.command.value = 'snr';
	f.qnid.value = qnid;
	f.action = '/sarge';
	f.submit();
}

function editQnOptions() {
	f=document.surveyForm;
	f.command.value = 'edit_qn';
	f.action = '/sarge';
	f.submit();
}

function editSieveLogic() {
	f=document.surveyForm;
	f.command.value = 'edit_sieve_logic';
	f.action = '/sarge';
	f.submit();
}

function editRtypes() {
	f=document.surveyForm;
	f.command.value = 'edit_rtypes';
	f.action = '/sarge';
	f.submit();
}

function editQuestionCodes() {
	f=document.surveyForm;
	f.command.value = 'edit_question_codes';
	f.action = '/sarge';
	f.submit();
}

function editSecTitle(sid) {
	f=document.surveyForm;
	f.sid.value = sid;
	f.command.value = 'edit_sec';
	f.action = '/sarge';
	f.submit();
}

function editPageTitle(pid) {
	f=document.surveyForm;
	if (pid > 0) f.pid.value = pid;
	f.command.value = 'edit_p';
	f.action = '/sarge';
	f.submit();
}

function editPageTitleAll() {
	f=document.surveyForm;
	f.command.value = 'edit_p_all';
	f.action = '/sarge';
	f.submit();
}

function editPageLogic(pid) {
	f=document.surveyForm;
	f.pid.value = pid;
	f.command.value = 'edit_page_logic';
	f.action = '/sarge';
	f.submit();
}

function addPTop(pid) {
	f=document.surveyForm;
	f.pid.value = pid;
	f.command.value = 'add_p_top';
	f.action = '/sarge';
	f.submit();
	showUploadDiv();
}

function addPAfter(pid,qid) {
	f=document.surveyForm;
	f.pid.value = pid;
	f.qid.value = qid;
	f.command.value = 'add_p_after';
	f.action = '/sarge';
	f.submit();
	showUploadDiv();
}

function deleteP(pid) {
	f=document.surveyForm;
	f.pid.value = pid;
	f.command.value = 'delete_p';
	f.action = '/sarge';
	f.submit();
	showUploadDiv();
}

function addHelpText(qid) {
	f=document.surveyForm;
	f.qid.value = qid;
	f.command.value = 'add_help_text';
	f.action = '/sarge';
	f.submit();
}

function addQTop(pid) {
	f=document.surveyForm;
	f.pid.value = pid;
	f.command.value = 'add_q_top_nqt';
	f.action = '/sarge';
	f.submit();
}

function addQAfter(qid) {
	f=document.surveyForm;
	f.qid.value = qid;
	f.command.value = 'add_q_after_nqt';
	f.action = '/sarge';
	f.submit();
}

function addLibraryQTop(pid) {
	f=document.surveyForm;
	f.pid.value = pid;
	f.command.value = 'add_lib_q_top';
	f.action = '/sarge';
	f.submit();
}

function addLibraryQAfter(qid) {
	f=document.surveyForm;
	f.qid.value = qid;
	f.command.value = 'add_lib_q_after';
	f.action = '/sarge';
	f.submit();
}

function addPresQTop(pid) {
	f=document.surveyForm;
	f.pid.value = pid;
	f.command.value = 'add_pres_q_top_nqt';
	f.action = '/sarge';
	f.submit();
}

function addPresQAfter(qid) {
	f=document.surveyForm;
	f.qid.value = qid;
	f.command.value = 'add_pres_q_after_nqt';
	f.action = '/sarge';
	f.submit();
}

function editQ(t,qid,cid) {
	f=document.surveyForm;
	f.qid.value = qid;
	f.cid.value = cid;
	f.command.value = 'edit_q';
	if (f.q_type.tagName == 'input' || f.q_type.tagName == 'INPUT') f.q_type.value = t;
	setAction(t);
	f.submit();
}

function deleteQ(qid) {
	f=document.surveyForm;
	f.qid.value = qid;
	f.command.value = 'delete_q';
	f.action = '/sarge';
	f.submit();
}

function moveQUp(qid) {
	f=document.surveyForm;
	f.qid.value = qid;
	f.command.value = 'move_q_up';
	f.action = '/sarge';
	f.submit();
}

function moveQDown(qid) {
	f=document.surveyForm;
	f.qid.value = qid;
	f.command.value = 'move_q_down';
	f.action = '/sarge';
	f.submit();
}

function editQLogic(qid) {
	f=document.surveyForm;
	f.qid.value = qid;
	f.command.value = 'edit_question_logic';
	f.action = '/sarge';
	f.submit();
}

function editDeleteResults() {
	f=document.surveyForm;
	f.edit_delete_results.value = 1;
	f.command.value = 'edit_q';
	setAction(f.q_type.value);
	f.submit();
}

function restrictedEdit() {
	f=document.surveyForm;
	f.ra.value = "1";
	f.command.value = 'edit_q';
	setAction(f.q_type.value);
	f.submit();
}

function changeTheme() {
	f=document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'change_theme';
	f.submit();
	showUploadDiv();
}

function changeQType(save) {
	f=document.surveyForm;
	// This is necessary to stop the select from reverting to its original
	// value. Why? Who knows.
	if (f.q_type.value == "no_selection") {
		alert("Please select a valid question type");
		return true;
	}
	try {
		dynamicInput();
	} catch(e) {}
	if (f.command.value == "edit_q") f.changeQTypeSave.value = 1;
	f.q_type.blur();
	t=f.q_type.value;
	setAction(t);
	f.submit();
}

function setEditor(editorLocal) {
	if (typeof editorLocal.editor == "object") {
		if (editorLocal.mode == "HTML") {
			onsubmit_new();
			var text = document.getElementById(editorLocal.textArea).value;
			text = text.replace(/\n/g,"<br/>");
			document.getElementById(editorLocal.htmlArea).value = text;
			editorLocal.editor.putHTML(document.getElementById(editorLocal.htmlArea).value);
			document.getElementById(editorLocal.htmlDiv).style.display = "block";
			document.getElementById(editorLocal.textDiv).style.display = "none";
		} else {
			//onsubmit_new();
			var text = document.getElementById(editorLocal.htmlArea).value;
			text = text.replace(/\<br\s*[\/]{0,1}\>/g, "\n");
			text = text.replace(/^\<br\s+class\=\"innova\"\>$/,"");
			document.getElementById(editorLocal.textArea).value = text;
			document.getElementById(editorLocal.textDiv).style.display = "block";
			document.getElementById(editorLocal.htmlDiv).style.display = "none";
		}
		if (typeof editorLocal.callback == 'function') {
			editorLocal.callback();
		}
	}
}

function switchEditor(editorLocal) {
	editorLocal.mode = editorLocal.mode == "text" ? "HTML" : "text";
	setEditor(editorLocal);
}

function saveEditors(fieldO) {
	var editorFieldArray = new Array();
	for (x in editors) {
		if (editors[x].mode == "text") {
			onsubmit_new();
			document.getElementById(editors[x].htmlArea).value = document.getElementById(editors[x].textArea).value;
		} else {
			onsubmit_new();
			document.getElementById(editors[x].textArea).value = document.getElementById(editors[x].htmlArea).value;
		}
		editorFieldArray.push(x+":"+editors[x].mode);
	}
	fieldO.value = editorFieldArray.join(",");
}

function readEditorSet(field) {
	var editorSets = field.value.split(/\,/);
	for (x in editorSets) {
		if (editorSets[x].length > 0) {
			var pair = editorSets[x].split(/\:/);
			editors[pair[0]].mode = pair[1];
		}
	}
}

function changeEditView(vtype) {
	f=document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'change_edit_view';
	f.edit_view.value = vtype;
	showUploadDiv();
	f.submit();
}

function allocateRes(cc) {
	f=document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'allocate_res';
	f.cancel_command.value = cc;
	f.submit();
}

function editDetailsPw() {
	f=document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'edit_details_pw';
	f.submit();
}

function showInvoices() {
	f=document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'show_invoices';
	f.submit();
}

function buyRes() {
	f=document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'shop_first_page';
	f.submit();
}

function supportEmail() {
	document.location = '/sarge?command=support';
}

function imageLibrary() {
	f=document.surveyForm;
	f.action = '/sarge';
	// Hmmm, not happy about this. I need this for the 'Back' button, but this
	//	seems a dodge-malodge way to capture it. 
	f.cancel_command.value = f.command.value;
	f.command.value = 'image_library';
	f.submit();
}                 

function uploadImage() {
	f=document.surveyForm;
	f.action = '/sarge';
	f.encoding = 'multipart/form-data';
	f.command.value = 'upload_image';
	f.submit();
}

function deleteImage(fn) {
	f=document.surveyForm;
	f.action = '/sarge';
	f.command.value = 'delete_image';
	f.image_filename.value = fn;
	f.submit();
}

function popupInvoice(i,j) {
	if (j) {
		winw=window.open('/sarge?command=show_invoice&i='+i+'&j='+j,'invoice','width=640,height=480,left=50,top=50,scrollbars=yes,toolbar=no,statusbar=no');
	} else {
		winw=window.open('/sarge?command=show_invoice&i='+i,'invoice','width=640,height=480,left=50,top=50,scrollbars=yes,toolbar=no,statusbar=no');
	}
}

/* User functions end */

// End of button drivers

// This is used by editQ (the function invoked by the edit question button)
// and changeQType (the onChange handler for the question type dropdown). It
// sets the action of the form for each of the different question types.

// IMPORTANT: You must add a case for any new question type which is added

function setAction(type) {
	f=document.surveyForm;
	switch(type) {
		case 'sfw_scale_cond':
			f.action = '/sarge';
			break;
		case 'sfw_scale_gradient':
			f.action = '/sarge';
			break;
		case 'aina':
			f.action = '/sarge';
			break;
		case 'checkbox':
			f.action = '/sarge';
			break;
		case 'checkbox_matrix':
			f.action = '/sarge';
			break;
		case 'constant_sum':
			f.action = '/sarge';
			break;
		case 'dropdown':
			f.action = '/sarge';
			break;
		case 'dropdown_matrix':
			f.action = '/sarge';
			break;
		case 'email':
			f.action = '/sarge';
			break;
		case 'essay':
			f.action = '/sarge';
			break;
		case 'name_and_address':
			f.action = '/sarge';
			break;
		case 'parameter':
			f.action = '/sarge';
			break;
		case 'pres_image':
			f.action = '/sarge';
			break;
		case 'pres_text':
			f.action = '/sarge';
			break;
		case 'pres_video':
			f.action = 'q_pres_video.pl';
			break;
		case 'radio_matrix':
			f.action = '/sarge';
			break;
		case 'radio':
			f.action = '/sarge';
			break;
		case 'rank':
			f.action = '/sarge';
			break;
		case 'scale':
			f.action = '/sarge';
			break;
		case 'scale_matrix':
			f.action = '/sarge';
			break;
		case 'text':
			f.action = '/sarge';
			break;
		case 'text_matrix':
			f.action = '/sarge';
			break;
		case 'text_true_matrix':
			f.action = '/sarge';
			break;
		case 'timestamp':
			f.action = '/sarge';
			break;
		case 'validated_parameter':
			f.action = '/sarge';
			break;
		case 'dynamic_dropdown':
			f.action = '/sarge';
			break;
		default:
			f.action = '/sarge';
			f.command.value = f.command.value + '_nqt';
			break;
	}
}

// Start of the functions which control the behaviour of the standard
// components.

// Constructor for the Radio object. This is merely an Array which holds the
// members of the Radio group

function Radio(a) {
	var i;
	this.name = a[0];
	this.members = new myArray;
	for(i=1;i<a.length;i++) this.members.push(document.getElementById(a[i]));
}

// createRadio is called from the onLoad init() function to ...
// surprise surprise, initialise the Radio objects

function createRadio(c) {
	eval('Obj'+c+' = new Radio(arguments)');
}

// Constructor for the ConstantSum object. Is actually identical to the Radio
// object constructor, but it would confuse if it was called as such.

function ConstantSum(a) {
	var i;
	this.name = a[0];
	this.members = new myArray;
	for(i=1;i<a.length;i++) this.members.push(document.getElementById(a[i]));
}

function createConstantSum(c) {
	eval('Obj'+c+' = new ConstantSum(arguments)');
}

// Constructor for the Rank object. Is actually identical to the Radio
// object constructor, but it would confuse if it was called as such.

function Rank(a) {
	var i;
	this.name = a[0];
	this.members = new myArray;
	for(i=1;i<a.length;i++) this.members.push(document.getElementById(a[i]));
}

function createRank(c) {
	eval('Obj'+c+' = new Rank(arguments)');
}

// The next fn* functions do the dirty work of making the images behave like
// radio buttons, checkboxes, scale groups, etc.
// I could tell you how they work but then I'd have to kill you ;-)
// It's not that difficult, think about how they behave and read the code.

function fnSFWScaleCond(c,i,a,v) {
	// c = class, i = element id, a = action, v = value
	// optional last arg is the text to put in the conditional
	//	text box, useful for init of previous answers
	var r;
	var t = c; // a copy of the class to id the text input
	var clicked = false;
	var o = document.getElementById(i);
	var base = o.src.slice(0,o.src.lastIndexOf('_'));
	var path = base.slice(0,base.lastIndexOf('/'));
	if(o.src.search('click') > -1) clicked = true;

	if (a == 'click') {
		if (!clicked) {
			o.src = base+'_click.gif';
			document.getElementById(c).value = v;
		}
		r = eval('Obj'+c);
		t = document.getElementById(t.replace(/^R/,'T'));
		if (arguments[4] != null) t.value = arguments[4];
		for(i=0;i<r.members.length;i++) {
			if (!(r.members[i]==o)) {
				r.members[i].src = path+'/smiley'+(i+1)+'_normal.gif';
			} else {
				if (i==0 || i==3) {
					t.disabled = false;
					if (isIE) {
						t.style.color = '#000000';
						t.style.fontStyle = 'normal';
						t.style.backgroundColor = '#FFFFFF';
					}
				} else {
					t.disabled = true;
					if (isIE) {
						t.value = '';
						t.style.color = '#999999';
						t.style.fontStyle = 'italic';
						t.style.backgroundColor = '#BFCEDB';
					}
				}
			}
		}
	}
	if (!clicked) {
		if (a == 'over') o.src = base+'_over.gif';
		if (a == 'normal') o.src = base+'_normal.gif';
	}
}

function fnSFWScaleGradient(c,i,a,v) {
	// c = class, i = element id, a = action, v = value
	var r;
	var clicked = false;
	var o = document.getElementById(i);
	var base = o.src.slice(0,o.src.lastIndexOf('_'));
	var path = base.slice(0,base.lastIndexOf('/'));
	if(o.src.search('click') > -1) clicked = true;

	if (a == 'click') {
		if (!clicked) {
			o.src = base+'_click.gif';
			document.getElementById(c).value = v;
		}
		r = eval('Obj'+c);
		for(i=0;i<r.members.length;i++) { if (!(r.members[i]==o)) r.members[i].src = path+'/gradient'+(i+1)+'_normal.gif'; }
	}
	if (!clicked) {
		if (a == 'over') o.src = base+'_over.gif';
		if (a == 'normal') o.src = base+'_normal.gif';
	}
}

function fnScale(c,i,a,v) {
	// c = class, i = element id, a = action, v = value
	var r;
	var clicked = false;
	var o = document.getElementById(i);
	var base = o.src.slice(0,o.src.lastIndexOf('_'));
	var path = base.slice(0,base.lastIndexOf('/'));
	if(o.src.search('click') > -1) clicked = true;

	if (a == 'click') {
		if (!clicked) {
			r = eval('Obj'+c);
			o.src = base+'_click.gif';
			document.getElementById(c).value = v;
			for(i=0;i<r.members.length;i++) { if (!(r.members[i]==o)) r.members[i].src = path+'/'+(i+1)+'_normal.gif'; }
		} else {
			o.src = base+'_over.gif';
			document.getElementById(c).value = '';
		}
	} else if (a == 'click_text') {
		if (!clicked) {
			r = eval('Obj'+c);
			o.src = base+'_click.gif';
			document.getElementById(c).value = v;
			for(i=0;i<r.members.length;i++) { if (!(r.members[i]==o)) r.members[i].src = base+'_normal.gif'; }
		} else {
			o.src = base+'_normal.gif';
			document.getElementById(c).value = '';
		}
	} else if (a == 'click_text_box') {
		if (! clicked) {
			r = eval('Obj'+c);
			o.src = base+'_click.gif';
			document.getElementById(c).value = v;
			for(i=0;i<r.members.length;i++) { if (!(r.members[i]==o)) r.members[i].src = base+'_normal.gif'; }
		}
	}
	if (!clicked) {
		if (a == 'over') o.src = base+'_over.gif';
		if (a == 'normal') o.src = base+'_normal.gif';
	}
}

var fnRadioPrevious = new Array();

function fnRadio(c,i,a,v) {
	// c = class, i = element id, a = action, v = value
	var r;
	var clicked = false;
	var o = document.getElementById(i);
	var base = o.src.slice(0,o.src.lastIndexOf('_'));
	if(o.src.search('click') > -1) clicked = true;
	//alert("A:"+a);
	if (a == 'click') {
		if (!clicked) {
			r = eval('Obj'+c);
			o.src = base+'_click.gif';
			document.getElementById(c).value = v;
			for(i=0;i<r.members.length;i++) { if (!(r.members[i]==o)) { r.members[i].src = base+'_normal.gif'; } }
			if (fnRadioPrevious[c]) clearOptionText(c,fnRadioPrevious[c]);
			fnRadioPrevious[c] = v;
		} else {
			o.src = base+'_over.gif';
			document.getElementById(c).value = '';			
			clearOptionText(c,v);
		}
	} else if (a == 'click_text') {
		if (!clicked) {
			r = eval('Obj'+c);
			o.src = base+'_click.gif';
			document.getElementById(c).value = v;
			for(i=0;i<r.members.length;i++) { if (!(r.members[i]==o)) r.members[i].src = base+'_normal.gif'; }
			if (fnRadioPrevious[c]) clearOptionText(c,fnRadioPrevious[c]);
			fnRadioPrevious[c] = v;
		} else {
			o.src = base+'_normal.gif';
			document.getElementById(c).value = '';
		}
	} else if (a == 'click_text_box') {
		if (! clicked) {
			r = eval('Obj'+c);
			o.src = base+'_click.gif';
			document.getElementById(c).value = v;
			for(i=0;i<r.members.length;i++) { if (!(r.members[i]==o)) r.members[i].src = base+'_normal.gif'; }
			if (fnRadioPrevious[c]) clearOptionText(c,fnRadioPrevious[c]);
			fnRadioPrevious[c] = v;
		}
	}
	if (!clicked) {
		if (a == 'over') o.src = base+'_over.gif';
		if (a == 'normal') o.src = base+'_normal.gif';
	}
}

function limitTextArea(o, chars) {
	if (chars == 0) return;
	if (o.value.length > chars) {
		o.value = o.value.substr(0, chars);
	}
}

function selectOnly(mode, c, so, eid, srcbox) {
	if (mode == "standard") {                    
		if (so != "1") {
			var srcSO = eval('selectOnlyRef'+c);
			if (srcSO == "0") return false;                           

			var isAnIndex = srcSO.split(/\,/);
			for (x in isAnIndex) {
				var soRef = isAnIndex[x].split(/\:/);
				fnCheckbox(c, soRef[0], 'unclick', soRef[1]);
				fnCheckbox(c, soRef[0], 'normal', 0);
			} 
			return false;
		}
		var o = eval('boxRange'+c);
		var BREids = eval('boxRangeEids'+c);
		var eids = BREids.split(/\,/);   
		
		var clicker = document.getElementById(srcbox);
		var m = clicker.src.match(/check_click\.gif$/);
		var wasChecked = m != null ? false : true;
		                               
		var index = 0;
		var i;
		for (i = parseInt(o[0]); i <= parseInt(o[1]); i++) {      
			var box = document.getElementById('box'+i);
			if (box != null) {               
				fnCheckbox(c, 'box'+i, 'unclick', eids[index]);
				fnCheckbox(c, 'box'+i, 'normal', 0);
			}
			index++;
		}                                     

		if (wasChecked) {
			fnCheckbox(c ,srcbox,'unclick', eid);
		} else { 
			fnCheckbox(c ,srcbox,'click', eid);
		} 
	} else if (mode == "simple") {
		if (so != "1") {
			var srcSO = eval('selectOnlyRef'+c);
			var elements = document.getElementsByName(c);
			var isAnIndex = srcSO.split(/\,/);
			for (i = 0; i < elements.length; i++) {
				for (x in isAnIndex) {
					if (elements[i].value == isAnIndex[x]) elements[i].checked = false;
				}
			}
			return false;
		}
		var elements = document.getElementsByName(c);
		for (i = 0; i < elements.length; i++) {
			if (elements[i].value != eid) elements[i].checked = false;
		}
	}
}

function fnCheckbox(c,i,a,v) {
	// c = class, i = element id, a = action, v = value
	var clicked = false;
	var o = document.getElementById(i);
	var path = o.src.split('check');
	if (path[1] == '_click.gif') clicked = true;

	if (a == 'click') {
		if (clicked) {
			o.src = path[0]+'check_over.gif';
			lr(c,v);
			clearOptionText(c,v);
		} else {
			o.src = path[0]+'check_click.gif';
			la(c,v);
		}
	} else if (a == 'click_text') {
		if (clicked) {
			o.src = path[0]+'check_normal.gif';
			lr(c,v);
		} else {
			o.src = path[0]+'check_click.gif';
			la(c,v);
		}
	} else if (a == 'click_text_box') {
		if (! clicked) {
			o.src = path[0]+'check_click.gif';
			la(c,v);
		}
	} else if (a == 'unclick') {
		o.src = path[0]+'check_over.gif';
		lr(c,v);
		clearOptionText(c,v);
	}
	if (!clicked) {
		if (a == 'over') o.src = path[0]+'check_over.gif';
		if (a == 'normal') o.src = path[0]+'check_normal.gif';
	}
	
}

function clearOptionText(c,v) {
	if (c == null) return;
	var to = eval('document.surveyForm.'+ c.replace(/^./,'Q'));
	if (to == null) {
		var bits = c.split("_");
		var p = bits[0] + '_' + bits[1] + '_' + v;
		to = eval('document.surveyForm.'+ p.replace(/^./,'Q'));
	}
	if (to == null) to = eval('document.surveyForm.'+ c.replace(/^./,'O'));
	if (to == null) {
		var p = c + '_' + v;
		to = eval('document.surveyForm.'+ p.replace(/^./,'O'));
	}
	if (to == null) to = eval('document.surveyForm.'+ c.replace(/^./,'M'));
	if (to == null) {
		var p = c + '_' + v;
		to = eval('document.surveyForm.'+ p.replace(/^./,'M'));
	}
	if ((to != null) && (to.type == "text")) {
		to.value = "";
	} else if ((to != null) && (to.type == "textarea")) {
		to.value = "";
	}
	
}

function simpleComponentHook(c,v) {
	clearOptionText(c,v);
}

// Updates the total for a Constant Sum question when a value is entered

function CSTotal(c) {
	// c = class
	var t = 0;
	r = eval('Obj'+c);
	for(i=0;i<r.members.length;i++) {
		var v = r.members[i].value;
		var w = new String(v);
		if (w.match(/^%+$/)) {
			r.members[i].value = 0;
		} else if (w.match(/%/)) {
			var x = w.replace(/%/,'');
			r.members[i].value = parseInt(x);
		}
		var addWidth;
		if(isNaN(parseInt(r.members[i].value))) {
			addWidth = 0;
			if (r.members[i].value != "") r.members[i].value = "";
		} else {
			addWidth = parseInt(r.members[i].value);
		} 
		t += addWidth;
	}
	document.getElementById('total_'+c).value = t;
}

// BOING! The rollover function

function zapI(id,a) {
	// id = element id, a = action
	var clicked = false;
	var o = null;
	o = document.getElementById(id);
	if (o == null) { return }
	var base = o.src.slice(0,o.src.lastIndexOf('_'));
	if(o.src.search('click') > -1) clicked = true;

    if (a == 'click') {
		o.src = base+'_click.gif';
	}
	if (!clicked) {
		if (a == 'over') o.src = base+'_over.gif';
		if (a == 'normal') o.src = base+'_normal.gif';
	}
	if (a == 'reset') {
		o.src = base+'_normal.gif';
	}
}

// Adds a value to the hidden input for a checkbox object

function la(c,v) {
	var l;
	var a = new myArray();
	if (l = document.getElementById(c).value) a = l.split(':');
	var found = get_index(a,v);
	if (found == -1) a[a.length] = v;
	document.getElementById(c).value = a.join(':');
}

// Removes a value from the hidden input for a checkbox object

function lr(c,v) {
	var l;
	var a = new myArray();
	if (l = document.getElementById(c).value) a = l.split(':');
	var found = get_index(a,v);
	if (found > -1) a = splice(a,found);
	document.getElementById(c).value = a.join(':');
}

// IE does not support the Array.splice method, so we roll our own

function splice(a,p) {
    var b = new myArray(), c = new myArray();
    if (p == 0) { b = a.slice(1);
    } else if (p == a.length) { b = a.slice(0,-1);
    } else { b = a.slice(0,p); c = a.slice(p+1);
    }
    return b.concat(c);
}

// Returns the index of s in the array a, or -1 if not found

function get_index(a,s) {
    var index = -1;
    for(var i = 0;i<a.length;i++) {
        if (a[i] == s) index = i;
    }
    return index;
}

// Validates the rank question's answers

function checkRank(c,s) {
	// c = class (ie base select name)
	// s = actual select name (value of c with the row eid on the end)
	var v = document.getElementById(s).value;
	if (v == '') return;
	var o = eval('Obj'+c);
	for(i=0;i<o.members.length;i++) {
		if (s != o.members[i].id) {
			if (v == o.members[i].value) {
				o.members[i].selectedIndex = 0
			}
		}
	}
}

// Bigass validator for required questions. Also validates any dates / times
// and totals for Constant Sum qs even for questions which aren't required.
var keepBoxStyle = new Array();

function checkRequired(q,t,c,n,qt) {
	// q = question number
	// t = type (enum: text_int,text_float,constant_sum,constant_sum_req,timestamp,timestamp_req,rank,rank_req)
	// c = class (ie (hidden) input name)
	// n = exact number of answers to be given to this question
	/*
	 * If the theme uses standard internet controls (ie document.standard_internet_theme is
	 * true) then we need different checks for the following question types:
	 * radio,checkbox,scale
	 * This is because we have to iterate over the group members rather than just checking
	 * the underlying text input as we do when using the cooked javascript driven themes.
	 * Here we do some logic to decide what to do down below.
	 */

    for (x in keepBoxStyle) {
		var o = eval('document.surveyForm.'+x);
		if (o != null) {
			if (keepBoxStyle[x] != null) {
				o.style.borderColor = keepBoxStyle[x];
				keepBoxStyle[x] = null;
			}
		}
	}

	var do_standard_internet_theme_check = 0;
	if (t == 'radio' || t == 'checkbox' || t == 'scale' || t == 'other') {
		if (document.standard_internet_theme) do_standard_internet_theme_check = 1;
	}
	var q_prefix;
	if (q != '') {
		q_prefix = "Question Number "+q+": \""+qt+"\".\n";
	} else {
		q_prefix = "Question: \""+qt+"\".\n";
	}
	//if (q > 0) {
	//	q_spec = 'question '+q;
	//} else {
	//	q_spec = t.replace(/_/,' ');
	//	q_spec = q_spec.replace(/req/,'');
	//	q_spec = 'a '+q_spec+' question';
	//}
	if (! document.userRequiredQAlert) {
		document.requiredQAlert = q_prefix+'You haven\'t completed this question. Please answer this question fully before trying to continue.';  // Please note that all questions marked with an asterisk (*) are required and must be answered before continuing
	}
	document.requiredQTextAlert = q_prefix+'No required text entered for this question. Please enter text or select a different answer to continue';

	if (t == 'text_int') {
		var o = eval('document.surveyForm.'+c);
		if (keepBoxStyle[""+c] == null) {
			keepBoxStyle[""+c] = o.style.borderColor;
		} else {
			o.style.borderColor = keepBoxStyle[""+c];
		}
		var asInt = parseInt(o.value,10);
		//if (o.value != '' && (isNaN(o.value) || asInt != o.value)) {
		if ((o.value != "") && (o.value.match(/^[\d\-]*$/) == null)) {
			o.style.borderColor = "#FF0000";
			window.scrollTo(o.offsetLeft, o.offsetTop); 
			alert(q_prefix+'Please only enter a whole number for this question. The number you enter may not contain a decimal point or any other characters such as letters, symbols, punctuation etc');
			return true;
		} else if ((asInt < -2147483648) || (asInt > 2147483647)) {
			o.style.borderColor = "#FF0000";
			window.scrollTo(o.offsetLeft, o.offsetTop); 
			alert(q_prefix+'Please only enter a whole number for this question between\n-2147483648 and 2147483647. The number you enter may not contain a decimal point or any other characters such as letters, symbols, punctuation etc');
		    return true;
		}
	} else if (t == 'text_float') {            
		var floatMax = Math.pow(10, 27) - 1;
		var floatMin = Math.pow(10, 27) * -1;
		var o = eval('document.surveyForm.'+c);
		if (keepBoxStyle[""+c] == null) {
			keepBoxStyle[""+c] = o.style.borderColor;
		} else {
			o.style.borderColor = keepBoxStyle[""+c];
		}
		var asFloat = parseFloat(o.value,10);
		//if (o.value != '' && (isNaN(o.value) || asFloat != o.value)) {
		if ((o.value != "") && (o.value.match(/^[\d\-\.]*$/) == null)) {	
			o.style.borderColor = "#FF0000";
			window.scrollTo(o.offsetLeft, o.offsetTop); 
			alert(q_prefix+'Please only enter a number for this question. The number may contain a decimal point, but no other characters such as letters, symbols, punctuation etc');
		  	return true;
		} else if ((asFloat < floatMin) || (asFloat > floatMax)) {
			o.style.borderColor = "#FF0000";
			window.scrollTo(o.offsetLeft, o.offsetTop); 
			alert(q_prefix+'Please only enter a number for this question between\n-10^27 and 10^27. The number may contain a decimal point, but no other characters such as letters, symbols, punctuation etc');
		  	return true;
		}
	} else if (t == 'dec_places') {	
		var floatMax = Math.pow(10, 27) - 1;
		var floatMin = Math.pow(10, 27) * -1;
		var o = document.getElementById(c);
		if (keepBoxStyle[""+c] == null) {
			keepBoxStyle[""+c] = o.style.borderColor;
		} else {
			o.style.borderColor = keepBoxStyle[""+c];
		}
		var asFloat = parseFloat(o.value,10);
		if ((o.value != "") && (o.value.match(/^[\d\-\.]*$/) == null)) {
		//if (o.value != '' && (isNaN(o.value) || asFloat != o.value)) { 
			o.style.borderColor = "#FF0000";
			window.scrollTo(o.offsetLeft, o.offsetTop); 
			alert(q_prefix+'Please only enter a number for this question. The number may contain a decimal point, but no other characters such as letters, symbols, punctuation etc');
		  	return true;
		} else if ((asFloat < floatMin) || (asFloat > floatMax)) {
			o.style.borderColor = "#FF0000";
			window.scrollTo(o.offsetLeft, o.offsetTop); 
			alert(q_prefix+'Please only enter a number for this question between\n-10^27 and 10^27. The number may contain a decimal point, but no other characters such as letters, symbols, punctuation etc');
		  	return true;
		}	 
                         
		if (o.value != "") {
			var numberElements = o.value.split(/\./);
			if (numberElements.length > 1) {
				if (numberElements[1].length != n) {
					o.style.borderColor = "#FF0000";
					window.scrollTo(o.offsetLeft, o.offsetTop); 
					alert(q_prefix+"Please enter a number which has "+n+" decimal places for this question");
					return true;
				}
		    }
		}
	} else if (t == 'text_range_integer') {
		var o = eval('document.surveyForm.'+c);
		if (keepBoxStyle[""+c] == null) {
			keepBoxStyle[""+c] = o.style.borderColor;
		} else {
			o.style.borderColor = keepBoxStyle[""+c];
		}
		                           
		var intMax = "2147483647";
		var intMin = "-2147483648";
		
		var use_max = intMax;
		var use_min = intMin;
		var asInt = parseInt(o.value,10);
		var hasRange = 0;
		
		var min = eval('textRangeStart'+c);
		if ((min != null) && (min != "")) {
			use_min = min;
			hasRange++;
		}
		var max = eval('textRangeEnd'+c);
		if ((max != null) && (max != "")) {
			use_max = max;
			hasRange++;
		}   

		if (o.value != "") {
			if (o.value.match(/^[\d\-]*$/) == null) {
				o.style.borderColor = "#FF0000";
				window.scrollTo(o.offsetLeft, o.offsetTop); 
				alert(q_prefix+'The number you enter may not contain a decimal point or any other characters such as letters, symbols, punctuation etc.'); 
				return true;
			} else if (asInt < parseInt(min)) {
				o.style.borderColor = "#FF0000";
				window.scrollTo(o.offsetLeft, o.offsetTop);
				var appendMessage = hasRange == 2 ? "Please only enter a whole number between \n"+min+" and "+max+" for this question." : "Please only enter a whole number, \n"+min+" and above for this question.";
				alert(q_prefix+"The number you have entered is lower\nthan the specified limit of "+min+".\n"+appendMessage);
				return true;
			} else if (asInt > parseInt(max)) {
				o.style.borderColor = "#FF0000";
				window.scrollTo(o.offsetLeft, o.offsetTop);
				var appendMessage = hasRange == 2 ? "Please only enter a whole number between \n"+min+" and "+max+" for this question." : "Please only enter a whole number, \n"+max+" or lower for this question.";
				alert(q_prefix+"The number you have entered is higher\nthan the specified limit of "+max+".\n"+appendMessage);
				return true;
			} else if (asInt < parseInt(intMin)) {
				o.style.borderColor = "#FF0000";
				window.scrollTo(o.offsetLeft, o.offsetTop);
				alert(q_prefix+"The number you have entered is lower\nthan the system limit of "+intMin+".\nPlease only enter a whole number between \n"+intMin+" and "+use_max+" for this question.");
				return true;
			} else if (asInt > parseInt(intMax)) {
				o.style.borderColor = "#FF0000";
				window.scrollTo(o.offsetLeft, o.offsetTop);
				alert(q_prefix+"The number you have entered is higher\nthan the system limit of "+intMax+".\nPlease only enter a whole number between \n"+use_min+" and "+intMax+" for this question.");
				return true;
			}          
	   	}
	   
	} else if (t == 'text_range_float') {  
		var o = eval('document.surveyForm.'+c);
		if (keepBoxStyle[""+c] == null) {
			keepBoxStyle[""+c] = o.style.borderColor;
		} else {
			o.style.borderColor = keepBoxStyle[""+c];
		}
		                           
		var floatMax = Math.pow(10, 27) - 1;
		var floatMin = Math.pow(10, 27) * -1;
		var hasRange = 0;
		
		var use_max = floatMax;
		var use_min = floatMin;
		var asFloat = parseFloat(o.value,10);
		
		var min = eval('textRangeStart'+c);
		if ((min != null) && (min != "")) {
			use_min = min;
			hasRange++;
		}
		var max = eval('textRangeEnd'+c);
		if ((max != null) && (max != "")) {
			use_max = max;
			hasRange++;
		}
		
		if (o.value != "") {
			if (o.value.match(/^[\d\-\.]*$/) == null) {
				o.style.borderColor = "#FF0000";
				window.scrollTo(o.offsetLeft, o.offsetTop); 
				alert(q_prefix+'The number may contain a decimal point, but no other characters such as letters, symbols, punctuation etc'); 
				return true;
			} else if (asFloat < parseFloat(min)) {
				o.style.borderColor = "#FF0000";
				window.scrollTo(o.offsetLeft, o.offsetTop); 
				var appendMessage = hasRange == 2 ? "Please only enter a number between \n"+min+" and "+max+" for this question." : "Please only enter a number, \n"+min+" and above for this question.";
				alert(q_prefix+"The number you have entered is lower\nthan the specified limit of "+min+".\n"+appendMessage);
				return true;
			} else if (asFloat > parseFloat(max)) {
				o.style.borderColor = "#FF0000";
				window.scrollTo(o.offsetLeft, o.offsetTop); 
				var appendMessage = hasRange == 2 ? "Please only enter a number between \n"+min+" and "+max+" for this question." : "Please only enter a number, \n"+max+" or lower for this question.";
				alert(q_prefix+"The number you have entered is higher\nthan the specified limit of "+max+".\n"+appendMessage);
				return true;
			} else if (asFloat < parseFloat(floatMin)) {
				o.style.borderColor = "#FF0000";
				window.scrollTo(o.offsetLeft, o.offsetTop); 
				alert(q_prefix+"The number you have entered is lower\nthan the system limit of "+floatMin+".\nPlease only enter a number between \n"+floatMin+" and "+use_max+" for this question.");
				return true;
			} else if (asFloat > parseFloat(floatMax)) {
				o.style.borderColor = "#FF0000";
				window.scrollTo(o.offsetLeft, o.offsetTop); 
				alert(q_prefix+"The number you have entered is higher\nthan the system limit of "+floatMax+".\nPlease only enter a number between \n"+use_min+" and "+floatMax+" for this question.");
				return true;
			}
	   	}
	} else if (t == 'constant_sum') {
		var tot,req;
		CSTotal(c);
		tot = document.getElementById('total_'+c).value;
		req = document.getElementById('req_'+c).value;
		if (tot == req || tot == 0) {
			return false;
		} else if (isNaN (tot) || parseInt(tot,10) != tot) {
			alert(q_prefix+'You have entered some non-numeric answers for this question. Please correct this and try again.');
			return true;
		} else {
			alert(q_prefix+'The answers to this question don\'t add up to '+req+'. You don\'t have to answer the question, but if you do, your answers must add up to '+req+'.');
			return true;
		}
	} else if (t == 'constant_sum_req') {
		var tot,req;
		CSTotal(c);
		tot = document.getElementById('total_'+c).value;
		req = document.getElementById('req_'+c).value;
		if (tot == req) {
			return false;
		} else if (isNaN (tot) || parseInt(tot) != tot) {
			alert(q_prefix+'You have entered some non-numeric answers for this question. Please correct this and try again.');
			return true;
		} else {
			alert(q_prefix+'The answers to this question don\'t add up to '+req+'.');
			return true;
		}
	} else if (t == 'rank') {
		/*
		var o = eval('Obj'+c);
		var ops = o.members[0].length;	
		var mem = o.members.length;
		
		if (ops > mem) ops = mem;
		
		var count = 0;
		for (i = 0; i < ops; i++) {
			if (o.members[i].value != '') count++;
		}
		var retval = true;
		if (count >= 1) {
			if (count < ops) {
				alert('You must give exactly '+n+' answers for '+q_spec);
			} else if (count >= ops) {
				retval = false;
			}
		} else {
			retval = false;
		}
		return retval;
		*/
		return false;
	} else if (t == 'rank_req') {
		var o = eval('Obj'+c);
		var ops = o.members[0].length - 1;
		var mem = o.members.length;
		
		if (ops > mem) ops = mem;
		
		var count = 0;
		for (i = 0; i < mem; i++) {
			if (o.members[i].value != '') count++;
		}

		var retval = true;
		if (count < 1) {
			alert(q_prefix+'You haven\'t completed this question. You must answer each part.');
		} else if (count < ops) {
			alert(q_prefix+'You must give exactly '+n+' answers for this question');
		} else if (count >= ops) {
			retval = false;
		}
		return retval;
	} else if (t == 'timestamp') {
		if (c.search(/^[DE]/) > -1) {
			var year = document.getElementById(c+'_year').value;
			var month = document.getElementById(c+'_month').value;
			var day = document.getElementById(c+'_day').value;
			if (!(year == '' && month == '' && day == '')) {
				var vD = validateDate(year,month,day);
				if (vD == false) return false;
				alert(q_prefix+'The '+vD+' is invalid for this question');
				return true;
			}
		}
		if (c.search(/^[DF]/) > -1) {
			var hour = document.getElementById(c+'_hour').value;
			var min = document.getElementById(c+'_min').value;
			var meridian = getRadioValue(eval('document.surveyForm.'+c+'_meridian'));
			if (!(hour == '' && min == '')) {
				/*
				if (hour<1) {
					alert('The hour is invalid for '+q_spec);
					return true;
				} else if (min=='') {
					alert('The minute is invalid for '+q_spec);
					return true;
				} else if (meridian != 'AM' && meridian != 'PM') {
					alert('The meridian is invalid for '+q_spec);
					return true;
				}
				*/
				if (hour<1 || hour>12) {
					alert(q_prefix+'The hour is invalid for this question');
					return true;
				} else if (min<0 || min>59) {
					alert(q_prefix+'The minute is invalid for this question');
					return true;
				} else if (meridian != 'AM' && meridian != 'PM') {
					alert(q_prefix+'The meridian is invalid for this question');
					return true;
				}
			}
		}
	} else if (t == 'timestamp_req') {
		if (c.search(/^[DE]/) > -1) {
			var year = document.getElementById(c+'_year').value;
			var month = document.getElementById(c+'_month').value;
			var day = document.getElementById(c+'_day').value;
			var vD = validateDate(year,month,day);
			if (vD == false) return false;
			alert(q_prefix+'The '+vD+' is invalid for this question');
			return true;
		}
		if (c.search(/^[DF]/) > -1) {
			var hour = document.getElementById(c+'_hour').value;
			var min = document.getElementById(c+'_min').value;
			var meridian = getRadioValue(eval('document.surveyForm.'+c+'_meridian'));
			if (hour<1 || hour>12) {
				alert(q_prefix+'The hour is invalid for this question');
				return true;
			} else if (min<0 || min>59) {
				alert(q_prefix+'The minute is invalid for this question');
				return true;
			} else if (meridian != 'AM' && meridian != 'PM') {
				alert(q_prefix+'The meridian is invalid for this question');
				return true;
			}
		}
	} else if (t == 'other') {
		if (do_standard_internet_theme_check) {
			var o = eval('document.surveyForm.'+c);
			var i,found = true;
			// If there is only a single form control in a particular class, then
			//	javascript will helpfully(!) return an HTMLObject instead of an
			//	array
			if (o.length) {
				for(i=0;i<o.length;i++) {
					if (o[i].checked || o[i].selected) {
						var to = eval('document.surveyForm.'+c.replace(/^./,'O') + '_' + o[i].value);
						if (to) {
							found = false;
							if (to.value) found = true;
							if (!found) break;
						}
					}
				}
			} else {
				if (o.checked || o.selected) {
					var to = eval('document.surveyForm.'+c.replace(/^./,'O') + '_' + o.value);
					if (to) {
						found = false;
						if (to.value) found = true;
					}
				}
			}
			if (found) {;
				return false;
			} else {
				alert(document.requiredQTextAlert);
				return true;
			}
		} else {
			var o = document.getElementById(c);	
			if (o.value == '') {
				return false;
			} else {
				// Ok, so they clicked something. Now we have to check for empty
				//	text boxes

				var eids = o.value.split(/:/);
				var found = true;
				var whoa = new Array();
				whoa = c.split('_');
				if (whoa.length < 3) {
					for(i=0;i<eids.length;i++) {
						var to = eval('document.surveyForm.'+c.replace(/^./,'O') + '_' + eids[i]);
						if (to) {
							found = false;
							if (to.value) {
								found = true;
								var findSpaces = to.value;
								var matches = findSpaces.match(/^\s+$/);
								if (matches != null) found = false;
							}
							if (!found) break;
						}
					}
					if (found) {
						return false;
					} else {
						alert(document.requiredQTextAlert);
						return true;
					}
				} else {
					var to;
					//if (whoa.length > 3) {
					//	to = eval('document.surveyForm.'+c.replace(/^./,'O'));
					//} else {
						to = eval('document.surveyForm.'+c.replace(/^./,'Q'));
					//}
					if (to) {
						found = false;
						if (to.value) {
							found = true;
							var findSpaces = to.value;
							var matches = findSpaces.match(/^\s+$/);
							if (matches != null) found = false;
						}
					}
					if (found) {
						return false;
					} else {
						alert(document.requiredQTextAlert);
						return true;
					}
				}
			}
		}
	} else if (do_standard_internet_theme_check) {
		var o = eval('document.surveyForm.'+c);
		var i;
		var count = 0;
		var retval = true;
		var found = false;
		var selectedValue;
		var isAnIndex = [];
		
		try {
	   		var srcSO = eval('selectOnlyRef'+c);
   			isAnIndex = srcSO.split(/\,/);
		} catch(e) {
			//alert(""+e);
		}
				
		// If there is only a single form control in a particular class, then
		//	javascript will helpfully(!) return an HTMLObject instead of an
		//	array
		if (o.length) {
			for(i=0;i<o.length;i++) {
				if (o[i].checked || o[i].selected) {
					for (x in isAnIndex) {
						if (isAnIndex[x] == o[i].value) n = 0;
					}
					found = true;
					count++;
					selectedValue = o[i].value;
				}
			}
		} else {
			if (o.checked || o.selected) {
				for (x in isAnIndex) {
					if (isAnIndex[x] == o.value) n = 0;
				}
				found = true;
				count++;
				selectedValue = o.value;
			}
		}

		var foundText = false;
		var hasText = false;
		var to = eval('document.surveyForm.'+ c.replace(/^./,'O'));
		if (to == null) {
			var p = c + '_' + selectedValue;
			to = eval('document.surveyForm.'+ p.replace(/^./,'O'));
		}
		if (to == null) to = eval('document.surveyForm.'+ c.replace(/^./,'Q'));
		if (to == null) {
			foundText = false;
		} else {
			hasText = true;
			if (to.value != '') {
				var findSpaces = to.value;
				var matches = findSpaces.match(/^\s+$/);
				if (matches == null) foundText = true;
			}
		}
        
		if (n > 0) {
			if (count == n) {
				retval = false;
			} else {
				alert(q_prefix+'You must give exactly '+n+' answers for this question');
			}
		} else if ((found == false) && (foundText == true)) {
			alert(q_prefix+"In this question your optional heading cannot be submitted unless an option on that row is selected.");
			retval = true;
		} else if ((found == true) && (foundText == true)) {
			retval = false;
		} else if ((found == false) && (foundText == false) && (hasText == true)) {
			retval = false;
		} else if ((found == true) && (to == null)) {
			retval = false;
		} else {
			alert(document.requiredQAlert);
		}
		
		return retval;
	

	} else if (t == 'mandatory_answers') {
		var o = document.getElementById(c);
  			
		// ignore mandatory checks if  running in select only
		var srcSO = eval('selectOnlyRef'+c);
		var m = srcSO.match(/\:/);
		if (m != null) {
			var isAnIndex = srcSO.split(/\,/);
			for (x in isAnIndex) {
				var soRef = isAnIndex[x].split(/\:/);
				var reg = new RegExp(""+soRef[1]);
				m = o.value.match(reg);
				if (m != null) return false;
			}
		}
		   
		retval = true;

		if (n > 0 && o.value != '') {
			var count = o.value.split(/:/).length;
			if (count == n) {
				retval = false;
			} else {
				alert(q_prefix+'You must give exactly '+n+' answers for this question');
			}
		} else { retval = false; }
		return retval;
	} else if (t == 'email') {
		var o = document.getElementById(c);
		var src = o.value;
		if (src != "") {
			src = src.replace(/^\s+/, "");
			src = src.replace(/\s+$/, "");
			var m = src.match(/^[\w\.\-\+\&]+\@[\w\.\-]+\.[\w]{2,4}$/);
			if (m == null) {
				alert(q_prefix+"Please supply a valid email address for this question.");
				return true;
			}
		}
	} else {		
		var o = document.getElementById(c);
		retval = true;
		if (o.value == '') {
			if 	(document.surveyForm.selector_lookup != null) {
				var oo = document.getElementById("SLT_"+c);
				if (oo != null) {
					if (oo.checked == true) retval = false;
				} else {
					var pieces = c.split("_");
					var col_eid = pieces.pop();
					var tag = pieces.join("_");
					var oo = document.getElementById("SLT_"+tag);
					var elements = oo.value.split(':');
					for (i = 0; i < elements.length; i++) {
						if (elements[i] == col_eid) retval = false;
					}
				}
				
				if (retval == true) {
					alert(document.requiredQAlert);
				}
			} else {
				alert(document.requiredQAlert);
			}
		} else {
			retval = false;
		}
		return retval;
	}  

/*
else {
		var o = document.getElementById(c);
		retval = true;
		if (n > 0 && o.value != '') {
			var count = o.value.split(/:/).length;
			if (count == n) {
				retval = false;
			} else {
				alert('You must give exactly '+n+' answers for '+q_spec);
			}
		} else if (o.value == '') {
			alert(document.requiredQAlert);
		} else {
			retval = false;
		}
		return retval;
	}
*/

}

// Rudimentary preloadImage script. Should probably work more closely with nda

function preloadImages() {
	var i = null;
	for(i=0;i<arguments.length;i++) {
		imageArray.push(new Image);
		imageArray[i].src = arguments[i];
	}
	if (noDOM) { if (no_nda) nda = new nd() }
}

// Load timestamp questions with previous answers. Just too damn long to do
// inline

function initTimestamp(c,h,n,d,m,y,a) {
	// c = class, y = year, m = month, d = day, h = hour, n = min, a = meridian (AM/PM)
	// To distinguish between full timestamp, just date and just time, use the
	// first letter of class: D = full, E = date only, F = time only
	if (c.search(/^[DE]/) > -1) {
		document.getElementById(c+'_year').value = y;
		clickDropdown(c+'_month',m);
		clickDropdown(c+'_day',d);
	}
	if (c.search(/^[DF]/) > -1) {
		clickDropdown(c+'_hour',h);
		clickDropdown(c+'_min',n);
		clickRadio(c+'_meridian',a);
	}
}

// Validate dates

function validateDate(year,month,day) {
	// Returns year,month or day if the date is NOT valid
	// Returns false if the date is valid
	var monthdays = new myArray(31,28,31,30,31,30,31,31,30,31,30,31);

	if (year % 100 == 0) {
		if (year % 400 == 0) monthdays[1] = 29;
	} else {
		if (year % 4 == 0) monthdays[1] = 29;
	}
	if (year<1) {
		return 'year';
	} else if (month<1 || month>12) {
		return 'month';
	} else if (day<1 || day>monthdays[month-1]) {
		return 'day';
	}
	return false;
}

function editQuestionQuotas() {
	f=document.surveyForm;
	f.command.value = 'edit_question_quotas';
	f.action = '/sarge';
	f.submit();
}

function editCustomReportQuestionnaireEmails(qncrid) {
	f=document.emailForm;
	f.command.value = 'edit_custom_report_questionnaire_emails';
	f.qncrid.value = qncrid;
	f.action = '/admin';
	f.submit();
}

function editCustomReportUpdate(qncrid) {
	f=document.updateForm;
	f.visible.value = document.getElementById("visible_"+qncrid).checked == true ? '1' : '0';
	f.active.value = document.getElementById("active_"+qncrid).checked == true ? '1' : '0';
	f.notify.value = document.getElementById("notify_"+qncrid).checked == true ? '1' : '0';
	f.run_order.value = document.getElementById("run_order_"+qncrid).value;
	f.command.value = 'wl_custom_report_update';
	f.qncrid.value = qncrid;
	f.action = '/admin';
	f.submit();
}

function trashManagement() {
	f=document.surveyForm;
	f.command.value = 'trash_management';
	f.action = '/admin';
	f.submit();
} 

/*
var shifted = false;

function radioToggle(aRef) {
	if (aRef.checked == true && shifted == true) {
		aRef.checked = false;
		shifted = false;
	}
}

function keyInit() {
    if (document.addEventListener)
    {
       document.addEventListener("keydown",keydown,false);
	   document.addEventListener("keyup",keyup,false);
    }
    else if (document.attachEvent)
    {
       document.attachEvent("onkeydown", keydown);
	   document.attachEvent("onkeyup", keyup);
    }
    else
    {
       document.onkeydown= keydown;
	   document.onkeyup= keyup;
    }
}

function keydown(e) {
   	if (!e) e= event;
	if (e.keyCode == 27) {
   		shifted = true;
	}
   	return e.keyCode;
}

function keyup(e) {
   	if (!e) e= event;
	if (e.keyCode == 27) {
   		shifted = false;
	}
   	return e.keyCode;
}
*/
var state = new Array();
var stateName = new Array();
var stateValue = new Array();
var previous = new Array();

function radioToggle(aRef) {
	var index = aRef.id;
	var name = aRef.name;
	simpleComponentHook(stateName[previous[name]], stateValue[previous[name]]);
	if (aRef.checked == true && previous[name] == index && state[previous[name]] == true) {
		aRef.checked = false;
	}
	stateName[index] = aRef.name;
	stateValue[index] = aRef.value;
	state[index] = aRef.checked;
	previous[name] = index;
}

function deleteSelectedQuestions() {
	f=document.surveyForm;
	f.command.value = 'delete_selected_questions';
	f.action = '/sarge';
	f.submit();
}

function searchAddressBookList() {
	f=document.surveyForm;
	f.command.value = 'address_book';
	f.action = '/sarge';
	f.submit();
}

var limitColour;

function measureChars(cid, limit) {
	f=document.surveyForm;
	var c = document.getElementById(cid);
	var l = document.getElementById('limit_'+cid);
	var contents = c.value;
	var left = limit - contents.length;
	if (contents.length >= limit) {
		l.innerHTML = "0";
		c.value = c.value.substring(0, limit);
		l.style.color = "#FF0000";
	} else {
		l.innerHTML = "" + left;
		l.style.color = limitColour;
	}
}

function loadChars() {
	var elements = document.getElementsByTagName("div");
	for (i = 0; i < elements.length; i++) {
		var matches = elements[i].id.match(/^limit_(.*)$/);
		if (matches != null) {
			while (matches.length != 0) {
				var div = matches.shift();
				var id = matches.shift();
				var theDiv = document.getElementById(div);
				var theId = document.getElementById(id);
				var limit = theDiv.innerHTML;
				var chars = limit - theId.value.length;
				limitColour = document.getElementById(div).style.color;
				if (chars < 0) {
					theDiv.innerHTML = "0";
					theId.value = theId.value.substring(0, limit);
					theDiv.style.color = "#FF0000";
				} else {
					theDiv.innerHTML = chars;
				}
			}
		}
	}
}

function testLink(link, name) {
	link = link.replace(/http:\/\//gi, "");
	link = "http://" + link;
	var win=window.open(link, name); 
	return false;
}

function optOutTextBox(mode, ref, box) {
	if 	(document.surveyForm.selector_lookup != null) {
		if (mode == "standard") {
			var o = document.getElementById("SLT_"+ref);
			o.checked = false;
		} else {
			var pieces = ref.split("_");
			var col_eid = pieces.pop();
			var tag = pieces.join("_");
			fnCheckbox("SLT_"+tag, box, "unclick", col_eid);
		}
	}
}

function optOutCheckBox(mode, ref, value) {
	if 	(document.surveyForm.selector_lookup != null) {
		if (mode == "standard") { 
			ref = ref.replace(/^SLT_/, '');
			var o = document.getElementById(ref);
			o.value = "";
		} else {
			ref = ref.replace(/^SLT_/, '');
			var tag = ref+"_"+value;
			var o = document.getElementById(tag);
			o.value = "";
		}
	}
}

function selectAddOrCopy(stage) {
	f=document.surveyForm;
	f.action = '/sarge';
	f.stage.value = stage;
	f.submit();
}
function projectDelete(fid, title) {
	f = document.surveyForm;
	var c = confirm("Are you sure you want to delete the project titled " + title + "?");
	if (c == true) {
		document.location = "/dashboard?command=delete_project&project_fid=" + fid;
	} else {
		alert("Your project was NOT deleted");
	}
}

function numbersOnly(o) {
	o.value = o.value.replace(/[^0-9]/,"");
}

function blockClose() {
	if (enableBlockClose != null) {
		//return enableBlockClose.check();
	}
}         

function clearRangeBoxes(c) {
	var list = document.getElementsByName(c);
	for (x in list) {
		if (list[x].checked == false) {
			if (list[x].value == "range_integer") {
				document.surveyForm.range_min_int.value = ""; 
				document.surveyForm.range_max_int.value = ""; 
			} else if (list[x].value == "range_float") {
				document.surveyForm.range_min_float.value = ""; 
				document.surveyForm.range_max_float.value = ""; 
			} else if (list[x].value == "dec_places") {
			   document.surveyForm.answer_count.value = ""; 
			} else if (list[x].value == "cv_ran_int") {
				document.surveyForm.cv_ran_int_min.value = ""; 
				document.surveyForm.cv_ran_int_max.value = ""; 
			} else if (list[x].value == "cv_ran_float") {
				document.surveyForm.cv_ran_flt_min.value = ""; 
				document.surveyForm.cv_ran_flt_max.value = ""; 
			} else if (list[x].value == "cv_dec") {
			   document.surveyForm.cv_dec_places.value = ""; 
			} else if (list[x].value == "ov_ran_int") {
				document.surveyForm.ov_ran_int_min.value = ""; 
				document.surveyForm.ov_ran_int_max.value = ""; 
			} else if (list[x].value == "ov_ran_float") {
				document.surveyForm.ov_ran_flt_min.value = ""; 
				document.surveyForm.ov_ran_flt_max.value = ""; 
			} else if (list[x].value == "ov_dec") {
			   document.surveyForm.ov_dec_places.value = ""; 
			}
		}
	}
}

function hotAlertEdit(qncrid, idx) { 
	f=document.surveyForm; 
	f.qncrid.value = qncrid;
	f.idx.value = idx;
	f.hotAlertMode.value = 'edit';
	f.submitme.value = "";
	f.submit();
}

function hotAlertDelete(qncrid, idx) {
	f=document.surveyForm; 
	f.qncrid.value = qncrid;
	f.idx.value = idx;
	f.hotAlertMode.value = 'delete';
	f.submitme.value = "";
	f.submit();
}

function hotAlertUpdate() { 
	f=document.surveyForm;
	f.hotAlertMode.value = 'update';
	f.submitme.value = "";
	f.submit();
}

function hotAlertActive(qncrid, idx, o) { 
	f=document.surveyForm;
	f.qncrid.value = qncrid;
	f.idx.value = idx;
	var swap = !o.checked;
	f.activeState.value = o.checked ? '1' : '0'; 
	f.hotAlertMode.value = 'active';
	f.submitme.value = "";
	f.submit();
}

function hotAlertAdd() { 
	f=document.surveyForm; 
	f.hotAlertMode.value = 'add';
	f.submitme.value = "";
	var doSub = true;
	if (f.hot_alert_rule.value.length > 0) {
		if (f.hot_alert_rule.value.length > 1200) {
			alert('The hot alert rule you have specified is too long!');
			doSub = false;
		}
	}
	if (doSub) f.submit();
}
