/**
 * @Autor: David (davisvil@gmail.com)
 * 2008 08 06
 */
var __LARGO_MAXIMO_CACHE__ = 10;
var __TAMANO_MAXIMO_CACHE__ = 1000000;

function CacheClientClass(){
	this.agregar = cache_agregar;
	this.hash_table = new HashtableNav();
	this.index_items = new Array();
	this.tamano = 0;
	this.se_puede_agregar = cache_se_puede_agregar;
	this.eliminar_mas_viejo = cache_eliminar_mas_viejo;
	this.update_index = cache_update_index;
	this.eliminar_mas_viejo = cache_eliminar_mas_viejo;
	this.existe = cache_existe;
	this.get = cache_get;
	this.existe_item = cache_existe_item;
}

function cache_existe_item(id_item){
	for (var i =0;i<this.index_items.length;i++){
		if(this.index_items[i] == id_item){
			return true;
		}
	}
	return false;
}
function cache_se_puede_agregar(){
	if(this.index_items.length >= __LARGO_MAXIMO_CACHE__ ){
		return false;		
	}
	return true;
}
function cache_agregar(id_item,nombre,valor){
	if (this.existe_item(id_item)){
		var item = this.hash_table.get(id_item);
		var arreglo = new Array(10);
		var nombres = cache_nombres_Array();
		for (var i=0;i<nombres.length; i++){
			if (nombre == nombres[i]) {
				var campo = item[nombres[i]];
				if (campo) {
					this.tamano = this.tamano - campo.length;
				}
			}
			arreglo[cache_posicion_por_nombre(nombres[i])] = item[nombres[i]];
		}
		var posicion = cache_posicion_por_nombre(nombre);
		var largo_valor = valor.length;
		arreglo[posicion] = valor;
		this.hash_table.put(id_item,arreglo);
		this.tamano = this.tamano + largo_valor;
		this.update_index(id_item);
		
	}else if(this.se_puede_agregar()){
		var arreglo = new Array(10);
		var posicion = cache_posicion_por_nombre(nombre);
		var largo_valor = valor.length;
		arreglo[posicion] = valor;
		this.hash_table.put(id_item,arreglo);
		this.update_index(id_item);
		this.tamano = this.tamano + largo_valor;
	}else{
		this.eliminar_mas_viejo(nombre);
		return this.agregar(id_item,nombre,valor);
	}
}

function cache_update_index(id_item){
	var tmp_index = new Array();
	for (var i =0;i<this.index_items.length;i++){
		if(this.index_items[i] == id_item){
			continue;
		}
		tmp_index.push(this.index_items[i]);
	}
	tmp_index.push(id_item);
	this.index_items = tmp_index;	
}

function cache_eliminar_mas_viejo(){
	var id_item = this.index_items[0];
	var tmp_index = new Array();

	this.hash_table.remove(id_item);

	for (var i =0;i<this.index_items.length;i++){
		if(i == 0){
			continue;
		}
		tmp_index.push(this.index_items[i]);
	}
	this.index_items = tmp_index;	
}

function cache_nombres_Array(){
	var nombres = new Array();
	nombres.push("id_norma");
	nombres.push("id_parte");
	nombres.push("version");
	nombres.push("texto_sin_notas");
	nombres.push("texto_con_notas");
	nombres.push("vinculaciones");
	nombres.push("ultima_modificacion");
	nombres.push("datos");
	nombres.push("alertas");
	return nombres;	
}
function cache_posicion_por_nombre(nombre){
	var obj = new Object();
	obj.id_norma = 0;
	obj.id_parte = 1;
	obj.version = 2;
	obj.texto_sin_notas = 3;
	obj.texto_con_notas = 4;
	obj.versiones = 5;
	obj.vinculaciones = 6;
	obj.ultima_modificacion = 7;
	obj.datos = 8;
	obj.alertas = 9;
	try {
		return obj[nombre];
	} 
	catch (e) {
		alert("Este nombre no esta considerado: "+nombre);
		throw(e);
	}
}
function cache_existe(id_item,campo){
	if (this.existe_item(id_item)) {
		var item = this.hash_table.get(id_item);
		var valor_campo = item[campo];
		if (typeof(valor_campo) != 'undefined') {
			return true;
		}
	}
	return false;
}

function cache_get(id_item,nombre){
	var item = this.hash_table.get(id_item);
	try {
		return item[nombre];
	} 
	catch (e) {
		alert("Este nombre no esta considerado: " + nombre)
		throw(e);
	}
}

function creaIdItemCache(idNorma,idParte,idVersion){
	if(idParte == ""){
		idParte = 0;
	}
	return idNorma+"_"+idParte+"_"+idVersion;
}

var CacheClient = new CacheClientClass();

function cachearDivConNotasProcesadas(div, enviarAServer){
	var idNormaC = $F('idNorma');
	var idParteC = $F('idParte_interna');
	var idVersionC = $F('idVersion_interna');
	var id_item = creaIdItemCache(idNormaC, idParteC, idVersionC);
	var nombre = "texto_con_notas";
	var valor = div.innerHTML;
	CacheClient.agregar(id_item, nombre, valor);
	
	if (typeof(enviarAServer) != 'undefined') {
		if (!enviarAServer) {
			return;
		}
	}
	if (!idParteC) {
		idParteC = "0";
	}
	if (esNormaCacheable != false && (idParteC in oc(esNormaCacheable[$F('idNorma')]['idsParte']))) {
		var objTexto = new Object();
		objTexto[idNormaC+"_"+idParteC+"_"+idVersionC] = valor;
		valor = JSON.stringifyLeyChile(objTexto);

		setCacheParte(idNormaC, idParteC, idVersionC, valor, "True");
	}
}

function guardarCacheNormaParte(idObjeto, texto){
	var tmp      	= idObjeto.split("_");
	var idNormaC 	= tmp[0];
	var idParteC 	= tmp[1];
	var idVersionC  = $F('idVersion_interna');
	if((esNormaCacheable != false) && (idParteC in oc(esNormaCacheable[idNormaC]['idsParte']))){
		var objTexto = new Object();
		objTexto[idObjeto] = texto;
		texto = JSON.stringifyLeyChile(objTexto);
		setCacheParte(idNormaC,idParteC,idVersionC,texto);
	}
}

function setCacheParte(idNormaC, idParteC, idVersionC, texto, conNotas){
	if (typeof(conNotas) == 'undefined') {
		conNotas = "False";
		var CNotas = "";
	}
	else {
		var CNotas = "CNotas";
	}
	
	$('textareaCache'+CNotas).value = texto;
	$('idVersionCache'+CNotas).value = idVersionC;
	$('idNormaCache'+CNotas).value = idNormaC;
	$('idParteCache'+CNotas).value = idParteC;
	$('fullCache'+CNotas).value = "True";
	$('esConNotas'+CNotas).value = conNotas;
	
	$("formCache"+CNotas).submit();
}

function verPartesConCache(){
	try {
		var objIdParte     = esNormaCacheable[idNorma];
		var idsParteC      = objIdParte["idsParte"];
		var idsVersionesC  = new Array();
		var idsPartesC     = new Array();
		var indicesPartes  = new Array();
		var chkSel 	   	   = document.getElementsByName('dtreecheck');
	    for (var i = 0; i < chkSel.length; i++) {
			var idParteT = chkSel[i].getAttribute('idpartenorma');
			if (idParteT in oc(idsParteC)) {
				chkSel[i].checked = true;
				idsPartesC.push(idParteT);
				var id   = chkSel[i].id;
				var pos  = id.split("_");
				pos 	 = pos[1];
				indicesPartes.push(pos);
				if(i==0){
					idsVersionesC.push(chkSel[i].getAttribute('fechavigencia'));
				}else{
					idsVersionesC.push(chkSel[i].getAttribute('inivigparte'));
				}
			}
			else {
				chkSel[i].checked = false;
			}
		}
		obtenerEstadoCachePartes(idNorma, idsPartesC, idsVersionesC, indicesPartes);
	}catch(e){
		alert("Esta Norma no tiene partes para Cache\n (quizas sea necesario reinstalar Tipos)");
	}
}

function guardarPartesConCache(){
	esNormaCacheable[idNorma] = new Object();
	var idsParteC   = new Array();
	var fechasVigC  = new Array();
	var chkSel 	   = document.getElementsByName('dtreecheck');
    for (var i = 0; i < chkSel.length; i++) {
		if (chkSel[i].checked) {
			if(i==0){
				var fechavigenciaT = chkSel[i].getAttribute('fechavigencia');
			}else{
				var fechavigenciaT = chkSel[i].getAttribute('inivigparte');
			}
			var idParteT       = chkSel[i].getAttribute('idpartenorma');
			fechasVigC.push(fechavigenciaT);
			idsParteC.push(idParteT);
		}
	}
	esNormaCacheable[idNorma]['idsParte']  = idsParteC;
	esNormaCacheable[idNorma]['idVersion'] = fechasVigC;
	
	var txtJson = JSON.stringifyLeyChile(esNormaCacheable[idNorma]);
	guardarCacheNormaParteAjax(txtJson,idNorma);
}
