﻿/*****isnis_calendar.js*****/
var currentOpenCalendar;
var currentIsOpen = 0;

function HideCalendar()
{
	if(currentIsOpen == 1)
	{
		currentOpenCalendar.style.display = "none";
		divOverSelect(true);
		currentOpenCalendar = null;
		document.onclick=null;
		defaultCoordinate.Remove();
	}
	currentIsOpen++;
	return true;
 }

function divOverSelect(Visible){
	if (eval(currentOpenCalendar.id.replace("_win","_obj")).HiddenObject==false)
		return;
	with (document.forms[0])
	{
		for (var i=0;i<length;i++)
		{
			if (item(i).type.indexOf("select")!=-1)
			{
				if (item(i).style.visibility=="" || item(i).style.visibility=="visible")
					item(i).style.visibility="hidden";
				else
					item(i).style.visibility="visible";
			}
		}
	}
}

function CalendarObject(ctrl,edit,initDate)
{
	this.owner = ctrl;
	this.editCtrl = edit;
	//mapping control from DOM to variable.
	this.yearCtrl = ctrl.children[0].children[0].children[0].children[0].children[0].children[0].children[1];
	this.monthCtrl = ctrl.children[0].children[0].children[0].children[0].children[0].children[0].children[5];
	this.headerFirst = ctrl.children[0].children[0].children[0].children[0].children[0].children[1];
	this.date_row1 = ctrl.children[0].children[0].children[0].children[0].children[0].children[2];
	this.date_row2 = ctrl.children[0].children[0].children[0].children[0].children[0].children[3];
	this.date_row3 = ctrl.children[0].children[0].children[0].children[0].children[0].children[4];
	this.date_row4 = ctrl.children[0].children[0].children[0].children[0].children[0].children[5];
	this.date_row5 = ctrl.children[0].children[0].children[0].children[0].children[0].children[6];
	this.date_row6 = ctrl.children[0].children[0].children[0].children[0].children[0].children[7];
	this.dateCtrls = new Array(32);
	this.currentDay;
	this.Today;			//The Server's Today
	this.DateFormat;	//The DateFormat(0 or other)
	this.Separator;
	if (initDate!="")
		this.currentDate = initDate;
	
	this.CoordinateNow = function()
	{
		currentIsOpen = 1;	
		HideCalendar();
	}
 
	 //Sync the edit control & calendar object
	this.UpdateData = function()
	{
			var baseDate = new Date(this.currentDate.getFullYear(),this.currentDate.getMonth(),this.currentDate.getDate());
			var ysel = baseDate.getFullYear();
			if (this.DateFormat=="0")
			{
				ysel-=1911;
				//ysel=this.leadingZero(ysel,3)
			}
			var msel = baseDate.getMonth()+1;
			msel=this.leadingZero(msel,2);
			var dsel = baseDate.getDate();
			dsel=this.leadingZero(dsel,2);
			if (this.Separator==false)
				this.editCtrl.value = ysel.toString()+msel.toString()+dsel.toString();
			else
				this.editCtrl.value = ysel+'/'+msel+'/'+dsel;
	}
 
 	this.leadingZero=function(vVal,iLen)
 	{
		var strVal="";
		strVal=vVal.toString();
		if (strVal.value=="")
			return "";
		var strLead="";
		for (i=strVal.length;i<iLen;i++)
			strLead=strLead+"0";
				
		strVal=strLead+strVal;
		return strVal;
 	}
 	
	//Count the max days of selected year-month
	this.getDayCount = function(Y,M)
	{
		var vMDate=new Date;
		var i=31,j,vMaxDate;
		//var Y=this.currentDate.getFullYear();
		//var M=this.currentDate.getMonth();
		vMDate.setFullYear(Y,M,i);
		j=vMDate.getDate();
		if (i==j)
			vMaxDate=i;
		else
			vMaxDate=i-j;
		return vMaxDate;
	}
 
	 //Set the selected day of calendar object
	this.SelectedDay = function(ctrl,flag)
	{
		if(ctrl.innerText == "")
			return;
		var dsel = parseInt(ctrl.innerText);
		this.currentDate.setDate(dsel);
		this.UpdateData();
		if(this.currentDay != null)
			this.currentDay.style.border ="none";
		this.currentDay = this.dateCtrls[dsel];
		this.dateCtrls[dsel].style.border = "1px solid #000099";
		if(flag)
			this.ShowCalendar();
	}

	//Draw Calendar
	this.RebuildCalendar = function()
	{
		var baseDate = new Date(this.currentDate.getFullYear(),this.currentDate.getMonth(),1);
		//var thisDate=this.currentDate.getDate();	//currentDate
		var dayOfWeek = baseDate.getDay();
		var dayOfMonth = this.getDayCount(this.currentDate.getFullYear(),this.currentDate.getMonth());
		var i,cPos=1,v;
		//clean non date cell.
		for(i = 0; i < dayOfWeek; i++)
			this.date_row1.children[i].innerText ='';
		for(i = 0; i < 7; i++)
		{
			this.date_row5.children[i].innerText ='';
			this.date_row6.children[i].innerText ='';
		}
		
		//fill date cell 1.
		for(i = dayOfWeek; i < 7; i++)
		{
			this.date_row1.children[i].innerText = cPos;
			this.dateCtrls[cPos] = this.date_row1.children[i];
			this.SetDateColor(i,this.dateCtrls[cPos]);
			cPos++;   
		}
		
		v=0;
		for(i = 0; i < 7; i++)
		{
			this.date_row2.children[v].innerText = cPos;
			this.dateCtrls[cPos] = this.date_row2.children[i];
			this.SetDateColor(i,this.dateCtrls[cPos]);
			cPos++;
			v++;
		}
		
		v=0;
		for(i = 0; i < 7; i++)
		{
			this.date_row3.children[v].innerText = cPos;
			this.dateCtrls[cPos] = this.date_row3.children[i];
			this.SetDateColor(i,this.dateCtrls[cPos]);
			cPos++;
			v++;
		}
	   
		v=0;
		for(i = 0; i < 7; i++)
		{
			if(cPos > dayOfMonth)
				return;
			this.date_row4.children[v].innerText = cPos;
			this.dateCtrls[cPos] = this.date_row4.children[i];
			this.SetDateColor(i,this.dateCtrls[cPos]);
			cPos++;
			v++;
		}
		
		v=0;
		for(i = 0; i < 7; i++)
		{
			if(cPos > dayOfMonth)
				return;
			this.date_row5.children[v].innerText = cPos;
			this.dateCtrls[cPos] = this.date_row5.children[i];
			this.SetDateColor(i,this.dateCtrls[cPos]);
			cPos++;
			v++;
		}
		
		v=0;
		for(i = 0; i < 7; i++)
		{
			if(cPos > dayOfMonth)
				return;
			this.date_row6.children[v].innerText = cPos;
			this.dateCtrls[cPos] = this.date_row6.children[i];
			this.SetDateColor(i,this.dateCtrls[cPos]);
			cPos++;
			v++;
		}
	}

	//Set the date's fore color
	this.SetDateColor=function (dayOfWeek,dCtrl)
	{
		var foreColor="black";
		switch (dayOfWeek){
			case 0:
				foreColor="red";
				break;
			case 6:
				foreColor="blue";
				break;
			default:
				break;
		}
		dCtrl.style.color=foreColor;
	}

	//Decide show calendar or not
	this.ShowCalendar = function()
	{
		if (this.owner.style.display == "none")
		{
			this.UpdateFromEdit();
			defaultCoordinate.Push(this);
			this.owner.style.display = "block";
			document.onclick=HideCalendar;
			currentOpenCalendar = this.owner;
			currentIsOpen = 0;
			divOverSelect(false);
		}
		else
		{
			this.owner.style.display = "none";
			document.onclick=null;
			currentIsOpen = 0;
			defaultCoordinate.Remove();
			divOverSelect(true);
		}
	}

	//The Next Year
	this.NextYear = function()
	{
		var currentYear = parseInt(this.yearCtrl.innerText);
		this.yearCtrl.innerText = currentYear+1;
		if (this.DateFormat=="0")
			currentYear+=1911;
		this.currentDate.setYear(currentYear+1);
		this.RebuildCalendar();
		currentIsOpen = 0;
		//this.SelectedDay(this.dateCtrls[this.currentDate.getDate()],false);
	}

	//The Previous Year
	this.PreviousYear = function()
	{
		var currentYear = parseInt(this.yearCtrl.innerText);
		this.yearCtrl.innerText = currentYear-1;
		if (this.DateFormat=="0")
			currentYear+=1911;
		this.currentDate.setYear(currentYear-1);
		this.RebuildCalendar();
		currentIsOpen = 0;
		//this.SelectedDay(this.dateCtrls[this.currentDate.getDate()],false);
	}

	// The Next Month
	this.NextMonth = function()
	{
		var currentMonth = this.currentDate.getMonth();
		if (currentMonth>=11){
			currentMonth=-1;
			var nextYear=this.currentDate.getFullYear()+1;
			this.currentDate.setYear(nextYear);
			if (this.DateFormat=="0")
				nextYear-=1911;
			this.yearCtrl.innerText=nextYear;
		}
		this.monthCtrl.innerText = currentMonth+2;

		this.currentDate.setMonth(currentMonth+1);

		this.RebuildCalendar();
		//this.currentDate.setDate(dsel);
		currentIsOpen = 0;
		//this.SelectedDay(this.dateCtrls[this.currentDate.getDate()],false);
	}

	//The Previous Month
	this.PreviousMonth = function()
	{
		var currentMonth = this.currentDate.getMonth();
			if (currentMonth==0){
				currentMonth=12;
				var previousYear=this.currentDate.getFullYear()-1;
				this.currentDate.setYear(previousYear);
				if (this.DateFormat=="0")
					previousYear-=1911;
				this.yearCtrl.innerText=previousYear;
			}
		this.monthCtrl.innerText = currentMonth;

		this.currentDate.setMonth(currentMonth-1);
		  
		this.RebuildCalendar();
		//this.currentDate.setDate(dsel);
		currentIsOpen = 0;
		//this.SelectedDay(this.dateCtrls[this.currentDate.getDate()],false);
	}

	//Parse String To Date Format
	this.ParseString = function(text)
	{
		var iyear,imonth,iday;
		var strDate=text;
		if (this.DateFormat==0)
		{
				var i=strDate.indexOf("/");
				if (i!=-1)
					strDate=Number(text.substring(0,i))+1911+text.substr(i);
				else
				{
					if (text!="")
						strDate=(Number(text)+19110000).toString();
				}
		}
		if (strDate=="")
		{
			strDate=new Date;
			if (this.Separator==false)
				strDate=strDate.getFullYear().toString()+this.leadingZero(strDate.getMonth()+1,2)+this.leadingZero(strDate.getDate(),2);
			else
				strDate=strDate.getFullYear().toString()+"/"+this.leadingZero((strDate.getMonth()+1),2)+"/"+this.leadingZero(strDate.getDate(),2);
		}
		
		if (this.Separator==false)
		{
			iyear=strDate.substr(0,strDate.length-4);
			imonth=Number(strDate.substr(strDate.length-4,2));
			iday=Number(strDate.substr(strDate.length-2,2));
			if(imonth < 0 || imonth > 12)
				return null;
			if (iday<1 || iday>this.getDayCount(iyear,imonth-1))
				return null;
		}
		else
		{
			var aryDate=strDate.split("/");
			if (aryDate.length==3)
			{
				iyear=parseInt(aryDate[0]);
				imonth=Number(aryDate[1]);
				if(imonth < 0 || imonth > 12)
					return null;
				iday=Number(aryDate[2]);
				if (iday<1 || iday>this.getDayCount(iyear,imonth-1))
					return null;
			}
			else
				return null;
		}
		
		if (isNaN(iyear) || isNaN(imonth) || isNaN(iday))
			return null;
			
		return new Date(iyear,imonth-1,iday);
	}

	//Set Today value From Server DateTime
	this.setToday=function (){
		var theDate="";
		if (this.DateFormat=="0")
		{
			var i=this.Today.indexOf("/");
			theDate=Number(this.Today.substring(0,i))-1911+this.Today.substr(i);
		}
		else
			theDate=this.Today;

		if (this.Separator==false)
			theDate=theDate.replace("/","").replace("/","");
			
		this.editCtrl.value=theDate;
		this.UpdateFromEdit();
	}

	// Sync calendar from textbox & check format
	this.UpdateFromEdit = function()
	{
		var	d = this.ParseString(this.editCtrl.value);
		if(d != null)
		{
			this.currentDate = new Date(d.getFullYear(),d.getMonth(),d.getDate());
			if (this.DateFormat=="0")
				this.yearCtrl.innerText = d.getFullYear()-1911;
			else
				this.yearCtrl.innerText = d.getFullYear();
			this.monthCtrl.innerText = d.getMonth()+1;
			this.RebuildCalendar();
			if (this.editCtrl.value!="")
			{
				this.SelectedDay(this.dateCtrls[d.getDate()]);
			}
			/*
			if(this.currentDay != null)
				this.currentDay.style.border ="none";
			this.currentDay = this.dateCtrls[d.getDate()];
			this.dateCtrls[d.getDate()].style.border = "1px solid #000099";
			*/
			return;
		}
			window.alert("日期格式輸入錯誤!");
			this.editCtrl.value="";
			this.editCtrl.focus();
	}
 
	//this.RebuildCalendar(); 
	//this.UpdateData();
	/*
	var dsel = this.currentDate.getDate();
	this.UpdateFromEdit();
	this.SelectedDay(this.dateCtrls[dsel],false);
	*/
}

/********************CoordinateObject********************/

function CoordinateObject()
{
	this.coordinateObjects = new Array();
	
	this.Push = function(srcObj)
	{  
		this.Pop();
		var l = this.coordinateObjects.length;
		this.coordinateObjects[l] = srcObj;
	}
	
	this.Pop = function()
	{
		//Coordinate-able object must provides a function named CoordinateNow()  
		if(this.coordinateObjects.length > 0)
		{
			this.coordinateObjects[0].CoordinateNow();       	
			var tempArray = this.coordinateObjects.slice(1,this.coordinateObjects.length);
			this.coordinateObjects[0] = null;
			this.coordinateObjects = tempArray;
		}
	}
	
	this.Remove = function()
	{
		if(this.coordinateObjects.length > 0)	
		{
			var tempArray = this.coordinateObjects.slice(1,this.coordinateObjects.length);
			this.coordinateObjects[0] = null;
			this.coordinateObjects = tempArray;
		}   
	}
}

var defaultCoordinate = new CoordinateObject();