function PiesoftCal(y,m,d){
	this.build	= _buildcal
	this.strDays = new Array()	
	this.strMonth	= new Array()
	this.Note = new Array()
	this.inote = 0
	this.SelectDate = _onSelectDate
	this.init =	initCal
	this.init()

	var HTML = ""
	this.calDate = new Date(y,m-1,d)
	this.updateCalData = updateCalData
	this.updateCalData()


	this.refresh_cal = refresh_cal
	this.addEvent	 = _addEvent
	this.showpicker = _showPicker

//// cal properties 
	this.SelectColor = "red"
	this.SelectFont  = "Ms Sans Serif"
	this.SelectWeight  = "bold"

	this.cellColor	= "green"
	this.bgColor = "white"

	this.tmpColor = ""

	this.HasTitle = false
	this.IsPicker = true

	this.upimg = "bd.gif"
	this.downimg = "bd.gif"
 	this.YearStyle = ""
	this.TitleStyle = ""
	
	this.obj_onMouseOver = obj_onMouseOver
}

function initCal()
{
	this.strDays[0]="Sun"
	this.strDays[1]="Mon"
	this.strDays[2]="Tue"
	this.strDays[3]="Wed"
	this.strDays[4]="Thu"
	this.strDays[5]="Fri"
	this.strDays[6]="Sat"
	
	this.strMonth[0] = "January"
	this.strMonth[1] = "February"
	this.strMonth[2] = "March"
	this.strMonth[3] = "April"
	this.strMonth[4] = "May"
	this.strMonth[5] = "June"
	this.strMonth[6] = "July"
	this.strMonth[7] = "August"
	this.strMonth[8] = "September"
	this.strMonth[9] = "October"
	this.strMonth[10] = "November"
	this.strMonth[11] = "December"
	
}

function updateCalData(){
	this.Date = this.calDate.getDate() 
	this.Month = this.calDate.getMonth()//(this.calDate.getMonth()>11)?this.calDate.getMonth()%11:this.calDate.getMonth()
//	alert(this.Month)
	this.Year = this.calDate.getFullYear() 
	
	this.Day = this.calDate.getDay()
	this.strDay = this.strDays[this.Day]
	this.firstDay = getFirstDay(this.Month,this.Year)
	this.leafDay = getLeafDate(this.Month,this.Year)
}

function _onSelectDate(iDate){
	//alert(this.Month)
	//alert(this.Year)
	//alert(iDate)
	this.calDate = new Date(this.Year,this.Month,iDate)
	this.updateCalData()
	this.refresh_cal()
	if(this.IsPicker){
 		if(this.SelDate>0 && this.SelDate<=mycal.leafDay){
  			this.calObj.style.display='none'
  		}
 	}

}

function _addEvent(oDate,StrNote)
{
	this.Note[this.inote] = StrNote
	this.inote++
}

function getLeafDate(iMonth, iYear) {
var dPrevDate = new Date(iYear, iMonth+1, 0);
	return dPrevDate.getDate();
}

function getFirstDay(iMonth, iYear) {
var dPrevDate = new Date(iYear, iMonth, 1);
	return dPrevDate.getDay();
}

function getDay(iDate,iMonth, iYear ) {
var dPrevDate = new Date(iYear, iMonth,iDate);
	return dPrevDate.getDay();
}

function _showPicker()
{

}

function _buildcal(){	
	instanName = this.InstanceName
	this.leafDay = getLeafDate(this.Month,this.Year)
	this.firstDay = getFirstDay(this.Month,this.Year)
	
	HTML="";
    if(this.IsPicker){
//		HTML+="<input type=text onfocus='"+instanName+".picker=this;"+dCal+".style.display=\"\";"+dCal+".style.left=this.offsetLeft;"+dCal+".style.top=this.offsetTop+this.offsetHeight;' "
//		HTML+=" onblur='"+dCal+".focus()'"	
//		HTML+=">"	
	}

	if(this.IsPicker){		
//		HTML+="<DIV  style= 'position:absolute;width:1;'onfocus='this.backgroundColor= \"red\"'onblur='this.style.display=\"none\"' >"	
	}
	
	HTML+= "<TABLE id=\"caltb\" cellSpacing=1 cellpadding=2 border=0 bgcolor=\""+this.bgColor +"\" style=\""+this.TBStyle+"\""
	
	if(this.IsPicker)
	{
	//	HTML+= "onmouseout='"+instanName+".calObj.style.display=\"none\"'>"	
	
	}else
	{
	//	HTML+=">"
	}
	HTML+=">"

	if(this.HasTitle)
	{
		HTML+="<TR id=caltitle>"
		HTML+="<TD align="+"center"+" colSPan=7>"
		HTML+=this.strMonth[this.Month]
	HTML+="<SELECT>"
	for(var m = 0 ;m <12 ; m ++){
		HTML+="<OPTION>"
		HTML+=m
		HTML+="</OPTION>"
	}
		
	HTML+="</SELECT>"	
		
		HTML+="&nbsp;"		
		HTML+=this.Year		
		HTML+="</TD>"
		HTML+="</TR>"
		
	}
	

	
	HTML+="<TR style='"+this.headerStyle+"'>"
	for(var i = 0; i<this.strDays.length; i++){
		HTML+="<TD width='"+this.cellWidth+"'>"
		HTML+= this.strDays[i]
		HTML+="</TD>"
	}
	HTML+="</TR>"



	writedate = false;
	intDate = 0
	i = 1
	x = 0
	while(true){
		if(i>this.firstDay){
			writedate=true;
			intDate=(intDate==0)?1:intDate;
		}
		else
			x++
		if(i>this.leafDay+x)
			writedate=false
		if( (i % 7) == 1){	
			HTML+="<TR id=rowdate>" 
			n = 0
		}			
		n++
		HTML+="<TD date="+intDate+" align=\"center\" style=\"cursor:hand;"
		
		if(this.Date==intDate)
			HTML+="background-color:"+this.SelectColor+";"
		else
			HTML+="background-color:"+this.cellColor+";"

		HTML+= "\"";
		HTML+="onClick=\""+instanName+".SelectDate(this.date);"+instanName+"_onSelectDate("+instanName+")\" "
		
		HTML+=">"
		if(writedate){
					
			HTML+=intDate
		
		}else{
			HTML+="&nbsp;"
		}
		HTML+="</TD>"
		
		if(intDate>0)
			intDate++
		
		if( (i % 7) == 0  ){
			 HTML+="</TR>" 
		}			
		i++
		if(i>this.leafDay+x && n==7)
			break
	}
	HTML+="</TABLE>"
	
	if(this.IsPicker){
//		HTML+="</DIV>"	
	}
	
	document.write(HTML)	
	if(typeof(caltb)=='undefined'){
		this.calObj = null
	}else{
		if(typeof(caltb.length)=='undefined'){
			this.calObj = caltb
		}else{
			this.calObj = caltb[caltb.length-1]
		}
	}
	if(this.IsPicker){
//		this.calObj.parentElement.style.display="none"		
	}
}

function refresh_cal(){

	debug ="Date="+this.Date + "\n"
	debug+="Month = "+(this.Month) +"\n"
	debug+="Year = "+this.Year  +"\n"
	debug+="First Day = "+(this.firstDay+1)  +"\n"
	debug+="Leaf Day="+this.leafDay + "\n"
//	alert(debug)

    idate = 0
    rowstart =0
	for(var i = 0;i<this.calObj.rows.length;i++)
	{		  
		  if(this.calObj.rows(i).id=="caltitle")
		  {
			 r = this.calObj.rows(i)
			 

				HTML ="<table cellSpacing=0 cellPadding=0 width=100% border=0><tr><td width=100% align=center style='"+this.TitleStyle+"'>"+this.strDays[this.Day]+"&nbsp;"+this.Date+"</td>"
				HTML+="<td><SELECT style=\"border:solid 1px black\" onChange=\"onSelectMonth(this,"+this.InstanceName+")\">"
				for(var m = 0 ;m <12 ; m ++){
					HTML+="<OPTION "
					HTML+="value="+m+" "
					if(m==this.Month){
						HTML+="SELECTED"
					}
					HTML+=" style=\"border:solid 1px black;\">"
					HTML+=this.strMonth[m]
					HTML+="</OPTION>"
				}
					
				HTML+="</SELECT>"	
					
					
				HTML+="&nbsp;"	
				HTML+="</td><td>"		
				HTML+="<table cellspacing=0 cellPadding=0 border=0 style='font-size:1px;'>"
				HTML+="<tr><td rowspan=2>"
				HTML+="<input name=\"tbSelYear\" value='"+this.Year+"' size=5 style='"+this.YearStyle+"' "
				HTML+="onblur=\"\"></td>"
				HTML+="<td valign=center style='cursor:hand;font-family:Webdings;font-size:1px;'"

				HTML+="onClick=\"this.parentElement.parentElement.childNodes.item(0).childNodes.item(0).value=parseInt(this.parentElement.childNodes.item(0).childNodes.item(0).value)+1;onSelectYear(this.parentElement.parentElement.childNodes.item(0).childNodes.item(0),"+this.InstanceName+")\" "
				
				HTML+=">&nbsp;<img src='"+this.upimg+"'  >"
				HTML+="</tr>"
				HTML+="<tr><td valign=center style='cursor:hand;font-family:Webdings;font-size:1px;'"

				HTML+="onClick=\"this.parentElement.parentElement.childNodes.item(0).childNodes.item(0).value=parseInt(this.parentElement.parentElement.childNodes.item(0).childNodes.item(0).childNodes.item(0).value)-1;onSelectYear(this.parentElement.parentElement.parentElement.childNodes.item(0).childNodes.item(0).childNodes.item(0),"+this.InstanceName+")\"	title=\"Previous Year\" value=\"x\" style=\"font-size:3px;\" "
				
				HTML+=">&nbsp;<img src='"+this.downimg+"' >"
				HTML+="</tr>"
				HTML+="</table>"
				HTML+="</td>"				
				
				HTML+="</tr></table>"
				
				r.childNodes.item(0).align = "right"
				r.childNodes.item(0).innerHTML =	HTML	
				//tb.childNodes 
		  }
		  if(this.calObj.rows(i).id=="rowdate")
		  {	
			 rowstart = i	  
			this.calObj.deleteRow(i--)
		  }
	}
	t = this.calObj
	i = 0
	iDate = 0
	DayOfWeek = 0
	nRow = 0
	while(iDate<=this.leafDay){
		DayOfWeek = (i%7)
		//alert(DayOfWeek)
		if(DayOfWeek==this.firstDay){
			iDate=(iDate==0)?1:iDate;
		}
		if(DayOfWeek==0)
		{
			row = t.insertRow()
			row.id="rowdate"
			for(var j=0;j<7;j++){
				oCell = row.insertCell()
				oCell.innerHTML="&nbsp;"
			}
			nRow++
		}
		if(iDate>0) iDate++
		i++
	}
	//alert(rowstart)
	for(var i=rowstart;i<nRow+rowstart;i++){
		for(var j=0;j<7;j++){
			x = (j+1)-7 +(7-this.firstDay ) +((i-2)*7)
			if(x > this.leafDay){
				cDate= x % this.leafDay
				t.rows(i).cells(j).style.backgroundColor="cacaca"
			}else if( x < 1 ){
				lastLeaf = getLeafDate(this.Month-1, this.Year)
				cDate = lastLeaf+x
				t.rows(i).cells(j).style.backgroundColor="cacaca"
			}else{
				if(x == this.Date){
					t.rows(i).cells(j).style.backgroundColor=this.SelectColor
				}
				cDate = x
			}			
			t.rows(i).cells(j).style.cursor = "hand"
			t.rows(i).cells(j).date = x
			t.rows(i).cells(j).innerHTML = cDate
			t.rows(i).cells(j).width = this.cellWidth
			t.rows(i).cells(j).style.fontFamily = this.cellFont
			t.rows(i).cells(j).style.textAlign = "center"
			t.rows(i).cells(j).onclick = this.onClick
			
			//this.SelDate = x
			t.rows(i).cells(j).onmouseover = this.obj_onMouseOver
		
		}
	}
	this.SelectColor = (this.tmpColor!="")?this.tmpColor:this.SelectColor;
	this.calObj.style.display=(this.IsPicker)?'':'none';
//	this.calObj.style.border="solid 1px black"
//	this.calObj.style.fontFamily = "Tahoma"
//	this.calObj.style.fontSize = 14 
//	this.calObj.border="0"

}

function obj_onMouseOver(){
	e = event
	this.SelCell = e.srcElement
	this.SelDate = e.srcElement.date
}

function onSelectMonth(oSel,obj){
	obj.tmpColor = obj.SelectColor
	if(oSel.value!=obj.Month)
		obj.SelectColor = ""
	obj.Month = oSel.value	
	obj.SelectDate( obj.Date)
}

function onSelectYear(oSel,obj){

	obj.tmpColor = obj.SelectColor
	if(obj.Year!= oSel.value)
		obj.SelectColor = ""

	obj.Year= oSel.value
	obj.SelectDate( obj.Date)
}


//function date----------start
function toggleCal()
{
	objActive=frmConsole.input_date1;
	cal1.style.display=(cal1.style.display=='none')?'':'none';
	this.src=(this.src=='image/new-51.gif')?'image/new-51.gif':'image/new-61.gif'
	
}

function toggleCal2()
{
	objActive=frmConsole.input_date2;
	cal2.style.display=(cal2.style.display=='none')?'':'none';
	this.src=(this.src=='image/new-51.gif')?'image/new-51.gif':'image/new-61.gif'	
}

function toggleCal3()
{
	objActive=frmConsole.input_date3;
	cal3.style.display=(cal3.style.display=='none')?'':'none';
	this.src=(this.src=='image/new-51.gif')?'image/new-51.gif':'image/new-61.gif'	
}

function toggleCal4()
{
	objActive=frmConsole.input_date4;
	cal4.style.display=(cal4.style.display=='none')?'':'none';
	this.src=(this.src=='image/new-51.gif')?'image/new-51.gif':'image/new-61.gif'	
}
	function toggleCal5()
{
	objActive=frmConsole.input_date5;
	cal5.style.display=(cal5.style.display=='none')?'':'none';
	this.src=(this.src=='image/new-51.gif')?'image/new-51.gif':'image/new-61.gif'	
}

function myCal_onClick(){
	 mycal.SelectDate(this.SelDate)
	 if(this.SelDate>0 && this.SelDate<=mycal.leafDay){
		cal1.style.display='none'
	 }
	setSessionDate(frmConsole.Day1,frmConsole.Month1,frmConsole.Year1,mycal)
	c = mycal.calDate
	objActive=frmConsole.input_date1;
	objActive.value= mycal.strDays[c.getDay()] +" "+ c.getDate() + " " + mycal.strMonth[c.getMonth()] + " " + c.getFullYear()
}

function myCal2_onClick(){
	 mycal2.SelectDate(this.SelDate)
	 if(this.SelDate>0 && this.SelDate<=mycal2.leafDay){
		cal2.style.display='none'
	 }
	setSessionDate(frmConsole.Day2,frmConsole.Month2,frmConsole.Year2,mycal2)
	c = mycal2.calDate
	objActive=frmConsole.input_date2;
	objActive.value= mycal2.strDays[c.getDay()] +" "+ c.getDate() + " " + mycal2.strMonth[c.getMonth()] + " " + c.getFullYear()
}

function myCal3_onClick(){
	 mycal3.SelectDate(this.SelDate)
	 if(this.SelDate>0 && this.SelDate<=mycal3.leafDay){
		cal3.style.display='none'
	 }
	setSessionDate(frmConsole.Day3,frmConsole.Month3,frmConsole.Year3,mycal3)
	c = mycal3.calDate
	objActive=frmConsole.input_date3;
	objActive.value= mycal3.strDays[c.getDay()] +" "+ c.getDate() + " " + mycal3.strMonth[c.getMonth()] + " " + c.getFullYear()
}

function myCal4_onClick(){	
	 mycal4.SelectDate(this.SelDate)
	 if(this.SelDate>0 && this.SelDate<=mycal4.leafDay){
		cal4.style.display='none'
	 }
	setSessionDate(frmConsole.Day4,frmConsole.Month4,frmConsole.Year4,mycal4)
	c = mycal4.calDate
	objActive=frmConsole.input_date4;
	//objActive2=frmConsole.input_date5;
	objActive.value= mycal4.strDays[c.getDay()] +" "+ c.getDate() + " " + mycal4.strMonth[c.getMonth()] + " " + c.getFullYear();
	//objActive2.value= mycal4.strDays[c.getDay()] +" "+ c.getDate() + " " + mycal4.strMonth[c.getMonth()] + " " + (c.getFullYear()+1);
	frmConsole.input_date5.value= c.getDate() + " " + mycal4.strMonth[c.getMonth()] + " " + (c.getFullYear()+1);
	frmConsole.Year5.value=(c.getFullYear()+1);
	frmConsole.Month5.value=mycal4.strMonth[c.getMonth()];
	frmConsole.Day5.value=c.getDate();
}

function myCal5_onClick(){
	 mycal5.SelectDate(this.SelDate)
	 if(this.SelDate>0 && this.SelDate<=mycal5.leafDay){
		cal5.style.display='none'
	 }
	setSessionDate(frmConsole.Day5,frmConsole.Month5,frmConsole.Year5,mycal5)
	c = mycal5.calDate
	objActive=frmConsole.input_date5;
	objActive.value= mycal5.strDays[c.getDay()] +" "+ c.getDate() + " " + mycal5.strMonth[c.getMonth()] + " " + c.getFullYear()
}

function setSessionDate(d,m,y,cal){
	d.value = cal.Date
	m.value = 1+cal.Month
	y.value = cal.Year
}
//function date----------end
