/* 	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 PopMoreTags(form,name) {
         link="moretags.php?form="+form+"&text="+name
         newWin=window.open(link,"moresmile","height=500,width=775,resizable=no,scrollbars=yes");
         if (window.focus) {newWin.focus()}
}




/* 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;
}

	