// Catch possible bugs with WebTV and other older browsers
var is_regexp = (window.RegExp) ? true : false;

//-- Browsercheck
function BrowserCheck()
{
	var v;
	this.opera = navigator.userAgent.indexOf('Opera') > -1;
	this.konqueror = navigator.userAgent.indexOf('Konqueror') > -1;
	this.mac = navigator.userAgent.indexOf('Macintosh') > -1;
	this.ie = !this.opera && !!document.all && !!(v = navigator.userAgent.match(/MSIE (\d(\.\d)?)/));
	if (this.ie) this.ieversion = parseFloat(v[1]);
}
var is = new BrowserCheck();

function session_win(url, title) 
{
	window.open(url,title,"height=460,width=430,toolbar=no,statusbar=no,scrollbars=yes");
}

function brackets_window(url, title)
{
	window.open(url,title,"height="+(screen.height-100)+",width="+(screen.width-50)+",toolbar=no,statusbar=yes,scrollbars=yes");
}

// (Un)Check All
function checkAllItems(ref) 
{
  var chkAll = document.getElementById('checkAll');
  var checks = document.getElementsByName('delItem[]');
  var boxLength = checks.length;
  var allChecked = false;
  
  if ( ref == 1 ) 
  {
    if ( chkAll.checked == true ) 
	{
      for ( i=0; i < boxLength; i++ ) 
	  {
        checks[i].checked = true;
      }
    }
    else 
	{
      for ( i=0; i < boxLength; i++ ) 
	  {
        checks[i].checked = false;
      }
    }
  }
  else 
  {
    for ( i=0; i < boxLength; i++ ) 
	{
      if ( checks[i].checked == true ) 
	  {
        allChecked = true;
        continue;
      }
      else 
	  {
        allChecked = false;
        break;
      }
    }
    if ( allChecked == true ) 
	{
      chkAll.checked = true;
    }
    else 
	{
      chkAll.checked = false;
    }
  }
}

// Disable field
function disableField(formName, objectName)
{
	document.form.submit.disabled = true;
}

function alert_message(message)
{
	var agree = confirm(message);
		
	if(agree) {
		return true; 
	} 
	else { 
		return false; 
	}
}

// Roep aan met onClick="return del('iets')"
function del(x, msg)
{
	if(x=="" || x=="x" ) {
		
	    var agree=confirm(msg);
		
		if(agree) { return true; } else { return false; }
	}
	
	if(x=="msg") {		
	
	    var agree=confirm("Are you sure you want to delete this message?");
		
		if(agree) { return true; } else { return false; }
		
	}
	if(x=="close") {		
	
	    var agree=confirm("Are you sure you want to close this topic?");
		
		if(agree) { return true; } else { return false; }
		
	}
	if(x=="open") {		
	
	    var agree=confirm("Are you sure you want to open this topic?");
		
		if(agree) { return true; } else { return false; }
		
	}	
	if(x=="fr_cat") {		
	
	    var agree=confirm("Are you sure you want do delete this category?");
		
		if(agree) { return true; } else { return false; }
		
	}
	if(x=="fr_sub") {		
	
	    var agree=confirm("Are you sure you want do delete this sub-category?");
		
		if(agree) { return true; } else { return false; }
		
	}	
	if(x=="cat") {		
	
	    var agree=confirm("Are you sure you want do delete this category?");
		
		if(agree) { return true; } else { return false; }
		
	}
	if(x=="file") {		
	
	     var agree=confirm("Are you sure you want do delete this file?");
		
		if(agree) { return true; } else { return false; }
		
	}	
	if(x=="ladder") {		
	
	    var agree=confirm("Are you sure you want do delete this ladder?");
		
		if(agree) { return true; } else { return false; }
		
	}	
	if(x=="game") {		
	
	    var agree=confirm("Are you sure you want do delete this game?");
		
		if(agree) { return true; } else { return false; }
		
	}	
}

// BB FUNCTIE JAVESCRIPT
var target = null;
function bb(type, text) 
{
	target = fetch_object('messageBox');
	
	comp = navigator.userAgent.toLowerCase(); 
	vers = parseInt(navigator.appVersion);
	msie = ((comp.indexOf("msie") != -1) && (comp.indexOf("opera") == -1));
	nets = ((comp.indexOf('mozilla')!=-1) && (comp.indexOf('spoofer')==-1) && (comp.indexOf('compatible') == -1) && (comp.indexOf('opera')==-1) && (comp.indexOf('webtv')==-1) && (comp.indexOf('hotjava')==-1));
	win  = ((comp.indexOf("win")!=-1) || (comp.indexOf("16bit") != -1));
	
	if ((vers >= 4) && msie && win) 
	{
		text = document.selection.createRange().text;
		
		if (!text) 
		{
			target.value += det_replace(type, '');

			target.focus();
			return;
		}
		else
		{
			document.selection.createRange().text = det_replace(type, text);
			target.focus();
			return;
		}
		
		if (target.createTextRange) 
		{
			target.caretPos = document.selection.createRange().duplicate();
		}
	}
	else
	{
		if (target.cursorPos)
		{
			var cursorPos = target.cursorPos;
			if (type != 'plain') text = cursorPos.text;
			cursorPos.text = det_replace(type, text);
		}
		else if (target.selectionStart != undefined)
		{
			// remember scrollposition
			var scrollTop = target.scrollTop;
			var sStart = target.selectionStart;
			var sEnd = target.selectionEnd;
	
			if (type != 'plain') text = target.value.substring(sStart, sEnd);
			
			text = det_replace(type, text);
			target.value = target.value.substr(0, sStart) + text + target.value.substr(sEnd);
				
			var nStart = sStart == sEnd ? sStart + text.length : sStart;
			var nEnd = sStart + text.length;
				
			target.setSelectionRange(nStart, nEnd);
	
			// reset scrollposition
			target.scrollTop = scrollTop;
		}
		else
		{
			if (type != 'plain') text = '';
			target.value += det_replace(type, text);
		}
	
		target.focus();	
		if (target.cursorPos) target.onselect();
	}
}

function PutString(text)
{
	target = fetch_object('messageBox');
	
	target.value += det_replace('plain', text);
	target.focus();
}

function det_replace(type, text)
{
	var val = '';
	switch (type)
	{
		case 'plain':
			break;
		case 'AND':
			if (text != '')	text = text.replace(/(\w+)\s+/g, '$1 AND ');
			else text = ' AND ';
			break;
		case 'OR':
			if (text != '')	text = text.replace(/(\w+)\s+/g, '$1 OR ');
			else text = ' OR ';
			break;
		case 'brackets':
			if (text != '')	text = '('+text+')';
			else text = '()';
			break;
		case 'title':
			if (text != '')	text = 'title:('+text+')';
			else text = 'title:';
			break;
		case 'start':
			if (text != '')	text = 'start:('+text+')';
			else text = 'start:';
			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 'small':
			text = '[small]'+text+'[/small]';
			break;
		case 'left':
			text = '[left]'+text+'[/left]';
			break;
		case 'center':
			text = '[center]'+text+'[/center]';
			break;
		case 'right':
			text = '[right]'+text+'[/right]';
			break;
		case 'justify':
			text = '[justify]'+text+'[/justify]';
			break;
		case 'listbullet':
			text = '[list]\r\n[li]'+(text.split(/\r?\n/).join('[/li]\r\n[li]'))+'[/li]\r\n[/list]';
			break;
		case 'listnum':
			text = '[list=1]\r\n[li]'+(text.split(/\r?\n/).join('[/li]\r\n[li]'))+'[/li]\r\n[/list]';
			break;
		case 'bgcolor':
			val = prompt('Hexadecimale Code:','#');
			if (val !== null) text = '[bgcolor='+val+']'+text+'[/bgcolor]';
			break;
		case 'color':
			val = prompt('Hexadecimale Code:','#');
			if (val !== null) text = '[color='+val+']'+text+'[/color]';
			break;
		case 'url':
			if (/^(http:\/\/|www\.)/i.test(text))
			{
				val = prompt('Description:', text);
				if (val !== null && val != '') text = '[url='+text+']'+val+'[/url]';
			}
			else
			{
				val = prompt('URL:','http:\/\/');
				if (val !== null && val != 'http:\/\/')
				{
					if (text == '') text = '[url]'+val+'[/url]';
					else text = '[url='+val+']'+text+'[/url]';
				}
			}
			break;
		case 'img':
			if (text == '')
			{
				val = prompt('URL:','http:\/\/');
				if (val !== null && val != 'http:\/\/') text = '[img]'+val+'[/img]';
			}
			else
			{
				text = '[img]'+text+'[/img]';
			}
			break;
		case 'table':
			text = '[table border=1 width=350 cellpadding=2 bordercolor=#000000]\r\n[tr]\r\n[td]'+(text.split(/\r?\n/).join('[/td]\r\n[/tr]\r\n[tr]\r\n[td]'))+'[/td]\r\n[/tr]\r\n[/table]';
			break;
		case 'hr':
			text += '[hr]';
			break;
		case 'quote':
			text += '[/quote]\r\n[quote]';
			break;
	}

	return text;
}

function fetch_object(idname)
{
	if (document.getElementById)
	{
		return document.getElementById(idname);
	}
	else if (document.all)
	{
		return document.all[idname];
	}
	else if (document.layers)
	{
		return document.layers[idname];
	}
	else
	{
		return null;
	}
}

function fetch_cookie(name)
{
	cookie_name = name + '=';
	cookie_length = document.cookie.length;
	cookie_begin = 0;
	
	while (cookie_begin < cookie_length)
	{
		value_begin = cookie_begin + cookie_name.length;
		
		if (document.cookie.substring(cookie_begin, value_begin) == cookie_name)
		{
			var value_end = document.cookie.indexOf (';', value_begin);
			
			if (value_end == -1)
			{
				value_end = cookie_length;
			}
			
			return unescape(document.cookie.substring(value_begin, value_end));
		}
		
		cookie_begin = document.cookie.indexOf(' ', cookie_begin) + 1;
		
		if (cookie_begin == 0)
		{
			break;
		}
	}
	
	return null;
}

function set_cookie(name, value, expires)
{
	document.cookie = name + '=' + escape(value) + '; path=/' + (typeof expires != 'undefined' ? '; expires=' + expires.toGMTString() : '');
}

function delete_cookie(name)
{
	document.cookie = name + '=' + '; expires=Thu, 01-Jan-70 00:00:01 GMT' +  '; path=/';
}

function toggle_collapse(objid)
{
	if (!is_regexp)
	{
		return false;
	}

	obj = fetch_object('collapseobj_' + objid);
	img = fetch_object('collapseimg_' + objid);

	if (!obj)
	{
		// nothing to collapse!
		if (img)
		{
			// hide the clicky image if there is one
			img.style.display = 'none';
		}
		return false;
	}

	if (obj.style.display == 'none')
	{
		obj.style.display = '';
		save_collapsed(objid, false);
		if (img)
		{
			img_re = new RegExp("_collapsed\\.gif$");
			img.src = img.src.replace(img_re, '.gif');
		}
	}
	else
	{
		obj.style.display = 'none';
		save_collapsed(objid, true);
		if (img)
		{
			img_re = new RegExp("\\.gif$");
			img.src = img.src.replace(img_re, '_collapsed.gif');
		}
	}
	return false;
}

function save_collapsed(objid, addcollapsed)
{
	var collapsed = fetch_cookie('dms_forum_collapse');
	var tmp = new Array();

	if (collapsed != null)
	{
		collapsed = collapsed.split('\n');

		for (var i in collapsed)
		{
			if (collapsed[i] != objid && collapsed[i] != '')
			{
				tmp[tmp.length] = collapsed[i];
			}
		}
	}

	if (addcollapsed)
	{
		tmp[tmp.length] = objid;
	}
	
	var expires = new Date();
	expires.setTime(expires.getTime() + (1000 * 86400 * 365));
	set_cookie('dms_forum_collapse', tmp.join('\n'), expires);
}

function reload_game_icon(objvalue)
{
	current = document.form.icon_menu.selectedIndex;
	document.images.icon_container.src = site_url+"/images/game_icons/"+document.form.icon_menu[current].value;
}

function resizeImg()
{
	var _resizeWidth  = 520;
	var _resizeHeight = 520;
	var im = getElementsByClassName('img_rml', 'img'), i = im.length;

	while (i--)
	{
		var resized = false;
		var imgObj = im[i];
		var imgwidth = 0, imgheight = 0, extract_px = 0;
		
		var dum = new Image(); dum.src = imgObj.src;
		imgwidth = dum.width;
		imgheight = dum.height;	
		
			
		if(imgwidth > imgheight && imgwidth > _resizeWidth)
		{
			extract_px = imgwidth - _resizeWidth;
			imgObj.style.width = _resizeWidth + 'px';
			imgObj.style.height = (imgheight-extract_px) + 'px';
			
			resized = true;
		}
		else if(imgwidth < imgheight && imgheight > _resizeHeight)
		{
			extract_px = imgheight - _resizeHeight;
			imgObj.style.width = (imgwidth-extract_px) + 'px';
			imgObj.style.height = _resizeHeight + 'px';
			
			resized = true;
		}
		else if(imgwidth == imgheight && imgwidth > _resizeWidth && imgheight > _resizeHeight)
		{
			imgObj.style.width = _resizeWidth + 'px';
			imgObj.style.height = _resizeHeight + 'px';
			
			resized = true;
		}
		
		if(resized == true)
		{
			imgObj.onclick = openImage;	
			imgObj.title = 'This image is resized to fit on your screen. Click on the image to view the large version.';
			imgObj.className += ' img_resized';
		}			
	}
}

function getElementsByClassName(needle, tagname)
{
	if (!tagname) tagname = '*';

	var s = document.getElementsByTagName(tagname), i = s.length, e, r = [];

	var re = new RegExp('(^|\\s)' + needle + '(\\s|$)');


	while (i--)
	{
		e = s[i];

		if (e.className && re.test(e.className)) r.push(e);
	}

	return r;
}

function openImage()
{
	var winwidth = (this.naturalWidth < screen.availWidth ? this.naturalWidth : screen.availWidth);
    var winheight = (this.naturalHeight < screen.availHeight ? this.naturalHeight : screen.availHeight);
	
	window.open(site_url+"/showimage.html?"+this.src,'','width='+winwidth+',height='+winheight+',scrollbars=no,left=0,top=0');
}

window.onload = resizeImg;


