/**
	This is a Javascript implementation of the Java Hashtable object.
	
	Contructor(s):
	 Hashtable()
			  Creates a new, empty hashtable
	
	Method(s):
	 void clear() 
			  Clears this hashtable so that it contains no keys. 
	 boolean containsKey(String key) 
			  Tests if the specified object is a key in this hashtable. 
	 boolean containsValue(Object value) 
			  Returns true if this Hashtable maps one or more keys to this value. 
	 Object get(String key) 
			  Returns the value to which the specified key is mapped in this hashtable. 
	 boolean isEmpty() 
			  Tests if this hashtable maps no keys to values. 
	 Array keys() 
			  Returns an array of the keys in this hashtable. 
	 void put(String key, Object value) 
			  Maps the specified key to the specified value in this hashtable. A NullPointerException is thrown is the key or value is null.
	 Object remove(String key) 
			  Removes the key (and its corresponding value) from this hashtable. Returns the value of the key that was removed
	 int size() 
			  Returns the number of keys in this hashtable. 
	 String toString() 
			  Returns a string representation of this Hashtable object in the form of a set of entries, enclosed in braces and separated by the ASCII characters ", " (comma and space). 
	 Array values() 
			  Returns a array view of the values contained in this Hashtable. 
	 void replace(String key, Object value) 
			  replace de value of key. 
			
*/
function Hashtable(){
	this.clear = hashtable_clear;
	this.containsKey = hashtable_containsKey;
	this.containsValue = hashtable_containsValue;
	this.get = hashtable_get;
	this.getById = hashtable_get_by_id;
	this.isEmpty = hashtable_isEmpty;
	this.keys = hashtable_keys;
	this.put = hashtable_put;
	this.remove = hashtable_remove;
	this.size = hashtable_size;
	this.toString = hashtable_toString;
	this.values = hashtable_values;
	this.hashtable = new Array();
	this.replace = hashtable_replace;
}                

/*=======Private methods for internal use only========*/
function hashtable_clear(){
	this.hashtable = new Array();
}

function hashtable_containsKey(key){
	var exists = false;
	for (var i in this.hashtable) {
		if (i == key && this.hashtable[i] != null) {
			exists = true;
			break;
		}
	}
	return exists;
}

function hashtable_containsValue(value){
	var contains = false;
	if (value != null) {
		for (var i in this.hashtable) {
			if (this.hashtable[i] == value) {
				contains = true;
				break;
			}
		}
	}
	return contains;
}

function hashtable_get(key){
	return this.hashtable[key];
}

function hashtable_get_by_id(id){
	for (var i in this.hashtable) {
		if (this.hashtable[i] != null && i != 'in_array') {
			var obj = this.get(i);
			if (obj.id == id){
				return obj;
			}
		}
	}
}
function hashtable_isEmpty(){
	return (this.size == 0) ? true : false;
}

function hashtable_keys(){
	var keys = new Array();
	for (var i in this.hashtable) {
		if (this.hashtable[i] != null && i != 'in_array') 
			keys.push(i);
	}
	return keys;
}

function hashtable_put(key, Array_values){
	if (key == null || Array_values == null || Array_values.length < 9) {
		throw "NullPointerException {" + key + "},{" + Array_values + "}";
	}else{
		/*
		 * Modificado por David para adaptarlo a las necesidades de BA.
		 * Se espera q el segundo argumento sea un arreglo de largo 8
		 * en el siguiente orden:
		 * 0->id
		 * 1->nombreparametro
		 * 2->tipo
		 * 3->agrupador
		 * 4->valor_defecto
		 * 5->valores (puede ser un array o una variable simple [string, int, etc])
		 * 6->validacion (se espera una expresión regular para los campos del tipo "text")
		 * 7->tipoBusqueda (este campo puede ser "normal" ó "oracle")
		 * 8->estado (este campo indica segun preferencias si el campo esta VISIBLE o NO por defecto
		 * 
		 * Por otro lado se espera que el key sea el id del paramatro que se usa en el html,
		 * es decir el campo "parametro" que viene en el diccionario
		 * 
		 */
		var obj = new Object();
		obj.id = Array_values[0];
		obj.nombreparametro = Array_values[1];
		obj.tipo = Array_values[2];
		obj.agrupador = Array_values[3];
		obj.valor_defecto = Array_values[4];
		if (is_array(Array_values[5])) {
			obj.valores = Array_values[5];
		}else{
			obj.valores = new Array(Array_values[5]);
		}
		obj.validacion = new Array();
		obj.validacion = Array_values[6];
		obj.tipoBusqueda = Array_values[7];
		obj.estado = Array_values[8];

		this.hashtable[key] = obj;
	}
}

function hashtable_replace(key,value){
	this.remove(key);
	this.hashtable[key] = value;
}
function hashtable_remove(key){
	var rtn = this.hashtable[key];
	this.hashtable[key] = null;
	return rtn;
}

function hashtable_size(){
	var size = 0;
	for (var i in this.hashtable) {
		if (this.hashtable[i] != null) 
			size ++;
	}
	return size;
}

function hashtable_toString(){
	var result = "";
	for (var i in this.hashtable)
	{      
		if (this.hashtable[i] != null && i != "in_array") {
			result ="";
			var obj = this.hashtable[i];
			var str = obj.id + "\n\t" + obj.nombreparametro + "\n\t" + obj.tipo + "\n\t" + obj.agrupador + "\n\t" + obj.valor_defecto + "\n\t" + obj.valores.join(";")+"\n";
			result += "{" + i + "},{" + str + "}\n";
			//var tst = /fecha_publicacion/;
			if (str(obj.id) == '80') {
				alert(result);
			}
		}
	}
	return result;
}

function hashtable_values(){
	var values = new Array();
	for (var i in this.hashtable) {
		if (this.hashtable[i] != null && i!='in_array') 
			values.push(this.hashtable[i]);
	}
	return values;
}
function is_array( mixed_var ) {
    return ( mixed_var instanceof Array );
}