var base64; var ddls; $(function () { if (!Array.prototype.filter) { Array.prototype.filter = function (fun /*, thisp */) { "use strict"; if (this === null) throw new TypeError(); var t = Object(this); var len = t.length >>> 0; if (typeof fun !== "function") throw new TypeError(); var res = []; var thisp = arguments[1]; for (var i = 0; i < len; i++) { if (i in t) { var val = t[i]; // in case fun mutates this if (fun.call(thisp, val, i, t)) res.push(val); } } return res; }; } //欄位名對應 var fields = { 'cCourseLength': '偏好的課程長度', 'cCourseLength2': '其他課程長度', 'cCoursePeople': '預計上課人數', 'cRegion': '離您最近的三星智慧館', 'cBookStore_ID': '離您最近的三星智慧館', 'cCourseDate': '預計上課日期', 'cCourseTime': '預計上課時間', 'district': '預計上課地點', 'cExpect_Addr': '預計上課地點', 'cUser_Name': '聯絡人姓名', 'cUser_Phone': '聯絡人手機號碼', 'cUser_EMail': '聯絡人E-mail', 'cContact_Time': '方便聯繫的時間', 'cGroup_Age': '學員平均年齡', 'cGroup_Prf': '學員族群(身分)', 'cIsAgree': '我同意 台灣三星 Galaxy Workshop 與我聯繫洽談預約事宜', 'privacy': '我同意個人資料使用同意聲明隱私權條款', 'validCode': '驗證碼' }; //團體 if (id('aBtn')) { $('#twzipcode').twzipcode({ readonly: true }); //時長 $("select[name=cCourseLength]").change(function () { if ($("select[name=cCourseLength] option:last").is(':selected')) { $(".lenBox").removeClass('hide'); $("input[name=cCourseLength2]").attr('required', true); } else { $(".lenBox").addClass('hide'); $("input[name=cCourseLength2]").removeAttr('required').val(''); } }); //上課人數連動 $("select[name=cCoursePeople]").change(function () { var peo = $("select[name=cCoursePeople]").val(); $("select[name=cBookStore_ID],select[name=cCourseDate],select[name=cCourseTime]").find('option:not(:eq(0))').remove(); $("select[name=cRegion] option").removeAttr('selected'); //console.log(t); if (peo !== '') { } }); //區域連動 $("select[name=cRegion]").change(function () { var rgn = $("select[name=cRegion]").val(); var self = $('select[name=cRegion] option:selected').data("s"); var peo = $('select[name=cCoursePeople] option:selected').data("p"); $("select[name=cBookStore_ID],select[name=cCourseDate],select[name=cCourseTime]").find('option:not(:eq(0))').remove(); if (peo == undefined) peo = '01'; if (rgn !== '') { $('select[name=cBookStore_Temp] option[data-r=' + rgn + '][data-p*=' + peo + ']').clone().appendTo('select[name=cBookStore_ID]'); if (self == 'B') { $("select[name=cBookStore_ID] option:last").attr("selected", "selected").trigger('change'); } else { if ($('#ask').is(':checked')) { $('#ask').val('').trigger('click'); } } } }); $('#ask').click(function () { if ($(this).is(':checked')) { $('.adrBox').removeClass('hide'); $('select[name=district],input[name=cExpect_Addr]').attr('required', true); if ($("select[name=cRegion]").val() !== undefined) { $("select[name=cRegion] option[data-s='B']").prop('selected', true); $("select[name=cRegion]").trigger('change'); } } else { $('.adrBox').addClass('hide'); $('select[name=district],input[name=cExpect_Addr]').removeAttr('required'); if ($('select[name=cRegion] option:selected').data("s") != 'A') { $("select[name=cRegion] option").removeAttr('selected'); $("select[name=cBookStore_ID],select[name=cCourseDate],select[name=cCourseTime]").find('option:not(:eq(0))').remove(); } } }); //門市連動 $("select[name=cBookStore_ID]").change(function () { var sto = $("select[name=cBookStore_ID]").val(); var self = $('select[name=cBookStore_ID] option:selected').data("s"); $("select[name=cCourseDate],select[name=cCourseTime]").find('option:not(:eq(0))').remove(); if (sto !== '') { $('select[name=cCourseDate_Temp] option[data-s=' + sto + ']').clone().appendTo('select[name=cCourseDate]'); //console.log($("select[name=cCourseDate]").find("option").length); if (self == 'B') {//自定 $('#ask').val('1').trigger('click'); } else { } } }); //日期連動 $("select[name=cCourseDate]").change(function () { var sto = $("select[name=cBookStore_ID]").val(); var sld = $("select[name=cCourseDate]").val(); $("select[name=cCourseTime]").find('option:not(:eq(0))').remove(); //console.log(sto); //console.log(sld); if (sld !== '') { $("select[name=cCourseTime_Temp] option[data-s='" + sto + "'][data-d='" + sld + "']").clone().appendTo('select[name=cCourseTime]'); } }); $("#aBtn").click(function (e) { // console.log('aaa'); if (!checkEmpty() || !checkFormat()) { e.preventDefault(); e.stopPropagation(); return false; } else { $('html').addClass('loading'); $("input[name=mode]").val('G'); $("input[name=ls]").val($('select[name=cCourseTime] option:selected').data("l")); $.post("api/ajax_group.ashx", $("#form1").serialize(), function (data) { $('html').removeClass('loading'); if (data.status < 0) { alert(data.message); } else { $('section.secGroup').addClass('hide'); $('section.secGroup2').removeClass('hide'); $('#tmpBox').empty().html(data.objectz); } }, "json"); } return false; }); $("#rBtn").click(function (e) { $('section.secGroup').addClass('hide'); $('section.secGroup1').removeClass('hide'); $('#tmpBox').empty(); return false; }); $("#bBtn").click(function (e) { //確認後送出 //console.log('bbb'); var url = $(this).data("href"); if (!checkEmpty() || !checkFormat()) { e.preventDefault(); e.stopPropagation(); return false; } else { $('html').addClass('loading'); $("input[name=mode]").val('H'); $("input[name=ls]").val($('select[name=cCourseTime] option:selected').data("l")); $.post("api/ajax_group.ashx", $("#form1").serialize(), function (data) { $('html').removeClass('loading'); if (data.status < 0) { alert(data.message); } else { if (url.indexOf('#') > 0) { location.replace(url.replace(/#/, "?uo=" + encodeURIComponent(data.objectz) + "#")); } else { location.replace(url + "?uo=" + encodeURIComponent(data.objectz)); } } }, "json"); } return false; }); } //step3 確認後送出 if (id('cBtn')) { $("#cBtn").click(function (e) { if (!confirm('請再次確認您的報名資訊')) { return false; } var url = $(this).data("href"); if (!checkEmpty() || !checkFormat()) { e.preventDefault(); e.stopPropagation(); return false; } else { $('html').addClass('loading'); $.post("api/ajax_group.ashx", $("#form1").serialize(), function (data) { if (data.status < 0) { setTimeout(function () { alert(data.message); // 延遲一些顯示 }, 500); $('html').removeClass('loading'); } else { if (url.indexOf('#') > 0) { location.replace(url.replace(/#/, "?uo=" + encodeURIComponent(data.objectz) + "#")); } else { location.replace(url + "?uo=" + encodeURIComponent(data.objectz)); } } }, "json"); } return false; }); } //查詢 if (id('sBtn')) { $("#sBtn").click(function (e) { var url = $(this).data("href"); if (!checkEmpty() || !checkFormat()) { e.preventDefault(); e.stopPropagation(); return false; } else { $('html').addClass('loading'); $.post("api/ajax_group.ashx", $("#form1").serialize(), function (data) { if (data.status < 0) { alert(data.message); $('html').removeClass('loading'); } else { location.replace(url); } }, "json"); } return false; }); } //團體課程頁面 $(".searchSlick a").click(function () { var g = $(this).data('con'); $(".secGroup .box > div").addClass("hide"); $(".secGroup .box > div[data-con*='" + g + "']").removeAttr("class"); return false; }); //檢查格式 function checkFormat() { var message = ''; obj = ''; $('input[data-validate],textarea[data-validate]').each(function (index, element) { // new RegExp('ab+c', 'i'); var regexp = new RegExp($(element).data('validate'), 'i'); if (element.name === 'cOrder_SN' && $(element).data('validate') === 'X') { regexp = new RegExp($('select[name=cChannel_ID] option:selected').data("g"), 'i'); } if (element.name === 'cProduct_SN' && $(element).data('validate') === 'X') { regexp = new RegExp("(\\d|\\w){" + $('select[name=cProductModel_ID] option:selected').data("i") + "}", 'i'); console.log($(element).val()); console.log(regexp); } var value = $(element).val(); if (value !== '') { if (!regexp.test(value)) { message = fields[element.name] + '的格式不正確,請重新輸入'; obj = element.name; return false; } } }); if (message !== '') { $("[name=" + obj + "]").focus(); alert(message); message = ''; return false; } else { return true; } } //檢查未填的欄位 function checkEmpty() { var flag = true; var obj = '', message = ''; $('input[required]:not([type=radio]):not([type=checkbox]),select[required],textarea[required]').each(function (index, element) { if ($(element).val() === '') { if (element.tagName.toLowerCase() === 'select') { message = '請選擇' + fields[element.name]; } else { if (fields[element.name] === undefined) { message = '請填寫' + $(element).attr("placeholder"); } else { message = '請填寫' + fields[element.name]; } } obj = element.name; flag = false; return false; } }); if (flag) { $('[type=radio][required],[type=checkbox][required]').each(function (index, element) { var iptn = $(element).attr('type') === 'checkbox' ? '請勾選' : '請選擇'; var group = $(element).attr('name'); //console.log(ipt); if ($('[name=' + group + ']:checked').length === 0) { if (fields[group] === undefined) { message = iptn + ' [' + $(element).next().text() + ' ]'; } else { message = iptn + fields[group]; } obj = group; } }); } if (message !== '') { $("[name=" + obj + "]").focus(); alert(message); message = ''; return false; } else { return true; } } function readUrl(input) { if (input.files && input.files[0]) { var reader = new FileReader(); reader.onload = function (e) { //var imgData = e.target.result; var imgName = input.files[0].name; input.setAttribute("data-title", imgName); // console.log(e.target.result); var image = new Image(); //image.src = e.target.result; getImgData(e.target.result, function (data) { //这里可以使用校正后的图片data了 $('input[name=cUser_Base64]').val(data); image.src = data; image.onload = function () { // access image size here if (id('mBtn')) { //手機版 $('#mBtn').trigger('click'); } else { $(".upload-zone").removeClass('uploaded').addClass('uploaded'); $('.preview-image').attr("src", data); } //$(image).appendTo($('.inputDnD')); //$('.inputDnD').height(image.height); }; }); }; reader.readAsDataURL(input.files[0]); } } window.readUrl = readUrl; function getImgData(img, next) { var image = new Image(); image.onload = function () { var degree = 0, drawWidth, drawHeight; var maxWidth = 2000; drawWidth = this.naturalWidth; drawHeight = this.naturalHeight; //以下改变一下图片大小 var maxSide = Math.max(drawWidth, drawHeight); if (maxSide > maxWidth) { var minSide = Math.min(drawWidth, drawHeight); minSide = minSide / maxSide * maxWidth; maxSide = maxWidth; if (drawWidth > drawHeight) { drawWidth = maxSide; drawHeight = minSide; } else { drawWidth = minSide; drawHeight = maxSide; } } var canvas = document.createElement('canvas'); canvas.width = drawWidth; canvas.height = drawHeight; var context = canvas.getContext('2d'); //context.rotate(degree * Math.PI / 180); context.drawImage(this, 0, 0, drawWidth, drawHeight); //返回校正图片 next(canvas.toDataURL("image/jpeg", .8)); } image.src = img; } }); //----------------------------其它--------------------------------------------------------------------- var putVoucher = function (img) { var image = new Image(); image.src = img; image.onload = function () { // access image size here $(".upload-zone").removeClass('uploaded').addClass('uploaded'); $('.preview-image').attr("src", img); }; } //----------------------------必備--------------------------------------------------------------------- function keyEsc() { var esc = $.Event("keydown", { keyCode: 27 }); $("body").trigger(esc); }; function keyResend() { if (id('qBtn')) { $("#qBtn").trigger('click'); } var esc = $.Event("keydown", { keyCode: 27 }); $("body").trigger(esc); }; function id(DOM) { return document.getElementById(DOM); } function getParam(name, dvalue) { var AllVars = window.location.search.substring(1); var Vars = AllVars.split("&"); for (i = 0; i < Vars.length; i++) { var Var = Vars[i].split("="); if (Var[0] === name) return Var[1]; } return dvalue; } function trims(s) { //return s.replace(/(^[\s]*)|([\s]*$)/g, ""); return s.replace(/\s+/g, ""); }