//----------------------------------------------------------------------------------------
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_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_findObj(n, d) 
{ //v3.0
  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); 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];}
}

//-----------------------------------------------------------------------------------------------

var MM_openBrWindow_ventana ;

function MM_openBrWindow(theURL,winName,features) 
{ //v2.0
	var i, j, w, h, bColocar ;

	bColocar = false ;
	features = "" + features.toLowerCase() ;
	if ((features.indexOf("left=") < 0) && (features.indexOf("top=") < 0))
	{
		features = features + ",left=0,top=0" ;
		bColocar = true ;
	}
		
	var ventana = window.open("_blank",winName,features) ;
	ventana.opener = window ;
	ventana.blur() ;
	
	if (screen)
	{
		// BUSCO EL WIDTH Y EL HEIGHT EN features
		
		w = screen.width ;
		h = screen.height ;
		
		i = features.indexOf("width=") ;
		if (i>=0)
		{
			j = features.indexOf(",", i) ;
			if (j<0)
				j = features.length ;
			w = parseInt(features.substring(i+"width=".length,j)) ;
		}
		
		i = features.indexOf("height=") ;
		if (i>=0)
		{
			j = features.indexOf(",", i) ;
			if (j<0)
				j = features.length ;
			h = parseInt(features.substring(i+"height=".length,j)) ;
		}
		
		ventana.moveTo(((screen.width-w)/2) , ((screen.height-h)/2)) ;
	}
	
	ventana.document.location = theURL ;
	ventana.focus() ;
}

//--------------------------------------------------------------------------------------

function MM_openBrWindow_Cont() 
{
	var ventana, w, h ;
	
	ventana = MM_openBrWindow_ventana ;
	
	w = undefined ;
	h = undefined ;
		
	if (navigator.appName.indexOf("Netscape") >= 0)
	{
		w = ventana.innerWidth ;
		h = ventana.innerHeight ;
	}
	else
	{
		if ((ventana.document) && (ventana.document.body))
		{
			w = ventana.document.body.clientWidth;
			h = ventana.document.body.clientHeight;
		}
	}
	
	if (screen)
	{
		if ((w == undefined) || (h == undefined))
		{
			w=screen.width;
			h=screen.height;
		}
		
		ventana.moveTo(((screen.width-w)/2) , ((screen.height-h)/2)) ;
	}
	
	ventana.focus() ;
}

//--------------------------------------------------------------------------------------

function MM_timelinePlay(tmLnName, myID) 
{ //v1.2
  //Copyright 1997 Macromedia, Inc. All rights reserved.
  
  var i,j,tmLn,props,keyFrm,sprite,numKeyFr,firstKeyFr,propNum,theObj,firstTime=false;
  if (document.MM_Time == null) MM_initTimelines(); //if *very* 1st time
  tmLn = document.MM_Time[tmLnName];
  if (myID == null) 
  { 
  	myID = ++tmLn.ID; firstTime=true;
  }//if new call, incr ID
  if (myID == tmLn.ID) 
  { //if Im newest
    setTimeout('MM_timelinePlay("'+tmLnName+'",'+myID+')',tmLn.delay);
    fNew = ++tmLn.curFrame;
    for (i=0; i<tmLn.length; i++) 
    {
      sprite = tmLn[i];
      if (sprite.charAt(0) == 's') 
      {
        if (sprite.obj) {
          numKeyFr = sprite.keyFrames.length; firstKeyFr = sprite.keyFrames[0];
          if (fNew >= firstKeyFr && fNew <= sprite.keyFrames[numKeyFr-1]) {//in range
            keyFrm=1;
            for (j=0; j<sprite.values.length; j++) 
            {
              props = sprite.values[j]; 
              if (numKeyFr != props.length) 
              {
                if (props.prop2 == null) 
                	sprite.obj[props.prop] = props[fNew-firstKeyFr];
                else        
                	sprite.obj[props.prop2][props.prop] = props[fNew-firstKeyFr];
              } 
              else 
              {
                while (keyFrm<numKeyFr && fNew>=sprite.keyFrames[keyFrm]) keyFrm++;
                if (firstTime || fNew==sprite.keyFrames[keyFrm-1]) {
                  if (props.prop2 == null) sprite.obj[props.prop] = props[keyFrm-1];
                  else        sprite.obj[props.prop2][props.prop] = props[keyFrm-1];
        } } } } }
      } else if (sprite.charAt(0)=='b' && fNew == sprite.frame) eval(sprite.value);
      if (fNew > tmLn.lastFrame) tmLn.ID = 0;
  } }
}

//-------------------------------------------------------------------------------------------------------

function MM_initTimelines() 
{
    //MM_initTimelines() Copyright 1997 Macromedia, Inc. All rights reserved.
    
    var ns = navigator.appName == "Netscape";
    document.MM_Time = new Array(1);
    document.MM_Time[0] = new Array(1);
    document.MM_Time["Timeline1"] = document.MM_Time[0];
    document.MM_Time[0].MM_Name = "Timeline1";
    document.MM_Time[0].fps = 30;
    document.MM_Time[0][0] = new String("sprite");
    document.MM_Time[0][0].slot = 1;
    
    if (ns)
        document.MM_Time[0][0].obj = document["Layer1"];
    else
        document.MM_Time[0][0].obj = document.all ? document.all["Layer1"] : null;
        
    document.MM_Time[0][0].keyFrames = new Array(1, 3, 5, 7, 8, 12);
    document.MM_Time[0][0].values = new Array(3);
    document.MM_Time[0][0].values[0] = new Array(148,133,123,123,123,312,578,168,142,137,148,158);
    document.MM_Time[0][0].values[0].prop = "left";
    document.MM_Time[0][0].values[1] = new Array(126,120,116,116,116,122,131,116,116,118,119,121); 
    document.MM_Time[0][0].values[1].prop = "top";
    
    if (!ns) 
    {
        document.MM_Time[0][0].values[0].prop2 = "style";
        document.MM_Time[0][0].values[1].prop2 = "style";
    }
    
    document.MM_Time[0][0].values[2] = new Array("visible","visible","visible","visible","visible","inherit");
    document.MM_Time[0][0].values[2].prop = "visibility";
    
    if (!ns)
        document.MM_Time[0][0].values[2].prop2 = "style";
        
    document.MM_Time[0].lastFrame = 12;
    
    for (i=0; i<document.MM_Time.length; i++) 
    {
        document.MM_Time[i].ID = null;
        document.MM_Time[i].curFrame = 0;
        document.MM_Time[i].delay = 1000/document.MM_Time[i].fps;
    }
}

//------------------------------------------------------------------------------------
function MM_reloadPage(init) 
{  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}

//------------------------------------------------------------------------------------
function submitFormu( formu, sufijo, op, mensaje, bCompruebo, aCampos )
{	
	if ( mensaje.length > 0 )
		if ( !confirm(mensaje) )
			return false ;

	if ( formu.oh_sufijo )
		formu.oh_sufijo.value = sufijo ;
		
	formu.nh_subOp.value=op;
	 	 	
	if ( bCompruebo )
	{						
		if ( aCampos == null )
		{
			if ( !FormularioValido( formu ) )
				return false ;
		}
		else
		{
			// LE PEGO EL SUFIJO AL NOMBRE DE LOS CAMPOS
			
			var aCamposConSufijo, i, j ;
			
			aCamposConSufijo = new Array(aCampos.length) ;
			
			for ( i=0 ; i<aCampos.length ; i++ )
			{
				aCamposConSufijo[i] = null
				if (aCampos[i] != null) 
				{
					aCamposConSufijo[i] = new Array(aCampos[i].length) ;
					for ( j=0 ; j<aCampos[i].length ; j++ )
						aCamposConSufijo[i][j] = aCampos[i][j] ;
					
					aCamposConSufijo[i][0] = aCamposConSufijo[i][0] + sufijo ;
				}
			}
			
			if ( !FormularioValidoConArray( formu, aCamposConSufijo ) )
				return false ;
		}
	}
	   	 
	formu.submit() ;
}

//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
function confirmar( mensaje ) {
	if (! (confirm(mensaje))) {
		return false;
	}
	return true;
}
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
// returns true if number or ""

function checknum(a) {
   for(var i=0;i<a.length;i++) {
     if (a.charAt(i) < '0' || a.charAt(i)>'9')
       return false;
   }
   return true;
}
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
function checkNumDec (num)
{
	var iComa = 0;
	for (var i = 0; i < num.length; i++)
	{
		if (i == 0 && num.charAt (i) == '-')
			continue;
		if (num.charAt (i) == '.')
		{
			if (iComa++ > 1)
				return false;
		}
		else if (num.charAt(i) < '0' || num.charAt(i)>'9')
			return false;
	}
	return true;
}
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
function checkNumDecNeg (num)
{
	if (num.charAt (0) == '-' || num.charAt (0) == '+')
		return checkNumDec (num.substring (1, num.length) );
	else
		return checkNumDec (num);
}
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
function checkNumber(num, neg, dec)
{
	var i=0 ;
	var iComa = 0 ;

	if ( num.charAt(0) == '-' )
	{
		if ( neg == true )  // Permito el signo menos
			i++ ;
		else
			return false ;
	}
 
	for ( ; i < num.length; i++)
	{
		if (num.charAt(i) == ',')
		{
			if ( (dec == true) && (iComa==0) ) // permito decimales
			{
				iComa++ ;
			}
			else return false ;
		}
		else if (num.charAt(i) < '0' || num.charAt(i)>'9')
			return false;
	}
  
	return true;
}
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
function checkRangeInteger(num, ini, fin)
{
 var izq = parseInt(ini) ;
 var der = parseInt(fin) ;

 if (( isNaN(num) == true ) || ( isNaN(izq) == true ) || ( isNaN(der) == true ))
    return false ;

 if ( (num<izq) || (num>der))
    return false ;

 return true ;
}
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
function eliminaCerosIzquierda( cadena )
{
 return cadena ;
 var i=0 ;
 while ((i<cadena.lenght) && (cadena.charAt(i) == '0' ))
    i++ ;

 return cadena.substring(i, cadena.lenght) ;
}
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
// periodo_valido(fi,ff): fi debe ser una fecha menor o igual que ff

function periodo_valido(fi,ff) {
 var dd1 = fi.charAt(0) + fi.charAt(1) ;
 var mm1 = fi.charAt(3) + fi.charAt(4) ;
 var yyyy1 = fi.charAt(6) + fi.charAt(7) + fi.charAt(8) + fi.charAt(9);
 
 var dd2 = ff.charAt(0) + ff.charAt(1) ;
 var mm2 = ff.charAt(3) + ff.charAt(4) ;
 var yyyy2 = ff.charAt(6) + ff.charAt(7) + ff.charAt(8) + ff.charAt(9) ; 
 
 if ( yyyy1 + mm1 + dd1 > yyyy2 + mm2 + dd2 ) 
    return -4 ;
 
 return 1;
}
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
// fecha_valida(a): Devuelve true si "a"  tiene el 
// formato DD/MM/YYYY y false en el caso contrario.

function fecha_valida(a) {
 if (a.length != 10) return -1; 
 for(var i=0;i<a.length;i++) {
	if (i==2 || i==5) {
		if (a.charAt(i) != '/')  return -1; 
	}
	else {
		if (a.charAt(i) < '0' || a.charAt(i)>'9')  return -1; 
	}
 }

 var dd = parseInt(a.charAt(0) + a.charAt(1), 10);
 var mm = parseInt(a.charAt(3) + a.charAt(4), 10);
 
 // el día debe ser menor que 31 y el mes menor que 12
 if (dd<1 || dd>31) return -2;
 if (mm<1 || mm>12) return -3;
 
 return 1;
}
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
// CHEQUEO DE FORMULARIO MEDIANTE PREFIJOS DE LONGITUD VARIABLE
//  El prefijo estara compuesto por varias letras, tras las cuales ira un guion bajo, 
//   y el nombre descriptivo del campo.
//
//   1.   n (needed)
//        o (optional)
//
//   2.   n (number)
//        s (selection list)
//        t (text line)
//        m (multiple selection)
//        r (radio buttons)
//        c (checkbox)
//        a (textarea)
//        h (hidden)
//
//   3.1 NUMBER
//   3.1.1
//        z (positivo o negativo)
//        p (positivo)
//   3.1.2
//        e (entero)
//        d (decimal)
//   3.1.3
//        d (es un numero del 1 al 31)
//        m (es un numero del 1 al 12)
//        c (es un numero distinto de cero)
//
//   3.2 SELECTION LIST
//   3.2.1
//        f (first option is invalid)   
//
//   3.3 TEXT LINE (2 caracteres)
//		fi (fecha inicio de un periodo (dd/mm/yyyy))
//		ff (fecha fin de un periodo (dd/mm/yyyy))
//   3.4 MULTIPLE SELECTION
//
//   3.5 RADIO BUTTONS
//
//   3.6 CHECKBOX
//   3.6.1
//        --- ( Los tres caracteres siguientes a la 'c' de checkbox
//              identifican al grupo al que pertenece el checkbox. Esto
//              se usa para agrupar checkbox, de modo que podamos comprobar,
//              por ejemplo, que hay al menos uno marcado )
//
//   3.7 TEXT AREA
//
//   3.8 HIDDEN
//
//
//

 
function comprobarCampo( formulario, i, arrayLongitudesTextAreas, aDatosCampo )
{
	var j=0, pref0, pref1 ;

	pref0 = formulario.elements[i].name.charAt(0) ;
	pref1 = formulario.elements[i].name.charAt(1) ;

	// COMPRUEBO QUE EL PREFIJO DEL CAMPO ES VÁLIDO
	
	if (( pref0 != 'o' ) && ( pref0 != 'n' ))
	{
		alert ("NOMBRE DE CAMPO("+i+"): " + formulario.elements[i].name + " NO VÁLIDO. Avise al programador.\n");
		return false;
	}

   if (( pref1 != 'n' ) &&
       ( pref1 != 's' ) &&
       ( pref1 != 't' ) &&
       ( pref1 != 'm' ) &&
       ( pref1 != 'r' ) &&
       ( pref1 != 'c' ) &&
       ( pref1 != 'a' ) &&
       ( pref1 != 'h' ))
      {
       alert ("NOMBRE DE CAMPO("+i+"): " + formulario.elements[i].name + " NO VÁLIDO. Avise al programador.\n");
       return false;
      }
 
	// COMPRUEBO LA OBLIGATORIEDAD DEL CAMPO

	// NEEDED
   
	if ( pref0 == 'n' )
   {
   	if ( !campoFormuRelleno( formulario, i ) )
   	{
   		if ( aDatosCampo == null )
   			sTmp = "un campo obligatorio" ;
   		else
   			sTmp = "el campo '" + aDatosCampo[1] + "'" ;
   			
   		alert ("Se ha dejado " + sTmp + " vacío o sin marcar.\nRellene los campos obligatorios, por favor.\n");
       	return false;
      }
	}
    
	// TEXT (FECHAS), si es fecha inicio o fecha fin hay que comprobar que es un periodo válido
	
	if ( pref1 == 't' && formulario.elements[i].name.charAt (2) == 'f' && formulario.elements[i].value != ""  ) 
	{
		result = fecha_valida(formulario.elements[i].value,0);
		if (result == -1) 
		{
   		if ( aDatosCampo == null )
   			sTmp = "Las fechas deben" ;
   		else
   			sTmp = "El campo '" + aDatosCampo[1] + "' debe" ;
   			
			alert ( sTmp + " tener el formato DD/MM/AAAA.");
			return false;
		}
		if (result == -2) 
		{
   		if ( aDatosCampo == null )
   			sTmp = "En las fechas" ;
   		else
   			sTmp = "En el campo '" + aDatosCampo[1] + "'" ;
   			
			alert ( sTmp + " el día (DD) debe ser menor que 31." );
			return false;
		}
		if (result == -3) 
		{
   		if ( aDatosCampo == null )
   			sTmp = "En las fechas" ;
   		else
   			sTmp = "En el campo '" + aDatosCampo[1] + "'" ;
   			
			alert ( sTmp + " el mes (MM) debe ser menor que 12." );
			return false;
		}
		if (formulario.elements[i].name.charAt (3) == 'i') 
		{
			fi_anterior = formulario.elements[i].value;
		}
		if (formulario.elements[i].name.charAt (3) == 'f') 
		{
			result = periodo_valido(fi_anterior, formulario.elements[i].value);
			if (result<0) 
			{   			
				alert ("Error en periodo de vigencia: la fecha final debe ser posterior o igual que la fecha inicial.");
				return false;
			}
		} 
   }
	
	//  NUMBER
	 
   if ( pref1 == 'n' )
    {
     var pref2 = formulario.elements[i].name.charAt(2) ;
     var pref3 = formulario.elements[i].name.charAt(3) ;
     var pref4 = formulario.elements[i].name.charAt(4) ;

     var dec = false ;
     var neg = false ;

     if ( pref2 == 'z' )
        neg = true ;

     if ( pref3 == 'd' )
        dec = true ;
    
     if ( checkNumber (formulario.elements[i].value, neg, dec) == false)
      {
   		if ( aDatosCampo == null )
   			sTmp = "de valor " + formulario.elements[i].value ;
   		else
   			sTmp = "'" + aDatosCampo[1] + "'" ;
   			
       alert ("El campo " + sTmp + " debe ser un número válido.\n"+
              "Vuelva a rellenar dicho campo, por favor.\n");
       return false;
      }

		if (( pref4 == 'm' ) && ( formulario.elements[i].value != "" ))
			if (checkRangeInteger (formulario.elements[i].value, '01', '12') == false )
			{
	   		if ( aDatosCampo == null )
	   			sTmp = "de valor " + formulario.elements[i].value ;
	   		else
	   			sTmp = "'" + aDatosCampo[1] + "'" ;
   			
            alert ("El campo " + sTmp + " debe ser un número entre 1 y 12.\n"+
                   "Vuelva a rellenar dicho campo, por favor.\n");
            return false;
			}

		if (( pref4 == 'd' ) && ( formulario.elements[i].value != "" ))
			if (checkRangeInteger (formulario.elements[i].value, '01', '31') == false )
			{
	   		if ( aDatosCampo == null )
	   			sTmp = "de valor " + formulario.elements[i].value ;
	   		else
	   			sTmp = "'" + aDatosCampo[1] + "'" ;
   			
            alert ("El campo " + sTmp + " debe ser un número entre 1 y 31.\n"+
                   "Vuelva a rellenar dicho campo, por favor.\n");
            return false;
			}
           
		if (( pref4 == 'c' ) && ( formulario.elements[i].value != "" ))
			if ( formulario.elements[i].value == 0 )
			{
	   		if ( aDatosCampo == null )
	   			sTmp = "de valor " + formulario.elements[i].value ;
	   		else
	   			sTmp = "'" + aDatosCampo[1] + "'" ;
   			
				alert ("El campo " + sTmp + " tiene que ser distinto de cero.\n"+
                   "Vuelva a rellenar dicho campo, por favor.\n");
				return false;
			}
           

    }

	// SELECTION LIST
	
	if (( pref1 == 's' ) || ( pref2 == 'm' ))
	{
		var pref2 = formulario.elements[i].name.charAt(2) ;

		if ( pref2 == 'f' )
		{
       // no vale la primera opción, que será "Pulse aquí"
       // o algo parecido.

			if (formulario.elements[i].options[0].selected)
			{
	   		if ( aDatosCampo == null )
	   			sTmp = "" ;
	   		else
	   			sTmp = "del campo '" + aDatosCampo[1] + "'" ;
   			
				alert ( "Debe seleccionar una opción válida " + sTmp );
				return false;
			}
		}
	}
   
   // TEXT AREA
   
	if ( pref1 == 'a' ) 
	{   
		iMax = NaN ;  
		if ( arrayLongitudesTextAreas == null )
		{
			// EL TAM MAX ESTA EN UN HIDDEN
     	     	
			if ( formulario.elements['nnpe_' + formulario.elements[i].name] )
				iMax = parseInt(formulario.elements['nnpe_' + formulario.elements[i].name].value) ;
		}
		else
		{
			iMax = arrayLongitudesTextAreas[indiceTextArea] ;
			indiceTextArea++ ;
		}
     	
		if ( isNaN(iMax) )
		{
			alert ("TEXTAREA sin indicacion de longitud. Avise al programador") ;
			return false ;
		}

		if ( formulario.elements[i].value.length > iMax )
		{
   		if ( aDatosCampo == null )
   			sTmp = "de una caja de texto" ;
   		else
   			sTmp = "del campo '" + aDatosCampo[1] + "'" ;
   			
			alert ("El contenido " + sTmp + " excede el tamaño límite") ;
			return false ;         
		}	
	}
    
	return true ;
}

//------------------------------------------------------------------------------------
function campoFormuRelleno( formulario, i )
{
	var j ;
	var pref1 = formulario.elements[i].name.charAt(1) ;
	
	if (( pref1 != 'r' ) && ( pref1 != 'c') && ( pref1 != 's'))
	{
		if ( formulario.elements[i].value.length <= 0 )
			return false ;
	}
		
	if ( pref1 == 'r' )
	{
		// RADIO BUTTONS, si es obligatorio, alguno tiene que estar marcado
		
		var bAlguno = false;
		var longitud = eval ("formulario." + formulario.elements[i].name + ".length")
		
		for (var j = 0; j < longitud; j++)
		{
			if (eval ("formulario." + formulario.elements[i].name + "[j].checked") )
				bAlguno = true;
		}
       
		if (!bAlguno)
		{
			alert ( "Debe seleccionar alguna opción.");
			return false;
		}
	}
      

	if ( pref1 == 's' )
	{
		// SELECTION, al menos una tiene que estar marcada
		
		var bAlguno = false;
		var longitud = eval ("formulario." + formulario.elements[i].name + ".length") ;
		
		for (var j = 0; j < longitud; j++)
		{
			if ( eval ("formulario." + formulario.elements[i].name + ".options[j].selected") )
				bAlguno = true;
		}
		
		if (!bAlguno)
			return false;
	}
	
	return true ;
}

//------------------------------------------------------------------------------------
function FormularioValido (formulario, arrayLongitudesTextAreas )
{
	var sufijo, indiceTextArea = 0 ;

	sufijo = '' ;
	
	if ( formulario.oh_sufijo )
		sufijo = ''+formulario.oh_sufijo.value ;
		
   for (var i=0; i < formulario.elements.length; i++)
   {
   	if (( sufijo.length <= 0 ) || ( endsWith( formulario.elements[i].name, sufijo) ))
   	{
	   	if ( !comprobarCampo(formulario, i, arrayLongitudesTextAreas, null) )
	   		return false ;
	   }
   }
   
   return true;
}

//------------------------------------------------------------------------------------
function FormularioValidoConArray(formulario, aCampos )
{
	var sufijo, indiceTextArea = 0 ;
	var aComprobar, i, j, bTmp ;
	
	aComprobar = Array(aCampos.length) ;
		
	// PRIMERO, CONSTRUYO UN ARRAY CON UN BOOLEAN QUE INDICA, PARA CADA ELEMENTO, SI COMPROBARLO O NO
	
	for ( i=0 ; i<aCampos.length ; i++ )
	{
		aComprobar[i] = new Array(2) ;
		
		aComprobar[i][0] = 2 ;						// EL 2 INDICA "AUN NO SE SI TENGO QUE COMPROBARLO"
		aComprobar[i][1] = -1 ;						// INDICE DE ESTE CAMPO EN EL ARRAY DE ELEMENTS
	}

	// GUARDO LOS INDICES DE CADA ELEMENTO EN EL FORMU
	
   for ( i=0; i < aComprobar.length; i++)
		for ( j=0 ; j<formulario.elements.length ; j++ )
		{
			if ((aCampos[i] != null) && ( formulario.elements[j].name == aCampos[i][0] ))
			{
				aComprobar[i][1] = j ;
				break ;
			}
		}
		
	// AHORA, VOY RELLENANDO ESTE ARRAY
	
	for ( i=0 ; i<aCampos.length ; i++ )
	{
		if (aCampos[i] != null) 
		{
			// LOS DEL GRUPO VACIO SE COMPRUEBAN SIEMPRE
			
			 if (aCampos[i][2] == "")
			 {
			 	aComprobar[i][0] = true
			 }
			 else
			 {		 
				// PARA CADA CAMPO, MIRO SI YA HE COMPROBADO A ALGUNO DE SU GRUPO
				
				for ( j=0 ; j<i ; j++ )
				{
					if ( aCampos[j] != null )
					{
						if ( aCampos[i][2] == aCampos[j][2] )
						{
							aComprobar[i][0] = aComprobar[j][0] ;
							break ;
						}
					}
				}
			
				// SI NO HE COMPROBADO AUN NINGUNO DE ESE GRUPO, LO COMPRUEBO
				
				bTmp = false ;
				
				if ( aComprobar[i][0] == 2 )
				{			
					for ( j=0 ; j<aCampos.length ; j++ )
					{
						if (aCampos[j] != null)
						{
							// COMPRUEBO CADA ELEMENTO DEL GRUPO, MIRANDO SI TIENE ALGO RELLENO (SALVO QUE SEA OCULTO)
							
							if (( aCampos[i][2] == aCampos[j][2] ) && ( aCampos[j][0].charAt(1) != 'h' ))
							{
								if ( campoFormuRelleno( formulario, aComprobar[j][1] ) )
								{
									bTmp = true ;		
								}						
							}
						}
					}
					
					if ( bTmp )				// SI ALGUNO DEL GRUPO ESTABA RELLENO, DEBO COMPROBAR. SI NO, NO LO COMPRUEBO
						aComprobar[i][0] = 1 ;
					else
						aComprobar[i][0] = 0 ;
				}
			}
		}
	}

	// POR FIN, COMPRUEBO LOS CAMPOS QUE TENGA MARCADOS COMO "A COMPROBAR"
	
   for ( i=0; i < aComprobar.length; i++)
	{
		if ( aComprobar[i][0] == 1 )
		{	
			if ( !comprobarCampo(formulario, aComprobar[i][1], null, aCampos[i]) )
				return false ;
		}
   }
   
   return true;
}

//------------------------------------------------------------------------------------
function endsWith( sCadena, sSufijo )
{
	if (( sCadena == null ) || ( sSufijo == null ))
		return false ;
		
	if ( sCadena.length < sSufijo.length )
		return false ;
		
	
	var sTmp = sCadena.substring( sCadena.length-sSufijo.length, sCadena.length ) ;
	
	return (sTmp == sSufijo) ;
}

//------------------------------------------------------------------------------------

function comprobarNumeroCuenta( sCuenta )
{
	var sEntidadYOficina, sDC1, sNumero, sDC2, i
	var iPeso, iPesos
	
	if ( sCuenta.length != 20 ) 
		return false ;
		
	sEntidadYOficina = sCuenta.substr(0,8) ;
	sDC1 = sCuenta.substr(8,1) ;
	sNumero = sCuenta.substr(10,10) ;
	sDC2 = sCuenta.substr(9,1) ;
	
	iPesos = new Array( 4, 8, 5, 10, 9, 7, 3, 6 ) ;
	iPeso = 0 ;
	
	for ( i=0 ; i<sEntidadYOficina.length ; i++ )
		iPeso += ( parseInt(sEntidadYOficina.charAt(i)) * iPesos[i] ) ;
	
	iPeso = 11 - ( iPeso % 11 ) ;
	if (iPeso == 10)
		iPeso = 1 ;
	else if (iPeso == 11)
		iPeso = 0 ;
		
	if ( iPeso != parseInt(sDC1) )
		return false ;

	iPesos = new Array( 1, 2, 4, 8, 5, 10, 9, 7, 3, 6 ) ;
	iPeso = 0 ;
	
	for ( i=0 ; i<sNumero.length ; i++ )
		iPeso += ( parseInt(sNumero.charAt(i)) * iPesos[i] ) ;
	
	iPeso = 11 - ( iPeso % 11 ) ;
	if (iPeso == 10)
		iPeso = 1 ;
	else if (iPeso == 11)
		iPeso = 0 ;
	
	if ( iPeso != parseInt(sDC2) )
		return false ;
				
	return true ;	
}

//------------------------------------------------------------------------------------
function urlEncode( sUrl )
{
	var i, sUrlEncoded, c ;
	
	if ( sUrl == null )
		return null ;
		
	sUrlEncoded = "" ;
	
	for (i=0 ; i<sUrl.length ; i++)
	{
		c = sUrl.charAt(i) ;
		
		if (c < ' ')
			c='';
		else
		{
			switch(c)
			{
				case ' ':
					c="%20" ;
					break ;
					
				case '#':
					c="%23" ;
					break ;
	
				case '%':
					c="%25" ;
					break ;		
									
				case '?':
					c="%3F" ;
					break ;
					
				case '&':
					c="%26" ;
					break ;
					
				case '/':
					c="%2F" ;
					break ;	
			}
		}
		
		sUrlEncoded += c ;
	}
	
	return sUrlEncoded ;
}

//------------------------------------------------------------------------------------
function openUpload( sTipo, sArchivo )
{
	var w ;
	
	w = window.open( './upload/formUpload.asp?tipo=' + sTipo + '&sArchivo=' + sArchivo,'upload','resizable=yes,scrollbars=no,width=550,height=300') ;
	w.focus() ;
}

//------------------------------------------------------------------------------------
function autocompletar( campoTexto, evento, aValores )
{
	var sValor, iMin, iMax, i, sPref, code, ENTER ;
	
	if (!aValores) 
		return null ;
	
	sValor = campoTexto.value.toUpperCase() ;

	if (document.layers)
		code = "" + evento.which ;
	else if (document.all)
		code = "" + evento.keyCode ;
		
	ENTER = "13" ;
	
	if (code == ENTER)
	{		
		if (sValor.length > 0)
		{
			iMin = 0 ;
			iMax = aValores.length-1 ;
			
			sPref = aValores[iMin].substring(0,sValor.length).toUpperCase() ;
			if ( sPref == sValor )
			{
				campoTexto.value = aValores[iMin] ;
				return ;
			}
			else if (sPref > sValor)
				return ;

			sPref = aValores[iMax].substring(0,sValor.length).toUpperCase() ;
			if ( sPref == sValor )
			{
				campoTexto.value = aValores[iMax] ;
				return ;
			}
			else if ( sPref < sValor )
				return ;
							
			while ((iMax-iMin) > 1)
			{
				i = parseInt ((iMax+iMin)/2) ;
				
				sPref = aValores[i].substring(0,sValor.length).toUpperCase() ;
				
				if (sPref == sValor)
				{
					campoTexto.value = aValores[i] ;
					return ;
				}
				else if (sPref < sValor)	
				{
					iMin = i ;
				}
				else
				{
					iMax = i ;
				}
			}
		}
	}
}

//------------------------------------------------------------------------------------------------------

function comprobarFormatoFecha(sFecha)
{
	var iDia, iMes, iAnio, fecha ;
	
	if (sFecha == 'dd/mm/aaaa') 
		return true ;
		
	if (sFecha.length > 0)
	{
	   if (sFecha.length != 10)
	   {
	   	alert("El formato de la fecha no es correcto (DD/MM/AAAA)") ;
	   	return false ;
	   }
	   
	   if ((sFecha.substring(2,3) != "/") || (sFecha.substring(5,6) != "/"))
	   {
	   	alert("El formato de la fecha no es correcto (DD/MM/AAAA)") ;
	   	return false ;
	   }
	   
	   // EL parseInt FALLA CUANDO TIENE CEROS DELANTE
	   
	   sDia = sFecha.substring(0,2) ;
	   if (sDia.substring(0,1) == "0") 
	   	sDia = sDia.substring(1,2) ;

	   sMes = sFecha.substring(3,5) ;
	   if (sMes.substring(0,1) == "0") 
	   	sMes = sMes.substring(1,2) ;
	   		   	
	   iDia = parseInt(sDia) ;
	   iMes = parseInt(sMes) ;
	   iAnio = parseInt(sFecha.substring(6,10)) ;
	   
	   if (isNaN(iDia) || isNaN(iMes) || isNaN(iAnio))
	   {
	   	alert("El formato de la fecha no es correcto (DD/MM/AAAA)") ;
	   	return false ;
	   }
	   
	   iMes-- ;
	   	
	   fecha = new Date(iAnio,iMes,iDia) ;	  
	                                     
	  	if (( fecha.getYear() != iAnio) || (fecha.getMonth() != iMes) ||
	  			(fecha.getDate() != iDia)) 
	   {
	   	alert("La fecha no es correcta") ;
	   	return false ;
	   }
	}
	
	return true ;
}

//----------------------------------------------------------------------------
function getFechaHora()
{
var mydate=new Date();
var year=mydate.getYear();
if (year < 1000)
year+=1900;
var month=mydate.getMonth();
var daym=mydate.getDate();
if (daym<10)
daym="0"+daym;
var montharray=new Array("1","2","3","4","5","6","7","8","9","10","11","12");

return daym+" / "+montharray[month]+" / "+year ;
}

//-------------------------------------------------------------------------
function replaceSubstring(inputString, fromString, toString) {
   // Goes through the inputString and replaces every occurrence of fromString with toString
   var temp = inputString;
   if (fromString == "") {
      return inputString;
   }
   if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
      var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";
      // Find a string that doesn't exist in the inputString to be used
      // as an "inbetween" string
      while (midString == "") {
         for (var i=0; i < midStrings.length; i++) {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      } // Keep on going until we build an "inbetween" string that doesn't exist
      // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }
      // Next, replace the "inbetween" string with the "toString"
      while (temp.indexOf(midString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } // Ends the check to see if the string being replaced is part of the replacement string or not
   return temp; // Send the updated string back to the user
} 

//------------------------------------------------------------------------------------
function eurosToPesetas(sEuros)
{
	var euros, pesetas, i, sTmp ;
	
	euros = parseFloat(sEuros) ;
	pesetas = "" + parseInt(euros * 166.386) ;
	return ponerPuntosMiles(pesetas) ;
}

//------------------------------------------------------------------------------------------
function ponerPuntosMiles(string)
{
	var sNew, i, iDec, iCont ;
	
	string = "" + string ;
	if (string.indexOf(",") >= 0)
		iCont = ((-1) * (string.length - string.indexOf(","))) ;
	else
		iCont = -1 ;

	sNew = "" ;
	
	for ( i=string.length ; i>=0 ; i--)
	{
		sNew = string.charAt(i) + sNew ;
		iCont++ ;
		if ((iCont == 3) && (i > 0))
		{
			sNew = "." + sNew ;
			iCont = 0 ;
		}
	}
	
	return sNew ;
}

//---------------------------------------------------------------------
function getFecha()
{
	var mydate = new Date() ;
	var year = mydate.getYear() ;
	if (year < 1000)
	year+=1900 ;
	var month = mydate.getMonth() ;
	var daym = mydate.getDate() ;
	if (daym < 10)
		daym = "0" + daym ;
	var montharray = new Array("1","2","3","4","5","6","7","8","9","10","11","12") ;
	
	return daym + " / " + montharray[month] + " / " + year ;
}

//---------------------------------------------------------------------------------

function addToFavorites(sUrl, sTitle)
{
	var sNewTitle ;
	
	if (sTitle == null)
		sNewTitle = "" ;
	else
		sNewTitle = sTitle ;

	if (document.all)
		window.external.AddFavorite(sUrl, sNewTitle);
	else
		alert("Para añadir esta página a favoritos cierra esta ventana y pulsa <Ctrl-D>.");
}

//---------------------------------------------------------------------------------

function submitEnviarAmigo()
{
 	MM_openBrWindow('/scripts/enviar_amigo/form.asp','enviar_amigo','width=450,height=250');
 	setTimeout('if (document.formEnviarAmigo) document.formEnviarAmigo.submit();', 1000);                     	
}

//---------------------------------------------------------------------------------

function checkFields(form, aCampos)
{
	var i ;
 	for (i=0 ; i<aCampos.length ; i++)
  	{
  		obj = form.elements[aCampos[i][0]] ;
  		if (obj)
  		{
  			if ((obj.type == 'text') || (obj.type == 'textarea') || (obj.type == 'file') || (obj.type == 'password'))
  			{
  				if (obj.value.length <= 0) 
  				{
  					alert('Debes indicar ' + aCampos[i][1]);
  					obj.focus();
  					return false ;
  				}
  			}
  			else if (obj.type == 'select-one')
  			{ 
  				bTmp = false ;
  				for (j=0; j<obj.options.length; j++)
  					if ((obj.options[j].selected) && (obj.options[j].value.length > 0))
  					{
  						bTmp = true ;
  						break ;
  					}
  				if (!bTmp)
  				{
  					alert('Debes indicar ' + aCampos[i][1]);
  					obj.focus();
  					return false ;
  				}
  			}
  			else if (obj.length)
  			{ 
  				bTmp = false ;
  				for (j=0; j<obj.length; j++)
  					if (obj[j].checked)
  					{
  						bTmp = true ;
  						break ;
  					}
  				if (!bTmp)
  				{
  					alert('Debes indicar ' + aCampos[i][1]);
  					obj[0].focus();
  					return false ;
  				}
  			}
  		}
  	}
  	
  	return true ;
}
