﻿
var names=new Array("فروردين", "ارديبهشت", "خرداد", "تير", "مرداد", "شهريور", "مهر", "آبان", "آذر", "دي", "بهمن", "اسفند");
var startyear='1300'  ;
var endyear='';

var image_dir="calImages/"
var cancelText = "Cancel";

var dow   = new Array('شنبه','يک شنبه','دو شنبه','سه شنبه','چهار شنبه','پنج شنبه','جمعه');
var Emroz="";
var days  = new Array(31,31,31,31, 31, 31, 30, 30, 30, 30, 30, 29);

function padout(number) {  return (number < 10) ? '0' + number : number; }


function Calendar(M,Y,D,T) {
    
    var Month;
    var Year;
    var Day;
	var sM=new String;
	var sD=new String;
      Month=M;
     Year=Y;
     Day=D;
    if (T=='0')  
    {
    var HejriDate=ToShamsi(Year,Month,Day,'Short');
    Emroz=ToShamsi(Year,parseInt(Month),parseInt(Day),'Long');
    Year=parseInt(HejriDate.substr(0,4));
    Month=HejriDate.substr(5,2);
    Day=parseInt(HejriDate.substr(8,2));
    sD=Day;
    sM=Month;
    if ((sM=='08') || (sM=='09')) Month=sM.substr(1,1);
    if ((sD =='08') || (sD=='09') ) Day=sD.substr(1,1);
	
    Month=parseInt(Month);
	Day=parseInt(Day);
    }
   
    var output = '';
  
    var MilladyDay=ToGregorian(Year,Month,1);
    var NewYear=MilladyDay.substr(0,4);
    var NewMonth=MilladyDay.substr(5,2);
    var NewDay=MilladyDay.substr(8,2);
   
    var firstDay = new Date(NewYear,NewMonth-1,NewDay);
	if (T=='0')   endyear=Year;
  
    var startDay = firstDay.getDay()+1;
    if (startDay==7) startDay=0;
    if  (hshIsLeap (Year))
      days[12]=30;
     else
     days[12]=29;
    
   
    output += '<FORM NAME="Cal"><TABLE CELLPADDING=0 CELLSPACING=0 BORDER=0 WIDTH=28%><TR Style="background-color:#CCCCCC"><TD ROWSPAN=2 colspan=2><IMG SRC=' + image_dir + 'panel_top_left.gif height=5 width=5><\/TD><TD WIDTH=1000 HEIGHT=1 style="background-color:#FFFFFF"><IMG SRC=' + image_dir + 'pixel_color6.gif><\/TD><TD ROWSPAN=2 colspan=2><IMG SRC=' + image_dir + 'panel_top_right.gif height=5 width=5><\/TD></TR><TR><TD height=4 Style="background-color:#CCCCCC"><img src=' + image_dir + 'pixel_gray5.gif><\/td></tr><tr><td width=1 style="background-color:#FFFFFF"><img src=' + image_dir + 'pixel_color6.gif><\/td><td width=4 style="background-color:#CCCCCC"><img src=' + image_dir + 'pixel_gray5.gif><\/td><td style="background-color:#CCCCCC">';
    output += '<table border=0 width=100%><tr><td align=left valign=middle width=15%><a style="cursor:hand;" onclick="parent.skipback2(S,document.Cal.Year.options[document.Cal.Year.selectedIndex].value,document.Cal.Month.options[document.Cal.Month.selectedIndex].value);"><img title="سال قبل" src=' + image_dir + 'icon_previous_block.gif border=0><\/a><a style="cursor:hand;" onclick="parent.skipback(S,document.Cal.Year.options[document.Cal.Year.selectedIndex].value,document.Cal.Month.options[document.Cal.Month.selectedIndex].value);"><img title="ماه قبل" src=' + image_dir + 'icon_previous.gif border=0><\/a><\/td>';

    output += '<td  align=center valign=middle>';
    output += '<SELECT NAME="Month" style="font-family: Tahoma, Verdana, sans-serif;" onChange="parent.refreshCal(S,document.Cal.Year.options[document.Cal.Year.selectedIndex].value,document.Cal.Month.options[document.Cal.Month.selectedIndex].value);">';
	
    for (month=0; month<12; month++) {
        if (month == Month-1) output += '<OPTION VALUE="' + month + '" SELECTED>' + names[month] + '<\/OPTION>';
        else                output += '<OPTION VALUE="' + month + '">'          + names[month] + '<\/OPTION>';
    }

    output += '<\/SELECT>&nbsp;<SELECT NAME="Year" style="font-family: Tahoma, Verdana, sans-serif;" onChange="parent.refreshCal(S,document.Cal.Year.options[document.Cal.Year.selectedIndex].value,document.Cal.Month.options[document.Cal.Month.selectedIndex].value);">';

   startyear=parseInt(startyear);
   endyear=parseInt(endyear);
    for (year=startyear; year<endyear+10 ; year++) {
        if ( ((year % 10)==0)||(year == Year) )
        {
        if (year == Year) output += '<OPTION VALUE="' + year + '" SELECTED>' + year + '<\/OPTION>';
        else              output += '<OPTION VALUE="' + year + '">'          + year + '<\/OPTION>';
    }
    }

    output += '<\/SELECT><\/TD><td align=right valign=middle width=15%><a style="cursor:hand;" onclick="parent.skipforward(S,document.Cal.Year.options[document.Cal.Year.selectedIndex].value,document.Cal.Month.options[document.Cal.Month.selectedIndex].value);"><img title="ماه بعد" src=' + image_dir + 'icon_next.gif border=0><\/a><a style="cursor:hand;" onclick="parent.skipforward2(S,document.Cal.Year.options[document.Cal.Year.selectedIndex].value,document.Cal.Month.options[document.Cal.Month.selectedIndex].value);"><img title="سال بعد" src=' + image_dir + 'icon_next_block.gif border=0><\/a><\/td><\/tr><\/table>';

    //Calendar Table
    output += '<TABLE CELLSPACING=0 CELLPADDING=0 BORDER=0><TR><TD height=5><\/td><\/tr><TR>';

    //Weekday header row
    output += '<TD Style="background-color:#6699CC"><img src=' + image_dir + 'calendar_top_left.gif></td>';
    for (i=0; i<7; i++)
        output += '<TD WIDTH=35  style=" font-family: Tahoma, Verdana, sans-serif;    font-size:10PT;      background-color: #6699CC" ALIGN=CENTER VALIGN=MIDDLE>' + dow[i] +'<\/TD>';
		output += '<TD Style="background-color:#6699CC"><img src=' + image_dir + 'calendar_top_right.gif></td>';


    output += '<\/TR><TR ALIGN=CENTER VALIGN=MIDDLE><TD style="background-color: #FFFFFF"><img src=' + image_dir + 'pixel_transparent.gif><\/td>';

    var column = 0;
    var row=1;
    
    var lastMonth = Month - 1;
    if (lastMonth == -1) lastMonth = 11;
    
    for (i=0; i<startDay; i++, column++)
        output += '<TD WIDTH=35 HEIGHT=20   style="font-family:tohama, Helvetica, Geneva, sans-serif; background-color:#FFFFFF;   color: #999999;  font-size:10PT;  font-weight: bold; cursor:hand;  text-decoration:none"  onclick="parent.skipback(S,document.Cal.Year.options[document.Cal.Year.selectedIndex].value,document.Cal.Month.options[document.Cal.Month.selectedIndex].value);">' + (days[lastMonth]-startDay+i+1) + '<\/TD>';

    for (i=1; i<=days[Month-1]; i++, column++) {
        output += '<TD WIDTH=35 HEIGHT=20 style="background-color: #FFFFFF">' + '<A style=" font-family:tohama, Helvetica, Geneva, sans-serif; background-color:#FFFFFF;   color: #000000;  font-size:10PT; cursor:hand; font-weight: bold;  text-decoration:none" onclick="parent.changeDay(document.Cal.Year.options[document.Cal.Year.selectedIndex].value,document.Cal.Month.options[document.Cal.Month.selectedIndex].value,' + i + ')">' + i + '<\/A>' +'<\/TD>';
        if (column == 6) {
    column = -1;
    output += '<TD Style="background-color: #FFFFFF"><img src=' + image_dir + 'pixel_transparent.gif><\/TD><\/TR>';
    if (i<days[Month-1]) {
      row ++;
              output += '<TR ALIGN=CENTER VALIGN=MIDDLE><TD  style="background-color: #FFFFFF"><img src=' + image_dir + 'pixel_transparent.gif><\/TD>';
    }
        }
    }
    if (column > 0) {
        for (i=1; column<7; i++, column++) {
            output += '<TD WIDTH=35 HEIGHT=20 Style="background-color: #FFFFFF"><A style=" font-family:tohama, Helvetica, Geneva, sans-serif;cursor:hand;   color: #999999;  font-size:10PT;  font-weight: bold;  text-decoration:none" onclick="parent.skipforward(S,document.Cal.Year.options[document.Cal.Year.selectedIndex].value,document.Cal.Month.options[document.Cal.Month.selectedIndex].value)">' + i + '<\/A><\/TD>';
    }
    output += '<TD Style="background-color: #FFFFFF"><img src=' + image_dir + 'pixel_transparent.gif><\/TD><\/TR>';
    }

    output += '<TR><TD><img src=' + image_dir + 'white_container_bottom_left.gif><\/td><td Style="background-color: #FFFFFF" colspan=7><img src=' + image_dir + 'pixel_transparent.gif><\/td><TD><img src=' + image_dir + 'white_container_bottom_right.gif><\/td><\/tr>'
  output+='<tr><td colspan=9 height=20><\/td><\/tr>';
   while (row < 6) {
	//alert(row);
    output += '<tr><td colspan=9 height=20><\/td><\/tr>';
    row++; }
  output+='<tr bgcolor=#CCCCCC ><td dir=rtl style="font-weight: bold; color: #0047be; font-size: 12px; 	font-family: Tahoma; background-color: #cccccc;"  colspan=9> امروز:'+ Emroz + '  <\/td><\/tr>';
 

    output += '<\/TABLE>';

//begin bottom of outer container
output += '<\/td><td width=4 Style="background-color:#CCCCCC"><img src=' + image_dir + 'pixel_gray5.gif width=4><\/td><td width=1 Style="background-color:#333333"> <img src=' + image_dir + 'pixel_gray2.gif><\/td><\/tr><TR bgcolor=#CCCCCC><TD ROWSPAN=2 colspan=2><IMG SRC=' + image_dir + 'panel_bottom_left.gif height=5 width=5><\/TD><TD HEIGHT=4 bgcolor=#CCCCCC><IMG SRC=' + image_dir + 'pixel_gray5.gif><\/TD><TD ROWSPAN=2 colspan=2><IMG SRC=' + image_dir + 'panel_bottom_right.gif height=5 width=5><\/TD></TR><TR><TD height=1 bgcolor=#333333><img src=' + image_dir + 'pixel_gray2.gif><\/td><\/tr><tr><td height=3><\/td><\/TR>\</table>';

    output += '<table border=0 width=100%><tr><td align=right valign=middle>';
   // output += '<TABLE CELLPADDING=0 CELLSPACING=0 BORDER=0><TR><TD ROWSPAN=5><IMG SRC=' + im翽ge_dir + 'button_left_round_default.gif></TD><TD Style="background-color:#333333"><IMG SRC=' + image_dir + 'pixel_gray2.gif></TD><TD ROWSPAN=5><IMG SRC=' + image_dir + 'button_right_round_default.gif></TD><TR><TD Style="background-color:#FFFFFF"><IMG SRC=' + image_dir + 'pixel_color6.gif></TD></TR></TABLE>';
    output += '<\/td><\/tr><\/table><\/FORM>';

    return output;
}


function changeMonth(Month) {

}
function refreshCal(S,Y,M)
{

  
  var y=parseInt(Y);
  var m=parseInt(M)+1;
  S.innerHTML=Calendar(m,y,1,'1');
}
function changeYear() {
   opener.year = document.Cal.Year.options[document.Cal.Year.selectedIndex].value + '';
   location.reload();
}

function skipback(S,Y,M) { 

  var y=parseInt(Y);
  var m=parseInt(M);
  if (m == 0 ) {
    if (y == 0) return; 
    else   {y = y - 1;
       m = 11;
      } 
  }
  else { m = m - 1;
   }
  m=m+1;
  S.innerHTML=Calendar(m,y,1,'1');
}
function skipback2(S,Y,M) { 

  var y=parseInt(Y)-1;
  var m=parseInt(M);
	m=m+1;
  S.innerHTML=Calendar(m,y,1,'1');
 // alert(S.innerHTML);
}

function skipforward(S,Y,M) { 
   var y=parseInt(Y);
   var m=parseInt(M);
  if (m == 11 ) {
    if (y == 100) return; 
    else   {y = y + 1;
       m = 0;
      } 
  }
  else { m = m + 1; }
  m=m+1;
  S.innerHTML=Calendar(m,y,1,'1');
}
function skipforward2(S,Y,M) { 
   var y=parseInt(Y)+1;
   var m=parseInt(M);
   m=m+1;
   S.innerHTML=Calendar(m,y,1,'1');
}




var grgSumOfDays=Array(Array(0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365),Array(0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366));
var hshSumOfDays=Array(Array(0, 31, 62, 93, 124, 155, 186, 216, 246, 276, 306, 336, 365), Array(0, 31, 62, 93, 124, 155, 186, 216, 246, 276, 306, 336, 366));

function ToShamsi(grgYear,grgMonth,grgDay,Format)
{
	var hshYear = grgYear-621;
	var hshMonth,hshDay;
	
	var grgLeap=grgIsLeap (grgYear);
	var hshLeap=hshIsLeap (hshYear-1);
	
	var hshElapsed;
	
	var grgElapsed = grgSumOfDays[(grgLeap ? 1:0)][grgMonth-1]+grgDay;
	var XmasToNorooz = (hshLeap && grgLeap) ? 80 : 79;
    
	if (grgElapsed <= XmasToNorooz)
	{
		hshElapsed = grgElapsed+286;
		hshYear--;
		if (hshLeap && !grgLeap)
			hshElapsed++;
	}
	else
	{
		hshElapsed = grgElapsed - XmasToNorooz;
		hshLeap = hshIsLeap (hshYear);
	}
 
	for (var i=1; i <= 12 ; i++)
	{
		if (hshSumOfDays [(hshLeap ? 1:0)][i] >= hshElapsed)
		{
			hshMonth = i;
			hshDay = hshElapsed - hshSumOfDays [(hshLeap ? 1:0)][i-1];
			break;
		}
	}


	if (Format=="Long")
		return hshDayName (hshDayOfWeek(hshYear,hshMonth,hshDay)) + "  " + hshDay + " " + calNames("hf", hshMonth-1) + " " + hshYear;
	else
		return padout(hshYear) + "/" + padout(hshMonth) + "/" +padout(hshDay);
}

function ToGregorian (hshYear,hshMonth,hshDay)
{
	var grgYear = hshYear+621;
	var grgMonth,grgDay;

	var hshLeap=hshIsLeap (hshYear);
	var grgLeap=grgIsLeap (grgYear);

	var hshElapsed=hshSumOfDays [hshLeap ? 1:0][hshMonth-1]+hshDay;
	var grgElapsed;

	if (hshMonth > 10 || (hshMonth == 10 && hshElapsed > 286+(grgLeap ? 1:0)))
	{
		grgElapsed = hshElapsed - (286 + (grgLeap ? 1:0));
		grgLeap = grgIsLeap (++grgYear);
	}
	else
	{
		hshLeap = hshIsLeap (hshYear-1);
		grgElapsed = hshElapsed + 79 + (hshLeap ? 1:0) - (grgIsLeap(grgYear-1) ? 1:0);
	}

	for (var i=1; i <= 12; i++)
	{
		if (grgSumOfDays [grgLeap ? 1:0][i] >= grgElapsed)
		{
			grgMonth = i;
			grgDay = grgElapsed - grgSumOfDays [grgLeap ? 1:0][i-1];
			break;
		}
	}

	return padout(grgYear)+"/"+padout(grgMonth)+"/"+padout(grgDay);
}

function grgIsLeap (Year)
{
	return ((Year%4) == 0 && ((Year%100) != 0 || (Year%400) == 0));
}

function hshIsLeap (Year)
{
	Year = (Year - 474) % 128;
	Year = ((Year >= 30) ? 0 : 29) + Year;
	Year = Year - Math.floor(Year/33) - 1;
	return ((Year % 4) == 0);
}

function getHolidays(year, month)
{
 switch (year) {
  case 1380: return Array("xxxx56789x1xx4xxx890123x567890x", "123456x890123x567890x234567x901", "x2xx567890x23xx67x9x1234x678901", "x234567x901234x678901x345678x01", "1234x678901x345678x012345x78901", "xx345678x012345x789翽12x456789x1", "12345x78x012x456789x12x456x890", "123x56789xx234567x901234x67890", "1x345678x012345x789012x45x789x", "123456x890123x56789xx234567x90", "1234x678901x345678x01x345x7890", "12xx56789x1x3456x890123x5678x")[month-1];
  case 1381: return Array("xxxxx678x01xx45x789012x456789x1", "12345x789012x456789xx2x456x8901", "12x45678xx123xx6x890123x567890x", "123456x890123x567890x234567x901", "123x567890x234567x90x234x678901", "x234567x901234x678901x345678xx1", "1234x678901xx45678x012345x789x", "12x456789x123456x890123x567890", "x234x67x901234x678901x345678x0", "12345x78x012x456789x123456x890", "123x567890x234567x901xx4x67890", "xx345678x012345x78901xx45678x")[month-1];
  case 1382: return Array("xxxx567x901xx4x678901x345678x01", "12x4x67890xx345678x012345x78x01", "1x345678x0123xxx789012x456789x1", "12345x789012x456789x123456x8901", "12x456789xx23456x890123x567890x", "123456x890123x5678x0x234567x901", "1x3x567890x234567x9x1234x67890", "1x345678x012345x789012x4x6789x", "1234x6x890123x567890x234567xx0", "1234x678901x345678x012345x7890", "12x456789x12x456x890xx3x567890", "x234567x90xx34x678901x345678x")[month-1];
  case 1383: return Array("xxxx56x8901xxx567890x234567x901", "123x567890x234567x901234x678901", "x234567x90123xx678901x345678x01", "1234x678901x345678x012345x78901", "1x345678x012345x789012x456789x1", "12345x789012x456789x123456x8901", "12x456789x123456x890123x567890", "x234567x901234x678901x345678x0", "12345x789012x456789x123456x890", "123x567890x234567x901234x67890", "1x345678x012345x78901xx456789x", "123456x890123x567890x234567xx0")[month-1];
  }
}
function hshDayOfWeek(hshYear, hshMonth, hshDay)
{
	var value;
	value = hshYear - 1376 + hshSumOfDays[0][hshMonth-1] + hshDay - 1;

	for (var i=1380; i<hshYear; i++)
		if (hshIsLeap(i)) value++;
	for (var i=hshYear; i<1380; i++)
		if (hshIsLeap(i)) value--;

	value=value%7;
	if (value<0) value=value+7;

	return (value);
}

function hshDayName(DayOfWeek)
{
	return calNames("df", DayOfWeek%7);
}

 function calNames(calendarName, monthNo)
 {
  switch (calendarName)
  {
   case "hf": return Array("فروردین", "اردیبهشت", "خرداد", "تیر", "مرداد", "شهریور", "مهر", "آبان", "آذر", "دی", "بهمن", "اسفند")[monthNo];
   case "ge": return Array(" January ", " February ", " March ", " April ", " May ", " June ", " July ", " August ", " September ", " October ", " November ", " December ")[monthNo];
   case "gf": return Array("ژانویه", "فوریه", "مارس", "آوریل", "مه", "ژوثن", "ژوییه", "اوت", "سپتامبر", "اكتبر", "نوامبر", "دسامبر")[monthNo];
   case "df": return Array("شنبه", "یک‌شنبه", "دوشنبه", "سه‌شنبه", "چهارشنبه", "پنج‌شنبه", "جمعه")[monthNo];
   case "de": return Array("Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday")[monthNo];
   }
  }

var oPopup = window.createPopup();
var lef ;
var lef2 ;
var top ;
var top2 ;
function showMenu(menu, width, height,moveright,movedown)
{
  if (event!=null)
  {
	
	lef = event.clientX;
	lef2 = event.offsetX
	top = event.clientY;
	top2 = event.offsetY;
}

	lefter = lef;
	leftoff = lef2;
	topper = top;
	topoff = top2;
	

var oPopBody = oPopup.document.body;
moveMe = 0;
lefter= lefter + moveright;
topper= topper + movedown;


var HTMLContent = menu.innerHTML;


oPopBody.innerHTML = HTMLContent;
oPopup.show(lefter -width- leftoff - 2 - moveMe, topper - topoff + 22, width, height, document.body);
return false;
}




	function contextHilite(menu){
	    menu.runtimeStyle.backgroundColor = "Highlight";
	    if (menu.state){
	        menu.runtimeStyle.color = "GrayText";
	    } else {
	        menu.runtimeStyle.color = "HighlightText";
	    }
	}

	function contextDelite(menu){
	    menu.runtimeStyle.backgroundColor = "";
	    menu.runtimeStyle.color = "";
	}