//***************FORMS***************************************************************
function numsOnly(e)
{
	if(e.keyCode)
		var charCode=e.keyCode;
	else if (e.which)
		var charCode=e.which;
	else return true;

	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;
	return true;
}

function inputText(targetStr,text)
{
	target=document.getElementById(targetStr);
	target.value+=text;
}

//***************POLLS***************************************************************
function sendvote(form,acceptvote)
{
	if(acceptvote)
	{
		i=0;
		while(form.voteoption[i])
		{
			if(form.voteoption[i].checked)
				form.vote.value=form.voteoption[i].value;
			++i;
		}
		if(form.vote.value!='0') form.submit();
	}
	else
	{
		form.vote.value=0;
		form.submit();
	}
}

//***************MOVING*OBJECTS******************************************************
function getCenterPos(whatS)
{
	what=document.getElementById('floatingpopup');
	what.centerTop=Math.ceil((document.body.offsetHeight-what.offsetHeight)/2);
	what.centerLeft=Math.ceil((document.body.offsetWidth-what.offsetWidth)/2);
}

function moveObj(whatS,toX,toY,time,frames)
{
	what=document.getElementById(whatS);
	howMuchX=Math.ceil((toX-what.offsetLeft)/Math.ceil(time/(1000/frames)));
	howMuchY=Math.ceil((toY-what.offsetTop)/Math.ceil(time/(1000/frames)));
	what.style.top=what.offsetTop+howMuchY;
	what.style.left=what.offsetLeft+howMuchX;
	if(((time-(1000/frames))>0)&&(howMuchY||howMuchX)&&!what.stopMoving)time-=(1000/frames);
	else
	{
		what.isBeingMoved=false;
		what.stopMoving=false;
		return false;
	}
	setTimeout('moveObj(\''+whatS+'\','+toX+','+toY+','+time+','+frames+');',(1000/frames));
}

//***************WRITING*FORM********************************************************
function writingFormSubmit(form,action)
{
	useAction=false;
	if(form.tryingToSend)
		useAction=true;
	form.tryingToSend=false;

	

	formErrors = false;

	if (form.temat.value.length < 3)
		formErrors=document.getElementById(form.id+'_errorsTopic').value;
	else if (form.tresc.value.length < 3)
		formErrors=document.getElementById(form.id+'_errorsBody').value;
	else if(form.sondatyp)
	{
		sondatyp=parseInt((form.sondatyp.type=='hidden')?form.sondatyp.value:form.sondatyp.options[form.sondatyp.selectedIndex].value);
  		if(sondatyp)
  		{
			if(form.sondapytanie.value.length<4)
				formErrors=document.getElementById(form.id+'_errorsPollQuestion').value;
			else if(sondatyp==2)
			{
				var j=0;
				var i=0;
				while(eval('form.answer'+(j+1)))
				{
					if(eval('form.answer'+(j+1)).value.length>0) i++;
					j++;
				}
				if(i<2)	formErrors=document.getElementById(form.id+'_errorsPollNotEnoughAnswers').value;
			}
		}
	}

	if(formErrors)
	{
		alert(formErrors);
		return false;
	}
	else applyStyle(form.id+'_body',-1);
	
	
	

	if(useAction)
		form.action=action;
	return true;
}

stylesList=new Array('[b]','[/b]','[i]','[/i]','[u]','[/u]','[quote]','[/quote]','[code]','[/code]','[url]','[/url]','[img]','[/img]');

function applyStyle(targetStr,styleCode,buttonStr)
{
	target=document.getElementById(targetStr);
	if(!target.appliedStyles)
		target.appliedStyles=new Array();

	if(styleCode==-1)
	{
		for(i=target.appliedStyles.length-1;i>=0;--i)
		{
			if(target.appliedStyles[i])
			{
				target.value+=stylesList[target.appliedStyles[i][0]];
				button=document.getElementById(target.appliedStyles[i][1]);
				button.value=button.isActiveStyle;
				button.isActiveStyle=false;
			}
		}
		target.appliedStyles=new Array();

		target.focus();
		return;
	}

	button=document.getElementById(buttonStr);
	if(button.isActiveStyle)
	{
		target.value+=stylesList[styleCode+1];
		button.value=button.isActiveStyle;
		button.isActiveStyle=false;
		for(i=0;i<target.appliedStyles.length;++i)
		{
			if(target.appliedStyles[i]&&target.appliedStyles[i][0]==styleCode+1)
				target.appliedStyles[i]=false;
		}
	}
	else
	{
		target.value+=stylesList[styleCode];
		button.isActiveStyle=button.value;
		button.value+=' *';
		target.appliedStyles[target.appliedStyles.length]=new Array(styleCode+1,buttonStr);
	}

	target.focus();
}

function applyFontStyle(targetStr,switcherStr,openTag,closeTag)
{
	target=document.getElementById(targetStr);
	switcher=document.getElementById(switcherStr);
	style=switcher.options[switcher.selectedIndex].value;
	if(style=='default')
	{
		target.focus();
		return;
	}

	target.value+=openTag+style+closeTag;
	defaultOption=-1;
	for(i=0;i<switcher.options.length;i++)
	{
		if(switcher.options[i].value=='default')
			defaultOption=i;
	}
	if(defaultOption!=-1)
		switcher.selectedIndex=defaultOption;

	target.focus();
}

function switchPollRows(switcher,formID)
{
	type=switcher.options[switcher.selectedIndex].value;
	switch(parseInt(type))
	{
	case 0:
		document.getElementById(formID+'_pollRowTitle').style.display='none';
		document.getElementById(formID+'_pollRowDuration').style.display='none';
		document.getElementById(formID+'_pollRowQuestion').style.display='none';
		document.getElementById(formID+'_pollRowAnswers').style.display='none';
	break;
	case 1:
		document.getElementById(formID+'_pollRowTitle').style.display='';
		document.getElementById(formID+'_pollRowDuration').style.display='';
		document.getElementById(formID+'_pollRowQuestion').style.display='';
		document.getElementById(formID+'_pollRowAnswers').style.display='none';
	break;
	case 2:
		document.getElementById(formID+'_pollRowTitle').style.display='';
		document.getElementById(formID+'_pollRowDuration').style.display='';
		document.getElementById(formID+'_pollRowQuestion').style.display='';
		document.getElementById(formID+'_pollRowAnswers').style.display='';
	break;
	}
}


//***************MENU*POPUP**********************************************************
function moveMsgPopup(whatS,hide)
{
	what=document.getElementById(whatS);
	if(!what.started)
	{
		getCenterPos(whatS);
 		moveObj(whatS,what.centerLeft,-(what.offsetHeight+20),1,1);
 		what.started=true;
 		setTimeout('moveMsgPopup(\''+whatS+'\');',50);
 		return;
	}
	if(what.isBeingMoved)
	{
		if(what.isBeingMoved=='center'&&hide)
		{
			what.stopMoving=true;
			setTimeout('moveMsgPopup(\''+whatS+'\',true);',50);
		}
	}
	else if(hide)
	{
		getCenterPos(whatS);
		what.isBeingMoved='out';
		moveObj(whatS,-(what.offsetWidth+40),what.centerTop,300,40);
	}
	else
	{
		what.isBeingMoved='center';
		moveObj(whatS,what.centerLeft,what.centerTop,300,40);
	}
}

function closeMsgPopup(option,inbox,cookie_name,cookie_value,popup)
{
	for(i=0;i<option.length;++i)
	{
		if(option[i].checked)option=option[i].value;
	}
	switch(option)
	{
	case '1':
		location=inbox;
	break;
	case '2':
		document.cookie=cookie_name+'='+cookie_value;
	break;
	}

	if(option!=1)	moveMsgPopup(popup,true);
}
