var hexVals = new Array("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F");
var unsafeString = "\"<>%\\^[]`";

function highlight(element1){element1.focus();element1.select();}

function URLDecode()
{
	var returnstr=unescape(form1.string1.value);
	return returnstr;
}

function URLEncode(val)
{
	var state= 'urlenc';
	var len= val.length;
	var backlen = len;
	var i = 0;

	var newStr= "";
	var frag = "";
	var encval= "";

	for (i=0;i<len;i++) 
	{
		tval1=val.substring(i,i+1);
		newStr = newStr + "%" + decToHex(tval1.charCodeAt(0),16);
	}
	return newStr;
}

function decToHex(num, radix) // part of URL Encode
{
	var hexString = "";
	while (num >= radix)
	{
		temp = num % radix;
		num = Math.floor(num / radix);
		hexString += hexVals[temp];
	}
	hexString += hexVals[num];
	return reversal(hexString);
}

function reversal(s) // part of URL Encode
{
	var len = s.length;
	var trans = "";
	for (i=0; i<len; i++)
	{
		trans = trans + s.substring(len-i-1, len-i);
	}
	s = trans;
	return s;
}

function isURLok(compareChar) // part of URL Encode
{
	if (unsafeString.indexOf(compareChar) == -1 && compareChar.charCodeAt(0) > 32 && compareChar.charCodeAt(0) < 123) 
	{
		return true;
	}
	else
	{
 		return false;
	}
}



// ====================================================================
//       URLencode and URLdecode functions
//
// Copyright Albion Research Ltd. 2002
// http://www.albionresearch.com/
//
// You may copy these functions providing that 
// (a) you leave this copyright notice intact, and 
// (b) if you use these functions on a publicly accessible
//     web site you include a credit somewhere on the web site 
//     with a link back to http://www.albionresearch.com/
//
// If you find or fix any bugs, please let us know at albionresearch.com
//
// SpecialThanks to Neelesh Thakur for being the first to
// report a bug in URLDecode() - now fixed 2003-02-19.
// And thanks to everyone else who has provided comments and suggestions.
// ====================================================================
function URLencode(what)
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.~";					// RFC2396 Mark characters
//					"-_.!'~*()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = what;
	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(what)
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var encoded = what;
   var plaintext = "";
   var i = 0;
   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;
//   document.URLForm.F1.value = plaintext;
//   return false;
}