function DatePicker() { this.dataRow = 0; this.dataColumn = ""; this.dataColumnTh = ""; this.dataObject = null; this.currentElement = null; this.Gcoop = null; this.callbackFunction = null; this.PickDs = function (dsObject, row, column, callFunction) { var el = dsObject.GetElement(row, column); this.dataRow = row; this.dataColumn = column; this.dataColumnTh = ""; this.dataObject = dsObject; this.currentElement = el; try { if (callFunction == undefined || callFunction == null) { this.callbackFunction = null; } else { this.callbackFunction = callFunction; } } catch (err) { this.callbackFunction = null; } this.SecondPick(); } this.PickDw = function (dwObject, row, columnE, columnT, callFunction) { var el = this.Gcoop.GetElForm(columnT + "_" + (row - 1)); this.dataRow = row; this.dataColumn = columnE; this.dataColumnTh = columnT; this.dataObject = dwObject; this.currentElement = el; try { if (callFunction == undefined || callFunction == null) { this.callbackFunction = null; } else { this.callbackFunction = callFunction; } } catch (err) { this.callbackFunction = null; } this.SecondPick(); } this.SecondPick = function () { var valueEl = ""; var theDate = null; try { valueEl = this.currentElement.value; if (valueEl == null || valueEl == undefined) { valueEl = ""; } } catch (err) { valueEl = ""; } if (valueEl.length == 8) { try { if (valueEl != "00000000") { var d = this.Gcoop.ParseInt(valueEl.substr(0, 2)); var m = this.Gcoop.ParseInt(valueEl.substr(2, 2)); var yyyy = this.Gcoop.ParseInt(valueEl.substr(4)) - 543; theDate = new Date(); theDate.setFullYear(yyyy, m - 1, d); } } catch (err) { theDate = null; } } else if (valueEl.length == 10) { try { if (valueEl != "00/00/0000") { var d = this.Gcoop.ParseInt(valueEl.substr(0, 2)); var m = this.Gcoop.ParseInt(valueEl.substr(3, 2)); var yyyy = this.Gcoop.ParseInt(valueEl.substr(6)) - 543; theDate = new Date(); theDate.setFullYear(yyyy, m - 1, d); } } catch (err) { theDate = null; } } this.PerformCarlendar(theDate); } this.PerformCarlendar = function (performDate) { var persisDate = new Date(); if (performDate == null || performDate == undefined) { performDate = null; } else { persisDate = performDate; } this.SetFocusDate(persisDate); this.SetOutputDate(performDate); this.ShowCarlendar(); } this.SetFocusDate = function (focusDate) { var yyyy = focusDate.getFullYear(); var m = focusDate.getMonth(); var d = focusDate.getDate(); var dtFirst = new Date(); dtFirst.setFullYear(yyyy, m, 1); var firstDay = dtFirst.getDay(); firstDay--; if (firstDay < 0) firstDay = 6; var lastDay = 31; if (m == 3 || m == 5 || m == 8 || m == 10) { lastDay = 30; } else if (m == 1) { lastDay = 28; if (yyyy % 4 == 0) { lastDay = 29; } } var dd = 0; for (var i = 0; i < 42; i++) { if (i >= firstDay) { dd++; } if (dd > 0 && dd <= lastDay) { document.getElementById("divDayPickerIndex" + i).innerHTML = dd + ""; if (dd == d) { document.getElementById("divDayPickerIndex" + i).style.backgroundColor = "rgb(255,200,211)"; } else { document.getElementById("divDayPickerIndex" + i).style.backgroundColor = "rgb(211,231,255)"; } } else { document.getElementById("divDayPickerIndex" + i).innerHTML = ""; document.getElementById("divDayPickerIndex" + i).style.backgroundColor = "rgb(211,231,255)"; } } } this.SetOutputDate = function (dtOutput) { var yyyy = 0; var m = 0; var d = 0; if (dtOutput != null) { yyyy = dtOutput.getFullYear() + 543; m = dtOutput.getMonth() + 1; d = dtOutput.getDate(); } document.getElementById("tbDatePickerDay").value = d; document.getElementById("tbDatePickerMonth").value = m; document.getElementById("tbDatePickerYear").value = yyyy; this.SetOutputSelect("ddDatePickerDay", d); this.SetOutputSelect("ddDatePickerMonth", m); this.SetOutputSelect("ddDatePickerYear", yyyy); } this.SetOutputSelect = function (id, value) { var val = value + ""; var sel = document.getElementById(id); for (var i = 0; i < sel.options.length; ++i) { if (sel.options[i].value == val) { sel.selectedIndex = i; break; } } } this.ShowCarlendar = function () { var position = Gcoop.GetObjectPosition(this.currentElement); var top = Math.ceil(position[0]) + 20; var left = Math.ceil(position[1]); var lastPosition = Gcoop.GetObjectPosition(document.getElementById("theLastObject")); var lastLeft = Math.ceil(lastPosition[1]); var lastTop = Math.ceil(lastPosition[0]); if ((left + 260) > lastLeft) { left = lastLeft - 270; } if (top + 210 > lastTop) { top = lastTop - 210; } document.getElementById("divCalendarPicker").style.position = "absolute"; document.getElementById("divCalendarPicker").style.top = top + "px"; document.getElementById("divCalendarPicker").style.left = left + "px"; document.getElementById("divCalendarPicker").style.visibility = "visible"; document.getElementById("divCalendarPicker").style.zIndex = "10"; } this.Cancel = function () { this.currentElement = null; this.dataRow = 0; this.dataColumn = ""; this.dataColumnTh = ""; this.dataObject = null; this.callbackFunction = null; document.getElementById("divCalendarPicker").style.position = "absolute"; document.getElementById("divCalendarPicker").style.top = 0 + "px"; document.getElementById("divCalendarPicker").style.left = 0 + "px"; document.getElementById("divCalendarPicker").style.visibility = "hidden"; document.getElementById("divCalendarPicker").style.zIndex = "-1"; } this.VerifyDate = function (yyyy, m, d, isAlert) { var newD = d; if (m == 1) { if (yyyy % 4 == 0) { if (d > 29) { newD = 29; if (isAlert) { alert("ไม่มีวันที่ " + d + "/" + (m + 1) + "/" + (yyyy + 543) + " ในระบบปฏิทิน"); } } } else { if (d > 28) { newD = 28; if (isAlert) { alert("ไม่มีวันที่ " + d + "/" + (m + 1) + "/" + (yyyy + 543) + " ในระบบปฏิทิน"); } } } } else if (m == 3 || m == 5 || m == 8 || m == 10) { if (d > 30) { newD = 30; if (isAlert) { alert("ไม่มีวันที่ " + d + "/" + (m + 1) + "/" + (yyyy + 543) + " ในระบบปฏิทิน"); } } } var newData = new Date(); newData.setFullYear(yyyy, m, newD); return newData; } this.IsEqualTextBox = function (dtTest) { var elDate = document.getElementById("tbDatePickerDay"); var elMonth = document.getElementById("tbDatePickerMonth"); var elYear = document.getElementById("tbDatePickerYear"); var d = 0; var m = 0; var yyyy = 0; if (elDate.value == "0" || elMonth.value == "0" || elYear.value == "0") { return dtTest == null && elDate.value == "0" && elMonth.value == "0" && elYear.value == "0"; } else { d = this.Gcoop.ParseInt(elDate.value); m = this.Gcoop.ParseInt(elMonth.value) - 1; yyyy = this.Gcoop.ParseInt(elYear.value) - 543; } if (dtTest == null) return false; var theDate = this.VerifyDate(yyyy, m, d, false); return dtTest.getFullYear() == theDate.getFullYear() && dtTest.getMonth() == theDate.getMonth() && dtTest.getDate() == theDate.getDate(); } this.SubmitDatePicker = function (dtDate) { if (dtDate == null) { if (this.dataColumnTh == "") { this.dataObject.SetItem(this.dataRow, this.dataColumn, "0000-00-00"); } else { this.dataObject.SetItem(this.dataRow, this.dataColumnTh, "00000000"); this.dataObject.SetItem(this.dataRow, this.dataColumn, null); } var theCallBack = this.callbackFunction; this.Cancel(); if (theCallBack != null) { theCallBack(); } return; } var d = dtDate.getDate(); var m = dtDate.getMonth() + 1; var yyyy = dtDate.getFullYear(); var mm = (m < 10 ? "0" : "") + m; var dd = (d < 10 ? "0" : "") + d; var yyth = yyyy + 543; if (this.dataColumnTh == "") { this.dataObject.SetItem(this.dataRow, this.dataColumn, yyyy + "-" + mm + "-" + dd); } else { this.dataObject.SetItem(this.dataRow, this.dataColumnTh, dd + mm + yyth); this.dataObject.SetItem(this.dataRow, this.dataColumn, dtDate); } var theCallBack = this.callbackFunction; this.Cancel(); if (theCallBack != null) { theCallBack(); } } this.OnClickDivDateRunning = function (el) { try { var inText = this.Gcoop.Trim(el.innerHTML); if (inText == "") { return; } var d = this.Gcoop.ParseInt(inText); var elDate = document.getElementById("tbDatePickerDay"); var elMonth = document.getElementById("tbDatePickerMonth"); var elYear = document.getElementById("tbDatePickerYear"); var m = 0; var yyyy = 0; if (elDate.value == "0" || elMonth.value == "0" || elYear.value == "0") { var dtToday = new Date(); m = dtToday.getMonth(); yyyy = dtToday.getFullYear(); } else { m = this.Gcoop.ParseInt(elMonth.value) - 1; yyyy = this.Gcoop.ParseInt(elYear.value) - 543; } var theDate = this.VerifyDate(yyyy, m, d, false); if (this.IsEqualTextBox(theDate)) { this.SubmitDatePicker(theDate); } else { this.PerformCarlendar(theDate); } } catch (err) { } } this.OnClickDivMonthBack = function () { var elDate = document.getElementById("tbDatePickerDay"); var elMonth = document.getElementById("tbDatePickerMonth"); var elYear = document.getElementById("tbDatePickerYear"); var d = 0; var m = 0; var yyyy = 0; if (elDate.value == "0" || elMonth.value == "0" || elYear.value == "0") { var dtToday = new Date(); d = dtToday.getDate(); m = dtToday.getMonth(); yyyy = dtToday.getFullYear(); } else { d = this.Gcoop.ParseInt(elDate.value); m = this.Gcoop.ParseInt(elMonth.value) - 1; yyyy = this.Gcoop.ParseInt(elYear.value) - 543; } m--; if (m < 0) { m = 11 yyyy--; } var theDate = this.VerifyDate(yyyy, m, d, false); this.PerformCarlendar(theDate); } this.OnClickDivMonthNext = function () { var elDate = document.getElementById("tbDatePickerDay"); var elMonth = document.getElementById("tbDatePickerMonth"); var elYear = document.getElementById("tbDatePickerYear"); var d = 0; var m = 0; var yyyy = 0; if (elDate.value == "0" || elMonth.value == "0" || elYear.value == "0") { var dtToday = new Date(); d = dtToday.getDate(); m = dtToday.getMonth(); yyyy = dtToday.getFullYear(); } else { d = this.Gcoop.ParseInt(elDate.value); m = this.Gcoop.ParseInt(elMonth.value) - 1; yyyy = this.Gcoop.ParseInt(elYear.value) - 543; } m++; if (m > 11) { m = 0 yyyy++; } var theDate = this.VerifyDate(yyyy, m, d, false); this.PerformCarlendar(theDate); } this.OnClickDivYearBack = function () { var elDate = document.getElementById("tbDatePickerDay"); var elMonth = document.getElementById("tbDatePickerMonth"); var elYear = document.getElementById("tbDatePickerYear"); var d = 0; var m = 0; var yyyy = 0; if (elDate.value == "0" || elMonth.value == "0" || elYear.value == "0") { var dtToday = new Date(); d = dtToday.getDate(); m = dtToday.getMonth(); yyyy = dtToday.getFullYear(); } else { d = this.Gcoop.ParseInt(elDate.value); m = this.Gcoop.ParseInt(elMonth.value) - 1; yyyy = this.Gcoop.ParseInt(elYear.value) - 543; } yyyy--; var theDate = this.VerifyDate(yyyy, m, d, false); this.PerformCarlendar(theDate); } this.OnClickDivYearNext = function () { var elDate = document.getElementById("tbDatePickerDay"); var elMonth = document.getElementById("tbDatePickerMonth"); var elYear = document.getElementById("tbDatePickerYear"); var d = 0; var m = 0; var yyyy = 0; if (elDate.value == "0" || elMonth.value == "0" || elYear.value == "0") { var dtToday = new Date(); d = dtToday.getDate(); m = dtToday.getMonth(); yyyy = dtToday.getFullYear(); } else { d = this.Gcoop.ParseInt(elDate.value); m = this.Gcoop.ParseInt(elMonth.value) - 1; yyyy = this.Gcoop.ParseInt(elYear.value) - 543; } yyyy++; var theDate = this.VerifyDate(yyyy, m, d, false); this.PerformCarlendar(theDate); } this.OnDdPickDayChanged = function (el) { var elDate = document.getElementById("tbDatePickerDay"); var elMonth = document.getElementById("tbDatePickerMonth"); var elYear = document.getElementById("tbDatePickerYear"); var d = this.Gcoop.ParseInt(el.value); var m = 0; var yyyy = 0; if (elMonth.value == "0" || elYear.value == "0") { var dtToday = new Date(); m = dtToday.getMonth(); yyyy = dtToday.getFullYear(); } else { m = this.Gcoop.ParseInt(elMonth.value) - 1; yyyy = this.Gcoop.ParseInt(elYear.value) - 543; } var theDate = this.VerifyDate(yyyy, m, d, true); this.PerformCarlendar(theDate); } this.OnDdPickMonthChanged = function (el) { var elDate = document.getElementById("tbDatePickerDay"); var elMonth = document.getElementById("tbDatePickerMonth"); var elYear = document.getElementById("tbDatePickerYear"); var d = 0; var m = this.Gcoop.ParseInt(el.value) - 1; var yyyy = 0; if (elDate.value == "0" || elYear.value == "0") { var dtToday = new Date(); d = dtToday.getDate(); yyyy = dtToday.getFullYear(); } else { d = this.Gcoop.ParseInt(elDate.value); yyyy = this.Gcoop.ParseInt(elYear.value) - 543; } var theDate = this.VerifyDate(yyyy, m, d, false); this.PerformCarlendar(theDate); } this.OnDdPickYearChanged = function (el) { var elDate = document.getElementById("tbDatePickerDay"); var elMonth = document.getElementById("tbDatePickerMonth"); var elYear = document.getElementById("tbDatePickerYear"); var d = 0; var m = 0; var yyyy = this.Gcoop.ParseInt(el.value) - 543; if (elDate.value == "0" || elMonth.value == "0") { var dtToday = new Date(); d = dtToday.getDate(); m = dtToday.getMonth(); } else { d = this.Gcoop.ParseInt(elDate.value); m = this.Gcoop.ParseInt(elMonth.value) - 1; } var theDate = this.VerifyDate(yyyy, m, d, false); this.PerformCarlendar(theDate); } this.OnClickNullButton = function () { if (this.IsEqualTextBox(null)) { this.SubmitDatePicker(null); } else { this.PerformCarlendar(null); } } this.OnClickToDayButton = function () { if (this.IsEqualTextBox(new Date())) { this.SubmitDatePicker(new Date()); } else { this.PerformCarlendar(new Date()); } } this.OnClickSubmitButton = function () { var elDate = document.getElementById("tbDatePickerDay"); var elMonth = document.getElementById("tbDatePickerMonth"); var elYear = document.getElementById("tbDatePickerYear"); var d = 0; var m = 0; var yyyy = 0; if (elDate.value == "0" || elMonth.value == "0" || elYear.value == "0") { this.SubmitDatePicker(null); } else { d = this.Gcoop.ParseInt(elDate.value); m = this.Gcoop.ParseInt(elMonth.value) - 1; yyyy = this.Gcoop.ParseInt(elYear.value) - 543; var theDate = this.VerifyDate(yyyy, m, d, false); this.SubmitDatePicker(theDate); } } }