function countit(pocet) 
{
  var f1 = document.forms["form1"];
  var str = f1.text;
  var cislo2 = f1.cislo2;
  var sc = document.getElementById("smsCount");
  if(str.value.length>pocet)
  	str.value = f1.text.value.substr(0,pocet);
  //uspora
  if(f1.uspora.checked)
  {
  	f1.text.value = getUspora(str.value);
  }
  document.getElementById("charsWritten").innerHTML =  str.value.length;
  document.getElementById("charsLeft").innerHTML = pocet - str.value.length;
  cislo = f1.cislo.value;
  if(cislo==""&&cislo2.selectedIndex!=-1)
     cislo = cislo2.options[cislo2.selectedIndex].value;
  o = getOperator(cislo);
  if(o==0)
	d = 56;
  else
  if(o==1)
    d = 160;
  if(o!=-1)
	sc.innerHTML = Math.ceil(str.value.length/d);
  else
    sc.innerHTML = "?";
}
function getUspora(str)
{
	return str.replace(/[ ]+([^ ])/g,function(str,p1){return p1.toUpperCase();});
}
function getOperator(cislo)
{
	var o = new Array();
	o[0] = new Array("601","602","606","607","720","721","722","723","724","725","726","727","728","729");
	o[1] = new Array("608","773","774","775","776","777");
	o[2] = new Array("603","604","605","730","731","732","733","734","735","736","737","738","739");
	predcisli = cislo.substr(0,3);
	for(j = 0;j < 3;j++){
		for(i = 0;i < o[j].length;i++){
			if(predcisli == o[j][i])
				return j;
		}
	}
	return -1;
}
function validace()
{
	form = document.forms["form1"];
	txtText = document.getElementById("errorText");
	txtCislo = document.getElementById("errorCislo");
	//vymazat
	txtText.innerHTML = "";
	txtCislo.innerHTML = "";
	chyba = false;
	//kontrola textu
	if(form.text.value =="")
	{
		txtText.innerHTML = "Vyplňte prosím text!";
		chyba = true;
	}
	//kontrola cisla
	cislo = form.cislo.value;
	re = /\d{9,9}/;
	if(!re.test(cislo) && form.cislo2.selectedIndex == -1)
	{
		txtCislo.innerHTML = "Chybně vyplněno číslo!";
		chyba = true;
	}
	else
	//kontrola operatora
	if(getOperator(cislo)==-1&&form.cislo2.selectedIndex==-1)
	{
		txtCislo.innerHTML = "Chybný operátor!";
		return false;
	}	
	if(!chyba)
	{			
		txtText.innerHTML = "Odesílá se, chvilku strpení...";
		form.fok.disabled = true;
		return true;		
	}
	return false;
}
function setCislo(c){
    document.forms["form1"].cislo2.selectedIndex = -1;
    if(getOperator(c)==1)
        show("divOdesilatel");
    else
        hide("divOdesilatel");
    show("form_op");
    countit(600);
}
function setCislo2(c){
    document.forms["form1"].cislo.value = "";
    var i;
    b=false;
    for(i=0;i<c.options.length;i++){
        if(c.options[i].selected){
            if(getOperator(c.options[i].value)==1){
               b = true;
               break;
            }
        }
    }
    if(b)
        show("divOdesilatel");
    else
        hide("divOdesilatel");
    hide("form_op");
    countit(600);
}
function show(id){
    document.getElementById(id).style.visibility='visible';
}
function hide(id){
    document.getElementById(id).style.visibility='hidden';
}

function pretizeniAlert(){
    var ch = document.getElementById("bez_fronty");
    if(!ch.checked)
        if(confirm("Fronta je momentálně přetížená, chcete raději použít SMS bez fronty?"))
            ch.checked = true;
}
