var d = document;

var incompetent_browser = document.all && navigator.userAgent.indexOf('MSIE') > -1 && navigator.userAgent.indexOf('Opera') == -1;
var atarget = null;
function initMessageBox(isqr)
{
	atarget = $$('message');
	if (atarget !== null)
	{
		if (isqr != '1') { atarget.focus(); }
		if (typeof atarget.createTextRange != 'undefined')
		{
			atarget.onkeydown = shortkey;
			atarget.onkeyup = storeCursor;
			atarget.onclick = storeCursor;
			atarget.onselect = storeCursor;
			atarget.onselect();
		}
		else
		{
			atarget.onkeypress = shortkey;
		}
	}
}
function storeCursor()
{
	this.cursorPos = document.selection.createRange().duplicate();
}

function det_replace(type, text)
{
	var aval = '';
	switch (type)
	{
		case 'plain':
			break;
		case 'bold':
			text = '[b]'+text+'[/b]';
			break;
		case 'italic':
			text = '[i]'+text+'[/i]';
			break;
		case 'underline':
			text = '[u]'+text+'[/u]';
			break;
		case 'strike':
			text = '[s]'+text+'[/s]';
			break;
		case 'sub':
			text = '[sub]'+text+'[/sub]';
			break;
		case 'sup':
			text = '[sup]'+text+'[/sup]';
			break;
		case 'spoiler':
			text = '[spoiler]'+text+'[/spoiler]';
			break;
		case 'code':
			text = '[code]'+text+'[/code]';
			break;
		case 'php':
			text = '[php]'+text+'[/php]';
			break;
		case 'quote':
			text = '[quote]'+text+'[/quote]';
			break;
		case 'me':
			text = '[me='+thisuser+']'+text+'[/me]';
			break;
		case 'ctext':
			text = '[ctext]'+text+'[/ctext]';
			break;
		case 'rtext':
			text = '[rtext]'+text+'[/rtext]';
			break;
		case 'listbullet':
			//text = '\r\n[*]'+(text.split(/\r?\n/).join('\r\n[*]'))+'\r\n';
			text = '\r\n[*]'+(text.split(/\r?\n/).join('\r\n[*]'));
			break;
		case 'url':
			if (/^(http:\/\/www\.)/i.test(text))
			{
				aval = prompt('Insert description:', text);
				if (aval !== null && aval !== '') {text = '[url='+text+']'+aval+'[/url]';}
			}
			else
			{
				aval = prompt('Insert URL:','http:\/\/');
				if (aval !== null && aval != 'http:\/\/')
				{
					if (text === '') {text = '[url]'+aval+'[/url]';}
					else {text = '[url='+aval+']'+text+'[/url]';}
				}
			}
			break;
		case 'img':
			if (text === '')
			{
				aval = prompt('Insert image URL:','http:\/\/');
				if (aval !== null && aval != 'http:\/\/') {text = '[img]'+aval+'[/img]';}
			}
			else
			{
				text = '[img]'+text+'[/img]';
			}
			break;
		case 'limg':
			if (text === '')
			{
				aval = prompt('Insert image URL:','http:\/\/');
				if (aval !== null && aval != 'http:\/\/') {text = '[limg]'+aval+'[/limg]';}
			}
			else
			{
				text = '[limg]'+text+'[/limg]';
			}
			break;
		case 'rimg':
			if (text === '')
			{
				aval = prompt('Insert image URL:','http:\/\/');
				if (aval !== null && aval != 'http:\/\/') {text = '[rimg]'+aval+'[/rimg]';}
			}
			else
			{
				text = '[rimg]'+text+'[/rimg]';
			}
			break;
		case 'youtube':
			if (text === '')
			{
				aval = prompt('Insert YouTube video URL:','http:\/\/');
				if (aval !== null && aval != 'http:\/\/') {text = '[youtube]'+aval+'[/youtube]';}
			}
			else
			{
				text = '[youtube]'+text+'[/youtube]';
			}
			break;
		case 'quotesplits':
			text += '[/quote]\r\n[quote]';
			break;
	}

	return text;
}

function shortkey(e)
{
	if (!e) {e = event;}

	var akey = 0;
	if (e.KeyCode) {key = e.KeyCode;}
	else if (e.which) {akey = e.which - 32;}

	if (e.ctrlKey && !e.shiftKey)
	{
		switch (akey)
		{
			case 66:
				putExt('bold');
				return cancelEvent(e);
			case 73:
				putExt('italic');
				return cancelEvent(e);
			case 83:
				putExt('strike');
				return cancelEvent(e);
			case 85:
				putExt('underline');
				return cancelEvent(e);
		}
	}

	return true;
}

function putStr(text)
{
	putExt('plain', text);
}

function AddSmile(text)
{
	putExt('plain', text);
}

function putExt(type, text)
{
	if (atarget !== null)
	{
		if (typeof atarget.cursorPos != 'undefined')
		{
			var cursorPos = atarget.cursorPos;
			if (type != 'plain') {text = cursorPos.text;}
			cursorPos.text = det_replace(type, text);
		}
		else if (typeof atarget.selectionStart != 'undefined')
		{
			// remember scrollposition
			var scrollTop = atarget.scrollTop;

			var sStart = atarget.selectionStart;
			var sEnd = atarget.selectionEnd;
			if (type != 'plain') {text = atarget.value.substring(sStart, sEnd);}
			text = det_replace(type, text);
			atarget.value = atarget.value.substr(0, sStart) + text + atarget.value.substr(sEnd);
			var nStart = sStart == sEnd ? sStart + text.length : sStart;
			var nEnd = sStart + text.length;
			atarget.setSelectionRange(nStart, nEnd);

			// reset scrollposition
			atarget.scrollTop = scrollTop;
		}
		else
		{
			if (type != 'plain') {text = '';}
			atarget.value += det_replace(type, text);
		}

		atarget.focus();
		if (typeof atarget.cursorPos != 'undefined') {atarget.onselect();}
	}
}

/*
AJAX
*/

function getHTTPObject(){
	if (window.ActiveXObject) 
		return new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest) 
		return new XMLHttpRequest();
	else {
		alert("Your browser does not support AJAX.");
		return null;
	}
}

function FQ(thisQuote){
	httpObject = getHTTPObject();
	if (httpObject != null) {
		httpObject.open("GET", "/getquote/"+thisQuote+"/", true);
	httpObject.send(null);
	httpObject.onreadystatechange = setOutput;
	}

	document.getElementById('message').focus();
}

function setOutput(){
	if(httpObject.readyState == 4){
		document.getElementById('message').value += httpObject.responseText;
	}
}

function rate(thisTopic,thisRate){
	httpObject = getHTTPObject();
	if (httpObject != null) {
		httpObject.open("GET", "/getrate/"+thisTopic+"/"+thisRate+"/", true);
	httpObject.send(null);
	httpObject.onreadystatechange = setRate;
	}
}

function setRate(){
	if(httpObject.readyState == 4){
		document.getElementById('topic_rating').innerHTML = httpObject.responseText;
	}
}

/*
DOM functies
*/

function add_bookmark(form) {
var name = form['titel'].value;
if(name) {
if (!name) {
alert("Bookmark is niet toegevoegd.");
return false;
}
if (name.length>0) {
alert("Bookmark wordt toegevoegd");
form['titel'].value=name;
return true;
//alert("Bookmark met titel: '"+titel+"' toegevoegd. Via de bookmarks-pagina kun je een omschrijving toevoegen.");
}
}else{
alert("Bookmark is niet toegevoegd.");
return false;
}

}

function $$() {
	var elements = new Array();
	for (var il = 0; il < arguments.length; il++) {
		var element = arguments[il];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}

/* hier begint client.js */
	var request;
	var response;
	var callback;
	var theParams;
	var getMessageField = '';

	if (typeof(basepath) == "undefined") {
	var	basepath = '/';
	}
	var getMessageField = ''; 






function ImagesResize() {
  var obj = document.getElementsByTagName("div");
  for(var i = 0;i<obj.length;i++) {
    if (obj[i].className == "bbimg") {
      var img = obj[i].getElementsByTagName("img");
      if (img[0].width > 500)
      {
        obj[i].innerHTML = '<div align=center><a href="javascript:photoWindow(\'/photopopup.php?url=' + img[0].src + '&width=' + img[0].width + '&height=' + img[0].height + '\',' + img[0].width + ',' + img[0].height + ')"><img style="border: dashed 1px #ff0000;" src="' + img[0].src + '" width="500"></a>' + '<br><table cellspacing="0" cellpadding="0" border="0"><tr><td align="center" height="15" style="width: 500px; border: 1px solid #CFCFCE; background: url(/Images/gradient.png);"><a class="textForumBlock" href="javascript:photoWindow(\'/photopopup.php?url=' + img[0].src + '&width=' + img[0].width + '&height=' + img[0].height + '\',' + img[0].width + ',' + img[0].height + ')"><b>Show image in full size</b> (' + img[0].width + ' x ' + img[0].height + ' pixels)</a></td></tr></table></div>';
      }
    }
  }
}

function popEdited(url,width,height)
{
  window.open(url, 'Posts', "width="+width+",height="+height+",scrollbars=yes");
}

function photoWindow(url,width,height)
{
  window.open(url, 'Picture', "width="+width+",height="+height+",scrollbars=no");
}

function show(object)
{
  if (document.getElementById && document.getElementById(object) != null)
  {
    node = document.getElementById(object).style.visibility='visible';
    node = document.getElementById(object).style.display='';
  }
  else if (document.layers && document.layers[object] != null)
  {
    document.layers[object].visibility = 'visible';
    document.layers[object].display= '';
  }
  else if (document.all)
  {
    document.all[object].style.visibility = 'visible';
    document.all[object].style.display= '';
  }
}

function hide(object)
{
  if (document.getElementById && document.getElementById(object) != null)
  {
    node = document.getElementById(object).style.visibility='hidden';
    node = document.getElementById(object).style.display='none';
  }
  else if (document.layers && document.layers[object] != null)
  {
    document.layers[object].visibility = 'hidden';
    document.layers[object].display= 'none';
  }
  else if (document.all)
  {
    document.all[object].style.visibility = 'hidden';
    document.all[object].style.display= 'none';
  }
}

function insertImage()
{
  var txtarea = document.posting.message;
  var insertImageLink = document.posting.insertImageLink.value;

  if (insertImageLink!='')
  {
    txtarea.value  += '[img]'+insertImageLink+'[/img]';
    txtarea.focus();
  }
  else
    alert('No url entered, the image cannot be inserted');
}

function insertLink()
{
  var txtarea = document.posting.message;
  var insertLinkURL = document.posting.insertLinkURL.value;
  var insertLinkTitle = document.posting.insertLinkTitle.value;

  if (insertLinkURL=='')
  {
    alert('No URL entered, the link cannot be inserted');
  }
  else
  {
    if (insertLinkTitle!='')
      txtarea.value  += '[url='+insertLinkURL+']'+insertLinkTitle+'[/url]';
    else
      txtarea.value  += '[url]'+insertLinkURL+'[/url]';
  }
}


function displayTopic(trColor,topicID,topicPosts,topicViews,topicPages,topicStatus,topicTitle,topicTitleFlat,topicLastPost,lastPostBy,lastPostID)
{
  d.write('        <tr>');
  d.write('          <td colspan="6" class="00"><img src="/S/spacer_black.png" height="1" width="100%" alt="black line"></td>');
  d.write('        </tr>');
  if (trColor==0)
    d.write('        <tr height="17">');
  else
    d.write('        <tr height="17" class="forumYellow">');

  d.write('          <td>');
  d.write('            <div class="spacing">');
  d.write('              <img src="/S/forum_'+topicStatus+'.gif" alt="Topic status image">');
  d.write('            </div>');
  d.write('          </td>');

  d.write('          <td width="55%">');
  d.write('           &nbsp;<a href="/topic/'+topicID+'/0/'+topicTitleFlat+'.php"><b>'+topicTitle+'</b></a>');

  if (topicPages>8)
  {
    for(i=1;i<=4;i++)
      d.write('           <font class="textForum"><a href="/topic/'+topicID+'/'+i+'/'+topicTitleFlat+'.php">'+i+'</a></font>');
    d.write('           ... ');
    for(i=topicPages-4;i<topicPages;i++)
      d.write('           <font class="textForum"><a href="/topic/'+topicID+'/'+i+'/'+topicTitleFlat+'.php">'+i+'</a></font>');
  }
  else
  {
    for(i=1;i<topicPages;i++)
      d.write('           <font class="textForum"><a href="/topic/'+topicID+'/'+i+'/'+topicTitleFlat+'.php">'+i+'</a></font>');
  }
  d.write('          </td>');

  d.write('          <td width="7%" nowrap>');
  d.write('            <font class="textForum">'+topicViews+'</font>');
  d.write('          </td>');

  d.write('          <td width="7%" nowrap>');
  d.write('            <font class="textForum">'+topicPosts+'</font>');
  d.write('          </td>');

  d.write('          <td width="18%" nowrap>');
  d.write('            <font class="textForum">&nbsp;<a href="/profile/'+lastPostID+'/" target="_blank">'+lastPostBy+'</a></font>');
  d.write('          </td>');

  d.write('          <td width="18%" nowrap>');
  d.write('            <font class="textForum">'+topicLastPost+'&nbsp;</font>');
  d.write('          </td>');

  d.write('        </tr>');
}

function displayForumHead(forumName,forumLanguage)
{
  if (forumLanguage=='en') rss = 'english';
  if (forumLanguage=='nl') rss = 'dutch';
  if (forumLanguage=='id') rss = 'indonesian';

  d.write('        <tr>');
  d.write('          <td colspan="6" class="00"><img src="/S/spacer_black.png" height="1" width="100%" alt="black line"></td>');
  d.write('        </tr>');
  d.write('        <tr style=" background: url(/Images/gradient.png);">');
  d.write('          <td width="68%" colspan="2" height="20">');
  d.write('            <div class="spacing"><img src="/Images/'+forumLanguage+'.gif" width="18" height="13" style="margin-right: 5px;"><a href="http://forum.indahnesia.com/forum_active_topics_'+rss+'.xml" target="_blank"><img border="0" src="/Images/rss20.png" style="margin-right: 5px;"></a><font class="headforum"><b>'+forumName+'</b></font></div>');
  d.write('          </td>');
  d.write('          <td width="7%">');
  d.write('            <b>Topics</b>');
  d.write('          </td>');
  d.write('          <td width="7%">');
  d.write('            <b>Posts</b>');
  d.write('          </td>');
  d.write('          <td width="7%">');
  d.write('            <b>Views</b>');
  d.write('          </td>');
  d.write('          <td width="18%">');
  d.write('            <b>Last post</b>');
  d.write('          </td>');
  d.write('        </tr>');
}

function displayForum(forumID,forumTopics,forumPosts,forumViews,forumLastPost,forumName,forumStatus,forumDescription)
{
  d.write('        <tr>');
  d.write('          <td colspan="6" class="00"><img src="/S/spacer_black.png" height="1" width="100%" alt="black line"></td>');
  d.write('        </tr>');
  d.write('        <tr>');
  d.write('          <td height="25" vaign="middle" align="center">');
  d.write('           &nbsp;&nbsp;&nbsp;<img src="/S/forum_'+forumStatus+'.gif" alt="Topic status icon">');
  d.write('          </td>');
  d.write('          <td>');
  d.write('            <div class="spacing">');
  d.write('              <a class="titleforum" href="/forum/'+forumID+'/">'+forumName+'</A><BR>');
  d.write('              <font class="textForum">'+forumDescription+'</font>');
  d.write('            </div>');
  d.write('          </td>');
  d.write('          <td valign="bottom">');
  d.write('            <font class="textForum">'+forumTopics+'</font>');
  d.write('          </td>');
  d.write('          <td valign="bottom">');
  d.write('            <font class="textForum">'+forumPosts+'</font>');
  d.write('          </td>');
  d.write('          <td valign="bottom">');
  d.write('            <font class="textForum">'+forumViews+'</font>');
  d.write('          </td>');
  d.write('          <td valign="bottom" nowrap>');
  d.write('            <font class="textForum">'+forumLastPost+'&nbsp;</font>');
  d.write('          </td>');
  d.write('        </tr>');
}

function displayAT(topicCounter,forumGroup,forumID,topicStatus,topicID,topicPosts,postsPerPage,topicTitle,topicTitleFull,topicStatus,topicBlueMark,forumName,topicLastPost,userLastLogin,topicLastPostLong,topicLastReplyBy,topicLastReplyID,viewAction,viewActionNumber)
{
  var aantalCols = 4;
  if (viewAction!="")
    aantalCols = 5;

  var displayTopicStatus = "";
  var i = 0;
  var totalPosts = topicPosts+1;

  if (topicStatus=="0")
  {
    displayTopicStatus = "normal";
    if ((userLastLogin<topicLastPost) && (userLastLogin!="0"))
      displayTopicStatus = "normal_new";
  }
  if (topicStatus=="1")
    displayTopicStatus = "sticky";

  if (topicStatus=="9")
  {
    displayTopicStatus = "locked";
    if ((userLastLogin<topicLastPost) && (userLastLogin!="0"))
      displayTopicStatus = "locked_new";
  }

  d.write('        <tr>');
  d.write('          <td colspan="'+aantalCols+'" class="00"><img src="/S/spacer_black.png" height="1" width="100%" alt="black line"></td>');
  d.write('        </tr>');

  if (topicBlueMark==1)
    d.write('        <tr class="forumYellow">');
  else
    d.write('        <tr>');

  d.write('          <td height="21">');
  d.write('            <div class="spacing">');
  d.write('            <a onmouseover="show(\'forum_'+topicCounter+'\')" onmouseout="hide(\'forum_'+topicCounter+'\')" href="/forum/'+forumID+'/" title="This is a topic in the '+forumName+' subforum"><font face="courier"><b>'+forumGroup+'</b></font></a>');
  d.write('            </div>');
  d.write('          </td>');

  d.write('          <td width="90%">');
  d.write('            <div id="forum_'+topicCounter+'" style="margin-top: 0px; margin-left: -10px; position:absolute; visibility: hidden;">');
  d.write('              <table cellspacing="0" cellpadding="1" border="0" class="colorTable">');
  d.write('                <tr class="menuTable">');
  d.write('                  <td>');
  d.write('                    <font class="date">This is a topic in the '+forumName+' subforum</font>');
  d.write('                  </td>');
  d.write('                </tr>');
  d.write('              </table>');
  d.write('            </div>');
  d.write('            <img src="/S/forum_'+displayTopicStatus+'.gif" alt="Topic status image">');
  d.write('            <a href="/topic/'+topicID+'/0/'+topicTitle+'.php"><b>'+topicTitleFull+'</b></a>');

  if (totalPosts>postsPerPage)
  {
    var aantalPaginas= Math.ceil(totalPosts/postsPerPage);

    if (aantalPaginas>8)
    {
      for(i=1;i<=4;i++)
        d.write('            <a href="/topic/'+topicID+'/'+i+'/'+topicTitle+'.php" class="textforumblock">'+i+'</a>');
      d.write('            ... ');
      for(i=aantalPaginas-4;i<aantalPaginas;i++)
        d.write('            <a href="/topic/'+topicID+'/'+i+'/'+topicTitle+'.php" class="textforumblock">'+i+'</a>');
    }

    else
    {
      for(i=1;i<aantalPaginas;i++)
        d.write('            <a href="/topic/'+topicID+'/'+i+'/'+topicTitle+'.php" class="textforumblock">'+i+'</a>');
    }
  }

  d.write('          </td>');
  d.write('          <td nowrap>');
  d.write('            <font class="textForum">&nbsp;'+topicLastPostLong+'</font>');
  d.write('          </td>');
  d.write('          <td width="10%" align="right" nowrap>');
  d.write('            <font class="textForum"><a href="/profile/'+topicLastReplyID+'/" target="_blank">'+topicLastReplyBy+'</a>&nbsp;</font>&nbsp;');
  d.write('          </td>');

  if (viewAction!="")
  {
    d.write('          <td width="40">');
    d.write('            <font class="textForum"><b>'+viewActionNumber+'</b></font>');
    d.write('          </td>');
  }
  d.write('        </tr>');
}

function displayATb(topicCounter,forumGroup,forumID,topicStatus,topicID,topicPosts,postsPerPage,topicTitle,topicTitleFull,topicStatus,topicBlueMark,forumName,topicLastPost,userLastLogin,topicLastPostLong,topicLastReplyBy,topicLastReplyID,viewAction,viewActionNumber)
{
  var aantalCols = 4;
  if (viewAction!="")
    aantalCols = 5;

  var displayTopicStatus = "";
  var i = 0;
  var totalPosts = topicPosts+1;

  if (topicStatus=="0")
  {
    displayTopicStatus = "normal";
    if ((userLastLogin<topicLastPost) && (userLastLogin!=""))
      displayTopicStatus = "normal_new";
  }
  if (topicStatus=="1")
    displayTopicStatus = "sticky";

  if (topicStatus=="9")
  {
    displayTopicStatus = "locked";
    if ((userLastLogin<topicLastPost) && (userLastLogin!=""))
      displayTopicStatus = "locked_new";
  }

  d.write('        <tr>');
  d.write('          <td colspan="'+aantalCols+'" class="00"><img src="/S/spacer_black.png" height="1" width="100%" alt="black line"></td>');
  d.write('        </tr>');

  if (topicBlueMark==1)
    d.write('        <tr class="forumYellow">');
  else
    d.write('        <tr>');

  d.write('          <td height="21" nowrap>');
  d.write('            <div class="spacing">');
  d.write('            <a href="/profile/track/'+topicID+'/1/"><img border="0" src="/S/forum_delete.gif" alt="remove this topic from the list"></a>&nbsp;');
  d.write('            <a onmouseover="show(\'forum_'+topicCounter+'\')" onmouseout="hide(\'forum_'+topicCounter+'\')" href="/forum/'+forumID+'/" title="This is a topic in the '+forumName+' subforum"><font face="courier"><b>'+forumGroup+'</b></font></a>');
  d.write('            </div>');
  d.write('          </td>');

  d.write('          <td width="100%">');
  d.write('            <div id="forum_'+topicCounter+'" style="margin-top: 0px; margin-left: -10px; position:absolute; visibility: hidden;">');
  d.write('              <table cellspacing="0" cellpadding="1" border="0" class="colorTable">');
  d.write('                <tr class="menuTable">');
  d.write('                  <td>');
  d.write('                    <font class="date">This is a topic in the '+forumName+' subforum</font>');
  d.write('                  </td>');
  d.write('                </tr>');
  d.write('              </table>');
  d.write('            </div>');
  d.write('            <img src="/S/forum_'+displayTopicStatus+'.gif" alt="Topic status image">');
  d.write('            <a href="/topic/'+topicID+'/0/'+topicTitle+'.php"><b>'+topicTitleFull+'</b></a>');

  if (totalPosts>postsPerPage)
  {
    var aantalPaginas= Math.ceil(totalPosts/postsPerPage);

    if (aantalPaginas>8)
    {
      for(i=1;i<=4;i++)
        d.write('            <a href="/topic/'+topicID+'/'+i+'/'+topicTitle+'.php" class="textforumblock">'+i+'</a>');
      d.write('            ... ');
      for(i=aantalPaginas-4;i<aantalPaginas;i++)
        d.write('            <a href="/topic/'+topicID+'/'+i+'/'+topicTitle+'.php" class="textforumblock">'+i+'</a>');
    }

    else
    {
      for(i=1;i<aantalPaginas;i++)
        d.write('            <a href="/topic/'+topicID+'/'+i+'/'+topicTitle+'.php" class="textforumblock">'+i+'</a>');
    }
  }

  d.write('          </td>');
  d.write('          <td nowrap>');
  d.write('            <font class="textForum">&nbsp;'+topicLastPostLong+'</font>');
  d.write('          </td>');
  d.write('          <td>');
  d.write('            &nbsp;<font class="textForum"><a href="/profile/'+topicLastReplyID+'/" target="_blank">'+topicLastReplyBy+'</a>&nbsp;</font>');
  d.write('          </td>');

  if (viewAction!="")
  {
    d.write('          <td width="40">');
    d.write('            <font class="textForum"><b>'+viewActionNumber+'</b></font>');
    d.write('          </td>');
  }
  d.write('        </tr>');
}
