// Requiere String.js

//---------------------------------------------
// Valida si los parámetros componen una fecha
//---------------------------------------------
function IsDate(iYear, iMonth, iDay)
{
  iYear = parseInt(iYear, 10);
  if (isNaN(iYear) || iYear < 1900 || iYear > 2100)
    return false;

  iMonth = parseInt(iMonth, 10);
  if (isNaN(iMonth) || iMonth < 1 || iMonth > 12)
    return false;

  var iLastDay = GetDaysInMonth(iYear, iMonth);

  if (isNaN(iDay) || iDay < 1 || iDay > 31)
    return false;

  iDay = parseInt(iDay, 10);
  return (iDay > 0 && iDay <= iLastDay && !isNaN(iDay))
}
//--------------------
// Formatea una fecha
//--------------------
function FormatDate(iYear, iMonth, iDay, sFormat)
{
  if (FormatDate.lenght = 3)
    sFormat = "yyyy-mm-dd";

  if (IsDate(iYear, iMonth, iDay))
    switch (sFormat.toLowerCase())
    {
    case "dd/mm/yyyy":
      return LPad(iDay, 2, "0") + "/" + LPad(iMonth, 2, "0") + "/" + LPad(iYear, 4, "0");
    case "mm/dd/yyyy":
      return LPad(iMonth, 2, "0") + "/" + LPad(iDay, 2, "0") + "/" + LPad(iYear, 4, "0");
    case "mmm-yyyy":
      return MonthName(iMonth) + "-" + LPad(iYear, 4, "0");
    case "yyyy-mm-dd":
      return LPad(iYear, 4, "0") + "-" + LPad(iMonth, 2, "0") + "-" + LPad(iDay, 2, "0");
    }
  return "";
}
//------------------------------------------------
// Obtiene la fecha del día en formato yyyy-mm-dd
//------------------------------------------------
function GetDate()
{
  var dtNow = new Date();
  return dtNow.getFullYear() + "-" + LPad(dtNow.getMonth() + 1, 2, "0") + "-"
       + LPad(dtNow.getDate(), 2, "0");
}
//---------------------------
// Obtiene el nombre del mes
//---------------------------
function MonthName(iMonth)
{
  switch (parseInt(iMonth, 10))
  {
  case 1:
    return "Ene";
  case 2:
    return "Feb";
  case 3:
    return "Mar";
  case 4:
    return "Abr";
  case 5:
    return "May";
  case 6:
    return "Jun";
  case 7:
    return "Jul";
  case 8:
    return "Ago";
  case 9:
    return "Sep";
  case 10:
    return "Oct";
  case 11:
    return "Nov";
  case 12:
    return "Dic";
  }
  return "";
}
//-----------------------------------
// Obtiene el numero de dias del mes
//-----------------------------------
function GetDaysInMonth(iYear, iMonth)
{
  if (iMonth == 1 || iMonth == 3 || iMonth == 5 || iMonth == 7 ||
      iMonth == 8 || iMonth == 10 || iMonth == 12)
    return 31;
  else if (iMonth == 4 || iMonth == 6 || iMonth == 9 || iMonth == 11)
    return 30;
  else if (iMonth == 2)
    if (IsLeapYear(iYear))
      return 29;
    else
      return 28;
  return 0;
}
//--------------------------------
// Verifica si un año es bisiesto
//--------------------------------
function IsLeapYear(iYear)
{
  if (iYear % 4 == 0 && iYear % 100 != 0 || iYear % 400 == 0)
    return true;
  else
    return false;
}
//--------------------------------
// Valida el rango de las fechas
//--------------------------------
function DateRange(YearTo,YearFrom,MonthTo,MonthFrom,DayTo,DayFrom)
{
	var fechaDesde = new Date(YearFrom.value, MonthFrom.value - 1, DayFrom.value)
	var fechaHasta = new Date(YearTo.value, MonthTo.value - 1, DayTo.value)

	if (fechaHasta < fechaDesde)
	{
		return false;
	}
	else
		return true;
}
//--------------------------------
// Valida el rango de las fechas
//--------------------------------
function IsValidDMY(sDate)
{
	var day;
	var month;
	var year;
	var iPos;
	var temp;

	iPos = sDate.indexOf("/");
	if(iPos < 0)
		iPos = sDate.indexOf("-");
	if(iPos < 0)
		return false;

	day = sDate.substring(0, iPos);
	temp = sDate.substring(iPos + 1)
	iPos = temp.indexOf("/")
	if(iPos < 0)
		iPos = temp.indexOf("-");

	if(iPos < 0)
		return false;

	month = temp.substring(0, iPos);
	year = temp.substring(iPos + 1);
	if(year.length != 4)
		return false;
	return IsDate(year, month, day);
}
//-------------------------------------------------
// Retorna el día de la semana, siendo domingo = 0
//-------------------------------------------------

function WeekDay(year, month, day)
{
	var _Date = new Date();
	_Date.setDate(day);
	_Date.setMonth(parseInt(month,10)-1);
	_Date.setFullYear(year);
	return _Date.getDay();
}

//-------------------------------------------------
// Retorna la cantidad de dias de esa fecha
//-------------------------------------------------
function GetDiffDays (year, month, day)
{
	var today = new Date();
	var date = new Date();
	date.setDate(day);
	date.setMonth(month - 1);
	date.setFullYear(year);
	return (today/86400000 - date/86400000);
}


function Saludo1()
{
   var Time = new Date();
   var frase = '';
   // HORA //
   var hora = Time.getHours();
   if (hora < 12){ frase = 'Buenos D&iacute;as  ';}
   if (hora >= 12 & hora <= 19){ frase = 'Buenas Tardes  ';}
   if (hora > 19){ frase = 'Buenas Noches  ';}
   return frase;
}

function Saludo2()
{
   var Time = new Date();
   // FECHA //
   var diasemana = Time.getDay()
   var dia = Time.getDate()
   var mes = Time.getMonth()
   // DIA DE LA SEMANA //
   switch (diasemana){
      case 0 :
		 diasemana = 'Domingo';
		 break;
      case 1 :
		diasemana = 'Lunes';
		break;
   	case 2 :
		diasemana = 'Martes';
		break;
   	case 3 :
		diasemana = 'Mi&eacute;rcoles';
		break;
   	case 4 :
		diasemana = 'Jueves';
		break;
   	case 5 :
		diasemana = 'Viernes';
		break;
   	case 6 :
		diasemana = 'Sábado';
		break;
   }
   // MES //
   switch (mes){
   	case 0 :
		mes = 'Enero';
		break;
   	case 1 :
		mes = 'Febrero';
		break;
   	case 2 :
		mes = 'Marzo';
		break;
   	case 3 :
		mes = 'Abril';
		break;
   	case 4 :
		mes = 'Mayo';
		break;
   	case 5 :
		mes = 'Junio';
		break;
   	case 6 :
   		mes = 'Julio';
   		break;
   	case 7 :
   		mes = 'Agosto';
		break;
   	case 8 :
		mes = 'Septiembre';
		break;
	  case 9 :
		mes = 'Octubre';
		break;
   	case 10 :
		mes = 'Noviembre';
		break;
   	case 11 :
		mes = 'Diciembre';
		break;
   }
   fecha = diasemana + " " + dia + " de " + mes;

   return fecha;
}

function Saludo()
{
   var Time = new Date();
   var frase = '';
   // HORA //
   var hora = Time.getHours();
   if (hora < 12){ frase = 'Buenos D&iacute;as';}
   if (hora >= 12 & hora <= 19){ frase = 'Buenas Tardes';}
   if (hora > 19){ frase = 'Buenas Noches';}

   // FECHA //
   var diasemana = Time.getDay()
   var dia = Time.getDate()
   var mes = Time.getMonth()
   // DIA DE LA SEMANA //
   switch (diasemana){
      case 0 :
		 diasemana = 'Domingo';
		 break;
      case 1 :
		diasemana = 'Lunes';
		break;
   	case 2 :
		diasemana = 'Martes';
		break;
   	case 3 :
		diasemana = 'Mi&eacute;rcoles';
		break;
   	case 4 :
		diasemana = 'Jueves';
		break;
   	case 5 :
		diasemana = 'Viernes';
		break;
   	case 6 :
		diasemana = 'Sábado';
		break;
   }
   // MES //
   switch (mes){
   	case 0 :
		mes = 'Enero';
		break;
   	case 1 :
		mes = 'Febrero';
		break;
   	case 2 :
		mes = 'Marzo';
		break;
   	case 3 :
		mes = 'Abril';
		break;
   	case 4 :
		mes = 'Mayo';
		break;
   	case 5 :
		mes = 'Junio';
		break;
   	case 6 :
   		mes = 'Julio';
   		break;
   	case 7 :
   		mes = 'Agosto';
		break;
   	case 8 :
		mes = 'Septiembre';
		break;
	  case 9 :
		mes = 'Octubre';
		break;
   	case 10 :
		mes = 'Noviembre';
		break;
   	case 11 :
		mes = 'Diciembre';
		break;
   }
   fecha = diasemana  +  " "  +  dia  +  " de "  +  mes;

   return( frase + ', ' + fecha);
}
