var NTextBox_ChkIn = "dchkin";
var NTextBox_ChkOut = "dchkout";
var NButton_ChkIn = "B_dchkin";
var NButton_ChkOut = "B_dchkout";
var DateShift_ChkIn = "2";


TimeShift();
function TimeShift(){
	var dNow = new Date();
	if((((dNow.getUTCHours()*60)+dNow.getUTCMinutes())  >= ((10*60)+5))  &&  ((dNow.getUTCHours()*60)  < (17*60))){
		DateShift_ChkIn = 3;
	}
	else{
		DateShift_ChkIn = 2;
	}
}

/*
function ShiftDate()
{
	var DateFix = "02";  //วันที่กำหนด
	var MonthFix = "12";  //เดือนที่กำหนด
	var YearFix = "2007";  //ปีที่กำหนด
	
	var DateNow = new Date();
	var ShiftY = DateNow.getFullYear();
	var ShiftM = DateNow.getMonth()+1;
	var ShiftD = DateNow.getDate();

	if((parseInt(ShiftY) == parseInt(YearFix)) && (parseInt(ShiftM) == parseInt(MonthFix)) && (parseInt(ShiftD) <= (parseInt(DateFix)-parseInt(DateShift_ChkIn)))) 
	{
		DateShift_ChkIn = ((parseInt(DateFix)-parseInt(DateShift_ChkIn))-parseInt(ShiftD))+parseInt(DateShift_ChkIn);
	}
}
*/
function ShiftDate()
{
	var DateNow = new Date();
	var ShiftY = DateNow.getFullYear();
	var ShiftM = DateNow.getMonth()+1;
	var ShiftD = DateNow.getDate();
	
	if(ShiftY==2007)
	{
		if(ShiftM==11)
		{
			if(ShiftD==28){DateShift_ChkIn=DateShift_ChkIn+4;}
			else if(ShiftD==29){DateShift_ChkIn=DateShift_ChkIn+3;}
			else if(ShiftD==30){DateShift_ChkIn=DateShift_ChkIn+2;}
		}
		else if(ShiftM==12)
		{
			if(ShiftD==1){DateShift_ChkIn=DateShift_ChkIn+1;}
			else if(ShiftD==2){DateShift_ChkIn=DateShift_ChkIn;}
		}
	}
}
ShiftDate();  //กำหนดให้มีการ Fix วันเริ่มต้นหรือไม่

// Start Process Date in textbox  : Auto  ==============================================

										function  DateA_Change_Combo(M_DateA) 
											{
												//if(document.getElementById("dchkout").value == "dd/mm/yyyy"){document.getElementById("dchkout").value = CheckDayMonth_Combo(M_DateA , 1);}
												document.getElementById("dchkout").value = CheckDayMonth_Combo(M_DateA , 1);
											}
											function DateB_Change_Combo(M_DateB)
											{
												if(document.getElementById("dchkin").value == "dd/mm/yyyy"){document.getElementById("dchkin").value = CheckDayMonth_Combo(M_DateB , 2);}
											}
											
											function CheckDayMonth_Combo(M_BuffDate , M_Status)
											{
												var M_DaMo=new Array(0,31,28,31,30,31,30,31,31,30,31,30,31);
												var M_Day = 0;
												var M_Month = 0;
												var M_Year = 0;
												var M_D = "";
												var M_M = "";
												var M_Y = "";
												var M_NDaMo = "";
										
													//M_Status = 1 คือวันที่เริ่มต้น    M_Status = 2  คือวันที่สิ้นสุด
													if(M_Status == 1){M_Day = Number(M_BuffDate.substring(0,2)) + 1;}else{M_Day = Number(M_BuffDate.substring(0,2)) - 1;}
													var M_Month = Number(M_BuffDate.substring(3,5));
													var M_Year = Number(M_BuffDate.substring(6,10));
													if(M_Year%4==0){ M_DaMo[2]=29;}
														
														if(M_Status == 1)
														{	
															if(M_DaMo[M_Month] <= (M_Day-1)){M_Day = 1; M_Month = M_Month + 1;}
															if(M_Month > 12){M_Day = 1; M_Month = 1; M_Year = M_Year + 1;}
														}
														else
														{
															if(M_Day == 0){M_Day = M_DaMo[M_Month-1]; M_Month = M_Month - 1;}
															if(M_Month == 0){M_Day = M_DaMo[12]; M_Month = 12; M_Year = M_Year - 1;}
														}
														
																if(String(M_Day).length == 1){M_D = "0" + String(M_Day);}else{M_D = String(M_Day);}
																if(String(M_Month).length == 1){M_M = "0" + String(M_Month);}else{M_M = String(M_Month);}
																if(String(M_Year).length == 4){M_Y = String(M_Year);}
															
															
																M_NDaMo = M_D + '/' + M_M + '/' + M_Y;
																return M_NDaMo;
											}
											
											function ShowCalendar_Combo(NButton)
											{
												if(NButton == NButton_ChkIn){document.getElementById("B_dchkin").onclick();}
												else if(NButton == NButton_ChkOut){document.getElementById("B_dchkout").onclick();}
											}

											function ResetDate_Combo()
											{
												document.getElementById("dchkin").value = "dd/mm/yyyy";
												document.getElementById("dchkout").value = "dd/mm/yyyy";
											}
										
// End Process Date in textbox  : Auto  ==============================================




// Start Process Set Date : Calendar  ==============================================
										var startDate;
												var endDate;
												var ONEDAY = 3600 * 24;
										
												function resetDates() {
													startDate = endDate = null;
												}
												
												function compareDatesOnly(date1, date2) {
													var year1 = date1.getFullYear();
													var year2 = date2.getFullYear();
													var month1 = date1.getMonth();
													var month2 = date2.getMonth();
													var day1 = date1.getDate();
													var day2 = date2.getDate();
													if (year1 > year2) {
														return -1;
													}
													if (year2 > year1) {
														return 1;
													}
												
													if (month1 > month2) {
														return -1;
													}
													if (month2 > month1) {
														return 1;
													}
													
													if(day1 == day2)
													{
														return -1;
													}
													
													if (day1 > day2) {
														return -1;
													}
													if (day2 > day1) {
														return 1;
													}
												
													return 0;
												
												}
												
												function filterDates1(cal) {
													startDate = cal.date;
													if (endDate == null) { 
														Zapatec.Calendar.setup({
															inputField     :    NTextBox_ChkOut ,
															button         :    NButton_ChkOut ,  // What will trigger the popup of the calendar
															ifFormat       :    "%d/%m/%Y",
															timeFormat     :    "24",
															date           :     startDate,
															electric       :     false,
															numberMonths   :     2,
															showsTime      :     false,          //no time
															disableFunc    :    dateInRange2, //the function to call
															onUpdate       :    filterDates2
														});
													}
												}
												
												function filterDates2(cal) {
													endDate = cal.date;
												}
												
												function dateInRange1(date) {
												
													if (endDate != null) {
														var compareEnd = compareDatesOnly(date, endDate);
														if  (compareEnd < 0) {
															return (true);
														}
												
														if  (compareEnd == 0) {
															{return "edges";}
														}
												
												
														if (startDate != null){
															var compareStart = compareDatesOnly(date, startDate);
															if  (compareStart < 0) {
																return "between";
															} 
														} 
													}
													//  New Code ==============================================
													var today = new Date();
													var M_Date = Number(today.getDate()) + parseInt(DateShift_ChkIn) ;
													var M_Month = Number(today.getMonth());
													var M_Year = Number(today.getFullYear());
													today = new Date(M_Year,M_Month,M_Date);
										
													//=====================================================
													var compareToday = compareDatesOnly(date, today);
													if (compareToday > 0) { 
														return(true);
													}
													
													return false;
													return(ret);
												}
										
												
												function dateInRange2(date) {
													if (startDate != null) {
														var compareDays = compareDatesOnly(startDate, date);
														if  (compareDays < 0) {
															return (true);
														}
														
														if  (compareDays == 0) {
															{return "edges";}
														}
												
														if ((endDate != null) && (date > startDate) && (date < endDate)) {
															return "between";
														} 
													} 
													
													//  New Code ==============================================
													var now = new Date();
													if((document.getElementById("dchkin").value == "dd/mm/yyyy") || (document.getElementById("dchkin").value == ""))
													{
															var M_Date = Number(now.getDate()) + parseInt(DateShift_ChkIn);
															var M_Month = Number(now.getMonth());
															var M_Year = Number(now.getFullYear());
															now = new Date(M_Year,M_Month,M_Date);
													}
													else
													{
															var tmArrChkIn = document.getElementById("dchkin").value.split("/");
															var tmChkInDate="";	
															if(tmArrChkIn.length==3)
															{
																var M_Date = tmArrChkIn[0];
																var M_Month = --tmArrChkIn[1];
																var M_Year = tmArrChkIn[2];
															}
															now = new Date(M_Year,M_Month,M_Date);
													}
													//=====================================================
										
													if (compareDatesOnly(now, date) < 0) {
														return (true);
													}
													
													return false;
												}
												
												
												
												var cal = new Zapatec.Calendar.setup({
													inputField     :    NTextBox_ChkIn ,   // id of the input field
													button         :    NButton_ChkIn ,  // What will trigger the popup of the calendar
													ifFormat       :    "%d/%m/%Y", // format of the input field: Mar 18, 2005
													showsTime      :     false,      //no time
													saveDate       :    2,            // save for two days
													electric       :     false,
													numberMonths   :     2,
													dateStatusFunc :    dateInRange1, //the function to call
													onUpdate       :    filterDates1
										
												});
												
												Zapatec.Calendar.setup({
													inputField     :    NTextBox_ChkOut ,
													button         :    NButton_ChkOut ,  // What will trigger the popup of the calendar
													ifFormat       :    "%d/%m/%Y", // format of the input field: Mar 18, 2005
													showsTime      :     false,      // no time
													saveDate       :    1,            // save for one day. You can use fractions here such as 1/2, 1/4, 1/24 ... 
													electric       :     false,
													numberMonths   :     2,
													dateStatusFunc :    dateInRange2, //the function to call
													onUpdate       :    filterDates2
												});

// End Process Set Date : Calendar  ==============================================
