/* 	Part of textbbcode function, global.php. Used for the 'compose message' frame on UG, it inserts
	the BBCode tags after clicking one of the buttons. */
function replaceSelection(container, replacement, splitChar) {
	var scrollPos = container.scrollTop;
	var start = container.selectionStart;

	if (!document.selection) {
		var end = container.selectionEnd;
		var before = container.value.substr(0, start);
		var after = container.value.substr(end);
		var text = container.value.substr(start, end - start);
		if (splitChar) {
			for (var entryList = text.split(splitChar), text = "", i = 0; typeof entryList[i] === "string"; i++) {
				text += replacement.replace("{{text}}", entryList[i]);
			}
		} else {
			text = replacement.replace("{{text}}", text);
		}

		container.value = before + text + after;
		container.selectionStart = container.selectionEnd = start + text.length;
	} else {
		if (!container.focus()) {
			container.focus();
		}
		var text = document.selection.createRange().text;

		if (splitChar) {
			for (var entryList = text.split(splitChar), text = "", i = 0; typeof entryList[i] === "string"; i++) {
				text += replacement.replace("{{text}}", entryList[i]);
			}
		} else {
			text = replacement.replace("{{text}}", text);
		}
		document.selection.createRange().text = text;
	}
	container.scrollTop = scrollPos;
	container.focus();
}

/* 	Part of textbbcode function, global.php. Used for the 'compose message' frame on UG, it inserts
	the BBCode tags after clicking one of the buttons. */
function BBTag(tagName, textboxId) {
	if (document.getElementById) {
		var textbox = document.getElementById(textboxId);
		if (tagName == "li") {
			var bbCode = "[*]{{text}}\n";
			replaceSelection(textbox, bbCode, "\n");
		} else {
			var bbCode = "[" + tagName + "]{{text}}[/" + tagName + "]";
			replaceSelection(textbox, bbCode);
		}
	}
}

/* 	Part of textbbcode function, global.php. Responsible for popping up and inserting smilies */
function SmileIT(smile, textboxId) {
	var textbox = document.getElementById(textboxId);
	replaceSelection(textbox, smile);
}
function PopMoreSmiles(form,name) {
		 link="moresmiles.php?form="+form+"&text="+name
		 newWin=window.open(link,"moresmile","height=500,width=450,resizable=no,scrollbars=yes");
		 if (window.focus) {newWin.focus()}
}

function widen() {
	if (document.getElementById("previewbottom"))
	{
		var pvbtmwidth = document.getElementById("previewbottom").offsetWidth;
		pvbtmwidth = pvbtmwidth.toString();
	}

	document.getElementById("expand_control").innerHTML="Expanded";
	document.forms["preview"]["body"].rows = 30;
	document.getElementById("mainframe").style.width="100%";
	document.getElementById("mainframe").style.margin="0";
	document.getElementById("messagepreview").style.width="100%";
	document.getElementById("smileytable").style.width="1px";

	if (document.getElementById("previewbottom")){
	document.getElementById("previewbottom").style.width=pvbtmwidth+"px";
	document.getElementById("previewbottom").style.marginLeft="25%";
	}
}

/* Similar to togglediv, with improvements */
function togglethis(obj) {
	vis=obj.nextSibling.nextSibling.style;
	vis.display = (vis.display == "block" ? "none" : "block");
	if (vis.display == "block")
	{
		obj.firstChild.src=obj.firstChild.src.replace("plus","minus");
		obj.innerHTML=obj.innerHTML.replace("to expand","to contract");
	}
	else
	{
		obj.firstChild.src=obj.firstChild.src.replace("minus","plus");
		obj.innerHTML=obj.innerHTML.replace("to contract","to expand");
	}
}

/* set class of an element with specified id */
function setclass(id,cl)
{
    itm=document.getElementById(id);

    if(!itm)
        return false;

    /* className is for IE7 */
    if(itm.getAttribute('class') == cl || itm.getAttribute('className') == cl)
        {
            itm.setAttribute('class', '')
            itm.setAttribute('className', '')
            return 0;
        }
    else
        {
            itm.setAttribute('class', cl)
            itm.setAttribute('className', cl)
            return 1;
        }
    return false;
}

/* Easter egg (shh!) */
function load_egg()
{
  ph = document.getElementById("content");
  // Insert div inside content div
  
  ph.innerHTML='<div class="konami"><IFRAME name="iframeName" width="512" height="480" scrolling="no" frameborder="0" marginheight="0" 	marginwidth="0" src="/vnes"> </IFRAME><br><a href="#" onClick="javascript:unload_egg();return false;"> (close) </a></div>'+ph.innerHTML;
  return false;
}

function unload_egg()
{
  itm=document.getElementById("content");
  //itm.firstChild.style.display="none";
  //itm.style.display="none";
  itm.firstChild.style.display="none";
  itm.firstChild.innerHTML = "";
  

  //return false;
}


/* AJAX-style scripts. Used for showing and hiding frames. This is what makes the collapse tag work */
function togglediv(id) {
	if (document.getElementById) {
		aref = document.getElementById("a"+id);
		if (aref.innerHTML == "Click here to expand")
			aref.innerHTML = "Click here to contract";
		else if (aref.innerHTML == "Click here to contract")
			aref.innerHTML = "Click here to expand";
		img = document.getElementById("i"+id);
		img.src = (img.src.match("pic/plus.gif") == null ? "pic/plus.gif" : "pic/minus.gif");
		vis = document.getElementById("d"+id).style;
		vis.display = (vis.display == "block" ? "none" : "block");
	}
}

/* Used by the other AJAX functions here (getAJAX, setAJAX, etc) */
function setContent(ph, content)
{
	switch(ph.tagName)
	{
		case 'TEXTAREA': case 'INPUT':
			ph.value = content;
			break;
		case 'DIV': case 'SPAN': default:
			ph.innerHTML = content;
			break;
	}
}

function getAJAX(placeholder, url) {
	//Make sure the browser is DOM compliant
	if (!document.getElementById) {
		//Allow the hyperlink to process normally
		return true;
	}
	//Get the placeholder
	ph = document.getElementById(placeholder);
	if (ph.innerHTML != "")	{
		//The placaeholder has content; just toggle its display
		ph.style.display = (ph.style.display == "none" ? "block" : "none");
	}
	else {
		//The div is empty; use AJAX to populate it with the contents from url
		//Initialize AJAX
		var xmlHttp;
		// Firefox, Opera 8.0+, Safari
		if (window.XMLHttpRequest) {
			xmlHttp = new XMLHttpRequest();
		}
		// Internet Explorer
		else if (window.ActiveXObject) {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}

		if (xmlHttp == null) {
			setContent(ph, "Your browser does not support AJAX.");
			//Allow the hyperlink to process normally
			return true;
		}

		//Remove any pre-existing data
		setContent(ph, 'Loading...<br><img alt="Loading" src="/ajax/loading.gif">');

		xmlHttp.onreadystatechange = function() {
			if(xmlHttp.readyState==4) {
				setContent(ph, xmlHttp.responseText);
			}
		}
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
	//Prevent the anchor href from being called
	return false;
}

function refreshAJAX(placeholder, url) {
	//Make sure the browser is DOM compliant
	if (!document.getElementById) {
		//Allow the hyperlink to process normally
		return true;
	}
	//Get the placeholder div
	ph = document.getElementById(placeholder);

	var xmlHttp;
	// Firefox, Opera 8.0+, Safari
	if (window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
	}
	// Internet Explorer
	else if (window.ActiveXObject) {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}

	if (xmlHttp == null) {
		setContent(ph, "Your browser does not support AJAX.");
		//Allow the hyperlink to process normally
		return true;
	}

	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState==4) {
			setContent(ph, xmlHttp.responseText);
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);

	//Prevent the anchor href from being called
	return false;
}

function postAJAX(placeholder, url, form)
{
		//Make sure the browser is DOM compliant
	if (!document.getElementById) {
		//Allow the hyperlink to process normally
		return true;
	}
	//Get the placeholder div
	ph = document.getElementById(placeholder);

	//Initialize AJAX
	var xmlHttp;
	// Firefox, Opera 8.0+, Safari
	if (window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
	}
	// Internet Explorer
	else if (window.ActiveXObject) {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}

	if (xmlHttp == null) {
		setContent(ph, "Your browser does not support AJAX.");
		//Allow the hyperlink to process normally
		return true;
	}

	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState==4) {
			setContent(ph, xmlHttp.responseText);
		}
	}

	//Generate the parameters from the form
	var params = '';
	for (i = 0; i < form.elements.length;i++) {
		var ctl = form.elements[i];
		if (ctl.name != '')
			switch (ctl.type) {
				case 'radio':
					if (ctl.checked)
						params += (params == '' ? '' : '&') + ctl.name + '=' + escape(ctl.value);
				break;
				default:
					params += (params == '' ? '' : '&') + ctl.name + '=' + escape(ctl.value);
				break;
			}			
	}
	params += "\n";

	xmlHttp.open("POST",url,true);

	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");

	xmlHttp.send(params);
	//Prevent the form from being submitted
	return false;
}

// Toggles class="rollup" for the given id,
// and sets or removes a cookie.
// Ex: [sidebar_community] => 'rollup'
function rollup(id) {
    if (setclass(id, 'rollup')==1)
    {
        setCookie(id, 'rollup');
    }
    else
    {
        delCookie(id);
    }
}

//used in more than sidebar; going to change all
//instances to rollup() and remove this
function sidebar_rollup(id) {
    if (setclass(id, 'rollup')==1)
    {
        setCookie(id, 'rollup');
    }
    else
    {
        delCookie(id);
    }
}

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}

/* commented out because it's not used yet */
/*
function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}
*/

function delCookie(c_name) {
document.cookie = c_name +
'=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
}

