// ********************
// Begin Popup Calendar
// ********************
var popCalDstFld;
var temp;
var popCalWin;

// ******************************
// Expected params:
// [0] Window Name
// [1] Destination Field
// [2] Short Date Format
// [3] Month Names
// [4] Day Names
// [5] Localized Today string
// [6] Localized Close string
// [7] Window Title
// [8] First Day of Week
// ******************************
function popupCal()
{
	debugger;			//remove slashes to activate debugging in Visual Studio
	var tmpDate         = new Date();
	var tmpString       = "";
	var tmpNum          = 0;
	var popCalDateVal;
	var dstWindowName   = "";

	//Initialize the window to an empty object.
	popCalWin = new Object();
	
	//Check for the right number of arguments
	if (arguments.length < 2)
	{
		alert("popupCal(): Wrong number of arguments.");
		return void(0);
	}
	//Get the command line arguments -- Localization is optional
	dstWindowName = popupCal.arguments[0];
	popCalDstFld = popupCal.arguments[1];
	temp = popupCal.arguments[1];
	popCalDstFmt = popupCal.arguments[2]; //Localized Short Date Format String
	popCalMonths = popupCal.arguments[3]; //Localized Month Names String
	popCalDays = popupCal.arguments[4];   //Localized Day Names String
	popCalToday = popupCal.arguments[5];  //Localized Today string
	popCalClose = popupCal.arguments[6];  //Localized Close string
	popCalTitle = popupCal.arguments[7];  //Window Title
	popCalFirstDayWeek = popupCal.arguments[8];  //First Day of Week
	
	//check destination field name
	if (popCalDstFld != "")
	  popCalDstFld = document.getElementById(popCalDstFld);

	//default localized short date format if not provided
	if (popCalDstFmt == "")
	  popCalDstFmt = "m/d/yyyy";

	//default localized months string if not provided
	if (popCalMonths == "")
	  popCalMonths = "January,February,March,April,May,June,July,August,September,October,November,December";
 
	//default localized months string if not provided
	if (popCalDays == "")
	  popCalDays = "Sun,Mon,Tue,Wed,Thu,Fri,Sat";
	
	//default localized today string if not provided
	if (popCalToday == "" || typeof popCalToday == "undefined")
	  popCalToday = "Today";

	//default localized close string if not provided
	if (popCalClose == "" || typeof popCalClose == "undefined")
	  popCalClose = "Close";

	//default window title if not provided
	if (popCalTitle == "" || typeof popCalTitle == "undefined")
	  popCalTitle = "Calendar";
 
	tmpString = new String(popCalDstFld.value);  
	//If tmpString is empty (meaning that the field is empty) 
	//use todays date as the starting point
	if(tmpString == "")
		popCalDateVal = new Date()
	else
	{
		//Make sure the century is included, if it isn't, add this 
		//century to the value that was in the field
		tmpNum = tmpString.lastIndexOf( "/" );
		if ( (tmpString.length - tmpNum) == 3 )
		{
			tmpString = tmpString.substring(0,tmpNum + 1)+"20"+tmpString.substr(tmpNum+1);
			popCalDateVal = new Date(tmpString);
		}
		else
		{
			//localized date support:
			//If we got to this point, it means the field that was passed 
			//in has no slashes in it. Use an extra function to build the date 
			//according to supplied date formatstring.
			popCalDateVal = getDateFromFormat(tmpString,popCalDstFmt);
		}
	}
	
	//Make sure the date is a valid date.  Set it to today if it is invalid
	//"NaN" is the return value for an invalid date
	if( popCalDateVal.toString() == "NaN" || popCalDateVal.toString() == "0")
	{
		popCalDateVal = new Date();
		popCalDstFld.value = "";
	}
			
	//Set the base date to midnight of the first day of the specified month, 
	//this makes things easier?
 	var dateString = String(popCalDateVal.getMonth()+1) + "/" + String(popCalDateVal.getDate()) + "/" + String(popCalDateVal.getFullYear());

	//Call the routine to draw the initial calendar
	reloadCalPopup(dateString, dstWindowName);
	
	return void(0);
}
 
function closeCalPopup()
{
	//Can't tell the child window to close itself, the parent window has to 
	//tell it to close.
	popCalWin.close();
	return void(0);
}
 
function reloadCalPopup() //[0]dateString, [1]dstWindowName
{
	//Set the window's features here

	var windowFeatures = "toolbar=no, location=no, status=no, menubar=no, scrollbars=no, resizable=no, height=270, width=270, top=" + ((screen.height - 270)/2).toString()+",left="+((screen.width - 270)/2).toString();
	var tmpDate = new Date( reloadCalPopup.arguments[0] );
	
	if (tmpDate.toString() == "Invalid Date")
	    tmpDate = new Date();
	
	tmpDate.setDate(1);
	
	//Get the calendar data
	var popCalData = calPopupSetData(tmpDate,reloadCalPopup.arguments[1]);
 
	//Check to see if the window has been initialized, create it if it hasn't been
	if( popCalWin.toString() == "[object Object]" )
	{
		popCalWin = window.open("",reloadCalPopup.arguments[1],windowFeatures);
		popCalWin.opener = self;
		// Window im Vordergrund
		popCalWin.focus();
	}
	else 
	{
    popCalWin.document.close();
		popCalWin.document.clear();
  }
	
	//this is the line with the big problem
  popCalWin.document.write(popCalData);
	return void(1);
}
 
function calPopupSetData(firstDay,dstWindowName)
{
	var popCalData = "";
    var lastDate = 0;
	var fnt = new Array( "<FONT SIZE=\"1\">", "<B><FONT SIZE=\"2\">", "<FONT SIZE=\"2\" COLOR=\"#EF741D\"><B>");
	var dtToday = new Date();
	var thisMonth = firstDay.getMonth();
	var thisYear = firstDay.getFullYear();
	var nPrevMonth = (thisMonth == 0 ) ? 11 : (thisMonth - 1);
	var nNextMonth = (thisMonth == 11 ) ? 0 : (thisMonth + 1);
	var nPrevMonthYear = (nPrevMonth == 11) ? (thisYear - 1): thisYear;
	var nNextMonthYear = (nNextMonth == 0) ? (thisYear + 1): thisYear;
	var sToday = String((dtToday.getMonth()+1) + "/01/" + dtToday.getFullYear());
	var sPrevMonth = String((nPrevMonth+1) + "/01/" + nPrevMonthYear);
	var sNextMonth = String((nNextMonth+1) + "/01/" + nNextMonthYear);
	var sPrevYear1 = String((thisMonth+1) + "/01/" + (thisYear - 1));
	var sNextYear1 = String((thisMonth+1) + "/01/" + (thisYear + 1));
 	var tmpDate = new Date( sNextMonth );
	
	tmpDate = new Date( tmpDate.valueOf() - 1001 );
	lastDate = tmpDate.getDate();

	if (this.popCalMonths.split) // javascript 1.1 defensive code
	{
		var monthNames = this.popCalMonths.split(",");
		var dayNames = this.popCalDays.split(",");
	}
	else  // Need to build a js 1.0 split algorithm, default English for now
	{
		var monthNames = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
		var dayNames = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat")
	}

 	var styles = "<style><!-- body{font-family:Arial,Helvetica,sans-serif;font-size:9pt}; td {  font-family: Arial, Helvetica, sans-serif; font-size: 9pt; color: #666666}; A { text-decoration: none; };TD.day { border-bottom: solid black; border-width: 0px; }--></style>"
	var cellAttribs = "align=\"center\" class=\"day\" BGCOLOR=\"#F1F1F1\"onMouseOver=\"temp=this.style.backgroundColor;this.style.backgroundColor='#CCCCCC';\" onMouseOut=\"this.style.backgroundColor=temp;\""
	var cellAttribs2 = "align=\"center\" BGCOLOR=\"#F1F1F1\" onMouseOver=\"temp=this.style.backgroundColor;this.style.backgroundColor='#CCCCCC';\" onMouseOut=\"this.style.backgroundColor=temp;\""
	var htmlHead = "<HTML><HEAD><TITLE>"+popCalTitle+"</TITLE>" + styles + "</HEAD><BODY BGCOLOR=\"#F1F1F1\" TEXT=\"#000000\" LINK=\"#364180\" ALINK=\"#FF8100\" VLINK=\"#424282\">";
	var htmlTail = "</BODY></HTML>";
	var closeAnchor = "<CENTER><input type=button value=\""+popCalClose+"\" onClick=\"javascript:window.opener.closeCalPopup()\"></CENTER>";            
	var todayAnchor = "<A HREF=\"javascript:window.opener.reloadCalPopup('"+sToday+"','"+dstWindowName+"');\">"+popCalToday+"</A>";
	var prevMonthAnchor = "<A HREF=\"javascript:window.opener.reloadCalPopup('"+sPrevMonth+"','"+dstWindowName+"');\">" + monthNames[nPrevMonth] + "</A>";
	var nextMonthAnchor = "<A HREF=\"javascript:window.opener.reloadCalPopup('"+sNextMonth+"','"+dstWindowName+"');\">" + monthNames[nNextMonth] + "</A>";
	var prevYear1Anchor = "<A HREF=\"javascript:window.opener.reloadCalPopup('"+sPrevYear1+"','"+dstWindowName+"');\">"+(thisYear-1)+"</A>";
	var nextYear1Anchor = "<A HREF=\"javascript:window.opener.reloadCalPopup('"+sNextYear1+"','"+dstWindowName+"');\">"+(thisYear+1)+"</A>";
		
	popCalData += (htmlHead + fnt[1]);
	popCalData += ("<DIV align=\"center\">");
	popCalData += ("<TABLE BORDER=\"0\" cellspacing=\"0\" callpadding=\"0\" width=\"250\"><TR><TD width=\"45\">&nbsp</TD>");
	popCalData += ("<TD width=\"45\" align=\"center\" " + cellAttribs2);
	popCalData += (" >");
	popCalData += (fnt[0]+prevYear1Anchor+"</FONT></TD>");
	popCalData += ("<TD width=\"70\" align=\"center\" "+cellAttribs2);
	popCalData += (" >");
	popCalData += (fnt[0]+todayAnchor+"</FONT></TD>");
	popCalData += ("<TD width=\"45\" align=\"center\" "+cellAttribs2);
	popCalData += (" >");
	popCalData += (fnt[0]+nextYear1Anchor+"</FONT></TD><TD width=\"45\">&nbsp</TD>");
	popCalData += ("</TR></TABLE>");
	popCalData += ("<TABLE BORDER=\"0\" cellspacing=\"0\" callpadding=\"0\" width=\"250\">");          
	popCalData += ("<TR><TD width=\"55\" align=\"center\" "+cellAttribs2);
	popCalData += (" >");
	popCalData += (fnt[0] + prevMonthAnchor + "</FONT></TD>");
	popCalData += ("<TD width=\"140\" align=\"center\">");
	popCalData += ("&nbsp;&nbsp;"+fnt[1]+"<FONT COLOR=\"#000000\">" + monthNames[thisMonth] + ", " + thisYear + "&nbsp;&nbsp;</FONT></TD>");
	popCalData += ("<TD width=\"55\" align=\"center\" "+cellAttribs2);
	popCalData += (" >");
	popCalData += (fnt[0]+nextMonthAnchor+"</FONT></TD></TR></TABLE><BR>");       
	popCalData += ("<TABLE BORDER=\"0\" cellspacing=\"2\" cellpadding=\"1\"  width=\"245\">" );
	popCalData += ("");
	popCalData += ("<TR>");
	
	/*
	popCalData += ("<TD width=\"35\" align=\"center\">"+fnt[1]+"<FONT COLOR=\"#000000\">"+dayNames[0]+"</FONT></TD>");
	popCalData += ("<TD width=\"35\" align=\"center\">"+fnt[1]+"<FONT COLOR=\"#000000\">"+dayNames[1]+"</FONT></TD>");
	popCalData += ("<TD width=\"35\" align=\"center\">"+fnt[1]+"<FONT COLOR=\"#000000\">"+dayNames[2]+"</FONT></TD>");
	popCalData += ("<TD width=\"35\" align=\"center\">"+fnt[1]+"<FONT COLOR=\"#000000\">"+dayNames[3]+"</FONT></TD>");
	popCalData += ("<TD width=\"35\" align=\"center\">"+fnt[1]+"<FONT COLOR=\"#000000\">"+dayNames[4]+"</FONT></TD>");
	popCalData += ("<TD width=\"35\" align=\"center\">"+fnt[1]+"<FONT COLOR=\"#000000\">"+dayNames[5]+"</FONT></TD>");
	popCalData += ("<TD width=\"35\" align=\"center\">"+fnt[1]+"<FONT COLOR=\"#000000\">"+dayNames[6]+"</FONT></TD>");
	*/
	var xday = 0;
	for (xday = 0; xday < 7; xday++)
	{
		popCalData += ("<TD width=\"35\" align=\"center\">"+fnt[1]+"<FONT COLOR=\"#000000\">"+dayNames[(xday+popCalFirstDayWeek)%7]+"</FONT></TD>");
	};
	popCalData += ("</TR>");
	
	var calDay = 0;
	var monthDate = 1;
	var weekDay = firstDay.getDay();
	do
	{
		popCalData += ("<TR>");
		for (calDay = 0; calDay < 7; calDay++ )
		{
			if(((weekDay+7-popCalFirstDayWeek)%7 != calDay) || (monthDate > lastDate))
			{
				popCalData += ("<TD width=\"35\">"+fnt[1]+"&nbsp;</FONT></TD>");
				continue;
			}
			else
			{
				anchorVal = "<A HREF=\"javascript:window.opener.calPopupSetDate(window.opener.popCalDstFld,'" + constructDate(monthDate,thisMonth+1,thisYear) + "');window.opener.closeCalPopup()\">";
				jsVal = "javascript:window.opener.calPopupSetDate(window.opener.popCalDstFld,'" + constructDate(monthDate,thisMonth+1,thisYear) + "');window.opener.closeCalPopup()";

				popCalData += ("<TD width=\"35\" "+cellAttribs+" onClick=\""+jsVal+"\">");
				
				if ((firstDay.getMonth() == dtToday.getMonth()) && (monthDate == dtToday.getDate()) && (thisYear == dtToday.getFullYear()) )
					popCalData += (anchorVal+fnt[2]+monthDate+"</A></FONT></TD>");
				else
					popCalData += (anchorVal+fnt[1]+monthDate+"</A></FONT></TD>");
				
				weekDay++;
				monthDate++;
			}
		}
		weekDay = popCalFirstDayWeek;
		popCalData += ("</TR>");
	} while( monthDate <= lastDate );
	
	popCalData += ("</TABLE></DIV><BR>");
 
	popCalData += (closeAnchor+"</FONT>"+htmlTail);
	return( popCalData );
}
 
function calPopupSetDate()
{
	calPopupSetDate.arguments[0].value = calPopupSetDate.arguments[1];
}

// utility function
function padZero(num)
{
  return ((num <= 9) ? ("0" + num) : num);
}

// Format short date
function constructDate(d,m,y)
{
  var fmtDate = this.popCalDstFmt
  fmtDate = fmtDate.replace ('dd', padZero(d))
  fmtDate = fmtDate.replace ('d', d)
  fmtDate = fmtDate.replace ('MM', padZero(m))
  fmtDate = fmtDate.replace ('M', m)
  fmtDate = fmtDate.replace ('yyyy', y)
  fmtDate = fmtDate.replace ('yy', padZero(y%100))
  return fmtDate;
}

// ------------------------------------------------------------------
// Utility functions for parsing in getDateFromFormat()
// ------------------------------------------------------------------
function _isInteger(val) {
	var digits="1234567890";
	for (var i=0; i < val.length; i++) {
		if (digits.indexOf(val.charAt(i))==-1) { return false; }
		}
	return true;
	}
function _getInt(str,i,minlength,maxlength) {
	for (var x=maxlength; x>=minlength; x--) {
		var token=str.substring(i,i+x);
		if (token.length < minlength) { return null; }
		if (_isInteger(token)) { return token; }
		}
	return null;
	}
	
// ------------------------------------------------------------------
// getDateFromFormat( date_string , format_string )
//
// This function takes a date string and a format string. It matches
// If the date string matches the format string, it returns the 
// getTime() of the date. If it does not match, it returns 0.
// ------------------------------------------------------------------
function getDateFromFormat(val,format) {
	val=val+"";
	format=format+"";
	var i_val=0;
	var i_format=0;
	var c="";
	var token="";
	var x,y;
	var now=new Date();
	var year=now.getYear();
	var month=now.getMonth()+1;
	var date=1;
		
	while (i_format < format.length) {
		// Get next token from format string
		c=format.charAt(i_format);
		token="";
		while ((format.charAt(i_format)==c) && (i_format < format.length)) {
			token += format.charAt(i_format++);
			}
		// Extract contents of value based on format token
		if (token=="yyyy" || token=="yy" || token=="y") {
			if (token=="yyyy") { x=4;y=4; }
			if (token=="yy")   { x=2;y=2; }
			if (token=="y")    { x=2;y=4; }
			year=_getInt(val,i_val,x,y);
			if (year==null) { return 0; }
			i_val += year.length;
			if (year.length==2) {
				if (year > 70) { year=1900+(year-0); }
				else { year=2000+(year-0); }
				}
			}
		else if (token=="MM"||token=="M") {
			month=_getInt(val,i_val,token.length,2);
			if(month==null||(month<1)||(month>12)){return 0;}
			i_val+=month.length;}
		else if (token=="dd"||token=="d") {
			date=_getInt(val,i_val,token.length,2);
			if(date==null||(date<1)||(date>31)){return 0;}
			i_val+=date.length;}
		else {
			if (val.substring(i_val,i_val+token.length)!=token) {return 0;}
			else {i_val+=token.length;}
			}
		}
	// If there are any trailing characters left in the value, it doesn't match
	if (i_val != val.length) { return 0; }
	// Is date valid for month?
	if (month==2) {
		// Check for leap year
		if ( ( (year%4==0)&&(year%100 != 0) ) || (year%400==0) ) { // leap year
			if (date > 29){ return 0; }
			}
		else { if (date > 28) { return 0; } }
		}
	if ((month==4)||(month==6)||(month==9)||(month==11)) {
		if (date > 30) { return 0; }
		}
	var newdate=new Date(year,month-1,date);
	return newdate;
	}

	// *********************************************************
///////////////K1////////////////Persian Calendar Goes Here
	// *********************************************************


	var datePickerDivID = "datepicker";
	var iFrameDivID = "datepickeriframe";

	var dayArrayShort = new Array('&#1588;&#1606;', '&#1740;&#1705;', '&#1583;&#1608;', '&#1587;&#1607;', '&#1670;&#1607;', '&#1662;&#1606;', '&#1580;&#1605;');
	var dayArrayMed = new Array('&#1588;&#1606;&#1576;&#1607;', '&#1740;&#1705;&#1588;&#1606;&#1576;&#1607;', '&#1583;&#1608;&#1588;&#1606;&#1576;&#1607;', '&#1587;&#1607;&#32;&#1588;&#1606;&#1576;&#1607;', '&#1670;&#1607;&#1575;&#1585;&#1588;&#1606;&#1576;&#1607;', '&#1662;&#1606;&#1580;&#1588;&#1606;&#1576;&#1607;', '&#1580;&#1605;&#1593;&#1607;');
	var dayArrayLong = dayArrayMed;
	var monthArrayShort = new Array('&#1601;&#1585;&#1608;&#1585;&#1583;&#1740;&#1606;', '&#1575;&#1585;&#1583;&#1740;&#1576;&#1607;&#1588;&#1578;', '&#1582;&#1585;&#1583;&#1575;&#1583;', '&#1578;&#1740;&#1585;', '&#1605;&#1585;&#1583;&#1575;&#1583;', '&#1588;&#1607;&#1585;&#1740;&#1608;&#1585;', '&#1605;&#1607;&#1585;', '&#1570;&#1576;&#1575;&#1606;', '&#1570;&#1584;&#1585;', '&#1583;&#1740;', '&#1576;&#1607;&#1605;&#1606;', '&#1575;&#1587;&#1601;&#1606;&#1583;');
	var monthArrayMed = monthArrayShort;
	var monthArrayLong = monthArrayShort;

	// these variables define the date formatting we're expecting and outputting.
	// If you want to use a different format by default, change the defaultDateSeparator
	// and defaultDateFormat variables either here or on your HTML page.
	var defaultDateSeparator = "/";        // common values would be "/" or "."
	var defaultDateFormat = "ymd"    // valid values are "mdy", "dmy", and "ymd"
	var dateSeparator = defaultDateSeparator;
	var dateFormat = defaultDateFormat;

	function displayDatePicker(dateFieldName, displayBelowThisObject, dtFormat, dtSep) {
		var targetDateField = document.getElementsByName(dateFieldName).item(0);

		// if we weren't told what node to display the datepicker beneath, just display it
		// beneath the date field we're updating
		if (!displayBelowThisObject)
			displayBelowThisObject = targetDateField;

		// if a date separator character was given, update the dateSeparator variable
		if (dtSep)
			dateSeparator = dtSep;
		else
			dateSeparator = defaultDateSeparator;

		// if a date format was given, update the dateFormat variable
		if (dtFormat)
			dateFormat = dtFormat;
		else
			dateFormat = defaultDateFormat;

		var x = displayBelowThisObject.offsetLeft;
		var y = displayBelowThisObject.offsetTop + displayBelowThisObject.offsetHeight;

		// deal with elements inside tables and such
		var parent = displayBelowThisObject;
		while (parent.offsetParent) {
			parent = parent.offsetParent;
			x += parent.offsetLeft;
			y += parent.offsetTop;
		}

		drawDatePicker(targetDateField, x, y);
	}

	/**
	Draw the datepicker object (which is just a table with calendar elements) at the
	specified x and y coordinates, using the targetDateField object as the input tag
	that will ultimately be populated with a date.

	This function will normally be called by the displayDatePicker function.
	*/
	function drawDatePicker(targetDateField, x, y) {
		var dt = getFieldDate(targetDateField.value);

		// the datepicker table will be drawn inside of a <div> with an ID defined by the
		// global datePickerDivID variable. If such a div doesn't yet exist on the HTML
		// document we're working with, add one.
		if (!document.getElementById(datePickerDivID)) {
			// don't use innerHTML to update the body, because it can cause global variables
			// that are currently pointing to objects on the page to have bad references
			//document.body.innerHTML += "<div id='" + datePickerDivID + "' class='dpDiv'></div>";
			var newNode = document.createElement("div");
			newNode.setAttribute("id", datePickerDivID);
			newNode.setAttribute("class", "dpDiv");
			newNode.setAttribute("style", "visibility: hidden;");
			document.body.appendChild(newNode);
		}

		// move the datepicker div to the proper x,y coordinate and toggle the visiblity
		var pickerDiv = document.getElementById(datePickerDivID);
		pickerDiv.style.position = "absolute";
		pickerDiv.style.left = x + "px";
		pickerDiv.style.top = y + "px";
		pickerDiv.style.visibility = (pickerDiv.style.visibility == "visible" ? "hidden" : "visible");
		pickerDiv.style.display = (pickerDiv.style.display == "block" ? "none" : "block");
		pickerDiv.style.zIndex = 10000;

		// draw the datepicker table
		refreshDatePicker(targetDateField.name, dt[0], dt[1], dt[2]);
	}


	/**
	This is the function that actually draws the datepicker calendar.
	*/
	function refreshDatePicker(dateFieldName, year, month, day) {
		// if no arguments are passed, use today's date; otherwise, month and year
		// are required (if a day is passed, it will be highlighted later)
		var thisDay = getTodayPersian();
		var weekday = (thisDay[3] - thisDay[2] + 1) % 7;
		if (!day)
			day = thisDay[2];
		if (!month)
			month = thisDay[1];
		if (!year)
			year = thisDay[0];
		if ((month >= 1) && (year > 0)) {
			thisDay = calcPersian(year, month, 1);
			weekday = thisDay[3];
			thisDay = new Array(year, month, day, weekday);
			thisDay[2] = 1;
		} else {
			day = thisDay[2];
			thisDay[2] = 1;
		}

		// the calendar will be drawn as a table
		// you can customize the table elements with a global CSS style sheet,
		// or by hardcoding style and formatting elements below
		var crlf = "\r\n";
		var TABLE = "<table cols=7 class='dpTable'>" + crlf;
		var xTABLE = "</table>" + crlf;
		var TR = "<tr class='dpTR'>";
		var TR_title = "<tr class='dpTitleTR'>";
		var TR_days = "<tr class='dpDayTR'>";
		var TR_todaybutton = "<tr class='dpTodayButtonTR'>";
		var xTR = "</tr>" + crlf;
		var TD = "<td class='dpTD' onMouseOut='this.className=\"dpTD\";' onMouseOver=' this.className=\"dpTDHover\";' ";    // leave this tag open, because we'll be adding an onClick event
		var TD_title = "<td colspan=5 class='dpTitleTD'>";
		var TD_buttons = "<td class='dpButtonTD'>";
		var TD_todaybutton = "<td colspan=7 class='dpTodayButtonTD'>";
		var TD_days = "<td class='dpDayTD'>";
		var TD_selected = "<td class='dpDayHighlightTD' onMouseOut='this.className=\"dpDayHighlightTD\";' onMouseOver='this.className=\"dpTDHover\";' ";    // leave this tag open, because we'll be adding an onClick event
		var xTD = "</td>" + crlf;
		var DIV_title = "<div class='dpTitleText'>";
		var DIV_selected = "<div class='dpDayHighlight'>";
		var xDIV = "</div>";

		// start generating the code for the calendar table
		var html = TABLE;

		// this is the title bar, which displays the month and the buttons to
		// go back to a previous month or forward to the next month
		html += TR_title;
		html += TD_buttons + getButtonCode(dateFieldName, thisDay, -1, "&lt;") + xTD;
		html += TD_title + DIV_title + monthArrayLong[thisDay[1] - 1] + " " + thisDay[0] + xDIV + xTD;
		html += TD_buttons + getButtonCode(dateFieldName, thisDay, 1, "&gt;") + xTD;
		html += xTR;

		// this is the row that indicates which day of the week we're on
		html += TR_days;
		var i;
		for (i = 0; i < dayArrayShort.length; i++)
			html += TD_days + dayArrayShort[i] + xTD;
		html += xTR;

		// now we'll start populating the table with days of the month
		html += TR;

		// first, the leading blanks
		if (weekday != 6)
			for (i = 0; i <= weekday; i++)
			html += TD + "&nbsp;" + xTD;

		// now, the days of the month
		var len = 31;
		if (thisDay[1] > 6)
			len = 30;
		if (thisDay[1] == 12 && !leap_persian(thisDay[0]))
			len = 29;

		for (var dayNum = thisDay[2]; dayNum <= len; dayNum++) {
			TD_onclick = " onclick=\"updateDateField('" + dateFieldName + "', '" + getDateString(thisDay) + "');\">";

			if (dayNum == day)
				html += TD_selected + TD_onclick + DIV_selected + dayNum + xDIV + xTD;
			else
				html += TD + TD_onclick + dayNum + xTD;

			// if this is a Friday, start a new row
			if (weekday == 5)
				html += xTR + TR;
			weekday++;
			weekday = weekday % 7;

			// increment the day
			thisDay[2]++;
		}

		// fill in any trailing blanks
		if (weekday > 0) {
			for (i = 6; i > weekday; i--)
				html += TD + "&nbsp;" + xTD;
		}
		html += xTR;

		// add a button to allow the user to easily return to today, or close the calendar
		var today = new Date()
		var todayString = "Today is " + dayArrayMed[today.getDay()] + ", " + monthArrayMed[today.getMonth()] + " " + today.getDate();
		html += TR_todaybutton + TD_todaybutton;
		html += "<button class='dpTodayButton' onClick='refreshDatePicker(\"" + dateFieldName + "\");'>&#1575;&#1605;&#1585;&#1608;&#1586;</button> ";
		html += "<button class='dpTodayButton' onClick='updateDateField(\"" + dateFieldName + "\");'>&#1576;&#1587;&#1578;&#1606;</button>";
		html += xTD + xTR;

		// and finally, close the table
		html += xTABLE;

		document.getElementById(datePickerDivID).innerHTML = html;
		// add an "iFrame shim" to allow the datepicker to display above selection lists
		adjustiFrame();
	}


	/**
	Convenience function for writing the code for the buttons that bring us back or forward
	a month.
	*/
	function getButtonCode(dateFieldName, dateVal, adjust, label) {
		var newMonth = (dateVal[1] + adjust) % 12;
		var newYear = dateVal[0] + parseInt((dateVal[1] + adjust) / 12);
		if (newMonth < 1) {
			newMonth += 12;
			newYear += -1;
		}

		return "<button class='dpButton' onClick='refreshDatePicker(\"" + dateFieldName + "\", " + newYear + ", " + newMonth + ");'>" + label + "</button>";
	}


	/**
	Convert a JavaScript Date object to a string, based on the dateFormat and dateSeparator
	variables at the beginning of this script library.
	*/
	function getDateString(dateVal) {
		var dayString = "00" + dateVal[2];
		var monthString = "00" + (dateVal[1]);
		dayString = dayString.substring(dayString.length - 2);
		monthString = monthString.substring(monthString.length - 2);

		switch (dateFormat) {
			case "dmy":
				return dayString + dateSeparator + monthString + dateSeparator + dateVal[0];
			case "ymd":
				return dateVal[0] + dateSeparator + monthString + dateSeparator + dayString;
			case "mdy":
			default:
				return monthString + dateSeparator + dayString + dateSeparator + dateVal[0];
		}
	}


	/**
	Convert a string to a JavaScript Date object.
	*/
	function getFieldDate(dateString) {
		var dateVal;
		var dArray;
		var d, m, y;

		try {
			dArray = splitDateString(dateString);
			if (dArray) {
				switch (dateFormat) {
					case "dmy":
						d = parseInt(dArray[0], 10);
						m = parseInt(dArray[1], 10);
						y = parseInt(dArray[2], 10);
						break;
					case "ymd":
						d = parseInt(dArray[2], 10);
						m = parseInt(dArray[1], 10);
						y = parseInt(dArray[0], 10);
						break;
					case "mdy":
					default:
						d = parseInt(dArray[1], 10);
						m = parseInt(dArray[0], 10);
						y = parseInt(dArray[2], 10);
						break;
				}
				dateVal = new Array(y, m, d);
			} else if (dateString) {
				dateVal = getTodayPersian();
			} else {
				dateVal = getTodayPersian();
			}
		} catch (e) {
			dateVal = getTodayPersian();
		}

		return dateVal;
	}


	/**
	Try to split a date string into an array of elements, using common date separators.
	If the date is split, an array is returned; otherwise, we just return false.
	*/
	function splitDateString(dateString) {
		var dArray;
		if (dateString.indexOf("/") >= 0)
			dArray = dateString.split("/");
		else if (dateString.indexOf(".") >= 0)
			dArray = dateString.split(".");
		else if (dateString.indexOf("-") >= 0)
			dArray = dateString.split("-");
		else if (dateString.indexOf("\\") >= 0)
			dArray = dateString.split("\\");
		else
			dArray = false;

		return dArray;
	}

	/**
	Update the field with the given dateFieldName with the dateString that has been passed,
	and hide the datepicker. If no dateString is passed, just close the datepicker without
	changing the field value.

	Also, if the page developer has defined a function called datePickerClosed anywhere on
	the page or in an imported library, we will attempt to run that function with the updated
	field as a parameter. This can be used for such things as date validation, setting default
	values for related fields, etc. For example, you might have a function like this to validate
	a start date field:

	function datePickerClosed(dateField)
	{
	var dateObj = getFieldDate(dateField.value);
	var today = new Date();
	today = new Date(today.getFullYear(), today.getMonth(), today.getDate());
 
	if (dateField.name == "StartDate") {
	if (dateObj < today) {
	// if the date is before today, alert the user and display the datepicker again
	alert("Please enter a date that is today or later");
	dateField.value = "";
	document.getElementById(datePickerDivID).style.visibility = "visible";
	adjustiFrame();
	} else {
	// if the date is okay, set the EndDate field to 7 days after the StartDate
	dateObj.setTime(dateObj.getTime() + (7 * 24 * 60 * 60 * 1000));
	var endDateField = document.getElementsByName ("EndDate").item(0);
	endDateField.value = getDateString(dateObj);
	}
	}
	}

	*/
	function updateDateField(dateFieldName, dateString) {
		var targetDateField = document.getElementsByName(dateFieldName).item(0);
		if (dateString)
			targetDateField.value = dateString;

		var pickerDiv = document.getElementById(datePickerDivID);
		pickerDiv.style.visibility = "hidden";
		pickerDiv.style.display = "none";

		adjustiFrame();
		targetDateField.focus();

		// after the datepicker has closed, optionally run a user-defined function called
		// datePickerClosed, passing the field that was just updated as a parameter
		// (note that this will only run if the user actually selected a date from the datepicker)
		if ((dateString) && (typeof (datePickerClosed) == "function"))
			datePickerClosed(targetDateField);
	}


	/**
	Use an "iFrame shim" to deal with problems where the datepicker shows up behind
	selection list elements, if they're below the datepicker. The problem and solution are
	described at:

	http://dotnetjunkies.com/WebLog/jking/archive/2003/07/21/488.aspx
	http://dotnetjunkies.com/WebLog/jking/archive/2003/10/30/2975.aspx
	*/
	function adjustiFrame(pickerDiv, iFrameDiv) {
		// we know that Opera doesn't like something about this, so if we
		// think we're using Opera, don't even try
		var is_opera = (navigator.userAgent.toLowerCase().indexOf("opera") != -1);
		if (is_opera)
			return;

		// put a try/catch block around the whole thing, just in case
		try {
			if (!document.getElementById(iFrameDivID)) {
				// don't use innerHTML to update the body, because it can cause global variables
				// that are currently pointing to objects on the page to have bad references
				//document.body.innerHTML += "<iframe id='" + iFrameDivID + "' src='javascript:false;' scrolling='no' frameborder='0'>";
				var newNode = document.createElement("iFrame");
				newNode.setAttribute("id", iFrameDivID);
				newNode.setAttribute("src", "javascript:false;");
				newNode.setAttribute("scrolling", "no");
				newNode.setAttribute("frameborder", "0");
				document.body.appendChild(newNode);
			}

			if (!pickerDiv)
				pickerDiv = document.getElementById(datePickerDivID);
			if (!iFrameDiv)
				iFrameDiv = document.getElementById(iFrameDivID);

			try {
				iFrameDiv.style.position = "absolute";
				iFrameDiv.style.width = pickerDiv.offsetWidth;
				iFrameDiv.style.height = pickerDiv.offsetHeight;
				iFrameDiv.style.top = pickerDiv.style.top;
				iFrameDiv.style.left = pickerDiv.style.left;
				iFrameDiv.style.zIndex = pickerDiv.style.zIndex - 1;
				iFrameDiv.style.visibility = pickerDiv.style.visibility;
				iFrameDiv.style.display = pickerDiv.style.display;
			} catch (e) {
			}

		} catch (ee) {
		}

	}

	/*
	Jalali (Shamsi) Calendar Date Picker Version 1.00 (JavaScript)
	-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
	Written By : Amin Habibi Shahri
	E-mail : habibiamin@gmail.com
	Homepage: http://habibiamin.googlepages.com
	*/

	/*  MOD  --  Modulus function which works for non-integers.  */

	function mod(a, b) {
		return a - (b * Math.floor(a / b));
	}

	function jwday(j) {
		return mod(Math.floor((j + 1.5)), 7);
	}

	var Weekdays = new Array("Sunday", "Monday", "Tuesday", "Wednesday",
                          "Thursday", "Friday", "Saturday");

	//  LEAP_GREGORIAN  --  Is a given year in the Gregorian calendar a leap year ?

	function leap_gregorian(year) {
		return ((year % 4) == 0) &&
            (!(((year % 100) == 0) && ((year % 400) != 0)));
	}

	//  GREGORIAN_TO_JD  --  Determine Julian day number from Gregorian calendar date

	var GREGORIAN_EPOCH = 1721425.5;

	function gregorian_to_jd(year, month, day) {
		return (GREGORIAN_EPOCH - 1) +
           (365 * (year - 1)) +
           Math.floor((year - 1) / 4) +
           (-Math.floor((year - 1) / 100)) +
           Math.floor((year - 1) / 400) +
           Math.floor((((367 * month) - 362) / 12) +
           ((month <= 2) ? 0 :
                               (leap_gregorian(year) ? -1 : -2)
           ) +
           day);
	}

	//  JD_TO_GREGORIAN  --  Calculate Gregorian calendar date from Julian day

	function jd_to_gregorian(jd) {
		var wjd, depoch, quadricent, dqc, cent, dcent, quad, dquad,
        yindex, dyindex, year, yearday, leapadj;

		wjd = Math.floor(jd - 0.5) + 0.5;
		depoch = wjd - GREGORIAN_EPOCH;
		quadricent = Math.floor(depoch / 146097);
		dqc = mod(depoch, 146097);
		cent = Math.floor(dqc / 36524);
		dcent = mod(dqc, 36524);
		quad = Math.floor(dcent / 1461);
		dquad = mod(dcent, 1461);
		yindex = Math.floor(dquad / 365);
		year = (quadricent * 400) + (cent * 100) + (quad * 4) + yindex;
		if (!((cent == 4) || (yindex == 4))) {
			year++;
		}
		yearday = wjd - gregorian_to_jd(year, 1, 1);
		leapadj = ((wjd < gregorian_to_jd(year, 3, 1)) ? 0
                                                  :
                  (leap_gregorian(year) ? 1 : 2)
              );
		month = Math.floor((((yearday + leapadj) * 12) + 373) / 367);
		day = (wjd - gregorian_to_jd(year, month, 1)) + 1;

		return new Array(year, month, day);
	}

	//  LEAP_PERSIAN  --  Is a given year a leap year in the Persian calendar ?

	function leap_persian(year) {
		return ((((((year - ((year > 0) ? 474 : 473)) % 2820) + 474) + 38) * 682) % 2816) < 682;
	}

	//  PERSIAN_TO_JD  --  Determine Julian day from Persian date

	var PERSIAN_EPOCH = 1948320.5;
	var PERSIAN_WEEKDAYS = new Array("í˜ÔäÈå", "ÏæÔäÈå",
                                 "Óå ÔäÈå", "åÇÑÔäÈå",
                                 "äÌ ÔäÈå", "ÌãÚå", "ÔäÈå");

	function persian_to_jd(year, month, day) {
		var epbase, epyear;

		epbase = year - ((year >= 0) ? 474 : 473);
		epyear = 474 + mod(epbase, 2820);

		return day +
            ((month <= 7) ?
                ((month - 1) * 31) :
                (((month - 1) * 30) + 6)
            ) +
            Math.floor(((epyear * 682) - 110) / 2816) +
            (epyear - 1) * 365 +
            Math.floor(epbase / 2820) * 1029983 +
            (PERSIAN_EPOCH - 1);
	}

	//  JD_TO_PERSIAN  --  Calculate Persian date from Julian day

	function jd_to_persian(jd) {
		var year, month, day, depoch, cycle, cyear, ycycle,
        aux1, aux2, yday;


		jd = Math.floor(jd) + 0.5;

		depoch = jd - persian_to_jd(475, 1, 1);
		cycle = Math.floor(depoch / 1029983);
		cyear = mod(depoch, 1029983);
		if (cyear == 1029982) {
			ycycle = 2820;
		} else {
			aux1 = Math.floor(cyear / 366);
			aux2 = mod(cyear, 366);
			ycycle = Math.floor(((2134 * aux1) + (2816 * aux2) + 2815) / 1028522) +
                    aux1 + 1;
		}
		year = ycycle + (2820 * cycle) + 474;
		if (year <= 0) {
			year--;
		}
		yday = (jd - persian_to_jd(year, 1, 1)) + 1;
		month = (yday <= 186) ? Math.ceil(yday / 31) : Math.ceil((yday - 6) / 30);
		day = (jd - persian_to_jd(year, month, 1)) + 1;
		return new Array(year, month, day);
	}

	function calcPersian(year, month, day) {
		var date, j;

		j = persian_to_jd(year, month, day);
		date = jd_to_gregorian(j);
		weekday = jwday(j);
		return new Array(date[0], date[1], date[2], weekday);
	}

	//  calcGregorian  --  Perform calculation starting with a Gregorian date
	function calcGregorian(year, month, day) {
		month--;

		var j, weekday;

		//  Update Julian day

		j = gregorian_to_jd(year, month + 1, day) +
           (Math.floor(0 + 60 * (0 + 60 * 0) + 0.5) / 86400.0);

		//  Update Persian Calendar
		perscal = jd_to_persian(j);
		weekday = jwday(j);
		return new Array(perscal[0], perscal[1], perscal[2], weekday);
	}

	function getTodayGregorian() {
		var t = new Date();
		var today = new Date();

		var y = today.getYear();
		if (y < 1000) {
			y += 1900;
		}

		return new Array(y, today.getMonth() + 1, today.getDate(), t.getDay());
	}

	function getTodayPersian() {
		var t = new Date();
		var today = getTodayGregorian();

		var persian = calcGregorian(today[0], today[1], today[2]);
		return new Array(persian[0], persian[1], persian[2], t.getDay());
	}


