PTCalendar = function (firstDayOfWeek, dateStr, onSelected, onClose) {
	this.calType = calendarType;
	this.activeDiv = null;
	this.currentDateEl = null;
	this.timeout = null;
	this.onSelected = onSelected || null;
	this.onClose = onClose || null;
	this.hidden = false;
	this.minYear = 1970;
	this.maxYear = 2050;
	this.dateFormat = null;
	this.isPopup = true;
	this.weekNumbers = true;
	this.firstDayOfWeek = typeof firstDayOfWeek == "number" ? firstDayOfWeek : 0;
	this.ar_days = null;
	this.hiliteToday = true;
	this.multiple = null;
	this.showsOtherMonths = false;
	// HTML elements
	this.table = null;
	this.Headertable = null;
	this.element = null;
	this.tbody = null;
	this.firstdayname = null;
	// Combo boxes
	this.monthsCombo = null;
	this.yearsCombo = null;
	this.hilitedMonth = null;
	this.activeMonth = null;
	this.hilitedYear = null;
	this.activeYear = null;
	// Information
	this.dateClicked = false;
	this.firstDayOfMonth=0;
	this.CellSel = null;
	this.TodayCell = null;
};
PTCalendar._C = null;
PTCalendar.is_ie = ( /msie/i.test(navigator.userAgent) &&
		   !/opera/i.test(navigator.userAgent) );
PTCalendar.is_ie5 = ( PTCalendar.is_ie && /msie 5\.0/i.test(navigator.userAgent) );
PTCalendar.is_opera = /opera/i.test(navigator.userAgent);
PTCalendar.is_khtml = /Konqueror|Safari|KHTML/i.test(navigator.userAgent);
PTCalendar.getAbsolutePos = function(el) {
	var SL = 0, ST = 0;
	var is_div = /^div$/i.test(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 = this.getAbsolutePos(el.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
	}
	return r;
};

PTCalendar.removeClass = function(el, className) {
	if (!(el && el.className)) {
		return;
	}
	var cls = el.className.split(" ");
	var ar = new Array();
	for (var i = cls.length; i > 0;) {
		if (cls[--i] != className) {
			ar[ar.length] = cls[i];
		}
	}
	el.className = ar.join(" ");
};

PTCalendar.addClass = function(el, className) {
	PTCalendar.removeClass(el, className);
	el.className += " " + className;
};


PTCalendar.getElement = function(ev) {
	var f = PTCalendar.is_ie ? window.event.srcElement : ev.currentTarget;
	while (f.nodeType != 1 || /^div$/i.test(f.tagName))
		f = f.parentNode;
	return f;
};

PTCalendar.getTargetElement = function(ev) {
	var f = PTCalendar.is_ie ? window.event.srcElement : ev.target;
	while (f.nodeType != 1)
		f = f.parentNode;
	return f;
};

PTCalendar.stopEvent = function(ev) {
	ev || (ev = window.event);
	if (PTCalendar.is_ie) {
		ev.cancelBubble = true;
		ev.returnValue = false;
	} else {
		ev.preventDefault();
		ev.stopPropagation();
	}
	return false;
};

PTCalendar.addEvent = function(el, evname, func) {
	if (el.attachEvent) { // IE
		el.attachEvent("on" + evname, func);
	} else if (el.addEventListener) { // Gecko / W3C
		el.addEventListener(evname, func, true);
	} else {
		el["on" + evname] = func;
	}
};

PTCalendar.removeEvent = function(el, evname, func) {
	if (el.detachEvent) { // IE
		el.detachEvent("on" + evname, func);
	} else if (el.removeEventListener) { // Gecko / W3C
		el.removeEventListener(evname, func, true);
	} else {
		el["on" + evname] = null;
	}
};

PTCalendar.createElement = function(type, parent) {
	var el = null;
	if (document.createElementNS) {
		// use the XHTML namespace; IE won't normally get here unless
		// _they_ "fix" the DOM2 implementation.
		el = document.createElementNS("http://www.w3.org/1999/xhtml", type);
	} else {
		el = document.createElement(type);
	}
	if (typeof parent != "undefined") {
		parent.appendChild(el);
	}

	el.style.zIndex = 350;

	return el;
};

// END: UTILITY FUNCTIONS

// BEGIN: CALENDAR STATIC FUNCTIONS

/** Internal -- adds a set of events to make some element behave like a button. */
PTCalendar._add_evs = function(el) {
	with (PTCalendar) {
		addEvent(el, "click", dayMouseClick);
		addEvent(el, "dblclick", dayMouseDblClick);
		if (is_ie) {
			//addEvent(el, "dblclick", dayMouseDblClick);
			el.setAttribute("unselectable", true);
		}
	}
};

// event handlers

PTCalendar.tableMouseDown = function (ev) {
	if (PTCalendar.getTargetElement(ev) == PTCalendar.getElement(ev)) {
		return PTCalendar.stopEvent(ev);
	}
};


PTCalendar.dayMouseClick = function(ev) {
	var cal = _PS_popupCalendar;
	if(cal!= null)
	{
		if(PTCalendar.getElement(ev).disabled == true)
			return;
		cal.dateClicked=true;
		PTCalendar.cellClick(PTCalendar.getElement(ev), ev || window.event);
		if (PTCalendar.is_ie) document.selection.empty();
	}
};

PTCalendar.dayMouseDblClick = function(ev) {
	var cal = _PS_popupCalendar;
	cal.dayMouseClick(ev);
};

PTCalendar.closeWindow = function() {
	dateBoxOpen = false;
    var cal = window._PS_popupCalendar;
	if(cal!=null)
    	cal.callCloseHandler();
       
};

PTCalendar.HandleInnercell = function() {
	var cal = _PS_popupCalendar;
	if(cal!=null)
	{
		var cell1 = cal.TodayCell;
		cal.dateClicked=true;
		PTCalendar.cellClick(cell1, cell1 || window.event);
	}
	
};

PTCalendar.previousMonth = function() {
	prevMonth(0);
};
PTCalendar.nextMonth = function() {
	nextMonth(0)
};
PTCalendar.toDay = function() {
var date = new Date();
callHandler();
};
PTCalendar.MonthChanged = function() {
	var cal = window._PS_popupCalendar;
	var mon = cal.monthsCombo.selectedIndex;
	cal.dateClicked = false;
	selectMonth(mon);
};

PTCalendar.YearChanged = function() {
	var cal = window._PS_popupCalendar;
    var date = null;
	cal.dateClicked = false;
	if(cal.calType =="H")
	{
		date = new window.HijriDate(cal.date.year,cal.date.month,cal.date.day);
		date.year =cal.yearsCombo.selectedIndex+1321;
	}
	else
	{
		date = new Date(cal.date);
		var nYearIndex = cal.yearsCombo.selectedIndex+1900;
		if(cal.calType =="T") 
		    nYearIndex+=543;
		date.PT_setFullYear(nYearIndex);
	}
	cal.callHandler();
	cal.setDate(date);
};

PTCalendar.SelectToday = function() {
    var date = null;
    var date1 = null;
	var cal = window._PS_popupCalendar;
    cal.dateClicked=true;
	if(cal.calType =="H")
	{
		var Hijridate = new window.HijriDate();
		date = new Date(Hijridate.year,Hijridate.month,Hijridate.day);
		date1 = date;
	}
	else
		date = new Date();	
	cal.setDate(date);
	if(cal.calType =="H")
		cal.date = date1;
	cal.callHandler();
	cal.callCloseHandler();
};
