/**
 * @author Mehdi Norouzi
 */
function MyDate() 
{
var  day,date,month,year;
var a = new  Date(); 
setDate(a);
/**********************/ 
function setday(d)
{ day=d;}
/**********************/
function setdate(d)
{ date=d;}
/**********************/
function setmonth(d)
{month=d;}
/**********************/
function setyear(d)
{year=d;}
/**********************/
function getday()
{return day;} 
/**********************/
function getdate()
{return date;}
/**********************/
function getmonth()
{return month} 
/**********************/
function getyear()
{return year}
/**********************/
function set(b)
{ 
setday(b.getDay());
setdate(b.getDate());
   		setmonth(b.getMonth());
   		setyear(b.getYear()); 
  	}
/**********************/
function setDate(a) 
{ 
var week= new Array("&#1740;&#1705;&#1588;&#1606;&#1576;&#1607;","&#1583;&#1608;&#1588;&#1606;&#1576;&#1607;","&#1587;&#1607; &#1588;&#1606;&#1576;&#1607;","&#1670;&#1607;&#1575;&#1585; &#1588;&#1606;&#1576;&#1607;","&#1662;&#1606;&#1580; &#1588;&#1606;&#1576;&#1607;","&#1580;&#1605;&#1593;&#1607;","&#1588;&#1606;&#1576;&#1607;") 
var months = new Array("&#1601;&#1585;&#1608;&#1585;&#1583;&#1610;&#1606;","&#1575;&#1585;&#1583;&#1610;&#1576;&#1607;&#1588;&#1578;","&#1582;&#1585;&#1583;&#1575;&#1583;","&#1578;&#1610;&#1585;","&#1605;&#1585;&#1583;&#1575;&#1583;","&#1588;&#1607;&#1585;&#1610;&#1608;&#1585;","&#1605;&#1607;&#1585;","&#1570;&#1576;&#1575;&#1606;","&#1570;&#1584;&#1585;","&#1583;&#1610;","&#1576;&#1607;&#1605;&#1606;","&#1575;&#1587;&#1601;&#1606;&#1583;"); 
var d= a.getDay(); 
var day= a.getDate();
var month = a.getMonth()+1; 
var year= a.getFullYear(); 
year = (year== 0)?2000:year; 
(year<1000)? (year += 2000):true; 
if ( year==2000||year==2004||year==2008||year==2012||year==2016||year==2020||year==2024||year==2028||year==2032||year==2036||year==2040 ){
        	if (month > 2) {
            	d += 1;
            	if(day<31){
            		day+=1;
            	}else{
            		day=1;
            		month+=1;
            	}
            	a=new Date(year,month,day);
year= a.getFullYear();
month = a.getMonth();            		
        	}
        }
year -= ( (month < 3) || ((month == 3) && (day < 21)) )? 622:621; 
switch (month) 
{ 
case 1: (day<21)? (month=10, day+=10,setdate(day)):(month=11, day-=20,setdate(day)); break; 
case 2: (day<20)? (month=11, day+=11,setdate(day)):(month=12, day-=19,setdate(day)); break; 
case 3: (day<21)? (month=12, day+=9,setdate(day)):(month=1, day-=20,setdate(day)); break; 
case 4: (day<21)? (month=1, day+=11,setdate(day)):(month=2, day-=20,setdate(day)); break; 
case 5: 
case 6: (day<22)? (month-=3, day+=10,setdate(day)):(month-=2, day-=21,setdate(day)); break; 
case 7: 
case 8: 
case 9: (day<23)? (month-=3, day+=9,setdate(day)):(month-=2, day-=22,setdate(day)); break; 
case 10:(day<23)? (month=7, day+=8,setdate(day)):(month=8, day-=22,setdate(day)); break; 
case 11: 
case 12:(day<22)? (month-=3, day+=9,setdate(day)):(month-=2, day-=21,setdate(day)); break; 
default: break; 
} 
month--;
setday(d);
setmonth(month);
setyear(year);
}
/**********************/
function jTog(a)	{  
 		day=a.getdate();
 		month=a.getmonth();
 		year=a.getyear();
month++;
 		switch(month)
 		{
 			case 1:(day>11)?(month=4,day-=11):(month=3,day+=20);break;
  			case 2:(day>11)?(month=5,day-=10):(month=4,day+=20);break;
  			case 3:(day>10)?(month=6,day-=10):(month=5,day+=21);break;
  			case 4:(day>9)?(month=7,day-=9):(month=6,day+=21);break;
  			case 5:
  			case 6:(day>9)?(month+=3,day-=9):(month+=2,day+=22);break;
  			case 7:(day>8)?(month=10,day-=8):(month=9,day+=22);break;
  			case 8:(day>9)?(month=11,day-=9):(month=10,day+=22);break;
  			case 9:(day>9)?(month=12,day-=9):(month=11,day+=21);break;
  			case 10:(day>10)?(month=1,day-=10):(month=12,day+=21);break;
  			case 11:(day>11)?(month=2,day-=11):(month=1,day+=20);break;
  			case 12:(day>9)?(month=3,day-=9):(month=2,day+=19);break;
  			default: break
  		}
  		if (year==1387||year==1391||year==1395||year==1399||year==1403||year==1407||year==1411||year==1415||year==1419||year==1423||year==1427||year==1431) {
day-=1;
  		}
  		
  		year += ( (month < 3) || ((month == 3) && (day < 21)) )? 622:621;
  		var  date=new Date(year,month-1,day);
  		setday(date.getDay());
  		setdate(date.getDate());
  		setyear(date.getYear())
  		setmonth(date.getMonth());
  	}
this.jTog=jTog;   
 	this.setday=setday;
 	this.getday=getday;
 	this.setdate=setdate;
 	this.getdate=getdate;
 	this.setmonth=setmonth;
 	this.getmonth=getmonth;
 	this.setyear=setyear;
 	this.getyear=getyear;
 	this.set=set;
 	this.setDate=setDate;
}
var	fixedX = -1			// x position (-1 if to appear below control)
var	fixedY = -1			// y position (-1 if to appear below control)
var startAt = 1			// 0 - sunday ; 1 - monday
var showWeekNumber = 1	// 0 - don't show; 1 - show
var showToday = 1		// 0 - don't show; 1 - show
var imgDir = "calanderimg/"			// directory for images ... e.g. var imgDir="/img/"
var gotoString = "Go To Current Month"	
var todayString = "&#1575;&#1605;&#1585;&#1608;&#1586;   "
var weekString = "&#1588;&#1605;&#1575;&#1585;&#1607;  &#1607;&#1601;&#1578;&#1607;  "
var scrollLeftMessage = "Click to scroll to previous month. Hold mouse button to scroll automatically."
var scrollRightMessage = "Click to scroll to next month. Hold mouse button to scroll automatically."
var selectMonthMessage = "Click to select a Year."
var selectYearMessage = "Click to select a Month."
var selectDateMessage = "Select [date] as date." // do not replace [date], it will be replaced by date.
var	 frameobj, crossobj, crossMonthObj, crossYearObj, monthSelected, yearSelected, dateSelected, omonthSelected, oyearSelected, odateSelected, monthConstructed, yearConstructed, intervalID1, intervalID2, timeoutID1, timeoutID2, ctlToPlaceValue, ctlNow, dateFormat, nStartingYear;
var ctlyearSelectId	, ctlmonthSelectId ,ctldaySelectId;
var yearRangeStart = 1350;
var yearRangeEnd = 1410;
var	bPageLoaded=false;
var	ie=document.all;
var	dom=document.getElementById;
var	ns4=document.layers;
var	today =	new	Date();
var today1= new MyDate();
var	dateNow	 = today1.getdate();
var	monthNow = today1.getmonth();
var	yearNow	 = today1.getyear();
                
var	imgsrc = new Array("drop1.gif","drop2.gif","left1.gif","left2.gif","right1.gif","right2.gif")
var	img	= new Array()
var week = new Array("&#1583;&#1608;&#1588;&#1606;&#1576;&#1607;","&#1587;&#1607; &#1588;&#1606;&#1576;&#1607;","&#1670;&#1607;&#1575;&#1585; &#1588;&#1606;&#1576;&#1607;","&#1662;&#1606;&#1580; &#1588;&#1606;&#1576;&#1607;","&#1580;&#1605;&#1593;&#1607;","&#1588;&#1606;&#1576;&#1607;","&#1740;&#1705;&#1588;&#1606;&#1576;&#1607;") 
var mytoday=new Date();
    
if(mytoday.getDay()==0)
   	{var dif=6}
else
   	{var dif=mytoday.getDay()-1}
    
var today_day=week[dif];
//////////////////////////////////////////////////////////////////////////////////
function HolidayRec (d, m, y, desc)
{
this.d = d
this.m = m
this.y = y
this.desc = desc
}
//////////////////////////////////////////////////////////////////////////////////
var HolidaysCounter = 0
var Holidays = new Array()
//////////////////////////////////////////////////////////////////////////////////
function addHoliday (d, m, y, desc)
{		
Holidays[HolidaysCounter++] = new HolidayRec ( d, m, y, desc )
}
//////////////////////////////////////////////////////////////////////////////////
if (dom)
{
for	(i=0;i<imgsrc.length;i++)
{
img[i] = new Image
img[i].src = imgDir + imgsrc[i]
}
//added by mahdy khayyamian
document.write ("<iframe id='myFrame' src='' scrolling='no' frameborder='0' style='position:absolute;width:50px;height:120px;top:0px;left:0px;border:none;display:block;z-index:0;visibility:hidden'></iframe><div id='calendar'	style='position:absolute;visibility:hidden;'><table width="+((showWeekNumber==1)?250:220)+" style='font-family:arial;font-size:11px;border-width:1;border-style:solid;border-color:#a0a0a0;font-family:arial; font-size:11px}' bgcolor='#ffffff'><tr bgcolor='#0000aa'><td><table width='"+((showWeekNumber==1)?248:218)+"'><tr><td style='padding:2px;font-family:arial; font-size:11px;'><font color='#ffffff'><B><span id='caption'></span></B></font></td><td align=right><a href='javascript:hideCalendar()'><IMG SRC='"+imgDir+"close.gif' WIDTH='15' HEIGHT='13' BORDER='0' ALT='Close the Calendar'></a></td></tr></table></td></tr><tr><td style='padding:5px' bgcolor=#ffffff><span id='content'></span></td></tr>")
if (showToday==1)
{
document.write ("<tr bgcolor=#f0f0f0><td style='padding:5px' align=center><span id='lblToday'></span></td></tr>")
}
document.write ("</table></div><div id='selectMonth' style='position:absolute;visibility:hidden;'><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td></div><div id='selectYear'	style='position:absolute;visibility:hidden;'></div>");
}
var yearMonth = new Array("&#1601;&#1585;&#1608;&#1585;&#1583;&#1610;&#1606;","&#1575;&#1585;&#1583;&#1610;&#1576;&#1607;&#1588;&#1578;","&#1582;&#1585;&#1583;&#1575;&#1583;","&#1578;&#1610;&#1585;","&#1605;&#1585;&#1583;&#1575;&#1583;","&#1588;&#1607;&#1585;&#1610;&#1608;&#1585;","&#1605;&#1607;&#1585;","&#1570;&#1576;&#1575;&#1606;","&#1570;&#1584;&#1585;","&#1583;&#1610;","&#1576;&#1607;&#1605;&#1606;","&#1575;&#1587;&#1601;&#1606;&#1583;"); 
var dayName = new Array("&#1588;&#1606;&#1576;&#1607;","&#1740;&#1705;&#1588;&#1606;&#1576;&#1607;","&#1583;&#1608;&#1588;&#1606;&#1576;&#1607;","&#1587;&#1607; &#1588;&#1606;&#1576;&#1607;","&#1670;&#1607;&#1575;&#1585; &#1588;&#1606;&#1576;&#1607;","&#1662;&#1606;&#1580; &#1588;&#1606;&#1576;&#1607;","&#1580;&#1605;&#1593;&#1607;" )
var	styleAnchor="text-decoration:none;color:black;"
var	styleLightBorder="border-style:solid;border-width:1px;border-color:#a0a0a0;"
//////////////////////////////////////////////////////////////////////////////////
function swapImage(srcImg, destImg)
{
if (ie)	{ document.getElementById(srcImg).setAttribute("src",imgDir + destImg) }
}
//////////////////////////////////////////////////////////////////////////////////
function init(id){
var dateTxt = document.getElementById(id);
generateCal(dateTxt);
}
function init_cal(cal, yearparam, monthparam, dayparam){
generateCal(cal, yearparam, monthparam, dayparam);
}
function generateCal(cal, yearparam, monthparam, dayparam){
if (!ns4)
{
//if (!ie) { yearNow += 1900	}
crossobj=(dom)?document.getElementById("calendar").style : ie? document.all.calendar : document.calendar
//added by mahdy khayyamian	
frameobj=(dom)?document.getElementById("myFrame").style : ie? document.all.myFrame : document.myFrame
frameobj.width = ((showWeekNumber==1)?250:220)
hideCalendar();
crossMonthObj=(dom)?document.getElementById("selectMonth").style : ie? document.all.selectMonth	: document.selectMonth
crossYearObj=(dom)?document.getElementById("selectYear").style : ie? document.all.selectYear : document.selectYear
monthConstructed=false;
yearConstructed=false;
//if (showToday==1)
//	{ 
  //      document.getElementById("lblToday").innerHTML =	todayString + " <a onmousemove='window.status=\""+gotoString+"\"' onmouseout='window.status=\"\"' title='"+gotoString+"' style='"+styleAnchor+"' href='javascript:monthSelected=monthNow;yearSelected=yearNow;constructCalendar();'>"+today_day +"   ,   " + dateNow + "    " +  yearMonth[monthNow]+ "	     " +yearNow	+ "</a>"
//	}
sHTML1="<span id='spanLeft'	style='border-style:solid;border-width:1;border-color:#3366FF;cursor:pointer' onmouseover='swapImage(\"changeLeft\",\"left2.gif\");this.style.borderColor=\"#88AAFF\";window.status=\""+scrollLeftMessage+"\"' onclick='javascript:decMonth()' onmouseout='clearInterval(intervalID1);swapImage(\"changeLeft\",\"left1.gif\");this.style.borderColor=\"#3366FF\";window.status=\"\"' onmousedown='clearTimeout(timeoutID1);timeoutID1=setTimeout(\"StartDecMonth()\",500)'	onmouseup='clearTimeout(timeoutID1);clearInterval(intervalID1)'>&nbsp<IMG id='changeLeft' SRC='"+imgDir+"left1.gif' width=10 height=11 BORDER=0>&nbsp</span>&nbsp;"
sHTML1+="<span id='spanRight' style='border-style:solid;border-width:1;border-color:#3366FF;cursor:pointer'	onmouseover='swapImage(\"changeRight\",\"right2.gif\");this.style.borderColor=\"#88AAFF\";window.status=\""+scrollRightMessage+"\"' onmouseout='clearInterval(intervalID1);swapImage(\"changeRight\",\"right1.gif\");this.style.borderColor=\"#3366FF\";window.status=\"\"' onclick='incMonth()' onmousedown='clearTimeout(timeoutID1);timeoutID1=setTimeout(\"StartIncMonth()\",500)'	onmouseup='clearTimeout(timeoutID1);clearInterval(intervalID1)'>&nbsp<IMG id='changeRight' SRC='"+imgDir+"right1.gif'	width=10 height=11 BORDER=0>&nbsp</span>&nbsp"
sHTML1+="<span id='spanMonth' style='border-style:solid;border-width:1;border-color:#3366FF;cursor:pointer'	onmouseover='swapImage(\"changeMonth\",\"drop2.gif\");this.style.borderColor=\"#88AAFF\";window.status=\""+selectMonthMessage+"\"' onmouseout='swapImage(\"changeMonth\",\"drop1.gif\");this.style.borderColor=\"#3366FF\";window.status=\"\"' onclick='popUpYear()'></span>&nbsp;"
sHTML1+="<span id='spanYear' style='border-style:solid;border-width:1;border-color:#3366FF;cursor:pointer' onmouseover='swapImage(\"changeYear\",\"drop2.gif\");this.style.borderColor=\"#88AAFF\";window.status=\""+selectYearMessage+"\"'	onmouseout='swapImage(\"changeYear\",\"drop1.gif\");this.style.borderColor=\"#3366FF\";window.status=\"\"'	onclick='popUpMonth()'></span>&nbsp;"
document.getElementById("caption").innerHTML  =	sHTML1
bPageLoaded=true
//Calling a calendar function and passing the name of the text 
//box which holds the date field. Added by D.T.
//var dateTxt = document.getElementById(id);
popUpCalendar(cal, yearparam, monthparam, dayparam);
}
}
//////////////////////////////////////////////////////////////////////////////////
function hideCalendar()	
{
crossobj.visibility="hidden"
//added by mahdy khayyamian
frameobj.visibility="hidden"
if (crossMonthObj != null){crossMonthObj.visibility="hidden"}
if (crossYearObj !=	null){crossYearObj.visibility="hidden"}
//Closing a popup window when user clicked 'x', close the calendar. Added by D.T.
//self.closed
}
//////////////////////////////////////////////////////////////////////////////////
function padZero(num) 
{
return (num	< 10)? '0' + num : num ;
}
//////////////////////////////////////////////////////////////////////////////////
function constructDate(d,m,y)
{
/*sTmp = dateFormat
sTmp = sTmp.replace	("dd","<e>")
sTmp = sTmp.replace	("d","<d>")
sTmp = sTmp.replace	("<e>",padZero(d))
sTmp = sTmp.replace	("<d>",d)
sTmp = sTmp.replace	("mmm","<o>")
sTmp = sTmp.replace	("mm","<n>")
sTmp = sTmp.replace	("m","<m>")
sTmp = sTmp.replace	("<m>",m+1)
sTmp = sTmp.replace	("<n>",padZero(m+1))
sTmp = sTmp.replace	("<o>",yearMonth[m])*/
return '';//sTmp.replace ("yyyy",y)
}
//////////////////////////////////////////////////////////////////////////////////
function closeCalendar() 
{
var	sTmp
hideCalendar();
alert(dateSelected);
alert(monthSelected);
constructjalaliDate();
//constructDate(dateSelected,monthSelected,yearSelected);
//ctlToPlaceValue.value =	
//ctlToPlaceValue.focus();
//Closing popup window when date is selected. Added By D.T.
//self.close()
}
function constructjalaliDate()
{
var range = yearRangeEnd - yearRangeStart;
var startyear = '';
var jalalihiden = document.getElementById( ctlyearSelectId + '-' + ctlmonthSelectId + '-' + ctldaySelectId );
var jalalihidenindex = document.getElementById( ctlyearSelectId + '-' + ctlmonthSelectId + '-' + ctldaySelectId + '-index' );
alert(jalalihidenindex.value)
var jalalihidenindices = jalalihidenindex.value.split('-');
var jalalihidenValue = jalalihiden.value;
var tempdays = jalalihidenValue.split("-");
//var index = parseInt(item);
var year = document.getElementById(ctlyearSelectId);
var month = document.getElementById(ctlmonthSelectId);
var day = document.getElementById(ctldaySelectId);
var y = jalalihidenindices[0]
var m =  jalalihidenindices[1]
var d = jalalihidenindices[2];
//alert('yyyyyyyyyyyyy' + y);
day.options[parseInt(d)].value= tempdays[2];
month.options[parseInt(m)].value = tempdays[1];
year.options[parseInt(y)].value = tempdays[0];
//alert('vdfsfsf' + d);
var yearindex = yearSelected - yearRangeStart;
var yearValue = year.options[yearindex].value;
var monthindex = monthSelected;
var monthValue = monthindex;//month.options[monthindex].value;
var dayindex = parseInt(dateSelected) - 1;
var dayValue = dayindex; //day.options[dayindex].value;
alert('dayindex ' + dayindex);
var date;
// if ( parseInt(yearValue) > 1900 )
// {
 
//  	 date = new Date(parseInt(yearValue), parseInt(monthValue), parseInt(dayValue)+1); 
//  }
// else 
  /*if ( index ==0 ){
   	  date = toGregorian(year.options[yearindex].value, parseInt(monthValue), parseInt(dayValue));
   	  jalalihiden.value = year.options[yearindex].value  +'-' + monthValue  +'-' + dayValue;
   	  }
   else if ( index ==1 ){
      date = toGregorian(parseInt(yearValue), monthindex, parseInt(dayValue));
      jalalihiden.value = yearValue + '-' + monthindex + '-' + dayValue;
      }
   else if ( index ==2 ){
   	  date = toGregorian(parseInt(yearValue), parseInt(monthValue), parseInt(dayindex)+1);
   	  jalalihiden.value = yearValue + '-' + monthValue + '-' + parseInt(dayindex)+1;
   	  }*/
   	  
alert(dayindex); 	  
date = toGregorian(parseInt(yearValue), parseInt(monthValue), parseInt(dayindex)+ 1);
jalalihiden.value = yearValue + '-' + monthValue + '-' + (parseInt(dayindex)+ 1);
jalalihidenindex.value = (yearValue - yearRangeStart) + '-' + monthValue + '-' + dayindex;
   	     	  
var tempday= date.getDate();
var tempmonth = date.getMonth(); 
var tempyear= date.getFullYear(); 
//alert(jalalihidenindex.value);
alert(date);
day.options[dayindex].value= tempday;
month.options[monthindex].value = tempmonth;
year.options[yearindex].value = tempyear;
day.selectedIndex = dayindex;
month.selectedIndex = monthindex;
year.selectedIndex = yearindex;
 
//var jalalihiden = document.getElementById( yearSelectId + '-' + monthSelectId + '-' + daySelectId );
//var jalalihidenValue = jalalihiden.value;
 //alert(jalalihiden.value);
alert('enddd');
}
//////////////////////////////////////////////////////////////////////////////////
/************************************* Month ************************************/
function StartDecMonth()
{
intervalID1=setInterval("decMonth()",80)
}
//////////////////////////////////////////////////////////////////////////////////
function StartIncMonth()
{
intervalID1=setInterval("incMonth()",80)
}
//////////////////////////////////////////////////////////////////////////////////
function incMonth () 
{
monthSelected++
if (monthSelected>11) 
{
monthSelected=0
yearSelected++
}
constructCalendar()
}
//////////////////////////////////////////////////////////////////////////////////
function decMonth () 
{
monthSelected--
if (monthSelected<0) 
{
monthSelected=11
yearSelected--
}
constructCalendar()
}
//////////////////////////////////////////////////////////////////////////////////
function constructMonth() 
{
popDownYear()
if (!monthConstructed) 
{
sHTML =	""
for	(i=0; i<12;	i++) 
{
sName =	yearMonth[i];
if (i==monthSelected)
{
sName =	"<B>" +	sName +	"</B>"
}
sHTML += "<tr><td id='m" + i + "' onmouseover='this.style.backgroundColor=\"#CDEAFE\"' onmouseout='this.style.backgroundColor=\"\"' style='cursor:pointer' onclick='monthConstructed=false;monthSelected=" + i + ";constructCalendar();popDownMonth();event.cancelBubble=true'>&nbsp;" + sName + "&nbsp;</td></tr>"
}
document.getElementById("selectMonth").innerHTML = "<table  width=60 dir='rtl'	style='font-family:arial; font-size:11px; border-width:1; border-style:solid; border-color:#a0a0a0;' bgcolor='#F7FCFE' cellspacing=0 onmouseover='clearTimeout(timeoutID1)'	onmouseout='clearTimeout(timeoutID1);timeoutID1=setTimeout(\"popDownMonth()\",100);event.cancelBubble=true'>" +sHTML +	"</table>"
monthConstructed=true
}
}
//////////////////////////////////////////////////////////////////////////////////
function popUpMonth() 
{
constructMonth()
crossMonthObj.visibility = (dom||ie)? "visible"	: "show"
    crossMonthObj.left = parseInt(crossobj.left) + 90
    crossMonthObj.top = parseInt(crossobj.top) + 10
}
//////////////////////////////////////////////////////////////////////////////////
function popDownMonth()	
{
crossMonthObj.visibility= "hidden"
}
//////////////////////////////////////////////////////////////////////////////////
/************************************** year ************************************/
function incYear() 
{
for	(i=0; i<7; i++)
{
newYear	= (i+parseInt(nStartingYear))+1;
if ( newYear > yearRangeEnd)
break;
if (newYear==yearSelected)
{ txtYear =	"&nbsp;<B>"	+ newYear +	"</B>&nbsp;" }
else
{ txtYear =	"&nbsp;" + newYear + "&nbsp;" }
document.getElementById("y"+i).innerHTML = txtYear;
}
if((7+parseInt(nStartingYear)) < yearRangeEnd)
nStartingYear++;
}
//////////////////////////////////////////////////////////////////////////////////
function decYear() 
{
for	(i=0; i<7; i++)
{
newYear	= (i+parseInt(nStartingYear))-1;
if ( newYear < yearRangeStart)
break;
if (newYear == yearSelected)
{ txtYear =	"&nbsp;<B>"	+ newYear +	"</B>&nbsp;" }
else
{ txtYear =	"&nbsp;" + newYear + "&nbsp;" }
document.getElementById("y"+i).innerHTML = txtYear;
}
if((parseInt(nStartingYear) - 1) > yearRangeStart)
nStartingYear --;
}
//////////////////////////////////////////////////////////////////////////////////
function selectYear(nYear) 
{
yearSelected=parseInt(nYear+nStartingYear);
yearConstructed=false;
constructCalendar();
popDownYear();
}
//////////////////////////////////////////////////////////////////////////////////
function constructYear() 
{
popDownMonth()
var sHTML =	""
if (!yearConstructed) 
{
sHTML =	"<tr><td align='center'	onmouseover='this.style.backgroundColor=\"#CDEAFE\"' onmouseout='clearInterval(intervalID1);this.style.backgroundColor=\"\"' style='cursor:pointer'	onmousedown='clearInterval(intervalID1);intervalID1=setInterval(\"decYear()\",30)' onmouseup='clearInterval(intervalID1)'>-</td></tr>"
var j =	0;
nStartingYear =	yearSelected-3;
for	(i=(yearSelected-3); i<=(parseInt(yearSelected)+3); i++) 
{
sName =	i;
if (i==yearSelected)
{
sName =	"<B>" +	sName +	"</B>"
}
sHTML += "<tr><td id='y" + j + "' onmouseover='this.style.backgroundColor=\"#CDEAFE\"' onmouseout='this.style.backgroundColor=\"\"' style='cursor:pointer' onclick='selectYear("+j+");event.cancelBubble=true'>&nbsp;" + sName + "&nbsp;</td></tr>"
j++;
}
sHTML += "<tr><td align='center' onmouseover='this.style.backgroundColor=\"#CDEAFE\"' onmouseout='clearInterval(intervalID2);this.style.backgroundColor=\"\"' style='cursor:pointer' onmousedown='clearInterval(intervalID2);intervalID2=setInterval(\"incYear()\",30)'	onmouseup='clearInterval(intervalID2)'>+</td></tr>"
document.getElementById("selectYear").innerHTML	= "<table width=44 style='font-family:arial; font-size:11px; border-width:1; border-style:solid; border-color:#a0a0a0;'	bgcolor='#F7FCFE' onmouseover='clearTimeout(timeoutID2)' onmouseout='clearTimeout(timeoutID2);timeoutID2=setTimeout(\"popDownYear()\",100)' cellspacing=0>"	+ sHTML	+ "</table>"
yearConstructed	= true
}
}
//////////////////////////////////////////////////////////////////////////////////
function popDownYear() 
{
clearInterval(intervalID1)
clearTimeout(timeoutID1)
clearInterval(intervalID2)
clearTimeout(timeoutID2)
crossYearObj.visibility= "hidden"
}
//////////////////////////////////////////////////////////////////////////////////
function popUpYear() 
{
var	leftOffset;
constructYear();
crossYearObj.visibility	= (dom||ie)? "visible" : "show";
leftOffset = parseInt(crossobj.left) + document.getElementById("spanYear").offsetLeft;
if (ie)
{
leftOffset += 6;
}
crossYearObj.top = parseInt(crossobj.top) + 10;
    crossYearObj.left =parseInt(crossobj.left) + 50;
}
//////////////////////////////////////////////////////////////////////////////////
/********************************** calendar ************************************/
function WeekNbr(today1)
{
var Year =today1.getyear();
var Month = today1.getmonth();
var Day = today1.getdate();
    var s=new MyDate();
    s.jTog(today1);
var now = Date.UTC(s.getyear(),s.getmonth(),s.getdate()+1 ,0 ,0 ,0);
var Firstday = new MyDate();
    Firstday.setyear(Year);
Firstday.setmonth(1);
Firstday.setdate(1);
    var Firstday1 = new MyDate();
    Firstday1.jTog(Firstday);
    Firstday.setday(Firstday1.getday());
    var s1= new MyDate();
    s1.setyear(Year);
    s1.setmonth(0);
    s1.setdate(1);
    var s2= new MyDate();
    s2.jTog(s1);
var then = Date.UTC(s2.getyear(),s2.getmonth(),s2.getdate(),0,0,0);
var Compensation = Firstday.getday();
if (Compensation > 3) Compensation -= 4;
else Compensation += 3;
NumberOfWeek =  Math.round((((now-then)/86400000)+Compensation)/7);
if(((((now-then)/86400000)+Compensation)/7)  - Math.round((((now-then)/86400000)+Compensation)/7)>0)
        {NumberOfWeek++;}
    return NumberOfWeek;
}
//////////////////////////////////////////////////////////////////////////////////
function takeYear(theDate)
{
x = theDate.getYear();
var y = x % 100;
y += (y < 38) ? 2000 : 1900;
return y;
}
//////////////////////////////////////////////////////////////////////////////////
function constructCalendar () 
{
var aNumDays = Array (31,31,31,31,31,31,30,30,30,30,30,29)
var dateMessage
var	startDate =	new	Date (yearSelected,monthSelected,1);
var test = startDate.getDay();
    var startDate = new MyDate();
    var startDate1 = new MyDate();
    startDate.setdate(1);
    startDate.setmonth(monthSelected);
    startDate.setyear(yearSelected);
    startDate1.jTog(startDate);
    startDate.setday(startDate1.getday());
var endDate
if (monthSelected==11)
{
d=yearSelected%33;
        if( (d==26)||(d==22)||(d==17)||(d==13)||(d==9)||(d==5)||(d==1)||(d==30) ) 
    		{numDaysInMonth = 30}
        else
        	{numDaysInMonth = aNumDays[monthSelected];}
    }
else
{
numDaysInMonth = aNumDays[monthSelected];
}
    var dayPointer = test + 1 ;
    if (dayPointer>6)
    {
    dayPointer=0 
}
    sHTML =	"<table	  border=0 dir='rtl'  style='font-family:verdana;font-size:10px;'><tr>"
    if (showWeekNumber==1)
{
sHTML += "<td width=27><b>" + weekString + "</b></td><td width=1 rowspan=7 bgcolor='#d0d0d0' style='padding:0px'><img src='"+imgDir+"divider.gif' width=1></td>"
}
for	(i=0; i<7; i++)	
{
sHTML += "<td width='27' align='right'><B>"+ dayName[i]+"</B></td>"
}
sHTML +="</tr><tr>"
if (showWeekNumber==1)
{
sHTML += "<td align=right>" + WeekNbr(startDate) + "&nbsp;</td>"
}
    for	( var i=1; i<=dayPointer;i++ )
{
sHTML += "<td>&nbsp;</td>"
}
for	( datePointer=1; datePointer<=numDaysInMonth; datePointer++ )
{
dayPointer++;
sHTML += "<td align=right lang='fa'>"
sStyle=styleAnchor
if ((datePointer==odateSelected) &&	(monthSelected==omonthSelected)	&& (yearSelected==oyearSelected))
{ sStyle+=styleLightBorder }
sHint = ""
for (k=0;k<HolidaysCounter;k++)
{
if ((parseInt(Holidays[k].d)==datePointer)&&(parseInt(Holidays[k].m)==(monthSelected+1)))
{
if ((parseInt(Holidays[k].y)==0)||((parseInt(Holidays[k].y)==yearSelected)&&(parseInt(Holidays[k].y)!=0)))
{
sStyle+="background-color:#FFDDDD;"
sHint+=sHint==""?Holidays[k].desc:"\n"+Holidays[k].desc
}
}
}
//var regexp= /\"/g
//sHint=sHint.replace(regexp,"&quot;")
dateMessage = "onmousemove='window.status=\""+selectDateMessage.replace("[date]",constructDate(datePointer,monthSelected,yearSelected))+"\"' onmouseout='window.status=\"\"' "
if ((datePointer==dateNow)&&(monthSelected==monthNow)&&(yearSelected==yearNow))
{ sHTML += "<b><a "+dateMessage+" title=\"" + sHint + "\" style='"+sStyle+"' href='javascript:dateSelected="+datePointer+";closeCalendar();'><font color=#ff0000>&nbsp;" + datePointer + "</font>&nbsp;</a></b>"}
else if	(dayPointer % 7 == ((startAt * -1) + 1) )
{ sHTML += "<a "+dateMessage+" title=\"" + sHint + "\" style='"+sStyle+"' href='javascript:dateSelected="+datePointer + ";closeCalendar();'>&nbsp;<font color=#909090>" + datePointer + "</font>&nbsp;</a>" }
else
{ sHTML += "<a "+dateMessage+" title=\"" + sHint + "\" style='"+sStyle+"' href='javascript:dateSelected="+datePointer + ";closeCalendar();'>&nbsp;" + datePointer + "&nbsp;</a>" }
sHTML += "";
if ((dayPointer+startAt) % 7 == startAt) 
{ 
sHTML += "</tr><tr>" 
if ((showWeekNumber==1)&&(datePointer<numDaysInMonth))
{
   	dd=new Date(yearSelected,monthSelected,datePointer+1)	
               	d1=dd.getDay();
              	d2=dd.getDate();
               	d3=dd.getMonth();
                d4=dd.getYear();
                dd=new MyDate();
                dd.setday(d1);
                dd.setdate(d2);
                dd.setmonth(d3);
                dd.setyear(d4);
                sHTML += "<td align=right>" + (WeekNbr(dd)) + "&nbsp;</td>"
}
}
}
   	document.getElementById("content").innerHTML   = sHTML
    document.getElementById("spanYear").innerHTML = "&nbsp;" +	yearMonth[monthSelected] + "&nbsp;<IMG id='changeYear' SRC='"+imgDir+"drop1.gif' WIDTH='12' HEIGHT='10' BORDER=0>"
document.getElementById("spanMonth").innerHTML =	"&nbsp;" + yearSelected	+ "&nbsp;<IMG id='changeMonth' SRC='"+imgDir+"drop1.gif' WIDTH='12' HEIGHT='10' BORDER=0>"
}
//////////////////////////////////////////////////////////////////////////////////
function popUpCalendar(ctl, yearparam, monthparam, dayparam) 
{
var	leftpos=0
var	toppos=0
if (bPageLoaded)
{
if ( crossobj.visibility ==	"hidden" ) 
{
ctlyearSelectId	= yearparam;
ctlmonthSelectId = monthparam;
ctldaySelectId = dayparam;
/*dateFormat=format;
formatChar = " "
aFormat	= dateFormat.split(formatChar)
if (aFormat.length<3)
{
formatChar = "/"
aFormat	= dateFormat.split(formatChar)
if (aFormat.length<3)
{
formatChar = "."
aFormat	= dateFormat.split(formatChar)
if (aFormat.length<3)
{
formatChar = "-"
aFormat	= dateFormat.split(formatChar)
if (aFormat.length<3)
{
// invalid date	format
formatChar=""
}
}
}
}
tokensChanged =	0
if ( formatChar	!= "" )
{
// use user's date
aData =	ctl.value.split(formatChar)
for	(i=0;i<3;i++)
{
if ((aFormat[i]=="d") || (aFormat[i]=="dd"))
{
dateSelected = parseInt(aData[i], 10)
tokensChanged ++
}
else if	((aFormat[i]=="m") || (aFormat[i]=="mm"))
{
monthSelected =	parseInt(aData[i], 10) - 1
tokensChanged ++
}
else if	(aFormat[i]=="yyyy")
{
yearSelected = parseInt(aData[i], 10)
tokensChanged ++
}
else if	(aFormat[i]=="mmm")
{
for	(j=0; j<12;	j++)
{
if (aData[i]==yearMonth[j])
{
monthSelected=j
tokensChanged ++
}
}
}
}
}
if ((tokensChanged!=3)||isNaN(dateSelected)||isNaN(monthSelected)||isNaN(yearSelected))
{
dateSelected = dateNow
monthSelected =	monthNow
yearSelected = yearNow
}*/
setcurrentDate(yearparam, monthparam, dayparam);
odateSelected=dateSelected;
omonthSelected=monthSelected;
oyearSelected=yearSelected;
//ctl = document.getElementById( yearparam);
var positions = findPositionWithScrolling(ctl);
alert('position ' + positions.x);
/*aTag = ctl;
do 
{
aTag = aTag.offsetParent;
leftpos	+= aTag.offsetLeft;
toppos += aTag.offsetTop;
} while(aTag.tagName!="BODY");
alert(leftpos);*/
leftpos = positions.x;
toppos =  positions.y;
//aded by mahdy khayyamian
frameobj.left =	fixedX==-1 ? ctl.offsetLeft	+ leftpos :	fixedX
frameobj.top = fixedY==-1 ?	ctl.offsetTop +	toppos + ctl.offsetHeight +	2 :	fixedY
crossobj.left =	fixedX==-1 ? ctl.offsetLeft	+ leftpos :	fixedX
crossobj.top = fixedY==-1 ?	ctl.offsetTop +	toppos + ctl.offsetHeight +	2 :	fixedY
            //Setting left and top of the calendar in a popUp window. Added by D.T.
constructCalendar (1, monthSelected, yearSelected);
//aded by mahdy khayyamian
frameobj.height=183;
frameobj.wifth=255;					
frameobj.visibility=(dom||ie)? "visible" : "show";
crossobj.visibility=(dom||ie)? "visible" : "show";
}
else
{
hideCalendar();
if (ctlNow!=ctl) 
{popUpCalendar(ctl, format)}
}
ctlNow = ctl;
}
}
function test()
{
var	today =	new	Date();
var today1= new MyDate();
var	dateNow	 = today1.getdate();
var	monthNow = today1.getmonth();
var	yearNow	 = today1.getyear();
//alert(yearNow + "/" + monthNow + "/" + dateNow);
today1.gregorian();
var testDate = toGregorian(yearNow, monthNow+1, dateNow);
alert(testDate);
//alert(today.getFullYear());
}
function toGregorian(jalyear, jalmonth, jalday)
{
alert('toGregorian' + jalyear + '/' + jalmonth + '/' + jalday);
switch(jalmonth)
{
case 1:(jalday>11)?(jalmonth=4,jalday-=11):(jalmonth=3,jalday+=20);break;
case 2:(jalday>11)?(jalmonth=5,jalday-=10):(jalmonth=4,jalday+=20);break;
case 3:(jalday>10)?(jalmonth=6,jalday-=10):(jalmonth=5,jalday+=21);break;
case 4:(jalday>9)?(jalmonth=7,jalday-=9):(jalmonth=6,jalday+=21);break;
case 5:
case 6:(jalday>9)?(jalmonth+=3,jalday-=9):(jalmonth+=2,jalday+=22);break;
case 7:(jalday>8)?(jalmonth=10,jalday-=8):(jalmonth=9,jalday+=22);break;
case 8:(jalday>9)?(jalmonth=11,jalday-=9):(jalmonth=10,jalday+=22);break;
case 9:(jalday>9)?(jalmonth=12,jalday-=9):(jalmonth=11,jalday+=21);break;
case 10:(jalday>10)?(jalmonth=1,jalday-=10):(jalmonth=12,jalday+=21);break;
case 11:(jalday>11)?(jalmonth=2,jalday-=11):(jalmonth=1,jalday+=20);break;
case 12:(jalday>9)?(jalmonth=3,jalday-=9):(jalmonth=2,jalday+=19);break;
default: break
}
if (jalyear==1387||jalyear==1391||jalyear==1395||jalyear==1399||jalyear==1403||jalyear==1407||jalyear==1411||jalyear==1415||jalyear==1419||jalyear==1423||jalyear==1427||jalyear==1431) {
jalday-=1;
}
  		
jalyear += ( (jalmonth < 3) || ((jalmonth == 3) && (jalday < 21)) )? 622:621;
var  date = new Date(jalyear,jalmonth,jalday);
return date;
}
function jalali(item, yearSelectId, monthSelectId, daySelectId)
{
alert('start');
var range = yearRangeEnd - yearRangeStart;
var startyear = '';
var jalalihiden = document.getElementById( yearSelectId + '-' + monthSelectId + '-' + daySelectId );
var jalalihidenindex = document.getElementById( yearSelectId + '-' + monthSelectId + '-' + daySelectId + '-index' );
alert(jalalihidenindex.value)
var jalalihidenindices = jalalihidenindex.value.split('-');
var jalalihidenValue = jalalihiden.value;
var tempdays = jalalihidenValue.split("-");
var index = parseInt(item);
var year = document.getElementById(yearSelectId);
var month = document.getElementById(monthSelectId);
var day = document.getElementById(daySelectId);
var y = jalalihidenindices[0]
var m =  jalalihidenindices[1]
var d = jalalihidenindices[2];
//alert('yyyyyyyyyyyyy' + y);
day.options[parseInt(d)].value= tempdays[2];
month.options[parseInt(m)].value = tempdays[1];
year.options[parseInt(y)].value = tempdays[0];
//alert('vdfsfsf' + d);
var yearindex = year.selectedIndex;
var yearValue = year.options[yearindex].value;
var monthindex = month.selectedIndex;
var monthValue = monthindex;//month.options[monthindex].value;
var dayindex = day.selectedIndex;
var dayValue = dayindex; //day.options[dayindex].value;
var date;
// if ( parseInt(yearValue) > 1900 )
// {
 
//  	 date = new Date(parseInt(yearValue), parseInt(monthValue), parseInt(dayValue)+1); 
//  }
// else 
  /*if ( index ==0 ){
   	  date = toGregorian(year.options[yearindex].value, parseInt(monthValue), parseInt(dayValue));
   	  jalalihiden.value = year.options[yearindex].value  +'-' + monthValue  +'-' + dayValue;
   	  }
   else if ( index ==1 ){
      date = toGregorian(parseInt(yearValue), monthindex, parseInt(dayValue));
      jalalihiden.value = yearValue + '-' + monthindex + '-' + dayValue;
      }
   else if ( index ==2 ){
   	  date = toGregorian(parseInt(yearValue), parseInt(monthValue), parseInt(dayindex)+1);
   	  jalalihiden.value = yearValue + '-' + monthValue + '-' + parseInt(dayindex)+1;
   	  }*/
   	  
   	  
date = toGregorian(parseInt(yearValue), parseInt(monthValue), parseInt(dayindex)+1);
jalalihiden.value = yearValue + '-' + monthValue + '-' + (parseInt(dayindex)+1);
jalalihidenindex.value = (yearValue - yearRangeStart) + '-' + monthValue + '-' + dayindex;
   	     	  
var tempday= date.getDate();
var tempmonth = date.getMonth(); 
var tempyear= date.getFullYear(); 
//alert(jalalihidenindex.value);
alert(date);
day.options[dayindex].value= tempday;
month.options[monthindex].value = tempmonth;
year.options[yearindex].value = tempyear;
 
//var jalalihiden = document.getElementById( yearSelectId + '-' + monthSelectId + '-' + daySelectId );
//var jalalihidenValue = jalalihiden.value;
 //alert(jalalihiden.value);
alert('enddd');
}
function setcurrentDate( yearSelectId, monthSelectId, daySelectId)
{
var jalalihiden = document.getElementById( yearSelectId + '-' + monthSelectId + '-' + daySelectId );
var jalalihidenValue = jalalihiden.value;
var tempdays = jalalihidenValue.split("-");
dateSelected = tempdays[2];
monthSelected =	tempdays[1];
yearSelected = tempdays[0];
}
/*function findPositionWithScrolling( oElement ) {
  function getNextAncestor( oElement ) {
    var actualStyle;
    if( window.getComputedStyle ) {
      actualStyle = getComputedStyle(oElement,null).position;
    } else if( oElement.currentStyle ) {
      actualStyle = oElement.currentStyle.position;
    } else {
      //fallback for browsers with low support - only reliable for inline styles
      actualStyle = oElement.style.position;
    }
    if( actualStyle == 'absolute' || actualStyle == 'fixed' ) {
      //the offsetParent of a fixed position element is null so it will stop
      return oElement.offsetParent;
    }
    return oElement.parentNode;
  }
  if( typeof( oElement.offsetParent ) != 'undefined' ) {
    var originalElement = oElement;
    for( var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent ) {
      posX += oElement.offsetLeft;
      posY += oElement.offsetTop;
    }
    if( !originalElement.parentNode || !originalElement.style || typeof( originalElement.scrollTop ) == 'undefined' ) {
      //older browsers cannot check element scrolling
      return [ posX, posY ];
    }
    oElement = getNextAncestor(originalElement);
    while( oElement && oElement != document.body && oElement != document.documentElement ) {
      posX -= oElement.scrollLeft;
      posY -= oElement.scrollTop;
      oElement = getNextAncestor(oElement);
    }
alert('pos  ' + posX + '   '+ posY);
    return [ posX, posY ];
  } else {
  	alert(oElement.x + '   '+ oElement.y);
    return [ oElement.x, oElement.y ];
  }
}*/
function findPositionWithScrolling(el) {
var SL = 0, ST = 0;
var is_div = /^div$/i.test(el.tagName);
alert(el.tagName);
if (is_div && el.scrollLeft)
SL = el.scrollLeft;
if (is_div && el.scrollTop)
ST = el.scrollTop;
var r = { x: el.offsetLeft - SL, y: el.offsetTop - ST };
if (el.offsetParent) {
var tmp = findPositionWithScrolling(el.offsetParent);
r.x += tmp.x;
r.y += tmp.y;
}
alert(r.x);
return r;
}

