// $Id: form_scripts.js 1015 2005-12-14 15:38:43Z zeke $

//
// Show/hide any tag by its id.
// parameters:
// id - element id
function fn_show_tag(id, status)
{
	if (document.getElementById(id)) {
		if (status == true || status == false) {
			document.getElementById(id).style.display = (status == true)?"none":"";
		} else {
			document.getElementById(id).style.display = (document.getElementById(id).style.display == "")?"none":"";
		}
	}
}

//
// Select text in text input
//
function fn_select_input(select)
{
	select.select();
}

//
// Disable/enable all form elements inside the tag 
// @id - element id
// @status - boolean (true to disable and false to enable)
function fn_disable_elements(id, status)
{

	node = document.getElementById(id);
	// Process INPUT types...
	inputs = node.getElementsByTagName('INPUT');
	for (i=0;i<inputs.length;i++) {
		node.getElementsByTagName('INPUT')[i].disabled = status;
	}

	// Process TEXTAREA type...
	texts = node.getElementsByTagName('TEXTAREA');
	for (i=0;i<texts.length;i++) {
		node.getElementsByTagName('TEXTAREA')[i].disabled = status;
	}

	// Process SELECT type...
	selects = node.getElementsByTagName('SELECT');
	for (i=0;i<selects.length;i++) {
		node.getElementsByTagName('SELECT')[i].disabled = status;
	}
}

//
// Open pop-up window with detailed image
//
function fn_open_popup_image(popup_script, image_width, image_height)
{
	if (image_width == 0) {
		image_width = 400;
	}
	if (image_height == 0) {
		image_height = 400;
	}
	image_width += 10;
	image_height += 10;

	if ((typeof(handle_popup_image)!='undefined') && (handle_popup_image.closed == false)) {
		handle_popup_image.close();
	}
    handle_popup_image = window.open(popup_script, 'popup_image', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,left=200,top=100,width=' + image_width + ',height=' + image_height + ',resizable=yes');
}

//
// Perform request via <SCRIPT> tag
//
function fn_http_request(script_id,url,status_msg,status_id)
{

	if (status_msg && status_id) {
		if (document.getElementById(status_id)) {
			document.getElementById(status_id).innerHTML = status_msg;
		}
	}

	var script_tag = document.getElementById(script_id);
	if (script_tag) {
		script_tag.parentNode.removeChild(script_tag);
	}

	script_tag = document.createElement('SCRIPT');
	script_tag.type = 'text/javascript';
	script_tag.language = 'javascript';
	script_tag.id = script_id;
    script_tag.src = url;
	document.body.appendChild(script_tag);
}

//
// String utility functions
//

// extract front part of string prior to searchString
function fn_str_get_front(mainStr,searchStr)
{
	foundOffset = mainStr.indexOf(searchStr)
	if (foundOffset == -1) {
		return null
	}
	
	return mainStr.substring(0,foundOffset)
}

// extract back end of string after searchString
function fn_str_get_end(mainStr,searchStr) 
{
	foundOffset = mainStr.indexOf(searchStr)
	if (foundOffset == -1) {
		return null
	}
	
	return mainStr.substring(foundOffset+searchStr.length,mainStr.length)
}

// insert insertString immediately before searchString
function fn_str_insert_string(mainStr,searchStr,insertStr) 
{
	var front = fn_str_get_front(mainStr,searchStr)
	var end = fn_str_get_end(mainStr,searchStr)

	if (front != null && end != null) {
		return front + insertStr + searchStr + end
	}
	
	return null
}

// remove deleteString
function fn_str_delete_string(mainStr,deleteStr) 
{
	return fn_str_replace_string(mainStr,deleteStr,"");
}

// replace searchString with replaceString
function fn_str_replace_string(mainStr,searchStr,replaceStr) 
{
	var front = fn_str_get_front(mainStr,searchStr)
	var end = fn_str_get_end(mainStr,searchStr)

	if (front != null && end != null) {
		return front + replaceStr + end
	}
	
	return null
}

//
// Check / uncheck all checkboxes in form
// @form_name - form name whose checkboxes should be checked or unchecked
// @checkbox_id - tag identifier of checkboxes that should be checked or unchecked
// @flag - can be true or false
function fn_check_all_checkboxes(form_name, flag, checkbox_id)
{
	if (!checkbox_id)
		checkbox_id = 'delete_checkbox';

	if(!(d_form = document.forms[form_name]))
		return false;

	for(i=0; i < d_form.length; i++) {
		if (d_form.elements[i].id == checkbox_id && !d_form.elements[i].disabled)
			d_form.elements[i].checked = flag;
	}
	return true;
}

// Check email address for validity
function fn_check_email(email, msg) {

	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (filter.test(email)) {
		return true;
	} else{
		alert(msg);
		return false;
	}
}

