function calculateSEOStats() {
var kwInput = document.getElementById('target_kw').value.toLowerCase().trim();
var textInput = document.getElementById('content_box').value.toLowerCase().trim();
var resultsPanel = document.getElementById('seo_results_panel');
if (kwInput === " || textInput === ") {
alert('Please provide both a keyword and content.');
return;
}
var wordArr = textInput.split(/\s+/).filter(function(w) { return w.length > 0; });
var totalWords = wordArr.length;
var regex = new RegExp('\\b' + kwInput.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + '\\b', 'g');
var matches = textInput.match(regex);
var matchCount = matches ? matches.length : 0;
var density = ((matchCount / totalWords) * 100).toFixed(2);
document.getElementById('res_word_count').innerText = totalWords;
document.getElementById('res_kw_matches').innerText = matchCount;
document.getElementById('res_density').innerText = density + '%';
var advice = ";
var adviceColor = '#3182ce';
if (density = 0.5 && density <= 2.5) {
advice = 'SEO Tip: Excellent! Your keyword density is within the optimal range (0.5% – 2.5%).';
adviceColor = '#38a169';
} else {
advice = 'SEO Tip: Warning! Keyword density is high. You may be at risk of "keyword stuffing" penalties.';
adviceColor = '#dd6b20';
}
resultsPanel.style.borderLeftColor = adviceColor;
document.getElementById('res_advice').innerText = advice;
resultsPanel.style.display = 'block';
}