var Diacritic = "�����������������������������ż�������������؎";
var DiacRemoved = "aacdeeillnoooorstuuuuyrzAACDEEILLNOOOORSTUUUUYRZ";

function zprava() {
	napsano = document.form_sms.text.value.length;
	document.form_sms.cntr1.value = napsano;
	zbyva = maxDelka - napsano;
	if (zbyva>=0) document.form_sms.cntr2.value = zbyva;
	if (zbyva<0) {
		document.form_sms.text.value = document.form_sms.text.value.substring(0, maxDelka);
		napsano = document.form_sms.text.value.length;
		document.form_sms.cntr1.value = napsano;
		zbyva = maxDelka - napsano;
		document.form_sms.cntr2.value = zbyva;
	}
	document.form_sms.pocet.value = Math.ceil(napsano/delka);
}

function setOperator(selectedOperator) {
	operator = selectedOperator;
	//window.alert(operator);
	showCaptcha(operator);
	showOperator();
	document.form_sms.cntr2.value = maxDelka;
}

function changeOperator() {
	//window.alert(document.form_sms.operator[0].value);
	if (!document.form_sms.operator[0].checked) {
		if (document.form_sms.cislo.value.substring(0,2)=="72" || document.form_sms.cislo.value.substring(0,3)=="602" || document.form_sms.cislo.value.substring(0,3)=="606" || document.form_sms.cislo.value.substring(0,3)=="607")
		{
			operator = "o2";
			document.form_sms.cntr2.value = 60;
		}
		else if (document.form_sms.cislo.value.substring(0,2)=="73" || document.form_sms.cislo.value.substring(0,3)=="603" || document.form_sms.cislo.value.substring(0,3)=="604" || document.form_sms.cislo.value.substring(0,3)=="605") {
			operator = "tmobile";
			document.form_sms.cntr2.value = 140;
		}
		else if (document.form_sms.cislo.value.substring(0,2)=="77" || document.form_sms.cislo.value.substring(0,3)=="608") {
			operator = "vodafone";
			document.form_sms.cntr2.value = 140;
		}
		else operator = "";
		document.form_sms.auto_operator.value = operator;
		showCaptcha(operator);
		showOperator();
	}
}

function showOperator() {
	//window.alert(operator);
	switch (operator) {
		case '':
			document.getElementById('operatorSelected').innerHTML = "automaticky";
			maxDelka = "160";
			delka = "152";
			break;
		case 'vodafone':
			document.getElementById('operatorSelected').innerHTML = "Vodafone";
			maxDelka = "140";
			delka = "152";
			break;
		case 'o2':
			document.getElementById('operatorSelected').innerHTML = "O2";
			maxDelka = "50";
			delka = "35";
			break;
		case 'tmobile':
			document.getElementById('operatorSelected').innerHTML = "T-Mobile";
			maxDelka = "140";
			delka = "150";
			break; 
	}
	
	document.form_sms.cntr2.value = maxDelka;
	zprava();
}


var vfLoaded = false;
var tmLoaded = false;
var o2Loaded = false;

function showCaptcha(o) {
	//window.alert(operator);
	if (o!='') operator = o;
	
	document.getElementById('preloader').style.display = "none";
	switch (operator) {
		case '':
			document.getElementById('captchaO2').style.display = "none";
			document.getElementById('captchaTM').style.display = "none";
			document.getElementById('captchaVF').style.display = "none";
		break;
		case 'vodafone':
			if (!vfLoaded) {
				$("#captchaVF").attr("src", "images/loading.gif");
				$.post('/ajax.php', 'vf=1', function(response){
						$("#captchaVF").attr("src", response);
						vfLoaded = true;			
				});
			} 
			document.getElementById('captchaO2').style.display = "none";
			document.getElementById('captchaTM').style.display = "none";
			document.getElementById('captchaVF').style.display = "block";
		break;
		case 'o2':
			if (!o2Loaded) {
				$("#captchaO2").attr("src", "images/loading.gif");
				$.post('/ajax.php', 'o2=1', function(response){
						$("#captchaO2").attr("src", response);
						o2Loaded = true;			
				});
			} 
			document.getElementById('captchaO2').style.display = "block";
			document.getElementById('captchaTM').style.display = "none";
			document.getElementById('captchaVF').style.display = "none";
		break;
		case 'tmobile':
			if (!tmLoaded) {
				$("#captchaTM").attr("src", "images/loading.gif");
				$.post('/ajax.php', 'tm=1', function(response){
						$("#captchaTM").attr("src", response);
						tmLoaded = true;			
				});
			} 
			document.getElementById('captchaO2').style.display = "none";
			document.getElementById('captchaTM').style.display = "block";
			document.getElementById('captchaVF').style.display = "none";
		break; 
	}
}

function noDiacritic(field) {
	var ptext="";
	var text=field.value;
	for(i=0;i<text.length;i++) {
		if (Diacritic.indexOf(text.charAt(i))!=-1)
			ptext+=DiacRemoved.charAt(Diacritic.indexOf(text.charAt(i)));
		else
      		ptext+=text.charAt(i);
	}
  	field.value=ptext;
  	zprava();
}

function compressMessage(field) {
	var tx="";
	var text=field.value;
	for(p=0;p<text.length;p++) {
		if (text.charAt(p) == " ") {
			p++;
			tx+= text.charAt(p).toUpperCase();
		}
		else if (p == 0)
        	tx+= text.charAt(p).toUpperCase();
      	else
        	tx+= text.charAt(p);
  	}
	field.value=tx;
	zprava();
}

