function isNumber(str){
	var re = /^[0-9]*$/;
	if (!re.test(str))
        return false;
	else
        return true;
}

function strreplace(strObj, need){
	var re = new RegExp(need , "g");
	var newstrObj = strObj.replace(re, " ");

	return newstrObj;
}

function strcount(strObj, need, count){
	var index = 0;
	var indexstart = 0;

	while (index != -1){
		index = strObj.indexOf(need, indexstart);
		if (index >= 0){
			indexstart = index+1;
			count++;
		}
	}
	return count;
}


function precalculate(){
	var txtObj = document.getElementById('tText').value;

	var re = new RegExp('\n' , "g");
	txtObj = txtObj.replace(re, " ");
	var re = new RegExp('\r' , "g");
	txtObj = txtObj.replace(re, " ");

	var tarray = txtObj.split(" ");
	var wrdsCount = 0;
	var index = 0;
	var indexstart = 0
	var ltrsCount = 0;

	wrdsCount = strcount(txtObj, "¹", wrdsCount);
	txtObj = strreplace(txtObj, "¹");
	wrdsCount = strcount(txtObj, "@", wrdsCount);
	txtObj = strreplace(txtObj, "@");
	wrdsCount = strcount(txtObj, "$", wrdsCount);
	txtObj = strreplace(txtObj, "$");
	wrdsCount = strcount(txtObj, "%", wrdsCount);
	txtObj = strreplace(txtObj, "%");
	wrdsCount = strcount(txtObj, "&", wrdsCount);
	txtObj = strreplace(txtObj, "&");
	wrdsCount = strcount(txtObj, "ª", wrdsCount);
	txtObj = strreplace(txtObj, "ª");
	wrdsCount = strcount(txtObj, " ïð.", wrdsCount);
	txtObj = strreplace(txtObj, " ïð\\.");
	wrdsCount = strcount(txtObj, " óë.", wrdsCount);
	txtObj = strreplace(txtObj, " óë\\.");
	wrdsCount = strcount(txtObj, " ã.", wrdsCount);
	txtObj = strreplace(txtObj, " ã\\.");
	wrdsCount = strcount(txtObj, " ò.", wrdsCount);
	txtObj = strreplace(txtObj, " ò\\.");
	wrdsCount = strcount(txtObj, " ä.", wrdsCount);
	txtObj = strreplace(txtObj, " ä\\.");
	wrdsCount = strcount(txtObj, " êâ.", wrdsCount);
	txtObj = strreplace(txtObj, " êâ\\.");
	wrdsCount = strcount(txtObj, " êã.", wrdsCount);
	txtObj = strreplace(txtObj, " êã\\.");
	wrdsCount = strcount(txtObj, " ñì.", wrdsCount);
	txtObj = strreplace(txtObj, " ñì\\.");
	wrdsCount = strcount(txtObj, " ãð.", wrdsCount);
	txtObj = strreplace(txtObj, " ãð\\.");

	for(el in tarray){	
		if (isNumber(tarray[el])){
			wrdsCount += tarray[el].length;
			continue;
		}

		tmpStrArr = tarray[el].split("");
		for(tmpel in tmpStrArr){	
			if (isNumber(tmpStrArr[tmpel])){
				wrdsCount += 1;
				continue;
			}
		}

		txtObj = strreplace(txtObj, '0');
		txtObj = strreplace(txtObj, '1');
		txtObj = strreplace(txtObj, '2');
		txtObj = strreplace(txtObj, '3');
		txtObj = strreplace(txtObj, '4');
		txtObj = strreplace(txtObj, '5');
		txtObj = strreplace(txtObj, '6');
		txtObj = strreplace(txtObj, '7');
		txtObj = strreplace(txtObj, '8');
		txtObj = strreplace(txtObj, '9');
	}


	var txarray = txtObj.split(" ");
	for(el in txarray){	
		var sElement = txarray[el].toString();
	
	
		index = sElement.indexOf('.', 0);
		if (index >= 1 && index < (sElement.length-1)){
			tmpStrArr2 = sElement.split('.');
			wrdsCount += tmpStrArr2.length;
			wrdsCount += tmpStrArr2.length-1;
		}
		else{
			if (sElement.length > 2 && sElement.length <= 14) wrdsCount += 1; 
			if (sElement.length > 14) wrdsCount += 2;
			if ((sElement.length < 3) && (sElement != "-") && (sElement != " ") 
			&& (sElement != ".") && (sElement != ",") && (sElement != "¹") 
			&& (sElement != "@") && (sElement != "ª") && (sElement != "$") 
			&& (sElement != "%") && (sElement != "&") && (sElement != "")){ 
				ltrsCount++;
			}
		}
    }

	if (ltrsCount > 0) wrdsCount+= Math.ceil(ltrsCount/4);
	wrdsCount = wrdsCount.toFixed(0);

	if (txtObj == '') wrdsCount = '0';
	return wrdsCount;
}


function ClearInput(){

	var MinHrObj = document.getElementById('minhr');
	var OptHrObj = document.getElementById('opthr');
	var WrdsCntObj = document.getElementById('wrdscnt');
	var WrdsActionObj = document.getElementById('wrdsaction');
	var HelpObj = document.getElementById('lblhelp');

	MinHrObj.innerHTML = '';
	OptHrObj.innerHTML = '';
	WrdsActionObj.innerHTML = '';
	WrdsCntObj.innerHTML = '';
	HelpObj.style.visibility = "hidden";
	
	document.forms.frmHronomer.tText.value = '';
	document.forms.frmHronomer.tText.focus();
	return (true);
}

function fieldFocus(){
	document.forms.frmHronomer.tText.focus();
	return (true);
}

function activeField(sCheck,sField){
	var LabelObj = document.getElementById('lblattent');
	eval("if (document.forms.frmHronomer."+sCheck+".checked) document.forms.frmHronomer."+sField+".disabled=false; else document.forms.frmHronomer."+sField+".disabled=true;");
	if (sCheck=='iKoef'){
		if (document.forms.frmHronomer.iKoef.checked){
			LabelObj.innerHTML = "<img src=\"images/time_on.gif\" alt=\"\" width=\"15\" height=\"15\">";
		}
		else{
			LabelObj.innerHTML = "<img src=\"images/time_off.gif\" alt=\"\" width=\"15\" height=\"15\">";
		}
	}
	return (true);
}
