ET.DATETIME = 
{
    todaysDate: function()
    {
        var today = new Date();
        return this.leadingZero(today.getDate()) +'/'+ this.leadingZero(today.getMonth() + 1) +'/'+ today.getFullYear();
    },
    
    tomorrowsDate: function()
    {
        var today = new Date();
        today.setTime(today.getTime( ) + (1000*3600*24));
        return this.leadingZero(today.getDate() ) +'/'+ this.leadingZero(today.getMonth() + 1) +'/'+ today.getFullYear();
    },
    
    firstDayNextMonth: function()
    {
        var today = new Date();
        nextMonth = new Date(today.getFullYear(), today.getMonth() + 1, 1)
        nextMonth.getDate() +'/'+ (nextMonth.getMonth() + 1) +'/'+ nextMonth.getFullYear();
        return this.leadingZero(nextMonth.getDate()) +'/'+ this.leadingZero(nextMonth.getMonth() + 1) +'/'+ nextMonth.getFullYear();
    },
    
    firstDayPlus7: function()
    {
        var today = new Date();
        nextMonth = new Date(today.getFullYear(), today.getMonth() + 1, 8)
        nextMonth.getDate() +'/'+ (nextMonth.getMonth() + 1) +'/'+ nextMonth.getFullYear();
        return this.leadingZero(nextMonth.getDate()) +'/'+ this.leadingZero(nextMonth.getMonth() + 1) +'/'+ nextMonth.getFullYear();
    },
    
    futureDateDays: function(days)
    {
        var futureDate = new Date();
        futureDate.setDate(futureDate.getDate() + days);
        return this.leadingZero(futureDate.getDate()) +'/'+ this.leadingZero(futureDate.getMonth() + 1) +'/'+ this.leadingZero(futureDate .getFullYear());
    },
    
    timeHHMM: function()
    {
        var today = new Date();
        return this.leadingZero(today.getHours()) + this.leadingZero(today.getMinutes());
    },
    
    timeHHMMSS: function()
    {
        var today = new Date();
        return this.leadingZero(today.getHours()) +':'+ this.leadingZero(today.getMinutes()) +':'+ this.leadingZero(today.getSeconds());
    },
    
    weekFromToday: function()
    {
        var today = new Date();
        var nextWeek = new Date();
        nextWeek.setDate(today.getDate() + 7);
        return this.leadingZero(nextWeek.getDate()) +'/'+ this.leadingZero(nextWeek.getMonth() + 1) +'/'+ this.leadingZero(nextWeek .getFullYear());
    },

    convertUSFormat: function(dateStr, seperator)
    {
        seperator = (typeof(seperator) == 'undefined') ? '-' : seperator;
        re = new RegExp('([0-9]{2})/([0-9]{2})/([0-9]{4})', 'm');
        matches = re.exec(dateStr);
        return matches[2] + seperator + matches[1] + seperator + matches[3];
    },
    
    convertUStoAUSDate: function(dateStr, seperator)
    {
        seperator = (typeof(seperator) == 'undefined') ? '-' : seperator;
        re = new RegExp('([0-9]{2})/([0-9]{2})/([0-9]{4})', 'm');
        matches = re.exec(dateStr);
        return matches[2] + seperator + matches[1] + seperator + matches[3];
    },
    
    isValidDate: function(year, month, day)
    {
        var dt = new Date(month + '/' + day + '/' + year);
        if(dt.getDate() != day || dt.getMonth() != month-1 || dt.getFullYear()!=year)
        {
            return false;
        }
        
        return true;
    },
    
    dateToYYYYMMDD: function(dateObj) 
    { 
        return  dateObj.getFullYear() + '' + this.leadingZero(dateObj.getMonth() + 1)  + '' + this.leadingZero(dateObj.getDate());
    },
    
    dateToDDMMYYYY: function(dateObj) 
    { 
        return  dateObj.getFullYear() + '' + this.leadingZero(dateObj.getMonth() + 1)  + '' + this.leadingZero(dateObj.getDate());
    },
    
    stringToDate: function(dateString) 
    {
        try
        {
            var matches = dateString.match(/([0-9]{2})\/([0-9]{2})\/([0-9]{4})/);
            if(this.isValidDate(matches[3], matches[2], matches[1]) === false)
            {
                return false;
            }
        
            return new Date(matches[3], parseInt(matches[2], 10)-1, matches[1]);
        }
        catch(e)
        {
            return false;
        }
    },
    
    leadingZero: function(val)
    {
        var str = val + '';
        if(str.length == 1)
        {
            str = '0' + str;
        }
        
        return str;
    },
    
    checkDepatureArrivalDateChange: function(departureDate, returnDate)
    {
        var dep = ET.DATETIME.stringToDate(departureDate.val());
        var ret = ET.DATETIME.stringToDate(returnDate.val());
        if(ret < dep)
        {
            alert('Arrival date cannot be before departure date');
            returnDate.val('');
        }
    },
    
    parseDate: function(val) 
    {
        var preferEuro=(arguments.length==2)?arguments[1]:false;
        generalFormats=new Array('y-M-d','MMM d, y','MMM d,y','y-MMM-d','d-MMM-y','MMM d');
        monthFirst=new Array('M/d/y','M-d-y','M.d.y','MMM-d','M/d','M-d');
        dateFirst =new Array('d/M/y','d-M-y','d.M.y','d-MMM','d/M','d-M');
        var checkList=new Array('generalFormats',preferEuro?'dateFirst':'monthFirst',preferEuro?'monthFirst':'dateFirst');
        var d=null;
        for (var i=0; i<checkList.length; i++) 
        {
            var l=window[checkList[i]];
            for (var j=0; j<l.length; j++) 
            {
                d=getDateFromFormat(val,l[j]);
                if (d!=0) 
                { 
                    return new Date(d); 
                }
    
            }
        }
        return null;
    },
    
    isLeapYear: function(year)
    {
        year = parseInt(year, 10);
        if(year % 4 == 0)
        {
            if(year % 100 != 0)
            {
                return true;
            }
            else
            {
                if(year % 400 == 0)
                {
                    return true;
                }      
                else
                {
                    return false;
                }     
            }
        }
        return false;
    },

    compareDates: function(from, to)
    { 
        var dateResult;
        if(typeof(to)=='object' && typeof(from)=='object'){
            dateResult = to.getTime() - from.getTime();
        }else {
            dateResult = 0;
        }
        
        var dateObj = {};
        dateObj.weeks =  Math.ceil(dateResult/(1000 * 60 * 60 * 24 * 7));
        dateObj.days = Math.ceil(dateResult/(1000 * 60 * 60 * 24));
        dateObj.hours = Math.ceil(dateResult/(1000 * 60 * 60));
        dateObj.minutes = Math.ceil(dateResult/(1000 * 60));
        dateObj.seconds = Math.ceil(dateResult/(1000));
        dateObj.milliseconds = dateResult;
        return dateObj;

    },
    
    compareDatesDDMMYYYY: function(from, to)
    { 
        from = from.split('/');
        from = new Date(from[2], from[1], from[0]); 
        to = to.split('/');
        to = new Date(to[2], to[1], to[0]); 
        return this.compareDates(from, to);
    }
};