// binder_forms.js
// tools to help build binderized settings panels
// should be loaded page-level, *not* lib-level, as it is document-dependent

var DEFAULT_BLANK_FIELD = "(none)";
var DEBUG_BINDER_FORMS = true;

// utility functions

function make_presentable(s, special_blank) {
	return ((s==undefined||s=="undefined"||s==""||s==null)?((special_blank != null)?special_blank:DEFAULT_BLANK_FIELD):s);
}
function make_binderized(s, special_blank) {
	return ((s==((special_blank != null)?special_blank:DEFAULT_BLANK_FIELD)||s=="")?"":s);
}

// form processing functions

function changed_setting(form, field) {
	var newValue = make_binderized(document[form][field].value);
	var changed = (newValue != make_binderized(document[form][field + "_orig"].value));
	if ( DEBUG_BINDER_FORMS && changed )
		beos.dprintf('setting "' + field + '" changed from "'
			+ document[form][field + "_orig"].value + '" to "'
			+ document[form][field].value + '"');
	return changed?newValue:false;
}

function load_setting(form, field, node, prop, pureblank) {
	var _val = node[prop].valueOf();
	var val;
	if (pureblank)
		val = make_presentable(_val, "");
	else
		val = make_presentable(_val);
	document[form][field].value = val;
	document[form][field + "_orig"].value = val;
}

function save_changed_setting(form, field, node, prop) {
	var val;
	if ( (val = changed_setting(form, field)) ) {
		node[prop] = val;
		return true;
	} else {
		return false;
	}
}

// form rendering functions

function generate_setting_field(field, type, value, htmlArgs) {
	if (type == null) type = "text";
	return '<INPUT TYPE="'+ type +'" NAME="'
		+ field
		+ '" VALUE="'+value+'" ' + htmlArgs + '>'
		+ '<INPUT TYPE=hidden NAME="'
		+ field
		+ '_orig" VALUE="'+value+'">';
}