// Copyright (C) 2007 Alan Ardzinov, Saint-Petersburg, Russia 
// http://www.terchy.com, ardalan@mail.ru, ICQ 56320602

/*
function dt_GetCurrDate()
function MyTest(sPar1)
function dt_Pause (iSeconds)
function dt_GetSeparatorForThisPosition(sFormat, iPos)
function dt_GetPartIndexForThisPosition(sFormat, iPos)
function dt_GetFormatPartForThisPosition(sFormat, iPos)
function dt_Format (iDate, sFormat)
function dt_Diff (dDate1, dDate2, sDatePart)
function dt_DecriptFormat (sFormat)
function dt_Create (iYear, iMonth, iDay, iHour, iMinute, iSecond)
function dt_Check (sDate, sFormat)
function dt_Add (dDate, iNumber, iDatePart)

*/
sDT_FORMAT = "";
/*
  to define this please input:
  sDT_FORMAT = "<#App_sDateFormat#>";
  after the js libraries

*/

ixDT_DD   = 1;
ixDT_D    = 2;
ixDT_MM   = 3;
ixDT_M    = 4;
ixDT_YYYY = 5;
ixDT_YY   = 6;
ixDT_HH   = 7;
ixDT_H    = 8;
ixDT_NN   = 9;
ixDT_N    = 10;
ixDT_SS   = 11;
ixDT_S    = 12;

aDT_PARTS = new Array();

aDT_PARTS[ixDT_DD]   = "dd";
aDT_PARTS[ixDT_D]    = "d";
aDT_PARTS[ixDT_MM]   = "mm";
aDT_PARTS[ixDT_M]    = "m";
aDT_PARTS[ixDT_YYYY] = "yyyy";
aDT_PARTS[ixDT_YY]   = "yy";
aDT_PARTS[ixDT_HH]   = "hh";
aDT_PARTS[ixDT_H]    = "h";
aDT_PARTS[ixDT_NN]   = "nn";
aDT_PARTS[ixDT_N]    = "n";
aDT_PARTS[ixDT_SS]   = "ss";
aDT_PARTS[ixDT_S]    = "s";

//  количества миллисекунд в различных частях даты/времени
aDT_MSEK = new Array();
aDT_MSEK[ixDT_DD]   = 24*60*60*1000;
aDT_MSEK[ixDT_D]    = 24*60*60*1000;
aDT_MSEK[ixDT_HH]   = 60*60*1000;
aDT_MSEK[ixDT_H]    = 60*60*1000;
aDT_MSEK[ixDT_NN]   = 60*1000;
aDT_MSEK[ixDT_N]    = 60*1000;
aDT_MSEK[ixDT_SS]   = 1000;
aDT_MSEK[ixDT_S]    = 1000;

//--------------------------------------------------------------
function dt_GetCurrDate()
{
  d = new Date();
  return d;  
}

//--------------------------------------------------------------
function dt_Format (iDate, sFormat)
{
  var iNextPos = 1; 
  var sStr = "";    
  var iIndex;

  var iYear   = iDate.getFullYear();
  var iMonth  = iDate.getMonth() + 1;
  var iDay    = iDate.getDate();
  var iHour   = iDate.getHours();
  var iMinute = iDate.getMinutes();
  var iSecond = iDate.getSeconds();

  while (iNextPos <= sFormat.length)
  {
    iIndex = dt_GetPartIndexForThisPosition(sFormat, iNextPos)

    if ( iIndex != 0 )
    {
      switch (iIndex) 
      {
      case ixDT_DD:
        sStr += s_AddLeadZeros(iDay, 2);
        break;
      case ixDT_D:
        sStr += iDay;
        break;
      case ixDT_MM:
        sStr += s_AddLeadZeros(iMonth, 2);
        break;
      case ixDT_M:
        sStr += iMonth;
        break;
      case ixDT_YYYY:
        sStr += iYear;
        break;
      case ixDT_YY:
        sStr += s_Mid(iYear, 3);
        break;

      case ixDT_HH:
        sStr += s_AddLeadZeros(iHour, 2);
        break;
      case ixDT_H:
        sStr += iHour;
        break;
      case ixDT_NN:
        sStr += s_AddLeadZeros(iMinute, 2);
        break;
      case ixDT_N:
        sStr += iMinute;
        break;
      case ixDT_SS:
        sStr += s_AddLeadZeros(iSecond, 2);
        break;
      case ixDT_S:
        sStr += iSecond;
        break;

      }
      
      iNextPos = iNextPos + aDT_PARTS[iIndex].length;
    }
    else
    {
      sStr = sStr + sFormat.charAt(iNextPos - 1);

      iNextPos = iNextPos + 1;
    }
  }

  return sStr;

}

//--------------------------------------------------------------
/*
gsDay = "день";
gsMonth = "месяц";
gsYear4 = "год(4 знака)";
gsYear2 = "год(2 знака)";
gsHours = "часы";
gsMinutes = "минуты";
gsSeconds = "секунды";
*/
function dt_DecriptFormat (sFormat)
{
  var iNextPos = 1; 
  var sStr = "";    
  var iIndex;

  while (iNextPos <= sFormat.length)
  {
    iIndex = dt_GetPartIndexForThisPosition(sFormat, iNextPos)

    if ( iIndex != 0 )
    {
      switch (iIndex) 
      {
      case ixDT_DD:
      case ixDT_D:
        sStr += gsDay;
        break;
      case ixDT_MM:
      case ixDT_M:
        sStr += gsMonth;
        break;
      case ixDT_YYYY:
        sStr += gsYear4;
        break;
      case ixDT_YY:
        sStr += gsYear2;
        break;
      case ixDT_HH:
      case ixDT_H:
        sStr += gsHours;
        break;
      case ixDT_NN:
      case ixDT_N:
        sStr += gsMinutes;
        break;
      case ixDT_SS:
      case ixDT_S:
        sStr += gsSeconds;
        break;

      }
      
      iNextPos = iNextPos + aDT_PARTS[iIndex].length;
    }
    else
    {
      sStr = sStr + sFormat.charAt(iNextPos - 1);

      iNextPos = iNextPos + 1;
    }
  }

  return sStr;

}

//-------------------------------------------------------------

function dt_GetPartIndexForThisPosition(sFormat, iPos)
{
  var i;

  for (i = 1; i < aDT_PARTS.length; i++)
  {
    if ( s_Mid(sFormat, iPos, aDT_PARTS[i].length) == aDT_PARTS[i] )
    {
      return i;
    }
  }
  return 0;
}

//-------------------------------------------------------------

function dt_GetFormatPartForThisPosition(sFormat, iPos)
{
  var i;

  for (i = 1; i < aDT_PARTS.length; i++)
  {
    if ( s_Mid(sFormat, iPos, aDT_PARTS[i].length) == aDT_PARTS[i] )
    {
      return aDT_PARTS[i];
    }
  }
  return "";
}

//-------------------------------------------------------------

function dt_GetSeparatorForThisPosition(sFormat, iPos)
{
  var i;
  
  var sSeparator = "";
  
  while (iPos <= sFormat.length)
  {
    if ( dt_GetFormatPartForThisPosition(sFormat, iPos) != "" )
    {
      break; 
    }
    else
    {
      sSeparator = sSeparator + s_Mid(sFormat, iPos, 1);
    }
    iPos ++;
  }

  return sSeparator;
}

//-------------------------------------------------------------

function MyTest(sPar1)
{
  MyTest.arguments[0] = MyTest.arguments[0] + 1;
}

//---------------------------------------------------------------
function dt_Check (sDate, sFormat)
{
// last update 2005-11-12
// last update 2005-04-21
// last update 2007-01-06:
/*
  iNewDate_Year   = dDate.getYear();
  was changed to :
  iNewDate_Year   = dDate.getFullYear();
*/

  var aDateParts = new Array;
  var aDateValues = new Array;
  var aFormatParts = new Array;
  var aFormatPartIsSelector = new Array;
  var sNextFormatPart = "";

  var i;
  var iNextPos = 1;

  var j = 0;


  while (iNextPos <= sFormat.length)
  {
    sNextFormatPart = dt_GetFormatPartForThisPosition(sFormat, iNextPos)

    if ( sNextFormatPart != "" )
    {
      aFormatParts[j] = sNextFormatPart;
      aFormatPartIsSelector[j] = false;
    }
    else
    {
      sNextFormatPart = dt_GetSeparatorForThisPosition(sFormat, iNextPos)
      aFormatParts[j] = sNextFormatPart;
      aFormatPartIsSelector[j] = true;
    }

    j++;
    iNextPos = iNextPos + sNextFormatPart.length;

  }

  
  i = 0;

/*    
alert(aFormatPartIsSelector);
alert(aFormatParts)  ;
*/
  // now we get real values of date parts  
  for (j = 0; j <= aFormatParts.length - 1; j++)
  {
/*
alert("FormatPartIsSelector " + j + "=" + aFormatPartIsSelector[j]);    
alert("FormatPart " + j + "=" + aFormatParts[j]);    
alert("sDate before =" + sDate);
*/
    if (aFormatPartIsSelector[j])
    {
      sDate = s_Mid(sDate, aFormatParts[j].length + 1);
    }
    else
    {
      // this part of format is datepart
      if (j == aFormatParts.length - 1)
      {
        // this part of format is datepart 
        // and it is the last part
        aDateValues[i] = sDate;
        aDateParts[i] = aFormatParts[j];
//alert("aDateValues[" + i + "] A =" + aDateValues[i]);
        i ++;
      }
      else
      {
        if (aFormatPartIsSelector[j + 1])
        {
          aDateValues[i] = s_GetToken (sDate, aFormatParts[j + 1]);
          aDateParts[i] = aFormatParts[j];
//alert("aDateValues[" + i + "] B =" + aDateValues[i]);

          sDate = s_Mid(sDate, aDateValues[i].length + 1);
          i ++;
        }
        else
        {
          aDateValues[i] = s_Mid(sDate, 1, aFormatParts[j].length);
          aDateParts[i] = aFormatParts[j];

//alert("aDateValues[" + i + "] C =" + aDateValues[i]);

          sDate = s_Mid(sDate, aDateValues[i].length + 1);
          i ++;
        }
      }
    }

//alert("sDate after =" + sDate);

  }

//alert(aDateValues);
//alert(aDateParts);

  for (i = 0; i <= aDateValues.length - 1; i++)
  {
    // check that date parts are integers

    if (aDateValues[i] != parseInt(s_RemoveLeadZeros(aDateValues[i])))
    {
//alert ("not integer "  +  aDateValues[i]);    
      return false;
    }
    else
    {
      aDateValues[i] = parseInt(s_RemoveLeadZeros(aDateValues[i]));
    }
  }

//alert(aDateValues);
  
  var iYear   = s_Init (iYear, 0);
  var iMonth  = s_Init (iMonth, 0);
  var iDay    = s_Init (iDay, 0);
  var iHour   = s_Init (iHour, 0);
  var iMinute = s_Init (iMinute, 0);
  var iSecond = s_Init (iSecond, 0);

  for (i = 0; i <= aDateParts.length - 1 ; i++)
  {
//alert('aDateParts[' + i + ']' + aDateParts[i]);  
//alert('aDateValues[' + i + ']' + aDateValues[i]);  

    switch(aDateParts[i])
    {
      case aDT_PARTS[ixDT_DD]:
      case aDT_PARTS[ixDT_D]:
        if (typeof(aDateValues[i]) != "undefined" && aDateValues[i] != "")
        iDay = aDateValues[i];
        break;
      case aDT_PARTS[ixDT_MM]:
      case aDT_PARTS[ixDT_M]:
        if (typeof(aDateValues[i]) != "undefined" && aDateValues[i] != "")
        iMonth = aDateValues[i];
        break;
      case aDT_PARTS[ixDT_YYYY]:
      case aDT_PARTS[ixDT_YY]:
        if (typeof(aDateValues[i]) != "undefined" && aDateValues[i] != "")
        iYear = aDateValues[i];
        break;
      case aDT_PARTS[ixDT_HH]:
      case aDT_PARTS[ixDT_H]:
        if (typeof(aDateValues[i]) != "undefined" && aDateValues[i] != "")
        iHour = aDateValues[i];
        break;
      case aDT_PARTS[ixDT_NN]:
      case aDT_PARTS[ixDT_N]:
        if (typeof(aDateValues[i]) != "undefined" && aDateValues[i] != "")
        iMinute = aDateValues[i];
        break;
      case aDT_PARTS[ixDT_SS]:
      case aDT_PARTS[ixDT_S]:
        if (typeof(aDateValues[i]) != "undefined" && aDateValues[i] != "")
        iSecond = aDateValues[i];
        break;
    }
  }

/*
alert(
"iYear=" + iYear + "\n" +
"iMonth=" + iMonth + "\n" +
"iDay=" + iDay + "\n" +
"iHour=" + iHour + "\n" +
"iMinute=" + iMinute + "\n" +
"iSecond=" + iSecond )
*/


  dDate = new Date( iYear, iMonth - 1, iDay, iHour, iMinute, iSecond );

//alert("dDate=" + dDate);

  iNewDate_Day    = dDate.getDate();
  iNewDate_Month  = dDate.getMonth() + 1;
  iNewDate_Year   = dDate.getFullYear();
  iNewDate_Hours   = dDate.getHours();
  iNewDate_Minutes = dDate.getMinutes();
  iNewDate_Seconds = dDate.getSeconds();

  if (iNewDate_Year > 30 && iNewDate_Year < 100) {iNewDate_Year = 1900 + iNewDate_Year;}  

  bResult =  (
             (iDay   == iNewDate_Day ) && 
             (iMonth == iNewDate_Month) && 
             (iYear  == iNewDate_Year) &&
             (iHour  == iNewDate_Hours) &&
             (iMinute  == iNewDate_Minutes) &&
             (iSecond  == iNewDate_Seconds)
             );

/*
alert(
"User date = Composed date\n" +
"iYear: " + iYear + "=" + iNewDate_Year +  "\n" +
"iMonth: " + iMonth + "=" + iNewDate_Month +  "\n" +
"iDay: " + iDay + "=" + iNewDate_Day +  "\n" +
"iHour: " + iHour + "=" + iNewDate_Hours +  "\n" +
"iMinute: " + iMinute + "=" + iNewDate_Minutes +  "\n" +
"iSecond: " + iSecond + "=" + iNewDate_Seconds)
*/

  if (!bResult) {return bResult;} else {return dDate;}
}

/*---------------------------------------------------------*/


function dt_Create (iYear, iMonth, iDay, iHour, iMinute, iSecond)
{
  iMonth  = s_Init(iMonth, 1);
  iDay    = s_Init(iDay, 1);
  iHour   = s_Init(iHour, 0);
  iMinute = s_Init(iMinute, 0);
  iSecond = s_Init(iSecond, 0);
  var dDate = new Date(iYear, iMonth - 1, iDay, iHour, iMinute, iSecond);
  return dDate;
}

//---------------------------------------------------------------

function dt_Diff (dDate1, dDate2, sDatePart)
{
//alert ("sad");    

// last update 2005-11-12
  sDatePart = s_Init(sDatePart, "");

  iDateDiff = dDate1 - dDate2;
  if (sDatePart == "" || iDateDiff < 0) {return iDateDiff;}
  dDate = new Date();
  dDate.setTime(iDateDiff) 
  
  switch(sDatePart)
  {
    case aDT_PARTS[ixDT_DD]:
    case aDT_PARTS[ixDT_D]:
      iDateDiff    = dDate.getDate();
      break;
    case aDT_PARTS[ixDT_MM]:
    case aDT_PARTS[ixDT_M]:
      iDateDiff  = dDate.getMonth() + 1;
      break;
    case aDT_PARTS[ixDT_YYYY]:
    case aDT_PARTS[ixDT_YY]:
      iDateDiff   = dDate.getYear() - 69;
      break;
    case aDT_PARTS[ixDT_HH]:
    case aDT_PARTS[ixDT_H]:
      iDateDiff   = dDate.getHours();
      break;
    case aDT_PARTS[ixDT_NN]:
    case aDT_PARTS[ixDT_N]:
      iDateDiff = dDate.getMinutes();
      break;
    case aDT_PARTS[ixDT_SS]:
    case aDT_PARTS[ixDT_S]:
      iDateDiff = dDate.getSeconds();
      break;
  }
  iDateDiff = iDateDiff - 1;
  return (iDateDiff);
}


//---------------------------------------------------------------

function dt_Add (dDate, iNumber, iDatePart)
{
  // iDatePart is Days as default
  iDatePart = s_Init(iDatePart, ixDT_D);
  
  var iYear   = dDate.getYear();
  var iMonth  = dDate.getMonth() + 1;
  var iDay    = dDate.getDate();
  var iHour   = dDate.getHours();
  var iMinute = dDate.getMinutes();
  var iSecond = dDate.getSeconds();
  
  switch (iDatePart ) 
  {
  case ixDT_SS:
  case ixDT_S:
  case ixDT_NN:
  case ixDT_N:
  case ixDT_HH:
  case ixDT_H:
  case ixDT_DD:
  case ixDT_D:

    iCountOfMillisecondsIndDate = dDate.getTime();
    iCountOfMillisecondsToAdd = aDT_MSEK[iDatePart] * iNumber;
    dDate.setTime(iCountOfMillisecondsIndDate + iCountOfMillisecondsToAdd);

    break;
  case ixDT_MM:
  case ixDT_M:
  alert(iMonth);
    iMonth = iMonth + iNumber;
    dDate = new Date(iYear, iMonth - 1, iDay, iHour, iMinute, iSecond);
    break;
  case ixDT_YYYY:
  case ixDT_YY:
    iYear = iYear + iNumber;
    dDate = new Date(iYear, iMonth - 1, iDay, iHour, iMinute, iSecond);
    break;
  }

  return dDate;

}

//--------------------------------------------------------------

function dt_Pause (iSeconds)
{
  dStartDate = new Date();
  dEndDate = dt_DataAdd (dStartDate, iSeconds, ixDT_S);

  dStartDate = new Date();
  while (dStartDate <= dEndDate)
  {
    dStartDate = new Date();
  }
  
}

/*---------------------------------------------------------*/

function dt_CheckFromTo(oForm, sFromName, sToName, sDateFormat, sMsg)
{
  var dDateFrom = IsDate(oForm.elements[sFromName], sDateFormat);
  if (!dDateFrom) {return false;}

  var dDateTo = IsDate(oForm.elements[sToName], sDateFormat);
  if (!dDateTo) {return false;}

  if (oForm.elements[sFromName].value != "" && oForm.elements[sToName].value != "") 
  {
    var i = dt_Diff (dDateFrom, dDateTo);

    if (i >= 0) 
    {
      alert(sMsg);
      oForm.elements[sFromName].focus();
      return false;
    }
  }
  return true;

}

