function ck_bloque(id, etiqueta, name) {
	var clase = document.getElementById(id).className;
	document.getElementById(id).className = (clase == 'bloque_oculto') ? 'bloque_visible'
			: 'bloque_oculto';
	if (document.getElementById(etiqueta))
		document.getElementById(etiqueta).innerHTML = (clase == 'bloque_oculto') ? '<img src="/img/close.png" border="0">'
				: '<img src="/img/open.png" border="0">';

	return;
}

function ck_bloqueAvanzado(id, etiqueta, estado, name) {
	var clase = document.getElementById(id).className;
	document.getElementById(id).className = (clase == 'bloque_oculto') ? 'bloque_visible'
			: 'bloque_oculto';
	if (document.getElementById(etiqueta))
		document.getElementById(etiqueta).innerHTML = (clase == 'bloque_oculto') ? '<input type="image" src="/img/minus.gif" id="'
				+ estado
				+ '" onclick="cambia_bloque(\''
				+ id
				+ '\',this.value,\'' + name + '\')"/>'
				: '<input type="image" src="/img/add.gif" id="' + estado
						+ '" onclick="cambia_bloque(\'' + id
						+ '\',this.value,\'' + name + '\')"/>';
	if (document.getElementById(estado))
		document.getElementById(estado).value = (clase == 'bloque_oculto') ? ''
				: 'checked';
	return;
}

function contador(id, id_c, max) {
	var obj = document.getElementById(id);
	var len = obj.value.length;
	if (len >= max) {
		obj.value = obj.value.substring(0, max);
		document.getElementById(id_c).value = max - obj.value.length;
		return false;
	}
	document.getElementById(id_c).value = max - len;
}

function muestra_bloque(id, etiqueta) {
	document.getElementById(id).className = 'bloque_visible';
	if (document.getElementById(etiqueta))
		document.getElementById(etiqueta).innerHTML = '-';

	return;
}

function oculta_bloque(id, etiqueta) {
	document.getElementById(id).className = 'bloque_oculto';
	if (document.getElementById(etiqueta))
		document.getElementById(etiqueta).innerHTML = '+';

	return;
}

function muestraOcultaFila(idFila) {
	var fila = document.getElementById(idFila);

	if (!fila)
		return;

	if (fila.style.display == '')
		fila.style.display = 'none';
	else
		fila.style.display = '';
}

function redirect_popup(action, variables, w, h, msg, color) {
	var estilos = '';
	estilos += 'body{ margin-left:20px; margin-right:20px; background-color:'
			+ color + '; padding:0px; }';
	estilos += 'p, div, td{ font-family:Verdana; font-size:12px; color:#000; }';

	var win_ventana = window.open('', '', 'width=' + w + ', height=' + h
			+ ', resizable=yes, top=20, left=20, scrollbars=yes');
	var doc = win_ventana.document;
	doc.writeln('<html>');
	doc.writeln('<head>');
	doc.writeln('<style type="text/css">' + estilos + '</style>');
	doc.writeln('<body onload="document.forms[0].submit()" >');
	doc.writeln('<body>');
	doc.writeln('<p>' + msg
			+ '&nbsp;<img src="/images/preload.gif" align="absmiddle" /></p>');
	doc.writeln('<form name="fData" id="fData" action="' + action
			+ '" method="post">');
	if (variables) {
		for ( var key in variables) {
			doc.writeln('<input type="hidden" name="' + key + '" value="'
					+ variables[key] + '">');
		}
	}
	doc.writeln('</form>');
	doc.writeln('</body>');
	doc.writeln('</html>');
	doc.close();
}

// Dreamweaver
function MM_preloadimages() { // v3.0
	var d = document;
	if (d.images) {
		if (!d.MM_p)
			d.MM_p = new Array();
		var i, j = d.MM_p.length, a = MM_preloadimages.arguments;
		for (i = 0; i < a.length; i++)
			if (a[i].indexOf("#") != 0) {
				d.MM_p[j] = new Image;
				d.MM_p[j++].src = a[i];
			}
	}
}

function MM_swapImgRestore() { // v3.0
	var i, x, a = document.MM_sr;
	for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++)
		x.src = x.oSrc;
}

function MM_findObj(n, d) { // v4.01
	var p, i, x;
	if (!d)
		d = document;
	if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
		d = parent.frames[n.substring(p + 1)].document;
		n = n.substring(0, p);
	}
	if (!(x = d[n]) && d.all)
		x = d.all[n];
	for (i = 0; !x && i < d.forms.length; i++)
		x = d.forms[i][n];
	for (i = 0; !x && d.layers && i < d.layers.length; i++)
		x = MM_findObj(n, d.layers[i].document);
	if (!x && d.getElementById)
		x = d.getElementById(n);
	return x;
}

function MM_swapImage() { // v3.0
	var i, j = 0, x, a = MM_swapImage.arguments;
	document.MM_sr = new Array;
	for (i = 0; i < (a.length - 2); i += 3)
		if ((x = MM_findObj(a[i])) != null) {
			document.MM_sr[j++] = x;
			if (!x.oSrc)
				x.oSrc = x.src;
			x.src = a[i + 2];
		}
}

function MM_openBrWindow(theURL, winName, features) { // v2.0
	window.open(theURL, winName, features);
}

function borrarElemento(id) {
	/*
	 * document.getElementById(id).style.visibility = 'hidden';
	 * document.getElementById(id).style.display = 'none';
	 */
	// document.getElementById(id + '[borrar]').value = 1;

	// Obtenemos el elemento
	var elem = document.getElementById(id);
	// Obtenemos el padre de dicho elemento
	var padre = elem.parentNode;
	// Eliminamos el hijo (el) del elemento padre
	padre.removeChild(elem);
}

function refreshCaptcha(idElem) {
	if (!idElem)
		return false;

	var newDate = new Date;
	var source = "/captcha.html?" + newDate.getTime();

	document.getElementById(idElem).src = source;
}

// Función que convierte grados a radianes
Number.prototype.toRad = function() {
	return this * Math.PI / 180;
}

// Función que devuelve la distancia en kilómetros
// entre dos puntos dados por sus coordenadas
function getDistance(lat1, lon1, lat2, lon2) {
	var R = 6371; // km
	var dLat = (lat2 - lat1).toRad();
	var dLon = (lon2 - lon1).toRad();
	var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(lat1.toRad())
			* Math.cos(lat2.toRad()) * Math.sin(dLon / 2) * Math.sin(dLon / 2);
	var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
	var d = R * c;

	return d;
}

// tipo: checkbox, input, radio, ...
// evento: click, change, ... (sin el prefijo on)
function setEventoFormularioElemento(formid, tipo, evento, funcion) {
	var f = document.getElementById(formid);
	var num = f.elements.length;
	for ( var i = 0; i <= num - 1; i++) {
		el=f.elements[i];
		if(el.type==tipo){
			if (el.addEventListener) el.addEventListener(evento, funcion, false); 
			else if (el.attachEvent) el.attachEvent("on"+evento, funcion);
		}
	}
	return;
}

function fechasValidas(f_entrada, n_entrada, f_salida, n_salida) {
	re = /^[0-9][0-9]\-[0-9][0-9]\-[0-9][0-9][0-9][0-9]$/
	if ((f_entrada != undefined && f_entrada.length != 0)
			|| (f_salida != undefined && f_salida.length != 0)) {
		if (f_entrada.length == 0) {
			alert('Debe especificar fecha de entrada');
			return false;
		}
		if (f_salida.length == 0) {
			alert('Debe especificar fecha de salida');
			return false;
		}

		if (!re.exec(f_entrada) || !re.exec(f_salida)) {
			alert("La fecha no tiene formato correcto dd-mm-aaaa");
			return false;
		}

		frag_entrada = f_entrada.split('-');
		frag_salida  = f_salida.split('-');
		
		if(frag_salida[2]+"-"+frag_salida[1]+"-"+frag_salida[0] <= frag_entrada[2]+"-"+frag_entrada[1]+"-"+frag_entrada[0])
		{
			alert('La fecha de salida debe ser posterior a la fecha de entrada');
			return false;
		}
		
		var today = (new Date()); 
		if ((frag_entrada[2]+"-"+frag_entrada[1]+"-"+frag_entrada[0] < (today.getFullYear()+"-"+addCero(parseInt(today.getMonth()+1)) +"-"+addCero(today.getDate()))) ||
		   (frag_salida[2]+"-"+frag_salida[1]+"-"+frag_salida[0]  < (today.getFullYear()+"-"+addCero(parseInt(today.getMonth()+1)) +"-"+addCero(today.getDate())))) 
		{
			alert('Las fechas no pueden ser anteriores al día de hoy'); return false; 
		}
	}
	return '&' + n_entrada + '=' + f_entrada
			+ '&' + n_salida + '=' + f_salida;
}

function addCero(num)
{	
	num = num.toString();
	if(num.length == 1) return '0'+num;
	return num;
}

function _getSeccion() {
	var path = document.location.pathname;
	var seccion = path.replace(".html", "");
	seccion = seccion.replace("/", "");

	return seccion ? seccion : "inicio";
}

// Funciones para validar ccc por javascript || Alberto 15/11/2010

function obtenerDigito(valor) {
	valores = new Array(1, 2, 4, 8, 5, 10, 9, 7, 3, 6);

	control = 0;

	for (i = 0; i <= 9; i++)
		control += parseInt(valor.charAt(i)) * valores[i];

	control = 11 - (control % 11);

	if (control == 11)
		control = 0;
	else if (control == 10)
		control = 1;

	return control;
}

function numerico(valor) {
	cad = valor.toString();

	for ( var i = 0; i < cad.length; i++) {
		var caracter = cad.charAt(i);
		if (caracter < "0" || caracter > "9")
			return false;
	}

	return true;
}

function validar_ccc(ccc) {

	// var ccc = document.getElementById('ccc');
	var ccc_desglosado = ccc.split("-");
	// var mensaje = ": La cuenta corriente no es correcta";

	if (ccc.length != 23) {
		// alert("[1]"+mensaje);
		return false;
	}
	if (!numerico(ccc_desglosado[0]) || !numerico(ccc_desglosado[1])
			|| !numerico(ccc_desglosado[2]) || !numerico(ccc_desglosado[3])) {
		// alert("[2]"+mensaje);
		return false;
	}
	if (!(obtenerDigito("00" + ccc_desglosado[0] + ccc_desglosado[1]) == parseInt(ccc_desglosado[2]
			.charAt(0)))
			|| !(obtenerDigito(ccc_desglosado[3]) == parseInt(ccc_desglosado[2]
					.charAt(1)))) {
		// alert("[3]"+mensaje);
		return false;
	}
	return true;
}

// FIN::Funciones para validar ccc por javascript

// Valida que el numero sea entero, decimal con punto o decimal con coma.
function validarIntegerFloat(numero)
{
	if ((/^([0-9])*[.]?[0-9]*$/.test(numero)) || (/^([0-9])*$/.test(numero)) ) return true;
	
	alert("El valor " + numero + " no es un número");
	return false;
}

