function CheckEmail(mail) { var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; if (mail.length == 0) return true; return filter.test(mail); } function showContactList(data) { var rows = []; for (var i = 0; i < data.length; i++) { var r = data[i]; for (var j in r) { var a = r[j]; } r.Text = false; r.Email = false; var fr = { Values: r }; rows.push(fr); } grid_maildt.setData(rows); } var grid_maildt; function InitEmailGridData() { grid_maildt = new GridView('#contactlist'); grid_maildt.lang = { all: GetTextByKey("P_GRID_ALL", "(All)"), ok: GetTextByKey("P_GRID_OK", "OK"), reset: GetTextByKey("P_GRID_RESET", "Reset") }; var list_columns = [ { name: 'DisplayName', caption: GetTextByKey("P_MV_CONTACTNAME", "Contact Name"), valueIndex: 'DisplayName', css: { 'width': 148, 'text-align': 'left' } }, { name: 'ContactTypeName', caption: GetTextByKey("P_MV_CONTACTTYPE", "Contact Type"), valueIndex: 'ContactTypeName', css: { 'width': 148, 'text-align': 'left' } }, //{ name: 'Text', caption: GetTextByKey("P_MV_TEXT", "Text"), valueIndex: 'Text', type: 3, css: { 'width': 45, 'text-align': 'center' } }, { name: 'Email', caption: GetTextByKey("P_MV_EMAIL", "Email"), valueIndex: 'Email', type: 3, css: { 'width': 60, 'text-align': 'center' } } ]; var columns = []; // head for (var hd in list_columns) { var col = {}; col.name = list_columns[hd].name; col.caption = list_columns[hd].caption; col.visible = true; col.sortable = true; col.width = list_columns[hd].css.width; col.align = list_columns[hd].css["text-align"] col.key = list_columns[hd].valueIndex; if (list_columns[hd].type) { col.type = list_columns[hd].type; } columns.push(col); if (col.name === "Text") { col.enabled = function (item) { return item.TextAddress !== ''; }; } if (col.name === "Email") { col.enabled = function (item) { return item.ID !== ''; }; } } grid_maildt.canMultiSelect = false; grid_maildt.columns = columns; grid_maildt.init(); grid_maildt.selectedrowchanged = function (rowindex) { var rowdata = grid_maildt.source[rowindex]; if (rowdata) { } } } function getDispatchContacts(assetids) { var ctr = $("#selContractor").val(); if (!ctr) ctr = ""; var p = ctr + ";"; var ids = []; if (_sendType == 0 || _sendType == 2) { ids = assetids; } else { for (var i = 0; i < grid_dt.source.length; i++) { var ct = grid_dt.source[i].Values; if (ct.Selected && ids.indexOf(ct.AssetId) < 0) ids.push(ct.AssetId); } } jobsitequery("GetDispatchContacts", JSON.stringify(ids), function (data) { if (typeof (data) !== "string") showContactList(data); }, function () { }); } var _selectdispatchids = []; var _assignto; var aftersend = null; function openSendEmails(assignto, assetids, next) { _assignto = ""; if (assignto) _assignto = assignto; aftersend = next; var title = GetTextByKey("P_JS_SENDDISPATCHREQUEST", 'Send Dispatch Request'); var aids = []; if (_sendType == 0) aids = assetids; else if (_sendType == 1) { aids = _selectdispatchids; if (aids.length == 0) { showAlert(GetTextByKey('P_JS_PLEASESELECTDISPATCH', 'Please select Dispatch.'), title); return; } } else if (_sendType == 2) aids = _selectdispatchids; getDispatchContacts(aids); $('#sendmail_otheremailaddress').val(''); $('#sendmail_othertextaddress').val(''); $('#sendmail_desc').val('Please make the pick-ups/deliveries as indicated.'); $('#dialog_sendmail .dialog-title span.title').text(title); $('#dialog_sendmail') .attr('act', 'edit') .css({ 'width': 500, 'top': (document.documentElement.clientHeight - $('#dialog_sendmail').height()) / 4, 'left': (document.documentElement.clientWidth - $('#dialog_sendmail').width()) / 2 }) .showDialogfixed(); } function onSendEmails() { var alerttitle = GetTextByKey("P_WO_SENDEMAIL", 'Send Email'); var ids = []; if (_sendType == 0) { ids = _createrequireids; } else { ids = _selectdispatchids; } if (ids.length == 0) return; var emailaddress = []; var textaddress = []; var otheremailaddressstr = $('#sendmail_otheremailaddress').val(); if (otheremailaddressstr !== "") { var address = otheremailaddressstr.split(';'); for (var i = 0; i < address.length; i++) { if (!CheckEmail($.trim(address[i]))) { showAlert(GetTextByKey("P_MV_OTHEREMAILADDRESSISINVALID", 'The other email address {0} is invalid.').replace('{0}', emailaddress[i]), alerttitle); return; } emailaddress.push({ 'Key': '', 'Value': address[i] }); } } var othertextaddressstr = $('#sendmail_othertextaddress').val(); if (othertextaddressstr !== "") { var address = othertextaddressstr.split(';'); for (var i = 0; i < address.length; i++) { if (!CheckEmail($.trim(address[i]))) { showAlert(GetTextByKey("P_MV_OTHERTEXTADDRESSISINVALID", 'The other text address {0} is invalid.').replace('{0}', emailaddress[i]), alerttitle); return; } textaddress.push({ 'Key': '', 'Value': address[i] }); } } for (var i = 0; i < grid_maildt.source.length; i++) { var ct = grid_maildt.source[i].Values; if (ct.Email) { emailaddress.push({ 'Key': ct.IID, 'Value': ct.ID }); } if (ct.Text) { textaddress.push({ 'Key': ct.IID, 'Value': ct.TextAddress }); } } if (emailaddress.length == 0 && textaddress.length == 0) { $('#dialog_sendmail').hideDialog(); showmaskbg(false); return; } var item = { 'Type': _sendType, 'ObjectIDs': ids, 'Description': $('#sendmail_desc').val(), 'EmailAddress': emailaddress, 'TextAddress': textaddress, 'AssignTo': _assignto }; jobsitequery('SendEmails', htmlencode(JSON.stringify(item)), function (data) { if (_sendType == 0) { $('#dialog_sendmail').hideDialog(); showmaskbg(false); OnExit(data); } else { if (data !== 'OK') showAlert(data, alerttitle); else showAlert(GetTextByKey("P_WO_MESSAGESENT", 'Message sent'), alerttitle); $('#dialog_sendmail').hideDialog(); showmaskbg(false); } if (aftersend) aftersend(); }); }