//
//                 IFS Research & Development
//
//  This program is protected by copyright law and by international
//  conventions. All licensing, renting, lending or copying (including
//  for private use), and all other use of the program, which is not
//  expressively permitted by IFS Research & Development (IFS), is a
//  violation of the rights of IFS. Such violations will be reported to the
//  appropriate authorities.
//
//  VIOLATIONS OF ANY COPYRIGHT IS PUNISHABLE BY LAW AND CAN LEAD
//  TO UP TO TWO YEARS OF IMPRISONMENT AND LIABILITY TO PAY DAMAGES.
// ----------------------------------------------------------------------------
// File          : calendar.js
// Description   :
// Notes         :
// ----------------------------------------------------------------------------
// Modified      :
//    Ramila H   2002-07-25 - Created. 
//    Ramila H   2002-07-31 - Always called post_method at the end of validating date.
//    Ramila H   2002-08-01 - Changed resizable to no.
//    Ramila H   2002-08-02 - Changed resizable back to yes.
//    Ramila H   2002-11-20 - Removed call to post_method when null;
//    Sampath    2003-01-10 - made changes to work with out clientutil applet if necessory
//    Ramila H   2003-01-23 - corrected leap year algo. stopped submitting form.
//    Ramila H   2004-01-20 - Bug id 42101 implemented.
//    Chandana D 2004-01-27 - Bug 42299. Modified getFooterTag() and setDate().
// ----------------------------------------------------------------------------

function showCalendar(_target_field,_year,_month,_post_method,_mask,_pos,_label,_time_enabled)
{
    target_field = _target_field;
    post_method = _post_method;
    mask = _mask;
    mask = mask.replace(/\'/g,"\\\'");
    pos = _pos;
    label = _label.replace(/\'/g,"\\\'");
    time_enabled = _time_enabled;

    scripts_location = __CALENDAR_SCRIPT_LOC;

    win_width = __CALENDAR_WINDOW_WIDTH;
    win_height = __CALENDAR_WINDOW_HEIGHT;

    arr_months = new Array(12);
    arr_months[0] = __JANUARY;
    arr_months[1] = __FEBRUARY;
    arr_months[2] = __MARCH;
    arr_months[3] = __APRIL;
    arr_months[4] = __MAY;
    arr_months[5] = __JUNE;
    arr_months[6] = __JULY;
    arr_months[7] = __AUGUST;
    arr_months[8] = __SEPTEMBER;
    arr_months[9] = __OCTOBER;
    arr_months[10] = __NOVEMBER;
    arr_months[11] = __DECEMBER;

    arr_weekdays = new Array(7);
    arr_weekdays[0] = __SUNDAY;
    arr_weekdays[1] = __MONDAY;
    arr_weekdays[2] = __TUESDAY;
    arr_weekdays[3] = __WEDNESDAY;
    arr_weekdays[4] = __THURSDAY;
    arr_weekdays[5] = __FRIDAY;
    arr_weekdays[6] = __SATURDAY;

    arr_days_in_month = ["31","28","31","30","31","30","31","31","30","31","30","31"];
    arr_leap_year_month = ["31","29","31","30","31","30","31","31","30","31","30","31"];


    bgcolor = __CALENDAR_BGCOLOR;
    alt_bgcolor = __CALENDAR_ALT_BGCOLOR;
    body_bgcolor = __CALENDAR_BODY_BGCOLOR;
    cmdbar_bgcolor = __CALENDAR_CMDBAR_BGCOLOR;

    dt_date = new Date();
    dt_today = new Date();

    dt_date.setDate(1);
    if (_month != '') 
        dt_date.setMonth(_month);

    if (_year != '') 
        dt_date.setYear(_year);
    
    curr_month = dt_date.getMonth();
    curr_year = dt_date.getFullYear();
    curr_time = dt_date.getTime();
    curr_hour = dt_date.getHours()
    curr_mins = dt_date.getMinutes();
    curr_sec  = dt_date.getSeconds();
    curr_day  = dt_date.getDay();
    curr_date = dt_date.getDate();

    week_start_day = __WEEK_START_DAY;
    first_day = getFirstDay();
    
    if (curr_hour <10) 
        curr_hour = "0"+curr_hour;

    if (curr_mins <10) 
        curr_mins = "0"+curr_mins;

    if (curr_sec <10) 
        curr_sec = "0"+curr_sec;

    (document.getElementById) ? style_sheet = "Explorer.css" : style_sheet = "Netscape.css";
    
    var str_buffer = new String(getHeadTag()+getBodyTag()+getFooterTag());
    
    if (document.all) 
    {
        var x = mouseX;
        var y = mouseY+120;

        window_width = document.body.clientWidth;
        window_height = document.body.clientHeight;
    }
    else
    {
        var x = mouseX - window.pageXOffset;
        var y = mouseY - window.pageYOffset +100;

        window_width = window.innerWidth;
        window_height = window.innerHeight;
    }


    if (!document.all && document.getElementById) {
        win_width = win_width - 15;
        //win_height = win_height - 15;
    }
    
    if (time_enabled != "true")
        win_width = win_width - 12;
    
    x1 = Number(x) + Number(win_width);

    if (x1 > window_width)
        x = x - win_width - 10; 

    var h = Number(win_height);

    if((y + h) > window_height + 120)
        y = window_height - win_height +90;


    calendar_window = window.open("", "CALENDAR","width="+win_width+",height="+win_height+",status=no,resizable=yes,top="+y+",left="+x);
    
    calendar_window.opener = self;
    var cal_doc = calendar_window.document;
    cal_doc.close();
    
    cal_doc = calendar_window.document;
   cal_doc.write (str_buffer);
    cal_doc.close();

}


function getHeadTag()
{
    head_tag =   "<HTML>\n"+
        "<head>\n"+
        "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">"+
        "<link rel=\"STYLESHEET\" href=\""+__CALENDAR_STYLESHEET_PATH+style_sheet+"\" type=\"text/css\">"+
        " <title>"+__CALENDAR_TITLE+"</title>\n"+
        "</head>\n"+
        "<STYLE TYPE='text/css'>\n"+
        "<!-- A:link    {text-decoration: none}\n"+
        "A:visited {text-decoration: none}\n"+
        "A:active  {text-decoration: none}\n"+
        "A:hover   {text-decoration: none} -->\n"+
/*        "<!--     \n"+
        ".TextLabel\n"+
        "{\n"+
        "FONT-FAMILY: 'Verdana';\n"+
        "FONT-SIZE: xx-small; \n"+
        "font-weight: bold;\n"+
        "}\n"+
        ".TextValue\n"+
        "{\n"+
        "FONT-FAMILY: 'Verdana';\n"+
        "FONT-SIZE: xx-small;"+
        "}     \n"+
        "--> \n"+
        */
        "</STYLE> \n"+
        "</head>\n";

    return head_tag;

}



function getBodyTag()
{
    body_tag = "<body bgcolor=\""+body_bgcolor+"\">\n"+
        "<form name=\"form\" onSubmit=\"return false;\">\n"+
        "<table border=0> \n"+
        "<!-- calendar header start--> \n"+
        "<tr>\n"+
        "<td>\n"+
        "<table bgcolor=\""+cmdbar_bgcolor+"\" width=100% class=borders>\n"+
        "<tr>\n"+
        "<td nowrap><select name='MONTH' class=TextValue onChange=\"javascript:window.opener.showCalendar('"+target_field+"',document.form.YEAR.value,document.form.MONTH[document.form.MONTH.selectedIndex].value,'"+post_method+"','"+mask+"',"+pos+",'"+label+"','"+time_enabled+"');\"> \n";

        for (i=0;i<arr_months.length; i++) {
            if(curr_month != i)
                body_tag += "<option value="+i+">"+arr_months[i]+"</option>\n";
            else
                body_tag += "<option value="+i+" selected>"+arr_months[i]+"</option>\n";
        }
        
        
    body_tag += "</select></td>\n"+
        "<td width=20>&nbsp;</td>    \n"+
        "<td nowrap><a href=\"javascript:window.opener.showCalendar('"+target_field+"',document.form.YEAR.value-1,document.form.MONTH[document.form.MONTH.selectedIndex].value,'"+post_method+"','"+mask+"',"+pos+",'"+label+"','"+time_enabled+"');\"><font face=\"Verdana\" size=2>-</font></a></td>\n"+
        "<td nowrap><input class=TextValue name='YEAR' onBlur=\"javascript:this.value = window.opener.validateYear(this.value);window.opener.showCalendar('"+target_field+"',document.form.YEAR.value,document.form.MONTH[document.form.MONTH.selectedIndex].value,'"+post_method+"','"+mask+"',"+pos+",'"+label+"','"+time_enabled+"');\" size=5 value="+curr_year+" maxlength=4></td> \n"+
        "<td nowrap><a href=\"javascript:window.opener.showCalendar('"+target_field+"',Number(document.form.YEAR.value)+1,document.form.MONTH[document.form.MONTH.selectedIndex].value,'"+post_method+"','"+mask+"',"+pos+",'"+label+"','"+time_enabled+"');\"><font face=\"Verdana\" size=1>+</font></a></td>\n"+
        "</tr>\n"+
        "</table>\n"+
        "</td>  \n"+
        "</tr> \n"+
        "<!-- calendar header end-->\n";


    body_tag += "<!-- calendar body start--> \n"+
        "<tr>\n"+
        "<td>\n"+
        "<table width=100%>\n"+
        "<tr>\n";

    // week day titles
    for (i=0;i<arr_weekdays.length;i++)
        body_tag +="<td nowrap class=TextValue align=\"center\"><b>"+arr_weekdays[((i+week_start_day)%7)]+"</b></td>\n";
        //body_tag +="<td class=TextValue bgcolor=\""+__CALENDAR_TITLECOLOR+"\" align=\"center\"><b>"+arr_weekdays[((i+week_start_day)%7)]+"</b></td>\n";

    body_tag += "</tr>\n";


    month_date = 1;
    // first row with leading blanks
    body_tag += "<tr bgcolor="+alt_bgcolor+">\n";
    for (i=0;i<7;i++)
    {
        if (i<first_day)
            body_tag +="<td nowrap class=TextValue align=\"right\"></td>\n";
        else
        {
            if (!isToday(month_date))
                font_color = "#000000";
            else
                font_color = "#FF0000";

            body_tag +="<td nowrap class=TextValue align=\"right\"><a href=\"javascript:assignFormattedDate(document.form.YEAR.value,document.form.MONTH[document.form.MONTH.selectedIndex].value,"+month_date+",document.form.HOURS.value,document.form.MINUTES.value,document.form.SECS.value);window.close();\"><font color=\""+font_color+"\">"+month_date+"</font></a></td>\n";
            
            month_date++;
        }

    }
    body_tag += "</tr>\n";

    //rest of days
    row = 2;
    while (month_date <= getNoOfDaysInMonth()) 
    {
        if ((row % 2) == 0)
            color = bgcolor;
        else
            color = alt_bgcolor;

        body_tag += "<tr bgcolor="+color+">\n\n";

        for (i=0;i<7;i++) {
            if (month_date <= getNoOfDaysInMonth()) 
            {
                if (!isToday(month_date))
                    font_color = "#000000";
                else
                    font_color = "#FF0000";

                body_tag +="<td nowrap class=TextValue align=\"right\"><a href=\"javascript:assignFormattedDate(document.form.YEAR.value,document.form.MONTH[document.form.MONTH.selectedIndex].value,"+month_date+",document.form.HOURS.value,document.form.MINUTES.value,document.form.SECS.value);window.close();\"><font color=\""+font_color+"\">"+month_date+"</font></a></td>\n";
            }
            //else
            //    body_tag +="<td class=TextValue align=\"right\"></td>\n";

            month_date++;
        }

        row++;

        body_tag += "</tr>\n";
    }
 


    body_tag += "</table>\n"+
        "</td>\n"+
        "</tr>\n"+
        "<!-- calendar body end-->\n";



    return body_tag;
}


function getFooterTag()
{
    if ("true" != time_enabled) 
    {
        readonly = "readonly";
        css_class = "ReadOnlyTextValue";
    }
    else
    {
        readonly = "";
        css_class = "TextValue";
    }
    //Bug 42299, start
    if(VALIDATION_METHOD=='APPLET')
    {
       insert_applet  ="<applet codebase=\""+__APPLET_CODEBASE+"\" code=\""+__APPLET_CLASS+"\" name=ClientUtil width=1 height=1>\n"+
                        "\t<param name=url value=\""+__APPLET_URL+"\">\n"+
                        "\t<param name=bgcolor value=\""+body_bgcolor+"\">\n"+
                        "</applet>\n";
    }
    else
       insert_applet = "";
    
    call_connect_function = "\t r = window.opener.__connect(\n"; 
    //Bug 42299, end

    mask = mask.replace(/\\/g,"");
    mask = URLClientEncode(mask);

    footer_tag = "<!-- calendar footer start-->\n"+
        "<tr>\n"+
        "<td nowrap>\n"+
        "<table bgcolor=\""+cmdbar_bgcolor+"\" width=100% class=borders>\n"+
        "<tr>\n"+
        //"<td class=TextValue><b>"+__CALENDAR_TIME+" : </b><input class=\""+css_class+"\" name='TIME' value='"+curr_hour+":"+curr_mins+":"+curr_sec+"' size=9 maxlength=8 "+readonly+"></td>\n"+
        "<td nowrap class=TextValue><b>"+__CALENDAR_TIME+": </b>";
        
        if ("true" == time_enabled)
        {
            footer_tag += "<input class=\"TextValue\" name='HOURS' value='00' size=2 maxlength=2 onBlur=\"javascript:this.value = window.opener.validateHours(this.value);\"></td>\n"+
                "<td nowrap class=TextValue><b>:</b><input class=\"TextValue\" name='MINUTES' value='00' size=2 maxlength=2 onBlur=\"javascript:this.value = window.opener.validateMins(this.value);\"></td>\n"+
                "<td nowrap class=TextValue><b>:</b><input class=\"TextValue\" name='SECS' value='00' size=2 maxlength=2 onBlur=\"javascript:this.value = window.opener.validateSecs(this.value);\"></td>\n";
        }
        else
        {
            footer_tag += "00:00:00</td>\n"+
                "<input type=hidden name='HOURS' value='00'>\n"+
                "<input type=hidden name='MINUTES' value='00'>\n"+
                "<input type=hidden name='SECS' value='00'>\n";

        }

    footer_tag += "<td width=20>&nbsp;</td>\n";

        if ("true" == time_enabled)
        {
            footer_tag += "<td nowrap><a href=\"javascript:setCurrentTime()\">"+
                          "<img src=\""+__CALENDAR_IMAGE_PATH+"/clock.gif\" border=0 alt=\""+__CALENDAR_CURR_TIME+"\" title=\""+__CALENDAR_CURR_TIME+"\"></a></td>\n";
                          
        }

    footer_tag += "<td width=10>&nbsp;</td>" +
        "<td nowrap><a href=\"javascript:assignFormattedDate("+dt_today.getFullYear()+","+dt_today.getMonth()+","+dt_today.getDate()+",document.form.HOURS.value,document.form.MINUTES.value,document.form.SECS.value);window.close();\">"+
        "<img src=\""+__CALENDAR_IMAGE_PATH+"/today.gif\" border=0 alt=\""+__CALENDAR_TODAY+"\" title=\""+__CALENDAR_TODAY+"\"></a></td>\n"+
        "</tr>\n"+
        "</table>  \n"+
        "</td>\n"+
        "</tr>\n"+
        "<!-- calendar footer end-->\n"+
        "</table>\n"+
        insert_applet +

        "<script language=javascript>\n"+
        "function assignFormattedDate(sYear,sMonth,sDate,sHour,sMin,sSec)\n"+
        "{\n"+
        "\tmask =\""+mask+"\";\n"+ 
        call_connect_function +
        "\t   '"+scripts_location+"DateFieldFormatter.page?'\n"+
        "\t    +'VALIDATE=Y&YEAR='+sYear\n"+
        "\t    +'&MONTH='+sMonth\n"+
        "\t    +'&DAY='+sDate \n"+
        "\t    +'&HOURS='+sHour\n"+
        "\t    +'&MIN='+sMin\n"+
        "\t    +'&SEC='+sSec\n"+
        "\t    +'&MASK='+mask);\n\n"+
        "\t window.opener.setDate(r);\n"+
        "}\n"+
        "\n"+
        "function setCurrentTime()\n"+
        "{\n"+
        "\t curr_date = new Date();\n"+
        "\t curr_hour = curr_date.getHours();\n"+
        "\t curr_mins = curr_date.getMinutes();\n"+
        "\t curr_sec = curr_date.getSeconds();\n"+
        
        "\t if (curr_hour <10) curr_hour = \"0\"+curr_hour; \n"+
        "\t if (curr_mins <10) curr_mins = \"0\"+curr_mins;\n"+
        "\t if (curr_sec <10) curr_sec = \"0\"+curr_sec;\n"+
        
        "\t document.form.HOURS.value = curr_hour;\n"+
        "\t document.form.MINUTES.value = curr_mins;\n"+
        "\t document.form.SECS.value = curr_sec;\n"+
        "}\n"+
        "</script>\n"+


        "</form>\n"+
        "</body>\n"+
        "</html>\n";

    return footer_tag;
}



function setDate(r)
{
    if( checkStatus_(r,target_field,i,label) )
   {    
        //Bug 42299, start
        n = r.length-1;
        //Bug 42299, end
        getField_(target_field,pos).value = r.substring(1,n);  // remove leading blank
    }

    if (post_method != '')
        eval(post_method+"(pos);");
}


function getFirstDay()
{
    var first_day = new Date(curr_year,curr_month,1);
    first_day = ((first_day.getDay() - week_start_day)+7)%7;
    return first_day;
}


function getNoOfDaysInMonth()
{
   if ((curr_year%4 == 0 && curr_year%100 != 0) || curr_year %400 ==0)  
       return arr_leap_year_month[curr_month];
   else
       return arr_days_in_month[curr_month];
}


function isToday(chk_date)
{
    return ((curr_year == dt_today.getFullYear())&&(curr_month == dt_today.getMonth())&&(chk_date == dt_today.getDate()));
}



function validateYear(sYear)
{
   var reg = /^\d{4}$/;
   if (reg.test(sYear))
        return sYear;
    else
        return dt_today.getFullYear();

}


function validateHours(sHours)
{
    var reg = /^([0-9]|0[0-9]|1[0-9]|2[0-3])$/;
    if (reg.test(sHours))
        return (sHours.length == 1)?"0"+sHours:sHours;
    else
        return curr_hour;
}

function validateMins(sMin)
{
    var reg = /^([0-5]\d|[0-9])$/;
    if (reg.test(sMin))
        return (sMin.length == 1)?"0"+sMin:sMin;
    else
        return curr_mins;
}

function validateSecs(sSec)
{
    var reg = /^([0-5]\d|[0-9])$/;
    if (reg.test(sSec))
        return (sSec.length == 1)?"0"+sSec:sSec;
    else
        return curr_sec;
}


