//***************************************************************************
//S-T-A-R-T													 GLOBAL VARIABLES
//***************************************************************************

var B_FORM_IS_SUBMITED = false;
var STR_COOKIE_LEGEND = "display_legend";
var HTML_TR_SELECTED_CLASS = "tr_selected";
var HTML_BTN_SUBMIT			= "__btn_submit";
var HTML_BTN_SEARCH			= "__btn_search";
var HTML_BTN_ACTION			= "__btn_action";
var HTML_HID_SUBMIT_PRESSED = "__hid_submit_pressed";
var HTML_LIST_HIDDEN_ACTION = "__hid_action_string";
var HTML_ACTIONED_ID		= "__hid_actioned_id";
var HTML_SINGLE_ACTION_NAME	= "__hid_single_action_name";
var HTML_LIST_HIDDEN_SEARCH = "__hid_search_string";
var HTML_LIST_CHECKBOX_ALL = "__chk_all";
var HTML_LIST_CHECKBOX_ITEM = "__chk_";
var HTML_EXPORT_CHECKBOX_ITEM = "__exp_chk_";
var HTML_LIST_TR_PREFIX = "__tr_";
var HTML_LIST_ICO_PREFIX = "__ico_";
var HTML_TR_LEGEND = "__tr_legend";
var HTML_TR_EXPORT = "__tr_export";
var HTML_IMG_HELP_ON = "__img_help_on";
var HTML_IMG_HELP_OFF = "__img_help_off";
var HTML_RADIO_SHOW = "__rad_show";
var HTML_RADIO_HIDE = "__rad_hide";
var SQL_TEXT_LIKE = "__like_";
var SQL_TEXT_EQUAL = "__equal_";
var SQL_SORT_BY = "sort_by";
var SQL_SORT_DIR = "sort_dir";
var URL_RECS_PER_PAGE = "recs";
var URL_PAGE_NAME = "page";
var URL_EXPORT_KEY = "export";
var NOT_SELECTED = -1000;
var ON_FORM_SUBMIT = null;

//***************************************************************************
//S-T-A-R-T													  SUBMIT HANDLING
//***************************************************************************

// DISABLE SUBMIT BUTTON AFTER SUBMITTING
function do_submit (str_on_form_submit)
{
	if (str_on_form_submit == null)
	{
		if (!B_FORM_IS_SUBMITED)
		{
			try
			{
				if (RAW_URL.indexOf ("step1.html") >= 0 || RAW_URL.indexOf ("step2.html") >= 0 || RAW_URL.indexOf ("step3.html") >= 0 || RAW_URL.indexOf ("step4.html") >= 0 || RAW_URL.indexOf ("step5.html") >= 0) 
					set_visible (get_element_by_id ("spn_loading"), true);
			}
			catch (ex) {}
			
			var index = 0;
			for (index = 0; index < document.getElementsByTagName ("input").length; index ++)
			{
				if (document.getElementsByTagName ("input").item (index).type == "submit")
				{
					document.getElementsByTagName ("input").item (index).disabled = true;
				}
			}			
			
			if (get_element_by_id (HTML_HID_SUBMIT_PRESSED) != null)
				get_element_by_id (HTML_HID_SUBMIT_PRESSED).value = document.pressed;
			
			
			document.forms [0].submit ();
			B_FORM_IS_SUBMITED = true;
		}
	}
	else
	{
		ON_FORM_SUBMIT = null;
		eval (str_on_form_submit);
	}
	return false;
}

// APPLY PRESSED BTN FOR BUTTONS
function pressed_button (str_action_name)
{
	get_element_by_id (HTML_HID_SUBMIT_PRESSED).value = str_action_name;
}	

// CHECK IF 'ENTER' KEY WAS PRESSED
function is_enter (e)	// E IS EVENT OBJECT PASSED FROM FUNCTION INVOCATION
{	
	var i_character_code; 

	// IF WHICH PROPERTY OF EVENT OBJECT IS SUPPORTED (NN4)
	if (is_NS ())
	{ 
		e = e;
		i_character_code = e.keyCode; // CHARACTER CODE IS CONTAINED IN NN4'S WHICH PROPERTY
	}
	else
	{
		e = event;
		i_character_code = e.keyCode; // CHARACTER CODE IS CONTAINED IN IE'S KEYCODE PROPERTY
	}

	// IF GENERATED CHARACTER CODE IS EQUAL TO ASCII 13 (IF ENTER KEY)
	if (i_character_code == 13)
		return true;
	else
		return false;
}

//****************************************************************
//S-T-A-R-T								REFERING ELEMENTS HANDLING
//****************************************************************

// GET ELEMENT BY NAME
function get_element_by_name (str_ename)
{
	var obj;
	if (document.all)
		eval ("obj = document.all.item (\"" + str_ename + "\")");
	else if (document.layers)
		eval ("obj = document.layers [\"" + str_ename + "\"]");
	else	
		obj =  undefined;
	return obj;
}

// GET INPUT BY NAME
function get_input_by_name (str_fname, str_ename)
{
	var obj;
	eval ("obj = document.forms [\"" + str_fname + "\"]." + replace_char (str_ename, ':', '_') + ";");
	return obj;
}

// GET ELEMENT BY ID
function get_element_by_id (str_ename)
{
	var obj;
	if (document.all)
	{
		eval ("obj = document.all [\"" + str_ename + "\"]");
	}
	else if (document.layers)
	{		
		eval ("obj = document.layers [\"" + str_ename + "\"]");
	}
	else if (document.getElementById)
	{	
		eval ("obj = document.getElementById (\"" + str_ename + "\")");		
	}
	else	
		obj =  undefined;
	return obj;
}

// RETURNS OBJECT CONTROL INSIDE ANY "NOT RUNAT SERVER" CONTROLS WITH THE ID="OBJ_ID"   
function get_inner_object (obj_id)
{
	obj_over = get_element_by_id (obj_id);
	
	if (obj_over != null && obj_over != undefined && obj_over != 'undefined')
	{
		for(i = 0; i < obj_over.childNodes.length; i++)
		{
			if (obj_over.childNodes [i].nodeType == 1 && obj_over.childNodes [i].tagName != 'undefined')
				return obj_over.childNodes [i];
		}
	}
	return null;
}

//***************************************************************************
//S-T-A-R-T											  LINKS ELEMENTS HANDLING
//***************************************************************************

// BUILD LINK BASED ON URL RECEIVED
function build_link (str_url)
{
	if (str_url.indexOf ('http://') != -1)
		return str_url;
	return "http://" + (SITE_LIVE == "True" ? "www." : "") + SITE_URL + str_url;
}

// BUILD LINK BASED ON MODULE / PAGE / QS RECEIVED
function build_link_ex (str_module, str_page, str_qs)
{
	var str_link = "/" + (str_module != "site" ? str_module + "-" : "") + str_page + ".html";
	
	if ((str_qs != null) && (str_qs.length > 0))
		str_link += str_qs;
		
	return build_link (str_link);
}

// BUILD LINK BASED ON ~PATH_AND_QUERY~ RECEIVED
function build_link_ext (str_path_and_query)
{
	var str_link = "";
	str_link  = str_path_and_query;
	return build_link (str_link);
}

// GET KEY FROM URL
function get_parameter (str_url_key)
{
	return query_string (str_url_key);
}

//***************************************************************************
//S-T-A-R-T											  HANDLE SINGLE PAGE ACTION SUBMIT
//***************************************************************************

// HANDLE SINGLE PAGE ACTION SUBMIT
function handle_action (str_action_name, str_actioned_id, str_confirm)
{
	try
	{
		get_element_by_id (HTML_ACTIONED_ID).value = str_actioned_id;
		get_element_by_id (HTML_SINGLE_ACTION_NAME).value = str_action_name;
	}
	catch (ex) {alert ("no hidden values found");} 
	
	if (str_confirm != "" && str_confirm != null && str_confirm != undefined && str_confirm != 'undefined')
	{
		if (confirm (str_confirm))
			document.forms [0].submit ();
	}
	else
		document.forms [0].submit ();
}

//***************************************************************************
//S-T-A-R-T													   POPUP HANDLING
//***************************************************************************

// POP-UP WINDOW
function pop_up (str_page, i_width, i_height)
{
	win = window.open (str_page, 'i', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width=' + i_width + ', height=' + i_height + ', left=100, top=100');
	if (win == null)
	{
		alert ("Your popup blocker stopped an window from opening\nPlease disable your popup blocker if you wish to see window content!");
		return false;
	}
	return true;
}

// POP-UP IN NAMED WINDOW
function pop_up (str_page, i_width, i_height, str_name)
{
	win = window.open (str_page, str_name, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width=' + i_width + ', height=' + i_height + ', left=100, top=100');
	if (win == null)
	{
		alert ("Your popup blocker stopped an window from opening\nPlease disable your popup blocker if you wish to see window content!");
		return false;
	}	
	return true;
}

// POP-UP NEW WINDOW
function pop_up_new (str_page, i_width, i_height)
{
	var str_name = generate_random ();
	win = window.open (str_page, str_name, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width=' + i_width + ', height=' + i_height + ', left=100, top=100');
	if (win == null)
	{
		alert ("Your popup blocker stopped an window from opening\nPlease disable your popup blocker if you wish to see window content!");
		return false;
	}
	return true;
}

//***************************************************************************
//S-T-A-R-T													    PAGE HANDLING
//***************************************************************************

// CLOSE WINDOW
function close_page ()
{
	window.close ();
}

//***************************************************************************
//S-T-A-R-T													  STRING HANDLING
//***************************************************************************

// TRIM SPECIFIED STRING
function trim (str)
{
	if (str == null)
		return "";
	var index_a, index_b, str;
	i_length = str.length;
	index_a = 0;
	if (i_length == 0)
		return "";
	while ((str.substr (index_a, 1) == " ") && (index_a < i_length))
		index_a ++;
	if (index_a > 0) 
	{
		str = str.substr (index_a);
		i_length = i_length - index_a;
	}
	index_b = i_length - 1;
	while ((str.substr (index_b, 1) == " ") && (index_b > 0))
		index_b --;
	if (index_b < i_length - 1)
		str = str.substr (0, index_b + 1);
	return str;
}

// TRIM A STRING TO A SPECIFIED LENGTH
function trim_length (str, i_str_length)
{
	if (str == null)
		return "";
		
	i_length = str.length;
	
	if (i_length == 0)
		return "";
		
	if (str.length > i_str_length + 3) 
	{	
		str = str.substr (0, i_str_length);
		str = str + "...";
	}	
	
	return str;
}

// CHECK IF A STRING IS ALPHA NUMERIC
function is_alfa_num (str) 
{
  var str_valid_chars = "abcdefghijklmnopqrstuvwxyz01234567890_";
  
  if (trim (str).length == 0)
	return false;
	
  for (var index = 0; index < str.length; index ++) 
  {
    var chr_letter = str.charAt (index).toLowerCase ();
    if (str_valid_chars.indexOf (chr_letter) == -1)
		return false;
  }
  return true;
}

// CHECK IF A STRING IS ALPHA
function is_alpha (str) 
{
  var str_valid_chars = "abcdefghijklmnopqrstuvwxyz";
  
  if (trim (str).length == 0)
	return false;
	
  for (var index = 0; index < str.length; index ++) 
  {
    var chr_letter = str.charAt (i).toLowerCase ();
    if (str_valid_chars.indexOf (chr_letter) == -1)
		return false;
  }
  return true;
}

// CHECK IF A STRING IS NUMERIC
function is_num (str) 
{
  var str_valid_chars = "0123456789";
  
  if (trim (str).length == 0)
	return false;
	
  for (var index = 0; index < str.length; index ++) 
  {
    var chr_letter = str.charAt (index).toLowerCase ();
    if (str_valid_chars.indexOf (chr_letter) == -1)
		return false;
  }
  return true;
}

// REPLACE ALL INSTANCES OF A CHARACTER INSIDE A STRING
function replace_char (str, char_in, char_out)
{
	while (str.indexOf (char_in) > 0)
	{
		str = str.replace (char_in, char_out);
	}
	return str;
}

// REPLACE ALL INSTANCES OF A STRING INSIDE ANOTHER STRING
function replace_str (str, str_old, str_new)
{
	while (str.indexOf (str_old) >= 0)
	{
		str = str.replace (str_old, str_new);
	}
	return str;
}

// CHECK IF A STRING LENGTH IS LIMITED
function is_limited (str, i_min, i_max)
{
	if ((trim (str).length > i_min) && (trim (str).length < i_max))
		return true;
	else
		return false;
}

// GENERATE RANDOM STRING
function generate_random ()
{
	var str_random = new String (Math.random ()).replace ("0.", "").substr (0,5);
	return str_random;
}

// VALIDATE IF STRING IS EMPTY
function is_empty (str)
{
	var b_empty = false;
	
	if ((trim (str).length == 0) || (str == null))
		b_empty = true;
	
	return b_empty;
}

// VALIDATE IF OBJECT CONTAINS NUMERIC CHARACTERS
function validate_num (obj)
{
	if (obj != null)
	{
		var str_valid_chars = "0123456789.";
		if (obj.value.indexOf ('.') != obj.value.lastIndexOf ('.'))
		{
			obj.value = obj.value.replace ('.', '#');
			obj.value = replace_str (obj.value, ".", "");
			obj.value = obj.value.replace ('#', '.');
		}
		if (obj.value.indexOf ('.') == 0)
			obj.value = obj.value.replace ('.', '0.');
		for (var index = 0; index < obj.value.length; index ++) 
		{
			var chr_letter = obj.value.charAt (index).toLowerCase ();
			if (str_valid_chars.indexOf (chr_letter) <= -1)
				obj.value = obj.value.replace (chr_letter, '');
		}
	}
}

// VALIDATE IF OBJECT CONTAINS ALPHA CHARACTERS
function validate_alpha (obj)
{
	if (obj != null)
	{
		var str_valid_chars = "abcdefghijklmnopqrstuvwxyz";
		for (var index = 0; index < obj.value.length; index ++) 
		{
			var chr_letter = obj.value.charAt (index).toLowerCase ();
			if (str_valid_chars.indexOf (chr_letter) == -1)
				obj.value = obj.value.replace (chr_letter, '');
		}
	}
}

// VALIDATE IF OBJECT IS VALID DOMAIN
function validate_domain (obj, str_err)
{
	if (obj != null)
	{
		var str_valid_chars = "abcdefghijklmnopqrstuvwxyz1234567890-";
		for (var index = 0; index < obj.value.length; index ++) 
		{
			var chr_letter = obj.value.charAt (index).toLowerCase ();
			if (str_valid_chars.indexOf (chr_letter) == -1)
			{
				obj.value = obj.value.replace (chr_letter, '');
			}
		}
	}
}

// VALIDATE IF A STRING IS A VALID DATE
function is_date (str_date)
{
	str_date_regex = /^(?:\s+)?(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($| (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(( [AP]M)|( [ap]m)))|([01]\d|2[0-3])(:[0-5]\d){1,2})?(?:\s+)?$/i;
	
	return (str_date_regex.test (str_date));
}

// VALIDATE IF A STRING IS A VALID EMAIL ADDRESS
function is_email (str_email_address)
{
	str_email_regex = /^[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*@[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*\.([A-Za-z]){2,4}$/i;
	
	return (str_email_regex.test (str_email_address));
}

// VALIDATE IF A STRING IS A VALID IP ADDRESS
function is_ip (str_ip_address)
{
	str_ip_regex = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;

	return (str_ip_regex.test (str_ip_address));
}

//***************************************************************************
//S-T-A-R-T													  BROWSER SNIFFER
//***************************************************************************

function is_IE ()
{
	if (navigator.appName.toLowerCase ().indexOf ("microsoft") != -1)
		return true;
	else
		return false;
}

function is_NS ()
{
	if (navigator.appName.toLowerCase ().indexOf ("netscape") != -1)
		return true;
	else
		return false;
}

function is_OPERA ()
{
	if (navigator.appName.toLowerCase ().indexOf ("opera") != -1)
		return true;
	else
		return false;
}

function is_MAC ()
{
	if (navigator.appName.toLowerCase ().indexOf ("macintosh") != -1)
		return true;
	else
		return false;
}

//***************************************************************************
//S-T-A-R-T												   CHECK ALL HANDLING
//***************************************************************************

var arr_class_name = new Array ();
var b_arr_class_initialized = false;
var arr_checked = new Array ();
var b_arr_checked_initialized = false;
var b_action_selected = false;
var b_record_selected = false;
var STR_IDS = "";

// SAVE LIST ORIGINAL CLASSES
function init_clases (str_ids)
{
	STR_IDS = str_ids;
	if (!b_arr_class_initialized)
	{
		var str_ids_split = str_ids.split (','); 
		var index = 0;

		while (index < str_ids_split.length)
		{
			arr_class_name [str_ids_split [index]] = get_element_by_id (HTML_LIST_TR_PREFIX + str_ids_split [index]).className;
			index ++;
		}
		b_arr_class_initialized = true;
	}
}

// INITIALIZE ALL CHECKBOXES WITH NULL 
function init_checked (str_ids)
{
	STR_IDS = str_ids;
	if (!b_arr_checked_initialized)
	{
		var str_ids_split = str_ids.split (','); 
		var index = 0;

		while (index < str_ids_split.length)
		{
			arr_checked [str_ids_split [index]] = null;
			index ++;
		}
		b_arr_checked_initialized = true;
	}
}

// CHECK ALL ITEMS
function check_all (str_ids)
{
	var str_ids_split = str_ids.split (','); 
	var index = 0;
	init_clases (str_ids);
	init_checked (str_ids);
	
	while (index < str_ids_split.length)
	{
		if (get_element_by_id (HTML_LIST_CHECKBOX_ALL).checked == true)
		{	
			get_element_by_id (HTML_LIST_CHECKBOX_ITEM + str_ids_split [index]).checked = true;
			get_element_by_id (HTML_LIST_TR_PREFIX + str_ids_split [index]).className = HTML_TR_SELECTED_CLASS;
			arr_checked [str_ids_split [index]] = "1";
			
			b_record_selected = true;
			
			if (b_action_selected)
			{
				try
				{
					get_element_by_id (HTML_BTN_ACTION).disabled = false;
					get_element_by_id (HTML_BTN_ACTION).title = "";
					
					get_element_by_id ("btn_list_action").disabled = false;
					get_element_by_id ("btn_list_action").title = "";
				}
				catch (er)
				{}
			}
		}
		else
		{
			get_element_by_id (HTML_LIST_TR_PREFIX + str_ids_split [index]).className = arr_class_name [str_ids_split [index]];
			get_element_by_id (HTML_LIST_CHECKBOX_ITEM + str_ids_split [index]).checked = false;
			arr_checked [str_ids_split [index]] = null;
			
			b_record_selected = false;
			try
			{
				get_element_by_id (HTML_BTN_ACTION).disabled = true;
				get_element_by_id ("btn_list_action").disabled = true;
			}
			catch (er)
			{}
		}
		index ++;
	}
}

// CHECK ITEM
function check_item (str_ids, str_tr_id)
{
	init_clases (str_ids);
	init_checked (str_ids);

	if (get_element_by_id (HTML_LIST_CHECKBOX_ITEM + str_tr_id).checked == true)
	{
		get_element_by_id (HTML_LIST_TR_PREFIX + str_tr_id).className = HTML_TR_SELECTED_CLASS;
		arr_checked [str_tr_id] = "1";
		
		b_record_selected = true;
		if (b_action_selected)
		{
			try
			{
				get_element_by_id (HTML_BTN_ACTION).disabled = false;
				get_element_by_id (HTML_BTN_ACTION).title = "";
				
				get_element_by_id ("btn_list_action").disabled = false;
				get_element_by_id ("btn_list_action").title = "";
			}
			catch (er)
			{}
		}
	}
	else
	{
		get_element_by_id (HTML_LIST_TR_PREFIX + str_tr_id).className = arr_class_name [str_tr_id];
		arr_checked [str_tr_id] = null;
	}
	
	var str_ids_split = str_ids.split (','); 
	var index = 0;
	var checked_count = 0;
	while (index < str_ids_split.length)
	{
		if (get_element_by_id (HTML_LIST_CHECKBOX_ITEM + str_ids_split [index]).checked == true)
			checked_count ++;
		index ++;
	}
	if (checked_count == str_ids_split.length)
	{
		try
		{
			get_element_by_id (HTML_LIST_CHECKBOX_ALL).checked = true;
		}
		catch (er)
		{}
	}
	else
	{
		try
		{
			get_element_by_id (HTML_LIST_CHECKBOX_ALL).checked = false;
		}
		catch (er)
		{}
		
		if (checked_count == 0)
		{
			try
			{
				get_element_by_id (HTML_BTN_ACTION).disabled = true;
				get_element_by_id ("btn_list_action").disabled = true;
			}
			catch (er)
			{}
			b_record_selected = false;
		}
	}
}

// CHECK SIMPLE PAGE ITEM
function check_item_box (str_ids, str_tr_id)
{
	init_clases (str_ids);
	init_checked (str_ids);

	if (get_element_by_id (HTML_LIST_CHECKBOX_ITEM + str_tr_id).checked == true)
		get_element_by_id (HTML_LIST_TR_PREFIX + str_tr_id).className = HTML_TR_SELECTED_CLASS;
	else
		get_element_by_id (HTML_LIST_TR_PREFIX + str_tr_id).className = arr_class_name [str_tr_id];
}

// UNCHECK ALL PASSED ITEMS
function uncheck_items (str_ids)
{
	init_clases (str_ids);
	init_checked (str_ids);
	
	var str_ids_split = str_ids.split (','); 
	
	for (index = 0; index < str_ids_split.length; index ++)
	{
		get_element_by_id (HTML_LIST_CHECKBOX_ITEM + str_ids_split [index]).checked = false;
		get_element_by_id (HTML_LIST_TR_PREFIX + str_ids_split [index]).className = arr_class_name [str_ids_split [index]];
	}
}

// GET CHECKED ITEMS ON PAGE ACTION SUBMIT
function get_checked ()
{
	var str_ids_split = STR_IDS.split (','); 
	var index = 0;
	var str_checked_items = "";

	while (index < str_ids_split.length)
	{
		if (arr_checked [str_ids_split [index]] != null)
			str_checked_items += str_ids_split [index] + ",";
		index ++;
	}
	return str_checked_items.substring (0, str_checked_items.length - 1);
}
		
// SAVE PAGE ACTION
function save_page_action (str_action_value, b_show_hint)
{
	if (str_action_value.split ('#')[0] != -1000)
	{
		b_action_selected = true;
		if (b_record_selected)
		{
			get_element_by_id (HTML_BTN_ACTION).disabled = false;
			get_element_by_id (HTML_BTN_ACTION).title = "";
			
			get_element_by_id ("btn_list_action").disabled = false;
			get_element_by_id ("btn_list_action").title = "";
		}
	}
	else
	{
		b_action_selected = false;
		get_element_by_id (HTML_BTN_ACTION).disabled = true;
		get_element_by_id ("btn_list_action").disabled = true;
	}
	
	if (b_show_hint == "True")
	{
		set_visible (get_element_by_id ("td_hint"), true);
		get_element_by_id ("spn_hint").innerHTML = str_action_value.split ('#')[1];
	}
	
	get_element_by_id (HTML_LIST_HIDDEN_ACTION).value = str_action_value.split ('#')[0];
}

var arr_tr_details = new Array;
var b_arr_details_initialized = false;
var b_expand_all_details = false;

// SAVE LIST ORIGINAL CLASSES
function init_details (str_ids)
{
	if (!b_arr_details_initialized)
	{
		var str_ids_split = str_ids.split (','); 
		var index = 0;

		while (index < str_ids_split.length)
		{
			arr_tr_details [str_ids_split [index]] = false;
			index ++;
		}
		b_arr_details_initialized = true;
	}
}

// SHOW HIDE
function show_hide (obj_this, str_tr_id, str_type)
{
	arr_class_name [str_tr_id + str_type] = !arr_class_name [str_tr_id + str_type];
	set_visible (get_element_by_id (HTML_LIST_TR_PREFIX + str_tr_id + "_" + str_type), arr_class_name [str_tr_id + str_type]);
	
	if (obj_this.src)
	{
		if (arr_class_name [str_tr_id + str_type])
			obj_this.src = obj_this.src.replace ("on", "off");
		else
			obj_this.src = obj_this.src.replace ("off", "on");
	}
}

// HIDE SHOW (STARTS WITH FALSE)
function hide_show (obj_this, str_tr_id, str_type)
{
	arr_class_name [str_tr_id + str_type] = !arr_class_name [str_tr_id + str_type];
	set_visible (get_element_by_id (HTML_LIST_TR_PREFIX + str_tr_id + "_" + str_type), !arr_class_name [str_tr_id + str_type]);
	
	if (obj_this.src)
	{
		if (arr_class_name [str_tr_id + str_type])
			obj_this.src = obj_this.src.replace ("off", "on");
		else
			obj_this.src = obj_this.src.replace ("on", "off");
	}
}

// SHOW HIDE ITEM
function show_hide_item (str_tr_id)
{
	arr_class_name [str_tr_id] = !arr_class_name [str_tr_id];
	set_visible (get_element_by_id (str_tr_id), arr_class_name [str_tr_id]);
}

// SHOW HIDE ALL
function show_hide_all (str_ids, str_type)
{
	var str_ids_split = str_ids.split (','); 
	var index = 0;

	b_expand_all_details = !b_expand_all_details;
	
	while (index < str_ids_split.length)
	{
		set_visible (get_element_by_id (HTML_LIST_TR_PREFIX + str_ids_split [index] + "_" + str_type), b_expand_all_details);
		arr_class_name [str_ids_split [index]] = b_expand_all_details;
		
		if (b_expand_all_details)
			get_element_by_id (HTML_LIST_ICO_PREFIX + str_ids_split [index] + "_" + str_type).src = get_element_by_id (HTML_LIST_ICO_PREFIX + str_ids_split [index] + "_" + str_type).src.replace ("on", "off");
		else
			get_element_by_id (HTML_LIST_ICO_PREFIX + str_ids_split [index] + "_" + str_type).src = get_element_by_id (HTML_LIST_ICO_PREFIX + str_ids_split [index] + "_" + str_type).src.replace ("off", "on");
			
		index ++;
	}
}

//***************************************************************************
//S-T-A-R-T													  SELECT HANDLING
//***************************************************************************

// HIDE ALL 'SELECT' ELEMENTS FROM PAGE
function hide_selects (b_visibility_status)
{
	var index = 0;
	for (index = 0; index < document.getElementsByTagName ("select").length; index ++)
		document.getElementsByTagName ("select").item (index).style.visibility = b_visibility_status;
}

// SET SELECT OPTION
function set_select (obj, str_val)
{
	if (obj != null && obj != undefined && obj != 'undefined')
	{
		var index = 0; 
		while (index < obj.length)
		{
			if (obj.options [index].value == str_val)
			{
				obj.options [index].selected = true;
				return true;
			}
			index ++;
		}
		return false;
	}
	else
		return false;
}

// GET THE TEXT FROM SELECTED OPTION
function get_select_text (obj, str_val)
{
	if (obj != null)
	{
		var index = 0; 
		while (index < obj.length)
		{
			if (obj.options [index].value == str_val)
			{
				return obj.options [index].text;
			}
			index ++;
		}
		return null;
	}
}

//***************************************************************************
//S-T-A-R-T													TEXTAREA HANDLING
//***************************************************************************

// INSERT TEXT AT CURSOR POSITION INSIDE TEXTAREA
function insert_text (obj, str_text, i_limit)
{
	if (obj != null)
	{
		if (obj.createTextRange && obj.caretPos)
		{
			var caretPos = obj.caretPos;
			caretPos.text = caretPos.text.charAt (caretPos.text.length - 1) == ' ' ? str_text + ' ' : str_text;
			obj.focus ();
		}
		else 
		{
			obj.value += str_text;
			obj.focus ();
		}
		store_caret (this);
		count_limit (obj, i_limit, true)
    }
}

// STORE CARET  
function store_caret (obj) 
{
	if (obj.createTextRange) 
	{
		obj.caretPos = document.selection.createRange ().duplicate ();
	}
}

// COUNT REMAINING CHARACTERS
function count_limit (obj, i_limit, b_show_remaining) 
{
	if (obj != null)
	{
		if (obj.value.length > i_limit)
			obj.value = obj.value.substring (0, i_limit);
		else  
		{
			obj_spn = obj.id.replace ("tpl_", "spn_count_");
			if (get_element_by_id (obj_spn) != null)
			{
				get_element_by_id (obj_spn).innerHTML = "&nbsp;[" + (i_limit - obj.value.length) + "]";
				set_visible (get_element_by_id (obj_spn), b_show_remaining);
			}
		}
	}
}

// COUNT REMAINING CHARACTERS
function count_limit_by_id (obj, i_limit, str_counter_id, b_show_remaining) 
{
	if (obj != null)
	{
		i_length = Number (obj.value.length);
		i_limit = Number (i_limit);
		if (i_length > i_limit)
			obj.value = obj.value.substring (0, i_limit);
		else  
		{
			obj_spn = str_counter_id;
			
			if (get_element_by_id (obj_spn) != null)
			{
				get_element_by_id (obj_spn).innerHTML = "&nbsp;[" + (i_limit - i_length) + "]";
				set_visible (get_element_by_id (obj_spn), b_show_remaining);
			}
		}
	}
}

//***************************************************************************
//S-T-A-R-T											 TEXT DISPLAYING HANDLING
//***************************************************************************

// BLINK FORM ELEMENT
var b_blink_on_off = false;

function do_blink (obj, i_timer)
{
	if (obj != null)
	{
		b_blink_on_off = !b_blink_on_off;	
		set_visible (get_element_by_id (obj), b_blink_on_off);
		setTimeout ("do_blink (\"" + obj + "\",\"" + i_timer + "\");", i_timer);
	}
}

// SET FORM ELEMENT VISIBLE
function set_visible (obj, b_visible)
{
	if (obj != null)
	{
		if (b_visible == true)
		{
			obj.style.display = "";
			obj.style.visibility = "visible";
		}
		else 
		{
			obj.style.display = "none";
			obj.style.visibility = "hidden";
		}
	}
}

//***************************************************************************
//S-T-A-R-T													  LEGEND HANDLING
//***************************************************************************

// SHOW / HIDE LEGEND
var b_show_legend = true;
function show_legend ()
{
	set_visible (get_element_by_id (HTML_TR_LEGEND), b_show_legend);
	set_visible (get_element_by_id (HTML_IMG_HELP_ON), b_show_legend);
	set_visible (get_element_by_id (HTML_IMG_HELP_OFF), !b_show_legend);
	
	b_show_legend = !b_show_legend;
}

// SAVE NEW LEGEND SETTINGS
function save_legend_settings (b_show)
{
	if (b_show)
	{
		set_cookie (STR_COOKIE_LEGEND, "1", 10);
		b_show_legend = false;
		set_visible (get_element_by_id (HTML_IMG_HELP_ON), !b_show_legend);
		set_visible (get_element_by_id (HTML_IMG_HELP_OFF), b_show_legend);
	}
	else
	{
		set_visible (get_element_by_id (HTML_TR_LEGEND), false);
		set_cookie (STR_COOKIE_LEGEND, "2", 10);
		b_show_legend = true;
		set_visible (get_element_by_id (HTML_IMG_HELP_OFF), b_show_legend);
		set_visible (get_element_by_id (HTML_IMG_HELP_ON), !b_show_legend);
	}
}

// SHOW OR HIDE LEGEND BASED ON COOKIE INFORMATION
function handle_legend ()
{
	if (get_element_by_id (HTML_TR_LEGEND) != null)
	{
		if (get_cookie (STR_COOKIE_LEGEND))
		{
			if (get_cookie (STR_COOKIE_LEGEND) == 1)
			{
				set_visible (get_element_by_id (HTML_TR_LEGEND), true);
				b_show_legend = false;
				if (get_element_by_id (HTML_RADIO_SHOW) != null)
					get_element_by_id (HTML_RADIO_SHOW).checked = true;
				if (get_element_by_id (HTML_IMG_HELP_ON) != null)	
					set_visible (get_element_by_id (HTML_IMG_HELP_ON), true);
			}
			else if (get_cookie (STR_COOKIE_LEGEND) == 2)
			{
				set_visible (get_element_by_id (HTML_TR_LEGEND), false);
				b_show_legend = true;
				if (get_element_by_id (HTML_RADIO_SHOW) != null)
					get_element_by_id (HTML_RADIO_SHOW).checked = true;
				if (get_element_by_id (HTML_IMG_HELP_OFF) != null)	
					set_visible (get_element_by_id (HTML_IMG_HELP_OFF), true);
			}
		}
		else
		{
			set_visible (get_element_by_id (HTML_TR_LEGEND), true);
			b_show_legend = false;
			if (get_element_by_id (HTML_RADIO_SHOW) != null)
				get_element_by_id (HTML_RADIO_SHOW).checked = true;
			if (get_element_by_id (HTML_IMG_HELP_ON) != null)	
				set_visible (get_element_by_id (HTML_IMG_HELP_ON), true);
		}
	}
}

//***************************************************************************
//S-T-A-R-T													   FOCUS HANDLING
//***************************************************************************

// SET FOCUS ON FIRST INPUT TYPE=TEXT FROM PAGE
function handle_focus ()
{
	var index = 0;
	var b_input_has_focus = false;

	while ((index < document.getElementsByTagName ("input").length) && (!b_input_has_focus))
	{
		try
		{
			if (document.getElementsByTagName ("input").item (index).type == "text")
			{
				document.getElementsByTagName ("input").item (index).focus ();
				b_input_has_focus = true;
			}
		}
		catch (e)
		{
			// IF DISABLED OR CANNOT FOCUS, MOVE TO THE NEXT INPUT TYPE=TEXT
		}
		index ++;
	}
}

// SET FOCUS
function set_focus (obj)
{
	if (obj != null)
		obj.focus ();
}

//***************************************************************************
//S-T-A-R-T													  COOKIE HANDLING
//***************************************************************************

// GET COOKIE BY NAME
function get_cookie (str_cookie_name) 
{
	var i_start = document.cookie.indexOf (str_cookie_name + "=");
	var i_lenght = i_start + str_cookie_name.length + 1;
	
	if ((!i_start) && (str_cookie_name != document.cookie.substring (0, str_cookie_name.length))) 
		return null;
	
	if (i_start == -1) 
		return null;
	
	var i_end = document.cookie.indexOf (";", i_lenght);
	if (i_end == -1) 
		i_end = document.cookie.length;
		
	return unescape (document.cookie.substring (i_lenght, i_end));
}
	
// SET NEW COOKIE
function set_cookie (str_cookie_name, str_cookie_value, i_exp_years, str_path, str_domain, str_secure) 
{
	var d_exp = new Date ();
	d_exp.setTime (d_exp.getTime () + (i_exp_years * 360 * 24 * 60 * 60 * 1000));
	document.cookie = str_cookie_name + "=" + escape (str_cookie_value) +
		( (i_exp_years) ? ";expires=" + d_exp.toGMTString () : "") +
		( (str_path) ? ";path=" + str_path : "") +
		( (str_domain) ? ";domain=" + str_domain : "") +
		( (str_secure) ? ";secure" : "");
}

// DELETE EXISTING COOKIE
function delete_cookie (str_name, str_path, str_domain)
{
	if (get_cookie (str_name))
	{
		document.cookie = str_name + "=" + 
			((str_path) ? "; path=" + str_path : "") +
			((str_domain) ? "; domain=" + str_domain : "") +
			"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

//***************************************************************************
//S-T-A-R-T							                 URL QUERYSTRING HANDLING
//***************************************************************************

function page_query (q)
{
	if (q.length > 1) 
		this.q = q.substring (1);
	else 
		this.q = null;
			
	this.keyValuePairs = new Array ();
		
	if (this.q) 
	{
		for (var i = 0; i < this.q.split ("&").length; i ++) 
		{
			this.keyValuePairs [i] = unescape (this.q.split ("&")[i]);
		}
	}
	this.getKeyValuePairs = function () 
	{ 
		return this.keyValuePairs; 
	}
	this.getValue = function (s) 
	{
		for (var j = 0; j < this.keyValuePairs.length; j ++) 
		{
			if (this.keyValuePairs [j].split ("=")[0] == s)
				return unescape (this.keyValuePairs [j].split ("=")[1]);
		}
		return false;
	}
	this.remKeyValue = function (s) 
	{
		var str_url = "?";
		for (var j = 0; j < this.keyValuePairs.length; j ++) 
		{
			if (this.keyValuePairs [j].split ("=")[0] != s)
				str_url += this.keyValuePairs [j] + "&";
		}
		return str_url.substring (0, str_url.length - 1);
	}
	this.getParameters = function () 
	{
		var a = new Array (this.getLength ());
		for (var j = 0; j < this.keyValuePairs.length; j ++) 
		{
			a [j] = unescape (this.keyValuePairs [j].split ("=")[0]);
		}
		return a;
	}
	this.getValues = function () 
	{
		var a = new Array (this.getLength ());
		for (var j = 0; j < this.keyValuePairs.length; j ++) 
		{
			a [j] = unescape (this.keyValuePairs [j].split ("=")[1]);
		}
		return a;
	}
	this.getLength = function () 
	{ 
		return this.keyValuePairs.length; 
	} 
}

function query_string (key)
{
	var page = new page_query (window.location.search); 
	return page.getValue (key); 
}

function query_string_rem (key)
{
	var page = new page_query (window.location.search); 
	return page.remKeyValue (key); 
}

function query_string_values ()
{
	var page = new page_query (window.location.search); 
	return page.getValues (); 
}

function query_string_keys ()
{
	var page = new page_query (window.location.search); 
	return page.getParameters (); 
}

function query_string_keys_values ()
{
	var page = new page_query (window.location.search);
	return page.getKeyValuePairs ();
}

//***************************************************************************
//S-T-A-R-T							                EMAIL VALIDATION HANDLING
//***************************************************************************

// VALIDATE EMAIL ADDRESS
function validate_email (str_email_address, b_man, b_alert) 
{
	if (str_email_address == "" && b_man) 
	{
		if (b_alert) 
			alert ('email address is mandatory');
		return false;
	}
	var str_invalid_chars = '\/\'\\ ";:?!()[]\{\}^|';
	for (index = 0; index < str_invalid_chars.length; i++) 
	{
		if (str_email_address.indexOf (str_invalid_chars.charAt (index), 0) > -1) 
		{
			if (b_alert) 
				alert ('email address contains invalid characters');
			return false;
		}
	}
	for (index = 0; index < str_email_address.length; index ++) 
	{
		if (str_email_address.charCodeAt (index) > 127) 
		{
			if (b_alert) 
				alert ("email address contains non ascii characters.");
			return false;
		}
	}

	var i_at_pos = str_email_address.indexOf ('@', 0);
	if (i_at_pos == -1) 
	{
		if (b_alert) 
			alert ('email address must contain an @');
		return false;
	}
	if (i_at_pos == 0) 
	{
		if (b_alert) 
			alert ('email address must not start with @');
		return false;
	}
	if (str_email_address.indexOf ('@', i_at_pos + 1) > - 1) 
	{
		if (b_alert) 
			alert ('email address must contain only one @');
		return false;
	}
	if (str_email_address.indexOf ('.', i_at_pos) == -1) 
	{
		if (b_alert) 
			alert ('email address must contain a period in the domain name');
		return false;
	}
	if (str_email_address.indexOf ('@.', 0) != -1) 
	{
		if (b_alert) 
			alert ('period must not immediately follow @ in email address');
		return false;
	}
	if (str_email_address.indexOf ('.@', 0) != -1)
	{
		if (b_alert) 
			alert ('period must not immediately precede @ in email address');
		return false;
	}
	if (str_email_address.indexOf ('..', 0) != -1) 
	{
		if (b_alert) alert ('two periods must not be adjacent in email address');
		return false;
	}
	var suffix = str_email_address.substring (str_email_address.lastIndexOf ('.') + 1);
	if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum')
	{
		if (b_alert) 
			alert ('invalid primary domain in email address');
		return false;
	}
	return true;
}

//***************************************************************************
//S-T-A-R-T													ENCODING HANDLING
//***************************************************************************

// ENCODES A STRING ACCORDING TO "URL ENCODING" RULES 
function encode_url (str)
{
	var result = "";
	var index = 0;

	for (index = 0; index < str.length; index ++) 
	{
		result = result + "%";
		result = result + "0123456789ABCDEF".charAt ((str.charCodeAt (index)/16)&0x0F);
		result = result + "0123456789ABCDEF".charAt ((str.charCodeAt (index)/1)&0x0F);
	}

	return result;
}

// DECODES A STRING THAT HAS BEEN PREVIOUSLY ENCODED ACCORDING TO THE encode_url () FUNCTION
function decode_url (str_encoded)
{
	// CREATE A REGULAR EXPRESSION TO SEARCH ALL +S IN THE STRING
	var lsRegExp = /\+/g;
	// RETURN THE DECODED STRING
	return unescape (String (str_encoded).replace (lsRegExp, " ")); 
}

// ENCODES A STRING ACCORDING TO "CHARACTER ENTITY" SYMBOL RULES IN HTML.
function encode_html (str_text) 
{
	var str_result = "";
	var index = 0;

	for (index = 0; index < str_text.length; index ++) 
	{
		var c = str_text.charCodeAt (index);
		var tmp = "";

		while (c >= 1) 
		{
			tmp = "0123456789".charAt (c % 10) + tmp;

			c = c / 10;
		}
		if (tmp == "")
			tmp = "0";

		tmp = "#" + tmp;
		tmp = "&" + tmp;
		tmp = tmp + ";";

		str_result += tmp;
	}
	return str_result;
}

// DECODES A STRING ACCORDING TO "CHARACTER ENTITY" SYMBOL RULES IN HTML.
function decode_html (str) 
{
	var result = new String ("");
	var index = 0;

	while (index < str.length) 
	{
		while (str.charAt (index) != '&' && index < str.length)
			result = result + str.charAt (index ++);

		while (str.charAt (index) == '&' && index < str.length) 
		{
			if (index + 1 >= str.length || str.charAt (index + 1) != '#') 
			{
				result = result + str.charAt (index ++);
				continue;
			}
			index += 2;

			if (index >= str.length) 
			{
				result += "?";
				continue;
			}

			if (str.charAt (index) == 'x' || str.charAt (index) == 'X') 
			{
				var c = 0;
				index ++;
				if (index >= str.length || !isxdigit (str.charAt (index)))
					c = str.charAt (index - 1);
				else
					while (isxdigit(str.charAt (index)) && index < str.length)
						c = c * 16 + hexval2 (str.charAt (index ++));

				result = result + String.fromCharCode (c);
				if (index < str.length && str.charAt (index) == ';')
					index ++;
			} 
			else if (isxdigit (str.charAt (index))) 
			{
				var c = 0;
				while (index < str.length && isxdigit (str.charAt (index)))
					c = c * 10 + decval (str.charAt (index ++));
				result = result + String.fromCharCode (c);
				if (index < str.length && str.charAt (index) == ';')
					index ++;
			}
		}
	}
	return result;
}

// REPLACES TAGS WITH ENCODED TAGS AND REVERSE
function handle_html_tags (str_text, b_encode)
{
	if (b_encode == true)
	{
		str_text = replace_str (str_text, "<", "&lt;");
		str_text = replace_str (str_text, ">", "&gt;");
	}
	else
	{
		str_text = replace_str (str_text, "&lt;", "<");
		str_text = replace_str (str_text, "&gt;", ">");
	}
	return str_text;
}

// ENCODE UTF8
function encode_utf8 (str, urlField, urlLink)
{
	var result = "";
	var index = 0;

	for (index = 0; index < str.length; index ++) 
	{
		var c = str.charCodeAt (index);

		result = result + hexstr (0xC0 + ((c >> 6) & 0x03)) + " ";
		result = result + hexstr (0x3F & c) + " ";
	}

	urlField.value = result;				/*SET THE FORM-FIELD WITH THIS VALUE*/
	urlLink.href = "mailto:" + result;		/*SET THE TEST HYPERLINK WITH THIS VALUE*/
}

function hexstr (c) 
{
	var result = "";

	result = result + "0123456789ABCDEF".charAt ((c/16)&0x0F);
	result = result + "0123456789ABCDEF".charAt ((c/1)&0x0F);

	return result;
}

//* A HELPER FUNCTION FOR CONVERTING A HEX DIGIT INTO THE CORRESPONDING INTEGER VALUE. 
function hexval (c)
{
	if (String ('0').charCodeAt (0) <= c && c <= String ('9').charCodeAt (0))
			return c - String ('0').charCodeAt (0);
	if (String ('A').charCodeAt (0) <= c && c <= String ('F').charCodeAt (0))
			return c - String ('A').charCodeAt (0) + 10;
	if (String ('a').charCodeAt (0) <= c && c <= String ('f').charCodeAt (0))
			return c - String ('a').charCodeAt (0) + 10;
	return 0;
}

function hexval2 (c)
{
	if (c == '0') return 0;
	if (c == '1') return 1;
	if (c == '2') return 2;
	if (c == '3') return 3;
	if (c == '4') return 4;
	if (c == '5') return 5;
	if (c == '6') return 6;
	if (c == '7') return 7;
	if (c == '8') return 8;
	if (c == '9') return 9;

	if (c == 'a') return 10;
	if (c == 'b') return 11;
	if (c == 'c') return 12;
	if (c == 'd') return 13;
	if (c == 'e') return 14;
	if (c == 'f') return 15;

	if (c == 'A') return 10;
	if (c == 'B') return 11;
	if (c == 'C') return 12;
	if (c == 'D') return 13;
	if (c == 'E') return 14;
	if (c == 'F') return 15;

	return 16;
}

function isxdigit (c)
{
	return hexval2 (c) < 16;
}

function decval (c) 
{
	if (c == '0') return 0;
	if (c == '1') return 1;
	if (c == '2') return 2;
	if (c == '3') return 3;
	if (c == '4') return 4;
	if (c == '5') return 5;
	if (c == '6') return 6;
	if (c == '7') return 7;
	if (c == '8') return 8;
	if (c == '9') return 9;
	
	return 10;
}

function isdigit (c) 
{
	return decval (c) < 10;
}			