Current Mahadasha
Enter your birth details to view your active dasha
(function(){"use strict";const root=document.querySelector(".pk-mahadasha-card");if(!root||root.dataset.ready==="1"){return}
root.dataset.ready="1";const API_URL="/wp-json/prashnakundli/v1/mahadasha";const LEAD_URL="/wp-json/prashnakundli/v1/save-lead";const API_TIMEOUT_MS=30000;const LEAD_TIMEOUT_MS=15000;const CACHE_TTL_MS=30*60*1000;const PLANET_LINKS={sun:"https://planet.stage.prashnakundli.com/surya-ki-mahadasha-ka-landing-page",moon:"https://planet.stage.prashnakundli.com/chandra-ki-mahadasha-ka-landing-page",mars:"https://planet.stage.prashnakundli.com/mangal-ki-mahadasha-ka-landing-page",mercury:"https://planet.stage.prashnakundli.com/budh-ki-mahadasha-ka-landing-page",jupiter:"https://planet.stage.prashnakundli.com/guru-ki-mahadasha-ka-landing-page",venus:"https://planet.stage.prashnakundli.com/shukra-ki-mahadasha-ka-landing-page",saturn:"https://planet.stage.prashnakundli.com/shani-ki-mahadasha-ka-landing-page",rahu:"https://planet.stage.prashnakundli.com/rahu-ki-mahadasha-ka-landing-page",ketu:"https://planet.stage.prashnakundli.com/ketu-ki-mahadasha-ka-landing-page"};const el={form:root.querySelector("#pkMahadashaForm"),name:root.querySelector("#pkMahadashaName"),birthDate:root.querySelector("#pkMahadashaBirthDate"),birthTime:root.querySelector("#pkMahadashaBirthTime"),birthPlace:root.querySelector("#pkMahadashaBirthPlace"),phone:root.querySelector("#pkMahadashaPhone"),countryCode:root.querySelector("#pkMahadashaCountryCode"),button:root.querySelector(".sbmt-btn"),status:root.querySelector("#pkMahadashaStatus"),result:root.querySelector("#pkMahadashaResult"),planetButtons:root.querySelector("#pkMahadashaPlanetButtons")};let isSubmitting=!1;let activePlanet="";function escapeHtml(value){return String(value??"").replace(/&/g,"&").replace(//g,">").replace(/"/g,""").replace(/'/g,"'")}
function normalizePhone(rawPhone,countryCode){let digits=String(rawPhone||"").replace(/\D+/g,"");if(countryCode==="91"){if(digits.length===12&&digits.startsWith("91")){digits=digits.slice(2)}
if(digits.length===11&&digits.startsWith("0")){digits=digits.slice(1)}}
return digits}
function validatePhone(phone,countryCode){if(countryCode==="91"){return/^[6-9]\d{9}$/.test(phone)}
return/^\d{6,15}$/.test(phone)}
function getValues(){const values={name:el.name.value.trim(),phone:normalizePhone(el.phone.value,el.countryCode.value),countryCode:el.countryCode.value,birthPlace:el.birthPlace.value.trim(),birthDate:el.birthDate.value,birthTime:el.birthTime.value};if(!values.name||!values.phone||!values.birthPlace||!values.birthDate||!values.birthTime){throw new Error("Please fill all details.")}
if(values.name.length<2||values.name.length>120){throw new Error("Please enter a valid name.")}
if(values.birthPlace.length<3||values.birthPlace.length>160){throw new Error("Please enter a valid birth place.")}
if(!validatePhone(values.phone,values.countryCode)){throw new Error(values.countryCode==="91"?"Please enter a valid 10-digit Indian mobile number.":"Please enter a valid phone number.")}
return values}
function hash(value){let result=2166136261;for(let index=0;index>>0).toString(36)}
function fingerprint(values){return[values.birthDate,values.birthTime,values.birthPlace.toLowerCase().replace(/\s+/g," ").trim()].join("|")}
function cacheKey(fp){return"pk_mahadasha_result_"+hash(fp)}
function leadKey(values,fp){return"pk_mahadasha_lead_"+hash(values.phone+"|"+fp)}
function getCached(fp){try{const raw=sessionStorage.getItem(cacheKey(fp));if(!raw){return null}
const item=JSON.parse(raw);if(!item||typeof item.savedAt!=="number"||Date.now()-item.savedAt>CACHE_TTL_MS||!item.payload){sessionStorage.removeItem(cacheKey(fp));return null}
return item.payload}catch(error){return null}}
function setCached(fp,payload){try{sessionStorage.setItem(cacheKey(fp),JSON.stringify({savedAt:Date.now(),payload:payload}))}catch(error){}}
async function fetchJson(url,options,timeoutMs){const controller=new AbortController();const timer=window.setTimeout(function(){controller.abort()},timeoutMs);try{const response=await fetch(url,Object.assign({},options,{signal:controller.signal}));const payload=await response.json().catch(function(){return null});if(!response.ok){throw new Error(payload&&payload.message?payload.message:payload&&payload.data&&payload.data.message?payload.data.message:"Request could not be completed.")}
return payload}catch(error){if(error&&error.name==="AbortError"){throw new Error("Request timed out. Please try again.")}
throw error}finally{window.clearTimeout(timer)}}
function formatDate(value){if(!value){return"Not available"}
const parsed=new Date(value);if(Number.isNaN(parsed.getTime())){return String(value)}
return parsed.toLocaleDateString("en-IN",{day:"2-digit",month:"short",year:"numeric"})}
function renderSection(title,dasha){if(!dasha||typeof dasha!=="object"){return}
const wrapper=document.createElement("div");wrapper.className="section";const heading=document.createElement("h3");heading.textContent=title+" โ "+String(dasha.name||"Not available");const start=document.createElement("p");const startLabel=document.createElement("strong");startLabel.textContent="Start: ";start.append(startLabel,formatDate(dasha.start));const end=document.createElement("p");const endLabel=document.createElement("strong");endLabel.textContent="End: ";end.append(endLabel,formatDate(dasha.end));wrapper.append(heading,start,end);el.result.appendChild(wrapper)}
function renderPlanetButton(){el.planetButtons.replaceChildren();const link=PLANET_LINKS[activePlanet];if(!link){return}
const button=document.createElement("button");button.type="button";button.className="planet-btn";button.textContent="View "+activePlanet.toUpperCase()+" Mahadasha Remedy";button.addEventListener("click",function(){window.location.href=link});el.planetButtons.appendChild(button)}
function extractDashas(payload){const data=payload&&payload.data?payload.data:{};const dashas=data&&data.response&&typeof data.response==="object"?data.response:null;if(!dashas||!dashas.mahadasha){throw new Error("Invalid Mahadasha response.")}
return dashas}
async function saveLeadOnce(values,fp,dashas){const storageKey=leadKey(values,fp);try{if(sessionStorage.getItem(storageKey)==="saved"){return}}catch(error){}
try{const saved=await fetchJson(LEAD_URL,{method:"POST",headers:{"Content-Type":"application/json","Accept":"application/json"},credentials:"same-origin",body:JSON.stringify({lead_type:"single_person",calculator_type:"mahadasha",name:values.name,phone:values.phone,country_code:values.countryCode,email:"",birth_date:values.birthDate,birth_time:values.birthTime,birth_place:values.birthPlace,result_title:String(dashas.mahadasha.name||"")+" Mahadasha",result:{mahadasha:dashas.mahadasha,antardasha:dashas.antardasha||null},page_url:window.location.href,referrer_url:document.referrer})},LEAD_TIMEOUT_MS);if(saved&&saved.success===!0){try{sessionStorage.setItem(storageKey,"saved")}catch(error){}}}catch(error){console.warn("Mahadasha lead save failed:",error)}}
function display(values,payload,fromCache){const dashas=extractDashas(payload);el.result.replaceChildren();el.planetButtons.replaceChildren();activePlanet=String(dashas.mahadasha.name||"").toLowerCase();renderSection("Mahadasha",dashas.mahadasha);if(dashas.antardasha){renderSection("Antardasha",dashas.antardasha)}
renderPlanetButton();el.status.textContent=fromCache?"Previously generated Mahadasha result shown.":"";void saveLeadOnce(values,fingerprint(values),dashas)}
el.form.addEventListener("submit",async function(event){event.preventDefault();if(isSubmitting){return}
let values;try{values=getValues()}catch(error){el.status.textContent=error.message;return}
const fp=fingerprint(values);const cached=getCached(fp);el.result.replaceChildren();el.planetButtons.replaceChildren();if(cached){display(values,cached,!0);return}
const dateParts=values.birthDate.split("-");const apiDob=dateParts.length===3?dateParts[2]+"/"+dateParts[1]+"/"+dateParts[0]:values.birthDate;isSubmitting=!0;el.button.disabled=!0;el.button.setAttribute("aria-busy","true");el.button.textContent="Calculating...";el.status.textContent="Fetching Mahadasha...";try{const payload=await fetchJson(API_URL,{method:"POST",headers:{"Content-Type":"application/json","Accept":"application/json"},credentials:"same-origin",body:JSON.stringify({name:values.name,phone:values.phone,country_code:values.countryCode,dob:values.birthDate,tob:values.birthTime,place:values.birthPlace,date:values.birthDate,time:values.birthTime,location:values.birthPlace,birth_date:values.birthDate,birth_time:values.birthTime,birth_place:values.birthPlace,dob_formatted:apiDob})},API_TIMEOUT_MS);if(!payload||payload.success!==!0||!payload.data){throw new Error(payload&&payload.message?payload.message:"Mahadasha request failed.")}
setCached(fp,payload);display(values,payload,!1)}catch(error){el.status.textContent=error&&error.message?error.message:"Error fetching data."}finally{isSubmitting=!1;el.button.disabled=!1;el.button.removeAttribute("aria-busy");el.button.textContent="Get Mahadasha"}})})()) {
statusBox.textContent =
error && error.message
? error.message
: "Error fetching data.";
} finally {
resetButton(button);
}
});
})();