////////////////////////
// Variables Globales //
////////////////////////

var ancho = screen.availWidth - 15;
var alto = screen.availHeight - 65;


/////////////////////////////
// Funciones para Imágenes //
/////////////////////////////

function ampliarImagen(sourceImg, ancho, alto) {
	code='<html><head><link href="../css/HojaDeEstilos.css" type="text/css" rel="stylesheet"><title>MAC - Escuela de Conducción</title></head><body scroll="no" bgcolor="white" topmargin="0" leftmargin="0" marginwidth="0" marginheight="0"><table width=100% height=100% cellspacing=0 cellpadding=0 border=0><tr><td align=center valign=middle>'
	code=code+"<a href='javascript:window.close();' onFocus='if(this.blur)this.blur()'><img src='" + sourceImg + "' border=0 alt='[ C E R R A R ]' title='[ C E R R A R ]'></a></td></tr></table></body></html>"
	dimensiones = 'width=' + ancho + ',height=' + alto + ',resizable=1,titlebar=1,left=' + (790-ancho)/2 + ',top=' + (570-alto)/2;
	info=window.open("","info",dimensiones);
	info.document.open();
	info.document.write(code);
	info.document.close();
}


///////////////////////////
// Funciones para Listar //
///////////////////////////

function encima(obj) {
	if(obj.style.backgroundColor != '#ffcc00') {
		obj.style.backgroundColor = '#E0E0D0';
	}  
}

function fuera(obj, color_default) {
	if(obj.style.backgroundColor != '#ffcc00') {
		obj.style.backgroundColor = color_default;
	}
}

function encima2(id) {
	obj = eval("document.all.tr_" + id);
	obj2 = eval("document.all.tr2_" + id);
	if(obj.style.backgroundColor != '#ffcc00') {
		obj.style.backgroundColor = '#E0E0D0';
		obj2.style.backgroundColor = '#E0E0D0';
	}  
}

function fuera2(id, color_default) {
	obj = eval("document.all.tr_" + id);
	obj2 = eval("document.all.tr2_" + id);
	if(obj.style.backgroundColor != '#ffcc00') {
		obj.style.backgroundColor = color_default;
		obj2.style.backgroundColor = color_default;
	}
}
		
		
/////////////////////////////////////
// Funciones Para Cadenas (String) //
/////////////////////////////////////

function LTrim(s) {
	// Devuelve una cadena sin los espacios del principio
	var i=0, j=0;

	// Busca el primer caracter <> de un espacio
	for(i=0; i<=s.length-1; i++) if(s.substring(i,i+1) != ' ') { j=i; break; }
	
	return s.substring(j, s.length);
}

function RTrim(s) {
	// Quita los espacios en blanco del final de la cadena
	var j=0;

	// Busca el último caracter <> de un espacio
	for(var i=s.length-1; i>-1; i--) if(s.substring(i,i+1) != ' ') { j=i; break; }

	return s.substring(0, j+1);
}

function Trim(s) {
	// Quita los espacios del principio y del final
	if (LTrim(RTrim(s)) == ' ') {
		return '';
	} else {
		return LTrim(RTrim(s));
	}
}


///////////////////////////////////////////
// Función para poner la Fecha y la Hora //
///////////////////////////////////////////

function ponFecha(){
	dias  = new Array ("Domingo","Lunes","Martes","Miércoles","Jueves","Viernes","Sábado");
	meses = new Array ("Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre");
	
	fecha = new Date();
	indiceDia = fecha.getDay();
	indiceMes = fecha.getMonth();
	
	fechaActual = dias[indiceDia] + ", " + fecha.getDate() + " de " + meses[indiceMes] + " de " + fecha.getUTCFullYear();
	document.write(fechaActual);
}


////////////////////////
// Funciones del MENÚ //
////////////////////////

function eliminarFicheros(id) {
	var alto = 400;
	var ancho = 640;
	var arriba = ((screen.availHeight - alto) / 2) - 30;
	var izquierda = ((screen.availWidth - ancho) / 2) - 6;
	
	window.open("eliminar_ficheros.php", "", "resizable=1,titlebar=1,status=1,height=" + alto + ",width=" + ancho + ",top=" + arriba + ",left=" + izquierda);
}

function verMac90() {
	var alto = 440;
	var ancho = 600;
	var arriba = ((screen.availHeight - alto) / 2) - 30;
	var izquierda = ((screen.availWidth - ancho) / 2) - 6;
	
	window.open("mac90.php", "", "resizable=yes,titlebar=1,status=1,height=" + alto + ",width=" + ancho + ",top=" + arriba + ",left=" + izquierda);
}

function verNoticia(id) {
	var alto = 400;
	var ancho = 640;
	var arriba = ((screen.availHeight - alto) / 2) - 30;
	var izquierda = ((screen.availWidth - ancho) / 2) - 6;
	
	window.open("ver_noticia.php?id=" + id, "", "resizable=1,titlebar=1,status=1,height=" + alto + ",width=" + ancho + ",top=" + arriba + ",left=" + izquierda);
}

function verEspecificaciones() {
	var alto = 350;
	var ancho = 585;
	var arriba = ((screen.availHeight - alto) / 2) - 30;
	var izquierda = ((screen.availWidth - ancho) / 2) - 6;
	
	window.open("especificaciones.html", "", "resizable=1,titlebar=1,status=1,height=" + alto + ",width=" + ancho + ",top=" + arriba + ",left=" + izquierda);
}

function verCircuito(circuito) {
	var alto = 400;
	var ancho = 640;
	var arriba = ((screen.availHeight - alto) / 2) - 30;
	var izquierda = ((screen.availWidth - ancho) / 2) - 6;
	
	window.open("circuito_" + circuito + ".html", "", "resizable=1,titlebar=1,status=1,height=" + alto + ",width=" + ancho + ",top=" + arriba + ",left=" + izquierda);
}

function verNormas() {
	var alto = 450;
	var ancho = 660;
	var arriba = ((screen.availHeight - alto) / 2) - 30;
	var izquierda = ((screen.availWidth - ancho) / 2) - 6;
	
	window.open("normas_mac.html", "", "resizable=1,titlebar=1,status=1,height=" + alto + ",width=" + ancho + ",top=" + arriba + ",left=" + izquierda);
}

function verCurso(id) {
	var alto = 400;
	var ancho = 640;
	var arriba = ((screen.availHeight - alto) / 2) - 30;
	var izquierda = ((screen.availWidth - ancho) / 2) - 6;
	
	window.open("ver_curso.php?id=" + id, "", "resizable=1,titlebar=1,status=1,height=" + alto + ",width=" + ancho + ",top=" + arriba + ",left=" + izquierda);
}

function verVideos() {
	var alto = 475;
	var ancho = 790;
	var arriba = ((screen.availHeight - alto) / 2) - 30;
	var izquierda = ((screen.availWidth - ancho) / 2) - 6;
	
	window.open("videos.html", "", "resizable=1,titlebar=1,status=1,height=" + alto + ",width=" + ancho + ",top=" + arriba + ",left=" + izquierda);
}


/////////////////////////////
// Funciones de formulario //
/////////////////////////////

function formatDate(obj) {
	var fecha = obj.value;
	
	if (fecha.length == 2) {
		obj.value = obj.value + "/";
	} else if (fecha.length == 5) {
		obj.value = obj.value + "/";
	}
}

function aMayuscula(e) {
	var charCode;
	var letras=new RegExp("[abcdefghijklmnñopqrstuvwxyz]");
			
	if (navigator.appName == "Netscape") // me fijo 
		charCode = e.which // leo la tecla en ASCII que ingreso
	else
		charCode = e.keyCode // leo la tecla en ASCII que ingreso
	
	if ( letras.test(String.fromCharCode(charCode)) )
	{
		if (navigator.appName == "Netscape") // me fijo 
			window.event.which = window.event.which - 32; //Paso a Mayúscula
		else
			window.event.keyCode = window.event.keyCode - 32; //Paso a Mayúscula
	}
	return true;
}

function soloNumeros(e) {
	var charCode;
	var letras=new RegExp("[0123456789/]");
			
	if (navigator.appName == "Netscape") // me fijo 
		charCode = e.which // leo la tecla en ASCII que ingreso
	else
		charCode = e.keyCode // leo la tecla en ASCII que ingreso
	
	if ( !letras.test(String.fromCharCode(charCode)) )
	{
		if (navigator.appName == "Netscape") // me fijo 
			window.event.which = 0;
		else
			window.event.keyCode = 0;
	}
	return true;
}


////////////////////////////
// Funciones para números //
////////////////////////////

function quitarPuntoMiles(numero) {
	var nuevoNumero = numero.toString();

	while (nuevoNumero.indexOf(".") != -1) {
		nuevoNumero = nuevoNumero.replace(".", "");
	}

	return nuevoNumero;
}

function cambiarPuntoPorComa(obj) {
	obj.value = (obj.value).replace(".", ",");
	
	return obj.value;
}

function devuelveConPunto(numero) {	
	var nuevoNumero = (numero.toString()).replace(",", ".");
	
	return nuevoNumero;
}

function establecerDecimales(value, precision, modo) {
	var comprobarCasosCeros = false;
	
	value = (devuelveConPunto(value.toString()).toString()); //convert value to string
	precision = parseInt(precision, 10);
	
	if (value.substring(0, 2) == "0.") {
		comprobarCasosCeros = true;
	}

	var whole = "" + Math.round(value * Math.pow(10, precision));
	var decPoint = whole.length - precision;

	if (decPoint != 0) {
			result = whole.substring(0, decPoint);
			result += ".";
			result += whole.substring(decPoint, whole.length);
	} else {
			result = whole;
	}
	
	if (result == ".0") {
			result = "0.00"
	}
	if (comprobarCasosCeros == true) {
		if (result.substring(0, 1) == ".") {
			result = "0.0" + result.substring(1, 2);
		} else if (result.toString().length == 2) {
			result = "0." + result;
		}
	}
	
	result = result.replace(".", ",");
	
	if (modo == "C" || modo == "c") { // Modo consulta, hay que separar los miles
		var result_SinDecimales = result.substr(0, result.indexOf(","));
		var resultado_Final = result.substr(result.indexOf(","), result.length); // Decimales con coma
		var contador = 1;

		for (i=(result_SinDecimales.length-1); i>0; i--) {
			if (contador == 3) {
				resultado_Final = "." + result_SinDecimales.charAt(i) + resultado_Final;
				contador = 1;
			} else {
				resultado_Final = result_SinDecimales.charAt(i) + resultado_Final;
				contador++;
			}
		}
		resultado_Final = result_SinDecimales.charAt(0) + resultado_Final;
		
		result = resultado_Final;
	}
	
	return result;
}

function esDecimalValido(numero) { // Campo Decimal
	numero = devuelveConPunto(numero);
	
	if ( ((parseFloat(numero)).toString() != (numero).toString()) && (((parseFloat(numero)).toString() + "0") != (numero).toString()) && (((parseFloat(numero)).toString() + ".00") != (numero).toString()) && (((parseFloat(numero)).toString() + ".0") != (numero).toString()) ) {
		return false;
	}
	
	return true;
}

function esEnteroValido(numero) { // Campo Entero
	if ( (parseInt(numero,10)).toString() != (numero).toString() ) {
		return false;
	}
	
	return true;
}

function esEmailValido(cadena) {
	//expresion regular
	var b=/^[^@\s]+@[^@\.\s]+(\.[^@\.\s]+)+$/
	
	//devuelve verdadero si validacion OK, y falso en caso contrario
	return b.test(cadena);
}