// File:		$Id: any.js 22402 2006-07-26 15:24:23Z bas $
// Copyright:	(c) 2003-2005 Mediamatic
// Description:	misc. support functions for anymeta sites

//

// Check for netscape or internet explorer
var gAppName   = navigator.appName.toLowerCase();
var gUserAgent = navigator.userAgent.toLowerCase();
var gNetscape  = gAppName.indexOf("netscape") >= 0;
var gMacOS     = gUserAgent.indexOf("mac") >= 0;
var gW3C       = document.getElementById != null;

// Function:	resizePopup
// Parameters:	
// Returns:		object
// Description:	resizes a popup window
//
var isNN;
var mw = 400;
if (parseInt(navigator.appVersion.charAt(0))>=4)
{
	NN = (navigator.appName=="Netscape") ? 1 : 0;
}
function resizePopup()
{
	if (document.images[0])
	{
		ww=(screen.width);
		wh=(screen.height);

		if (NN)
		{
			rw = document.images[0].width + 80;
			rh = document.images[0].height;
			if (ww < rw) {rw = ww;}
			if (rw < mw) {rw = mw;}
			if (wh < rh) {rh = wh;}
			if (rh < wh - 140) {rh = rh + 160}
			window.top.innerWidth  =rw;
			window.top.innerHeight =rh;
		}
		else
		{
			rw=document.images[0].width + 80;
			rh=document.images[0].height;
			if (ww < rw) {rw = ww;}
			if (rw < mw) {rw = mw;}
			if (wh < rh) {rh = wh;}
			if (rh < wh - 140) {rh = rh + 160}
			window.top.resizeTo(rw,rh);
		}
		parent.window.moveTo((screen.width - rw)/2, (screen.height - rh)/2);
	}
}


// Function:	getObj
// Parameters:	name of object
// Returns:		object
// Description:	creates object with right DOM
//
function getObj(name)
{
  if (document.getElementById && document.getElementById(name))
  {
  	this.obj = document.getElementById(name);
	this.style = document.getElementById(name).style;
  }
  else if (document.all && document.all[name])
  {
	this.obj = document.all[name];
	this.style = document.all[name].style;
  }
  else if (document.layers && document.layers[name])
  {
	this.obj = getObjNN4(document,name);
	this.style = this.obj;
  }
}

// Function:	getObjNN4
// Parameters:	name of object
// Returns:		layer
// Description:	creates object with right nested layers for NN4 
//
function getObjNN4(obj,name)
{
	var x = obj.layers;
	var foundLayer;
	for (var i=0;i<x.length;i++)
	{
		if (x[i].id == name)
		 	foundLayer = x[i];
		else if (x[i].layers.length)
			var tmp = getObjNN4(x[i],name);
		if (tmp) foundLayer = tmp;
	}
	return foundLayer;
}

function showObj(o)
{
	var x = new getObj(o);
	if (x.obj) x.style.display = "block";
}

function hideObj(o) 
{
	var x = new getObj(o);
	if (x.obj) x.style.display = "none";
}

// Function:	swapObj
// Parameters:	o	string or object
// Description:	Swaps the given object(s)
//				o could be an object swapObj({01,02})
//
function swapObj(o) {
	if (typeof o == 'object') {
		for (var p in o) {
			swapHideShowObj(o[p])
		}
	} else if (isArray(o)) {
		for (i=0;i<o.length;i++) {
			swapHideShowObj(o[i]);
		}
	} else {
		swapHideShowObj(o);
	}		
}

// Function:	swapHideShowObj
// Parameters:	o	string
// Description:	Swaps the given object depending on display
//
function swapHideShowObj(o) {
	var x = new getObj(o);
	if(x.style.display == "block") {
		hideObj(o)
	} else {
		showObj(o);
	}
}

// Function:	hideObjAll
// Parameters:	a	which obj to hide (can be an array) 				
//				b	which obj should openend afterwards (hide all but ...)
//				s	if s = true search for objects starting with the string a
// Description:	Hide all obj in an array or string and optionally show obj b (can be an array)
//
function hideObjAll ( a, b, s ) 
{
	if (typeof a == 'object') {
		for (var p in a) {
			hideObj(a[p])
		}
	} else if (isArray(a)) {
		for (i=0;i<a.length;i++) {
			hideObj(a[i]);
		}
	} else if (isString(a)) {
		if (s) {
			var d = document.getElementsByTagName('div');
			for(i=0;i<d.length;i++) {
				var l = a.length;
				var id = d[i].id;
				if (id.substring(0,l) == a) {
					hideObj(d[i].id);
				}
			}
		} else {
			hideObj(a);
		}
	}
	if (isArray(b)) {
		for (i=0;i<a.length;i++) {
			showObj(b[i]);
		}
	} else if (isString(b)) {
		showObj(b);
	}
}

function moveObj ( o, x, y )
{
	// Move the animation
	//elt = anyMapGetElt(nm);
	var x = new getObj(o);
	if (nm)
	{
		if (gW3C || !gNetscape)
		{
			x.style.top  = eval('"'+y+'px"');
			x.style.left = eval('"'+x+'px"');
		}
		else
		{
			x.top  = y;
			x.left = x;
		}
	}
}


// Function:	changeEditLink
// Parameters:	e	event
//				o	object
// Description:	if one holds the shift key is use the (optional) eHref as href.
//				this is used for editing in admin environment
//
function changeEditLink (e,o) {
	if (e.shiftKey) {
		if (o.getAttribute('eHref'))
		{
			o.target = '_blank';
			o.href   = o.getAttribute('eHref');
		}
	}
}


// Function:	resetForm
// Parameters:	form	form to be resetted
// Returns:		-
// Description:	when a form is entered twice (ie. in actions), first reset the form
//
function resetForm(form)
{
	var f = document.getElementById('f_'+form);
	if (!f)
	{
		f = document.forms['f_'+form];
	}
	if (f)
	{
		f.reset();
		showhide("frm_"+form+",-wait_"+form+",-ok_"+form);
	}
}

// Function:	showhide
// Parameters:	s		comma separated string with -/+ prefixed div ids
// Returns:		-
// Description:	accepts a comma separated string of div ids.  each id 
//				can be prefixed with a + or -.  Default is +.
//				a + prefix (or no prefix) show the given div
//				a - prefix hides the given div
//
function showhide(s)
{
	if (s != '' && s != null)
	{
		a = s.split(',');
		for (var i=0; i<a.length; i++)
		{
			s = a[i];
			c = s.charAt(0);
			v = 'block';
			if (c == '-')
			{
				v = 'none';
				s = s.substr(1,s.length-1);
			}
			else if (c == '+')
			{
				s = s.substr(1,s.length-1);
			}
			
			if (document.getElementById(s))
			{
				document.getElementById(s).style.display = v;
			}
			else
			{
				alert('rest show/hide: no elt "' + s + '"');
			}
		}
	}
}


function moveObj ( nm, x, y )
{
	// Move the animation
	//elt = anyMapGetElt(nm);
	elt = new getObj(nm);
	if (nm)
	{
		if (gW3C || !gNetscape)
		{
			elt.style.top  = eval('"'+y+'px"');
			elt.style.left = eval('"'+x+'px"');
		}
		else
		{
			elt.top  = y;
			elt.left = x;
		}
	}
}


// Function:	trim
// Parameters:	strText (string)
// Returns:		string without leading and trailing spaces
// Description:	trims leading and trailing spaces from a given string
//
function trim(strText) 
{ 
	while (strText.substring(0,1) == ' ') 
    	strText = strText.substring(1, strText.length);
    while (strText.substring(strText.length-1,strText.length) == ' ')
    	strText = strText.substring(0, strText.length-1);
	return strText;
}


// Function:	getvalue
// Parameters:	ffield
// Returns:		value of the formfield
//
function getvalue(ffield)
{
  v="";
  t=ffield.type;
  if(!t)t=ffield[0].type;
  if(t=="checkbox")if(ffield.checked)v=ffield.value;
  if(t=="file") v=ffield.value;
  if(t=="hidden") v=ffield.value;
  if(t=="password") v=ffield.value;

  if(t=="radio")
  {
  	if(ffield.length){for(gv_i=0;gv_i<ffield.length;gv_i++)if(ffield[gv_i].checked) v=ffield[gv_i].value;}  
    else{if(ffield.checked) v=ffield.value;}	
  }
    
  if(t=="select-multiple")  v=ffield.options[ffield.selectedIndex].value;
  if(t=="select-one")  v=ffield.options[ffield.selectedIndex].value;
  if(t=="text") v=ffield.value;
  if(t=="textarea") v=ffield.value;
  
  return v;
}

// Function:	isSizeSmaller
// Parameters:	v		value (form value)
//				size	compare size
// Returns:		boolean
//
function isSizeSmaller(v, size)
{
	if (v.length < size) return true;
	else return false;
}

// Function:	isSizeGreater
// Parameters:	v		value (form value)
//				size	compare size
// Returns:		boolean
//
function isSizeGreater(theValue, size)
{
	if (theValue.length > size) return true;
	else return false;
}

// Function:	isEmailAddress
// Parameters:	v		value (form value)
// Returns:		boolean
// Description: checks if emailadres is a valid one
//
function isEmailAddress(v)
{
	mailvalid=true
	if(v.indexOf('@')<1)mailvalid=false;
	else if(v.lastIndexOf('.') < v.indexOf('@'))isvalid=false;
	aapje=v.indexOf('@');  puntje=v.indexOf('.');  if(puntje-aapje==1)isvalid=false;
	return mailvalid;
}


// question when clicking on join

function check() {
return confirm("Joining again, means you'll logout and make a new user, are you sure?");
}

// EDITING AND QUOTING POST AND TOPICS
function getQuote ( p_id, username, p_date )
{
	var q_tekst = document.getElementById("pst_" + p_id).innerHTML;
	var q_title = document.getElementById("ttl_" + p_id).innerHTML;
	
	if (isString(q_title))
	{
		q_title = "Re: " + stripHTML(q_title);
		document.forms['posting'].subject.value = q_title;
	}
	
	if (isString(q_tekst))
	{
		q_tekst = "<BLOCKQUOTE>" + username + ' ' + p_date + '\n\n' + q_tekst + '<\/BLOCKQUOTE>';
		q_tekst = wikify_blockquotes(q_tekst);
		q_tekst = stripHTML(q_tekst);
	
		q_tekst = q_tekst.replace(/^\n+/, '');
		q_tekst = q_tekst.replace(/\n>+ $/, '\n\n');
	
		document.forms['posting'].body.value = q_tekst + '\n\n';
	}
	document.forms['posting'].subject.focus();
}


function wikify_blockquotes ( s )
{
	// Find the first and the last blockquote
	var start = s.indexOf('<BLOCKQUOTE>');

	if (start == -1)
	{
		start = s.indexOf('<blockquote>');
	}

	if (start >= 0)
	{
		var end = s.lastIndexOf('<\/BLOCKQUOTE>');
		
		if (end == -1)
		{
			end = s.lastIndexOf('<\/blockquote>');
		}

		if (end > 0)
		{
			// Fetch the to-be-wikified block
			var r = s.substring(start+12, end);
			
			// recurse for nested block quotes
			r 	= wikify_blockquotes(r);
			r	= r.replace(/\n(>+\s)?/g, '\n>$1 ');
	
			s = s.substring(0,start) + '\n' + r + s.substring(end+13);
			s = s.replace(/\n+/g, '\n');
		}
	}
	return s;
}

function stripHTML ( s ) 
{
	return s.replace(/<\S[^>]*>/g, '')
}


function editPost(p_id, username, p_date, p_rating)
{
	var q_tekst  = document.getElementById("pst_" + p_id).innerHTML;
	var q_title  = document.getElementById("ttl_" + p_id).innerHTML;

	if (isString(q_tekst) || isString(q_title))
	{
		q_title = stripHTML(q_title);
		q_tekst = wikify_blockquotes(q_tekst);
		q_tekst = stripHTML(q_tekst);

		if (document.forms['posting'].p_id)	  document.forms['posting'].p_id.value = p_id;
		if (document.forms['posting'].mode)	  document.forms['posting'].mode.value = 'edit';
		if (document.forms['posting'].body)	  document.forms['posting'].body.value = q_tekst;
		if (document.forms['posting'].rating && isNumber(p_rating))	document.forms['posting'].elements['rating'][p_rating].checked = true;
		if (document.forms['posting'].subject)
		{	
			document.forms['posting'].subject.value = q_title;
			document.forms['posting'].subject.focus();
		}
	}
	else
	{
		alert('Titel en tekst zijn leeg (' + p_id + ')');
	}
}


function setPredef(form, value)
{
	var f = document.getElementById(form);
	if (!f)
	{
		f = document.forms[form];
	}
	if (f && f.predef && value != null)
	{
		f.predef.value = value;
	}
}


function validate_qrb (alrt)
{
	if (alrt == null || !isString(alrt))
	{
		alrt = 'You may not post an empty reaction';
	}
	if((isSizeSmaller(document.forms['posting'].subject.value,1) || isSizeSmaller(document.forms['posting'].body.value,1)) && document.forms['posting'].predef_post.value == 0)
	{
		alert(alrt);
		return false;
	}
}

function set_post_offensive(thg_id, usr_id)
{
	resetForm('escalate');
	hideObjAll('fhid','fhid_escalate',true);
	document.forms['f_escalate'].thing_id.value = thg_id;
	document.forms['f_escalate'].usr_id.value = usr_id;
}

function previewpost(alrt)
{
	if (alrt == null || !isString(alrt))
	{
		alrt = 'You may not post an empty reaction';
	}
	
	var titel = document.forms['posting'].subject.value;
	var tekst = document.forms['posting'].body.value;
	if (isSizeSmaller(titel,1) && isSizeSmaller(tekst,1))
	{
		alert(alrt);
	}
	else
	{
		leftVal = (screen.width) / 2 - 250;
		tekst	= tekst.replace(/\n/g, "<br>");
		tekst	= escape(tekst);
		window.open('forum/preview.php?titel='+titel+'&text='+tekst, 'preview', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=500,height=300,left = '+leftVal+',top = 200');
	}
}




// helper is... function
function isAlien(a) {
   return isObject(a) && typeof a.constructor != 'function';
}
function isArray(a) {
    return isObject(a) && a.constructor == Array;
}
function isBoolean(a) {
    return typeof a == 'boolean';
}
function isEmpty(o) {
    var i, v;
    if (isObject(o)) {
        for (i in o) {
            v = o[i];
            if (isUndefined(v) && isFunction(v)) {
                return false;
            }
        }
    }
    return true;
}
function isFunction(a) {
    return typeof a == 'function';
}
function isNull(a) {
    return typeof a == 'object' && !a;
}
function isNumber(a) {
    return typeof a == 'number' && isFinite(a);
}
function isObject(a) {
    return (a && typeof a == 'object') || isFunction(a);
}
function isString(a) {
    return typeof a == 'string';
}
function isUndefined(a) {
    return typeof a == 'undefined';
} 

function inArray(a, value)
// Returns true if the passed value is found in the
// array.  Returns false if it is not.
{
	var i;
	for (i=0; i < a.length; i++) {
		// Matches identical (===), not just similar (==).
		if (a[i] === value) {
			return true;
		}
	}
	return false;
};

if (!Array.prototype.push) {
	Array.prototype.push = function() {
		var i = 0, b = this.length, a = arguments;
		for (i; i<a.length; i++) this[b+i] = a[i];
		return this.length;
	}
}

if (!Array.prototype.pop) {
	Array.prototype.pop = function() {
		var  b = this[this.length-1];
		this.length--;
		return b;
	}
}

// Popup
//
function popup(thg_id,o,l,t,w,h) 
{ 
	if (l == null) l = 100;
	if (t == null) t = 100;
	if (w == null) w = 400;
	if (h == null) h = 400;

	var aPopupWin=window.open('','PopupWin','scrollbars=yes,menubar=no,location=no,directories=no,statusbar=no,toolbar=no,resizable=yes,width='+w+',height='+h+',top='+t+',left='+l);
	aPopupWin.document.open(); 
	aPopupWin.document.close();
	order = '';
	if (o != null) order = '&order='+o;
	var p = 'popup.php?id='+thg_id+order;

	aPopupWin.document.location.href = p;
	if(aPopupWin&&!aPopupWin.closed) {
		aPopupWin.focus();
	}	
}

///////////////////////// external source file //////////////////////////

// sywinopen (c) Copyright 2001 by Marc Worrell (marcw@pobox.com)
// 
// Permission granted to Mediamatic to use this source in
// commercial and non-commercial projects.
//

// Compatibility code for older sylvia-made html
function window0 ( url )
{
	winopen(url, "sywin", 725, 440, '');
}

function window1 ( url)
{
	winopen(url, "sywin1", 400, 600, '');
}

// sywinopen
//
//   url    The location to open in the new window
//   id     The id of the window, use the same id for reusing
//          window with different content; another id to create
//          a new window
//   width  The width of the created window
//   height The height of the created window
//
// The bottom of the new window will be aligned with the bottom
// of the current window. The window will be centered around the
// center of the current window.  After creation the newly
// created window will receive the focus.
//
function winopen ( url, id, width, height, options )
{
	var w;
	var top;
	var left;
	var parent_width;
	var parent_height;
	var parent_left;
	var parent_top;
	var screen_width;
	var screen_height;
	var userAgent = navigator.userAgent.toLowerCase();

	var scrollbars = '0';
	
	if (options != null && options != '')
	{
		var opts = new String(options);
	
		if (opts.indexOf('scrollbars') >= 0)
		{
			scrollbars = '1';
		}
	}

	// Fetch the screen size
	if (screen.availWidth)
	{
		screen_width  = screen.availWidth;
		screen_height = screen.availHeight;
	}
	else
	{
		screen_width  = screen.width;
		screen_height = screen.height;
	}
	
	// Fetch the window size
	if (document.layers) 
	{
		// Netscape 4.x
		parent_top    = window.screenY;
		parent_left   = window.screenX;
    	parent_width  = window.outerWidth;
    	parent_height = window.outerHeight;
	}
	else if (document.all) 
	{
		// IE
		parent_top    = window.screenTop;
		parent_left   = window.screenLeft;
    	parent_width  = document.body.clientWidth;
    	parent_height = document.body.clientHeight;
	}
	else if (document.getElementById)
	{
		// DOM compatible browser
		parent_top    = window.screenY;
		parent_left   = window.screenX;
    	parent_width  = window.outerWidth;
    	parent_height = window.outerHeight;
	}

	if (   parent_top    == null 
		|| parent_left   == null
	    || parent_width  == null
	    || parent_height == null)
	{
		// other browsers: center on screen
		parent_top    = 0;
		parent_left   = 0;
		parent_width  = screen_width;
		parent_height = screen_height;

		// Place the window in the center of the screen
		top  = parent_top  + Math.floor((parent_height - height) /2);
		left = parent_left + Math.floor((parent_width  - width) / 2);
	}	
	else
	{
		// Place the window in the middle of the lower side of the current window
		top  = parent_top  + parent_height - height - 8;  // 8 pixels margin -- needed for startbar on IE/Windows...
		left = parent_left + Math.floor((parent_width  - width) / 2);
	}
	
	// Be sure that the window is on the screen
	if (top + height > screen_height)
	{
		top = screen_height - height;
	}
	if (left + width > screen_width)
	{
		left = screen_width - width;
	}
	if (top < 0)
	{
		top = 0;
	}
	if (left < 0)
	{
		left = 0;
	}


	if (document.all || userAgent.indexOf('applewebkit') > 0 || userAgent.indexOf('khtml') > 0)
	{
		// open the window in IE or Safari -- the size is the outer size
		w = window.open(url, id, 
	    	          'scrollbars=' + scrollbars + ',menubar=0,locationbar=0,' 
	        	    + 'directories=0,statusbar=0,toolbar=0,resizable=0,'
	            	+ 'width=' + width +',height=' + height 
	            	+',top=' + top + ',left=' + left);
	}
	else
	{
		// open the window in Netscape -- we have to specify the outer size!
		w = window.open(url, id, 
	    	          'scrollbars=' + scrollbars + ',menubar=0,locationbar=0,' 
	        	    + 'directories=0,statusbar=0,toolbar=0,resizable=0,'
	            	+ 'outerWidth=' + width +',outerHeight=' + height 
	            	+ 'width=' + width +',height=' + height 
	            	+',screenY=' + top + ',screenX=' + left
	            	+',top=' + top + ',left=' + left);
	}

	// focus the window -- needed when the window was already open
	w.focus();
}


//