function MM_changeProp(objName,theProp,theValue) { //v3.0
  var obj = MM_findObj(objName);
  if (obj && (theProp.indexOf("style.")==-1 || obj.style)) eval("obj."+theProp+"='"+theValue+"'");
}

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;
}

// Cambia la visibilidad de una capa
function estadoCapa(capa, estado)
{
    MM_changeProp(capa, "style.visibility", estado);
	//var Netscape = (navigator.appName == "Netscape")?true:false;
	//(Netscape)?eval("document."+capa+".visibility='" +estado+"'"):eval(capa+".style.visibility='"+estado+"'");
}

// Devuelve el texto mostrado del elemento seleccionado de un campo select (no debe ser multiselect)
function getTextoSelect(ctrl)
{
	for (i=0; i<ctrl.options.length; i++)
	{
		if (ctrl.options[i].value == ctrl.value)
			return ctrl.options[i].text;
	}
}

var wndVar;
function abrirSubVentana(url, nombre, ancho, alto)
{
	w = (screen.availWidth - ancho) / 2;
	h = (screen.availHeight - alto) / 2;
	features = "left="+w+",top="+h;
	features += ",width=" + ancho + ",height=" + alto + ",toolbar=no,menubar=no,resizable=yes,location=no,directories=no,status=no,scrollbars=yes,copyhistory=no";
	if(wndVar !=null)
	{
		wndVar.close();
		wndVar=null;
	}
	wndVar = window.open(url, nombre, features)
	wndVar.focus()
	return wndVar;
}

// Abre una subventana al tamaño máximo de pantalla
function abrirSubVentanaMax(url, nombre)
{
	w = 50;
	h = 50;
	features = "left="+w+",top="+h;
	features += ",width=" + (screen.availWidth - 100) + ",height=" + (screen.availHeight - 100) + ",toolbar=yes,menubar=yes,resizable=yes,location=yes,directories=yes,status=yes,scrollbars=yes,copyhistory=yes";
	if(wndVar !=null)
	{
		wndVar.close();
		wndVar=null;
	}
	wndVar = window.open(url, nombre, features)
	wndVar.focus()
	return wndVar;
}

function irPagina(url)
{
  f = document.formulario;
  f.action = url;
  f.submit();
}

function mostrarProcesando()
{
	estadoCapa('divProcesando', 'visible');
}

function irPaginaProcesando(url)
{
	mostrarProcesando();
	irPagina(url);
}

function pantallaCompleta()
{
	// Posicionar en toda la pantalla
	top.window.moveTo(0,0);
	if (document.all) 
	{
		top.window.resizeTo(screen.availWidth,screen.availHeight);
	}
	else if (document.layers || document.getElementById) 
	{
		if (top.window.outerHeight < screen.availHeight || top.window.outerWidth < screen.availWidth)
		{
			top.window.outerHeight = screen.availHeight;
			top.window.outerWidth = screen.availWidth;
		}
	}
}

function vaciarCombo(obj)
{
	for (var i = obj.options.length - 1; i >= 0; i--) {
		obj.options[i] = null;
	}
		obj.options[0] = new Option("","",false,false);
}

function LTrim(str)
{
        var whitespace = new String(" \t\n\r");

        var s = new String(str);

        if (whitespace.indexOf(s.charAt(0)) != -1) {
            var j=0, i = s.length;
            while (j < i && whitespace.indexOf(s.charAt(j))!= -1)
                j++;
            s = s.substring(j, i);
        }
        return s;
}

function RTrim(str)
{
        var whitespace = new String(" \t\n\r");
        var s = new String(str);

        if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
            var i = s.length - 1;       // Get length of string
            while (i >= 0 && whitespace.indexOf(s.charAt(i))!= -1)
                i--;
            s = s.substring(0, i+1);
        }
        return s;
}

function Trim(str) {
    return RTrim(LTrim(str));
}

function devuelveConPunto(numero) {	
	var nuevoNumero = (numero.toString()).replace(",", ".");
	
	return nuevoNumero;
}

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 vaciarRadioButton(ctrl)
{
	var i;
	for (i=0; i<ctrl.length; i++)
		ctrl[i].checked = false;
}

function seleccionadoRadioButton(ctrl)
{
	var i;
	if (!(ctrl.length))
	{
		if (ctrl.checked) return true;
	}
	else
	{
		for (i=0; i<ctrl.length; i++)
			if (ctrl[i].checked) return true;
	}
	return false;
}

function comprobarCampoVacio(obj, txt, tipo) {
	if (tipo == "S") { // Campo Si/No
		if (!seleccionadoRadioButton(obj)) {
			alert(txt + " es un campo obligatorio");
			return true;
		}
	} else if (tipo == "I") { // Campo Multi-Lista
		if (!seleccionadoRadioButton(obj)) {
			alert(txt + " es un campo obligatorio, debe marcar al menos una opción");
			return true;
		}
	} else { // El resto de campos
		if (obj.value == "") {
			alert(txt + " es un campo obligatorio");
			obj.focus();
			return true;
		}
	}
	return false;
}

/*
function comprobarFechaNoValida(obj) {
	if (obj.value != "")
		if (vFecha(obj,'dd/mm/yyyy')) return false;
		else return true;
}
*/
function comprobarFechaNoValida(obj){
	if (obj.value != "")
	{
		var Cadena = obj.value;
		if (Cadena == '') return false;
		var Fecha= new String(Cadena)	// Crea un string
		var RealFecha= new Date()	// Para sacar la fecha de hoy

		var Ano= new String(Fecha.substring(Fecha.lastIndexOf("/")+1,Fecha.length))
		var Mes= new String(Fecha.substring(Fecha.indexOf("/")+1,Fecha.lastIndexOf("/")))
		var Dia= new String(Fecha.substring(0,Fecha.indexOf("/")))

		// Valido el año
		if (isNaN(Ano) || Ano.length<4 || parseFloat(Ano)<1900){
			alert('Año inválido')
			obj.focus();
			return true
		}
		
		// Valido el Mes
		if (isNaN(Mes) || parseFloat(Mes)<1 || parseFloat(Mes)>12){
			alert('Mes inválido')
			obj.focus();
			return true
		}
		
		// Valido el Dia
		if (isNaN(Dia) || parseInt(Dia, 10)<1 || parseInt(Dia, 10)>31){
			alert('Día inválido')
			obj.focus();
			return true
		}
		if (Mes==4 || Mes==6 || Mes==9 || Mes==11 || Mes==2) {
			var maxFeb = 28;
			if ((((Ano % 4 == 0) && (Ano % 100 != 0)) || (Ano % 400 == 0))) maxFeb = 29;
			if (Dia>30 || (Mes==2 && Dia > maxFeb)) {
				alert('Día inválido')
				obj.focus();
				return true
			}
		}

		return false;
	}
}

function compararFechasMensaje(fecDesde, fecHasta, mensaje)
{

	if ((fecDesde !=null && fecDesde!="") && (fecHasta!=null && fecHasta!=""))
	{ 
		//Troceando la fecha desde
		var desde1 = fecDesde.indexOf('/');
		var desde2 = fecDesde.lastIndexOf('/');
		var desdeDD = fecDesde.substring(0,desde1);
		var desdeMM = fecDesde.substring(desde1+1,desde2);
		var desdeYY = fecDesde.substring(desde2+1,fecDesde.length);

		//Troceando la fecha hasta
		var hasta1 = fecHasta.indexOf('/');
		var hasta2 = fecHasta.lastIndexOf('/');
		var hastaDD = fecHasta.substring(0,hasta1);
		var hastaMM = fecHasta.substring(hasta1+1,hasta2);
		var hastaYY = fecHasta.substring(hasta2+1,fecHasta.length);

		fecDesde = desdeYY + desdeMM + desdeDD;
		fecHasta = hastaYY + hastaMM + hastaDD;	

		if (fecDesde > fecHasta)
		{ 
			if (mensaje != "")
				alert(mensaje)
			return(false);		
		}
	}
	return(true); 
}

function comprobarNumeroNoValido(obj) {
	if (obj.value != "") {
		obj.value = puntoPorComa(obj.value, ".");
		if (!esDecimalValido(obj.value)) {
			alert("Número incorrecto");
			obj.focus();
			return true;
		}
		else return false;
	}
}

function controlaLongitudTexto(obj, maximo)
{
	var largo
	largo = obj.value.length
	if (largo > maximo)
	obj.value = obj.value.substring(0,maximo)
}


