
var FormClass = {
	url: URL_ROOT + 'modules/form/form_submit.php',	// script where data is sent/received via HTTP class

	addQuestion: function (form_id, type, button) {	// A wrapper function to call the media chooser
		HTTP.connect({
			url: this.url,
			action: 'add_question',
			data: 'form_id=' + form_id + '&type=' + type,
			waiting: button,
			after: function() { document.location.href = document.location.href; }
		});		
	},

	deleteQuestion: function (question_id, button) {	// A wrapper function to call the media chooser
		var choice = window.confirm('Really delete this question?');
		if (choice) {
			HTTP.connect({
				url: this.url,
				action: 'delete_question',
				data: 'question_id=' + question_id,
				waiting: button,
				after: function() { document.location.href = document.location.href; }
			});		
		}
	},

	addMedia: function (event, question_id, option, button) {	// A wrapper function to call the media chooser
		this.question_id = question_id;
		this.option = option;
		this.button = button;
		FileClass.fileChooser(event, button, FormClass.saveMedia);
	},

	saveMedia: function (media_id) {	// callback after selecting in chooser
		FormClass.saveRadioCheckbox(FormClass.question_id,FormClass.button, 'media:' + FormClass.option + ':' + media_id);
	},

	addClick: function (event, question_id, option, button) {	// A wrapper function to call the media chooser
		this.question_id = question_id;
		this.option = option;
		this.button = button;
		FileClass.fileChooser(event, button, FormClass.saveClick);
	},

	mediaClick: function (media_id) {
		video_launch(media_id);
	},

	saveClick: function (media_id) {	// callback after selecting in chooser
		FormClass.saveRadioCheckbox(FormClass.question_id,FormClass.button, 'click:' + FormClass.option + ':' + media_id);
	},

	editQuestion: function (question_id, button) {	
		question_div = document.getElementById('form_question_' + question_id);	
		HTTP.connect({
			url: this.url,
			action: 'edit_question',
			data: 'question_id=' + question_id,
			waiting: button,
			update: question_div
		});
	},

	addOption: function (question_id, button) {
		this.saveRadioCheckbox(question_id, button, 'add:');
	},
	
	deleteOption: function (question_id, option_num, button) {
		var choice = window.confirm('Really delete this one?');
		if (choice)
			this.saveRadioCheckbox(question_id, button, 'delete:' + option_num);
	},

	// one save function needs to appear for the save button -- handler functions for specific cases
	saveQuestion: function (question_id, type, button) {
		switch (type) {
			case 'textarea':
			case 'textbox':
				this.saveTextTextarea(type, question_id, button);
				break;			
			case 'radio':
			case 'checkbox':
				this.saveRadioCheckbox(question_id, button, '');
				break;
			case 'select':
				this.saveSelect(question_id, button);
		}
	},

	saveRadioCheckbox: function (question_id, button, modify){
		question_div = document.getElementById('form_question_' + question_id);
		var inputs = question_div.getElementsByTagName('input');
		var numinputs = inputs.length;
		var input, text, value, tmp, option_id = '', options = '';
		for (i = 0; i < numinputs; i++) {
			input = inputs[i];
			if (!input.disabled && input.id) {
				value = input.value;
				if ( input.id.indexOf('_') != -1 ){	// fetch question text
					tmp = input.id.split('_');
					option_id = tmp[1];
					while ( value.search("'") != -1 ) {	// convert commas to HTML encodings (used as separator)
						value = value.replace("'", "&#39;"); // escape here because of ampersand in HTML entity
					}
					while ( value.search(",") != -1 ) {	// convert commas to HTML encodings (used as separator)
						value = value.replace(",", "&#44;"); // escape here because of ampersand in HTML entity
					}
					while ( value.search(":") != -1 ) {	// convert colons to HTML encodings (used as separator)
						value = value.replace(":", "&#58;"); // escape here because of ampersand in HTML entity
					}
					options += option_id + ':' + escape(value) + ',';	
				} else {	// otherwise must be option text
					text = value;
				}
			}
		}
		options = options.slice(0, -1); // remove last comma on end

		HTTP.connect({
			url: this.url,
			action: 'save_radiocheckbox',
			data: 'question_id=' + question_id + '&options=' + options + '&text=' + text + '&modify=' + modify,
			waiting: button,
			update: 'form_question_' + question_id
		});
	},
	
	saveTextTextarea: function (type, question_id, button) {
		var text = document.getElementById('text' + question_id).value;	// fetch question text
		var chars = document.getElementById('chars' + question_id).value;	// fetch max chars
		if (type == 'textarea') {
			var rows = document.getElementById('rows' + question_id).value;	// fetch rows
			var options = rows + ',' + chars;
		} else {
			var size = document.getElementById('size' + question_id).value;	// fetch rows
			var options = size + ',' + chars;
		}

		HTTP.connect({
			url: this.url,
			action: 'save_texttextbox',
			data: 'question_id=' + question_id + '&options=' + options + '&text=' + text,
			waiting: button,
			update: 'form_question_' + question_id
		});
	},

	changeSelect: function (question_id) {
		var select = document.getElementById(question_id + '_0');
		var input = document.getElementById('opt' + question_id);
		var value = input.value;
		select.options[select.selectedIndex] = new Option(value, value);
		input.value = '';
	},

	deleteSelect: function (question_id) {
		var select = document.getElementById(question_id + '_0');
		var input = document.getElementById('opt' + question_id);
		var value = input.value;
		var len = select.options.length;
		select.options[select.selectedIndex] = null;
		select.size = len - 1;
		input.value = '';
	},

	editSelect: function (question_id) {
		var value = document.getElementById(question_id + '_0').value;
		document.getElementById('opt' + question_id).value = value;
	},

	addSelect: function (question_id) {
		var input = document.getElementById('opt' + question_id);
		var newtext = input.value;	// fetch question text
		var select = document.getElementById(question_id + '_0');
		var len = select.options.length;
		select.options[len] = new Option(newtext, newtext);
		select.size = len + 1;
		input.value = '';
		input.focus();
	},

	moveSelect: function (question_id, direction) {
		var select = document.getElementById(question_id + '_0');
		var index = select.selectedIndex;
		var len = select.options.length;
		var currentval = select.options[index].value;
		if (direction) { // move up
			if (index == 0) return;	// stop at the top
			newindex = index - 1;
		} else { // move down 
			if (index == len) return;	// stop at the bottom
			newindex = index + 1;
		}

		var oldval = select.options[newindex].value;
		select.options[index] = new Option(oldval, oldval);
		select.options[newindex] = new Option(currentval, currentval);
		select.selectedIndex = newindex;
	},

	saveSelect: function (question_id, button) {
		var text = document.getElementById('text' + question_id).value;	// fetch question text			
		var select = document.getElementById(question_id + '_0');
		var len = select.options.length;
		var options = '', value;
		for (i = 0; i < len; i++) {
			value = select.options[i].value;
			while ( value.search(",") != -1 ) {	// convert commas to HTML encodings (used as separator)
				value = value.replace(",", escape("&#44;")); // escape here because of ampersand in HTML entity
			}
			while ( value.search(":") != -1 ) {	// convert colons to HTML encodings (used as separator)
				value = value.replace(":", escape("&#58;")); // escape here because of ampersand in HTML entity
			}
			options += i + ':' + value + ',';
		}
		options = options.slice(0, -1);
		HTTP.connect({
			url: this.url,
			action: 'save_select',
			data: 'question_id=' + question_id + '&options=' + options + '&text=' + text,
			waiting: button,
			update: 'form_question_' + question_id
		});
	},

	editOptions: function (form_id, event, button) {
		this.form_id = form_id;
		Box.derivePosition(event);
		HTTP.connect({
			url: this.url,
			action: 'edit_options',
			data: 'form_id=' + form_id,
			waiting: button,
			after: function (response) {
				Box.open({ title:'Form Editor', width:400, content: response });
			}
		});
	},

	saveOptions: function (button) {
		var form = document.getElementById('formForm');
		destination = form.destination.value;
		name = form.formname.value;
		formbutton = form.button.value;
		HTTP.connect({
			url: this.url,
			action: 'save_options',
			data: 'form_id=' + this.form_id + '&name=' + name + '&button=' + formbutton + '&destination=' + destination,
			waiting: button,
			update: 'form_' + this.form_id,
			after: function (response) {
				Box.close();
			}
		});
	},

	openUploader: function (form_question) {
		popup('/modules/form/form_uploader.php?form_question=' + form_question, 'form_upload', 366, 400);
	}
}


	function form_char_limit(input, limit) {
		var text = input.value;
		if (text.length > limit)
			input.value = text.substring(0, limit);
	}
