// javascript functions for building html forms
// Inspired by Rails Actionview::Helpers::FormHelper



function selected_button(group){
	for(var i=0, j=group.length; i<j; i++){
		if(group[i].checked){
			return group[i];
		}
	}	
	return null;
}
function selected_button_value(button_group){
	var s_button = selected_button(button_group);
	return s_button.value;
}

function true_value(val){
	if(val == true || val == 'true') return true;
	else return false;
}

function form(opts, elements){
	var str = "<form";
	str += add_options(opts);
	
	str += ">";
	for(i=0, j=elements.length; i<j; i++){
		str += elements[i];
	}
	str += "</form>";
	return str;
}

function combine_elements(elements){
	var str = "";
	for(var i=0, j= elements.length; i<j; i++){
		str += elements[i];
	}
	return str;
}

function label_field(model, method, str){
	var element_name = el_name(model, method);
	return "<label for='" + element_name + "' >" + str + "</label>";
}

function text_field(model, method, opts){
	var opts = opts || {};
	return input_type("text", model, method, opts);
}

function labelled_text_field(model, method, lbl, opts){
	var opts = opts || {};
	return label_field(model, method, lbl) + text_field(model, method, opts);
}

function text_area(model, method, opts){
	var opts = opts || {};
	var str = "<textarea " + "id='" + el_id(model, method) + "' name='"+ el_name(model, method) + "'"; 
	str += add_options(opts);
	str += ">";
	str += "</textarea>";
	return str;
}

function hidden_field(model, method, opts){
	var opts = opts || {};
	return input_type("hidden",  model, method, opts);
}

function radio_button(model, method, value, opts){
	var opts = opts || {};
	opts['value'] = value;
	return input_type("radio", model, method, opts);
}

function radio_group(model, method, values, checked_value, separator, opts){
	var opts = opts || {};
	var separator = separator || "";
	var str="";
	
	for(var val in values){
		var button_opts = copy_attributes(opts);
		if(checked_value == values[val]){button_opts['checked'] = 'checked';};
		str += radio_button(model, method, values[val], button_opts);
		str += val + separator;
	}
	
	return str;
}

function option_for_select(val, lbl, selected){
	var opts = {value : val};
	if(selected){opts['selected'] = true;}
	var str = "<option" + add_options(opts) + ">";
	str += lbl + "</option>";
	return str;
}

function select_field(model, method, values, selected, opts){
	var opts = opts || {};
	var selected = selected || null;
	var str = "<select " + "id='" + el_id(model, method) + "' name='"+ el_name(model, method) + "'" + add_options(opts) + ">";
	for(val in values){
		str += option_for_select(values[val], val, selected == values[val] ? true : false);
	}
	str += "</select>";
	return str;	
}

// Checkbox utilities
function selected_checkbox_values(f, name, choices){
	var arr = [];
	for(var item in choices){
		var el_name = name + "[" + choices[item] + "]";
		if(f.elements[el_name].checked){
			arr.push(f.elements[el_name].value);
		}
	}
	return arr;
}
function checkbox(model, method, label, value, opts){
	var opts = opts || {};
	opts['value'] = value;
	return input_type("checkbox", model, method, opts) + label ;
}

function checkbox_group(group_name, values, checked_values, separator, opts){
	var opts = opts || {};
	var separator = separator || "<br/>";
	var str="";
	
	for(var val in values){
		var box_opts = copy_attributes(opts);
		// NOTE: Uses prototype.js library for 'include' method
		if(checked_values.include(values[val])){box_opts['checked'] = 'checked';}
		box_opts['value'] = values[val];
		str += group_input("checkbox", group_name, values[val], box_opts) + val;
		str += separator;
	}
	
	return str;
}

function group_input(type, group_name, id, opts){
	var opts = opts || {};
	var str = "<input type='" + type + "'" ;
	str += "id='" + group_name + "_" + "id" + "_" + id + "' name='"+  group_name + "[" + id + "]" + "'"; 
	str += add_options(opts);
	str += "/>";
	return str;
	
}



function content_tag(type, content, opts){
	var opts = opts || {};
	var str = "<" + type + add_options(opts) + ">";
	str += content + "</" + type + ">";
	return str;
}
function p(content, opts){
	// var opts = opts || {};
	// var str = "<p" + add_options(opts) + ">";
	// str += content + "</p>";
	// return str;
	return content_tag("p", content, opts);
}
function a(content, link, opts){
	var opts = opts || {};
	opts['href'] = link;
	return content_tag("a", content, opts);
}
function div(content, opts){
	return content_tag("div", content, opts);
}

function h1(content, opts){
	return content_tag("h1", content, opts);
}

function h2(content, opts){
	return content_tag("h2", content, opts);
}
function h3(content, opts){
	return content_tag("h3", content, opts);
}
function h4(content, opts){
	return content_tag("h4", content, opts);
}
function h5(content, opts){
	return content_tag("h5", content, opts);
}

function strong(content, opts){
	return content_tag("strong", content, opts);
}

function img(src, opts){
	var opts = opts || {}
	var str = "<img src='" + src + "' " + add_options(opts) + ">";
	return str;
}

function a_img_w_text(content, img_src, link, img_opts, opts){
	var img_opts = img_opts || {};
	var opts = opts || {};
	var str = a(img(img_src, img_opts), link, opts);
	var str2 = a(content, link, opts);
	return str + space() + str2;
}

function space(num){return entity("&nbsp;", num);}

function pipe(num){ return entity("&#124;", num);}

function entity(symbol, num){
	var num = num || 1;
	var str = "";
	for(i=0; i < num; i++){
		str += symbol;
	}
	return str;
}

// utility functions
function input_type(type, model, method, opts){
	var opts = opts || {};
	var str = "<input type='" + type + "'" ;
	str += "id='" + el_id(model, method) + "' name='"+ el_name(model, method) + "'"; 
	str += add_options(opts);
	str += "/>";
	return str;
}




function el_id(model, method){
	return model + "_" + method;
}

function el_name(model, method){
	return model + "[" + method + "]";
}

function add_options(opts){
	var str = "";
	for(opt in opts){
		str += " " + opt + "='" + opts[opt] + "'";
	}
	return str;
}

function copy_attributes(obj){
	var h = {};
	for(item in obj){
		if(typeof(obj[item]) != 'object'){
			h[item] = obj[item];
		}
	}
	return h;
}

