﻿// JScript File
var popup = null;
var currentDate = null;
var currentMonth = null;
var currentYear = null;
var currentReturn = null;
var nextObj = null;
var style = "double";
var imagePath = null;
var flagStatus = false;
var arr_data1 = new Array("Mar 20 2007","Mar 21 2007","Mar 22 2007","Mar 23 2007");
var arr_data2 = new Array("Apr 22 2007","Apr 23 2007","Apr 26 2007");
var arr_selected = new Array(1);


function Calendar(month,year){
    var d = new Date();
    currentDate = d.getDate();
    currentMonth = (month-1) || d.getMonth();
    currentYear = year || d.getYear();
    
    this.show = function(imageCalendar,leftFrame,topFrame,width,height,returnObject,nextObject){
        if((returnObject == null) || (returnObject == "undefined")){
            alert("Please! Enter contain control");
            return;
        }
        
        var month1 = null;
        var month2 = null;
        var year1 = null;
        var year2 = null;
        
        if(currentMonth == 11){
            month1 = currentMonth;
            year1 = currentYear;
            month2 = 0;
            year2 = currentYear + 1;
        }
        else{
            month1 = currentMonth;
            year1 = currentYear;
            month2 = currentMonth + 1;
            year2 = currentYear;
        }
        if(document.getElementById(returnObject) != null && document.getElementById(returnObject) != "undefined"){
            var dt = document.getElementById(returnObject).value;
            var spl = dt.split("-");
            if(spl.length == 3){
                arr_selected[0] = new Date(spl[0],parseFloat(spl[1]) - 1,spl[2]).toDateString();
            }
        }        
        document.getElementById(returnObject).focus();
        currentReturn = returnObject;
        nextObj = nextObject;
        beginShow(imageCalendar,leftFrame,topFrame,width,height,year1,month1,year2,month2);
    }
    this.setMonthCalendar = function(month){
        currentMonth = month;
    }
    this.setYearCalendar = function(year){
        currentYear = year;
    }
    this.setImagePath = function (path){
        imagePath = path;
    }
    this.setStyle = function(istyle){
        style = istyle;
    }
    this.allowSelectPass = function(flag){
        flagStatus = flag;
    }   
    function beginShow(imageCalendar,leftFrame,topFrame,width,height,year1,month1,year2,month2){
        var arr1 = null;
        var arr2 = null;
        
        if(style == "normal"){
            arr1 = getArrayCalendar(year1,month1);
        }
        else{
            arr1 = getArrayCalendar(year1,month1);
            arr2 = getArrayCalendar(year2,month2);
        }
        var str = fillCalendar(arr1,arr2,false,style);
        
        popup = window.createPopup();
        var popupBody = popup.document.body;
        popupBody.innerHTML = str;
        
        for (var i = 0; i < popupBody.all.length; i++) {
            if (popupBody.all[i].tagName == "A")
                popupBody.all[i].onclick = doClick;
        }
        popup.show(imageCalendar.getBoundingClientRect().right + leftFrame,imageCalendar.getBoundingClientRect().top + topFrame, width, height, popupBody);
    }
     this.getMonthDay = function(dateTime){
        var _dateTime;
        
        if((dateTime == null) || (dateTime == "undefined")){
            _dateTime = new Date(currentYear,currentMonth,currentDate);
        }
        else{
            _dateTime = dateTime
        }
        
        do{
            _dateTime.setDate(_dateTime.getDate() + 1);
        }
        while(_dateTime.getDate() != 1)
        _dateTime.setDate(_dateTime.getDate() - 1);
        
        return _dateTime.getDate();
    }
}
function beginFillCalendar(arrayCalendar,str_data,tableId,style){
    var num = roundGreater(arrayCalendar.length / 7);
    var str = "";
    str += "<table id='" + tableId + "' border='1' cellpadding='2' cellspacing='0' rules='all' style='border-right: #717b8e 1px solid;";
    str += "border-top: #717b8e 1px solid; font-size: 8pt; border-left: #717b8e 1px solid;";
    str += "border-bottom: #717b8e 1px solid; font-family: Verdana;";
    str += "border-collapse: collapse' title='Calendar'>";
    str += "<tr>";
    str += "<td colspan='7'>";
    if(style == "normal"){
        str += "<table border='0' cellspacing='0' style='font-size: 9pt; width: 100%;";
        str += "font-family: Verdana; border-collapse: collapse'>";
        str += "<tr>";
        str += "<td>";        
        str += "<a href='' objectSelected='previous'>";
        //str += "<img src='"+ imagePath +"schedule_btn_1.jpg' alt='Go to the previous month' style='border-top-style: none; border-right-style: none; border-left-style: none; border-bottom-style: none' />";
		str += "<<";
        str += "</a></td>";
        str += "<td align='center' style='color: #717b8e'>";
        str += "<strong>";
        str += arrayCalendar[6].getFullYear() + "年 " + monthName(arrayCalendar[6].getMonth())+"月";
        str += "</strong>";
        str += "</td>";
        str += "<td align='right'>";
        str += "<a href='' objectSelected='next'>";
        //str += "<img src='" + imagePath + "schedule_btn_2.jpg' alt='Go to the previous month' style='border-top-style: none; border-right-style: none; border-left-style: none; border-bottom-style: none' />";
		str += ">>";
        str += "</a>";
        str += "</td>";
        str += "</tr>";
        str += "</table>";
    }
    else{
        if(tableId == "Calendar1"){
            str += "<table border='0' cellspacing='0' style='font-size: 9pt; width: 100%;";
            str += "font-family: Verdana; border-collapse: collapse'>";
            str += "<tr>";
            str += "<td>";        
            str += "<a href='' objectSelected='previous'>";
            //str += "<img src='" + imagePath + "schedule_btn_1.jpg' alt='Go to the previous month' style='border-top-style: none; border-right-style: none; border-left-style: none; border-bottom-style: none' />";
			str += "<<";
            str += "</a></td>";
            str += "<td align='center' style='color: #717b8e'>";
            str += "<strong>";
            str += arrayCalendar[6].getFullYear() + "年 " + monthName(arrayCalendar[6].getMonth())+"月";
            str += "</strong>";
            str += "</td>";
            str += "<td align='right'></td>";
            str += "</tr>";
            str += "</table>";
        }
        else{
            str += "<table border='0' cellspacing='0' style='font-size: 9pt; width: 100%;";
            str += "font-family: Verdana; border-collapse: collapse'>";
            str += "<tr>";
            str += "<td style='font-size: 9pt'>";
            str += "</td>";
            str += "<td align='center' style='color: #717b8e'>";
            str += "<strong>";
            str += arrayCalendar[6].getFullYear() + "年 " + monthName(arrayCalendar[6].getMonth())+"月";
            str += "</strong>";
            str += "</td>";
            str += "<td align='right' style='font-size: 9pt'>";
            str += "<a href='' objectSelected='next'>";
            //str += "<img src='"+ imagePath +"schedule_btn_2.jpg' alt='Go to the next month' style='border-top-style: none; border-right-style: none; border-left-style: none; border-bottom-style: none' />";
			str += ">>";
            str += "</a></td>";
            str += "</tr>";
            str += "</table>";
        }
    }
    str += "</td>";
    str += "</tr>";
    str += "<tr>";
    str += "<th abbr='Sunday' align='center' scope='col' style='font-weight: bold; height: 1px;";
    str += "background-color: #717b8e'>";
    //str += "<img src='" + imagePath + "sun.jpg' alt='Sunday' />";
	str +="日";
    str += "</th>";
    str += "<th abbr='Monday' align='center' scope='col' style='font-weight: bold; height: 1px;";
    str += "background-color: #717b8e'>";
    //str += "<img src='"+ imagePath +"mon.jpg' alt='Monday' />";
	str +="一";
    str += "</th>";
    str += "<th abbr='Tuesday' align='center' scope='col' style='font-weight: bold; height: 1px;";
    str += "background-color: #717b8e'>";
    //str += "<img src='" + imagePath + "tue.jpg' alt='Tuesday' />";
	str +="二";
    str += "</th>";
    str += "<th abbr='Wednesday' align='center' scope='col' style='font-weight: bold; height: 1px;";
    str += "background-color: #717b8e'>";
    //str += "<img src='"+ imagePath +"wed.jpg' alt='Wednesday' />";
	str +="三";
    str += "</th>";
    str += "<th abbr='Thursday' align='center' scope='col' style='font-weight: bold; height: 1px;";
    str += "background-color: #717b8e'>";
    //str += "<img src='"+ imagePath +"thu.jpg' alt='Thursday' />";
	str +="四";
    str += "</th>";
    str += "<th abbr='Friday' align='center' scope='col' style='font-weight: bold; height: 1px;";
    str += "background-color: #717b8e'>";
    //str += "<img src='"+ imagePath +"fri.jpg' alt='Friday' />";
	str +="五";
    str += "</th>";
    str += "<th abbr='Saturday' align='center' scope='col' style='font-weight: bold; height: 1px;";
    str += "background-color: #717b8e'>";
    //str += "<img src='"+ imagePath +"sat.jpg' alt='Saturday' />";
	str +="六";
    str += "</th>";
    str += "</tr>";
    var post = 0;
    var color = null;
    var bcolor = null;
    var fontWeight = null;
    var cur = new Date();
    
    for(var i=0;i<num;i++){        
        str += "<tr>";
        for(var j=0;j<7;j++){                
            str += "<td align='center' style='font-size: 9pt";
            if(post < arrayCalendar.length){
                if(arrayCalendar[post] != 0){
                    if(j == 0){
                        fontWeight = "bold";
                        color = "#ff9999";
                    }
                    else if(j == 6){
                        fontWeight = "bold";
                        color = "#003399";
                    }
                    else{
                        fontWeight = "normal";
                        color = "#000000";
                    }
                    if(cur.toDateString() == arrayCalendar[post].toDateString()){
                        bcolor = "#99ccff";
                    }
                    else{
                        if(findString(arr_selected,arrayCalendar[post].toDateString())){
                            bcolor = "#ffcc66";
                        }
                        else{
                            bcolor = "#f7f6f6";
                        }
                    }
                    if(!flagStatus){
                        if(compare(arrayCalendar[post],cur) < 0){
                            str += ";background-image: url(" + imagePath + "x_bg.jpg);color: " + color;
                            str += "'>";
                            str += arrayCalendar[post].getDate();
                        }
                        else{
                            str += ";background-color: "+ bcolor +"'>";
                            //str += "<a href='' objectSelected='"+ arrayCalendar[post] + "' style='font-weight: " + fontWeight + "; color: " + color + "' title='" + arrayCalendar[post].getMonth() + " " + arrayCalendar[post].getFullYear() + "'>";
							str += "<a href='' objectSelected='"+ arrayCalendar[post] + "' style='font-weight: " + fontWeight + "; color: " + color + "'>";
                            str += arrayCalendar[post].getDate();
                            str += "</a>";
                        }    
                    }
                    else{
                        str += ";background-color: "+ bcolor +"'>";
                        //str += "<a href='' objectSelected='"+ arrayCalendar[post] +"' style='font-weight: " + fontWeight + "; color: " + color + "' title='" + arrayCalendar[post].getMonth() + " " + arrayCalendar[post].getFullYear() + "'>";
						str += "<a href='' objectSelected='"+ arrayCalendar[post] +"' style='font-weight: " + fontWeight + "; color: " + color + "'>";
                        str += arrayCalendar[post].getDate();
                        str += "</a>";
                    }
                    
                }
                else{                        
                    str += "'>";
                }                                    
            }
            else{                
                str += "'>";
            }
            str += "</td>";
                            
            post++;
        }
        str += "</tr>";
    }
    str += "</table>"; 
    
    return str;
}
function fillCalendarHasBound(tableHTML){
    var str = "";
    
    str += "<table id= 'CalendarId' cellpadding='0' cellspacing='0' width='420' style='width: 446px; background-color: #FFFFFF;z-index: 100;'>";
    str += "<tr>";
    str += "<td colspan='3'><img height='15' src='" + imagePath + "scd_t_top.jpg' width='445' /></td>";
    str += "</tr>";
    str += "<tr>";
    str += "<td align='left'><img height='160' src='" + imagePath + "scd_t_l.jpg' width='24' /></td>";
    str += "<td align='center'>";
    str += tableHTML;
    str += "</td>";
    str += "<td align='right'><img height='160' src='" + imagePath + "scd_t_r.jpg' width='24' /></td>";
    str += "</tr>";
    str += "<tr>";
    str += "<td colspan='3'><img height='16' src='"+ imagePath +"scd_t_bot.jpg' width='445' /></td>";
    str += "</tr>";
    str += "</table>";
    
    return str;
}
function fillCalendar(arrayCalendar1,arrayCalendar2,hasBound,style){
    var str = "";
    if(!hasBound){
        if(style == "normal"){
            str += "<table id= 'CalendarId' cellpadding='0' cellspacing='0' style='width: 200px; background-color: #FFFFFF;z-index: 100; position: absolute'>";
            str += "<tr>";
            str += "<td style='vertical-align: top'>";        
            str += beginFillCalendar(arrayCalendar1,arr_data1,"Calendar1",style);
            str += "</td>";
            str += "</tr>";
            str += "</table>";
        }
        else{
            str += "<table id= 'CalendarId' cellpadding='0' cellspacing='0' style='width: 200px; background-color: #FFFFFF;z-index: 100; position: absolute'>";
            str += "<tr>";
            str += "<td style='vertical-align: top'>";        
            str += beginFillCalendar(arrayCalendar1,arr_data1,"Calendar1");
            str += "</td>";
            str += "<td style='vertical-align: top'>";
            str += beginFillCalendar(arrayCalendar2,arr_data2,"Calendar2");
            str += "</td>";
            str += "</tr>";
            str += "</table>";
        }
    }
    else{
        var lstr = "";
        
        lstr += "<table cellpadding='0' cellspacing='0'>";
        lstr += "<tr>";
        lstr += "<td style='vertical-align: top'>";        
        lstr += beginFillCalendar(arrayCalendar1,arr_data1,"Calendar1");
        lstr += "</td>";
        lstr += "<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>";
        lstr += "<td style='vertical-align: top'>";
        lstr += beginFillCalendar(arrayCalendar2,arr_data2,"Calendar2");
        lstr += "</td>";
        lstr += "</tr>";
        lstr += "</table>";
        
        str += fillCalendarHasBound(lstr);
    }
    
    return str;               
}
function getArrayCalendar(year,month){
    var ldate = new Date(year,month,1);
    var post = getDayInWeek(ldate);        
    var max = getMonthDays(ldate);
    
    ldate = new Date(year,month,1);
    var calendarArray = new Array(max + post);
    for(var i=0;i<post;i++){
        calendarArray[i] = 0;
    }
    var count = 0;
    for(var i=post;i< max + post;i++){
        calendarArray[i] = new Date(ldate.getFullYear(),ldate.getMonth(),ldate.getDate() + count);
        count++;
    }
    
    return calendarArray;
}
function getDayInWeek(dateTime){
    if((dateTime == null) || (dateTime == "undefined")){
        dateTime = new Date(currentYear,currentMonth,currentDate);
        return;
    }
    else{
        return dateTime.getDay();
    }        
}
function getMonthDays(dateTime){
    var _dateTime;
    
    if((dateTime == null) || (dateTime == "undefined")){
        _dateTime = new Date(currentYear,currentMonth,currentDate);
    }
    else{
        _dateTime = dateTime
    }
    
    do{
        _dateTime.setDate(_dateTime.getDate() + 1);
    }
    while(_dateTime.getDate() != 1)
    
    _dateTime.setDate(_dateTime.getDate() - 1);
    
    return _dateTime.getDate();
}    
function roundGreater(num){
    var str = num.toString();
    var arr = str.split(".");    
    if(arr.length > 1){
        return parseFloat(arr[0]) + 1;
    }
    else{
        return num;
    }
}
function monthName(str){
var lstr = null;
    switch(str){
        case 0:
            //lstr = "January";
			lstr = "1";
            break;
        case 1:
            //lstr = "February";
			lstr = "2";
            break;
        case 2:
            //lstr = "March";
			lstr = "3";
			break;
        case 3:
            //lstr = "April";
			lstr = "4";
            break;
        case 4:
            //lstr = "May";
			lstr = "5";
            break;
        case 5:
            //lstr = "June";
			lstr = "6";
            break;
        case 6:
            //lstr = "July";
			lstr = "7";
            break;
        case 7:
            //lstr = "August";
			lstr = "8";
            break;
        case 8:
           // lstr = "September";
		   lstr = "9";
            break;
        case 9:
            //lstr = "October";
			lstr = "10";
            break;
        case 10:
            //lstr = "November";
			lstr = "11";
            break;
        case 11:
            //lstr = "December";
			lstr = "12";
            break;
    }
    return lstr;
}
function findString(arr,value){
    for(var i=0;i<arr.length;i++){
        if(arr[i] == value){                
            return true;
        }
    }
    
    return false;
}
function compare(idate1,idate2){
    var date1 = new Date(idate1.getFullYear(),idate1.getMonth(),idate1.getDate());
    var date2 = new Date(idate2.getFullYear(),idate2.getMonth(),idate2.getDate());
    if(Date.parse(date1) < Date.parse(date2)){
        return -1;
    }
    else if(Date.parse(date1) == Date.parse(date2)){
        return 0;
    }
    else{
        return 1;
    }
}
function formatDateTime(dateTime){
    var d = new Date(dateTime);
    var strMonth = (d.getMonth()+ 1).toString();
    var strDate  = d.getDate().toString();
    
    if(strMonth.length == 1)
        strMonth = "0" + strMonth;
        
    if(strDate.length == 1)
        strDate = "0" + strDate;
        
    str = d.getYear() + "-" + strMonth + "-" + strDate;
    return str;
} 
function previous(){
    var month1 = null;
    var month2 = null;
    var year1 = null;
    var year2 = null;

    currentMonth--;
    if(currentMonth == -1){
        currentMonth = 11;
        currentYear--;
        
        month1 = currentMonth;
        year1 = currentYear;                    
        month2 = 0;
        year2 = currentYear + 1;
    }
    else{
        month1 = currentMonth;
        year1 = currentYear;    
        month2 = currentMonth + 1;
        year2 = currentYear;
    }
    var arr1 = null;
    var arr2 = null;
    
    if(style == "normal"){
        arr1 = getArrayCalendar(year1,month1);
    }
    else{
        arr1 = getArrayCalendar(year1,month1);
        arr2 = getArrayCalendar(year2,month2);
    }
    var str = fillCalendar(arr1,arr2,false,style);
    
    var popupBody = popup.document.body;
    popupBody.innerHTML = str;
    
    for (var i = 0; i < popupBody.all.length; i++) {
        if (popupBody.all[i].tagName == "A")
            popupBody.all[i].onclick = doClick;
    }
}
function next(){
    var month1 = null;
    var month2 = null;
    var year1 = null;
    var year2 = null;
    
    currentMonth++;
    if(currentMonth == 12){
        currentMonth = 0;
        currentYear++;
        
        month1 = currentMonth;
        year1 = currentYear;
        month2 = currentMonth + 1;
        year2 = currentYear;
    }
    else{
        month1 = currentMonth;
        year1 = currentYear;
        
        if(month1 == 11){
            month2 = 0;
            year2 = currentYear + 1;
        }
        else{
            month2 = currentMonth + 1;
            year2 = currentYear;
        }            
    }
    var arr1 = null;
    var arr2 = null;
    
    if(style == "normal"){
        arr1 = getArrayCalendar(year1,month1);
    }
    else{
        arr1 = getArrayCalendar(year1,month1);
        arr2 = getArrayCalendar(year2,month2);
    }
    var str = fillCalendar(arr1,arr2,false,style);
    
    var popupBody = popup.document.body;
    popupBody.innerHTML = str;
    
    for (var i = 0; i < popupBody.all.length; i++) {
        if (popupBody.all[i].tagName == "A")
            popupBody.all[i].onclick = doClick;
    }
}  
function doClick() {
    if(this.objectSelected == "previous"){
        previous();
    }
    else if(this.objectSelected == "next"){
        next();
    }
    else{
        document.getElementById(currentReturn).value = formatDateTime(this.objectSelected);
        //document.getElementById(nextObj).focus();
        popup.hide();
    }
    
  return false;
}

var Month_Names=new Array('January','February','March','April','May','June','July','August'
                            ,'September','October','November','December'
                            ,'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
var Day_Names=new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'
                        ,'Sun','Mon','Tue','Wed','Thu','Fri','Sat');
function Lz(x) {return(x<0||x>9?"":"0")+x}
function formatDateTimeNew(date,format) {
    format=format+"";
    var result="";
    var i_format=0;
    var c="";
    var token="";
    var y=date.getYear()+"";
    var M=date.getMonth()+1;
    var d=date.getDate();
    var E=date.getDay();
    var H=date.getHours();
    var m=date.getMinutes();
    var s=date.getSeconds();
    var yyyy,yy,MMM,MM,dd,hh,h,mm,ss,ampm,HH,H,KK,K,kk,k;
    // Convert real date parts into formatted versions
    var value=new Object();
    if (y.length < 4) {y=""+(y-0+1900);}
    value["y"]=""+y;
    value["yyyy"]=y;
    value["yy"]=y.substring(2,4);
    value["M"]=M;
    value["MM"]=Lz(M);
    value["MMM"]=Month_Names[M-1];
    value["NNN"]=Month_Names[M+11];
    value["d"]=d;
    value["dd"]=Lz(d);
    value["E"]=Day_Names[E+7];
    value["EE"]=Day_Names[E];
    value["H"]=H;
    value["HH"]=Lz(H);
    if (H==0){value["h"]=12;}
    else if (H>12){value["h"]=H-12;}
    else {value["h"]=H;}
    value["hh"]=Lz(value["h"]);
    if (H>11){value["K"]=H-12;} else {value["K"]=H;}
    value["k"]=H+1;
    value["KK"]=Lz(value["K"]);
    value["kk"]=Lz(value["k"]);
    if (H > 11) { value["a"]="PM"; }
    else { value["a"]="AM"; }
    value["m"]=m;
    value["mm"]=Lz(m);
    value["s"]=s;
    value["ss"]=Lz(s);
    while (i_format < format.length) {
        c=format.charAt(i_format);
        token="";
        while ((format.charAt(i_format)==c) && (i_format < format.length)) {
	        token += format.charAt(i_format++);
	        }
        if (value[token] != null) { result=result + value[token]; }
        else { result=result + token; }
        }
    return result;
}