/*
 * para navegar las alertas
 */
var _resultado_ajax_js_ = true;
function presentarOpcionesNavegarAlertas(listaOpciones, tipoAlerta, objRef){
	var msg = "Esta norma presenta más de una entrada en vigencia diferida, favor seleccione la que desea navegar";
	if (tipoAlerta == 'REFUNDIDO_POR'){
		msg = "Esta norma ha sido refundida por más de una norma, favor seleccione la que desea navegar";
	}
	var html = '<ul>';
	for(var i=0; i < listaOpciones.length; i++){
		var id =  listaOpciones[i]['idNorma'];
		var parte = listaOpciones[i]['idParte'];
		var fecha = listaOpciones[i]['fechaVigencia'];
		var evento = listaOpciones[i]['evento'];
		var metas = listaOpciones[i]['metas'];
		if (fecha == '2222-02-02'){
			var vigencia = " ("+evento+")";
		}else{
			var vigencia = " (vigente al "+darFormatoFechaTools(fecha,'dd-mm-aaa')+")";
		}
		var strInfo = metas['tipoNormaStrFull'] + " " +	metas['nroNorma'] + " de " + metas['organismo'] + ", publicada el " + metas['fecha_publicacion'] + vigencia;
		var link = 'javascript:NavegarNorma('+id+',\''+parte+'\',\''+fecha+'\')';
		link = '<li><a href="'+link+'">'+strInfo+'</a></li>';
		html += link + "<br />";
	}
	html += '</ul>';
	var div = $('divOpcionesNavegarAlertas');
	if (!div){
		div = document.createElement('div');
		div.setAttribute('id','divOpcionesNavegarAlertas');
		div.setAttribute('class','divNormasGrandes');
		div.innerHTML  = '<div style="width:100%;text-align:right"><a href="javascript:void(0)" onclick="cleanObject(\'divOpcionesNavegarAlertas\')">Cerrar</a></div><div id="divMsgOpcionesNavAlerta"></div><br /><div id="divContenidoOpcionesNavAlerta"></div>';
		document.body.appendChild(div);
	}
	div.style.display = '';
	$('divMsgOpcionesNavAlerta').innerHTML = msg;
	$('divContenidoOpcionesNavAlerta').innerHTML = html;
	SetPositionObjectByOtherObject(div,objRef,0,0);
}
NavegarAlertas = function(idNorma, fechaVigencia, idParte, tipoAlerta, msgDif, objRef) {

	if(tipoAlerta=="TEXTO_ACTUALIZADO"){
		NavegarNorma(idNorma,idParte,'','');
		return;
	}

	if(tipoAlerta=="REFUNDIDO_POR"){
		idParte = '';
	}
	if (typeof(msgDif) == 'undefined'){
		msgDif = '';
	}
	
	this.success = function(req){
		var JSON = req.responseText;
		var res = eval('('+JSON+')');
		if (res.length > 1 && tipoAlerta in {'REFUNDIDO_POR':'','DEROGACION_DIFERIDA':''}){
			presentarOpcionesNavegarAlertas(res,tipoAlerta, objRef);
			return;
		}else{
			res = res[0];
		}
		var fecha = res.fechaVigencia;
		if(res.idNorma && tipoAlerta.indexOf("DIFERIDA") > -1 && fecha){
			if (fecha == '2222-02-02'){
				if (msgDif){
					var msg = 'Usted está accediendo a la versión que entrará en vigencia cuando se cumpla el siguiente evento:\n';
					msgDif = extractEventoDiferido(msgDif);
					alert(msg+msgDif);
				}else{
					alert("Usted está accediendo a una versión que entrará en vigencia cuando se cumpla el evento indicado en la alerta 'Tiene texto diferido'");
				}
			}else{
				alert("Usted está accediendo a la versión que entrará en vigencia el "+darFormatoFechaTools(fecha,'dd-mm-aaa'));	
			}		
		}
		if (!res.idNorma && tipoAlerta == 'FECHA DEROGACION') {
			alert("Norma derogada por fecha");
		}
		else {
			if(tipoAlerta == 'REFUNDIDO_POR'){
				fecha = '';
			}
			NavegarNorma(res.idNorma, res.idParte, fecha);
		}
	};
	
	this.failure = function(responseText, responseXML ){ 
		alert(responseText.responseText);
	};
	
	var params = {
		method:'get',
		onSuccess: this.success,
		onFailure: this.failure,
		parameters: {idNorma: idNorma, fechaVigencia: fechaVigencia, idParte:idParte, tipoAlerta:tipoAlerta}
	};
	this.request = new Ajax.Request(parent.urlServidor + '/../Navegar/scripts/getInfoAlerta', params);
};

descargarNormaListadoRefundido =  function(idNorma, idVersion, idParte, idVersionParte, formato, nombreArchivo, tipoAlerta,nombreRefundida) {
	this.success = function(req){
		var JSON = req.responseText;
		var res = eval('('+JSON+')');
		res  = res[0];
		if (res.idNorma) {
			alert("Descargando la norma "+nombreArchivo+" que refunde a "+nombreRefundida);
			var d = document;
			var radioExportar = d.createElement("input");
			var exportar_formato = d.createElement("input");
			var nombrearchivo = d.createElement("input");
			var exportar_con_notas_bcn = d.createElement("input");
			var exportar_con_notas_originales = d.createElement("input");
			var exportar_con_notas_al_pie = d.createElement("input");
			var hddResultadoExportar = d.createElement("input");
			var f = d.createElement("form");
			
			radioExportar.setAttribute("name", "radioExportar");
			exportar_formato.setAttribute("name", "exportar_formato");
			nombrearchivo.setAttribute("name", "nombrearchivo");
			exportar_con_notas_bcn.setAttribute("name", "exportar_con_notas_bcn");
			exportar_con_notas_originales.setAttribute("name", "exportar_con_notas_originales");
			exportar_con_notas_al_pie.setAttribute("name", "exportar_con_notas_al_pie");
			hddResultadoExportar.setAttribute("name", "hddResultadoExportar");
			
			radioExportar.value = "Normas";
			exportar_formato.value = formato;
			
			nombrearchivo.value = nombreArchivo;
			
			exportar_con_notas_bcn.value = "True";
			exportar_con_notas_originales.value = "True";
			exportar_con_notas_al_pie.value = "True";
			
			hddResultadoExportar.value = res.idNorma + "." +  res.fechaVigencia + ".0.0#";
			if (typeof(urlServidor) == 'undefined') {
				urlServidor = parent.urlServidor;
			}
			f.setAttribute("action", urlServidor + "/../Consulta/Exportar")
			f.setAttribute("method", "get");
			f.style.display = "none";
			d.body.appendChild(f);
			
			f.appendChild(radioExportar);
			f.appendChild(exportar_formato);
			f.appendChild(nombrearchivo);
			f.appendChild(exportar_con_notas_bcn);
			f.appendChild(exportar_con_notas_originales);
			f.appendChild(exportar_con_notas_al_pie);
			f.appendChild(hddResultadoExportar);
			
			/* Se utiliza esta funcion para registrar la traza porque ocupa los mismos parametros*/
			RegistrarNevegarNorma(58,idNorma,idVersion,getCodigoModulo(),0,0,'');
			f.submit();

		}else{
			alert("No se ha encontrado la información de la norma "+nombreArchivo);
		}
	};
	
	this.failure = function(responseText, responseXML ){ 
		alert(responseText.responseText);
	};
	
	var params = {
		method:'get',
		onSuccess: this.success,
		onFailure: this.failure,
		parameters: {idNorma: idNorma, fechaVigencia: idVersion, idParte:idParte, tipoAlerta:tipoAlerta}
	};
	this.request = new Ajax.Request(parent.urlServidor + '/../Navegar/scripts/getInfoAlerta', params);
};

function cargaUrlIn(url,destino, onEnd){
	if (typeof(onEnd) == 'undefined' || !onEnd){
		onEnd = 'void(0);';
	}
	this.success = function(req){
		$(destino).style.display = "";
		$(destino).innerHTML = req.responseText;
		eval(onEnd);
	};
	this.failure = function(responseText, responseXML ){ 
		$(destino).style.display = "none";
	};
	var params = {
		method:'get',
		onSuccess: this.success,
		onFailure: this.failure,
		parameters: {}
	};
	this.request = new Ajax.Request(url, params);
};

function seleccionaBrowserCache(){
	var ids = {
		"ie": "MSIE",
		"ff": "Firefox",
		"ch": "Chrome",
		"sf": "Safari",
		"op": "Opera"
	};
	var nameN = navigator.userAgent;
	for(var key in ids){
		if (nameN.indexOf(ids[key]) > -1) {
			$(key).style.border="1px dotted";
			break;
		}
	}
}
function registraConsultaSimple(){
	var cadena = $F('cadena1');
	var exacta = $F('exacta');
	var totRes = $F('totalitems');
	var d = new Date();
	var r = Math.floor(Math.random()*d.getTime());
	this.success = function(req){
		//pass
	};
	this.failure = function(responseText, responseXML ){ 
		//pass
	};
	var params = {
		method:'get',
		onSuccess: this.success,
		onFailure: this.failure,
		parameters: {cadena: cadena, exacta: exacta, totRes:totRes, r:r}
	};
	var url = 'script/registroConsultaSimple';
	this.request = new Ajax.Request(url, params);	
}
