var hexVals = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
	              "A", "B", "C", "D", "E", "F");
	var unsafeString = "\"<>%\\^[]`\+\$\,";
	// deleted these chars from the include list ";", "/", "?", ":", "@", "=", "&" and #
	// so that we could analyze actual URLs
	
	function isUnsafe(compareChar)
	// this function checks to see if a char is URL unsafe.
	// Returns bool result. True = unsafe, False = safe
	{
	if (unsafeString.indexOf(compareChar) == -1 && compareChar.charCodeAt(0) > 32
	    && compareChar.charCodeAt(0) < 123)
	   { return false; } // found no unsafe chars, return false
	else
	   { return true; }
	}
	
	function decToHex(num, radix)
	// part of the hex-ifying functionality
	{
	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 the hex-ifying functionality
	{
	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 convert(val) // this converts a given char to url hex form
	{ return  "%" + decToHex(val.charCodeAt(0), 16); }
	
	
	function changeIt(val,state)	{
	var len     = val.length;
	var backlen = len;
	var i       = 0;
	
	var newStr  = "";
	var frag    = "";
	var encval  = "";
	var original = val;
	
	if (state == "none") // needs to be converted to normal chars
	   {
	     while (backlen > 0)
	           {
	             lastpercent = val.lastIndexOf("%");
	             if (lastpercent != -1) // we found a % char. Need to handle
	                {
	                  // everything *after* the %
	                  frag = val.substring(lastpercent+1,val.length);
	                  // re-assign val to everything *before* the %
	                  val  = val.substring(0,lastpercent);
	                  if (frag.length >= 2) // end contains unencoded
	                     {
	                     //  alert ("frag is greater than or equal to 2");
	                       encval = frag.substring(0,2);
	                       newStr = frag.substring(2,frag.length) + newStr;
	                       //convert the char here. for now it just doesn't add it.
	                       if ("01234567890abcdefABCDEF".indexOf(encval.substring(0,1)) != -1 &&
	                           "01234567890abcdefABCDEF".indexOf(encval.substring(1,2)) != -1)
	                          {
	                           encval = String.fromCharCode(parseInt(encval, 16)); // hex to base 10
	                           newStr = encval + newStr; // prepend the char in
	                          }
	                       // if so, convert. Else, ignore it.
	                     }
	                  // adjust length of the string to be examined
	                  backlen = lastpercent;
	                 // alert ("backlen at the end of the found % if is: " + backlen);
	                }
	            else { newStr = val + newStr; backlen = 0; } // if there is no %, just leave the value as-is
	           } // end while
	   }         // end 'state=none' conversion
	else         // value needs to be converted to URL encoded chars
	   {
	    for (i=0;i<len;i++)
	        {
	          if (val.substring(i,i+1).charCodeAt(0) < 255)  // hack to eliminate the rest of unicode from this
	             {
	              if (isUnsafe(val.substring(i,i+1)) == false)
	                 { newStr = newStr + val.substring(i,i+1); }
	              else
	                 { newStr = newStr + convert(val.substring(i,i+1)); }
	             }
	          else // woopsie! restore.
	             {
	               alert ("Found a non-ISO-8859-1 character at position: " + (i+1) + ",\nPlease eliminate before continuing.");
	               newStr = original; i=len;                // short-circuit the loop and exit
	             }
	        }
	
	   }
	    return newStr;
	}

	function ConvertString(input) {
		var output = "";
		var si = 0;
		for (var i = 0; i < input.length; i++) {
			//alert(input.charCodeAt(i));
			si = 0;
			
			if ((input.charCodeAt(i) == 13) && (input.charCodeAt(i + 1) == 10)) {
				si = 1;
				i++;
				output += "<BR>";
			}
			
			/*if (input.charCodeAt(i) == 32){
				si = 1;
				output += "%20";
			}*/
			if (input.charCodeAt(i)== 225){
				si = 1;
				output += "&aacute;";
			}
			if (input.charCodeAt(i)== 233){
				si = 1;
				output += "&eacute;";
			}
			if (input.charCodeAt(i)== 237){
				si = 1;
				output += "&iacute;";
			}
			if (input.charCodeAt(i)== 243){
				si = 1;
				output += "&oacute;";
			}
			if (input.charCodeAt(i)== 250){
				si = 1;
				output += "&uacute;";
			}
			if (input.charCodeAt(i)== 252){
				si = 1;
				output += "&uuml;";
			}
			if (input.charCodeAt(i)== 209){
				si = 1;
				output += "&ntilde;";
			}
			if (input.charCodeAt(i)== 193){
				si = 1;
				output += "&Aacute;";
			}
			if (input.charCodeAt(i)== 201){
				si = 1;
				output += "&Eacute;";
			}
			if (input.charCodeAt(i)== 205){
				si = 1;
				output += "&Iacute;";
			}
			if (input.charCodeAt(i)== 211){
				si = 1;
				output += "&Oacute;";
			}
			if (input.charCodeAt(i)== 218){
				si = 1;
				output += "&Uacute;";
			}
			if (input.charCodeAt(i)== 220){
				si = 1;
				output += "&Uuml;";
			}
			if (input.charCodeAt(i)== 241){
				si = 1;
				output += "&Ntilde;";
			}
			if (input.charCodeAt(i)== 39){
				si = 1;
				output += " ";
			}
			if (input.charCodeAt(i)== 38){
				si = 1;
				output += " ";
			}
			
			if (si == 0){
				output += input.charAt(i);
			}								   	
		}
		return output;
	}
	