var Titulo = "Menú Afiliados"
var Subtitulo = "Principal"

daysInMonth=[31,28,31,30,31,30,31,31,30,31,30,31]

monthNames=['January','February','March','April','May','June',
              'July','August','September','October','November','December'];

  // Returns the number of days in the month in a given year (January=0)
  function getDaysInMonth2(month,year){
    if ((month==1)&&(year%4==0)&&((year%100!=0)||(year%400==0))){
      return 29;
    }else{
      return daysInMonth[month];
    }
  }


// Esta función permitirá validar la fecha
// En el objeto text hacer lo Siguiente
/*
   <input type='text' name=cajaFecha onChange='fechas(this.value); this.value=borrar'>
*/
	
	
function fechas(valor)
{  
   if (valor)
   {  
      borrar = valor;
      if ((valor.substr(2,1) == "/") && (valor.substr(5,1) == "/"))
      {      
         for (i=0; i<10; i++)
	     {	
            if (((valor.substr(i,1)<"0") || (valor.substr(i,1)>"9")) && (i != 2) && (i != 5))
			{
               borrar = '';
               break;  
			}  
         }
	     if (borrar)
	     { 
	        a = valor.substr(6,4);
		    m = valor.substr(3,2);
		    d = valor.substr(0,2);
		    if((a < 1900) || (a > 2050) || (m < 1) || (m > 12) || (d < 1) || (d > 31))
		       borrar = '';
		    else
		    {
		       if((a%4 != 0) && (m == 2) && (d > 28))	   
		          borrar = ''; // Año no viciesto y es febrero y el dia es mayor a 28
			   else	
			   {
		          if ((((m == 4) || (m == 6) || (m == 9) || (m==11)) && (d>30)) || ((m==2) && (d>29)))
			         borrar = '';	      				  	 
			   }  // else
		    } // fin else
         } // if (error)
      } // if ((valor.substr(2,1) == \"/\") && (valor.substr(5,1) == \"/\"))			    			
	  else
	     borrar = '';
	  if (borrar == '')
	     alert('Fecha errónea: '+valor);
   } // if (caja)   
} // FUNCION	
	
	
function isEmptyStr(strValue) {
   if ( (strValue.length==0) ||
        (strValue==null) || (strValue=='') ) {
      return true;
   }
   else { 
	    return false; 
	 }
}		
	





function fecha_valida(elem,obligatorio){
    return(soloFechas2(elem,obligatorio)&&valido_dia_mes(elem.value));
}	
	

function valido_dia_mes3(string_fecha)
{
// paso cada componente del string dd/mm/aaaa que representa la fecha a un arreglo con la funcion split
var array_fecha = string_fecha.split("/");
var fecha2 = new Date(array_fecha[2],array_fecha[1]-1,1);

  if (array_fecha.length<3) {   alert('Formato de fecha inválido.');
    return false;
  }

  if (getDaysInMonth(fecha2) < array_fecha[0]){
   alert('Día inválido para el mes ingresado.');
    return false;
  }
    else return true;
}

function valido_dia_mes_2(array_fecha,fecha_date){
// paso cada componente del string dd/mm/aaaa que representa la fecha a un arreglo con la funcion split


   if ((0<array_fecha[0])&&(getDaysInMonth2(fecha_date.getMonth(),fecha_date.getFullYear()) < array_fecha[0])){
       return false;
   } else 
	     return true;
}


function esBisiesto(year) { 
   return (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) ? 1 : 0;
}


function soloFechas(elem) {
 if (elem.value!=''){
    if (!(/\d{2}[\/]\d{2}[\/]\d{4}/g.test(elem.value))){
		  alert('Fecha con formato invalido');
			elem.focus();
      // elem.value = elem.value.replace(/[^\d]/g, '');
			}
	}		
}

function soloFechas2(strFecha,obligatorio) {
 if (!isEmptyStr(strFecha)){
    if (!(/\d{2}[\/]\d{2}[\/]\d{4}/g.test(strFecha))){
		  alert('Fecha con formato inválido: '+strFecha+' oblig: '+obligatorio);
			return false;
		} else{ 
			  return valido_dia_mes3(strFecha);				
		  }
 }else{   
  alert('Fecha vacía');
  if(obligatorio=='S') {
 		         alert('Fecha obligatoria.');
		           return false;}
			    else { 
 		         alert('FECHA OK.');					
					   return true;
					}
 }
}

function soloFechas3(strFecha,obligatorio) {
 if (!isEmptyStr(strFecha)){
    if (!(/\d{2}[\/]\d{2}[\/]\d{4}/g.test(strFecha))){
		  alert('Fecha con formato inválido: '+strFecha+' oblig: '+obligatorio);
			return false;
		} else{ 
		    alert('fecha válida:'+strFecha);
			  return true;				
		  }
 }else{   
  alert('Fecha vacía');
  if(obligatorio=='S') {
 		         alert('Fecha obligatoria.');
		           return false;}
			    else { 
 		         alert('FECHA OK.');					
					   return true;
					}
 }
}





function soloEnteros2(elem) {
    if (/[^\d]/g.test(elem.value))
       elem.value = elem.value.replace(/[^\d]/g, '');
}

function soloEnteros(objEvent)
     {
       var iKeyCode;       
       iKeyCode = objEvent.keyCode;               
       if(iKeyCode>=48 && iKeyCode<=57) return true;
       return false;
     }

function mostrarTitulo()
{
  document.getElementById('Titulo').innerHTML = '<h2>'+Titulo+'</h2> '+Subtitulo;
}

function mostrarCampania(Campa)
{
  document.getElementById('campaDescrip').innerHTML=Campa;
}

function mostrarLote(Lote)
{
  document.getElementById('nroLote').innerHTML=Lote;
}

function fondoWorkArea(Color)
{
  document.getElementById('workarea').bgColor=Color;
}

function callPopupMatricula(){
   nw=window.open('matricula_buscarxNombre.jsp', 'newWindow', 
	                'height=500, width=600, status,scrollbars=yes,resizable=yes');
}

function callPopupNombre(matricula){
   nw=window.open('matricula_buscarxMatricula.jsp?matricula='+matricula, 'newWindow', 
	                'height=500, width=600, status,scrollbars=yes,resizable=yes');
}


// Funcion utilizada en el Subsistema Afiliados-ABM 
// para armar dinámicamente un link 

function armaLink(miLink, operacion){
  //para que funcione tienen que existir inputs mat y dv
  document.getElementById(miLink).href = 
   "principal.jsp?op="+operacion+"&matricula="+document.forms[0].mat.value+"&dv="+document.forms[0].dv.value; //+"&direccion="+document.forms[0].email.value;				 
}


function uppercase()
{
  key = window.event.keyCode;
  if ((key > 0x60) && (key < 0x7B))
    window.event.keyCode = key-0x20;
}


var z = false;
function deshabilitar(){
if(z){
alert('Ya envió el formulario');
return false;
}else{
z = true;
document.getElementById('boton').disabled = true;
return true;
}

}

function Trim(TRIM_VALUE){
if(TRIM_VALUE.length < 1){
return"";
}
TRIM_VALUE = RTrim(TRIM_VALUE);
TRIM_VALUE = LTrim(TRIM_VALUE);
if(TRIM_VALUE==""){
return "";
}
else{
return TRIM_VALUE;
}
} //End Function

function RTrim(VALUE){
var w_space = String.fromCharCode(32);
var v_length = VALUE.length;
var strTemp = "";
if(v_length < 0){
return"";
}
var iTemp = v_length -1;

while(iTemp > -1){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(0,iTemp +1);
break;
}
iTemp = iTemp-1;

} //End While
return strTemp;

} //End Function

function LTrim(VALUE){
var w_space = String.fromCharCode(32);
if(v_length < 1){
return"";
}
var v_length = VALUE.length;
var strTemp = "";

var iTemp = 0;

while(iTemp < v_length){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(iTemp,v_length);
break;
}
iTemp = iTemp + 1;
} //End While
return strTemp;
} //End Function


function kH(e) {
var pK = e ? e.which : window.event.keyCode;
return pK != 13;
}

document.onkeypress = kH;
if (document.layers){ 
   document.captureEvents(Event.KEYPRESS);
}


function getDaysInMonth(aDate){
   // returns the last day of a given month
    var m = new Number(aDate.getMonth());
    var y = new Number(aDate.getYear());
    var tmpDate = new Date(y, m, 28);
    var checkMonth = tmpDate.getMonth();
    var lastDay = 27;

    while(lastDay <= 31){
        temp = tmpDate.setDate(lastDay + 1);
        if(checkMonth != tmpDate.getMonth())
            break;
        lastDay++
    }
    return lastDay;
}



function valido_dia_mes(string_fecha)
{
// paso cada componente del string dd/mm/aaaa que representa la fecha a un arreglo con la funcion split
var array_fecha = string_fecha.split("/");
var fecha2 = new Date(array_fecha[2],array_fecha[1]-1,1);

if (getDaysInMonth(fecha2) < array_fecha[0])
 {
   alert('Día inválido para el mes ingresado');
    return false;
 }
  else { 
	   return true;
	}
}

function cuantos_anios(dia1,dia2){
//recibe 2 parámetros de la clase Date y devuelve la diferencia en años.
  var diaD = Date(dia1);
	var diaH = Date(dia2);
	var anios = dia2.getYear()- dia1.getYear();
	if (dia2.getMonth()<dia1.getMonth())
	 anios=anios-1;
	if (dia2.getMonth()==dia1.getMonth()){
	 if (dia2.getDay()<dia1.getDay())
	   anios=anios-1;
	 }
	 return anios; 
  }
	
	
	


