function textCounter(field,cntfield,maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
   field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else 
   cntfield.value = maxlimit - field.value.length;
   checkMe();
}
function checkMe(field,cntfield){
	var cache = document.forms.postcomment.com_text.value;
	if(cache=='Teksts')cache='';
	 if(countSmileys(cache)>5){
	 alert('Vienā komentārā drīkst izmantot līdz 5 smaidiņiem! Samaziniet to skaitu');	 
	 var leng=document.forms.postcomment.com_text.value.length-3;
	 document.forms.postcomment.com_text.value = document.forms.postcomment.com_text.value.substring(0, leng);
	 return false;
	}
}
function addSmile(code)
{
	var cache = document.forms.postcomment.com_text.value;
	if(cache=='Teksts')cache='';
 if(countSmileys(cache)>4){
 alert('Vienā komentārā drīkst izmantot līdz 5 smaidiņiem!');
 return false;
 }else{
	
	var leng=document.forms.postcomment.com_text.value.length;

	if(leng == 0 || cache[leng-1]==" ")
		document.forms.postcomment.com_text.value = cache  + code + " ";
	else{
		document.forms.postcomment.com_text.value = cache+" "+  code + " ";

	}
	textCounter(document.postcomment.com_text,document.postcomment.remLen1,500)
	//document.forms.postcomment.com_text.focus();
	return true;
	}
}
function substr_count(haystack,needle,offset,length){
	var pos=0,cnt=0;haystack+='';needle+='';
	if(isNaN(offset))offset=0;
	if(isNaN(length))length=0;
	offset--;
	while((offset=haystack.indexOf(needle,offset+1))!=-1){
		if(length>0&&(offset+needle.length)>length){
			return false;
		}else{
		cnt++;
		}
	}
	return cnt;
}

