﻿function utf8_encode ( string ) {
 
    string = (string+'').replace(/\r\n/g, "\n").replace(/\r/g, "\n");
 
    var utftext = "";
    var start, end;
    var stringl = 0;
 
    start = end = 0;
    stringl = string.length;
    for (var n = 0; n < stringl; n++) {
        var c1 = string.charCodeAt(n);
        var enc = null;
 
        if (c1 < 128) {
            end++;
        } else if((c1 > 127) && (c1 < 2048)) {
            enc = String.fromCharCode((c1 >> 6) | 192) + String.fromCharCode((c1 & 63) | 128);
        } else {
            enc = String.fromCharCode((c1 >> 12) | 224) + String.fromCharCode(((c1 >> 6) & 63) | 128) + String.fromCharCode((c1 & 63) | 128);
        }
        if (enc != null) {
            if (end > start) {
                utftext += string.substring(start, end);
            }
            utftext += enc;
            start = end = n+1;
        }
    }
 
    if (end > start) {
        utftext += string.substring(start, string.length);
    }
 
 	 //document.forms[0].Fname.value=utftext;
    return utftext;
}
function utf8_decode ( str_data ) {
 
    var tmp_arr = [], i = 0, ac = 0, c1 = 0, c2 = 0, c3 = 0;
 
    str_data += '';
 
    while ( i < str_data.length ) {
        c1 = str_data.charCodeAt(i);
        if (c1 < 128) {
            tmp_arr[ac++] = String.fromCharCode(c1);
            i++;
        } else if ((c1 > 191) && (c1 < 224)) {
            c2 = str_data.charCodeAt(i+1);
            tmp_arr[ac++] = String.fromCharCode(((c1 & 31) << 6) | (c2 & 63));
            i += 2;
        } else {
            c2 = str_data.charCodeAt(i+1);
            c3 = str_data.charCodeAt(i+2);
            tmp_arr[ac++] = String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
            i += 3;
        }
    }
 
    return tmp_arr.join('');
}

function URLEncode($xx )
{
  var plaintext = $xx
 
    var SAFECHARS = "0123456789" +					// Numeric
		        "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
		        "abcdefghijklmnopqrstuvwxyz" +
		        "-_.!~*'()";					// RFC2396 Mark characters
    var HEX = "0123456789ABCDEF";

    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 RFC2396 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( )
{
   var encoded = document.URLForm.F2.value;
   if (document.URLForm.RFC2396.checked) {  // OLD Browser mode
     // Replace + with ' '
     // Replace %xx with equivalent character
     // Put [ERROR] in output if %xx is invalid.
     var HEXCHARS = "0123456789ABCDEFabcdef"; 
     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
     document.URLForm.F1.value = plaintext;
   } else { // Modern browser mode
     try {
         document.URLForm.F1.value = decodeURIComponent(encoded);
     } catch (error) {
         alert( error + ' - Probably the second byte of a Unicode character is missing.' );
     }
   }
   document.URLForm.F1.select();
   return false;
};

