/********************************************************************************************
* BlueShoes Framework; This file is part of the php application framework.
* NOTE: This code is stripped (obfuscated). To get the clean documented code goto 
*       www.blueshoes.org and register for the free open source *DEVELOPER* version or 
*       buy the commercial version.
*       
*       In case you've already got the developer version, then this is one of the few 
*       packages/classes that is only available to *PAYING* customers.
*       To get it go to www.blueshoes.org and buy a commercial version.
* 
* @copyright www.blueshoes.org
* @author    sam blum <sam-at-blueshoes>
* @author    Andrej Arn <andrej-at-blueshoes>
*/
function bs_isValidDate(year, month, day) 
{
  if (year >= 1970) 
  {
    var tDate = new Date(year, month -1, day);
    if (day   != tDate.getDate())               
      return false;
    if (month != (tDate.getMonth() +1))         
      return false;
    if (year  != bs_fixYear(tDate.getYear())) 
      return false;
  } 
  else 
  {
    if ((day   < 1)    || (day   > 31))   
      return false;
    if ((month < 1)    || (month > 12))   
      return false;
    if ((year  < 1000) || (year  > 3000)) 
      return false;
  }
  if (day > 28) 
  {
    if (bs_getNumberOfDays(year, month) < day) 
      return false;
  }
  return true;
}

function bs_fixYear(year) 
{
  if (year < 100) 
  {
    year = parseInt('19' + year, 10);
  } 
  else if ((year >= 100) && (year < 110)) 
  {
    year = parseInt(200 + '' + year.toString().substr(2, 1), 10);
  }
  return year;
}

function bs_getNumberOfDays(year, month) 
{
  month = parseInt(month, 10);
  switch (month) 
  {
    case 2:
      if (bs_isLeapYear(year)) 
        return 29;
      return 28;
      break;
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
      return 31;
      break;
    default:
      return 30;
  }
}

function bs_dateToIsoDate(someDate, allowTime) 
{
  if (typeof(allowTime) == 'undefined') 
    allowTime = true;
  var someTime = '';  
  someDate = someDate.replace(/^\s*/, '');
 	someDate = someDate.replace(/\s*$/, '');
  if (someDate.length <  6) 
    return false;
  if (someDate.length > 10) 
  {
    if (allowTime) 
    {
      var tmp = someDate.split(' ', 2);
      if (tmp.length == 2) 
      {
        someDate = tmp[0];
        if ((tmp[1].length == 5) || (tmp[1].length == 8)) 
        {
          someTime = tmp[1];
        }
      }
    } 
    else 
    {
      someDate = someDate.substr(0, 10);
    }
  }
  if (someDate.indexOf('/') >= 0) 
  {
    var chunks = someDate.split('.');
    if (chunks.length != 3) 
      return false;
    var day   = parseInt(chunks[1], 10);
    var month = parseInt(chunks[0], 10);
    var year  = parseInt(chunks[2], 10);
  } 
  else if (someDate.indexOf('-') >= 0) 
  {
    var chunks = someDate.split('-');
    if (chunks.length != 3) 
      return false;
    var day   = parseInt(chunks[2], 10);
    var month = parseInt(chunks[1], 10);
    var year  = parseInt(chunks[0], 10);
  } 
  else if (someDate.indexOf('/') >= 0) 
  {
    var chunks = someDate.split('/');
    if (chunks.length != 3) 
      return false;
    var day   = parseInt(chunks[0], 10);
    var month = parseInt(chunks[1], 10);
    var year  = parseInt(chunks[2], 10);
  } 
  else 
  {
    return false;
  }
  if (year < 100) 
  {
    if (year < 30) 
    {
      year += 2000;
    } 
    else 
    {
      year += 1900;
    }
  }
  if (!bs_isValidDate(year, month, day)) 
    return false;
  var ret = '';
  ret += year + '-';
  if (month < 10) 
    ret += '0';
  ret += month + '-';
  if (day < 10) 
    ret += '0';
  ret += day;
  if (someTime != '') 
  {
    ret += ' ' + someTime;
  }
  return ret;
}
  
function bs_isLeapYear(year) 
{
  return (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0));
}

