/** keyboard navigation, only for popup calendars */
PTCalendar._keyEvent = function(ev) {
	var cal = window._PS_popupCalendar;
	if (!cal || cal.multiple)
		return false;
	(PTCalendar.is_ie) && (ev = window.event);
	var act = (PTCalendar.is_ie || ev.type == "keypress"),
		K = ev.keyCode;

	if (ev.ctrlKey) {
		switch (K) {
		    case 37: // KEY left
			act && PTCalendar.cellClick(cal._nav_pm);
			break;
		    case 38: // KEY up
			act && PTCalendar.cellClick(cal._nav_py);
			break;
		    case 39: // KEY right
			act && PTCalendar.cellClick(cal._nav_nm);
			break;
		    case 40: // KEY down
			act && PTCalendar.cellClick(cal._nav_ny);
			break;
		    default:
			return false;
		}
	} else switch (K) {
	    case 27: // KEY esc
		act && cal.callCloseHandler();
		break;
	    case 37: // KEY left
	    case 38: // KEY up
	    case 39: // KEY right
	    case 40: // KEY down
		if (act) {
		
			var prev, x, y, ne, el, step;
			prev = K == 37 || K == 38;
			step = (K == 37 || K == 39) ? 1 : 7;
			function setVars() {
				el = cal.currentDateEl;
				var p = el.pos;
				x = p & 15;
				y = p >> 4;
				ne = cal.ar_days[y][x];
			};setVars();
			
			while (1) {
				switch (K) {
				    case 37: // KEY left
					if (--x >= 0)
						ne = cal.ar_days[y][x];
					else {
						x = 6;
						K = 38;
						continue;
					}
					break;
				    case 38: // KEY up
					if (--y >= 0)
						ne = cal.ar_days[y][x];
					else {
					    if (x == 6)
					        prevMonth(1);
					    else
					        prevMonth(7);
						setVars();
					}
					break;
				    case 39: // KEY right
					if (++x < 7)
						ne = cal.ar_days[y][x];
					else {
						x = 0;
						K = 40;
						continue;
					}
					break;
				    case 40: // KEY down
					if (++y < cal.ar_days.length && cal.ar_days[y][x].innerHTML !="&nbsp;")
						ne = cal.ar_days[y][x];
					else {
					    if (x == 0)
					        nextMonth(1);
					    else
					        nextMonth(7);
						setVars();
					}
					break;
				}
				break;
			}
			if (ne) {
			
				if (!ne.disabled)
				{
					PTCalendar.cellClick(ne);
					}
				else 
				{
					if(K == 37 || K == 39)
						step = 1;
					else if(K == 38 || K == 40)
						step = 7;

					if (prev)
				       prevMonth(step);
				    else
					   nextMonth(step);
				}
			}
		}
		break;
	    case 13: // KEY enter
		if (act)
		{
		
			PTCalendar.cellClick(cal.currentDateEl, ev);
		}
		break;
	    default:
		return false;
	}
	return PTCalendar.stopEvent(ev);
};
/**
 *  (RE)Initializes the calendar to the given date and firstDayOfWeek
 */
PTCalendar.prototype._init = function (firstDayOfWeek, date) {
	var today,TY,TM,TD,SrcDay;
	var year,month,mday,no_days;

	if(this.calType == "H")
	{
		today = new window.HijriDate();
		TY = today.year;
		TM = today.month;
		TD = today.day;
		var srcDate = new window.HijriDate(date.year,date.month,date.day);
		SrcDay = srcDate.day;
		year = srcDate.year;
		month = srcDate.month;
		mday = srcDate.day;
		no_days = srcDate.daysOfMonth();
		var startJul = window.HijriToJulian(year,month,1);
		var startGregorian = window.JulianToGregorian(startJul);
		this.firstDayOfMonth = 	startGregorian.getDay();
		this.date = new window.HijriDate(year,month,mday);
	}
	else
	{
		today = new Date();
		TY = today.getFullYear();
		TM = today.getMonth();
		TD = today.getDate();
		//SrcDate = new Date(date);
		SrcDay = date.getDate();
		year = date.getFullYear();
		var year = date.getFullYear();
		if (year < this.minYear) {
			year = this.minYear;
			date.PT_setFullYear(year);
		} else if (year > this.maxYear) {
			year = this.maxYear;
			date.PT_setFullYear(year);
		}	
		month = date.getMonth();
		mday = date.getDate();
		no_days = date.getMonthDays();
		this.date = new Date(date);
		date.setDate(1);
		this.firstDayOfMonth = date.getDay();
	}
	
	this.table.style.visibility = "hidden";
	this.firstDayOfWeek = firstDayOfWeek;
	var day1 = (this.firstDayOfMonth - this.firstDayOfWeek) % 7;
    var currentDate = 1;
	if (day1 < 0)
		day1 += 7;
	var row = this.tbody.firstChild;
	var ar_days = this.ar_days = new Array();
	var dates = this.multiple ? (this.datesCells = {}) : null;
	for (var i = 0; i < 6; ++i, row = row.nextSibling) {
		var cell = row.firstChild;
		cell.style.backgroundImage = "";
		if (this.weekNumbers) {
				cell.style.width="1.6em";
				cell.style.textAlign  = 'left';
				cell.style.paddingTop = "1px";
				cell.style.paddingRight = "2px";
				cell.style.paddingBottom = "1px";
				cell.style.paddingLeft = "1px";

			cell = cell.nextSibling;
			
		}
		var hasdays = false, iday, dpos = ar_days[i] = [];
			for (var j = 0; j < 7; ++j, cell = cell.nextSibling) {
			if(this.calType =="H")
				if(currentDate> 31) break;
    			var sBorderCls="";
				cell.style.fontFamily = 'Arial, sans-serif';
				cell.style.width="19";
				cell.style.height="19";
				cell.style.color = '#000';
				cell.style.textAlign  = 'center';
				cell.style.paddingTop = "2px";
				cell.style.paddingRight = "3px";
				cell.style.paddingBottom = "3px";
				cell.style.paddingLeft = "2px";
				cell.style.fontWeight ="normal";
				if( PTCalendar.is_ie == true)
					cell.style.cursor='hand';

			if(PTCalendar.is_khtml == true)
				{
					cell.style.paddingRight = "4px";
					cell.style.paddingBottom = "2px";
				}
            	if(i==0 && j<7)
				{
					cell.style.borderTop = "1px";
					cell.style.borderBottom = "0px";
					cell.style.borderLeft = "0px";
					cell.style.borderRight = "0px";
					cell.style.borderStyle ="solid";
					cell.style.borderColor ="#797979";

				}
				if(j==0)
				{
					if(i!=0)
						cell.style.borderTop = "0px";
					else
						cell.style.borderTop = "1px";

					cell.style.borderBottom = "0px";
					if(this.table.dir == 'ltr')
					{
						cell.style.borderLeft = "1px";
						cell.style.borderRight = "0px";
					}
					else
					{
						cell.style.borderLeft = "0px";
						cell.style.borderRight = "1px";
					}

					cell.style.borderStyle ="solid";
					cell.style.borderColor ="#797979";

				}


				if(j>0 && j%6==0)
				{
					cell.style.borderBottom = "0px";
					if(i!=0)
						cell.style.borderTop = "0px";
					else
						cell.style.borderTop = "1px";
					if(this.table.dir == 'ltr')
					{
						cell.style.borderLeft = "0px";
						cell.style.borderRight = "1px";
					}
					else
					{
						cell.style.borderLeft = "1px";
						cell.style.borderRight = "0px";
					}
					cell.style.borderStyle ="solid";
					cell.style.borderColor ="#797979";
				}


				if(i==5&& j<7)
				{
					cell.style.borderTop = "0px";
					cell.style.borderBottom = "1px";
					if(j!=0) {
			 			if(this.table.dir == 'ltr')					
						   cell.style.borderLeft = "0px";					
					    else					
						   cell.style.borderRight = "0px";
					    }
                    else {
		                if(this.table.dir == 'ltr')					
						   cell.style.borderLeft = "1px";									
					    else									
						   cell.style.borderRight = "1px";
					 }
					if(j>0 && j%6==0) {
			           if(this.table.dir == 'ltr')
					    	cell.style.borderRight = "1px";
                       else
					    	cell.style.borderLeft = "1px"; }
                    else {
                         if(this.table.dir == 'ltr')
						    cell.style.borderRight = "0px";
                         else
						    cell.style.borderLeft = "0px";
                    }
					cell.style.borderStyle ="solid";
					cell.style.borderColor ="#797979";

				}
			cell.pos = i << 4 | j;
			dpos[j] = cell;
			var current_month = true;
			if(day1>0)
			{
				current_month = false;
				day1--;
			}
			if (!current_month) {
				
				cell.style.backgroundImage = "";
				cell.style.fontFamily = "Arial, sans-serif";
				cell.style.width="19px";
				cell.style.height="19px";
				cell.style.color = "#000000";
				cell.style.textAlign  = "center";
				cell.style.paddingTop = "2px";
				cell.style.paddingRight = "3px";
				cell.style.paddingBottom = "3px";
				cell.style.paddingLeft = "2px";
				cell.innerHTML = "&nbsp;";
				cell.disabled = true;
				continue;
			
			} else {
				cell.otherMonth = false;
				hasdays = true;
			}
			cell.disabled = false;
			if(currentDate>no_days)
			{
			    cell.style.backgroundImage = "";
				cell.style.fontFamily = "Arial, sans-serif";
				cell.style.width="19px";
				cell.style.height="19px";
				cell.style.color = "#000000";
				cell.style.textAlign  = "center";
				cell.style.paddingTop = "2px";
				cell.style.paddingRight = "3px";
				cell.style.paddingBottom = "3px";
				cell.style.paddingLeft = "2px";
				cell.innerHTML = "&nbsp;";
				cell.disabled = true;
				continue;
			}
			if( PTCalendar.is_ie == false)
				cell.innerHTML ="<a href = 'javascript:SelectDay("+currentDate+")' style = 'color:black;text-decoration:none;'>"+currentDate+"</a>";
			else
				cell.innerHTML = currentDate;
			
            cell.className+=sBorderCls;
			cell.style.backgroundImage = "";
			if(j>=7)
			{
            	cell.style.fontFamily = "Arial, sans-serif";
				cell.style.width="19px";
				cell.style.height="19px";
				cell.style.color = "#000000";
				cell.style.textAlign  = "center";
				cell.style.paddingTop = "2px";
				cell.style.paddingRight = "3px";
				cell.style.paddingBottom = "3px";
				cell.style.paddingLeft = "2px";
				cell.style.background = "";
				cell.innerHTML = "&nbsp;";
				cell.disabled = true;
				continue;
			}
			if (!cell.disabled) {
				cell.calday = currentDate;
								
				if(this.calType == "H")
				{
					cell.calMonth = date.month;
					cell.calYear = date.year; 
				}
				else
				{
					cell.calMonth = date.getMonth();
					cell.calYear = date.getFullYear(); 
				}
				if (!this.multiple && current_month
				    && currentDate == mday && this.hiliteToday) {
					this.currentDateEl = cell;
				}
				if(currentDate == SrcDay &&
					cell.calMonth == month &&
					cell.calYear == year)
				{
					this.CellSel = cell;
					this.day = currentDate;
					this.month = month;
					this.year = year;
					cell.style.backgroundImage  = "url('"+loadedImages[2].src+"')";
					cell.style.backgroundRepeat = "no-repeat";
                    cell.style.backgroundPosition = "center";       //ICE 1904588000 
					cell.style.fontWeight = 'bold';

					
				}

				if (currentDate == TD &&
					cell.calMonth == TM &&
					cell.calYear == TY) {
					this.TodayCell = cell;
					
					cell.style.width="19";
					cell.style.height="19";
					cell.style.color = '#000';
					cell.style.textAlign  = 'center';
					cell.style.padding = "1px";
					cell.style.backgroundImage  = "url('"+loadedImages[1].src+"')";
					cell.style.backgroundRepeat = "no-repeat";
                    cell.style.backgroundPosition = "center";       //ICE 1904588000
					if(TD == SrcDay)
					{
						if( PTCalendar.is_ie == false)
						{
							cell.style.backgroundImage  = "url('"+loadedImages[1].src+"')";
							cell.style.textAlign  = 'center';
							cell.style.backgroundRepeat = "no-repeat";
							if(currentDate<10)
								cell.innerHTML ="<a style='font-weight:bold;background:url("+loadedImages[2].src+"); text-decoration:none;color:black;width:19;height:19;padding: 1.45px 8.5px 1.5px 2.5px;' onclick = 'javascript:PTCalendar.HandleInnercell();'>"+currentDate+"</a>";
							else
								cell.innerHTML ="<a style='font-weight:bold;background:url("+loadedImages[2].src+"); text-decoration:none;color:black;width:19;height:19;padding: 1.45px 2px 2px 2px; padding-bottom: 1px;' onclick = 'javascript:PTCalendar.HandleInnercell();'>"+currentDate+"</a>";
							
						}
						else
						{
							cell.style.backgroundImage  = "url('"+loadedImages[2].src+"')";
							cell.innerHTML ="<a style='font-weight:bold;background:url("+loadedImages[1].src+");text-align: center; text-decoration:none;color:black;width:19;height:19;'onclick = 'javascript:PTCalendar.HandleInnercell();' >"+currentDate+"</a>";
						}
					}
					else
						cell.innerHTML ="<a style='font-weight:bold;text-align: center; text-decoration:none;color:black;width:19;height:19;' onclick = 'javascript:PTCalendar.HandleInnercell();'>"+currentDate+"</a>";

					
					this.TodayCell = cell;
					
				}
    			currentDate++;
			}
		}
		if (!(hasdays || this.showsOtherMonths))
			row.className = "PT_CALemptyrow";
	}
	this.table.style.visibility = "visible";
	
};

PTCalendar.prototype.setDate = function (date) {
	this._init(this.firstDayOfWeek, date);
};

PTCalendar.prototype.setCalendarTye = function (calType) {
    this.calType = calType;
}
/** Modifies the "firstDayOfWeek" parameter (pass 0 for Synday, 1 for Monday, etc.). */
PTCalendar.prototype.setFirstDayOfWeek = function (firstDayOfWeek) {
	this._init(firstDayOfWeek, this.date);
	var nCurMonth;
	var nCurYr;
	var nStartYear = 1900;
	var nEndYear = 2100;
	var nDay=0;
    var strMonth;
    var strMonthName;
	if(this.calType=="H")
    {
       nCurMonth = this.date.month;
       nCurYr = this.date.year;
       nStartYear = 1321;
       nEndYear = 1521;
	   strMonth= "<select style = 'width:130px;' name = 'PTMonth' onChange =PTCalendar.MonthChanged()>" ;
	   strMonth = strMonth + "<option value='0'>"+ 'Muharram'+ "</option>" ;
       strMonth = strMonth + "<option value='10'>"+ 'Safar' + "</option>" ;
	   strMonth = strMonth + "<option value='1'>"+ 'Rabi Al-Awwal' + "</option>" ;
	   strMonth = strMonth + "<option value='2'>"+  'Rabi Al-Akhar' + "</option>" ;
	   strMonth = strMonth + "<option value='3'>"+ 'Jumada Al-Awwal' + "</option>" ;
	   strMonth = strMonth + "<option value='4'>"+ 'Jumada Al-Akhirah' + "</option>" ;
	   strMonth = strMonth + "<option value='5'>"+ 'Rajab' + "</option>" ;
	   strMonth = strMonth + "<option value='6'>"+ 'Shaban' + "</option>" ;
	   strMonth = strMonth + "<option value='7'>"+ 'Ramadan' + "</option>" ;
	   strMonth = strMonth + "<option value='8'>"+ 'Shawwal' + "</option>" ;
	   strMonth = strMonth + "<option value='9'>"+ 'Dhul-Qadah' + "</option>" ;
	   strMonth = strMonth + "<option value='11'>"+ 'Dhul-Hijjah' + "</option>" ;
   }
   else
   {
	   nCurMonth = this.date.getMonth();
	   nCurYr = this.date.getYear();
       if (this.calType=="T")
       {
           if( PTCalendar.is_ie == true)
               nCurYr = nCurYr + 543;
           nStartYear = 2443;
           nEndYear = 2644;
       } 
	   strMonth= "&nbsp;&nbsp;&nbsp;<select name = 'PTMonth' onChange =PTCalendar.MonthChanged()>" ;
	   strMonth = strMonth + "<option value='0'>"+ 'January' + "</option>" ;
	   strMonth = strMonth + "<option value='1'>"+ 'February' + "</option>" ;
       strMonth = strMonth + "<option value='2'>"+ 'March' + "</option>" ;
       strMonth = strMonth + "<option value='3'>"+ 'April' + "</option>" ;
       strMonth = strMonth + "<option value='4'>"+ 'May' + "</option>" ;
       strMonth = strMonth + "<option value='5'>"+ 'June' + "</option>" ;
       strMonth = strMonth + "<option value='6'>"+ 'July' + "</option>" ;
       strMonth = strMonth + "<option value='7'>"+ 'August' + "</option>" ;
       strMonth = strMonth + "<option value='8'>"+ 'September' + "</option>" ;
       strMonth = strMonth + "<option value='9'>"+ 'October' + "</option>" ;
       strMonth = strMonth + "<option value='10'>"+ 'November' + "</option>" ;
       strMonth = strMonth + "<option value='11'>"+ 'December' + "</option>" ;  
   }    

  strMonth = strMonth + "</select>";
  var strYr = " <select name = 'PTYear' onChange = PTCalendar.YearChanged()>" ;
  for (var nYr = nStartYear; nYr <= nEndYear; ++nYr)
		strYr = strYr + "<option>" + nYr + "</option>" ;
	if(nCurYr > nStartYear) nCurYr -=nStartYear;
    this.title.innerHTML = strMonth + strYr;
    this._selectCombos();
    this.monthsCombo.selectedIndex = nCurMonth;
    this.yearsCombo.selectedIndex = nCurYr;
	this.minYear = nStartYear;
	this.maxYear = nEndYear;
	
};
/** Customization of allowed year range for the calendar. */
PTCalendar.prototype.setRange = function (a, z) {
	this.minYear = a;
	this.maxYear = z;
};
/** Calls the first user handler (selectedHandler). */
PTCalendar.prototype.callHandler = function () {
	if (this.onSelected) {
		this.onSelected(this, this.date);
	}
};
/** Calls the second user handler (closeHandler). */
PTCalendar.prototype.callCloseHandler = function () {
	if (this.onClose) {
		this.onClose(this);
	}
	
};
/** Removes the calendar object from the DOM tree and destroys it. */
PTCalendar.prototype.destroy = function () {
	if (this.isPopup) {
		PTCalendar.removeEvent(document, "keydown", PTCalendar._keyEvent);
		PTCalendar.removeEvent(document, "keypress", PTCalendar._keyEvent);
		PTCalendar.removeEvent(document, "mousedown", PTCalendar._checkCalendar);
	}
	var el = this.element.parentNode;
	if (typeof el != "undefined" && el != null) 
	el.removeChild(this.element);
	PTCalendar._C = null;
	window._PS_popupCalendar = null;
};

PTCalendar._checkCalendar = function(ev) {
};
