// JavaScript Document
function URLEncode(decodedtext)
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = decodedtext;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
}

function URLDecode(encodedtext)
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var encoded = encodedtext;
   var plaintext = "";
   var i = 0;
   
   if(encoded)
   {
	   while (i < encoded.length) {
		   var ch = encoded.charAt(i);
		   if (ch == "+") {
			   plaintext += " ";
			   i++;
		   } else if (ch == "%") {
				if (i < (encoded.length-2) 
						&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
						&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
					plaintext += unescape( encoded.substr(i,3) );
					i += 3;
				} else {
					alert( 'Bad escape combination near ...' + encoded.substr(i) );
					plaintext += "%[ERROR]";
					i++;
				}
			} else {
			   plaintext += ch;
			   i++;
			}
		} // while
   }
	return plaintext;
}

function cpt(xx) 
{
	var yy = xx.split(" ");
	for (x=0; x<yy.length; x++) 
	{
		yy[x] = yy[x].substring(0, 1).toUpperCase()+yy[x].substring(1);
	}
	xx = yy.join(" ");
	return xx;
}

function clear_select_options(selectbox)
{
	var _select = document.getElementById(selectbox);
	
	if(_select.length > 0)
	{
		while(_select.hasChildNodes())
		{
			for(var i = 0; i < _select.childNodes.length; i++)
			{
				_select.removeChild(_select.firstChild);
			}
		}
	}
}

function set_selected_index(selectbox, default_value)
{
	if(!selectbox)
	{
		return false;
	}
	
	if(selectbox.length > 0)
	{
		for(var i = 0; i < selectbox.length; i++)
		{
			if(selectbox.options[i].value == default_value)
			{
				selectbox.selectedIndex = i;
			}
		}
	}
	else
	{
		//Is Not a drop down box, just set value
		selectbox.value = default_value;
	}
}

function addSelectOption(selectbox,text,value)
{
	if(text.length < 1)
	{
		text = '';
	}

	if(value.length < 1)
	{
		value = '';
	}

	var form_element = document.getElementById(selectbox);

	var elOptNew = document.createElement('option');

	elOptNew.text = text;
	elOptNew.value = value;
	var elSel = form_element;
	
	try {
		elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
	}
	catch(ex) {
		elSel.add(elOptNew); // IE only
	}

	return true;
}

function change_table_cell_data(cell_id, new_data)
{
	if(document.getElementById(cell_id))
	{
		document.getElementById(cell_id).innerHTML = new_data;
	}
}

function hide_div_layer(div_layer_id)
{
	if(document.layers)	   //NN4+
	{
		document.getElementById(div_layer_id).visibility = "hide";
	}
	else if(document.getElementById)	  //gecko(NN6) + IE 5+
	{
		document.getElementById(div_layer_id).style.visibility = "hidden";
	}
	else if(document.all)	// IE 4
	{
		 document.getElementById(div_layer_id).style.visibility = "hidden";
	}
}

function show_div_layer(div_layer_id)
{
	if(document.getElementById(div_layer_id).style.visibility == 'hidden')
	{
		if(document.layers)	   //NN4+
		{
			document.getElementById(div_layer_id).visibility = "show";
		}
		else if(document.getElementById)	  //gecko(NN6) + IE 5+
		{
			document.getElementById(div_layer_id).style.visibility = "visible";
		}
		else if(document.all)	// IE 4
		{
			 document.getElementById(div_layer_id).style.visibility = "visible";
		}
	}
}

function addslashes(str) {
	str=str.replace(/\'/g,'\\\'');
	str=str.replace(/\"/g,'\\"');
	str=str.replace(/\\/g,'\\\\');
	str=str.replace(/\0/g,'\\0');
	return str;
}

function stripslashes(str) {
	str=str.replace(/\\'/g,'\'');
	str=str.replace(/\\"/g,'"');
	str=str.replace(/\\\\/g,'\\');
	str=str.replace(/\\0/g,'\0');
	return str;
}

function SetAllCheckBoxes(FormName, FieldName, CheckValue)
{
	var check_boxes = document.getElementsByName(FieldName+'[]');
	
	if(!check_boxes)
	{
		var check_boxes = document.getElementsByName(FieldName);
		
		if(!check_boxes)
		{
			var check_boxes = document.getElementById(FieldName);
			
			if(!check_boxes)
			{
				return false;	
			}
		}
	}
	
	var num_of_check_boxes = check_boxes.length;

	if(num_of_check_boxes < 1)
	{
		return false;
	}

	// set the check value for all check boxes
	for(var i = 0; i < num_of_check_boxes; i++)
	{
		check_boxes[i].checked = CheckValue;
	}
}

function remove_select_option(selectbox)
{
	var select_obj = document.getElementById(selectbox);
	var i;
	for(i=select_obj.options.length-1;i>=0;i--)
	{
		if(select_obj.options[i].selected)
		{
			select_obj.remove(i);
		}
	}
}

function add_select_option(select_id, option_text, option_value)
{
	var select_obj = document.getElementById(select_id);
	var num_of_items_in_select = select_obj.length;
	select_obj.options[num_of_items_in_select] = new Option(option_text,option_value);
	
	return true;
}

function copy_to_clipboard(field_id) 
{
	meintext = document.getElementById(field_id).value;
	
	window.clipboardData.setData('Text',meintext);
}

function ask_and_redirect(question, target_url)
{
	var ans = confirm(question);
	
	if(ans == true)
	{
		document.location.href = target_url;
	}
}

