/**
 *  A generic "click" handler :) handles all types of buttons defined in this
 *  calendar.
 */
PTCalendar.cellClick = function(el, ev) {
	var closing = false;
	var newdate = false;
	var date = null;
	var cal = el.PTCalendar;
	if (typeof el.navtype == "undefined") {
			if (cal.currentDateEl) {
                var cell = cal.CellSel;
                cell.background="";
				var bToday = false;
                var TodayDate;
				if(cal.calType == "H")
				{
					var todate = new window.HijriDate();
					TodayDate = todate.day;
					if(todate.day == el.calday && todate.month == el.calMonth && todate.year == el.calYear)
						bToday = true;
				}
				else
				{
					var todate = new Date();
					TodayDate = todate.getDate();
					if(todate.getDate() == el.calday && todate.getMonth() == el.calMonth && todate.getFullYear() == el.calYear)
					{
						bToday = true;
					}
				} 
						
				cell.style.backgroundRepeat = "no-repeat"; 
				
				if(cal.CellSel == cal.TodayCell)
				{
					cell.style.background  = "url('"+loadedImages[1].src+"')";
					cell.style.backgroundRepeat = "no-repeat"; 
					cell.innerHTML ="<a style='font-weight:bold; text-decoration:none;color:black;width:19;height:19;padding: 1.45px 2px 2px 2px;'>"+TodayDate+"</a>";
				}
				else
				{
					cell.style.background = "";
					cell.style.fontWeight ="normal";
				}
				el.style.background  = "url('"+loadedImages[2].src+"')";
				el.style.backgroundRepeat = "no-repeat";
				el.style.fontWeight ="normal";
				if(bToday == true)
				{
					if( PTCalendar.is_ie == false)
					{
						el.style.background  = "url('"+loadedImages[1].src+"')";
						el.style.textAlign  = 'left';
						el.style.backgroundRepeat = "no-repeat";
						if(el.calday<10)
							el.innerHTML ="<a style='font-weight:bold;background:url("+loadedImages[2].src+"); text-decoration:none;color:black;width:19;height:19;padding: 1.45px 7.5px 2px 2px;'>"+el.calday+"</a>";
						else
							el.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;'>"+el.calday+"</a>";
					}
					else
						el.innerHTML ="<a style='font-weight:bold;background:url("+loadedImages[1].src+"); repeat-x 100% 100%;text-align: center; text-decoration:none;color:black;width:19;height:19;'>"+el.calday+"</a>";
					
				}
				else
				{
					el.style.fontWeight ="Bold";
					el.style.background = "url('"+loadedImages[2].src+"')";
				}
            
                cal.CellSel = el;
				PTCalendar.removeClass(cal.currentDateEl, "PT_CALselected");
				PTCalendar.addClass(el, "PT_CALselected");
				closing = (cal.currentDateEl == el);
				if (!closing) {
					cal.currentDateEl = el;
				}
		   }
			if(cal.calType == "H")
			{
			cal.date = new Date(el.calYear,el.calMonth,el.calday);
			}
 
        cal.date.setDateOnly(el.calday,el.calMonth,el.calYear);
        cal.day = el.calday;
		cal.month = el.calMonth;
		cal.year = el.calYear;
		newdate =true;
		date = cal.date;
	}
	if (newdate) {
		cal.dateClicked=true;
		ev && cal.callHandler();
	}
	ev && cal.callCloseHandler();
};

// END: CALENDAR STATIC FUNCTIONS

// BEGIN: CALENDAR OBJECT FUNCTIONS
PTCalendar.prototype.create = function (_par) {
	var parent = null;
	if (! _par) {
		// default parent is the document body, in which case we create
		// a popup calendar.
		parent = document.getElementsByTagName("body")[0];
		this.isPopup = true;
	} else {
		parent = _par;
		this.isPopup = false;
	}

	var table1 = PTCalendar.createElement("table");
	this.Headertable = table1;
    table1.setAttribute("id","tailCalendar");       //ICE 1904588000
	table1.cellSpacing = 0;
	table1.cellPadding = 0;
	table1.style.fontSize = "11px";
	table1.PTCalendar = this;

	var thead1 = PTCalendar.createElement("thead", table1);
	var cell = null;
	var row = null;
	row = PTCalendar.createElement("tr", thead1);
	row.style.backgroundColor =  "white";		

	cell = PTCalendar.createElement("td", row);
	var spVar="";
/*	for(i=0;i<14;i++)
	  spVar+="&nbsp;";*/
	for(i=0;i<16;i++)
	  spVar+="&nbsp;";
    cell.innerHTML = spVar; 
	cell = PTCalendar.createElement("td", row);
	//cell.classname = "PT_CALlbutton";
	cell.style.textAlign  = 'left';
	cell.style.width = "4px";
	cell.style.height = "2px";

	cell.PTCalendar = cal;
	PTCalendar.addEvent(cell, "click", PTCalendar.previousMonth);
	var prevMonth = PTCalendar.createElement("img");
	prevMonth.src = loadedImages[3].src;
	prevMonth.alt = 'Previous Month';
	prevMonth.title= prevMonth.alt;
	cell.appendChild(prevMonth);
	var LeftSpaceCell = PTCalendar.createElement("td", row);
	LeftSpaceCell.innerHTML = "&nbsp;&nbsp;";
	cell = PTCalendar.createElement("td", row);
	var cellLink = PTCalendar.createElement("td", row);
	cellLink.align = "center";
	cellLink.valign = "Top";
	var HyperElem = document.createElement("a"); 
	HyperElem.setAttribute("href",'Javascript:PTCalendar.SelectToday()');
	var strCurrentDate = 'Current Date';
	HyperElem.appendChild(document.createTextNode(strCurrentDate));
	cellLink.appendChild(HyperElem);
	var RightSpaceCell = PTCalendar.createElement("td", row);
	RightSpaceCell.innerHTML = "&nbsp;&nbsp;";

	cell = PTCalendar.createElement("td", row);
	var NextMonth = PTCalendar.createElement("img");
	NextMonth.src = loadedImages[4].src;
	NextMonth.alt = 'Next Month';
	NextMonth.title = NextMonth.alt;
	cell.appendChild(NextMonth);
	cell.calendar = cal;
	PTCalendar.addEvent(cell, "click", PTCalendar.nextMonth);
	cell = PTCalendar.createElement("tr", row);
	for (var i = 5; i > 0; --i) {
	row2 = PTCalendar.createElement("tr", row);}

	var table = PTCalendar.createElement("table");
	this.table = table;
    table.setAttribute("id","bodyCalendar");        //ICE 1904588000
	table.cellSpacing = 0;
	table.cellPadding = 0;
	table.style.fontSize = "11px";
	table.PTCalendar = this;
	table.dir = 'ltr';
	PTCalendar.addEvent(table, "mousedown", PTCalendar.tableMouseDown);
	PTCalendar.addEvent(table1, "mousedown", PTCalendar.tableMouseDown);

	var div = PTCalendar.createElement("div");
	this.element = div;
    div.setAttribute("id","fullCalendar");      //ICE 1904588000
	//div.className = "PT_CAL";
	div.style.borderTop = "2px";
	div.style.borderBottom = "2px";
	div.style.borderLeft = "2px";
	div.style.borderRight = "2px";
	div.style.borderStyle ="solid";
	div.style.borderColor ="#797979";
	div.style.backgroundColor =  "white";
	div.style.position="absolute";
	

	if (this.isPopup) {
		div.style.position = "absolute";
		div.style.display = "none";
	}
	

    var table2 = PTCalendar.createElement("table");
    table2.setAttribute("id","headCalendar");       //ICE 1904588000
	table2.cellSpacing = 0;
	table2.cellPadding = 0;
	table2.style.fontSize = "11px";
	table2.dir = 'ltr';
	var thead2 = PTCalendar.createElement("thead", table2);
	var cell2 = null;
	
	var row = null;
	row = PTCalendar.createElement("tr", thead2);
	row.style.backgroundColor =  "white";		
	var strHeader;
	if(table2.dir == 'ltr')
	{
		if(this.calType == "H")
			strHeader="<map name=map1ltr><area shape='rect' coords='190,1,210,18' onclick='PTCalendar.closeWindow();' alt='Close' title='Close'/>"+"</map>" +  "<img usemap='#map1ltr' src="+loadedImages[0].src+" height='17' width='210' border='0'>";
		else
			strHeader="<map name=map1ltr><area shape='rect' coords='195,1,205,18' onclick='PTCalendar.closeWindow();' alt='Close' title='Close'/>"+"</map>" +  "<img usemap='#map1ltr' src="+loadedImages[0].src+" height='17' width='215' border='0'>";
	}
	else
	{
		if(this.calType == "H")
			strHeader="<map name=map1ltr><area shape='rect' coords='10,1,20,18' onclick='PTCalendar.closeWindow();' alt='Close' title='Close'/>"+"</map>" +  "<img usemap='#map1ltr' src="+loadedImages[0].src+" height='17' width='210' border='0'>";
		else
			strHeader="<map name=map1ltr><area shape='rect' coords='10,1,20,18' onclick='PTCalendar.closeWindow();' alt='Close' title='Close'/>"+"</map>" +  "<img usemap='#map1ltr' src="+loadedImages[0].src+" height='17' width='215' border='0'>";
		
	}
	cell = PTCalendar.createElement("td", row);
    cell.innerHTML = strHeader; 
    cell.id = "CalCloseHeader";     //ICE 1904588000

	div.appendChild(table2);	
	div.appendChild(table);
	div.appendChild(table1);
	
	
	var thead = PTCalendar.createElement("thead", table);
	var cell = null;
	var row = null;
	var cal = this;
	var CreateCell = function (text, cs, navtype) {
	    cell = PTCalendar.createElement("td", row);
		cell.colSpan = cs;
		cell.PTCalendar = cal;
		cell.navtype = navtype;
		var bCreateButton = 0;
        var src;	
		//	cell.className = "PT_CALbutton";
		cell.style.textAlign  = 'center';
		cell.style.width = "4px";
		cell.style.height = "2px";
		cell.style.fontWeight = 'bold'; 
		/*if (navtype != 0 && Math.abs(navtype) <= 2)
			cell.className += " nav";*/
	
        if (text=="WeekHeader")
		{
		    var ImageIdx = 6+cal.firstDayOfWeek;
		    src = loadedImages[ImageIdx].src;
			bCreateButton = 1;
		}
		if(bCreateButton == 1  )
		{
			
			var obj = PTCalendar.createElement("img", row);
			obj.src = src;
			text = '';
			cell.appendChild(obj);
			cell.navtype = navtype;
		
		}
		else if (bCreateButton == 0  )
		{
		
			cell.innerHTML = "<div unselectable='on'>" + text + "</div>";
		}

		return cell;
	};
	
	row = PTCalendar.createElement("tr", thead2);
	for (var i = 5; i > 0; --i) {
		row2 = PTCalendar.createElement("tr", thead2);
	}

	row = PTCalendar.createElement("tr", thead2);
	var title_length = 15;
	(this.isPopup) && --title_length;
	this.weekNumbers = true;
	(this.weekNumbers) && ++title_length;

	this.title = CreateCell("Title", title_length, 400);
	//this.title.className = "PT_CALtitle";
	this.title.style.fontWeight = 'bold';
	this.title.style.textAlign  = 'center';
	this.title.style.padding = "4px";

	row = PTCalendar.createElement("tr", thead);
	
	for (var i = 5; i > 0; --i) {
		row2 = PTCalendar.createElement("tr", thead);
	}

	row2 = PTCalendar.createElement("tr", thead);
	row = PTCalendar.createElement("tr", thead);

	if (this.weekNumbers) {
		cell = PTCalendar.createElement("td", row);
		//cell.className = "PT_CALname wn";
		cell.style.textAlign  = 'center';
		cell.style.padding = "1px";
		cell.style.color = '#fff'; 

	}

	this.firstdayname = (this.weekNumbers) ? row.firstChild.nextSibling : row.firstChild;

	row = PTCalendar.createElement("tr", thead);

	CreateCell("",1,1);
    //CreateCell("WeekHeader", 10, 1);

    //create week header
    var daystitle = new Array();
    daystitle[0] = "S";
    daystitle[1] = "M";
    daystitle[2] = "T";
    daystitle[3] = "W";
    daystitle[4] = "T";
    daystitle[5] = "F";
    daystitle[6] = "S";
    
    var daystitle_idx = -1 + cal.firstDayOfWeek;

    for (var i=0; i<7; i++) {
        daystitle_idx = daystitle_idx + 1;
    if (daystitle_idx> 6 ) {
        daystitle_idx = -1;
    daystitle_idx = daystitle_idx + 1;  }
    
    cell = PTCalendar.createElement("td", row);   
	cell.style.textAlign  = 'center';
    cell.style.padding = "1px";   
    cell.bgColor ="#A0CFEC"; 
    cell.innerHTML = daystitle[daystitle_idx] ;
    }
    //end create week header

    for (var i = 5; i > 0; --i)
		row2 = PTCalendar.createElement("tr", thead);

	var tbody = PTCalendar.createElement("tbody", table);
	this.tbody = tbody;

	for (i = 6; i > 0; --i) {
		row = PTCalendar.createElement("tr", tbody);
		if (this.weekNumbers) {
			cell = PTCalendar.createElement("td", row);
		}
		var noDays = 7;
		for (var j = noDays; j > 0; --j) {
			cell = PTCalendar.createElement("td", row);
			cell.PTCalendar = this;
			PTCalendar._add_evs(cell);
			}
	}

	row = PTCalendar.createElement("tr", tbody);

	var tfoot = PTCalendar.createElement("tfoot", table);
	row = PTCalendar.createElement("tr", tfoot);
	for (i = 6; i > 0; --i) {
	row2 = PTCalendar.createElement("tr", tfoot);
	}
	parent.appendChild(this.element);
};


function prevMonth(step) {
	NavigateMonth(-1,step);
};

function nextMonth(step) {
	NavigateMonth(1,step);
};

function NavigateMonth(monDir,step)
{
	var cal = window._PS_popupCalendar;
	cal.TodayCell = null;
	cal.dateClicked = false;
	var objCnt = document.getElementsByTagName("select").length;
	var objMonth =null;
	var objYear =null;
	for (var i = 0; i <objCnt;i++) {
	var objSelect = document.getElementsByTagName("select")[i];
	if(objSelect != null)
		{
		if(objSelect.name == 'PTMonth')
			objMonth = objSelect;
		else if(objSelect.name == 'PTYear')
			objYear = objSelect;
		}
		if(objMonth != null && objYear != null) break;

	}
	var date;
	if(cal.calType == "H")
		date = new window.HijriDate(cal.year,cal.month,cal.day);
	else
		date = new Date(cal.date);
	if(monDir==-1)
	{
		if(objMonth != null)
		{
			if(objMonth.selectedIndex>0)
				objMonth.selectedIndex = objMonth.selectedIndex-1;
			else
			{
				objMonth.selectedIndex = 11;
				if(objYear != null)
				objYear.selectedIndex = objYear.selectedIndex-1;
			}
		}

		if(step == 0)
		{
			var mon;
			if(cal.calType == "H")
				mon= date.month;
			else
				mon= date.getMonth();
			if(mon == 0)
			{
				mon =11;
				selectMonth(mon,-1);
			}
			else
				selectMonth(mon - 1);
		}
		else
		{
		   var yr,mn,day;
			if(cal.calType =="H")
			{
				date.day=cal.day;
				date.month = cal.month;
				date.year = cal.year;
				date.adjustDay(-step);
				cal.day = date.day;
				cal.month = cal.calmonth = date.month;
				cal.year = cal.calYear = date.year;
				 
			}
			else
			{
				date = cal.date;
				date.setDate(date.getDate() - step);
				day = date.getDate();
				mn = date.getMonth();
				yr = date.getFullYear();
			}
			cal.setDate(date);
		}
	}
	else
	{
		if(objMonth != null)
		{
			if(objMonth.selectedIndex<11)
				objMonth.selectedIndex = objMonth.selectedIndex+1;
			else
			{
				objMonth.selectedIndex = 0;
				if(objYear != null)
				objYear.selectedIndex = objYear.selectedIndex+1;
			}
		}
		if(step == 0)
		{
			var mon;
			if(cal.calType == "H")
				mon= date.month;
			else
				mon= date.getMonth();
			if(mon == 11)
			{
				mon =0;
				selectMonth(mon,1);
			}
			else
				selectMonth(mon+1);
		}
		else
		{
			if(cal.calType =="H")
			{
				date.day=cal.day;
				date.month = cal.month;
				date.year = cal.year;
				date.adjustDay(step);
				cal.day = date.day;
				cal.month = cal.calmonth = date.month;
				cal.year = cal.calYear = date.year;
			}
			else
			{
                date = cal.date;
				date.setDate(date.getDate() + step);
				cal.calmonth = date.getMonth();
				cal.calYear = date.getFullYear();
			}
			cal.setDate(date);
		}

	}
}


function selectMonth(Month,year) {
	var cal = window._PS_popupCalendar;
	if(cal.calType == "H")
	{
		var date = new window.HijriDate(cal.year,cal.month,cal.day);
		date.day = cal.day;
		var day = date.day;
		var max = date.daysOfMonth();
		if (day > max) {
			date.day=max;
		}
		date.month = Month;
		if(year == -1)
		{
			var yr=date.year-1;
			date.year =yr;
		}
		else if(year == 1)
		{
			var yr=date.year+1;
			date.year =yr;
		}
		cal.setDate(date);

	}
	else
	{
		var date = new Date(cal.date);
		var day = date.getDate();
		var max = date.getMonthDays(Month);
		if (day > max) {
			date.setDate(max);
		}
		date.setMonth(Month);
		var yr=date.getFullYear();
		if(year == -1)
			yr--;
		else if(year == 1)
			yr++;
		date.PT_setFullYear(yr);
		cal.setDate(date);
	}
	cal.dateClicked=false;
	cal.callHandler();
}
