
var today	= "2";
var day		= "";
var month	= "";
var year	= "";
var d_cDate = new Date();
var i_cYear = d_cDate.getYear();
var i_cMonth = d_cDate.getMonth();

function nDas(_month,_year){
	var monthDays	= new montharray(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	if ((_month == 1 && ((_year % 4 == 0) && (_year % 100 != 0)) || (_year % 400 == 0)))
        return monthDays[1] = 29;
    return monthDays[_month] 
}

function montharray(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11){
    this[0] = m0; this[1] = m1; this[2] = m2; this[3] = m3;
    this[4] = m4; this[5] = m5; this[6] = m6; this[7] = m7;
    this[8] = m8; this[9] = m9; this[10] = m10; this[11] = m11;
}

function fillYear(_year){
	lclStr	= ''
	year	= _year
	tmpDate = new Date()
	for (i= tmpDate.getFullYear() ; i < tmpDate.getFullYear() + 2 ;  i++)
		{
			var sel = ""
			if (i == _year) {sel = "selected"}
			lclStr += '<option ' + sel + ' >' + i + ' </opstion>'
		}
	return lclStr	
}

function fillMonth(monthNames,_month){
	lclStr = ''
	for (i=0 ; i <=11; i++)
	{
		var sel = ""
		if (_month == i+1 ) {sel = "selected"}
		lclStr += '<option value="' + i + '" ' + sel + '>' + monthNames.substring(i * 9, (i + 1) * 9) + ' </opstion>'
	}	
	return lclStr	
}

function markDays(lastDay,_today,color){
	if(color=='blue') color="#8bb3c2";
	else color="#ff9900";
	
	document.getElementById("day_" + today).style.backgroundColor = "#ffffff"
	document.getElementById("day_" + today).style.color = "#000000"
	//document.getElementById("day_" + _today).style.backgroundColor = "#ff9900"
	document.getElementById("day_" + _today).style.backgroundColor = color;
	document.getElementById("day_" + _today).style.color = "#FFFFFF"
	today = _today;

	if (today > 0){
		returnValue = today + "," + month + "," + year;
		UpdateDate(source, returnValue);
		if (source=='pick' && !document.form1.resid)
			add3Day(document.form1); 
		if(!document.form1.resid)
			numOfDays(document.form1);
		document.getElementById("calendarDiv").style.display = 'none';
	}
}

function moveTo(_year,_month){
	//i_cYear i_cMonth Are curing dates.
	if ((_year==i_cYear)&&(_month<i_cMonth)) return
	if ((_year==i_cYear)&&(_month>11)) {
		_year++;
		_month=0;
	}
	//alert("_year="+_year+"  _month="+_month)
	if ((_year==i_cYear+1)&&(_month==-1)) {
		_year--;
		_month=11;
	}
	if (_month == 0){ _month = 12  }
	if (_month == 13){  _month = 1 }
	rebuild(_year,_month)
}

function rebuild(_year,_month){
	
	document.getElementById('td_calendar').innerHTML =  calendar(_year,_month,1)
}


// Create the calender
function calendar(_year,_month,day,color){
    
    if (_month == undefined){_month = "4"} 
    if (_year == undefined) {_year = "2002"}
    
    month	= ++_month;
    year	= _year;
    today	= day
    var returnCal = ""
    
    
    var Days		= new Array ;
    for (i=1 ; i<=31 ; Days[i++] = false );
    for (i=0 ; i < today.length ; Days[today[i++]] = true );
    var monthNames	= "January  February March    April    May      June     July     August   SeptemberOctober  November December "
    var monthDays	= new montharray(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
		thisMonth	= new Date(_month + "/1/" + _year); // start the _month and _year (MM/dd/YYYY)
    var thisDay		= 23 //thisDay = thisMonth.getDate();
    
    var _month		= thisMonth.getMonth()
    
    if (((_year % 4 == 0) && (_year % 100 != 0)) || (_year % 400 == 0))
        monthDays[1] = 29;

    nDays = monthDays[_month];
    firstDay = thisMonth;
    firstDay.setDate(1);
    startDay = firstDay.getDay();
    
    // Strat the table
    returnCal +=('<TABLE border="0" cellspacing="1" cellpadding="0" width="220" bgcolor="#D4D0C0" dir="'+dir+'" style="color:#000000; font-size:12;FONT-FAMILY: Arial;">');
    // Title
    returnCal +=('<tr><td colspan="7">')
    // Dropdown list
    var dropStr	= '<table dir="'+dir+'" border="0" cellspacing="1" cellpadding="0"> \
						<tr> \
							<td> \
								<SELECT onchange=rebuild(' + _year +',this.options[this.selectedIndex].value) align="'+align+'" id="selMonth" style="WIDTH: 90px; font-size:12; background-color:#ffffff" name=select1> \
									' + fillMonth(monthNames,++_month) + ' \
								</SELECT> \
							</td> \
							<td> \
								<SELECT onchange=rebuild(this.options[this.selectedIndex].text,' +  _month + ') id="selYear" style="WIDTH: 60px; font-size:12; background-color:#ffffff" name=select1> \
									' + fillYear(_year) + ' \
								</SELECT> \
							</td> \
						</tr> \
					</table>'
			
			returnCal +=('<TABLE border="0" cellspacing="0" cellpadding="0" width="100%" bgcolor="#084A86" dir="rtl" style="color:#000000; font-size:12;FONT-FAMILY: Arial;">');
			returnCal +=('<TR bgcolor = "#000000" align="center" style="color:#000000; font-size:12;FONT-FAMILY: Wingdings 3;"><TD onclick=moveTo(' + _year + ',' + (_month * 1 ) + ') style="cursor: pointer;font-size:10px;color:#ffffff">&#x25ba;</TD>')
    
			returnCal +=('<TD  bgcolor = "#000000" COLSPAN=5 style="padding: 3px;">');
			returnCal +=(dropStr);  
			returnCal +=('</td><TD onclick=moveTo(' + _year + ',' + (_month * 1 - 2) +') style="cursor:pointer; color:#ffffff; font-size:10">&#x25c4;</TD>')
			returnCal +=('</table>')
    returnCal +=('</td></tr>')
    // Days Bar
    
    returnCal +=('<TR align="center" class="'+color+'BG" style="font:bold; color:#ffffff; ">');
  
    if(lang=="HU")
			returnCal +=('<TD>V</TD><TD>H</TD><TD>K</TD><TD>SZ</TD><TD>CS</TD><TD>P</TD><TD>SZ</TD>');
    else if(lang=="RU")
			returnCal +=('<TD>&#1042;&#1089;</TD><TD>&#1055;&#1085;</TD><TD>&#1042;&#1090;</TD><TD>&#1057;&#1088;</TD><TD>&#1063;&#1090;</TD><TD>&#1055;&#1090;</TD><TD>&#1057;&#1073;</TD>');
    else if(lang=="CH")
			returnCal +=('<TD>Ne</TD><TD>Po</TD><TD>Ut</TD><TD>St</TD><TD>Ct</TD><TD>Pa</TD><TD>So</TD>');
    else if(lang=="RO")
			returnCal +=('<TD>D</TD><TD>L</TD><TD>M</TD><TD>M</TD><TD>J</TD><TD>V</TD><TD>S</TD>');
    else if(lang=="EN")
			returnCal +=('<TD>S</TD><TD>M</TD><TD>T</TD><TD>W</TD><TD>T</TD><TD>F</TD><TD>S</TD>');
		else	
			returnCal +=('<TD>à</TD><TD>á</TD><TD>â</TD><TD>ã</TD><TD>ä</TD><TD>å</TD><TD>ù</TD>');
    
    returnCal +=('</TR><TR>');
    column = 0;
    
    // Blank days (if the first day is not Sunday)
    for (i=0; i<startDay; i++){
        returnCal +=('<TD bgcolor="#FFFFFF">&nbsp</td>');
        column++;
    }

    // Fill all _month days
    for (i=1; i<=nDays; i++){
        if (day == i)
            returnCal +=('<TD onclick=markDays(' + today + ',' + i +',"'+color+'") id = "day_' + i + '"  style="CURSOR: pointer;color:#000000" class="'+color+'BG">');
        else returnCal +=('<TD onclick=markDays(' + today + ',' + i +',"'+color+'") id = "day_' + i + '"  style="background-color:#FFFFFF;CURSOR: pointer">');
        returnCal +=('<CENTER>');
        returnCal +=(i);
        returnCal +=('</CENTER></td>');
        
        column++;
        if (column == 7){
            
            returnCal +=('</tr><TR>');
            column = 0;
        }
    }
    
    // Finish the last cells after the last day.
    for (i=column; (i<7) && (i != 0) ; i++)
		returnCal +=('<TD bgcolor="#FFFFFF">&nbsp</td>');	
	
    returnCal +=('</TABLE>');
    return returnCal
}

