/*

	Funciones y nuevos métodos para objetos de javascript.
	@autor@: Antonio Cortés (Dr Zippie) <antonio@antoniocortes.com>
	@license@: BSD

*/

var drzJs = Class.create();
drzJs.prototype = {
	initialize: function () {
	
	},
	changeSelectContent:  function( obj, data, post ) {
		obj.enabled = false;
 		var miArray = data.split("\n");
		
		obj.options.length  = 0 ;
		var length = 0 ;
		var defaultSelected = true;
		var selected = true;
		
		miArray.each( function( line  ){
			if ( line.split(':').length == 2 ) {
				obj.options[length] =  new Option( line.split(':')[1] ,    line.split(':')[0].UTF8Decode() ,  defaultSelected, selected) ;
				length = obj.options.length;
				if ( length == 1 ) {
					defaultSelected = false;
					selected = false;	
				}		}
			}
			);
			
		
		obj.enabled = true ;
		if ( post != null ) {
			post()
		}
	}
 }
/*

	Creamos 2 nuevos métodos para los objetos String 
	UTF8Decode -> string
	URLDecode-> string

*/
Object.extend(String.prototype, {
	URLDecode: function() { 
		var HEXCHARS = "0123456789ABCDEFabcdef"; 
		var plaintext = "";
		var i = 0 ;
		var encoded = this ;
		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 {
					plaintext += "%[ERROR]";
					i++;
				}
			} else {
				plaintext += ch;
				i++;
			}
		} 
		return plaintext ;
	} ,
	UTF8Decode  : function() {
		var utftext = this ;
		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;
	}
});
