// JavaScript Document
/* FUNCTION eliminarfilas() elimina todas las filas seccionadas de un listado*/
function mostrarElementoOculto(e,element) {
	
	  // step 1
	  var tempX = 0;
	  var tempY = 0;
	  var offset = 5;
	
	  // step 2
	  var obj = element;
	  if (obj==null) return;
	
	  // step 3
	  if (document.all) {
		tempX = event.clientX + document.body.scrollLeft;
		tempY = event.clientY ;
		
	  } else {
		tempX = e.pageX;
		tempY = e.pageY;
	  }
	  
	
	  // step 4
	  if (tempX < 0){tempX = 0}
	  if (tempY < 0){tempY = 0}
	
	  // step 5
	  obj.style.top  = (tempY + offset) + 'px';
	  obj.style.left = (tempX + 150) + 'px';
	
	  // step 6
		obj.style.display = 'block';

}		


function mostrarElementoOculto2(e,element) {
	
	  // step 1
	  var tempX = 0;
	  var tempY = 0;
	  var offset = 5;
	
	  // step 2
	  var obj = element;
	  if (obj==null) return;
	
	  // step 3
	  if (window.ActiveXObject) {

		tempX = event.clientX + document.documentElement.scrollLeft;
		tempY = event.clientY + document.documentElement.scrollTop ;
	  } else {
		tempX = e.pageX;
		tempY = e.pageY;
	  }
	  
//		alert(tempX)
//		alert(tempY)
	  // step 4
	  if (tempX < 0){tempX = 0}
	  if (tempY < 0){tempY = 0}
	  
	  // step 5
	  obj.style.top  = (tempY + offset) + 'px';
	  obj.style.left = (tempX + 25) + 'px';
	
	  // step 6
	  obj.style.display = 'block';
	 
	 
}	

	function eliminarfilas(){
		
		var camposSeleccionados = '';
		var activarAlert = 0;
		
			for(i=1;i<=numFilas;i++){
				if(document.getElementById('check'+ i).checked == true){
					camposSeleccionados += ', '+i;
					activarAlert++;
				}
			}
			if(activarAlert>0){
				var mensaje = '';
				
				if(activarAlert == 1)
					mensaje = '¿Está segudo de ELIMINAR el campo'+camposSeleccionados+'?';
				else
					mensaje = '¿Está segudo de ELIMINAR los campos'+camposSeleccionados+'?';
				
				document.getElementById('capaoscura').style.display = 'block';
				eliminar = confirm(mensaje);
				document.getElementById('capaoscura').style.display = 'none';
				if(eliminar==false){
					
					return false
				}else{
					document.getElementById('accion').value = 'eliminar';
					return document.getElementById('formRegistros').submit();
				}
			}
		return false;
	}





/* FUNCTION irPagina(url, pagina) navegar por las paginas con javascript **************************************************/
function irPagina(url, pagina){
	
	pagina = document.getElementById(pagina).value;
	location.href = url+"&pagina="+pagina;
	
}

function popUp(src,w,h) {
	window.open(src,"SmallWin","top=10,left=5,toolbar=no,directories=no,status=no, resizable=yes, scrollbars=yes, menubar=no,width=" + w + ",height=" + h);
}

function xPopUp(src,w,h){
	return window.open(src,"SmallWin","top=100,left=100,toolbar=no,directories=no,status=no, resizable=yes, scrollbars=yes, menubar=no,width=" + w + ",height=" + h);
}


function hoverTR(IDtr, color){
	document.getElementById(IDtr).style.background = color;
}

function clickTR_onCheck(IDchk){
	check = document.getElementById(IDchk);
	if(check.checked == false){
		check.checked = true;
	}else{
		check.checked = false;
	}
}

function locationPage(url){
	location.href = url;
}


/**************************************************************************/
function ltrim(s) { 
    return s.replace(/^\s+/, ""); 
} 
 
function rtrim(s) { 
    return s.replace(/\s+$/, ""); 
} 
 
function trim(s) { 
    return rtrim(ltrim(s)); 
}



/// FUNCTION BLOCKNONEID

function id_noneblock(id){
		if(document.getElementById(id)){
			e = document.getElementById(id);
			if(e.style.display == 'none')
				e.style.display = 'block';
			else if(e.style.display == 'block')
				e.style.display = 'none';
		}	
}

function id_clear(id){
	if(document.getElementById(id)){
		e = document.getElementById(id);
		e.innerHTML = '';
		e.style.display = 'none';
	}		
}

function mostrarMsjHelp(e,element,msj){
	mostrarElementoOculto2(e,element)
	element.innerHTML = msj;
}	
	

function in_array( what, where ){
	var a=false;
	for(var i=0;i<where.length;i++){
		if(what == where[i]){
			a=true;
			break;
		}
	}
	return a;
}


	function fnsActivarsolapa(e,val){
		var nS = arrSolapas.length;
		
		for(var i = 0; i < nS; i++){
			document.getElementById('div_'+ arrSolapas[i]).style.display = 'none';
		}

		document.getElementById('solapa_izq_on').id = 'solapa_izq_off';
		e.id = 'solapa_izq_on'
		document.getElementById('div_'+ val).style.display = 'block';
//		document.getElementById('hrNombreSolapa').innerHTML = e.innerHTML;
		
		locationPage('#'+ val);
	}	

function accionLink(){
		var url = (document.location.href);
		partes = url.split('#');
		if(partes.length > 1)
			return (partes[1]);
		else
			return '';
	}

function getElementsByName_iefix(tag, name) {
		 
		 var elem = document.getElementsByTagName(tag);
		 var arr = new Array();
		 for(i = 0,iarr = 0; i < elem.length; i++) {
			  att = elem[i].getAttribute("name");
			  if(att == name) {
				   arr[iarr] = elem[i];
				   iarr++;
			  }
		 }
		 return arr;
	}
	
	
	function listen(event, elem, func) {
		//elem = $(elem);
		if (elem.addEventListener){  // W3C DOM
			elem.addEventListener(event,func,false);
		}else if (elem.attachEvent) { // IE DOM
			var r = elem.attachEvent("on"+event, func);
			return r;
		}else throw 'No es posible añadir evento';
	}
	
	
	
	function mostrarMsjHelp(e,element,msj){
		mostrarElementoOculto2(e,element)
		element.innerHTML = msj;
		
		//setTimeout("document.getElementById('msjHelp').style.display = 'none'",3000);
		//setInterval id_noneblock('msjHelp')
	}	
	function verImagenGrande(e,img_replace){
		id_clear('msjHelp');
	//	document.getElementById('test').innerHTML = document.getElementById('test').innerHTML + img_replace + '==';
		if(img_replace !=''){	
			var imagenGrande = '<img src="../'+ img_replace +'"  />';//alert(imagenGrande);
			document.getElementById('msjHelp').innerHTML = imagenGrande;
			mostrarElementoOculto2(e,document.getElementById('msjHelp'));
			
			//document.getElementById('test').innerHTML = document.getElementById('test').innerHTML + img + '==';
		}//else alert('NADA');
	}
	
	
	var txtIdiomaActual = new Array();
	function cambiarIdiomaText(lang,id){
		var arrActual = getElementsByName_iefix('div', 'text'+txtIdiomaActual[id]+'_'+id);
		
		for(var i=0;i<arrActual.length;i++){
			arrActual[i].style.display = 'none';
		}
		
		var arrChange = getElementsByName_iefix('div', 'text'+lang+'_'+id);
		for(var i=0;i<arrChange.length;i++){
			arrChange[i].style.display = 'block';
		}
		
		txtIdiomaActual[id] = lang;
	}
	
	function byId(id){
		return document.getElementById(id);	
	}
	
	
	function activarDrop(idDD,grupo,destruir){  
		var list = document.getElementById(idDD);
		
		DragDrop.makeListContainer( list, grupo,true);
		DragDrop.makeListContainer( list, grupo,destruir);
		list.onDragOver = function() { };
		list.onDragOut = function() { };
	};

    function getOrden(grupo){
		return DragDrop.serData(grupo, null);
	}
	function getSort(group){
		var order = document.getElementById("order");
		order.value = DragDrop.serData(group, null);
	}
	
	function showValue(){
		order = document.getElementById("order");
		alert(order.value);
	}
	
	
	function mostrarAlt(e,id){
		id_noneblock('msjHelp'); mostrarMsjHelp(e,document.getElementById('msjHelp'),document.getElementById(id).innerHTML);
	}
	
	
	
	function mostrarBanner(id,lang){
		if(receiveReq.readyState == 4 || receiveReq.readyState == 0){
			var myRand = parseInt(Math.random()*999999999999999);
			var url = 'verbanner.php';
			
			receiveReq.open("GET", url +"?id="+id+"&l="+lang+"&rand="+myRand, true);
			receiveReq.onreadystatechange = function() {
				if (receiveReq.readyState == 4) {
					var req = receiveReq.responseText;
					document.getElementById('verban').innerHTML = req
				}else{
					document.getElementById('verban').innerHTML = '<img src="img/load.gif" />';				
				}
			};		
			receiveReq.send(null);
		}
	}
	
	function activarSolapa(element,onId){
		var n = sContenidoId.length;
		
		for(var i = 0; i < n; i++){
			document.getElementById(sContenidoId[i]).style.display = 'none';
		}
		document.getElementById('current').id = '';
		element.id = 'current';
		document.getElementById(onId).style.display = 'block';
	
	}
	
	function reemplazarCharURL(str){
		if(str.length > 0){
			str = str.replace(/#/gi,'**numeral**');	
			str = str.replace(/&/gi,'**and**');	
		}
		return str;
			
	}
	
	var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
 
function encode64(input) {
	var output = new StringMaker();
	var chr1, chr2, chr3;
	var enc1, enc2, enc3, enc4;
	var i = 0;
 
	while (i < input.length) {
		chr1 = input.charCodeAt(i++);
		chr2 = input.charCodeAt(i++);
		chr3 = input.charCodeAt(i++);
 
		enc1 = chr1 >> 2;
		enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
		enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
		enc4 = chr3 & 63;
 
		if (isNaN(chr2)) {
			enc3 = enc4 = 64;
		} else if (isNaN(chr3)) {
			enc4 = 64;
		}
 
		output.append(keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) + keyStr.charAt(enc4));
   }
   
   return output.toString();
}
 
function decode64(input) {
	var output = new StringMaker();
	var chr1, chr2, chr3;
	var enc1, enc2, enc3, enc4;
	var i = 0;
 
	// remove all characters that are not A-Z, a-z, 0-9, +, /, or =
	input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
 
	while (i < input.length) {
		enc1 = keyStr.indexOf(input.charAt(i++));
		enc2 = keyStr.indexOf(input.charAt(i++));
		enc3 = keyStr.indexOf(input.charAt(i++));
		enc4 = keyStr.indexOf(input.charAt(i++));
 
		chr1 = (enc1 << 2) | (enc2 >> 4);
		chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
		chr3 = ((enc3 & 3) << 6) | enc4;
 
		output.append(String.fromCharCode(chr1));
 
		if (enc3 != 64) {
			output.append(String.fromCharCode(chr2));
		}
		if (enc4 != 64) {
			output.append(String.fromCharCode(chr3));
		}
	}
 
	return output.toString();
}
	
	
		
	function insertarIcoGooglemap(id){
		var code = prompt('Inserte el codigo google map:');
		if(code!=''){
			var ico = '<a href="ver_mapa.php?code=' + encodeURI(encode64(code)) + '" rel="gb_page[600, 540]"><img src="../img/ver_mapa.gif" border="0" ></a>';			
			$('#'+ id).tinymce().execCommand('mceInsertContent',false,ico);
		}
	}
	
	
	var ua = navigator.userAgent.toLowerCase();
if (ua.indexOf(" chrome/") >= 0 || ua.indexOf(" firefox/") >= 0 || ua.indexOf(' gecko/') >= 0) {
	var StringMaker = function () {
		this.str = "";
		this.length = 0;
		this.append = function (s) {
			this.str += s;
			this.length += s.length;
		}
		this.prepend = function (s) {
			this.str = s + this.str;
			this.length += s.length;
		}
		this.toString = function () {
			return this.str;
		}
	}
} else {
	var StringMaker = function () {
		this.parts = [];
		this.length = 0;
		this.append = function (s) {
			this.parts.push(s);
			this.length += s.length;
		}
		this.prepend = function (s) {
			this.parts.unshift(s);
			this.length += s.length;
		}
		this.toString = function () {
			return this.parts.join('');
		}
	}
}