/************************************************************************************************************
KD_DateTimePicker v 1.8
Copyright (C) May 2009  Dakanalis Kostas , Heraklio - Crete - Greece

License :

This library is free software; you can redistribute it and/or
modify it
It published by the Kostas Dakanalis and either version 1.0
or (at your option) any later version is free .

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

************************************************************************************************************/
(function KD_DTPickerClass(){

// Months - Days - Words
var Months = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December",
"Ιανουάριος", "Φεβρουάριος", "Μάρτιος", "Απρίλιος", "Μάιος", "Ιούνιος", "Ιούλιος", "Αύγουστος", "Σεπτέμβριος", "Οκτώβριος", "Νοέμβριος", "Δεκέμβριος" ,
"Januar", "Februar", "Marz", "April", "Mai", "June", "Juli", "August", "September", "Oktober", "November", "Dezember" ,
"Janvier", "Fevrier", "Mars", "Avril", "Mai", "Juin", "Juillet", "Aout", "Septembre", "Octobre", "Novembre", "Decembre"];
var Days = [ "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", "Δευ", "Τρι", "Τετ", "Πεμ", "Παρ", "Σαβ", "Κυρ" ,
"Mon", "Dien", "Mitt", "Don", "Fre", "Sam", "Son" , "Lun", "Mar", "Mer", "Jeu", "Vend", "Sam", "Sun"];
var Dictionary = [ "Today :", "Year :", "Month", "Week", "Time", "Minutes", "Σήμερα :", "Έτος :", "Μήνας", "Εβδ.", "Ώρα", "Λεπτά",
"Heute :", "Year :", "Monat", "Woche", "Time", "Minuten", "Aujourd'hui :", "Annee :", "Mois", "Semaine", "Time", "Minutes"];
// ---------------------

// Id'S
var DTPickerDiv = null;
var DTPickerForm = null;
var DTPickerTopTable = null;
var DTPickerDaysTable = null;
var DTPickerTimeTable = null;
//var EditBoxId = null;

// Variables
var PrintDate = new Date();
var CalDate = new Date();
var CalTime = null, MainDiv = null, CalBoxID = null;
//var CalBoxName = null;
var DicLen = 6;
var charsToStripTag = /<(?:.|\s)*?>/g;
var Modified = false;
var formatHolder = new Array(8);
var falseClose = false;

// -------------------- Parameters --------------------
var dicVal = 0;
var highLightColor = '#CECECE', DTPBackround = '#CECECE', DTPForeColor = '#101010';
var DTPLines = '#109EEB', DTPBoard = '#FFFFFF', dateFormat = "d/M/yyyy HH:mm";
var rightButton = "", leftButton = "", upButton = "", downButton = "";
// ----------------------------------------------------

this.InitDTPicker =  function( inputBoxId, DTPickerName , mouseOverColorVal , bgColorVal, foreColor , linesColorVal , boardColorVal , dateFormatVal, dictionaryVal, Limg, Rimg, Uimg, Dimg)
{
    // -------------------------------- SET PARAMETERS --------------------------------

    // Defaults ---------------------
    dicVal = 0;
    highLightColor = '#CECECE';DTPBackround = '#CECECE';DTPForeColor = '#101010';
    DTPLines = '#109EEB';DTPBoard = '#FFFFFF';dateFormat = "d/M/yyyy HH:mm";
    rightButton = "";leftButton = "";upButton = "";downButton = "";
    falseClose = false;
    //-------------------------------
    if (mouseOverColorVal) if (mouseOverColorVal.length>0) highLightColor = mouseOverColorVal;
    if ( bgColorVal ) if (bgColorVal.length>0) DTPBackround = bgColorVal;
    if (foreColor) if (foreColor.length>0) DTPForeColor = foreColor;
    if (linesColorVal) if (linesColorVal.length>0) DTPLines = linesColorVal;
    if (boardColorVal) if (boardColorVal.length>0) DTPBoard = boardColorVal;
    if (dateFormatVal) if (dateFormatVal.length>0) dateFormat = dateFormatVal;
    if (dictionaryVal) if (dictionaryVal.length>0) dicVal=dictionaryVal;
    if (Limg) if (Limg.length>0) leftButton=Limg;
    if (Rimg) if (Rimg.length>0) rightButton=Rimg;
    if (Uimg) if (Uimg.length>0) upButton=Uimg;
    if (Dimg) if (Dimg.length>0) downButton=Dimg;
    // --------------------------------------------------------------------------------

    if (!inputBoxId || !DTPickerName)  alert ("Parameters error");
    MainDiv = FindObject(DTPickerName).parentNode.id;
    if (!FindObject( inputBoxId )) alert("Object " + inputBoxId + " not found , Please check parameters");
    if (DTPickerForm){ if (CalBoxID!=inputBoxId){ falseClose = true; }}
    CalBoxID = inputBoxId;

    // GET DATE FROM THE EDIT BOX
    var dateVal = FindObject( inputBoxId ).value;
    dateVal = trimString( dateVal );
    CalDate = new Date();
    CalTime = null;
    formatHolder = new Array(8);//alert(dateFormat);
    unFormatDate( dateVal );
    Modified=false;

    PrintDate.setDate( 1 );
    var dayofweek = PrintDate.getDay();
    if ( dayofweek == 0 ){ PrintDate.setDate( - 5 );
    }else{PrintDate.setDate( - dayofweek + 2 );}

    if ( DTPickerDiv ){ ShowHideObject( 'DTPickerForm' );
    }else{
        Modified = false;
        createObjectCal( );
    }
    if (DTPickerForm)
    {
        DTPickerForm.appendChild( DTPickerTopTable );
        DTPickerForm.appendChild( DTPickerDaysTable );
        DTPickerForm.appendChild( DTPickerTimeTable );
        DTPickerDiv.appendChild( DTPickerForm );
    }
}

function unFormatDate(dateVal)
{
    var strIndx = 0, fholderIndx = 0, emptyInput = false;
    var validNumbers = "1234567890";
    var clientDate = new Date();

    var Y_OK = false, M_OK = false, d_OK = false, D_OK = false, H_OK = false, m_OK = false;
    var Y_str = "", M_str = "", D_str = "", H_str = "", m_str = "";
    //var outString = "";
    if (dateVal.length>5){ var dateToEdit = dateVal + "\/ ";
    } else {
        emptyInput = true;
        dateToEdit = "000000000000000000 ";
    }

    if ( dateFormat.length < dateToEdit.length )
    {
        for ( var i = 0; i < dateFormat.length ; ++ i )
        {
            var subchar = dateFormat.substr( i, 1 );
            switch (subchar)
            {
                case "y" :
                {
                    // Year two or four numbers
                    if ( ! Y_OK)
                    {
                        var y_pos = 0;
                        for (var yy = 0; yy < 4; ++ yy)
                        {
                            if (validNumbers.indexOf(dateToEdit.substr( strIndx + yy, 1 )) > - 1)
                            {
                                Y_str = Y_str + dateToEdit.substr( strIndx + yy, 1 );
                                ++ y_pos;
                            }
                            else{yy = 4;}
                        }
                        if (Y_str.length == 2) Y_str = "20" + Y_str;
                        if (emptyInput){Y_str=""+clientDate.getFullYear();}
                        strIndx = strIndx + y_pos;
                        formatHolder[fholderIndx] = "Y";
                        ++ fholderIndx;
                        Y_OK = true;
                    }
                    break;
                }
                case "M" :
                {
                    // month one or two
                    if ( ! M_OK )
                    {
                        if (dateFormat.substr( i + 1, 1 ) == "M") //MM
                        {
                            if ( validNumbers.indexOf( dateToEdit.substr( strIndx, 1 ) ) > - 1 & validNumbers.indexOf( dateToEdit.substr( strIndx + 1, 1 ) ) > - 1 )
                            {
                                M_str = dateToEdit.substr( strIndx, 1 ) + dateToEdit.substr( strIndx + 1, 1 );
                                strIndx = strIndx + 2;
                            }
                            if (emptyInput)
                            {
                                M_str=""+(clientDate.getMonth()+1);
                                if (M_str.length==1) {M_str = "0"+M_str;}
                            }
                            M_OK = true;
                            formatHolder[fholderIndx] = "MM";
                            ++ fholderIndx;
                        }
                        else
                        {
                            if ( i + 1 <= dateToEdit.length )
                            {
                                if ( validNumbers.indexOf( dateToEdit.substr( strIndx, 1 ) ) > - 1 &  validNumbers.indexOf(dateToEdit.substr( strIndx + 1, 1 )) == - 1 )
                                {
                                    M_str = dateToEdit.substr( strIndx, 1 );
                                    ++ strIndx;
                                }
                                if ( validNumbers.indexOf( dateToEdit.substr( strIndx, 1 ) ) > - 1 & validNumbers.indexOf( dateToEdit.substr( strIndx + 1, 1 ) ) > - 1 )
                                {
                                    M_str = dateToEdit.substr( strIndx, 1 ) + dateToEdit.substr( strIndx + 1, 1 );
                                    strIndx = strIndx + 2;
                                }
                                if (emptyInput){M_str=""+(clientDate.getMonth()+1);}
                                M_OK = true;
                                formatHolder[fholderIndx] = "M";
                                ++ fholderIndx;
                            }
                        }
                    }
                    break;
                }
                case "d" :
                {
                    // date one or two
                    if ( ! d_OK )
                    {
                        if ( i + 1 <= dateToEdit.length )
                        {
                            if ( validNumbers.indexOf( dateToEdit.substr( strIndx, 1 ) ) > - 1 &  validNumbers.indexOf(dateToEdit.substr( strIndx + 1, 1 )) == - 1 )
                            {
                                D_str = dateToEdit.substr( strIndx, 1 );
                                ++ strIndx;
                            }
                            if ( validNumbers.indexOf( dateToEdit.substr( strIndx, 1 ) ) > - 1 & validNumbers.indexOf( dateToEdit.substr( strIndx + 1, 1 ) ) > - 1 )
                            {
                                D_str = dateToEdit.substr( strIndx, 1 ) + dateToEdit.substr( strIndx + 1, 1 );
                                strIndx = strIndx + 2;
                            }
                            if (emptyInput){D_str=""+clientDate.getDate();}
                            d_OK = true;
                            formatHolder[fholderIndx] = "D";
                            ++ fholderIndx;
                        }
                    }
                    break;
                }
                case "D" :
                {
                    // date two
                    if ( ! D_OK )
                    {
                        if ( i + 1 <= dateToEdit.length )
                        {
                            if ( validNumbers.indexOf( dateToEdit.substr( strIndx, 1 ) ) > - 1 & validNumbers.indexOf( dateToEdit.substr( strIndx + 1, 1 ) ) > - 1 )
                            {
                                D_str = dateToEdit.substr( strIndx, 1 ) + dateToEdit.substr( strIndx + 1, 1 );
                                strIndx = strIndx + 2;
                            }
                            if (emptyInput)
                            {
                                D_str=""+clientDate.getDate();
                                if (D_str.length==1) {D_str = "0"+D_str;}
                            }
                            D_OK = true;
                            formatHolder[fholderIndx] = "DD";
                            ++ fholderIndx;
                        }
                    }
                    break;
                }
                case "H" :
                {
                    if ( ! H_OK )
                    {
                        if ( i + 1 <= dateToEdit.length )
                        {
                            if ( validNumbers.indexOf( dateToEdit.substr( strIndx, 1 ) ) > -1 & validNumbers.indexOf( dateToEdit.substr( strIndx + 1, 1 ) ) > -1 )
                            {
                                H_str = dateToEdit.substr( strIndx, 1 ) + dateToEdit.substr( strIndx + 1, 1 );
                                strIndx = strIndx + 2;
                            }
                            if (emptyInput)
                            {
                                H_str=""+clientDate.getHours();
                                if (H_str.length==1) {H_str = "0"+H_str;}
                            }
                            H_OK = true;
                            formatHolder[fholderIndx] = "H";
                            ++ fholderIndx;
                        }
                    }
                    break;
                }
                case "m" :
                {
                    // minutes two
                    if ( ! m_OK )
                    {
                        if ( i + 1 <= dateToEdit.length )
                        {
                            if ( validNumbers.indexOf( dateToEdit.substr( strIndx, 1 ) ) > -1 & validNumbers.indexOf( dateToEdit.substr( strIndx + 1, 1 ) ) > -1 )
                            {
                                m_str = dateToEdit.substr( strIndx, 1 ) + dateToEdit.substr( strIndx + 1, 1 );
                                strIndx = strIndx + 2;
                            }
                            if (emptyInput)
                            {
                                m_str=""+clientDate.getMinutes();
                                if (m_str.length==1) {m_str = "0"+m_str;}
                            }
                            m_OK = true;
                            formatHolder[fholderIndx] = "m";
                            ++ fholderIndx;
                        }
                    }
                    break;
                }
                default :
                {
                    ++ strIndx;
                }
            }
        }
    }

    //alert (Y_str + "-" + M_str + "-" + D_str + " " + H_str + ":" + m_str);
    if (M_str>12 || M_str<1) M_str="";
    if (D_str>31 || D_str<1) D_str="";
    if (D_str>29 & M_str==2) D_str="";

    if (H_str.length > 0 & m_str.length > 0){ CalTime = new Array([H_str], [m_str]);
    } else { CalTime = null; }

    if (Y_str.length > 0 & M_str.length > 0 & D_str.length > 0)
    {
        CalDate = new Date( Y_str , M_str - 1, D_str );
        PrintDate = new Date( Y_str , M_str - 1, D_str );
    } else {
        CalDate = null;PrintDate = null;
        alert ("Input value error . Format=" + dateFormat );
    }
}

function trimString( str )
{
    while ( str.substring( 0, 1 ) == ' ' ){
        str = str.substring( 1, str.length );
    }
    while ( str.substring( str.length - 1, str.length ) == ' ' ){
        str = str.substring( 0, str.length - 1 );
    }
    return str;
}

function createObjectCal( )
{
    var CalBoxElem = FindObject( CalBoxID );
    var leftval = CalBoxElem.style.left;// FindObject( CalBoxID ).style.left;
    var topval = CalBoxElem.style.top;//FindObject( CalBoxID ).style.top;
    var heightval = CalBoxElem.style.height;//FindObject( CalBoxID ).style.height;
    leftval = leftval.replace( "px", "" );
    topval = topval.replace( "px", "" );
    //leftval = parseInt( leftval );
    if (heightval.length==0){heightval = 24;}
    topval = parseInt( topval ) + parseInt(heightval);

    DTPickerDiv = CalBoxElem.parentNode;//FindObject( CalBoxID ).parentNode;
    DTPickerForm = document.createElement( "DIV" );
    DTPickerForm.id = 'DTPickerForm';
    DTPickerForm.style.backgroundColor = DTPBackround;
    DTPickerForm.style.position = 'absolute';
    DTPickerForm.style.left = leftval + "px";
    DTPickerForm.style.top = topval + "px";
    //DTPickerForm.style.width = '248px';
    //DTPickerForm.style.height = '151px';
    DTPickerForm.style.zIndex = '1000';
    DTPickerForm.style.visibility = 'visible';
    DTPickerForm.style.display = 'block';

    createObjectTopTable();
    createObjectDaysTable();
    createObjectTimeTable();

    DTPickerForm.style.width = DTPickerTopTable.style.width;

    var toth = parseInt(DTPickerTopTable.style.height.replace("px",""));
    toth = toth + parseInt(DTPickerDaysTable.style.height.replace("px",""));
    toth = toth + parseInt(DTPickerTimeTable.style.height.replace("px",""));
    DTPickerForm.style.height = toth;
}

function createObjectTopTable()
{
    DTPickerTopTable = document.createElement( 'table' );
    DTPickerTopTable.id = "TopTable"
    DTPickerTopTable.cellPadding = "0";
    DTPickerTopTable.cellSpacing = "0";
    DTPickerTopTable.style.border = "1px " + DTPBackround + " solid";

    var CTBODY = document.createElement( 'TBODY' );
    var row1 = document.createElement( 'tr' );
    var td1 = document.createElement( 'td' );
    var td2 = document.createElement( 'td' );
    var td3 = document.createElement( 'td' );
    var td4 = document.createElement( 'td' );
    var td5 = document.createElement( 'td' );
    var td6 = document.createElement( 'td' );
    var td7 = document.createElement( 'td' );
    var td8 = document.createElement( 'td' );

    td1.align = "center";td1.vAlign = "center";td1.width = "40";td1.height = "18";
    td1.style.cursor = 'default';
    td1.bgColor = DTPBackround;
    td1.innerHTML = '<font style = "font-size:10px" color="' + DTPForeColor +'" face="Arial">' + Dictionary[ 1 + ( dicVal * DicLen ) ] + '<\/font>';

    td2.align = "center";td2.vAlign = "center";td2.width = "10";td2.height = "18";
    td2.style.border = "1px " + DTPLines + " solid";
    td2.style.cursor = 'pointer';
    td2.bgColor = DTPBackround;
    td2.onclick = function(){
        changeYear( - 1 )
    };
    if (leftButton.length>0)
    {
        td2.innerHTML = '<img src="' + leftButton + '" width="10" height="18">';
    } else {
        td2.innerHTML = '<font style = "font-size:8px" color="#000000" face="Arial">' + "<" + '<\/font>';
    }

    td3.id = 'DTP_' + MainDiv + '_YEAR';
    td3.align = "center";td3.vAlign = "center";td3.width = "40";td3.height = "18";
    td3.style.cursor = 'default';
    td3.bgColor = DTPBackround;
    td3.innerHTML = '<font style = "font-size:10px" color="' + DTPForeColor +'" face="Arial">' + CalDate.getFullYear() + '<\/font>';

    td4.align = "center";td4.vAlign = "center";td4.width = "10";td4.height = "18";
    td4.style.border = "1px " + DTPLines + " solid";
    td4.style.cursor = 'pointer';
    td4.bgColor = DTPBackround;
    td4.onclick = function(){
        changeYear( 1 )
    };
    if (rightButton.length>0)
    {
        td4.innerHTML = '<img src="' + rightButton + '" width="10" height="18">';
    } else {
        td4.innerHTML = '<font style = "font-size:8px" color="' + DTPForeColor +'" face="Arial">' + ">" + '<\/font>';
    }

    td5.align = "center";td5.vAlign = "center";td5.width = "58";td5.height = "18";
    td5.style.cursor = 'default';
    td5.bgColor = DTPBackround;
    td5.innerHTML = '<font style = "font-size:10px" color="' + DTPForeColor +'" face="Arial">' + Dictionary[ 2 + ( dicVal * DicLen ) ] + '<\/font>';

    td6.align = "center";td6.vAlign = "center";td6.width = "10";td6.height = "18";
    td6.style.border = "1px " + DTPLines + " solid";
    td6.style.cursor = 'pointer';
    td6.bgColor = DTPBackround;
    td6.onclick = function(){
        changeMonth( - 1 )
    };
    if (leftButton.length>0)
    {
        td6.innerHTML = '<img src="' + leftButton + '" width="10" height="18">';
    } else {
        td6.innerHTML = '<font style = "font-size:8px" color="' + DTPForeColor +'" face="Arial">' + "<" + '<\/font>';
    }

    td7.id = 'DTP_' + MainDiv + '_Month';
    td7.align = "center";td7.vAlign = "center";td7.width = "70";td7.height = "18";
    td7.style.cursor = 'default';
    td7.bgColor = DTPBackround;
    td7.innerHTML = '<font style = "font-size:10px" color="' + DTPForeColor +'" face="Arial">' + Months[ ( CalDate.getMonth() ) + ( dicVal * 12 ) ] + '<\/font>';

    td8.align = "center";td8.vAlign = "center";td8.width = "10";td8.height = "18";
    td8.style.border = "1px " + DTPLines + " solid";
    td8.style.cursor = 'pointer';
    td8.bgColor = DTPBackround;
    td8.onclick = function(){
        changeMonth( 1 )
    };
    if (rightButton.length>0)
    {
        td8.innerHTML = '<img src="' + rightButton + '" width="10" height="18">';
    } else {
        td8.innerHTML = '<font style = "font-size:8px" color="' + DTPForeColor +'" face="Arial">' + ">" + '<\/font>';
    }

    row1.appendChild( td1 );
    row1.appendChild( td2 );
    row1.appendChild( td3 );
    row1.appendChild( td4 );
    row1.appendChild( td5 );
    row1.appendChild( td6 );
    row1.appendChild( td7 );
    row1.appendChild( td8 );

    var w=0;
    for (var i=0;i<row1.childNodes.length;++i){ w = w + parseInt(row1.childNodes[i].width); }
    DTPickerTopTable.style.width = w;
    DTPickerTopTable.style.height = "18px";
    CTBODY.appendChild(row1);
    DTPickerTopTable.appendChild( CTBODY );
}

function createObjectDaysTable()
{
    DTPickerDaysTable = document.createElement( 'TABLE' );
    DTPickerDaysTable.id = "DaysTable"
    DTPickerDaysTable.cellPadding = "0";
    DTPickerDaysTable.cellSpacing = "0";
    DTPickerDaysTable.style.border = "1px " + DTPBackround + " solid";

    CTBODY = document.createElement( 'TBODY' );

    var countDayOfWeek = 0;
    var tempDate = new Date(CalDate.getFullYear(),0,1);
    var firstDay = new Date(CalDate.getFullYear(),CalDate.getMonth(),1);
    while (tempDate.getTime()<=firstDay.getTime())
    {
        tempDate.setDate(tempDate.getDate() + 7);
        ++countDayOfWeek;
    }

    var calRows = 7,w=0,h=0;
    var calrows = new Array( 6 );

    for ( var x = 0 ; x < calRows; ++ x )
    {
        w=0;
        var calcells = new Array( 8 );
        calrows[ x ] = document.createElement( 'tr' );
        for ( var z = 0; z < 8; ++ z )
        {
            calcells[ z ] = document.createElement( 'td' );
            calcells[ z ].align = "center";calcells[ z ].vAlign = "center";calcells[ z ].width = "30";calcells[ z ].height = "18";
            calcells[ z ].style.border = "1px " + DTPBackround + " solid";
            calcells[ z ].bgColor = DTPBackround;

            w=w+parseInt(calcells[ z ].width.replace("px",""));
            if ( x == 0 & z > 0 )
            {
                calcells[ z ].innerHTML = '<font style = "font-size:9px" color="' + DTPForeColor +'" face="Arial">' + Days[ ( z - 1 ) + ( dicVal * 7 ) ] + '<\/font>';
            }
            else
            {
                if ( x > 0 & z > 0 )
                {
                    calcells[ z ].style.border = "1px " + DTPBoard + " solid";
                    calcells[ z ].style.cursor = 'pointer';
                    calcells[ z ].bgColor = DTPBoard;

                    var thisDay = PrintDate.getDate();
                    if ( thisDay == CalDate.getDate() & PrintDate.getMonth() == CalDate.getMonth() )
                    {
                        // RED DAY
                        calcells[ z ].innerHTML = '<font style = "font-size:9px" color="#FF0000" face="Arial">' + thisDay + '<\/font>';
                        calcells[ z ].onmouseover = highLightCell;
                        calcells[ z ].onmouseout = highLightCell;
                        calcells[ z ].onclick = returnSelectionEvent;
                    }
                    else
                    {
                        // NormalDay
                        if ( CalDate.getMonth() == PrintDate.getMonth() )
                        {
                            calcells[ z ].onmouseover = highLightCell;
                            calcells[ z ].onmouseout = highLightCell;
                            calcells[ z ].onclick = returnSelectionEvent;
                            calcells[ z ].innerHTML = '<font style = "font-size:9px" color="' + DTPForeColor +'" face="Arial">' + thisDay + '<\/font>';
                        }
                        else
                        {
                            // Disable Day
                            calcells[ z ].innerHTML = '<font style = "font-size:9px" color="#cacaca" face="Arial">' + thisDay + '<\/font>';
                        }
                    }
                    PrintDate.setDate( thisDay + 1 );
                }
            }
            if ( z == 0 & x == 0 )
            {
                calcells[ z ].innerHTML = '<font style = "font-size:9px" color="' + DTPForeColor +'" face="Arial">' + Dictionary [ 3 + ( dicVal * DicLen ) ] + '<\/font>';
            }
            if ( z == 0 & x > 0 )
            {
                // WEEKS
                calcells[ z ].innerHTML = '<font style = "font-size:9px" color="' + DTPForeColor +'" face="Arial">' + countDayOfWeek + '<\/font>';
                ++countDayOfWeek;
            }

            calrows[ x ].appendChild( calcells[ z ] );
            if ( x > 3 & PrintDate.getMonth() != CalDate.getMonth() ) -- calRows;
        }
        h=h+18;
        CTBODY.appendChild( calrows[ x ] );
    }
    DTPickerDaysTable.appendChild(CTBODY);
    DTPickerDaysTable.style.width = w;
    DTPickerDaysTable.style.height = h;

    PrintDate.setDate( CalDate.getDate() );
    PrintDate.setMonth( CalDate.getMonth() );
}

function createObjectTimeTable()
{
    DTPickerTimeTable = document.createElement( 'table' );
    DTPickerTimeTable.id = "TimeTable"
    // DTPickerTimeTable.width = "100%";
    DTPickerTimeTable.cellPadding = "0";
    DTPickerTimeTable.cellSpacing = "0";
    DTPickerTimeTable.style.border = "1px " + DTPBackround + " solid";

    var CTBODY = document.createElement( 'TBODY' );
    var row1 = document.createElement( 'tr' );
    var td1 = document.createElement( 'td' );
    var td2 = document.createElement( 'td' );
    var td3 = document.createElement( 'td' );
    var td4 = document.createElement( 'td' );
    var td5 = document.createElement( 'td' );
    var td6 = document.createElement( 'td' );
    var td7 = document.createElement( 'td' );

    var todayDT = new Date();
    var todaySTR = todayDT.getDate() + "/" + ( todayDT.getMonth() + 1 ) + "/" + todayDT.getFullYear();
    if ( CalTime )
    {
        td1.align = "center";td1.vAlign = "center";td1.width = "148";td1.height = "18";
        td1.style.cursor = 'default';
        td1.bgColor = DTPBackround;
        td1.innerHTML = '<font style = "font-size:10px" color="' + DTPForeColor +'" face="Arial">' + Dictionary[ 0 + ( dicVal * DicLen ) ] + " " + todaySTR + '<\/font>';

        td2.align = "center";td2.vAlign = "center";td2.width = "10";td2.height = "18";
        td2.style.border = "1px " + DTPLines + " solid";
        td2.style.cursor = 'pointer';
        td2.bgColor = DTPBackround;
        td2.onclick = function(){
            changeHour( - 1 )
        };
        if (downButton.length>0)
        {
            td2.innerHTML = '<img src="' + downButton + '" width="10" height="18">';
        } else {
            td2.innerHTML = '<font style = "font-size:8px" color="' + DTPForeColor +'" face="Arial">' + "<" + '<\/font>';
        }

        td3.id = 'DTP_' + MainDiv + '_HOUR';
        td3.align = "center";td3.vAlign = "center";td3.width = "30";td3.height = "18";
        td3.style.cursor = 'default';
        td3.bgColor = DTPBackround;
        td3.innerHTML = '<font style = "font-size:10px" color="' + DTPForeColor +'" face="Arial">' + CalTime[ 0 ] + '<\/font>';

        td4.align = "center";td4.vAlign = "center";td4.width = "10";td4.height = "18";
        td4.style.border = "1px " + DTPLines + " solid";
        td4.style.cursor = 'pointer';
        td4.bgColor = DTPBackround;
        td4.onclick = function(){
            changeHour( 1 )
        };
        if (upButton.length>0)
        {
            td4.innerHTML = '<img src="' + upButton + '" width="10" height="18">';
        } else {
            td4.innerHTML = '<font style = "font-size:8px" color="' + DTPForeColor +'" face="Arial">' + ">" + '<\/font>';
        }

        td5.align = "center";td5.vAlign = "center";td5.width = "10";td5.height = "18";
        td5.style.border = "1px " + DTPLines + " solid";
        td5.style.cursor = 'pointer';
        td5.bgColor = DTPBackround;
        td5.onclick = function(){
            changeMinutes( - 1 )
        };
        if (downButton.length>0)
        {
            td5.innerHTML = '<img src="' + downButton + '" width="10" height="18">';
        } else {
            td5.innerHTML = '<font style = "font-size:8px" color="' + DTPForeColor +'" face="Arial">' + "<" + '<\/font>';
        }

        td6.id = 'DTP_' + MainDiv + '_MIN';
        td6.align = "center";td6.vAlign = "center";td6.width = "30";td6.height = "18";
        td6.style.cursor = 'default';
        td6.bgColor = DTPBackround;
        td6.innerHTML = '<font style = "font-size:10px" color="' + DTPForeColor +'" face="Arial">' + CalTime[ 1 ] + '<\/font>';

        td7.align = "center";td7.vAlign = "center";td7.width = "10";td7.height = "18";
        td7.style.border = "1px " + DTPLines + " solid";
        td7.style.cursor = 'pointer';
        td7.bgColor = DTPBackround;
        td7.onclick = function(){
            changeMinutes( 1 )
        };
        if (upButton.length>0)
        {
            td7.innerHTML = '<img src="' + upButton + '" width="10" height="18">';
        } else {
            td7.innerHTML = '<font style = "font-size:8px" color="' + DTPForeColor +'" face="Arial">' + ">" + '<\/font>';
        }

        row1.appendChild( td1 );
        row1.appendChild( td2 );
        row1.appendChild( td3 );
        row1.appendChild( td4 );
        row1.appendChild( td5 );
        row1.appendChild( td6 );
        row1.appendChild( td7 );
    } else {
        td1.align = "center";td1.vAlign = "center";td1.width = "100%";td1.height = "18";
        td1.style.cursor = 'default';
        td1.bgColor = DTPBackround;
        td1.innerHTML = '<font style = "font-size:10px" color="' + DTPForeColor +'" face="Arial">' + Dictionary[ 0 + ( dicVal * DicLen ) ] + " " + todaySTR + '<\/font>';
        row1.appendChild( td1 );
    }

    var w=0;
    if (row1.length>1)
    {
        for (var i=0;i<row1.childNodes.length;++i)
        {
            w = w + parseInt(row1.childNodes[i].width);
        }
        DTPickerTimeTable.style.width = w + 'px';
    } else { DTPickerTimeTable.style.width = '248px'; }
    DTPickerTimeTable.style.height = "18px";
    CTBODY.appendChild(row1);
    DTPickerTimeTable.appendChild( CTBODY );
}

function highLightCell()
{
    if ( this.bgColor.toUpperCase() == DTPBoard.toUpperCase() ) { this.bgColor = highLightColor;
    } else { this.bgColor = DTPBoard; }
}

this.returnSelection = function( dayCell )
{
    var yyyy = FindObject( 'DTP_' + MainDiv + '_YEAR' );
    var MM = FindObject( 'DTP_' + MainDiv + '_Month' );
    var selYear = yyyy.innerHTML.replace( charsToStripTag, "" );
    var selMonth = MM.innerHTML.replace( charsToStripTag, "" );

    // GET SELECTED DAY
    var selDay = "";
    if ( dayCell ){ selDay = dayCell.innerHTML.replace( charsToStripTag, "" );
    } else { selDay = ""+CalDate.getDate(); }

    // GET MONTH FROM DICTIONARY
    var valMonth = 0, strMonth="0";
    for ( var x = 0; x < Months.length; ++ x )
    {
        if ( Months[ x ] == selMonth )
        {
            valMonth = x;
            break;
        }
    }
    strMonth = "" + (valMonth - ( dicVal * 12 ) + 1);

    // GET HOUR & MINUTES IF EXIST
    var selHour = "", selMin = "";
    if ( CalTime )
    {
        var HH = FindObject( 'DTP_' + MainDiv + '_HOUR' );
        var MN = FindObject( 'DTP_' + MainDiv + '_MIN' );

        if (HH)
        {
            selHour = HH.innerHTML.replace( charsToStripTag, "" );
            selMin = MN.innerHTML.replace( charsToStripTag, "" );
            CalTime[0] = selHour;
            CalTime[1] = selMin;
        } else { CalTime = null; }
    }

    var retVal = "", sepCount = 0;
    for (var i = 0; i < formatHolder.length; ++ i)
    {
        switch (formatHolder[i])
        {
            case "Y" : { retVal = retVal + selYear; break; }
            case "M" : { retVal = retVal + strMonth; break; }
            case "MM" :
            {
                if (strMonth.length==1){ retVal = retVal + "0" + strMonth;
                } else { retVal = retVal + strMonth; }
                break;
            }
            case "D" : { retVal = retVal + selDay; break; }
            case "DD" :
            {
                if (selDay.length==1){ retVal = retVal + "0" + selDay;
                } else { retVal = retVal + selDay; }
                break;
            }
            case "H" :
            {
                if (CalTime){retVal = retVal + " " + selHour + ":"; }
                break;
            }
            case "m" :
            {
                if (CalTime){retVal = retVal + selMin + " ";}
                break;
            }
            default :
        }

        if (formatHolder[i] == "Y" || formatHolder[i] == "M" || formatHolder[i] == "MM" || formatHolder[i] == "D" || formatHolder[i] == "DD")
        {
            if (sepCount < 2){ retVal = retVal + "/"; ++ sepCount; }
        }
    }
    //alert(retVal + " " + falseClose);
    retVal = trimString(retVal);
    if ( !falseClose ){ FindObject( CalBoxID ).value = retVal; }
}

this.returnSelectionEvent = function()
{
    Modified = true;
    returnSelection( this );
    ShowHideObject( 'DTPickerForm' );
}

this.ShowHideObject = function ( id )
{
    var elem = FindObject( id );

    if ( elem )
    {
        if ( elem.style.visibility == 'visible' )
        {
            if ( !Modified ) returnSelection();
            elem.style.visibility = 'hidden';
            while (elem.firstChild)
            {
                elem.removeChild(elem.firstChild);
            }
            elem.parentNode.removeChild(elem);
            DTPickerDiv = null;CalTime = null;MainDiv = null;CalBoxID = null;
            DTPickerDiv = null;DTPickerForm = null;DTPickerTopTable = null;
            DTPickerDaysTable = null;DTPickerTimeTable = null;
        }
    }
}

function changeYear( chStep )
{
    var yyyy = FindObject( 'DTP_' + MainDiv + '_YEAR' );
    var thisyear = parseInt( yyyy.innerHTML.replace( charsToStripTag, "" ) );
    PrintDate.setFullYear( thisyear + chStep );
    CalDate.setFullYear( thisyear + chStep );
    PrintDate.setMonth( CalDate.getMonth() );
    PrintDate.setDate( 1 );
    var dayofweek = PrintDate.getDay();
    if ( dayofweek == 0 ){ PrintDate.setDate( - 5 );
    } else { PrintDate.setDate( - dayofweek + 2 ); }

    DTPickerForm.removeChild( DTPickerTopTable )
    DTPickerForm.removeChild( DTPickerDaysTable )
    DTPickerForm.removeChild( DTPickerTimeTable )
    createObjectTopTable();
    createObjectDaysTable();
    createObjectTimeTable();
    DTPickerForm.appendChild( DTPickerTopTable );
    DTPickerForm.appendChild( DTPickerDaysTable );
    DTPickerForm.appendChild( DTPickerTimeTable );
}

function changeMonth( chStep )
{
    var MM = FindObject( 'DTP_' + MainDiv + '_Month' );

    PrintDate.setMonth( CalDate.getMonth() + chStep );
    CalDate.setMonth( CalDate.getMonth() + chStep );

    if (CalDate.getMonth()==0 & chStep==1){ changeYear(1); }
    if (CalDate.getMonth()==11 & chStep==-1){ changeYear(-1); }

    MM.innerHTML = '<font style = "font-size:10px" color="' + DTPForeColor +'" face="Arial">' + Months[ ( CalDate.getMonth() ) + ( dicVal * 12 ) ] + '<\/font>';

    PrintDate.setDate( 1 );
    var dayofweek = PrintDate.getDay();
    if ( dayofweek == 0 ){ PrintDate.setDate( - 5 );
    } else { PrintDate.setDate( - dayofweek + 2 );}
    DTPickerForm.removeChild( DTPickerDaysTable )
    DTPickerForm.removeChild( DTPickerTimeTable )
    createObjectDaysTable();
    createObjectTimeTable();
    DTPickerForm.appendChild( DTPickerDaysTable );
    DTPickerForm.appendChild( DTPickerTimeTable );
}

function changeHour( chStep )
{
    var HH = FindObject( 'DTP_' + MainDiv + '_HOUR' );
    var thishour = HH.innerHTML.replace( charsToStripTag, "" );
    if (thishour.indexOf("0")==0) thishour = thishour.substr(1,1);
    thishour = parseInt(thishour);
    var newHour = '00';
    if (thishour<=23 & thishour>=0) newHour = (thishour + chStep).toString() ;
    if (thishour==0 & chStep==-1) newHour = '23';
    if (thishour==23 & chStep==1) newHour = '00';
    if (newHour.length==1) newHour = '0' + newHour.toString();
    HH.innerHTML = '<font style = "font-size:10px" color="' + DTPForeColor +'" face="Arial">' + newHour + '<\/font>';
}

function changeMinutes( chStep )
{
    var MM = FindObject( 'DTP_' + MainDiv + '_MIN' );
    var thismin = MM.innerHTML.replace( charsToStripTag, "" );
    if (thismin.indexOf("0")==0) thismin = thismin.substr(1,1);
    thismin = parseInt(thismin);
    var newMin = "0";
    if (thismin<=59 & thismin>=0) newMin = ( thismin + chStep ).toString();
    if (thismin==0 & chStep==-1) newMin = "59";
    if (thismin==59 & chStep==1) newMin = "00";
    if (newMin.length==1) newMin = "0" + newMin.toString();
    MM.innerHTML = '<font style = "font-size:10px" color="' + DTPForeColor +'" face="Arial">' + newMin + '<\/font>';
}

this.FindObject = function ( id, doc )
{
    var child, elem;
    if ( ! doc )
        doc = document;
    if ( doc.getElementById )
        elem = doc.getElementById( id );
    else
    if ( doc.layers )
        child = doc.layers;
    else
    if ( doc.all )
        elem = doc.all[ id ];
    if ( elem )
        return elem;
    if ( doc.id == id || doc.name == id )
        return doc;
    if ( doc.childNodes )
        child = doc.childNodes;
    if ( child )
    {
        for ( var i = 0; i < child.length; i ++ )
        {
            elem = FindObject( id, child[ i ] );
            if ( elem )
                return elem;
        }
    }
    var frm = doc.forms;
    if ( frm )
    {
        for ( var i = 0; i < frm.length; i ++ )
        {
            var elems = frm[ i ].elements;
            for ( var j = 0; j < elems.length; j ++ )
            {
                elem = FindObject( id, elems[ i ] );
                if ( elem )
                    return elem;
            }
        }
    }
    return null;
}
})();

