/*
	Dynamic control creation library v1.0
	(C) zIT 2008
*/

var max_elements_per_type = 500; // this is useful for creating the list of elements by same type;

function dcc_set_innerHTML(element_id, ihtml)
{
	var o = document.getElementById(element_id);
	if (o) o.innerHTML = ihtml;
	else return false;
	return true;
}

function dcc_add_innerHTML(parent_id, html_data)
{
	o = document.getElementById(parent_id);
	if (!o) return false;
	o.innerHTML = o.innerHTML + html_data;
	return true;
}

function dcc_assign_event(obj_name_id, obj_evt, obj_func)
{
	var o = document.getElementById(obj_name_id);
	if (!o) return false;
	switch (obj_evt)
	{
		case "onclick" :
			o.onclick = function() { eval (obj_func); };
			break;
		case "onchange" :
			o.onchange = function() { eval (obj_func); };
			break;
		case "onfocus" :
			o.onfocus = function() { eval (obj_func); };
			break;
		case "onmouseover" :
			o.onmouseover = function() { eval (obj_func); };
			break;
		case "onmouseout" :
			o.onmouseout = function() { eval (obj_func); };
			break;
		default:
			eval ("o." + obj_evt + " = function( eval (" + obj_func + "));");
	}
	return true;
}

function get_elements(first_name_id)
{
	var arr = new Array();
	o = document.getElementById(first_name_id);
	if (o) arr.push(first_name_id);
	for (i=1; i<=max_elements_per_type; i++)
	{
		cname = ""; cname = first_name_id + "_" + i;
		if (document.getElementById(cname)) arr.push(cname);
	}
	return arr.join("|");
}

function dcc_add_child(element_id, child_data)
{
	var o = null;
	if (element_id == "")
		o = document.body;
	else
		o = document.getElementById(element_id);
	o.appendChild(child_data);
	return true;
}

function dcc_remove_child(parent_id, name_id)
{
	var o = document.getElementById(parent_id);
	o.removeChild(document.getElementById(name_id));
	return true;
}

function dcc_add_button(parent_id, name_id, input_value, width_px, height_px, class_name, code_on_click)
{
	inc_val = 0;
	var cname_id = name_id;
	while (d = document.getElementById(cname_id))
	{
		inc_val++;
		cname_id = name_id + "_" + inc_val;
	}
	name_id = cname_id;
	var o = document.createElement("input");
	o.setAttribute("id", name_id);
	o.setAttribute("name", name_id);
	o.setAttribute("type", "button");
	o.value = input_value;
	if (class_name != "") o.className = class_name;
	if (width_px != "") o.style.width = width_px + "px";
	if (height_px != "") o.style.height = height_px + "px";
	if (code_on_click != "") o.onclick = function() { eval (code_on_click); }
	dcc_add_child(parent_id, o);
	return name_id;
}

function dcc_add_submit(parent_id, name_id, input_value, width_px, height_px, class_name, code_on_click)
{
	inc_val = 0;
	var cname_id = name_id;
	while (d = document.getElementById(cname_id))
	{
		inc_val++;
		cname_id = name_id + "_" + inc_val;
	}
	name_id = cname_id;
	var o = document.createElement("input");
	o.setAttribute("id", name_id);
	o.setAttribute("name", name_id);
	o.setAttribute("type", "submit");
	o.value = input_value;
	if (class_name != "") o.className = class_name;
	if (width_px != "") o.style.width = width_px + "px";
	if (height_px != "") o.style.height = height_px + "px";
	if (code_on_click != "") o.onclick = function() { eval (code_on_click); }
	dcc_add_child(parent_id, o);
	return name_id;
}

function dcc_add_reset(parent_id, name_id, input_value, width_px, height_px, class_name, code_on_click)
{
	inc_val = 0;
	var cname_id = name_id;
	while (d = document.getElementById(cname_id))
	{
		inc_val++;
		cname_id = name_id + "_" + inc_val;
	}
	name_id = cname_id;
	var o = document.createElement("input");
	o.setAttribute("id", name_id);
	o.setAttribute("name", name_id);
	o.setAttribute("type", "reset");
	o.value = input_value;
	if (class_name != "") o.className = class_name;
	if (width_px != "") o.style.width = width_px + "px";
	if (height_px != "") o.style.height = height_px + "px";
	if (code_on_click != "") o.onclick = function() { eval (code_on_click); }
	dcc_add_child(parent_id, o);
	return name_id;
}

function dcc_add_text(parent_id, name_id, input_value, width_px, height_px, class_name, code_on_click)
{
	inc_val = 0;
	var cname_id = name_id;
	while (d = document.getElementById(cname_id))
	{
		inc_val++;
		cname_id = name_id + "_" + inc_val;
	}
	name_id = cname_id;
	var o = document.createElement("input");
	o.setAttribute("id", name_id);
	o.setAttribute("name", name_id);
	o.setAttribute("type", "text");
	o.value = input_value;
	if (class_name != "") o.className = class_name;
	if (width_px != "") o.style.width = width_px + "px";
	if (height_px != "") o.style.height = height_px + "px";
	if (code_on_click != "") o.onclick = function() { eval (code_on_click); }
	dcc_add_child(parent_id, o);
	return name_id;
}

function dcc_add_checkbox(parent_id, name_id, input_value, width_px, height_px, class_name, code_on_click)
{
	inc_val = 0;
	var cname_id = name_id;
	while (d = document.getElementById(cname_id))
	{
		inc_val++;
		cname_id = name_id + "_" + inc_val;
	}
	name_id = cname_id;
	var o = document.createElement("input");
	o.setAttribute("id", name_id);
	o.setAttribute("name", name_id);
	o.setAttribute("type", "checkbox");
	o.checked = (input_value == "" ? false : true);
	if (class_name != "") o.className = class_name;
	if (width_px != "") o.style.width = width_px + "px";
	if (height_px != "") o.style.height = height_px + "px";
	if (code_on_click != "") o.onclick = function() { eval (code_on_click); }
	dcc_add_child(parent_id, o);
	return name_id;
}

function dcc_add_textarea(parent_id, name_id, input_value, width_px, height_px, class_name, code_on_click)
{
	inc_val = 0;
	var cname_id = name_id;
	while (d = document.getElementById(cname_id))
	{
		inc_val++;
		cname_id = name_id + "_" + inc_val;
	}
	name_id = cname_id;
	var o = document.createElement("textarea");
	o.setAttribute("id", name_id);
	o.setAttribute("name", name_id);
	o.value = input_value;
	if (class_name != "") o.className = class_name;
	if (width_px != "") o.style.width = width_px + "px";
	if (height_px != "") o.style.height = height_px + "px";
	if (code_on_click != "") o.onclick = function() { eval (code_on_click); }
	dcc_add_child(parent_id, o);
	return name_id;
}

function dcc_add_select(parent_id, name_id, input_value, width_px, height_px, class_name, code_on_click)
{
	inc_val = 0;
	var cname_id = name_id;
	while (d = document.getElementById(cname_id))
	{
		inc_val++;
		cname_id = name_id + "_" + inc_val;
	}
	name_id = cname_id;
	var o = document.createElement("select");
	o.setAttribute("id", name_id);
	o.setAttribute("name", name_id);
	o.setAttribute("size", "1");
	o.value = input_value;
	if (class_name != "") o.className = class_name;
	if (width_px != "") o.style.width = width_px + "px";
	if (height_px != "") o.style.height = height_px + "px";
 	if (code_on_click != "") o.onclick = function() { eval (code_on_click); }
	dcc_add_child(parent_id, o);
	return name_id;
}

function dcc_add_option(sel_name_id, option_text, option_value, option_default)
{
	o = document.getElementById(sel_name_id);
	olen = o.options.length;
	o.options[olen] = new Option(option_text, option_value);
	if ((option_default != "") && (option_default != "false")) o.value = option_value;
	return true;
}

function dcc_add_div(parent_id, name_id, div_value, width_px, height_px, class_name, code_on_click)
{
	var o = null;
	if (code_on_click != "strict")
	{
		inc_val = 0;
		var cname_id = name_id;
		while (d = document.getElementById(cname_id))
		{
			inc_val++;
			cname_id = name_id + "_" + inc_val;
		}
		name_id = cname_id;
		o = document.createElement("div");
	}
	else
	{
		d = document.getElementById(name_id);
		if (d)
		{
			d.innerHTML = "";
			o = d;
		}
		else o = document.createElement("div");
		code_on_click = "";
	}
	o.setAttribute("id", name_id);
	o.setAttribute("name", name_id);
	if (class_name != "") o.className = class_name;
	if (div_value != "") o.innerHTML = div_value;
	if (width_px != "") o.style.width = width_px + "px";
	if (height_px != "") o.style.height = height_px + "px";
 	if (code_on_click != "") o.onclick = function() { eval (code_on_click); }
	dcc_add_child(parent_id, o);
	return name_id;
}

function dcc_add_label(parent_id, name_id, div_value, width_px, height_px, class_name)
{
	inc_val = 0;
	var cname_id = name_id;
	while (d = document.getElementById(cname_id))
	{
		inc_val++;
		cname_id = name_id + "_" + inc_val;
	}
	name_id = cname_id;
	o = document.createElement("div");
	o.setAttribute("id", name_id);
	o.setAttribute("name", name_id);
	if (div_value != "") o.innerHTML = div_value;
	if (class_name != "") o.className = class_name;
	if (width_px != "") o.style.width = width_px + "px";
	if (height_px != "") o.style.height = height_px + "px";
	dcc_add_child(parent_id, o);
	return name_id;
}
