var width = ""; var position = ""; var dropdown_class = ""; var logout_link = ""; /* logout link */ var div_load = "swkid_plugin"; //function to generate a random state parameter function generateState() { return Math.random().toString(36).substring(7); } function swkid_sso_init(objSetting){ if(objSetting.display_div != '' && typeof objSetting.display_div !== 'undefined'){ div_load = objSetting.display_div; } if(document.getElementById(div_load)){ skwid_sso_before_login(objSetting); } login_form_build(objSetting); swkid_switch_account_form_build(objSetting); if (typeof swkid_callback === "function") { var usr_short_name = ""; var login_by = ""; var swkid_login_status = "before_swkid_login"; var swkid_obj = { usr_short_name: usr_short_name, login_by: login_by, swkid_login_status: swkid_login_status }; swkid_callback(swkid_obj); } } function skwid_sso_after_login(objSetting){ if(objSetting.icon_width!=''){ width = objSetting.icon_width; }else{ width = '25'; } position = objSetting.position; logout_redirect_uri = objSetting.logout_redirect_uri; if(position == "top-left"){ dropdown_class = "swkid-drop-down-top-left"; }else if(position == "bottom-left"){ dropdown_class = "swkid-drop-down-bottom-left"; }else if(position == "bottom-right"){ dropdown_class = "swkid-drop-down-bottom-right"; }else{ dropdown_class = "swkid-drop-down-top-right"; } if(objSetting.display_name=='on' && typeof objSetting.display_name !== 'undefined'){ var x = new Date() if(x.getHours()>4 && x.getHours()<12){ var greetings = 'Good Morning, '; }else if(x.getHours()>11 && x.getHours()<17){ var greetings = 'Good Afternoon, '; }else{ var greetings = 'Good Evening, '; } display_name = ""+greetings+" ..."; }else{ display_name = ''; } /*Add Main SWKID Button Div*/ var main_div = document.createElement("div"); profile_listing_style = objSetting.profile_listing_style; if(profile_listing_style=='seg'){ main_div.style.cssText = "position:relative;height:"+width+"px;display:flex;align-items:center;"; }else{ main_div.style.cssText = "width:"+width+"px;position:relative;height:"+width+"px;display:flex;align-items:center;"; } document.getElementById(div_load).appendChild(main_div); myworkspace_newtab = objSetting.myworkspace_newtab; if(myworkspace_newtab=='off'){ var v_myworkspace_profile_newtab="javascript:window.location.href=\'https://myworkspace.sarawak.gov.my/workspace/pub/my_workspace/my_profile/?l=en\';"; var v_myworkspace_newtab="javascript:window.location.href=\'https://myworkspace.sarawak.gov.my/workspace/pub/my_workspace/redirect/?l=en\';"; }else{ var v_myworkspace_profile_newtab="javascript:window.open(\'https://myworkspace.sarawak.gov.my/workspace/pub/my_workspace/my_profile/?l=en\');"; var v_myworkspace_newtab="javascript:window.open(\'https://myworkspace.sarawak.gov.my/workspace/pub/my_workspace/redirect/?l=en\');"; } payment_newtab = objSetting.payment_newtab; if(payment_newtab=='off'){ var v_payment_newtab="javascript:window.location.href=\'https://service.sarawak.gov.my/web/pub/public/enq_n_pay/340/\';"; }else{ var v_payment_newtab="javascript:window.open(\'https://service.sarawak.gov.my/web/pub/public/enq_n_pay/340/\');"; } switch_newtab = objSetting.switch_newtab; if(switch_newtab=='off'){ var v_switch_newtab="javascript:window.location.href= \'https://sarawakid.sarawak.gov.my/web/ssov1/switchaccountpb/\';"; }else{ var v_switch_newtab="javascript:window.location.href= \'https://sarawakid.sarawak.gov.my/web/ssov1/switchaccountpb/\';"; } if(profile_listing_style=='details'){ var html = '
'+ display_name+ ''+ ''+ ''; }else if(profile_listing_style=='seg'){ var html = ''+ ''+ // '
'+ // ''+ // '
'+ // 'Logout'+ // '
'+ // '
'+ '
'+ '
'+ '
'+ '

Switch to business workspace?

'+ '
'+ '
'+ '
'+ '

Please key in Business Registration Number

'+ // ""+ ''+ '
'+ '
'+ ''+ '
'+ '
'+ '
'+ '
'+ '
'+ '

Switch to business workspace?

'+ '
'+ '
'+ '

Please enter the OTP sent to your email

'+ ''+ '
'+ ''+ '
'+ '
'+ ''+ ''; }else{ var html = ''+ display_name+ ''+ ''+ ''; } main_div.innerHTML = html; } function profile_onclick() { var ul = document.getElementById('swkid-plugin-ul'); if(document.getElementById('sso-bar-lang-ul')){document.getElementById('sso-bar-lang-ul').style.display = 'none';} if (ul.style.display != 'block') { ul.style.display = 'block'; } else { ul.style.display = 'none'; } } function switch_acc(){ var modal = document.getElementById('swkid-myModal'); modal.style.display = "block"; } window.onclick = function(event) { if (event.target == document.getElementById('swkid-myModal')) { document.getElementById('swkid-myModal').style.display = "none"; } } function get_otp(){ var data = new FormData(); var temp_id = ""; data.append('temp_id', temp_id); $.ajax({ type:'POST', url:'https://sarawakid.sarawak.gov.my/web/ssov1/switchaccountpb/', dataType: "json", data: data, cache: false, processData: false, contentType: false, success: function(resp){ if (resp == "fail") { // alert("Failed to load secure code and otp. Please try again."); $("#general-modal-title4").text('Alert'); $("#general-content-container4").text('Failed to load secure code and otp. Please try again.'); $("#general-modal4").modal('show'); } else { // console.log('sampai loadOTP success ajax!'); sendOTP(resp[0]); var modal = document.getElementById('myModal'); var modal2 = document.getElementById('myModal2'); modal.style.display = "none"; modal2.style.display = "block"; } }, error: function(resp){ console.log('ajax error'); } }); } function swkid_logout(path, params) { var form = document.createElement("form"); form.setAttribute("id", "swkid_logout_form"); form.setAttribute("method", "post"); form.setAttribute("action", path); for(var key in params) { if(params.hasOwnProperty(key)) { var hiddenField = document.createElement("input"); hiddenField.setAttribute("type", "hidden"); hiddenField.setAttribute("name", key); hiddenField.setAttribute("value", params[key]); form.appendChild(hiddenField); } } document.body.appendChild(form); form.submit(); } function skwid_sso_before_login(objSetting){ width = objSetting.icon_width; if(objSetting.icon_width!=''){ width = objSetting.icon_width; }else{ width = '25'; } width = '17'; position = objSetting.position; if(position == "top-left"){ dropdown_class = "swkid-drop-down-top-left"; }else if(position == "bottom-left"){ dropdown_class = "swkid-drop-down-bottom-left"; }else if(position == "bottom-right"){ dropdown_class = "swkid-drop-down-bottom-right"; }else{ dropdown_class = "swkid-drop-down-top-right"; } style = objSetting.style; var main_div = document.createElement("div"); if(style == "icon-only"){ main_div.style.cssText = "width:"+width+"px;position:relative;height:100%;"; }else{ main_div.style.cssText = "width:auto;position:relative;height:100%;"; } document.getElementById(div_load).appendChild(main_div); var html=''; var icon_display='none'; if(style == "icon-text-old"){ html = '
'+ ''+ 'SarawakID'+ '
Register
'+ '
Login
'+ '
'; }else if(style == "icon-only"){ html = ''+ ''; }else if(style == "text-only"){ html = '
'+ ''+ 'Login with SarawakID'+ ''+ ''+ 'Login with SID'+ ''+ '
'; }else if(style == "icon-full-text"){ html = '
'+ ''+ ''+ ''+ ''+ 'Login with SarawakID'+ ''+ ''+ 'Login with SID'+ ''+ '
'; icon_display = 'flex'; }else if(style == "logo-text"){ html = '
'+ ''+ ''+ ''+ ''+ 'Register'+ ''+ ''+ 'Login'+ ''+ '
'; icon_display = 'flex'; }else{ html = '
'+ ''+ ' '+ 'SarawakID'+ ''+ ''+ 'Login'+ ''+ ''+ 'Register'+ ''+ '
'; icon_display = 'flex'; } html+=''+ ''; main_div.innerHTML = html; } function profile_onclick(){ var ul = document.getElementById('swkid-plugin-ul'); if(document.getElementById('sso-bar-lang-ul')){document.getElementById('sso-bar-lang-ul').style.display = 'none';} if (ul.style.display != 'block') { ul.style.display = 'block'; } else { ul.style.display = 'none'; } } function login_form_build(objSetting){ var form = document.createElement("form"); form.setAttribute("id", "swkid_login_form"); form.setAttribute("method", "post"); form.setAttribute("action", "https://sarawakid.sarawak.gov.my/web/ssov1/login/"); var hiddenField = document.createElement("input"); hiddenField.setAttribute("type", "hidden"); hiddenField.setAttribute("name", "client_id"); hiddenField.setAttribute("value", objSetting.client_id); form.appendChild(hiddenField); var hiddenField = document.createElement("input"); hiddenField.setAttribute("type", "hidden"); hiddenField.setAttribute("name", "u_id"); hiddenField.setAttribute("value", objSetting.u_id); form.appendChild(hiddenField); var hiddenField = document.createElement("input"); hiddenField.setAttribute("type", "hidden"); hiddenField.setAttribute("name", "state"); hiddenField.setAttribute("value", objSetting.state); form.appendChild(hiddenField); var hiddenField = document.createElement("input"); hiddenField.setAttribute("type", "hidden"); hiddenField.setAttribute("name", "response_type"); hiddenField.setAttribute("value", objSetting.response_type); form.appendChild(hiddenField); var hiddenField = document.createElement("input"); hiddenField.setAttribute("type", "hidden"); hiddenField.setAttribute("name", "redirect_uri"); hiddenField.setAttribute("value", objSetting.redirect_uri); form.appendChild(hiddenField); if(objSetting.servicelisting_uri){ var hiddenField = document.createElement("input"); hiddenField.setAttribute("type", "hidden"); hiddenField.setAttribute("name", "servicelisting_uri"); hiddenField.setAttribute("value", objSetting.servicelisting_uri); form.appendChild(hiddenField); } var hiddenField = document.createElement("input"); hiddenField.setAttribute("type", "hidden"); hiddenField.setAttribute("name", "logout_uri"); hiddenField.setAttribute("value", objSetting.logout_uri); form.appendChild(hiddenField); var hiddenField = document.createElement("input"); hiddenField.setAttribute("type", "hidden"); hiddenField.setAttribute("name", "misc_param"); hiddenField.setAttribute("value", objSetting.misc_param); form.appendChild(hiddenField); var hiddenField = document.createElement("input"); hiddenField.setAttribute("type", "hidden"); hiddenField.setAttribute("name", "hide_header"); hiddenField.setAttribute("value", objSetting.hide_header); form.appendChild(hiddenField); var hiddenField = document.createElement("input"); hiddenField.setAttribute("type", "hidden"); hiddenField.setAttribute("name", "permission"); hiddenField.setAttribute("value", objSetting.permission); form.appendChild(hiddenField); var hiddenField = document.createElement("input"); hiddenField.setAttribute("type", "hidden"); hiddenField.setAttribute("name", "sub_login_app_name"); hiddenField.setAttribute("value", objSetting.sub_login_app_name); form.appendChild(hiddenField); var hiddenField = document.createElement("input"); hiddenField.setAttribute("type", "hidden"); hiddenField.setAttribute("name", "login_method"); hiddenField.setAttribute("value", objSetting.login_method); form.appendChild(hiddenField); var hiddenField = document.createElement("input"); hiddenField.setAttribute("type", "hidden"); hiddenField.setAttribute("name", "hide_login_extra_link"); hiddenField.setAttribute("value", objSetting.hide_login_extra_link); form.appendChild(hiddenField); var hiddenField = document.createElement("input"); hiddenField.setAttribute("type", "hidden"); hiddenField.setAttribute("name", "ekyc_redirect"); hiddenField.setAttribute("value", objSetting.ekyc_redirect); form.appendChild(hiddenField); var hiddenField = document.createElement("input"); hiddenField.setAttribute("type", "hidden"); hiddenField.setAttribute("name", "ekyc_redirect_post_uri"); hiddenField.setAttribute("value", objSetting.ekyc_redirect_post_uri); form.appendChild(hiddenField); var hiddenField = document.createElement("input"); hiddenField.setAttribute("type", "hidden"); hiddenField.setAttribute("name", "corp_only"); hiddenField.setAttribute("value", objSetting.corp_only); form.appendChild(hiddenField); var hiddenField = document.createElement("input"); hiddenField.setAttribute("type", "hidden"); hiddenField.setAttribute("name", "switch_account"); hiddenField.setAttribute("value", objSetting.switch_account); form.appendChild(hiddenField); document.body.appendChild(form); } function login_form_submit(){ var form = document.getElementById('swkid_login_form'); form.submit(); } function swkid_login_form_submit(){ login_form_submit(); } function swkid_switch_account_form_build(objSetting) { var form = document.createElement("form"); form.setAttribute("id", "swkid_switch_account"); form.setAttribute("method", "post"); form.setAttribute("action", "https://sarawakid.sarawak.gov.my/web/ssov1/switchaccountpb/"); var hiddenField = document.createElement("input"); hiddenField.setAttribute("type", "hidden"); hiddenField.setAttribute("name", "client_id"); hiddenField.setAttribute("value", objSetting.client_id); form.appendChild(hiddenField); var hiddenField = document.createElement("input"); hiddenField.setAttribute("type", "hidden"); hiddenField.setAttribute("name", "switch_account_redirect_uri"); hiddenField.setAttribute("value", objSetting.switch_account_redirect_uri); form.appendChild(hiddenField); document.body.appendChild(form); } function swkid_switch_account_form_submit(){ var form = document.getElementById('swkid_switch_account'); form.submit(); } function addScriptWithPost(url, params, obj1) { var head= document.getElementsByTagName('head')[0]; // Remove the old form if it exists const oldFrm = document.getElementById('swkid_tk_form'); if (oldFrm) { oldFrm.parentNode.removeChild(oldFrm); } // Create a form element const form = document.createElement('form'); form.id = 'swkid_tk_form'; form.method = 'POST'; form.action = url; form.style.display = 'none'; // Hide the form // Append parameters as hidden inputs for (const key in params) { if (params.hasOwnProperty(key)) { const input = document.createElement('input'); input.type = 'hidden'; input.name = key; input.value = params[key]; form.appendChild(input); } } // Append the form to the body head.appendChild(form); const oldiFrm = document.getElementById('swkid_tk_iframe'); if (oldiFrm) { oldiFrm.parentNode.removeChild(oldiFrm); } // Create an iframe to target the form submission const iframe = document.createElement('iframe'); iframe.id = 'swkid_tk_iframe'; iframe.name = 'postFrame'; iframe.style.display = 'none'; head.appendChild(iframe); // Set the form target to the iframe form.target = 'postFrame'; // Submit the form form.submit(); // Add an event listener to the iframe to load the script once form is submitted iframe.onload = function() { // Remove the old script if it exists const oldScript = document.getElementById('swk_id_script'); if (oldScript) { oldScript.parentNode.removeChild(oldScript); } const script = document.createElement('script'); script.src = url; script.id= 'swk_id_script'; script.async = true; script.onload=function() { swkid_sso_init_after(obj1); }; head.appendChild(script); }; } function getCookie(cname) { if(navigator.cookieEnabled){ let name = cname + "="; let decodedCookie = decodeURIComponent(document.cookie); let ca = decodedCookie.split(';'); for(let i = 0; i