var qty				  = 0;   //smile
var aqty				= 0;   //válasz
var lqty				= 0;   //link
var alertmsg		= 0;
var alertsmile		= 0;
var redirectloop	= 0;

var maxchar			= 999;
var maxsmile		= 5;
var maxlink 		= 5;
var maxanswer		= 1;
var charAlertText	= 'Több karakter nem írható!';
var smileAlertText	= 'Több smile nem szúrható be!';
var linkAlertText	= 'Több link nem szúrható be!';

//var browser = (parseInt(navigator.appVersion) > 2) ? true : false;

function storecaret(textfield){
	if(textfield.createTextRange){
		textfield.caretPos = document.selection.createRange().duplicate();
	}
}

function checklength(textfield, charbox){
	if((maxchar-textfield.value.length) > 0){
		charbox.value = '('+(maxchar-textfield.value.length)+')';
	}
	else{
		charbox.value = '(0)';
	}
	if (textfield.value.length > maxchar){
		textfield.value = textfield.value.substr(0, maxchar);
		if(alertmsg == '0'){
			alert(charAlertText);
			alertmsg = 1;
		}
		else if(alertmsg == '1'){
			void(0);
		}
	}
}

function realsmile(textfield){
	var text = textfield.value;
	var seek = '[SMILE=';
	var i, tot;
	for(i = 0, tot = 0; i < text.length; i++){
		if(text.substring(i, i + seek.length) == seek){
			tot++
		}
	}
	qty = tot;
}

function checksmile(smilebox){
	smilebox.value = '('+(maxsmile-qty < 0 ? 0 : maxsmile-qty)+')';
}

function addsmile(which, textfield){
	var formattedsmile = ' [SMILE=' + which + '] ';
	if(qty < maxsmile){
		if((textfield.value.length + formattedsmile.length) <= maxchar){
			if(textfield.createTextRange && textfield.caretPos){
				var caretPos = textfield.caretPos;
				caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? formattedsmile + ' ' : formattedsmile;
			}
			else{
				textfield.value += formattedsmile;
			}
			qty++;
		}
	}
	else if(alertsmile == '0'){
		alert(smileAlertText);
		alertsmile = 1;
	}
	else if(alertsmile == '1'){
		void(0);
	}
}

function smile(textfield, charbox, smilebox, whichsmile){
	addsmile(whichsmile, textfield);
	realsmile(textfield);
	checksmile(smilebox);
	checklength(textfield, charbox);
	textfield.focus();
}

//link hozzáadása

function addlink(which, textfield){
	var formattedsmile = ' [LINK=' + which + '] ';
	if(lqty < maxlink){
		if((textfield.value.length + formattedsmile.length) <= maxchar){
			if(textfield.createTextRange && textfield.caretPos){
				var caretPos = textfield.caretPos;
				caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? formattedsmile + ' ' : formattedsmile;
			}
			else{
				textfield.value += formattedsmile;
			}
			lqty++;
		}
	}
	else {
		alert(linkAlertText);
	}
}

function linkadd(textfield, charbox, linkbox, whichsmile){     
	if (whichsmile == '' || whichsmile =='http://'){ textfield.focus() }
	else {
	addlink(whichsmile, textfield);
	checklength(textfield, charbox);
  linkbox.value = 'http://';	
	textfield.focus();
   }
}

// válasz hozzáadása

function addanswer(which, textfield){
	var formattedsmile = ' [VÁLASZ=' + which + '] ';
	if(aqty < maxanswer){
		if((textfield.value.length + formattedsmile.length) <= maxchar){
			if(textfield.createTextRange && textfield.caretPos){
				var caretPos = textfield.caretPos;
				caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? formattedsmile + ' ' : formattedsmile;
			}
			else{
				textfield.value += formattedsmile;				
			}
			aqty++;
		}
	}
}

function answer(textfield, charbox, whichsmile){
	addanswer(whichsmile, textfield);
	checklength(textfield, charbox);
	textfield.focus();
}


function submitform(formname, countryfield, nofloodfield)
{
	nofloodfield.value = 1;
	countryfield.disabled = false;
	formname.submit();
}

function resetform(textfield, link, charbox, smilebox)
{
	textfield.value = '';
	link.value = 'http://';
	qty = 0;
	aqty = 0;	
	lqty = 0;
	alertmsg = 0;
	alertsmile = 0;
	checksmile(smilebox);
	checklength(textfield, charbox);
	textfield.focus();	
}

//jobb gomb letiltás

if (window.Event)
{
	document.captureEvents(Event.MOUSEUP);
}

function nocontextmenu(){
	event.cancelBubble = true
	event.returnValue = false;
	return false;
}

function norightclick(e){
	if (window.Event){
		if (e.which == 2 || e.which == 3)
		return false;
	}
	else if (event.button == 2 || event.button == 3){
		event.cancelBubble = true
		event.returnValue = false;
		return false;
	}	
}

//document.oncontextmenu = nocontextmenu;
//document.onmousedown = norightclick;

function click(e){
	if (document.all){
		if (event.button == 2){
//			if(alertOnOff == 1) { alert(message); }
		return false;
		}
	}
	if (document.layers){
		if (e.which == 3){
//			if(alertOnOff == 1){ alert(message); }
		return false;
		}
	}
}
if (document.layers){
	document.captureEvents(Event.MOUSEDOWN);
}
document.onmousedown = click;

function F5(){
	if (event.keyCode == 116) {
//	alert("F5 letiltva");
	event.keyCode=0;
	event.cancelBubble = true;
	event.returnValue = false;
	}
} 
