
// return the selected radio button of a group in a form
function radio_getSelected(form_id, group_name) {
	radio_group = xGetElementById(form_id).elements[group_name];
	//alert(radio_group);
	var selected = null;
	for(var i=0; i < radio_group.length; i++) {
		if(radio_group[i].checked == true) {
			selected = radio_group[i].value;
		}
	}
	return selected;
}
// Return the count of all checked checkboxe or radio in [form_name]
function count_checkbox(form_name, checkbox_type) {

	var checked = 0;
	var form_elements = document.forms[form_name].elements;

	// Set default check for checkboxes if not set
	if(checkbox_type == null) {
		checkbox_type = "checkbox";
	}

	for (x = 0; x < form_elements.length; x++) {

		ele = document.forms[form_name].elements[x];
		if (form_elements[x].type == checkbox_type && form_elements[x].checked == true) {
			checked++;
		}
	}
	return checked;
}

function form_set_action(form_name, action) {
	document.forms[form_name].action_name.value = action;
}

function form_add(form_hide, form_show) {
	/* swap visible DIVs layers */
	form_swap(form_hide, form_show);
}

/*
this function is called when action was to modify, and user push "cancel" button
in this case, the action becomes "add" otherwise next push on "add" button
will remain on last "modify" state and it will not be possible to add a record
*/
function form_cancel(form_hide, form_show, action_name) {

	if(action_name != null) {
		xGetElementById(form_hide).action_name.value = action_name;
	}
	// reset all textfield in the form
	form_clear(xGetElementById(form_hide));
	// swap visible DIVs layers
	form_swap(form_hide, form_show);
}

// clear a field, blank all field
function form_clear(form_object, bool_reset) {  
	if(bool_reset) { 
		// reset the form to initial value
		form_object.reset(); 
	} 
	form_length = form_object.length;

	for(i=0; i < form_length; i++) { 
		form_element = form_object.elements[i]
		switch(form_element.type) {

			case "hidden":
			break;

			case "text":
				form_element.value = ""; 
			break;

			case "checkbox":
				form_element.checked = ""; 
			break;

			case "radio":
			break;
		}
	} 
}

function form_swap(form_hide, form_show) {
	// build the name of the div containing the the form
	var div_hide = form_hide.replace(/form/, "div");
	var div_show = form_show.replace(/form/, "div");
	xSwap_2(div_hide, div_show);
}

function form_send(form_id, action_name, message) {

	// Note : default action is to send the form
	var _form = xGetElementById(form_id);
	var submit_form = true;
	var bit_mask = 0;
	var access_mask = _form.action_access_mask.value;

	if(action_name == false) {
		action_name = _form.action_name.value;
	}

	switch(action_name) {

		case "send":
		case "read":
			bit_mask = 1;
		break;
		
		case "add":
			bit_mask = 2;
		break;

		case "update":
			bit_mask = 4;
		break;

		case "delete":
			bit_mask = 8;
		break;
	}

	/* boolean AND on acess mask */
	access_mask = (access_mask & bit_mask);

	_form.action_access_mask.value = access_mask;

	/* if message is set, then ask confirmation */
	if(message != "" &&  (access_mask != 0)) {
		submit_form = confirm(message);
	}

	/* confirmation has been done */
	if((submit_form == true) &&  (access_mask != 0)) {
		_form.submit();
	}
	else {
		/* nothing to do */
		alert("ERROR in form send : nothing to do or submit action not defined");
	}
}

function enable_delete(checkbox) {

	/* get the form name in wich the checkbox is */
	var _form = xGetElementById(checkbox.form.id);

	/*
	get the actual actions allowed in the form
	bit 0 (1) = read
	bit 1 (2) = add
	bit 2 (4) = modify
	bit 3 (8) = delete
	.
	.
	*/
	var access_mask = _form.action_access_mask.value;

	/* return the button object */
	btn = document.getElementById(_form.id + "_bdel");

	/* boolean OR enable delete action (bit 3) */
	_form.action_access_mask.value = (access_mask | 8);

	/*
	verify if there is at least one checkbox checked	
	if not, grey the button and set acess mask with no delete allowed
	boolean XOR on bit 3 (delete)
	*/

	/* get the number of checkbox selected */
	if(!count_checkbox(_form.id)) {
		btn.disabled = "true";
		btn.className = "btn-disabled";

		/* disable delete action (bit 3 set to zero) */
		_form.action_access_mask.value = (access_mask ^ 8);
	}
	else {
		btn.disabled = "";
		btn.className = "btn-enabled";
	}
	return;
}

function xSwap_2(hide, show) {
	var ele_1, ele_2;

	ele_1 = xGetElementById(hide)
	ele_2 = xGetElementById(show)

	if(ele_1 != null && ele_2 != null) {
		ele_1.style.display = "none";
		ele_2.style.display = "block";
	}
	else {
		alert("ERROR: <div> [" + hide + "] or [" + show + "] is missing");
	}
}

// tab_name arg is a STRING
function tab_select(tab_name) {

	var buffer = "";

	// Extract tab group name and tab name
	var rexp	= /tab\-([a-z0-9_]+)\-([a-z0-9_]+)/;

	var results	= tab_name.match(rexp);

	// builds the prefix of the group containing the tabs, remove tab_
	var tab_groupname	= results[1];

	var tab_selected	= results[2];

	// tabs_group is an object
	var tabs_group		= eval("tabs_" + tab_groupname);

	// change all tabs style to [disabled]
	for(tab_name in tabs_group) {

		// change selected tab style to [disabled]
		xGetElementById("tab-" + tab_groupname + "-" + tab_name).className = "tab";

		tab_div_layer = xGetElementById("div-tab-" + tab_groupname + "-" + tab_name);
		// if the div exists hide it, otherwise display an error
		if(tab_div_layer != null) {
			tab_div_layer.style.display = "none";
		}
		else {
			alert("ERROR: [hide] <div> [" + tab_name + "] is missing");
		}

		tabs_group[tab_name] = "";
	}

	tabs_group[tab_selected] = "-selected";

	// change selected tab style to [selected]
	xGetElementById("tab-" + tab_groupname + "-" + tab_selected).className = "tab-selected";

	tab_div_layer = xGetElementById("div-tab-" + tab_groupname + "-" + tab_selected);

	// if the div exists show it, otherwise display an error
	if(tab_div_layer != null) {
		tab_div_layer.style.display = "block";
	}
	else {
		alert("ERROR: [show] <div> [" + tab_selected + "] is missing");
	}

	// compose the form and tabs string for writing in a cookie
	for(entry in tabs_group) {
		buffer += entry + "=" + tabs_group[entry] + ":";					
	}

	// store tabs state in a cookie for server side processing
	document.cookie = "tabs_" + tab_groupname + "=" + buffer;
	return;
}
                                                 

