// JavaScript Document
function tagy(prvni,druhy){
  var textarea = top.document.getElementById("ta");

  if (textarea.setSelectionRange) {
  var zacatek = textarea.selectionStart;
  var konec = textarea.selectionEnd;

  textarea.value = textarea.value.substring(0, zacatek) + prvni + textarea.value.substring(zacatek, konec) + druhy + textarea.value.substring(konec);
  setCaretToPos(textarea, zacatek + prvni.length + druhy.length + (konec-zacatek));
  }

  else if (document.selection) {
  textarea.focus();

  document.selection.createRange().text = prvni + document.selection.createRange().text + druhy;
  }
}




//Base on: boule.cz mailto:info@boule.cz http://blog.openheads.org/data/stale/textarea/index.html

 var str;


function counted(str) {
  var orginal = str.length;
  return  orginal;
}

function progresbar (str) {
  var len = str.length;
  var maxWidths = document.getElementById('progres_main').style.width;
  maxWidth =  maxWidths.substr(0,maxWidths.length -2)-2;
  var pBar = document.getElementById('progres_bar');
  
  var newWidth = maxWidth/maxChars*len;
  var procento = Math.round((len/maxChars*100)-0.4)+'%';
  pBar.style.width = newWidth+'px';
  document.getElementById('progres_val').innerHTML=procento;
}

function countChars(nelze,znaku,maxChars){

   var theForm = document.getElementById('part1');
   theForm.counter.value = counted(theForm.obsah.value);
   progresbar (theForm.obsah.value);
   
   if ( theForm.counter.value >= maxChars ) {
     alert(nelze+' '+ maxChars+' '+znaku);
	 theForm.obsah.value=theForm.obsah.value.substr(0,maxChars);
   } else if (theForm.counter.value > maxChars*0.9) {
      theForm.counter.style.color = 'red';
	} else if (theForm.counter.value > maxChars*0.8) {
      theForm.counter.style.color = '#bb983b';
	} else if (theForm.counter.value > maxChars*0.7) {
      theForm.counter.style.color = '#adbb3b';
   } else if (theForm.counter.value > maxChars*0.6) {
      theForm.counter.style.color = '#8fbb3b';
   } else if (theForm.counter.value > maxChars*0.5) {
      theForm.counter.style.color = '#65bb3b';
   } else if (theForm.counter.value > maxChars*0.4) {
      theForm.counter.style.color = '#3bbb59';
   } else if (theForm.counter.value > maxChars*0.3) {
      theForm.counter.style.color = '#3bbbb3';
   } else if (theForm.counter.value > maxChars*0.2) {
      theForm.counter.style.color = '#3b7dbb';
   } else {
      theForm.counter.style.color = 'silver';
   }
}
