function checkValidThread()
{
  formTrim(document.text.subject);
  formTrim(document.text.message);

	if (document.text.forum_id.value == 0)
  {
		alert("Du måste välja i vilket forum du vill skapa din tråd.");
		document.text.forum_id.focus();
		return false;
	}
	else if (document.text.subject.value.length < 3 || document.text.subject.value == 'Rubrik...' )
  {
		alert("Ditt inlägg måste ha en rubrik på minst tre tecken.");
		document.text.subject.focus();
		return false;
	}
  else if (document.text.message.value.length < 5)
  {
		alert("Inlägget måste vara på minst fem tecken.");
		document.text.message.focus();
		return false;
	}
  else
  {
		return true;
	}
}


function checkValidAdvanced()
{
  formTrim(document.text.subject);
  formTrim(document.text.poll_question);
  formTrim(document.text.poll_choice_text1);
  formTrim(document.text.poll_choice_text2);
  formTrim(document.text.poll_choice_text3);
  formTrim(document.text.poll_choice_text4);
  formTrim(document.text.poll_choice_text5);
  formTrim(document.text.poll_choice_text6);
  formTrim(document.text.poll_choice_text7);
  formTrim(document.text.poll_choice_text8);
  formTrim(document.text.poll_choice_text9);
  formTrim(document.text.poll_choice_text10);
  formTrim(document.text.message);

	if (document.text.forum_id.value == 0)
  {
		alert("Du måste välja i vilket forum du vill skapa din tråd.");
		document.text.forum_id.focus();
		return false;
	}
	else if (document.text.subject.value.length < 3)
  {
		alert("Ditt inlägg måste ha en rubrik på minst tre tecken.");
		document.text.subject.focus();
		return false;
	}
  else if ((document.text.message.value.length < 5) && (document.text.poll_choice_text1.value.length < 1))
  {
		alert("Inlägget måste vara på minst fem tecken.");
		document.text.message.focus();
		return false;
	}
  else
  {
		return true;
	}
}


function checkValidReply()
{
  formTrim(document.text.message);

	if (document.text.message.value.length < 2)
  {
		alert("Kommentaren måste vara på minst två tecken.");
		document.text.message.focus();
		return false;
	}
  else {
		return true;
	}
}


function checkValidSearch()
{
  formTrim(document.search.sq);
  formTrim(document.search.su);

  if (document.search.sq.value.length < 3)
  {
		alert("Din söktext måste vara på minst tre tecken.");
		document.search.sq.focus();
		return false;
	}
  else
  {
		return true;
	}
}


var startQuoteUser = '';

function doAddQuote(user)
{
  var string = '';

	if (window.getSelection)
	{
    string = window.getSelection();
	}
	else if (document.getSelection)
	{
		string = document.getSelection();
	}
	else if (document.selection && document.selection.createRange)
	{
		string = document.selection.createRange().text;
	}	
  else
  {
		alert("Citeringsfunktionen fungerar tyvärr inte med din webläsare.");
	}

  string = string + '';
  string = string.replace(/(^\s*)|(\s*$)/g, '');    

	if (string.length == 0)
  {
		alert("Du måste markera den text du vill citera (max 600 tecken per citering).");
	}
  else if (string.length > 600)
  {
		var marked = string.length;
		alert("Du kan inte citera mer än 600 tecken per citeringsruta (du har markerat " + marked + " tecken).");
	}
  else if (user != startQuoteUser)
  {
		alert("Felaktig textmarkering. Vänligen kontrollera att du markerat rätt text.");
  }
  else
  {
    if (user == '') user = 'Okänd';
//    document.text.message.focus();
		document.text.message.value += (document.text.message.value.length>0?"\n\n":"") + "[quote=" + user + "]" + string + "[/quote]\n";
//    document.text.message.focus();
	}
}


function doEditText(id,page)
{
  window.location='forum.php?text_id=' + id + '&page=' + page + '#bottom';
}


function getRemoveMultiCount()
{
  count='';
  button = 0;
  while (true)
  {
    button++;
    var box = getHtmlElement('removebox' + button);
    if (!box) break;
    if (box.checked)
    {
      count++;
    }
  }
  return count;
}


function getRemoveMultiId()
{
  id='';
  button = 0;
  while (true)
  {
    button++;
    var box = getHtmlElement('removebox' + button);
    if (!box) break;
    if (box.checked)
    {
      if (id!='') id=id+',';
      id=id+box.value;
    }
  }
  return id;
}


function onRemoveCheck()
{
  checkBoxCount=getRemoveMultiCount();
  button = 0;
  while (true)
  {
    button++;
    var box = getHtmlElement('removebox' + button);
    var btn = getHtmlElement('removebutton' + button);
    if (!box || !btn) break;

    btn.disabled = (checkBoxCount && !box.checked);
    if (checkBoxCount && !box.checked)
    {
    	btn.src = 'gfx/icon_delete_gray.gif';
    }
    else
    {
    	btn.src = 'gfx/icon_delete.gif';
    }
  }
}


function doMergeTexts(thread, id, page)
{
  checkBoxCount=getRemoveMultiCount();
  if (checkBoxCount > 1)
  {
	  if (window.confirm('Är du säker på att du vill lägga samman ' + checkBoxCount + ' inlägg?'))
	  {
	    if (checkBoxCount) id=getRemoveMultiId();
	    window.location='forum.php?thread_id=' + thread + '&text_id=' + id + '&page=' + page + '&action=merge';
	  }
	}
	else
	{
		alert("Du måste välja två eller fler svar att slå ihop.");
	}
}


function doRemoveText(id,page)
{
  checkBoxCount=getRemoveMultiCount();
  if (window.confirm('Är du säker på att du vill ta bort ' + (checkBoxCount<=1?'inlägget':checkBoxCount+' inlägg') + '?'))
  {
    if (checkBoxCount) id=getRemoveMultiId();
    window.location='forum.php?text_id=' + id + '&page=' + page + '&action=remove';
  }
}


function doRemoveTextAttach(id,page)
{
  checkBoxCount=getRemoveMultiCount();
  if (window.confirm('Är du riktigt säker på att du vill ta bort ' + (checkBoxCount<=1?'inlägget och alla bilder':checkBoxCount+' inlägg') + '?'))
  {
    if (checkBoxCount) id=getRemoveMultiId();
    window.location='forum.php?text_id=' + id + '&page=' + page + '&action=remove';
  }
}


function doMoveThread(id)
{
  var tf = getHtmlElement('move_to_forum');
  if (tf && tf.value)
  {
    var opt = tf.options[tf.selectedIndex];
    if (opt && window.confirm('Detta kommer flytta tråden till forum "' + opt.getAttribute('forumName') + '". Är du säker på att du vill fortsätta?'))
    {
      window.location='forum.php?thread_id=' + id + '&action=move&to_forum=' + tf.value;
    }
  }
}


function doUnlockThread(id)
{
  if (window.confirm('Låsa upp tråden?'))
  {
    window.location='forum.php?thread_id=' + id + '&action=unlock';
  }
}

function doCloseThread(id)
{
  if (window.confirm('Vill du verkligen låsa tråden?'))
  {
    window.location='forum.php?thread_id=' + id + '&action=lock';
  }
}


function doRemoveThread(id)
{
  if (window.confirm('Detta kommer att ta bort hela tråden och alla svar. Är du säker på att du vill fortsätta?'))
  {
    window.location='forum.php?thread_id=' + id + '&action=remove';
  }
}


function doStickyThread(id)
{
  if (window.confirm('Klistra tråden?'))
  {
    window.location='forum.php?thread_id=' + id + '&action=sticky';
  }
}


function doUnStickyThread(id)
{
  if (window.confirm('Avklistra tråden?'))
  {
    window.location='forum.php?thread_id=' + id + '&action=unsticky';
  }
}


function doRestoreThread(id)
{
  var tf = getHtmlElement('move_to_forum');
  if (tf && tf.value)
  {
    var opt = tf.options[tf.selectedIndex];
    if (opt && window.confirm('Detta kommer återställa tråden till forum "' + opt.getAttribute('forumName') + '". Ok?'))
    {
      window.location='forum.php?thread_id=' + id + '&action=restore&to_forum=' + tf.value;
    }
  }
	else if (tf)
	{
		alert('Du måste välja ett forum att placera tråden i.');
	}
}


function doRemoveThreadPoll(id)
{
  if (window.confirm('Detta kommer att ta bort hela tråden, alla svar, undersökning samt alla registrerade svar. Är du riktigt säker på att du vill fortsätta?'))
  {
    window.location='forum.php?thread_id=' + id + '&action=remove';
  }
}


function doRemoveThreadAttach(id)
{
  if (window.confirm('Detta kommer att ta bort hela tråden, alla svar och alla bilagda bilder. Är du riktigt säker på att du vill fortsätta?'))
  {
    window.location='forum.php?thread_id=' + id + '&action=remove';
  }
}


function doRemoveFile(code,page)
{
  if (window.confirm('Är du säker på att du vill ta bort bilden?'))
  {
    window.location='forum.php?attachment_code=' + code + '&action=remove&page=' + page;
  }
}


function doPollVote(id,no)
{
  window.location='forum.php?poll_id=' + id + "&choice_id=" + no + "&action=vote";
}


function doPollAlertLogin()
{
  alert('Du måste vara inloggad för att kunna rösta.');
}


function doPollAlertSexAge()
{
  alert('Du måste komplettera dina användaruppgifter med kön och ålder för att kunna rösta.');
}


//

function doPollShow()
{
  getHtmlElement('poll_area_noinput').style.display = 'none';
  getHtmlElement('poll_area_input').style.display = 'block';
  document.text.poll_ok.value='1';
  document.text.poll_question.focus();
}

function doPollHide()
{
  getHtmlElement('poll_area_noinput').style.display = 'block';
  getHtmlElement('poll_area_input').style.display = 'none';
  document.text.poll_ok.value='';
}


function doSetupCategory(category_id, forum_id)
{
  var fcid = getHtmlElement('forum_category_id');
	fcid.options.length = 0;

  if (category_id == 0 && forum_id > 0)
  {
  	for (i = 0; i < forum_arr.length; i = i + 6)
  	{
  		if (forum_arr[i] == forum_id)
  		{
  		  category_id = forum_arr[i+1];
  		  break;
  		}
  	}
  }

	for (i = 0; i < category_arr.length; i = i + 3)
	{
		fcid.options[fcid.options.length] = new Option(category_arr[i+1], category_arr[i]);
		if (category_arr[i] == category_id && category_id > 0) fcid.options.item(fcid.options.length-1).selected = true;
	}

  onSelectCategory();
}


function onSelectCategory()
{
//  getHtmlElement('category_description').innerHTML = '';
  
  var fcid = getHtmlElement('forum_category_id');
  if (fcid && fcid.value > 0)
  {
  	for (i = 0; i < category_arr.length; i = i + 3)
  	{
  	  if (category_arr[i] == fcid.value)
  	  {
//        getHtmlElement('category_description').innerHTML = '&gt; ' + category_arr[i+2];
        break;
      }
    }
  }
  
  doSetupForum(0);
}


function doSetupForum(forum_id)
{
  var fcid = getHtmlElement('forum_category_id');
  var fid = getHtmlElement('forum_id');
	fid.options.length = 0;

  if (fcid.value > 0)
  {
  	for (i = 0; i < forum_arr.length; i = i + 6)
  	{
  		if (forum_arr[i+1] == 0 || forum_arr[i+1] == fcid.value)
  		{
    		fid.options[fid.options.length] = new Option(forum_arr[i+2], forum_arr[i]);
    		if (forum_arr[i] == forum_id && forum_id > 0) fid.options.item(fid.options.length-1).selected = true;
  		}
  	}
  }
  else
  {
 		fid.options[fid.options.length] = new Option('Välj kategori först...', 0);
  }
	
	onSelectForum();
}


function doForumSlide(id)
{
	$("#slideDiv"+id+"").animate({
    "width": "toggle", "opacity": "toggle"
  }, { duration: "fast" });
	
	if ($('#slideIcon'+id).attr('src') == '/gfx/icon_plus.gif')
	{
		$('#slideIcon'+id).attr('src', '/gfx/icon_minus.gif');
	}
	else
	{
		$('#slideIcon'+id).attr('src', '/gfx/icon_plus.gif');
	}
}


function onSelectForum()
{
//	getHtmlElement('forum_description').innerHTML = '';
	
  var fid = getHtmlElement('forum_id');
  if (fid && fid.value > 0)
  {
  	for (i = 0; i < forum_arr.length; i = i + 6)
  	{
  		if (forum_arr[i] == fid.value)
  		{
//				getHtmlElement('forum_description').innerHTML = '&gt; ' + forum_arr[i+3];

        var poll_area = getHtmlElement('poll_area');
        if (poll_area)
        {
          if (forum_arr[i+5] > 0)
          {
            poll_area.style.display = 'block';
          }
          else
          {
            doPollHide();
            poll_area.style.display = 'none';
          }
        }
        
        var upload_area = getHtmlElement('upload_area');
        if (upload_area)
        {
          if (forum_arr[i+4] > 0)
          {
            upload_area.style.display = 'block';
          }
          else
          {
            upload_area.style.display = 'none';
          }
        }
        
        break;
      }
    }
  }
}
