/* Tong_Zuu */
/* ใช้แทน CscCalendar.js ไม่ได้ เพราะชื่อ function ไม่เหมือนกัน อาจจะทำให้ต้องแก้เยอะ*/
/* คำสั่งที่ใช้ได้
	obj.setCscCalendar(""); เซ็ตวันที่ปัจจุบัน หรือกำหนดวันที่ลงไปได้

	obj1.beforeDate(obj2.getTimeStamp());
	obj1.beforeDateTime(obj2.getTimeStamp());
	obj1.beforeEqualDate(obj2.getTimeStamp());
	obj1.beforeEqualDateTime(obj2.getTimeStamp());

	obj1.afterDate(obj2.getTimeStamp());
	obj1.afterDateTime(obj2.getTimeStamp());
	obj1.afterEqualDate(obj2.getTimeStamp());
	obj1.afterEqualDateTime(obj2.getTimeStamp());

	obj.incDate();
	obj.incNumDate(num);
	obj.incMonth();
	obj.incYear();
	obj.decDate();
	obj.decNumDate(num);
	obj.decMonth();
	obj.decYear();

	obj.setCscTime(hhmm); Data ที่ส่งมาต้องมองเป็น String เท่านั้น เช่น "7.20"
	obj.addTime(hhmm); Data ที่ส่งมาต้องมองเป็น String เท่านั้น เช่น "7.20"
	obj.subTime(hhmm); Data ที่ส่งมาต้องมองเป็น String เท่านั้น เช่น "7.20"
	obj.incHour();
	obj.addHour(hh);
	obj.incMin();
	obj.addMin(mm);
	obj.decHour();
	obj.subHour(hh);
	obj.decMin();
	obj.subMin(mm);
	obj1.subForDay(obj2.getTimeStamp());
	obj1.subForTime(obj2.getTimeStamp());

	obj.getDDMMYYYY();
	obj.getDDMMYYYYHHMMSS();
	obj.getYYYYMMDD();
	obj.getYYYYMMDDHHMMSS();
	obj.getTimeStamp();

	obj.setFormatDate("-"); dd-mm-yyyy
	obj.setFormatTime(":"); hh:mm:ss


*/

		var CscCalendar = function(){
			var dayOfTimeStamp = 86400000;
			var hourOfTimeStamp = dayOfTimeStamp/24;
			var minuteOfTimeStamp = hourOfTimeStamp/60;
			var secondOfTimeStamp = minuteOfTimeStamp/60;

			this.formatDate = "-";
			this.formatTime = ".";

			this.nDayOfMonth = new Array(0,31,0,31,30,31,30,31,31,30,31,30,31);

			this.__dd = "0";
			this.__tmpDD =0;
			this.__mm = "0";
			this.__tmpMM = 0;
			this.__yyyy = "0000";
			this.__tmpYYYY = 0;
			this.__hh = 0;
			this.__tmpHH = 0;
			this.__min = 0;
			this.__tmpMin = 0;
			this.__ss = 0;
			this.__tmpSS = 0;
			
			var nDayOfMon = 28;
			var nDayOfYear = 365;
			
			this.setCscCalendar = function(strCalendar){
				if(strCalendar.indexOf(this.formatDate) > -1 && strCalendar.length == 10){
					var tmp = strCalendar.split(this.formatDate);
					if(tmp[0].length == 2){
						this.nDayOfMonth[2] = 32 - new Date(tmp[2], 1, 32).getDate();
						nDayOfYear = 367 - new Date(parseInt(tmp[2],10), 12, 367).getDate();
						this.setDate(tmp[0]);
						this.setMonth(tmp[1]);
						this.setYear(tmp[2]);
					}else if(tmp[0].length == 4){
						this.nDayOfMonth[2] = 32 - new Date(tmp[0], 1, 32).getDate();
						nDayOfYear = 367 - new Date(parseInt(tmp[0],10), 12, 367).getDate();
						this.setDate(tmp[2]);
						this.setMonth(tmp[1]);
						this.setYear(tmp[0]);
					}else{
						this.nDayOfMonth[2] = 32 - new Date(1900, 1, 32).getDate();
						nDayOfYear = 367 - new Date(parseInt(1900,10), 12, 367).getDate();
						this.setDate(01);
						this.setMonth(01);
						this.setYear(1900);
					}
				}else if(strCalendar == "now" || strCalendar == ""){
					var ndate = new Date();
					this.nDayOfMonth[2] = 32 - new Date(ndate.getYear(), 1, 32).getDate();
					nDayOfYear = 367 - new Date(parseInt(ndate.getYear(),10), 12, 367).getDate();
					this.setDate(ndate.getDate());
					this.setMonth(ndate.getMonth()+1);
					this.setYear(ndate.getYear());
					this.setHour(ndate.getHours());
					this.setMin(ndate.getMinutes());
					this.setSec(ndate.getSeconds());
				}else{
					this.nDayOfMonth[2] = 32 - new Date(1900, 1, 32).getDate();
					nDayOfYear = 367 - new Date(parseInt(1900,10), 12, 367).getDate();
					this.setDate(01);
					this.setMonth(01);
					this.setYear(1900);
				}
			}

			this.getValueInt = function(){
				return this.__yyyy+this.__mm+this.__dd;
			}

			this.beforeDate = function(TimeStamp){
				if(this.getNumDate(this.getTimeStamp()) < this.getNumDate(TimeStamp))return true;
				return false;
			}

			this.beforeDateTime = function(TimeStamp){
				if(this.getTimeStamp() < TimeStamp)return true;
				return false;
			}
			
			this.beforeEqualDate = function(TimeStamp){
				if(this.getNumDate(this.getTimeStamp()) <= this.getNumDate(TimeStamp))return true;
				return false;
			}
			
			this.beforeEqualDateTime = function(TimeStamp){
				if(this.getTimeStamp() <= TimeStamp)return true;
				return false;
			}
			
			this.afterEqualDateTime = function(TimeStamp){
				if(this.getTimeStamp() >= TimeStamp)return true;
				return false;
			}
			
			this.afterEqualDate = function(TimeStamp){
				if(this.getNumDate(this.getTimeStamp()) >= this.getNumDate(TimeStamp))return true;
				return false;
			}

			this.afterDate = function(TimeStamp){
				if(this.getNumDate(this.getTimeStamp()) > this.getNumDate(TimeStamp))return true;
				return false;
			}

			this.afterDateTime = function(TimeStamp){
				if(this.getTimeStamp() > TimeStamp)return true;
				return false;
			}
			
			this.getNumDate = function(TimeStamp){
				var num = 0;
				if(TimeStamp > dayOfTimeStamp){
					num = TimeStamp/dayOfTimeStamp;
				}
				return num;
			}

			this.getTime = function(){
				return this.getHour()+this.formatTime+this.getMin();
			}
			
			this.getDDMMYYYY = function(){
				return this.getDate()+this.formatDate+this.getMonth()+this.formatDate+this.getYear();
			}

			this.getDDMMYYYYHHMM = function(){
				return this.getDate()+this.formatDate+this.getMonth()+this.formatDate+this.getYear()+" "+this.getHour()+this.formatTime+this.getMin();
			}

			this.getDDMMYYYYHHMMSS = function(){
				return this.getDate()+this.formatDate+this.getMonth()+this.formatDate+this.getYear()+" "+this.getHour()+this.formatTime+this.getMin()+this.formatTime+this.getSec();
			}
			
			this.getYYYYMMDD = function(){
				return this.getYear()+this.formatDate+this.getMonth()+this.formatDate+this.getDate();
			}

			this.getYYYYMMDDHHMMSS = function(){
				return this.getYear()+this.formatDate+this.getMonth()+this.formatDate+this.getDate()+" "+this.getHour()+this.formatTime+this.getMin()+this.formatTime+this.getSec();
			}
			
			this.getTimeStamp = function(){
				return parseInt(this.__tmpYYYY,10)+parseInt(this.__tmpMM,10)+parseInt(this.__tmpDD,10)+parseInt(this.__tmpHH,10)+parseInt(this.__tmpMin,10)+parseInt(this.__tmpSS,10);
			}

			this.setTimeStamp = function(TimeStamp){
				//this.formatTime = format;
			}
			
			this.setFormatDate = function(format){
				this.formatDate = format;
			}

			this.setFormatTime = function(format){
				this.formatTime = format;
			}	
			
			/* Medthod Set And Get Default*/
			this.getDate = function(){
				if(parseInt(this.__dd,10) < 10){
					return "0"+parseInt(this.__dd,10);;
				}
				return this.__dd;
			}
			
			this.getMonth = function(){
				if(parseInt(this.__mm,10) < 10)return "0"+parseInt(this.__mm,10);
				return this.__mm;
			}
			
			this.getYear = function(){
				return this.__yyyy;
			}
			
			this.getHour = function(){
				if(parseInt(this.__hh,10) < 10)return "0"+parseInt(this.__hh,10);
				return this.__hh;
			}
			
			this.getMin = function(){
				if(parseInt(this.__min,10) < 10)return "0"+parseInt(this.__min,10)
				return this.__min;
			}
			
			this.getSec = function(){
				if(parseInt(this.__ss,10) < 10)return "0"+parseInt(this.__ss,10);
				return this.__ss;
			}
			
			this.setDate = function(dd){
				if(parseInt(this.getDate(),10) != parseInt(dd,10)){
					this.__tmpDD = parseInt(dd,10)*parseInt(dayOfTimeStamp,10);
				}
				this.__dd = dd;
			}
			
			this.setMonth = function(mm){
				if(parseInt(this.getMonth(),10) != parseInt(mm,10)){
					this.__tmpMM = (parseInt(this.getNumDayOfYearToNow(mm),10))*parseInt(dayOfTimeStamp,10);
				}
				this.__mm = mm;
			}
			
			this.setYear = function(yyyy){
				if(parseInt(this.getYear(),10) != parseInt(yyyy,10)){
					//this.__tmpYYYY = (parseInt(yyyy,10)*nDayOfYear)*parseInt(dayOfTimeStamp,10);
					this.__tmpYYYY = this.getNumDayUntilYear(yyyy)*parseInt(dayOfTimeStamp,10);
				}
				this.__yyyy = yyyy;
			}

			this.getNumDayUntilYear = function(year){
				var nYear = year-1970
				var num = Math.floor(nYear/4);
				var numDay = num*1021;//1021 = 255+255+255+256
				var modYear = nYear%4;
				numDay = numDay + (modYear*nDayOfYear);
				return numDay; 
			}
			
			this.setHour = function(hh){
				if(parseInt(this.getHour(),10) != parseInt(hh,10)){
					this.__tmpHH = parseInt(hh,10)*parseInt(hourOfTimeStamp,10);
				}
				this.__hh = hh;
			}
			
			this.setMin = function(mins){
				if(parseInt(this.getMin(),10) != parseInt(mins,10)){
					this.__tmpMin = parseInt(mins,10)*parseInt(minuteOfTimeStamp,10);
				}
				this.__min = mins;
			}
			
			this.setSec = function(ss){
				if(parseInt(this.getSec(),10) != parseInt(ss,10)){
					this.__tmpSS = parseInt(ss,10)*parseInt(secondOfTimeStamp,10);
				}
				this.__ss = ss;
			}

			this.setCscTime = function(hhmm){// hhmm =>> 7.2 จะต้องมีจุดทศนิยม ต้องส่งค่าเป็น String เท่านั้น 
				var datatime = ""+hhmm;
				if(datatime.indexOf(".")>-1){
					var time = hhmm.split(".");
					if(time.length == 2){
						this.setHour(this.isHour(time[0]));
						this.setMin(this.isMin(time[1]));
					}
				}
			}

			this.setTimeStamp = function(TimeStamp){
				
			}
			//End Set and Get Default
			
			this.incDate = function(){
				this.setDate(parseInt(this.getDate(),10)+1);
				var cd = 32 - new Date(this.getYear(), parseInt(this.getMonth(),10)-1, 32).getDate();
				if(parseInt(this.getDate(),10)>cd){
					this.incMonth();
					this.setDate(parseInt(this.getDate(),10)-cd);
				}
			}

			this.incNumDate = function(num){
				for(var i=0;i<num;i++)this.incDate();
			}
			
			this.incMonth = function(){
				this.setMonth(parseInt(this.getMonth(),10)+1);
				if(parseInt(this.getMonth(),10)>12){
					this.incYear();
					this.setMonth(parseInt(this.__mm,10)-12);
				}
			}
			
			this.incYear = function(){
				this.setYear(parseInt(this.getYear(),10)+1);
			}
			
			this.getNumDayOfYearToNow = function(mm){
				var numDay = 0;
				var i = 1;
				while(i < parseInt(mm,10)){
					numDay+=parseInt(this.nDayOfMonth[i++],10);
				}
				return numDay;
			}

			this.decDate = function(){
				this.setDate(parseInt(this.getDate(),10)-1);
				if(parseInt(this.getDate(),10) == 0){
					this.decMonth();
					var cd = 32 - new Date(this.getYear(), parseInt(this.getMonth(),10)-1, 32).getDate();
					this.setDate(parseInt(cd,10));
				}
			}

			this.decMonth = function(){
				this.setMonth(parseInt(this.getMonth(),10)-1);
				if(parseInt(this.getMonth(),10) == 0){
					this.setMonth(12);
					this.decYear();
				}
			}

			this.decNumDate = function(num){
				for(i=0;i<parseInt(num,10);i++){
					this.decDate();
				}
			}

			this.decYear = function(){
				this.setYear(parseInt(this.getYear(),10)-1);
			}

			this.subForDay = function(TimeStamp){//return NumDay
				return parseInt((parseInt(this.getTimeStamp(),10)-parseInt(TimeStamp,10))/dayOfTimeStamp,10);
			}

			this.subForTime = function(TimeStamp){//return HH.MM
				var diffMin = parseInt((parseInt(this.getTimeStamp(),10)-parseInt(TimeStamp,10))/minuteOfTimeStamp,10);
				var h = parseInt(diffMin/60,10);
				var m = parseInt(diffMin%60,10);
				return h+"."+m;
			}

			this.isHour = function(hh){//เช็คว่าค่าที่ส่งมา สามารถเซ็ตเป็น ชั่วโมงได้หรือเปล่า ถ้าได้ก็จะส่งค่านั้นกลับไป ถ้าไม่ได้ ก็จะส่ง 0 กลับไป
				var h = 0;
				if(parseInt(hh,10) >= 24 || parseInt(hh,10) < 0) return 0;//alert("Can't Set Hour Over 23 H. OR Less 0 H. Default Value Hour is Set 0 H.");
				else return hh;
				return h;
			}

			this.isMin = function(mm){
				var m = mm;
				//if(mm.length==1)m = mm+"0";
				if(parseInt(m,10)>59 || parseInt(m,10) < 0)return 0;// alert("Can't Set Minute Over 59 Min. OR Less 0 Min. Default Value Min is Set 0 Min.");
				else return m;
				return m
			}

			this.addTime = function(hhmm){//รับค่าเป็น String แบบ มีทศนิยม "7.20"
				var datatime = ""+hhmm;
				if(datatime.indexOf(".")>-1){
					var time = hhmm.split(".");
					if(time.length == 2){
						this.addHour(this.isHour(time[0]));
						this.addMin(this.isMin(time[1]));
					}
				}
			}

			this.addHour = function(hh){//รับค่าเป็นชั่วโมง
				for(var i=0;i<parseInt(hh,10);i++)this.incHour();
			}

			this.incHour = function(){
				this.setHour(parseInt(this.getHour(),10)+1);
				if(parseInt(this.getHour(),10)>=24){
					this.setHour(parseInt(this.getHour(),10)-24);
					this.incDate();
				}
			}

			this.addMin = function(mm){
				for(var i=0;i<parseInt(mm,10);i++)this.incMin();
			}

			this.incMin = function(){
				this.setMin(parseInt(this.getMin(),10)+1);
				if(parseInt(this.getMin(),10)>=60){
					this.setMin(parseInt(this.getMin(),10)-60);
					this.incHour();
				}
			}

			this.subTime = function(hhmm){//รับค่าเป็น String แบบ มีทศนิยม "7.20"
				var datatime = ""+hhmm;
				if(datatime.indexOf(".")>-1){
					var time = hhmm.split(".");
					if(time.length == 2){
						this.subHour(this.isHour(time[0]));
						this.subMin(this.isMin(time[1]));
					}
				}
			}


			this.subHour = function(hh){//รับค่าเป็นชั่วโมง
				for(var i=0;i<parseInt(hh,10);i++)this.decHour();
			}

			this.decHour = function(){
				if(parseInt(this.getHour(),10)==0){
					this.decDate();
					this.setHour(23);
				}else{
					this.setHour(parseInt(this.getHour(),10)-1);
				}
			}

			this.subMin = function(mm){
				for(var i=0;i<parseInt(mm,10);i++)this.decMin();
			}

			this.decMin = function(){
				if(parseInt(this.getMin(),10)==0){
					this.decHour();
					this.setMin(59);
				}else{
					this.setMin(parseInt(this.getMin(),10)-1);
				}
			}

			this.getDiffDateTime = function(TimeStamp){//return DD:HH:MM
				var Fraction = 0;
				var diffD = (parseInt(this.getTimeStamp(),10)-parseInt(TimeStamp,10))/dayOfTimeStamp;
				Fraction = (parseInt(this.getTimeStamp(),10)-parseInt(TimeStamp,10))%dayOfTimeStamp;
				var diffH = Fraction/hourOfTimeStamp;
				Fraction = Fraction%hourOfTimeStamp;
				var diffM = Fraction/minuteOfTimeStamp;
				return diffD+":"+diffH+":"+diffM;
			}

			this.getMessageError = function(){
				return "xxxxxxxx";
			}



			
		}