// +-----------------------------------------------------------------------+
// | Copyright (c) 2003-2004, Mat B. Isakov                                |
// | All rights reserved.                                                  |
// |                                                                       |
// | Redistribution and use in source and binary forms, with or without    |
// | modification, are permitted provided that the following conditions    |
// | are met:                                                              |
// |                                                                       |
// | o Redistributions of source code must retain the above copyright      |
// |   notice, this list of conditions and the following disclaimer.       |
// | o Redistributions in binary form must reproduce the above copyright   |
// |   notice, this list of conditions and the following disclaimer in the |
// |   documentation and/or other materials provided with the distribution.|
// | o The names of the authors may not be used to endorse or promote      |
// |   products derived from this software without specific prior written  |
// |   permission.                                                         |
// |                                                                       |
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS   |
// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT     |
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
// | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT  |
// | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
// | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT      |
// | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
// | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
// | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT   |
// | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
// | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  |
// |                                                                       |
// +-----------------------------------------------------------------------+
// | Author: Mat B. Isakov <matarm@bk.ru>                                  |
// +-----------------------------------------------------------------------+

function Calendar(current_day,current_month,current_year,uri){
 this.iconPrev='img/arr_sm_lt.gif';
 this.iconNext='img/arr_sm_rt.gif';
 this.iconx=16;
 this.icony=16;
 if (current_month > 12) current_month=1;
 if (current_month < 1) current_month=12;
 this.current_day = current_day;
 this.current_month = --current_month;
 this.current_year = current_year;
 this.uri = uri;
 this.UriVarDay = 'Cd';
 this.UriVarMonth = 'Cm';
 this.UriVarYear = 'Cy';
 this.styleNameTable = "clTable";
 this.styleNameNavigate = "clButtons";
 this.styleNameMonth = "clMonth";
 this.styleNameHeader = "clHeader";
 this.styleNameHolyDay = "clHoly";
 this.styleNameOrdynaryTD = "clOrdynary";
 this.styleNameEmptyTD = "clEmpty";
 this.styleNameCurrentTD = "clCurrent";
 this.styleNameHoverTD = "clHover";
 this.TableSpacing = 2;
 this.TablePadding = 0;
 this.Lang = 'ru';
 this.monthName = new Object();
 this.monthName['ru'] = ['ßíâàðü','Ôåâðàëü','Ìàðò','Àïðåëü','Ìàé','Èþíü','Èþëü','Àâãóñò','Ñåíòÿáðü','Îêòÿáðü','Íîÿáðü','Äåêàáðü'];
 this.monthName['en'] = ['January','February','March','April','May','June','Julay','August','September','Octovber','November','December'];
 this.wdayName = new Object();
 this.wdayName['ru'] = ['Ïí','Âò','Ñð','×ò','Ïò','Ñá','Âñ'];
 this.wdayName['en'] = ['Mn','Tu','Wd','Th','Fr','St','Su'];
 this.textLabels = new Object();
 this.textLabels['ru'] = ['Cëåäóþùèé','Ïðåäûäóùèé','ìåñÿö','ãîä'];
 this.textLabels['en'] = ['Next','Previous','month','year'];

 this.show = function(){
  var output;
  var r;
  var l=1;
  var currentMonth = stepMonth = (new Date(this.current_year,this.current_month,this.current_day)).getMonth();
  while (currentMonth != this.current_month){
   this.current_day--;
   currentMonth = stepMonth = (new Date(this.current_year,this.current_month,this.current_day)).getMonth();
  }
  output = "<table class=\""+this.styleNameTable+"\" cellpadding=\""+this.TablePadding+"\" cellspacing=\""+this.TableSpacing+"\" border=\"0\">";
  output += "<tr><td class=\""+this.styleNameNavigate+"\" title=\""+this.textLabels[this.Lang][1]+"&nbsp;"+this.textLabels[this.Lang][2]+"\" onClick=\"document.location.href='"+this.uri+"&"+this.UriVarDay+"="+this.current_day+"&"+this.UriVarMonth+"="+eval("this.current_month < 1? 12 : this.current_month")+"&"+this.UriVarYear+"="+this.current_year+"'\"><img src=\""+this.iconPrev+"\" width=\""+this.iconx+"\" height=\""+this.icony+"\" border=\"0\"></td><td colspan=\"5\" class=\""+this.styleNameMonth+"\">"+this.monthName[this.Lang][this.current_month]+"</td><td class=\""+this.styleNameNavigate+"\" title=\""+this.textLabels[this.Lang][0]+"&nbsp;"+this.textLabels[this.Lang][2]+"\" onClick=\"document.location.href='"+this.uri+"&"+this.UriVarDay+"="+this.current_day+"&"+this.UriVarMonth+"="+eval("this.current_month+2")+"&"+this.UriVarYear+"="+this.current_year+"'\"><img src=\""+this.iconNext+"\" width=\""+this.iconx+"\" height=\""+this.icony+"\" border=\"0\"></td></tr>";
  output += "<tr><td class=\""+this.styleNameHeader+"\">"+this.wdayName[this.Lang][0]+"</td><td class=\""+this.styleNameHeader+"\">"+this.wdayName[this.Lang][1]+"</td><td class=\""+this.styleNameHeader+"\">"+this.wdayName[this.Lang][2]+"</td><td class=\""+this.styleNameHeader+"\">"+this.wdayName[this.Lang][3]+"</td><td class=\""+this.styleNameHeader+"\">"+this.wdayName[this.Lang][4]+"</td><td class=\""+this.styleNameHolyDay+"\">"+this.wdayName[this.Lang][5]+"</td><td class=\""+this.styleNameHolyDay+"\">"+this.wdayName[this.Lang][6]+"</td></tr>";
  while(currentMonth == stepMonth) {
   output += "<tr>";
   r=1;
   while(r < 8){
    stepDate = new Date(this.current_year,this.current_month,l);
    stepWday = stepDate.getDay();
    stepWday = stepWday==0?7:stepWday;
    stepMonth = stepDate.getMonth();
    if((currentMonth==stepMonth) && (stepWday==r)){
     if (this.current_day==l)
      output += "<td class=\""+this.styleNameCurrentTD+"\">"+l;
     else 
      output += "<td class=\""+this.styleNameOrdynaryTD+"\" onMouseOver=\"this.className='"+this.styleNameHoverTD+"'\" onMouseOut=\"this.className='"+this.styleNameOrdynaryTD+"'\" onClick=\"document.location.href='"+this.uri+"&"+this.UriVarDay+"="+l+"&"+this.UriVarMonth+"="+eval("this.current_month+1")+"&"+this.UriVarYear+"="+this.current_year+"'\">"+l;
     l++;
    }
    else
     output += "<td class=\""+this.styleNameEmptyTD+"\">&nbsp;";
    output += "</td>";
    r++;
   }
   stepMonth = (new Date(this.current_year,this.current_month,l)).getMonth();
   output += "</tr>";
  } 
//  output += "<tr><td class=\""+this.styleNameNavigate+"\" title=\""+this.textLabels[this.Lang][1]+"&nbsp;"+this.textLabels[this.Lang][3]+"\" onClick=\"document.location.href='"+this.uri+"&"+this.UriVarDay+"="+this.current_day+"&"+this.UriVarMonth+"="+eval("this.current_month+1")+"&"+this.UriVarYear+"="+eval("this.current_year-1")+"'\"><img src=\""+this.iconPrev+"\" width=\""+this.iconx+"\" height=\""+this.icony+"\" border=\"0\"></td><td colspan=\"5\" class=\""+this.styleNameMonth+"\">"+this.current_year+"</td><td class=\""+this.styleNameNavigate+"\" title=\""+this.textLabels[this.Lang][0]+"&nbsp;"+this.textLabels[this.Lang][3]+"\" onClick=\"document.location.href='"+this.uri+"&"+this.UriVarDay+"="+this.current_day+"&"+this.UriVarMonth+"="+eval("this.current_month+1")+"&"+this.UriVarYear+"="+eval("this.current_year+1")+"'\"><img src=\""+this.iconNext+"\" width=\""+this.iconx+"\" height=\""+this.icony+"\" border=\"0\"></td></tr>";
  output += "</table>";
  document.write(output);
 }
}