/*

Class: general

Depends: none

Description: Implements common functions

*/

function yagi_general () {

	this.getObject = function (id, returnFirst) {

		if (id) {

			if (id.tagName) {

				return id;

			}

			var obj = document.getElementById(id);

			if (obj) {

				return obj;

			}
			else {

				obj = document.getElementsByName(id);

				if (obj.length == 0) {

					return false;

				}
				else {

					if (returnFirst) {

						return obj.item(0);

					}
					else {

						return obj;

					}

				}

			}

		}
		else {

			return false;

		}

	}

	this.getValue = function (id) {

		var obj = this.getObject(id);

		if (obj.value) {

			return obj.value;

		}
		else {

			for (var i = 0; i < obj.length; i++) {

				switch (obj.item(i).type) {

					case "radio":
						if (obj.item(i).checked) {

							return obj.item(i).value;

						}
						break;

					default:
						return obj.item(i).value;

				}

			}

			return "";

		}

	}

	this.ltrim = function (str) {

		while (str.substr(0, 1) == " ") {

			str = str.substr(1);

		}

		return str;

	}

	this.rtrim = function (str) {

		while (str.substr(str.length - 1, 1) == " ") {

			str = str.substr(0, str.length - 1);

		}

		return str;

	}

	this.trim = function (str) {

		return this.ltrim(this.rtrim(str));

	}

	this.inArray = function (value, array) {

		for (var i = 0; i < array.length; i++) {

			if (array[i] == value) {

				return true;

			}

		}

		return false;

	}

	this.utf8_encode = function (string) {

		var utftext = "";
 
		string = string.replace(/\r\n/g,"\n");

		for (var i = 0; i < string.length; i++) {
 
			var c = string.charCodeAt(i);
 
			if (c < 128) {

				utftext += String.fromCharCode(c);

			}
			else if((c > 127) && (c < 2048)) {

				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);

			}
			else {

				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);

			}
 
		}
 
		return utftext;

	}

	this.utf8_decode = function (utftext) {

		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while (i < utftext.length) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {

				string += String.fromCharCode(c);
				i++;

			}
			else if((c > 191) && (c < 224)) {

				c2 = utftext.charCodeAt(i + 1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;

			}
			else {

				c2 = utftext.charCodeAt(i + 1);
				c3 = utftext.charCodeAt(i +2 );
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;

			}
 
		}
 
		return string;

	}

}
