NRS_VERSION = 2; VOLUME_DIVIDE_FACTOR =5000; VOLUME_DIVIDE_FACTOR_DOMESTIC = 6000; VOL_SERVICE_ID_LIST = ['3280', //พัสดุไปรษณีย์ทางภาคพื้น 1 '362061', //พัสดุไปรษณีย์ทางภาคพื้น 2 '3301',// LOGISPOST WORLD '356120',//Courier Post (Merchandise) 1 '363224',//Courier Post (Merchandise) 2 '363226',//Courier Post (Merchandise) 3 '356121',//Courier Post (Document) 1 '363222',//Courier Post (Document) 2 '363223',//Courier Post (Document) 3 ]; function isVolumeService(service_id){ // console.log('checking '+service_id + ' = '+VOL_SERVICE_ID_LIST.indexOf(service_id)); if(service_id =='') return false; if(VOL_SERVICE_ID_LIST.indexOf(service_id) >=0) return true; else return false; } function number_format(number, decimals, dec_point, thousands_sep) { number = (number + "").replace(',', '') number = parseFloat(number); if (isNaN(number)) number = 0; var nstr = number.toString(); nstr += ''; x = nstr.split('.'); x1 = x[0]; x2 = (x.length > 1 ? dec_point + x[1] : dec_point) + "00"; var rgx = /(\d+)(\d{3})/; while (rgx.test(x1)) x1 = x1.replace(rgx, '$1' + thousands_sep + '$2'); return x1 + x2.substring(0, decimals + 1); } function updateVolWeight(VOLUME_DIVIDE_FACTOR) { $(".divWarningRequired").hide(); if($("[name=parcel_width]").val()){ var weight = $("[name=parcel_weight]").val().replace(",", ""); var width = $("[name=parcel_width]").val().replace(",", ""); var length = $("[name=parcel_length]").val().replace(",", ""); var height = $("[name=parcel_height]").val().replace(",", ""); } if(NRS_VERSION >=2 ){ // if (weight == "0.00") { // $("label[for=parcel_weight]").css("color", "red"); // $(".divWarningRequired").show(); // } else // $("label[for=parcel_weight]").css("color", "black"); // // // // if (width == "0.00") { // $("label[for=parcel_width]").css("color", "red"); // $(".divWarningRequired").show(); // } else // $("label[for=parcel_width]").css("color", "black"); // // // // if (length == "0.00") { // $("label[for=parcel_length]").css("color", "red"); // $(".divWarningRequired").show(); // } else // $("label[for=parcel_length]").css("color", "black"); // // // if (height == "0.00") { // $("label[for=parcel_height]").css("color", "red"); // $(".divWarningRequired").show(); // } else // $("label[for=parcel_height]").css("color", "black"); } var format_vol_weight = (width * length * height) / VOLUME_DIVIDE_FACTOR; format_vol_weight = number_format(format_vol_weight, 2, '.', ','); console.log("vol weight (kg) = "+ format_vol_weight); $("[name=parcel_vol_weight]").val(format_vol_weight); // debugger; } //1.1 change onle weight division factore , no additional validation and information function initVolWeightForm(factor) { if(NRS_VERSION < 2 ){ console.log("Hide v2 feature"); $('.v2').hide(); } else $('.v1').hide(); if(typeof(factor) == "undefined") factor = VOLUME_DIVIDE_FACTOR; $(".numonly") .keypress(function (event) { if ((event.which < 48 || event.which > 57) && event.which != 46 && event.which != 44) { event.preventDefault(); } }) .focus(function () { if($(this).val()=="0.00") $(this).val(''); }) .blur(function () { $(this).val(number_format($(this).val(), 2, '.', ',')); updateVolWeight(factor); }); $(".required_fld").blur(function(){ validateform() }); updateVolWeight(factor); $(".form").submit(function( event ) { if(!validateform()) event.preventDefault(); }); } function validateform(){ var result = true; if(NRS_VERSION >=2 ){ $(".required_fld").each(function(){ var label_selector = "label[for="+ $(this).attr("name") +"]"; if($(this).prop("tagName") == "INPUT") $(this).val(number_format($(this).val(), 2, '.', ',')); if ($(this).val() == null || $(this).val() == "0.00") { $(label_selector).css("color", "red"); result = false; } else $(label_selector).css("color", "black"); }); if(result) $(".divWarningRequired").hide(); else $(".divWarningRequired").show(); } return result; }