pacozaa

YipInSoi_MWA_ValidateField_function

Jun 10th, 2015
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var jqGridBC01020E = '';
  2. $(document).ready(function() {
  3.  
  4.     setFieldPattern("houseNo","0011000");
  5.     setFieldPattern("moo","0010000");
  6.     setFieldPattern("agentPhone","0010000");
  7.     setFieldPattern("agentMobile","0010000");
  8.     var actRtn = $("#act").val();
  9.     if (actRtn != 'NEW' && !actRtn.match(/INQ*/) && !actRtn.match(/UPD*/) && !actRtn.match(/ADD*/) && !actRtn.match(/DEL*/)) {
  10.         $("#barSearch").show();
  11.         $("#gridSearch").show();
  12.         $("#form1").hide();
  13.         createGrid();
  14.     } else {
  15.         $("#barSearch").hide();
  16.         $("#gridSearch").hide();
  17.         $("#form1").show();
  18.         $("#DEL").prop("disabled", false);
  19.         $("#UPD").show();
  20.         $("#ADD").hide();
  21.         $("#agentCode").focus();
  22.  
  23.         if ($("#recStatus").val() == '0') {
  24.             $("#DEL").prop("disabled", true);
  25.         }
  26.  
  27.         if ($("#act").val() == 'ADDERR') {
  28.             $("#agentCode").focus();
  29.             $("#recStatus").val("");
  30.             $("#updatedBy").val("");
  31.             $("#updatedDate").val("");
  32.             $("#UPD").hide();
  33.             $("#ADD").show();
  34.             $("#DEL").prop("disabled", true);
  35.  
  36.         } else if ($("#act").val() == 'ADDRTN' || $("#act").val() == 'NEW') {
  37.             $("#UPD").hide();
  38.             $("#DEL").prop("disabled", true);
  39.             $("#ADD").show();
  40.             $("#agentCode").focus();
  41.         }
  42.     }
  43.     setDDList();
  44.     setFormatDate("contactDate", 3);
  45.     $('#contactDate').datepicker({
  46.         language: 'th-th',
  47.         autoclose: true,
  48.         orientation: "auto",
  49.         format: "dd/mm/yyyy"
  50.     });
  51.     setFormatDate("expireDate", 3);
  52.     $('#expireDate').datepicker({
  53.         language: 'th-th',
  54.         autoclose: true,
  55.         orientation: "auto",
  56.         format: "dd/mm/yyyy"
  57.     });
  58.    
  59.  
  60. afterLoad();
  61. });
  62. function setDDList()
  63. {
  64.     //===================== ชุด จังหวัด + อำเภอ + ตำบล =====================//
  65.     //----- จังหวัด
  66.     $("#provinceCode").select2({
  67.         width: "100%",
  68.         placeholder: "กรุณาเลือก",
  69.         allowClear: true
  70.     }).on('change', function(){
  71.         $("#amphurCode").select2("val", "");
  72.         $("#districtCode").select2("val", "");
  73.         if($(this).select2("val").trim() == '')
  74.         {
  75.             $("#amphurCode").select2("readonly", true);
  76.          $("#amphurCodeCtl").val('');
  77.             $("#amphurNameCtl").val('');
  78.             $("#districtCode").select2("readonly", true);
  79.          $("#districtCodeCtl").val('');
  80.             $("#districtNameCtl").val('');
  81.             $("#zipCodeCtl").val('');
  82.         }
  83.         else
  84.         {
  85.             $("#amphurCode").select2("readonly", false);
  86.          $("#amphurCodeCtl").val('');
  87.             $("#amphurNameCtl").val('');
  88.             $("#districtCode").select2("readonly", true);
  89.          $("#districtCodeCtl").val('');
  90.             $("#districtNameCtl").val('');
  91.             $("#zipCodeCtl").val('');
  92.         }
  93.     });
  94.    
  95.     //----- อำเภอ
  96.     $("#amphurCode").select2({
  97.         ajax: {
  98.             url: "../bc/BCAgentInfoServlet?act=SRHAMPHUR",
  99.             dataType: 'json',
  100.             //term คือค่าที่ Key เข้ามาใน dropDownList
  101.             data: function(term, page) {
  102.                 return {
  103.                     AmphurName : term
  104.                     , ProvinceCode : $("#provinceCode").val()
  105.                 };
  106.             },
  107.             results: function(data, page ) {
  108.                 alert(data[0].text);
  109.                 //var test = [{text:data.objectName, children:[{id:data.objectCode, text:data.objectName}]}];
  110.                 return { results: data }
  111.             }
  112.         }
  113.         , allowClear: true
  114.         , placeholder: "กรุณาเลือก"
  115.         , initSelection: function (element, callback) {
  116.             return $.getJSON("../bc/BCAgentInfoServlet?act=SRHAMPHUR", {ProvinceCode : $("#provinceCode").val()}, function(data) {
  117.                amphurId = '';
  118.                amphurName = '';
  119.                if(typeof(data) !== 'undefined' && data.length > 0)
  120.                {
  121.                   for(i=0; i<data.length; i++)
  122.                   {
  123.                      amphurArray = data[i].id.split(":");
  124.                      if(amphurArray[0] == element.val())
  125.                      {
  126.                         amphurId = amphurArray[0];
  127.                         amphurName = amphurArray[1];
  128.                         break;
  129.                      }
  130.                   }
  131.                }
  132.                return callback({ id: amphurId, text: amphurName });
  133.             });
  134.         }
  135.     }).on('change', function(){
  136.         $("#districtCode").select2("val", "");
  137.         if($(this).select2("val").trim() == '')
  138.         {
  139.          $("#amphurCodeCtl").val('');
  140.             $("#amphurNameCtl").val('');
  141.             $("#districtCode").select2("readonly", true);
  142.          $("#districtCodeCtl").val('');
  143.             $("#districtNameCtl").val('');
  144.             $("#zipCodeCtl").val('');
  145.         }
  146.         else
  147.         {
  148.             $("#districtCode").select2("readonly", false);
  149.          $("#districtCodeCtl").val('');
  150.             $("#districtNameCtl").val('');
  151.             $("#zipCodeCtl").val('');
  152.             var amphurArray = $(this).select2("val").split(":");
  153.             $("#amphurCodeCtl").val(amphurArray[0]);
  154.             $("#amphurNameCtl").val(amphurArray[1]);
  155.         }
  156.     });
  157.    
  158.     //----- ตำบล
  159.     $("#districtCode").select2({
  160.         ajax: {
  161.             url: "../bc/BCAgentInfoServlet?act=SRHDISTRICT",
  162.             dataType: 'json',
  163.             //term คือค่าที่ Key เข้ามาใน dropDownList
  164.             data: function(term, page) {
  165.                 return {
  166.                     DistrictName : term
  167.                     , ProvinceCode : $("#provinceCode").val()
  168.                     , AmphurCode : $("#amphurCodeCtl").val()
  169.                 };
  170.             },
  171.             results: function(data, page ) {
  172.                 //alert(data[0].text);
  173.                 //var test = [{text:data.objectName, children:[{id:data.objectCode, text:data.objectName}]}];
  174.                 return { results: data }
  175.             }
  176.         }
  177.         //, width:'300px'
  178.         //, formatResult: formatList
  179.         , allowClear: true
  180.         , placeholder: "กรุณาเลือก"
  181.         , initSelection: function (element, callback) {
  182.             return $.getJSON("../bc/BCAgentInfoServlet?act=SRHDISTRICT",
  183.                {ProvinceCode : $("#provinceCode").val(), AmphurCode : $("#amphurCodeCtl").val()}
  184.                , function(data) {
  185.                districtId = '';
  186.                districtName = '';
  187.                if(typeof(data) !== 'undefined' && data.length > 0)
  188.                {
  189.                   for(i=0; i<data.length; i++)
  190.                   {
  191.                      districtArray = data[i].id.split(":");
  192.                      if(districtArray[0] == element.val()) {districtId = districtArray[0]; districtName = districtArray[1]; break;}
  193.                   }
  194.                }
  195.            
  196.                return callback({ id: districtId, text: districtName });
  197.             });
  198.         }
  199.     }).on('change', function(){
  200.         if($(this).select2("val").trim() == '')
  201.         {
  202.             $("#districtCodeCtl").val('');
  203.             $("#districtNameCtl").val('');
  204.            
  205.             $("#zipCodeCtl").val('');
  206.         }
  207.         else
  208.         {
  209.             var districtArray = $(this).select2("val").split(":");
  210.            
  211.             $("#districtCodeCtl").val(districtArray[0]);
  212.             $("#districtNameCtl").val(districtArray[1]);
  213.             $("#zipCodeCtl").val(districtArray[2]);
  214.         }
  215.     });
  216.    
  217.     if($("#provinceCode").select2("val")=='')
  218.     {
  219.        $("#amphurCode").select2("readonly", true);
  220.     }
  221.     if($("#amphurCode").select2("val")=='')
  222.     {
  223.        $("#districtCode").select2("readonly", true);
  224.     }
  225.    
  226.     //----- set ค่าตอน Inquiry มาแสดงที่จอภาพ
  227.     if($("#amphurCodeCtl").val() != '')
  228.     {
  229.         $('#amphurCode').select2('data', {id:$("#amphurCodeCtl").val(), text:$("#amphurNameCtl").val()} );
  230.     }
  231.     if($("#districtCodeCtl").val() != '')
  232.     {
  233.         $('#districtCode').select2('data', {id:$("#districtCodeCtl").val(), text:$("#districtNameCtl").val()} );
  234.     }
  235. }
  236. // End created by tosspoka 2015-05-03
  237.  
  238. function createGrid() {
  239.     jqGridBC01020E = jQuery("#masterGrid");
  240.     //$(function () {
  241.  
  242.     $('#act').val('SRH'); // Added for serialize
  243.  
  244.     // Setup grid
  245.     jqGridBC01020E.jqGrid({
  246.         url: "../bc/BCAgentInfoServlet?" + $("#frmBC01020E").serialize(),
  247.         datatype: "json",
  248.         mtype: 'POST',
  249.         colNames: ['', 'รหัสผู้รับจ้าง', 'ชื่อบริษัทผู้รับจ้าง', 'ที่อยู่', 'โทรศัพท์', 'วันที่ทำสัญญา', 'วันที่สิ้นสุดสัญญา', 'สถานะข้อมูล'],
  250.         colModel: [{
  251.             name: 'act',
  252.             index: 'act',
  253.             width: '3%',
  254.             align: 'center',
  255.             sortable: false,
  256.             formatter: function(cellval, options, rowObject) {
  257.                 return "<span class=\"glyphicon glyphicon-pencil\" style=\"cursor:pointer;\" onclick=\"getData('" + options.rowId + "');\"><\/span>";
  258.             }
  259.         }, {
  260.             name: 'agentCode',
  261.             index: 'aoBCAgentInfo.agentCode',
  262.             align: 'center',
  263.             editrules: {
  264.                 required: true
  265.             },
  266.             editoptions: {
  267.                 size: 20,
  268.                 maxlength: 20
  269.             },
  270.             editable: true,
  271.             width: '10%'
  272.         }, {
  273.             name: 'agentName',
  274.             index: 'aoBCAgentInfo.agentName',
  275.             align: 'center',
  276.             editrules: {
  277.                 required: true
  278.             },
  279.             editoptions: {
  280.                 size: 20,
  281.                 maxlength: 20
  282.             },
  283.             editable: true,
  284.             width: '10%'
  285.         }, {
  286.             name: 'address',
  287.             index: 'aoBCAgentInfo.address',
  288.             align: 'left',
  289.             width: '32%'
  290.         }, {
  291.             name: 'agentPhone',
  292.             index: 'aoBCAgentInfo.agentPhone',
  293.             align: 'center',
  294.             editoptions: {
  295.                 readonly: true,
  296.                 size: 20
  297.             },
  298.             width: '10%'
  299.         }, {
  300.             name: 'contactDate',
  301.             index: 'aoBCAgentInfo.contactDateDisplay',
  302.             align: 'right',
  303.             editrules: {
  304.                 required: true
  305.             },
  306.             editoptions: {
  307.                 size: 20,
  308.                 maxlength: 20
  309.             },
  310.             editable: true,
  311.             width: '15%'
  312.         }, {
  313.             name: 'expireDate',
  314.             index: 'aoBCAgentInfo.expireDateDisplay',
  315.             align: 'center',
  316.             width: '10%'
  317.         },{
  318.             name: 'recStatus',
  319.             index: 'aoBCAgentInfo.recStatus',
  320.             align: 'center',
  321.             formatter: formatStatus,
  322.             width: '10%'
  323.         }],
  324.         height: 300,
  325.         width: 700
  326.             ,
  327.         sortable: true
  328.             ,
  329.         multiSort: true
  330.             ,
  331.         autoencode: true,
  332.         pager: "#masterGridPager",
  333.         viewrecords: true // display the number of total records from the query in the pager bar
  334.             ,
  335.         multiselect: true
  336.             ,
  337.         loadComplete: function(data) {
  338.             if (data.rows.length > 0) {
  339.                 for (var id = 1; id <= data.rows.length; id++) {
  340.                     rowData = jqGridBC01020E.getRowData(id);
  341.                     if (rowData.recStatus == null) {
  342.                         continue;
  343.                     }
  344.                     if (rowData.recStatus.indexOf("Inactive") != -1) {
  345.                         jQuery("#jqg_masterGrid_" + id).prop('disabled', true);
  346.                     }
  347.                 }
  348.             }
  349.  
  350.             if (jqGridBC01020E.getGridParam("reccount") == 0) {
  351.                 // count data in grid
  352.                 //var htmlString = $("#reloading").html();
  353.                 if ($("#reloading").html() != "") {
  354.                     $("#reloading").delay(3000).fadeOut();
  355.                     $("#reloading").stop();
  356.                     $("#reloading").fadeIn(0);
  357.                 }
  358.  
  359.                 showMessage("reloading", "warning", "ไม่พบข้อมูลที่ต้องการค้นหา", "");
  360.             } else if (jqGridBC01020E.getGridParam("reccount") != 0) {
  361.                 $("#reloading").html("");
  362.             }
  363.             $("#agentCodeSrh").select();
  364.             $("#agentCodeSrh").focus();
  365.         },
  366.         beforeSelectRow: function(rowid, e) {
  367.             if ($("#jqg_masterGrid_" + rowid).prop("disabled")) {
  368.                 return false;
  369.             } else {
  370.                 return true;
  371.             }
  372.         },
  373.         onSelectAll: function(aRowids, status) {
  374.             if (status) {
  375.                 for (var i = 0; i < aRowids.length; i++) {
  376.                     if ($("#jqg_masterGrid_" + aRowids[i]).prop("disabled")) {
  377.                         $("#jqg_masterGrid_" + aRowids[i]).removeAttr("checked");
  378.                     }
  379.                 }
  380.  
  381.                 //modify the selarrrow parameter
  382.                 jqGridBC01020E[0].p.selarrrow = jqGridBC01020E.find("tr.jqgrow:has(td > input.cbox:checked)")
  383.                     .map(function() {
  384.                         return this.id;
  385.                     }) // convert to set of ids
  386.                     .get(); // convert to instance of Array
  387.             }
  388.         }
  389.     });
  390.  
  391.  
  392.     setTimeout(function() {
  393.         $("#masterGrid").setGridWidth(parseInt($("#form_body").width()) - 20);
  394.     }, 330);
  395.  
  396.     $(window).bind('resize', function() {
  397.         $("#masterGrid").setGridWidth($("#form_body").width() - 20);
  398.     }).trigger('resize');
  399. }
  400.  
  401. function showMessage(vaReload, vaType, vaErrMsg, vaFocus)
  402. {
  403.    var vaAlert = "alert-warning";
  404.     if(vaType == "danger")
  405.     {
  406.         vaAlert = "alert-danger";
  407.     }
  408.     else if(vaType == "success")
  409.     {
  410.         vaAlert = "alert-success";
  411.     }
  412.    
  413.     $("#"+vaReload).html("<div class='alert " + vaAlert +" alert-dismissable' style='text-align: center; margin-bottom: 5px;'>" + "<button type='button' class='close' data-dismiss='alert' aria-hidden='true' style='padding-top: 7px;'>&times;<\/button><b>" + vaErrMsg +" <\/b><\/div>");
  414.    $('html, body').animate({ scrollTop: 0 }, 0);
  415.    
  416.     //setFocus
  417.     if(vaFocus.length > 0)
  418.     {
  419.         if($("#"+vaFocus).prop('tagName') == "SELECT" || $("#"+vaFocus).attr('class').indexOf("select") > -1){
  420.             $("#"+vaFocus).select2("focus");
  421.         }
  422.         else{
  423.             $("#"+vaFocus).focus();
  424.         }
  425.     }
  426. }//showMessage
  427.  
  428. function verify(paAct) {
  429.     if (paAct == 'SRH') {
  430.         searchData();
  431.     } else if (paAct == 'OPNSRH') {
  432.         openBarSearch();
  433.     } else if (paAct == 'ADD') {
  434.         addData();
  435.     } else if (paAct == 'UPD') {
  436.         editData();
  437.     } else if (paAct == 'DEL') {
  438.         confirmDelete();
  439.     } else if (paAct == 'DELMLT') {
  440.         deleteDataMultiple();
  441.     } else if (paAct == 'CLR' || paAct == 'NEW') {
  442.         newData();
  443.     }
  444. }
  445.  
  446. function openBarSearch() {
  447.     $("#barSearch").show();
  448.     $("#gridSearch").show();
  449.     $("#form1").hide();
  450.     if (jqGridBC01020E == '') {
  451.         createGrid();
  452.     }
  453. }
  454.  
  455. function formatStatus(cellvalue, options, rowObject) {
  456.     var desc = '';
  457.     if (cellvalue == '0') {
  458.         desc = '<span style=\'color:red\'>Inactive<\/span>';
  459.     }
  460.     return desc;
  461. }
  462.  
  463. function searchData() {
  464.     $('#act').val('SRH'); // Added for serialize
  465.     jqGridBC01020E.setGridParam({
  466.         url: "../bc/BCAgentInfoServlet?" + $("#frmBC01020E").serialize(),
  467.         page: 1
  468.     });
  469.     jqGridBC01020E.trigger("reloadGrid");
  470. }
  471.  
  472. function deleteData() {
  473.     $("#act").val('DEL');
  474.     $("#frmBC01020E").submit();
  475. }
  476.  
  477. function confirmDelete() {
  478.     $("#modalConfirm").html("คุณต้องการลบข้อมูลนี้หรือไม่");
  479.     $("#modalButton").html("<button type=\"button\" class=\"btn btn-default btn-xs\" data-dismiss=\"modal\"><b>&nbsp;&nbsp;ยกเลิก&nbsp;&nbsp;<\/b><\/button><button type=\"button\" class=\"btn btn-primary btn-xs\" data-dismiss=\"modal\" onclick=\"deleteData();\"><b>&nbsp;&nbsp;ตกลง&nbsp;&nbsp;<\/b><\/button>");
  480.     $('#myModal').modal('show');
  481. }
  482.  
  483. function deleteDataMultiple() {
  484.     var gr = jqGridBC01020E.getGridParam('selarrrow');
  485.  
  486.     var rowData = '';
  487.     var agentCode = new Array();
  488.     for (var i = 0; i < gr.length; i++) {
  489.         rowData = jqGridBC01020E.getRowData(gr[i]);
  490.         agentCode[i] = rowData.agentCode;
  491.     }
  492.     if (gr != "")
  493.         jqGridBC01020E.delGridRow(gr, {
  494.             url: "../bc/BCAgentInfoServlet",
  495.             delData: {
  496.                 act: "DELMLT",
  497.                 agentCode: agentCode.toString()
  498.             },
  499.             reloadAfterSubmit: true,
  500.             afterComplete: function(response, postdata, formid) {
  501.                 if ($("#reloading").html() != "") {
  502.                     $("#reloading").delay(3000).fadeOut();
  503.                     $("#reloading").stop();
  504.                     $("#reloading").fadeIn(0);
  505.                 }
  506.                 //$("#reloading").stop();
  507.                 showMessage("reloading", "success", "ลบข้อมูลเรียบร้อยแล้ว", "");
  508.                 //$("#reloading").html("<div class='alert alert-success alert-dismissable' style='height: 37px; text-align: center; " + "vertical-align:middle; line-height: 30px; padding-top: 3px; margin-bottom: 5px;'>" + "<button type='button' class='close' data-dismiss='alert' aria-hidden='true' style='padding-top: 7px;'>&times;<\/button><label class='control-label'>" + "ลบข้อมูลเรียบร้อยแล้ว<\/label><\/div>");
  509.                 //$("#reloading").fadeIn(0);
  510.                 $("body").scrollTop(0);
  511.                 //$("#reloading").delay(3000).fadeOut();
  512.             },
  513.             errorTextFormat: function(response) {
  514.                 if (response.statusText == "D") {
  515.                     return '<span class="ui-icon ui-icon-alert" ' + 'style="float:left; margin-right:.3em;"><\/span>' + "ไม่สามารถลบข้อมูลได้";
  516.                 } else {
  517.                     return '<span class="ui-icon ui-icon-alert" ' + 'style="float:left; margin-right:.3em;"><\/span>' + response.statusText;
  518.                 }
  519.             }
  520.  
  521.         });
  522.     else {
  523.         $("#modalConfirm").html("กรุณาเลือกข้อมูลที่ต้องการลบ");
  524.         $("#modalButton").html("<button type=\"button\" class=\"btn btn-primary btn-xs\" data-dismiss=\"modal\"><b>&nbsp;&nbsp;ตกลง&nbsp;&nbsp;<\/b>");
  525.  
  526.         $('#myModal').modal('show');
  527.     }
  528. }
  529.  
  530. function getData(id) {
  531.     rowData = jqGridBC01020E.getRowData(id);
  532.     $("#agentCode").val(rowData.agentCode);
  533.     $("#act").val('INQ');
  534.     $("#frmBC01020E").submit();
  535. }
  536.  
  537. function addData() {
  538.     if (validateData()) {
  539.         $("#act").val('ADD');
  540.         $("#frmBC01020E").submit();
  541.     }
  542. }
  543.  
  544. function editData() {
  545.     if (validateData()) {
  546.         $("#act").val('UPD');
  547.         $("#frmBC01020E").submit();
  548.     }
  549. }
  550.  
  551. function addCommas(y) {
  552.     if (y.indexOf(",")) {
  553.         x = y.replace(/,/g, "");
  554.     } else {
  555.         x = y;
  556.     }
  557.     var a;
  558.     var check = x.split(".");
  559.     if ((x.split(".").length > 1) && check[1]) {
  560.         if (check[1] != "" && check[1] != "0") {
  561.             a = parseFloat(x);
  562.             a = Math.round(a * 100) / 100;
  563.         }
  564.     } else {
  565.         a = x;
  566.     }
  567.     var parts = a.toString().split(".");
  568.     parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
  569.     parts = parts.join(".");
  570.     return parts;
  571. }
  572.  
  573. function validateText(x) {
  574.     x = x.replace(/[0-9]/g, "");
  575.     return x;
  576. }
  577.  
  578. function validateDouble(keyCode) {
  579.     var isShift = false;
  580.     if (keyCode == 16) {
  581.         isShift = true;
  582.     }
  583.     if (((keyCode >= 48 && keyCode <= 57) || keyCode == 8 ||
  584.             keyCode <= 37 || keyCode <= 39 || keyCode == 190 ||
  585.             (keyCode >= 96 && keyCode <= 105)) && isShift == false) {
  586.         return true;
  587.     } else {
  588.         return false;
  589.     }
  590. }
  591.  
  592. function isDate(ExpiryDate) {
  593.     var objDate, // date object initialized from the ExpiryDate string
  594.         mSeconds, // ExpiryDate in milliseconds
  595.         day, // day
  596.         month, // month
  597.         year; // year
  598.     // date length should be 10 characters (no more no less)
  599.     if (ExpiryDate.length !== 10) {
  600.         return false;
  601.     }
  602.     // third and sixth character should be '/'
  603.     if (ExpiryDate.substring(2, 3) !== '/' || ExpiryDate.substring(5, 6) !== '/') {
  604.         return false;
  605.     }
  606.     // extract month, day and year from the ExpiryDate (expected format is mm/dd/yyyy)
  607.     // subtraction will cast variables to integer implicitly (needed
  608.     // for !== comparing)
  609.     day = ExpiryDate.substring(0, 2) - 0;
  610.     month = ExpiryDate.substring(3, 5) - 1; // because months in JS start from 0
  611.     year = ExpiryDate.substring(6, 10) - 0;
  612.     // test year range
  613.     if (year < 1000 || year > 3000) {
  614.         return false;
  615.     }
  616.     // convert ExpiryDate to milliseconds
  617.     mSeconds = (new Date(year, month, day)).getTime();
  618.     // initialize Date() object from calculated milliseconds
  619.     objDate = new Date();
  620.     objDate.setTime(mSeconds);
  621.     // compare input date and parts from Date() object
  622.     // if difference exists then date isn't valid
  623.     if (objDate.getFullYear() !== year ||
  624.         objDate.getMonth() !== month ||
  625.         objDate.getDate() !== day) {
  626.         return false;
  627.     }
  628.     // otherwise return true
  629.     return true;
  630. }
  631.  
  632. function validateDate(valDate) {
  633.     var ExpiryDate = valDate;
  634.     // check date and print message
  635.     if (isDate(ExpiryDate)) {
  636.         return true;
  637.     } else {
  638.         return false;
  639.     }
  640. }
  641.  
  642. function validateData() {
  643.     var vbRsl = false;
  644.     var vaErrMsg = "กรุณาระบุ";
  645.     var vaFocus = "";
  646.  
  647.     $("#agentCode").removeClass("has-error");
  648.     $("#agentName").removeClass("has-error");
  649.     $("#startDateDiv").removeClass("has-error");
  650.     $("#finishDateDiv").removeClass("has-error");
  651.         if ($("#agentCode").val() == '') {
  652.             if (vaErrMsg != "กรุณาระบุ") vaErrMsg = vaErrMsg + ",";
  653.             vaErrMsg = vaErrMsg + " รหัสพัสดุสาขา";
  654.             if (vaFocus == "") {
  655.                 vaFocus = "agentCode";
  656.             }
  657.             $("#agentCodeDiv").addClass("has-error");
  658.         }
  659.     if (validateDate($("#contactDate").val()) == false && $("#contactDate").val() != '') {
  660.         if (vaErrMsg != "กรุณาระบุ") vaErrMsg = vaErrMsg + ",";
  661.         vaErrMsg = vaErrMsg + " วันที่ทำสัญญาให้ถูกต้อง";
  662.         if (vaFocus == "") {
  663.             vaFocus = "contactDate";
  664.         }
  665.         $("#contactDateDiv").addClass("has-error");
  666.     }
  667.     if (validateDate($("#expireDate").val()) == false && $("#expireDate").val() != '') {
  668.         if (vaErrMsg != "กรุณาระบุ") vaErrMsg = vaErrMsg + ",";
  669.         vaErrMsg = vaErrMsg + " วันที่สิ้นสุดสัญญาให้ถูกต้อง";
  670.         if (vaFocus == "") {
  671.             vaFocus = "expireDate";
  672.         }
  673.         $("#expireDateDiv").addClass("has-error");
  674.     }
  675.  
  676.     if ($("#contactDate").val() != '' && $("#expireDate").val() != '') {
  677.         var vaDteStr = $("#contactDate").val();
  678.         var vaDteFin = $("#expireDate").val();
  679.         var vnDateDif = calDateDif(vaDteFin, vaDteStr, true);
  680.  
  681.         if (vnDateDif >= 0) {
  682.             if (vaErrMsg != "กรุณาระบุ") vaErrMsg = vaErrMsg + ",";
  683.             vaErrMsg = vaErrMsg + " วันที่สิ้นสุดสัญญา มากกว่า วันที่ทำสัญญา";
  684.             if (vaFocus == "") {
  685.                 vaFocus = "expireDate";
  686.             }
  687.             $("#expireDateDiv").addClass("has-error");
  688.         }
  689.     }
  690.  
  691.     if (vaErrMsg != 'กรุณาระบุ') {
  692.         showMessage("reloading", "warning", vaErrMsg, vaFocus);
  693.     } else {
  694.         vbRsl = true;
  695.     }
  696.  
  697.     return vbRsl;
  698. }
  699.  
  700. function newData() {
  701.     $("#act").val('NEW');
  702.     $("#frmBC01020E").submit();
  703. }
  704.  
  705. function getConfirm(confirmMessage, callback) {
  706.     confirmMessage = confirmMessage || '';
  707.  
  708.     $('#confirmbox').modal({
  709.         show: true,
  710.         backdrop: false,
  711.         keyboard: false
  712.     });
  713.  
  714.     $('#confirmMessage').html(confirmMessage);
  715.     $('#confirmFalse').click(function() {
  716.         $('#confirmbox').modal('hide');
  717.         if (callback) callback(false);
  718.  
  719.     });
  720.     $('#confirmTrue').click(function() {
  721.         $('#confirmbox').modal('hide');
  722.         if (callback) callback(true);
  723.     });
  724. }
  725.  
  726. // Description : Calculate two date and return value date2 - date1
  727. // Parameter : date1 = first date format dd/mm/yyyy
  728. //             date2 = second date format dd/mm/yyyy
  729. //             typeEra = true : Buddish Era and false : Cristian Era               
  730. function calDateDif(date1, date2, typeEra) {
  731.     var datePat = /^(\d{2})\/(\d{2})(\/(\d{4}))?$/;
  732.     var matchArray1 = null;
  733.     var matchArray2 = null;
  734.     if (typeEra == undefined) {
  735.         typeEra = true;
  736.     }
  737.  
  738.     if (date1 != '' && date1 != null && date2 != '' && date2 != null) {
  739.         matchArray1 = date1.match(datePat);
  740.         matchArray2 = date2.match(datePat);
  741.         if (matchArray1 == null || matchArray2 == null) {
  742.             alert("ไม่สามารถเปรียบเทียบค่าได้ เพราะ รูปแบบไม่ถูกต้อง");
  743.         }
  744.         else {
  745.             day1 = matchArray1[1];
  746.             month1 = matchArray1[2];
  747.             year1 = matchArray1[4];
  748.  
  749.             day2 = matchArray2[1];
  750.             month2 = matchArray2[2];
  751.             year2 = matchArray2[4];
  752.  
  753.             if (typeEra) {
  754.                 year1 = year1 - 543;
  755.                 year2 = year2 - 543;
  756.             }
  757.  
  758.             var one_day = 1000 * 60 * 60 * 24
  759.  
  760.             return ((Date.parse(eval(month2) + "/" + day2 + "/" + year2) - Date.parse(eval(month1) + "/" + day1 + "/" + year1))) / (one_day);
  761.         }
  762.     }
  763. }
Advertisement
Add Comment
Please, Sign In to add comment