function urlEncode_doIt(type) {
	var input = trim(document.getElementById('urlencode_input').value);
	//if(input == "") { 
		//alert("Input is empty! Please copy paste the string to encode/decode inside the top textbox.");
		//return false;
	//}
	if(type == "escape") {
		$('urlencode_escape').value = escape(input);
	} else if (type == "unescape") {
		$('urlencode_escape').value = unescape(input);
	} else if (type == "encodeURI") {
		$('urlencode_encodeURI').value = encodeURI(input);
	} else if (type == "decodeURI") {
		$('urlencode_encodeURI').value = decodeURI(input);
	} else if (type == "encodeURIComponent") {
		$('urlencode_encodeURIComponent').value = encodeURIComponent(input);
	} else if (type == "decodeURIComponent") {
		$('urlencode_encodeURIComponent').value = decodeURIComponent(input);
	} else if (type == "char-to-ascii") {
		$('urlencode_char-ascii').value = charToAscii(input);
	} else if (type == "ascii-to-char") {
		$('urlencode_char-ascii').value = asciiToChar(input);
	} else if (type == "char-to-hex") {
		$('urlencode_char-hex').value = charToHex(input);
	} else if (type == "hex-to-char") {
		$('urlencode_char-hex').value = hexToChar(input);
	} else if (type == "char-to-binary") {
		$('urlencode_char-binary').value = charToBinary(input);
	} else if (type == "binary-to-char") {
		$('urlencode_char-binary').value = binaryToChar(input);
	}
}
function applyPHP(resultid, mode) {
	var opt = {
		method: 'post',
		postBody: 'url=' + encodeURIComponent($('urlencode_input').value) + '&mode=' + mode,
		onSuccess: function(t) {
			$(resultid).value = t.responseText;
		},
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}
	};
	var oAjax = new Ajax.Request('http://www.williamkhoe.com/swissarmy/includes/php/urlencode.php', opt);
}
function getParsedURL() {
	var opt = {
		method: 'post',
		postBody: 'url=' + encodeURIComponent($('parseurl_input').value) + '&mode=parseurl',
		onSuccess: function(t) {
			try {
				eval(t.responseText); 
				$('parseurl_scheme').innerHTML = parseurl_result['scheme'];
				$('parseurl_host').innerHTML = parseurl_result['host'];
				$('parseurl_path').innerHTML = parseurl_result['path'];
				$('parseurl_fragment').innerHTML = parseurl_result['fragment'];	
				var queryOutput = "undefined";
				if(parseurl_result['query'] != null) {
					var queryArray = parseurl_result['query'].split("&");
					queryOutput = "<table cellpadding=\"2\" cellspacing=\"0\" border=\"0\">";
					var temp;
					for(var i = 0; i < queryArray.length; i++) {
						temp = queryArray[i].split("=");
						queryOutput += "<tr><td valign=\"top\">" + temp[0] + "</td><td valign=\"top\">= " + temp[1] + "</td></tr>";
					}
					queryOutput += "</table>";
				}
				$('parseurl_query').innerHTML = queryOutput;
				//$('parseurl_username').innerHTML = parseurl_result['username'];
				//$('parseurl_password').innerHTML = parseurl_result['password'];
			} catch(e) { alert(e); }
			$('parseurl_output').style.display = "block";
		},
		onFailure: function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}
	};
	var oAjax = new Ajax.Request('http://www.williamkhoe.com/swissarmy/includes/php/urlencode.php', opt);
}

var charArray = new Array(
	' ', '!', '"', '#', '$', '%', '&', "'", '(', ')', '*', '+', ',', '-',
	'.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';',
	'<', '=', '>', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
	'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
	'X', 'Y', 'Z', '[', '\\', ']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e',
	'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
	't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~', '', 'Ç', 'ü',
	'é', 'â', 'ä', 'à', 'å', 'ç', 'ê', 'ë', 'è', 'ï', 'î', 'ì', 'Ä', 'Å',
	'É', 'æ', 'Æ', 'ô', 'ö', 'ò', 'û', 'ù', 'ÿ', 'Ö', 'Ü', 'ø', '£', 'Ø',
	'×', 'ƒ', 'á', 'í', 'ó', 'ú', 'ñ', 'Ñ', 'ª', 'º', '¿', '®', '¬', '½',
	'¼', '¡', '«', '»', '_', '_', '_', '¦', '¦', 'Á', 'Â', 'À', '©', '¦',
	'¦', '+', '+', '¢', '¥', '+', '+', '-', '-', '+', '-', '+', 'ã', 'Ã',
	'+', '+', '-', '-', '¦', '-', '+', '¤', 'ð', 'Ð', 'Ê', 'Ë', 'È', 'i',
	'Í', 'Î', 'Ï', '+', '+', '_', '_', '¦', 'Ì', '_', 'Ó', 'ß', 'Ô', 'Ò',
	'õ', 'Õ', 'µ', 'þ', 'Þ', 'Ú', 'Û', 'Ù', 'ý', 'Ý', '¯', '´', '­', '±',
	'_', '¾', '¶', '§', '÷', '¸', '°', '¨', '·', '¹', '³', '²', '_', ' ');

var hex_digits = new Array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F');


function charToByte(c) {
	var i;
	for(i=0; i < charArray.length; i++) {
		if(c == charArray[i]) return i+32;
	}
	return 0;
}

function byteToChar(n) {
	if(n < 32 || n > 255) return " ";
	return charArray[n-32];
}

function byteToHex(n) {
	return hex_digits[(n >> 4) & 0xf] + hex_digits[n & 0xf];
}

function byteToBin(n) {
	var ret_str = "";
	var i;
	for(i=7; i>=0; i--) {
		ret_str += (n >> i) & 1;
	}
	return ret_str;
}

function clean_numstr(raw_str, base) {
	var ret_str = "";
	var c = "";
	var i;
	for(i=0; i < raw_str.length; i++) {
		c = raw_str.charAt(i);
		if(c == "0" || parseInt(c, base) > 0) {
			ret_str += c;
		}
	}
	return ret_str;
}

function isNothing(str, theForm, form_bit) {
	if(str == "") {
		alert("This field evaluates to nothing!");
		return false;
	}
	eval(form_bit);
	return true;
}


function hex_from_dec(input) {
	var dec_str = clean_numstr(input, 10);
	//var delimiter = theForm.delimiter.value;
	var hex_str = "";
	var num_str = "";
	var i = 0, n;

	while(i < dec_str.length) {
		n = 0;
		//if(i > 0) hex_str += delimiter;
		for(; i < dec_str.length && (n < 25 || (n == 25 && dec_str.charAt(i) < 6)); i++) {
			n *= 10;
			n += parseInt(dec_str.charAt(i));
		}
		hex_str += byteToHex(n);
	}
	return hex_str;
}

function chars_from_hex(input) {
	var hex_str = clean_numstr(input, 16);
	var char_str = "";
	var num_str = "";
	var i;
	for(i=0; i < hex_str.length; i+=2)
		char_str += byteToChar(parseInt(hex_str.substring(i, i+2), 16));
	return char_str;
}

function hex_from_chars(input) {
	var char_str = input;
	//var delimiter = theForm.delimiter.value;
	var hex_str = "";
	var i, n;
	for(i=0; i < char_str.length; i++) {
		n = charToByte(char_str.charAt(i));
		if(n != 0) {
			//if(i > 0) hex_str += delimiter;
			hex_str += byteToHex(n);
		}
	}
	return hex_str;
}

function dec_from_hex(input) {
	var hex_str = clean_numstr(input, 16);
	//var delimiter = theForm.delimiter.value;
	var dec_str = "";
	var dec_byte = "";
	var i, temp;
	var htmlFormat = $('parseurl_html').checked;
	for(i=0; i < hex_str.length-1; i+=2) {
		//if(i > 0) dec_str += delimiter;
		temp = parseInt(hex_str.substring(i, i+2), 16);
		if(htmlFormat) { temp = "&#" + temp + ";"; }
		dec_str += temp;
	}
	return dec_str;
}

function bin_from_hex(input) {
	var hex_str = clean_numstr(input, 16);
	//var delimiter = theForm.delimiter.value;
	var bin_str = "";
	var bin_byte = "";
	var i;
	for(i=0; i < hex_str.length-1; i+=2) {
		//if(i > 0) bin_str += delimiter;
		bin_str += byteToBin(parseInt(hex_str.substring(i, i+2), 16));
	}
	return bin_str;
}

function hex_from_bin(input) {
	//var delimiter = theForm.delimiter.value;
	var bin_str = clean_numstr(input, 2);
	var hex_str = "";
	var i;

	for (i=0; i < bin_str.length-7; i+=8) {
		//if(i > 0) hex_str += delimiter;
		hex_str += byteToHex(parseInt(bin_str.substring(i, i+8), 2));
	}
	return hex_str;
}

function charToAscii(input) {
	return dec_from_hex(hex_from_chars(input));
}
function asciiToChar(input) {
	return chars_from_hex(hex_from_dec(input));
}
function charToHex(input) {
	return hex_from_chars(input);
}
function hexToChar(input) {
	return chars_from_hex(input);
}
function charToBinary(input) {
	return bin_from_hex(hex_from_chars(input));
}
function binaryToChar(input) {
	return chars_from_hex(hex_from_bin(input));
}
