/*
Must include <com.cs.forms.Validation.js>
*/

/**
 * @author Karl Cassar
 * @version 1.0.0 
 * @lastmodified 28/02/08
 */


if (!com) var com = new Object();
if (!com.cs) com.cs = new Object();
if (!com.cs.util) com.cs.util = new Object();
if (!com.cs.util.Date) com.cs.util.Date = new Object();

com.cs.util.Date.CheckRequirements = function()
{
    if (!com.cs.forms || !com.cs.forms.Validation)
    {
        alert('Please include [com.cs.forms.Validation] for [com.cs.util.Date] to work]');
    }
}


/**
 * This will create a url handler object
 * @param {Object} url Url to parse
 */
Date.prototype.test = function(fd)
{
    alert(fd);
}

Date.prototype.HasTime = false;

Date.prototype.ErrorTime = false;
Date.prototype.ErrorDate = false;
Date.prototype.ErrorDay = false;
Date.prototype.ErrorMonth = false;
Date.prototype.ErrorYear= false;
Date.prototype.ErrorHour = false;
Date.prototype.ErrorMinute = false;
Date.prototype.ErrorSecond = false;

Date.prototype.ObjectName = 'Date';


Date.prototype.ParseString = function(str,hasTime)
    {
        this.ErrorDate = this.ErrorDay = this.ErrorHour = this.ErrorMinute = this.ErrorMonth = this.ErrorSecond = this.ErrorTime = this.ErrorYear = false;
    
        var dd,mm,yyyy,HH,MM,SS;
        dd = mm = yyyy = HH = MM = SS = 0;
    
        if (!hasTime)
            hasTime = false;
        this.HasTime = hasTime;
        
        
        var dateDelimiter = "/";
        var timeDelimiter = ":";
        var date = str;
        if (date.indexOf(" ") != -1) date = date.substring(0, date.indexOf(" "));
        var dateItems = date.split(dateDelimiter);
        
        dd = dateItems[0];
        mm = dateItems[1];
        yyyy = dateItems[2];
        
        if (dd == "" || dd == null || !com.cs.forms.Validation.IsNumeric(dd) || dd.indexOf(".") != -1) {
            this.ErrorDay = true;
        }
        if (mm == "" || mm == null || !com.cs.forms.Validation.IsNumeric(mm) || mm.indexOf(".") != -1) {
            this.ErrorMonth = true;
        }
        if (yyyy == "" || yyyy == null || !com.cs.forms.Validation.IsNumeric(yyyy) || yyyy.indexOf(".") != -1) {
            this.ErrorYear = true;
        }
        
        this.ErrorDate = this.ErrorDay || this.ErrorMonth || this.ErrorYear;
        
        if (!this.ErrorDate) {
            
            if (this.HasTime && str.indexOf(" ") != -1) {
        
                var time = str.substring(str.indexOf(" ")+1);
                if (time.indexOf(" ") != -1) time = time.substring(0, time.indexOf(" "));
                var timeItems = time.split(timeDelimiter);
                
                HH = timeItems[0] || 0;
                MM = timeItems[1] || 0;
                SS = timeItems[2] || 0;
                
                if (HH != 0 && (HH == "" || HH == null || !com.cs.forms.Validation.IsNumeric(HH) || HH.indexOf(".") != -1)) {
                    this.ErrorHour = true;
                }
                if (MM != 0 && (MM == "" || MM == null || !com.cs.forms.Validation.IsNumeric(MM) || MM.indexOf(".") != -1)) {
                    this.ErrorMinute = true;
                }
                if (SS != 0 && (SS == "" || SS == null || !com.cs.forms.Validation.IsNumeric(SS) || SS.indexOf(".") != -1)) {
                    this.ErrorSecond = true;
                }
                
                this.ErrorTime = this.ErrorHour || this.ErrorMinute || this.ErrorSecond;
                
                if (!this.ErrorTime) {
                    HH = Number(HH);
                    MM = Number(MM);
                    SS = Number(SS);
                }
            }
            
            if (!this.ErrorTime) {
            
                dd = Number(dd);
                mm = Number(mm)-1;
                yyyy = Number(yyyy);
                var currYear = Number((new Date()).getFullYear().toString().substring(2));
                
                if (yyyy < 100) {
                
                    if (yyyy < currYear + 20) {
                        yyyy += 2000;
                    }
                    else {
                        yyyy += 1900;
                    }
                }
                var d = new Date();
                d.setFullYear(yyyy,mm,dd);
                
                d.setHours(HH, MM, SS, 0);
                
                if (d.getDate() != dd) this.ErrorDay = true;
                if (d.getMonth() != mm) this.ErrorMonth = true;
                if (d.getFullYear() != yyyy) this.ErrorYear = true;
                
                this.ErrorDate = this.ErrorDay || this.ErrorMonth || this.ErrorYear;
                
                
                if (d.getHours() != HH) this.ErrorHour = true;
                if (d.getMinutes() != MM) this.ErrorMinute = true;
                if (d.getSeconds() != SS) this.ErrorSecond = true;
                
                this.ErrorTime = this.ErrorHour || this.ErrorMinute || this.ErrorSecond;
                
            }
        
        }
        
        var ok = !this.ErrorDate  && !this.ErrorTime;
        if (ok) {
            this.setFullYear(d.getFullYear(), d.getMonth(), d.getDate());
            this.setHours(d.getHours(), d.getMinutes(), d.getSeconds());
           
        }
        
        
        return ok;
        
    
    }
    Date.prototype.ParseValues = function(day,month,year,hour,minute,second)
    {
        var _self = this;
//        _self = new Date();
        _self.HasTime = (hour != null);
        _self.setFullYear(year,month,day);
        if (_self.HasTime)
        {
            if (second == null)
                second = 0;
            _self.setHours(hour,minute,second,0);
        }
    }
    
    Date.prototype.toString = function()
    {
        var _self = this;
        var format = "#DD#/#MM#/#YYYY#";
        if (_self.HasTime)
        {
            format += " " + "#hhh#:#mm#";
        }
        return _self.CustomFormat(format);
    }
    //usage:  #YYYY# = year in 4-digit
    //          #YY# = year in 2-digit
    //          #MMMM# = month as 'January'
    //          #MMM# = month as 'Jan'
    //          #MM# = month as '01'
    //          #MM# = month as '1'
    //        #ampm# = am/pm, accordingly
    //        #hh# = hour in 24-hour format
    //        #h# = hour in 12-hour format
    //        #hh# = hour in 12-hour format, 2-digits
    //        #hhh# = hour in 24-hour format
    //        #m# = minute
    //        #mm# = minute, always in 2-digit
    //        #s# = second
    //        #ss# = second, as 2-digit
    //        #hh# = hour in 24-hour format
    //        #hh# = hour in 24-hour format
    
   Date.prototype.CustomFormat=function(formatString)
    { 
       var YYYY,YY,MMMM,MMM,MM,M,DDDD,DDD,DD,D,hhh,hh,h,mm,m,ss,s,ampm,dMod,th;
       YY = ((YYYY=this.getFullYear())+"").substr(2,2);
       MM = (M=this.getMonth()+1)<10?('0'+M):M;
       MMM = (MMMM=["January","February","March","April","May","June","July","August","September","October","November","December"][M-1]).substr(0,3);
       DD = (D=this.getDate())<10?('0'+D):D;
       DDD = (DDDD=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"][this.getDay()]).substr(0,3);
       th=(D>=10&&D<=20)?'th':((dMod=D%10)==1)?'st':(dMod==2)?'nd':(dMod==3)?'rd':'th';
       formatString = formatString.replace("#YYYY#",YYYY).replace("#YY#",YY).replace("#MMMM#",MMMM).replace("#MMM#",MMM).replace("#MM#",MM).replace("#M#",M).replace("#DDDD#",DDDD).replace("#DDD#",DDD).replace("#DD#",DD).replace("#D#",D).replace("#th#",th);

       h=(hhh=this.getHours());
       if (h==0) h=24;
       if (h>12) h-=12;
       hh = h<10?('0'+h):h;
       if (hhh < 10) hhh = "0" + hhh;
       ampm=hhh<12?'am':'pm';
       mm=(m=this.getMinutes())<10?('0'+m):m;
       ss=(s=this.getSeconds())<10?('0'+s):s;
       return formatString.replace("#hhh#",hhh).replace("#hh#",hh).replace("#h#",h).replace("#mm#",mm).replace("#m#",m).replace("#ss#",ss).replace("#s#",s).replace("#ampm#",ampm);
    } 
    //returns -1 if this is smaller than cmpTo, 0 if equal, 1 if this is greater than cmpTo
    Date.prototype.CompareTo = function (cmpTo)
    {
        var _self = this;
        if (cmpTo.ObjectName == "Date")
        {
            //var _self = new Date();
            if (_self.getFullYear() < cmpTo.getFullYear())
                return -1;
            else if (_self.getFullYear() > cmpTo.getFullYear())
            {
                return 1;
            }
            else
            {
                if (_self.getMonth() < cmpTo.getMonth())
                    return -1;
                else if (_self.getMonth() > cmpTo.getMonth())
                    return 1;
                else
                {
                    if (_self.getDate() < cmpTo.getDate())
                        return -1;
                    else if (_self.getDate() > cmpTo.getDate())
                        return 1;
                    else
                    {
                        if (_self.HasTime && cmpTo.HasTime)
                        {
                            if (_self.getHours() > cmpTo.getHours())
                                return 1;
                            else if (_self.getHours() < cmpTo.getHours())
                                return -1;
                            else
                            {
                                if (_self.getMinutes() < cmpTo.getMinutes())
                                    return -1;
                                else if (_self.getMinutes() > cmpTo.getMinutes())
                                    return 1;
                                else
                                {
                                    if (_self.getSeconds() < cmpTo.getSeconds())
                                        return -1;
                                    else if (_self.getSeconds() > cmpTo.getSeconds())
                                        return 1;
                                    else
                                        return 0;
                                }
                            }
                        }
                        else
                        {
                            return 0;
                        }
                    }
                }
            }
        }
        return null;
    }
    Date.prototype.compareTo = Date.prototype.CompareTo;
    Date.prototype.removeTime = function()
    {
        this.setHours(0,0,0,0);
    }
    Date.prototype.clone = function()
    {
        var d =new Date();
        d.setFullYear(this.getFullYear(),this.getMonth(), this.getDate());
        d.setHours(this.getHours(),this.getMinutes(),this.getSeconds(),this.getMilliseconds());
        return d;
    }
    Date.prototype.getDayDifference = function(cmpTo)
    {
    
       var d1 = this.clone();
       var d2 = cmpTo.clone();
       d1.removeTime();
       d2.removeTime();
        var currValue = d1.getTime();
        var cmpValue = d2.getTime();
        var diff = cmpValue - currValue;
        if (diff < 0)
            diff *= -1; 
        var oneDay = 1000 * 60 * 60 * 24;
        var days = diff / oneDay;
        return days;
    }


//window.setTimeout('com.cs.util.Date.CheckRequirements();',2000);
