function updateStats() {
  var tweet = document.getElementById('tweet');
  var lstotal = document.getElementById('lstotal');
  var chartotal = document.getElementById('chartotal');
  var remchartotal = document.getElementById('remchartotal');
  var lcharratio = document.getElementById('lcharratio');
  
  var lstotalint = countLs(tweet.value)
  lstotal.innerHTML = lstotalint;
  if (lstotalint == 50) {
    lstotal.className = 'green';
  } else if (lstotalint > 50) {
    lstotal.className = 'red';
  } else {
    lstotal.className = '';
  }
  
  var chartotalint = tweet.value.length;
  chartotal.innerHTML = chartotalint; 
  if (chartotalint <= 138) {
    chartotal.className = 'green';
  } else {
    chartotal.className = 'red';
  }
  
  remchartotalint = 138 - chartotalint
  remchartotal.innerHTML = remchartotalint; 
  if (remchartotalint >= 0) {
    remchartotal.className = 'green';
  } else {
    remchartotal.className = 'red';
  }
  if (chartotalint != 0) {
    var lcharratiofloat = lstotalint / chartotalint;
    lcharratio.innerHTML = lcharratiofloat.toString().substr(0,5);
    if (lcharratiofloat >= 0.36231884057971014492753623188406) {
      lcharratio.className = 'green';
    } else {
      lcharratio.className = 'red';
    }
  } else {
    lcharratio.innerHTML = 'N/A';
    lcharratio.className = '';
  }
};

function countLs(s) {
  s = s.toLowerCase();
  var count = 0;
	for(i=0; i<s.length; i++) {
    if (s.charAt(i) == 'l') {
      count += 1
    }
  }
  return count;
};

function atStart() {
  var tweet = document.getElementById('tweet');
  return (tweet.value == 'Enter the text for your competition entry here. The aim is to include 50 L\'s and an #L tag (this doesn\'t count towards the 50 L\'s) in 140 characters. The words below may help.');
}

function checkForStart() {
  var tweet = document.getElementById('tweet');
  if (atStart()) {
    tweet.value = '';
  }
  updateStats();
};

function updateWords() {
  var dic = document.getElementById('dictionaries').options[document.getElementById('dictionaries').selectedIndex].value;
  if (dic != "") {
    var order = document.getElementById('ordering').options[document.getElementById('ordering').selectedIndex].value;
    $('#wordsarea').load('/dictionaries/' + dic + order + '.txt');
  }
};

function wordselected() {
  if (navigator.appName!='Microsoft Internet Explorer') {
  	var t = document.getSelection();
  	od(t);
  	}
  else {
  	var t = document.selection.createRange();
  	if(document.selection.type == 'Text' && t.text>'') {
  		document.selection.empty();
  		od(t.text);}
     }
  function od(t) {
    while (t.substr(t.length-1,1)==' ')
  	 t=t.substr(0,t.length-1);
    while (t.substr(0,1)==' ') 
  	 t=t.substr(1);
    if (t) {
     var tweet = document.getElementById('tweet');
     if (tweet.value != '' && tweet.value.substr(tweet.value.length - 1, 1) != ' ') {
      t = ' ' + t;
     }
     tweet.value += t;
    }
    tweet.focus();
  }
  updateStats();
};