var dia = 86400000;

function UltDiaMes(Mes, Ano) {
    switch (Mes) {
      case "01":
      case "03":
      case "05":
      case "07":
      case "08":
      case "10":
      case "12":
        return ("31");
      case "04":
      case "06":
      case "09":
      case "11":
        return ("30");
      case "02":
        if (((parseInt(Ano,10))%4) == 0)
          return ("29");
        else
          return ("28");
        }
}
function ValidaFechas() {

	/* Comprueba que no se hayan introducido caracteres que no sean numéricos */

	for ( i=0; i<2; i++ )
		if (document.form1.dia1.value.charAt(i)!='')
     			if ((document.form1.dia1.value.charAt(i)<'0') || (document.form1.dia1.value.charAt(i)>'9')){
				alert ( "Incorrect value in 'From' day" );
	      	     	return (false);
			}
	for ( i=0; i<2; i++ )
      	if (document.form1.mes1.value.charAt(i)!='')
     			if ((document.form1.mes1.value.charAt(i)<'0') || (document.form1.mes1.value.charAt(i)>'9')){
				alert ( "Incorrect value in 'From' month" );
	      	     	return (false);
			}
	for ( i=0; i<4; i++ )
     		if ((document.form1.anyo1.value.charAt(i)<'0') || (document.form1.anyo1.value.charAt(i)>'9')){
			alert ( "Incorrect value in 'From' year" );
      	     	return (false);
		}
	for ( i=0; i<2; i++ )
		if (document.form1.dia2.value.charAt(i)!='')
     			if ((document.form1.dia2.value.charAt(i)<'0') || (document.form1.dia2.value.charAt(i)>'9')){
				alert ( "Incorrect value in 'To' day" );
      		     	return (false);
			}
	for ( i=0; i<2; i++ )
		if (document.form1.mes2.value.charAt(i)=='')
     			if ((document.form1.mes2.value.charAt(i)<'0') || (document.form1.mes2.value.charAt(i)>'9')){
				alert ( "Incorrect value in 'To' month" );
      		     	return (false);
			}
	for ( i=0; i<4; i++ )
     		if ((document.form1.anyo2.value.charAt(i)<'0') || (document.form1.anyo2.value.charAt(i)>'9')){
			alert ( "Incorrect value in 'To' year" );
      	     	return (false);
		}

	/* Comprueba que no se haya introducido un valor en blanco. Si la cifra es de un
	dígito, se añade un cero delante */

	if ((document.form1.dia1.value.charAt(0)=='') && (document.form1.dia1.value.charAt(1)=='')){
		alert ( "Incorrect value in 'From' day" );
     	     	return (false);
	}else {
		if (document.form1.dia1.value.charAt(1)=='')
			document.form1.dia1.value='0'+document.form1.dia1.value.charAt(0);
	}
	if ((document.form1.mes1.value.charAt(0)=='') && (document.form1.mes1.value.charAt(1)=='')){
		alert ( "Incorrect value in 'From' month" );
     	     	return (false);
	}else {
		if (document.form1.mes1.value.charAt(1)=='')
			document.form1.mes1.value='0'+document.form1.mes1.value.charAt(0);
	}
	if ((document.form1.dia2.value.charAt(0)=='') && (document.form1.dia2.value.charAt(1)=='')){
		alert ( "Incorrect value in 'To' day" );
     	     	return (false);
	}else {
		if (document.form1.dia2.value.charAt(1)=='')
			document.form1.dia2.value='0'+document.form1.dia2.value.charAt(0);
	}
	if ((document.form1.mes2.value.charAt(0)=='') && (document.form1.mes2.value.charAt(1)=='')){
		alert ( "Incorrect value in 'To' month" );
     	     	return (false);
	}else {
		 if (document.form1.mes2.value.charAt(1)=='')
			document.form1.mes2.value='0'+document.form1.mes2.value.charAt(0);
	}

	/* Comprueba que el mes introducido está entre 1 y 12, ambos inclusive */

	if ((document.form1.mes1.value < "01") || (document.form1.mes1.value > "12")){
		alert ( "'From' month must be between 1 and 12" );
		return (false); }
	if ((document.form1.mes2.value < "01") || (document.form1.mes2.value > "12")){
		alert ( "'To' month must be between 1 and 12" );
		return (false); }

	/* Comprueba que el día introducido existe en el mes seleccionado */

	DiasMes=UltDiaMes(document.form1.mes1.value,document.form1.anyo1.value);
	if ((document.form1.dia1.value < "01") || (document.form1.dia1.value > DiasMes)){
		alert ( "'From' day must be between 1 and "+ DiasMes );
		return (false);
	}
	DiasMes=UltDiaMes(document.form1.mes2.value,document.form1.anyo2.value);
	if ((document.form1.dia2.value < "01") || (document.form1.dia2.value > DiasMes)){
		alert ( "'To' day must be between 1 and "+ DiasMes );
		return (false);
	}

	/* Comprueba que el año es correcto para el tipo de sector seleccionado.
	   Si es un sector antiguo la fecha tiene que ser anterior al 2002.
	   Si es un nuevo sector la fecha tiene que ser posterior al 2000 */

	if (document.form1.sector){
		if (document.form1.sector.options[document.form1.sector.selectedIndex].value.substring(3,5)=="01") {
			if (document.form1.anyo1.value > "2001") {
				alert ( "To old sectors, 'From' and 'To' dates must be before 2002 year" );
				return (false); }
			if (document.form1.anyo2.value > "2001") {
				alert ( "To old sectors, 'From' and 'To' dates must be before 2002 year" );
				return (false); }
		}else {
			if (document.form1.anyo1.value < "2001") {
				alert ( "To new sectors, 'From' and 'To' dates must be after 2002 year" );
				return (false); }
			if (document.form1.anyo2.value < "2001") {
				alert ( "To new sectors, 'From' and 'To' dates must be after 2002 year" );
				return (false); }
		}
	}

	/* Comprueba que la fecha 'Desde' no sea posterior a la fecha 'Hasta' */

	if (document.form1.anyo1.value > document.form1.anyo2.value) {
		alert ( "'From' date can't be after 'To' date");
		return (false);
	}else {
		if (document.form1.anyo1.value == document.form1.anyo2.value) {
			if (document.form1.mes1.value > document.form1.mes2.value) {
				alert ( "'From' date can't be after 'To' date");
				return (false);
			}else {
				if (document.form1.mes1.value == document.form1.mes2.value) {
					if (document.form1.dia1.value > document.form1.dia2.value) {
						alert ( "'From' date can't be after 'To' date");
						return (false); }
				}
			}
		}
	}

	/* Comprueba que si se visualiza por pantalla, el período sea de como máximo dos años */

	a1=parseInt(document.form1.anyo1.value);
	a2=parseInt(document.form1.anyo2.value);

	if (document.form1.tipo.value=="htm"){
		if (a1+2 < a2) {
			alert ( "If you choose on screen, the selected period can't be of more than two years");
			return (false);
		}else {
			if (a1+2 == a2) {
				if (document.form1.mes1.value < document.form1.mes2.value) {
					alert ( "If you choose on screen, the selected period can't be of more than two years");
					return (false);
				}else {
					if (document.form1.mes1.value == document.form1.mes2.value) {
						if (document.form1.dia1.value < document.form1.dia2.value) {
							alert ( "If you choose on screen, the selected period can't be of more than two years");
							return (false); }
					}
				}
			}
		}
	}

	document.form1.submit();
	return(true);
}
function ComprobarFecha(Fecha) {
    if (Fecha.length != 8)
        return (false);
    for ( ind=0; ind < 8; ind++ )
        if ((Fecha.charAt(ind)<'0') || (Fecha.charAt(ind)>'9'))
            return (false);
    Ano=Fecha.substr(0,4);
    Mes=Fecha.substr(4,2);
    Dia=Fecha.substr(6,2);
    UltimoDia=UltDiaMes(Mes,Ano)
    if ((Mes < '01') || (Mes > '12') || (Dia < '01') || (Dia > UltimoDia))
        return (false);
    return (true);
}
function comprobar(not) {
	var cad, cad2;

	if ( document.form1.hecho )
		if ( document.form1.hecho.options )
			if ( document.form1.hecho.options
			     [document.form1.hecho.selectedIndex].value == "" )
			    return (false);
	if ( document.form1.mes1 ) {
		if (document.form1.mes1.value.length==1) document.form1.mes1.value="0" + document.form1.mes1.value;
		if (document.form1.mes2.value.length==1) document.form1.mes2.value="0" + document.form1.mes2.value;
		if (document.form1.dia1.value.length==1) document.form1.dia1.value="0" + document.form1.dia1.value;
		if (document.form1.dia2.value.length==1) document.form1.dia2.value="0" + document.form1.dia2.value;

		cad = document.form1.anyo1.value+document.form1.mes1.value+document.form1.dia1.value;
		cad2 = document.form1.anyo2.value+document.form1.mes2.value+document.form1.dia2.value;
		if (!(ComprobarFecha(cad))) {
		    alert("'From' field is wrong.");
		    return( false);
		}
		if (!(ComprobarFecha(cad2))) {
		    alert("'To' field is wrong.");
		    return( false);
		}
	} else if ( document.form1.anyo1 ) {
		cad = document.form1.anyo1.value;
		cad2 = document.form1.anyo2.value;
	}
    if (cad>cad2) {
        alert("Wrong order in date.");
        return( false);
    }
    if ( not ) {

           	/* Comprueba que si no se introduce una empresa, el período sea de como máximo un mes */

/* 		a1=parseInt(document.form1.anyo1.value,10);
		a2=parseInt(document.form1.anyo2.value,10);
 		m1=parseInt(document.form1.mes1.value,10);
		m2=parseInt(document.form1.mes2.value,10);
 		d1=parseInt(document.form1.dia1.value,10);
		d2=parseInt(document.form1.dia2.value,10);

		if (document.form1.nombre.value.length==0){
			if (a1 == a2) {
				if (m1+1 == m2) {	
					if (d1 < d2) {
						alert ( "In case of date ranges of more than 1 month\nyou must enter the issuing company name" );
						return (false);
					}
				}else {
					if (m1 != m2) {	
						alert ( "In case of date ranges of more than 1 month\nyou must enter the issuing company name" );
						return (false);
					}
				}
			}else {
				if (a1+1 == a2) {
					if ((m1 != 12) || (m2 != 1)) {
						alert ( "In case of date ranges of more than 1 month\nyou must enter the issuing company name" );
						return (false);
					}else {
						if ((m1 == 12) && (m2 == 1)) {
							if (d1 < d2) {
								alert ( "In case of date ranges of more than 1 month\nyou must enter the issuing company name" );
								return (false);
							}
						}
					}
				}else {
					alert ( "In case of date ranges of more than 1 month\nyou must enter the issuing company name" );
					return (false);
				}
			}
		}*/

		var fecha1 = new Date( parseInt(document.form1.anyo1.value,10),
		                       parseInt(document.form1.mes1.value,10)-1,
		                       parseInt(document.form1.dia1.value,10) );
		var fecha2 = new Date( parseInt(document.form1.anyo2.value,10),
		                       parseInt(document.form1.mes2.value,10)-1,
		                       parseInt(document.form1.dia2.value,10) );
		if ( ((fecha2.getTime()-fecha1.getTime())>(dia*33+5600000)) &&
		     document.form1.nombre.value.length == 0 ) {
			alert ( "In case of date ranges of more than 1 month\nyou must enter the issuing company name" );
			return ( false );
		}
	}
    document.form1.submit();
    return ( true );
}
function compr_nombre() {
    var ind;
    for(ind=0;ind<document.form1.nombre.value.length; ind++) {
        if (document.form1.nombre.value.charAt(ind)!=' ') {
            return(true);
        };
    };
    alert("You must enter a name");
    return (false);
}
function fechas(not) {
    var hoy = new Date();
    var dosD = new Date();

    var agent = navigator.userAgent.toLowerCase();
    if (parseFloat(navigator.appVersion)<=4.01 && agent.indexOf('msie') == -1)
        return(true);

    aaaa=hoy.getFullYear();
    aaa1=aaaa - 1;
    if (not) mm=hoy.getMonth()+1;
    else mm=hoy.getMonth()+2;
    dd=hoy.getDate();
    if (mm>12){mm=mm-12;aaaa=aaaa+1;}
    if (mm<10) mm="0" + mm;
    if (dd>UltDiaMes(mm,aaaa)) dd=UltDiaMes(mm,aaaa);
    if (dd<10) dd="0" + dd;

	if ( document.form1.mes1 ) {
		document.form1.dia2.value="" + dd;
		document.form1.mes2.value="" + mm;
		if ( not ) {
			dosD.setTime(hoy.getTime() - (dia*30));
			mm=dosD.getMonth()+1;
			dd=dosD.getDate();
			if (mm<10) mm="0" + mm;
			if (dd<10) dd="0" + dd;
			document.form1.dia1.value="" + dd;
			document.form1.mes1.value="" + mm;
		} else {
			document.form1.dia1.value="01";
			document.form1.mes1.value="01";
		}
	} else {
		aaaa++;
	}
	if ( document.form1.anyo1 ) {
	    document.form1.anyo2.value=aaaa;
		if ( not ) {
			document.form1.anyo1.value=dosD.getFullYear();
		} else {
		    document.form1.anyo1.value=aaa1;
		}
	}

    return(true);
}
function comprobar2() {
    if ( document.form1.anyo1.value >
         document.form1.anyo2.value ) {
        alert("Wrong order in years");
        return( false);
    }
    return compr_nombre();
}

function EscribeConsFicha() {
  var html = "";

  html += '<form method=get action="/comun/fichaemp/fichaemp.asp" name=form1 OnSubmit="return (compr_nombre());">';
  html += '<input type=hidden name="id" value="ing">';
  html += '<input type=hidden name="tipo" value="FichaEmp">';
  html += '<table width=100% border=0 cellspacing=0 cellpadding=2 bgcolor=#000000>';
  html += ' <tr>';
  html += '  <td><img src="../images/FichaEmpresa.gif" width=200 height=27></td>';
  html += '  <td align=right valign=bottom>';
  html += '   <b><font color=#FFFFFF>Name </font></b>';
  html += '   <input type=text name="nombre" size=25 maxlength=40>';
  html += '   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  html += '   <input type=submit name="Buscar" value="Search">';
  html += '  </td>';
  html += ' </tr>';
  html += '</table>';
  document.write(html);
}

// Sets cookie values. Expiration date is optional
//
function setCookie(name, value, expire) {
  document.cookie = name + "=" + escape(value)
  + ((expire == null) ? "" : ("; expires=" + expire.toGMTString()))
  + "; PATH=/";
}
// Gets cookie values.
//
function getCookie(Name) {
  var search = Name + "=";
  if (document.cookie.length > 0) { // if there are any cookies
    offset = document.cookie.indexOf(search);
    if (offset != -1) { // if cookie exists
      offset += search.length;
      // set index of beginning of value
      end = document.cookie.indexOf(";", offset);
      // set index of end of cookie value
      if (end == -1)
        end = document.cookie.length;
      return unescape(document.cookie.substring(offset, end));
    }
  }
  return "";
}
