/**************************************************************************************
 *  CCwiDate                                                                          *
 **************************************************************************************/

CCwiDate = function(Style, StyleError)
{
  this.Style = Style;
  this.StyleError = StyleError;
  
  this.DaysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  this.bValidDate = false;
  this.iYear  = 0;
  this.iMonth = 0;
  this.iDay   = 0;
  
  this.oHtmlInput = "";
  this.Type = "com.ivengi.CCwiDate.object"; //some reflection
}

CCwiDate.prototype.IsType = function()
{
  return this.Type;
}

CCwiDate.prototype.GetDaysInMonth = function(iYear, iMonth)
{
	if (iMonth == 2) {
  	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((iYear % 4 == 0) && ( (!(iYear % 100 == 0)) || (iYear % 400 == 0))) ? 29 : 28 );
  }
  return this.DaysInMonth[iMonth-1];
}

CCwiDate.prototype.ParseDateString = function(sDate)
{
  // Input iso format : yyyy-mm-dd | yy-mm-dd 
  // Delimiter can be : dot, comma or dash
  sDate = sDate.split(" ").join("");              // remove spaces
  sDate = sDate.split(".").join("-");             // replace dots
  sDate = sDate.split("/").join("-");             // replace forslash
  sDate = sDate.split("\\").join("-");            // replace backslash
  
  return sDate.split("-");                        // split string on dash
}

CCwiDate.prototype.ValidateDate = function()
{  
  // alert("ValidateDate : " + this.iYear + ", " + this.iMonth + ", " + this.iDay);
  
  if ((this.iYear <= 0) || (this.iMonth <= 0) || (this.iDay <= 0)) {
    return false;
  }
 
  var Now = new Date()
      
  if (this.iYear >= 1000) {
    // 4+ digit notation
    if ((this.iYear > 9999) || (this.iYear < Now.getFullYear())) {
      return false;
    }
  } else {
    // 1, 2 or 3 digit notation
    this.iYear += 2000;
    if (this.iYear < Now.getFullYear()) {
      return false;
    }
  }
  
  
  if (this.iMonth > 12) {
    return false;
  }
  
  var iDaysInThisMonth = this.GetDaysInMonth(this.iYear, this.iMonth);
  
  if (this.iDay > iDaysInThisMonth) {  
    this.iDay = this.iDay - iDaysInThisMonth;
    this.iMonth++;
    if (this.iMonth > 12) {
      this.iMonth = 1;
      this.iYear++;
    }
    var iDaysInThisMonth = this.GetDaysInMonth(this.iYear, this.iMonth);
    if (this.iDay > iDaysInThisMonth) {
      this.iDay = iDaysInThisMonth;
    }
  }
  this.bValidDate = true;
  return true;
}

CCwiDate.prototype.ParseDate = function(DateFieldId)
{
  this.oHtmlInput = document.getElementById(DateFieldId);
  if (!this.oHtmlInput) {
    return false;
  }
  
  var bRetValue = false;
  var sDate = this.oHtmlInput.value;
  var asDate = this.ParseDateString(sDate);
  
  if ((sDate.length != 0) && (asDate.length == 3)) {
    this.iYear  = parseInt(asDate[2], 10);
    this.iMonth = parseInt(asDate[1], 10);
    this.iDay   = parseInt(asDate[0], 10);
    bRetValue   = this.ValidateDate();
  }
  
  if (bRetValue) {
    this.oHtmlInput.style.border = this.Style;
  } else {
    this.oHtmlInput.style.border = this.StyleError; 
  }
  return bRetValue;
}

CCwiDate.prototype.WriteDate = function()
{
  if (!this.bValidDate) {
    return false;
  }

  var sResult = "";
  if (this.iDay < 10) {
    sResult += "0";  
  }
  sResult += this.iDay;
  sResult += "-";
  if (this.iMonth < 10) {
    sResult += "0";  
  }
  sResult += this.iMonth;
  sResult += "-";
  sResult += this.iYear;
  this.oHtmlInput.value = sResult;
  return true;
}

CCwiDate.prototype.TimeSpanCheck = function(oCCwiDateEnd, AddDays, bStartTimeChange)
{
  // If this is before oCCwiDateEnd, add AddDays to oCCwiDateEnd
  if (oCCwiDateEnd.IsType() == this.IsType()) {
    oNow   = new Date();
    oStart = new Date(this.iYear, this.iMonth-1, this.iDay, 23, 59, 59);
    oEnd   = new Date(oCCwiDateEnd.iYear, oCCwiDateEnd.iMonth-1, oCCwiDateEnd.iDay, 23, 59, 59);
    this.oHtmlInput.style.border = this.Style;
    oCCwiDateEnd.oHtmlInput.style.border = this.Style;

// alert("Now : " + oNow.toGMTString() + "\nStart : " + oStart.toGMTString() + ", Stop : " + oEnd.toGMTString());

    if (oStart.getTime() < oNow.getTime()) {
      this.oHtmlInput.style.border = this.StyleError;
      return false;
    } else {
      if (oEnd.getTime() <= oStart.getTime()) {
        oCCwiDateEnd.iYear = this.iYear;
        oCCwiDateEnd.iMonth = this.iMonth
        oCCwiDateEnd.iDay = this.iDay + 1;
        oCCwiDateEnd.ValidateDate();
        oCCwiDateEnd.WriteDate();
      } else if ((oCCwiDateEnd.iDay > (this.iDay + 1)) && (bStartTimeChange == true)) {
      	oCCwiDateEnd.iYear = this.iYear;
        oCCwiDateEnd.iMonth = this.iMonth
        oCCwiDateEnd.iDay = this.iDay + 1;
        oCCwiDateEnd.ValidateDate();
        oCCwiDateEnd.WriteDate();
			}
    }
  }
  return true;
}

CCwiDate.prototype.CheckDates = function(oCCwiDateEnd)
{
  if (oCCwiDateEnd.IsType() == this.IsType()) {
    oNow   = new Date();
    oStart = new Date(this.iYear, this.iMonth-1, this.iDay, 23, 59, 59);
    oEnd   = new Date(oCCwiDateEnd.iYear, oCCwiDateEnd.iMonth-1, oCCwiDateEnd.iDay);
   
    this.oHtmlInput.style.border = this.Style;
    oCCwiDateEnd.oHtmlInput.style.border = this.Style;
        
	 if (oStart.getTime() < oNow.getTime()) {
      this.oHtmlInput.style.border = this.StyleError;
      return false;
    }
    
    if (oEnd.getTime() < oStart.getTime()) {
      oCCwiDateEnd.oHtmlInput.style.border = this.StyleError;
      return false;
    }
  }
  return true;
}

