document.addEventListener('DOMContentLoaded', function (){
if(window.__otinoFormScriptBound){
return;
}
window.__otinoFormScriptBound=true;
const otpInputs=document.querySelectorAll('.otino_otp_item');
const hiddenOtpInput=document.getElementById('otino_otp');
const formOtp=document.getElementById('landsell_otp_container');
const otpForm=document.querySelector('#otino-form-otp');
const otpFormSubmitBtn=document.querySelector('button[name="otino_otp_submit"]');
const phoneInput=document.getElementById('otino_mobile');
const maxPhoneLength=10;
const mobileForm=document.querySelector('#otino-login-register-form-mobile');
const mobileFormSubmitBtn=document.querySelector('button[name="otino_mobile_submit"]');
const nonSmsChannelButton=document.querySelector('button[name="otino_mobile_submit_bale"], button[name="otino_mobile_submit_telegram"], button[name="otino_mobile_submit_voice"]');
const rawMobileAutoSubmit=otino_ajax&&Object.prototype.hasOwnProperty.call(otino_ajax, 'mobile_auto_submit_enabled')
? otino_ajax.mobile_auto_submit_enabled
: true;
const normalizedMobileAutoSubmit=String(rawMobileAutoSubmit).trim().toLowerCase();
const mobileAutoSubmitEnabled = !['0', 'false', 'off', 'no', '', 'null', 'undefined'].includes(normalizedMobileAutoSubmit);
const responseDiv=document.getElementById('otino-response-box');
const loadingIndicator=document.querySelector('.otino-loading-box');
const firstOtpInput=otpForm ? otpForm.querySelector('#otp-input-0'):null;
let lastAutoSubmitOtp='';
let lastAutoSubmitMobile='';
function setupOTPAutoDetection(){
if('OTPCredential' in window){
navigator.credentials.get({
otp: { transport: ['sms'] }}).then(otp=> {
if(otp&&otp.code){
fillOTPInputs(otp.code);
}}).catch(err=> {
console.log('OTP auto-detection failed:', err);
});
}
if('navigator' in window&&'serviceWorker' in navigator){
document.addEventListener('paste', function(e){
const clipboardData=e.clipboardData||window.clipboardData;
const pastedData=clipboardData.getData('text');
const otpMatch=pastedData.match(/\b\d{4,8}\b/);
if(otpMatch&&otpInputs.length > 0){
e.preventDefault();
fillOTPInputs(otpMatch[0]);
}});
}}
function fillOTPInputs(code){
const codeStr=code.toString();
otpInputs.forEach((input, index)=> {
if(index < codeStr.length){
input.value=codeStr[index];
}else{
input.value='';
}});
updateHiddenOtpValue();
const lastFilledIndex=Math.min(codeStr.length - 1, otpInputs.length - 1);
if(lastFilledIndex >=0){
otpInputs[lastFilledIndex].focus();
}}
if(otpForm){
const observer=new MutationObserver(function(mutations){
mutations.forEach(function(mutation){
if(mutation.type==='attributes'&&mutation.attributeName==='style'){
if(otpForm.style.display!=='none'&&otpForm.offsetParent!==null){
setupOTPAutoDetection();
}}
});
});
observer.observe(otpForm, {
attributes: true,
attributeFilter: ['style', 'class'],
childList: true,
subtree: true
});
if(otpForm.style.display!=='none'&&otpForm.offsetParent!==null){
setupOTPAutoDetection();
}}
function sanitizePhoneNumber(phone){
if(phone.startsWith('98')){
phone=phone.substring(2);
}
if(phone.startsWith('+98')){
phone=phone.substring(3);
}
if(phone.startsWith('0098')){
phone=phone.substring(4);
}
if(phone.startsWith('0')){
phone=phone.substring(1);
}
return phone;
}
function isPhoneNumberValidOtino(phone){
return phone.length===maxPhoneLength&&phone.startsWith('9')&&/^9\d{9}$/.test(phone);
}
function updateHiddenOtpValue(){
if(!otpInputs||otpInputs.length===0||!hiddenOtpInput) return;
let otpValue='';
otpInputs.forEach(input=> {
if(input) otpValue +=input.value;
});
hiddenOtpInput.value=otpValue;
const otpSubmitLocked=otpFormSubmitBtn&&otpFormSubmitBtn.disabled;
if(otpValue.length===otpInputs.length&&otpFormSubmitBtn&&!otpSubmitLocked&&otpValue!==lastAutoSubmitOtp){
lastAutoSubmitOtp=otpValue;
otpFormSubmitBtn.click();
}}
if(otpInputs&&otpInputs.length > 0){
otpInputs.forEach((input, index)=> {
if(!input) return;
input.addEventListener('input', ()=> {
input.value=input.value
.replace(/[^0-9۰-۹]/g, '')
.replace(/[۰]/g, '0')
.replace(/[۱]/g, '1')
.replace(/[۲]/g, '2')
.replace(/[۳]/g, '3')
.replace(/[۴]/g, '4')
.replace(/[۵]/g, '5')
.replace(/[۶]/g, '6')
.replace(/[۷]/g, '7')
.replace(/[۸]/g, '8')
.replace(/[۹]/g, '9');
if(input.value.length===1&&index < otpInputs.length - 1){
otpInputs[index + 1].focus();
}
updateHiddenOtpValue();
});
input.addEventListener('keydown', (e)=> {
if(e.key==='Backspace'){
if(input.value===''&&index > 0){
otpInputs[index - 1].focus();
}}
});
input.addEventListener('keypress', (e)=> {
if(!/[0-9۰-۹]/.test(e.key)){
e.preventDefault();
}});
});
}
if(otpForm){
otpForm.addEventListener('submit', function (){
if(otpFormSubmitBtn){
otpFormSubmitBtn.disabled=true;
}});
}
if(phoneInput){
phoneInput.addEventListener('input', function (){
let phone=phoneInput.value
.replace(/[^0-9۰-۹]/g, '')
.replace(/[۰]/g, '0')
.replace(/[۱]/g, '1')
.replace(/[۲]/g, '2')
.replace(/[۳]/g, '3')
.replace(/[۴]/g, '4')
.replace(/[۵]/g, '5')
.replace(/[۶]/g, '6')
.replace(/[۷]/g, '7')
.replace(/[۸]/g, '8')
.replace(/[۹]/g, '9');
if(phone.length > 11){
phone=phone.substring(0, 11);
}
phoneInput.value=phone;
if(phone.length===0||/^0?9\d{9}$/.test(phone)){
phoneInput.style.borderColor='';
}else{
phoneInput.style.borderColor='red';
}
const sanitizedPhone=sanitizePhoneNumber(phone);
if(sanitizedPhone.length===maxPhoneLength&&isPhoneNumberValidOtino(sanitizedPhone)){
if(mobileFormSubmitBtn&&!nonSmsChannelButton&&mobileAutoSubmitEnabled&&sanitizedPhone!==lastAutoSubmitMobile){
lastAutoSubmitMobile=sanitizedPhone;
mobileFormSubmitBtn.click();
}}else{
lastAutoSubmitMobile='';
}});
}
if(mobileForm){
mobileForm.addEventListener('submit', function (e){
let phone=phoneInput.value
.replace(/[^0-9]/g, '');
phone=sanitizePhoneNumber(phone);
phoneInput.value=phone;
});
}
function setupOTPAutoDetection(){
if('OTPCredential' in window){
const abortController=new AbortController();
navigator.credentials.get({
otp: { transport: ['sms'] },
signal: abortController.signal
})
.then(otp=> {
if(otp&&otp.code){
fillOTPInputs(otp.code);
abortController.abort();
}})
.catch(err=> {
if(err.name!=='AbortError'){
console.log('⚠️ WebOTP پشتیبانی نمی‌شود یا خطا رخ داد:', err.message);
}});
setTimeout(()=> {
abortController.abort();
}, 120000);
}else{
console.log('⚠️ WebOTP API در این مرورگر پشتیبانی نمی‌شود');
}
setupPasteDetection();
}
function setupPasteDetection(){
if(!otpInputs||otpInputs.length===0) return;
otpInputs.forEach((input, index)=> {
if(!input) return;
input.addEventListener('paste', function(e){
e.preventDefault();
const clipboardData=e.clipboardData||window.clipboardData;
const pastedData=clipboardData.getData('text').trim();
const cleanedData=pastedData
.replace(/[۰]/g, '0')
.replace(/[۱]/g, '1')
.replace(/[۲]/g, '2')
.replace(/[۳]/g, '3')
.replace(/[۴]/g, '4')
.replace(/[۵]/g, '5')
.replace(/[۶]/g, '6')
.replace(/[۷]/g, '7')
.replace(/[۸]/g, '8')
.replace(/[۹]/g, '9')
.replace(/\D/g, '');
if(cleanedData.length >=otpInputs.length){
fillOTPInputs(cleanedData);
}else if(cleanedData.length > 0){
fillOTPInputs(cleanedData);
}});
});
if(otpForm){
document.addEventListener('paste', function(e){
if(otpForm.style.display==='none'||otpForm.offsetParent===null){
return;
}
const activeElement=document.activeElement;
const isOtpInputFocused=Array.from(otpInputs).includes(activeElement);
if(!isOtpInputFocused){
const clipboardData=e.clipboardData||window.clipboardData;
const pastedData=clipboardData.getData('text').trim();
const cleanedData=pastedData
.replace(/[۰-۹]/g, (match)=> String.fromCharCode(match.charCodeAt(0) - 1728))
.replace(/\D/g, '');
if(cleanedData.length >=otpInputs.length){
e.preventDefault();
fillOTPInputs(cleanedData);
}}
});
}}
function fillOTPInputs(code){
if(!otpInputs||otpInputs.length===0) return;
const codeStr=code.toString()
.replace(/[۰]/g, '0')
.replace(/[۱]/g, '1')
.replace(/[۲]/g, '2')
.replace(/[۳]/g, '3')
.replace(/[۴]/g, '4')
.replace(/[۵]/g, '5')
.replace(/[۶]/g, '6')
.replace(/[۷]/g, '7')
.replace(/[۸]/g, '8')
.replace(/[۹]/g, '9')
.replace(/\D/g, '');
otpInputs.forEach(input=> {
if(input) input.value='';
});
otpInputs.forEach((input, index)=> {
if(index < codeStr.length){
input.value=codeStr[index];
const event=new Event('input', { bubbles: true });
input.dispatchEvent(event);
input.style.transition='all 0.3s ease';
input.style.transform='scale(1.05)';
input.style.backgroundColor='#e8f5e9';
setTimeout(()=> {
input.style.transform='scale(1)';
input.style.backgroundColor='';
}, 300);
}});
updateHiddenOtpValue();
const lastFilledIndex=Math.min(codeStr.length - 1, otpInputs.length - 1);
if(lastFilledIndex >=0&&lastFilledIndex < otpInputs.length){
setTimeout(()=> {
otpInputs[lastFilledIndex].focus();
otpInputs[lastFilledIndex].select();
}, 100);
}}
if(otpForm){
const observer=new MutationObserver(function(mutations){
mutations.forEach(function(mutation){
if(mutation.type==='attributes' &&
(mutation.attributeName==='style'||mutation.attributeName==='class')){
const isVisible=otpForm.style.display!=='none' &&
otpForm.offsetParent!==null &&
!otpForm.classList.contains('hidden');
if(isVisible){
setTimeout(()=> {
setupOTPAutoDetection();
}, 100);
}}
});
});
observer.observe(otpForm, {
attributes: true,
attributeFilter: ['style', 'class'],
childList: false,
subtree: false
});
setTimeout(()=> {
if(otpForm.style.display!=='none'&&otpForm.offsetParent!==null){
setupOTPAutoDetection();
}}, 500);
}
if(firstOtpInput){
firstOtpInput.addEventListener('focus', function(){
setupOTPAutoDetection();
}, { once: true });
}});