// Check for Browser & Platform for PC & IE specific bits
// More details from: http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html
var clientPC = navigator.userAgent.toLowerCase(); // Get client info
var clientVer = parseInt(navigator.appVersion); // Get browser version

var is_ie = ((clientPC.indexOf("msie") != -1) && (clientPC.indexOf("opera") == -1));
var is_nav = ((clientPC.indexOf('mozilla')!=-1) && (clientPC.indexOf('spoofer')==-1)
                && (clientPC.indexOf('compatible') == -1) && (clientPC.indexOf('opera')==-1)
                && (clientPC.indexOf('webtv')==-1) && (clientPC.indexOf('hotjava')==-1));
var is_moz = 0;

var is_win = ((clientPC.indexOf("win")!=-1) || (clientPC.indexOf("16bit") != -1));
var is_mac = (clientPC.indexOf("mac")!=-1);

// JavaScript Document
function addStyle(box){
	var style;
	var current;
	var x = document.myform.addstyle;
	style = x.options[x.selectedIndex].text;
	//alert(style);
	current = document.getElementById(box).value;
	opentag = "[" + style + "]";
	closetag = "[/" + style + "]";
	mozWrap(document.getElementById(box),opentag,closetag);
	//document.getElementById(box).value = current + "[" + style + "] [/" + style + "]";
	document.getElementById(box).select.focus;
}

// From http://www.massless.org/mozedit/
function mozWrap(txtarea, open, close)
{
	if (is_ie){
		IEWrap(open,close,txtarea);
	}
	else
	{
	var selLength = txtarea.textLength;
	var selStart = txtarea.selectionStart;
	var selEnd = txtarea.selectionEnd;
	if (selEnd == 1 || selEnd == 2)
		selEnd = selLength;

	var s1 = (txtarea.value).substring(0,selStart);
	var s2 = (txtarea.value).substring(selStart, selEnd)
	var s3 = (txtarea.value).substring(selEnd, selLength);
	txtarea.value = s1 + open + s2 + close + s3;
	//return;
	}
}
function IEWrap(lft, rgt, txtarea) {
	strSelection = document.selection.createRange().text;
	if (strSelection!="") {
		document.selection.createRange().text = lft + strSelection + rgt;
	}
	else
	{
		txtarea.focus();
		sel = document.selection.createRange();
		sel.text = lft + rgt;
		//txtarea.value = txtarea.value + lft + rgt;
	}
}
	
function wrapSelection(txtarea, lft, rgt) {
	if (document.all) {IEWrap(lft, rgt);}
	else if (document.getElementById) {mozWrap(txtarea, lft, rgt);}
}

function addALink(box){
	var mylink;
	var current;
	var x = document.myform.addLink;
	mylink = x.options[x.selectedIndex].value;
	//alert(style);
	current = document.getElementById(box).value;
	//document.getElementById(box).value = current + " <<" + mylink + ">>";
	thelink = "<<" + mylink + ">>";
	mozWrap(document.getElementById(box),"",thelink);
	document.getElementById(box).select.focus;
}

function addALink2(box){
	var current;
	var url = document.myform.myURL.value;
	var linktext = document.myform.myLinkText.value;
	if (linktext != ""){
		linktext =  "|"+linktext;
	}
	current = document.getElementById(box).value;
	//document.getElementById(box).value = current + " <<" + mylink + ">>";
	var thelink = "<<" + url + linktext + ">>";
	mozWrap(document.getElementById(box),"",thelink);
	document.myform.myURL.value = "";
	document.myform.myLinkText.value = "";
	document.getElementById(box).select.focus;
}

function addImg(box){
	var img;
	var current;
	var x = document.myform.addimage;
	img = x.options[x.selectedIndex].value;
	//alert(img);
	current = document.getElementById(box).value;
	//document.getElementById(box).value = current + "{img:" + img + "}";
	theimage = "{img:" + img + "}";
	mozWrap(document.getElementById(box),"",theimage);
	document.getElementById(box).select.focus;
}

function addFile(box){
	var file;
	var current;
	var x = document.myform.addfile;
	file = x.options[x.selectedIndex].value;
	//alert(img);
	current = document.getElementById(box).value;
	//document.getElementById(box).value = current + "{file:" + file + "}";
	thefile = "{file:" + file + "}";
	mozWrap(document.getElementById(box),"",thefile);
	document.getElementById(box).select.focus;
}

function resample(box){
	var mystyle;
	var x = document.myform.style;
	mystyle = x.options[x.selectedIndex].text;
	document.getElementById(box).className = mystyle;
}

function resample2(box){
	var mystyle;
	var x = document.myform.addstyle;
	mystyle = x.options[x.selectedIndex].text;
	document.getElementById(box).className = mystyle;
}

function setColour(){
	var mycolour;
	var x = document.myform.colour;
	mycolour = x.value;
	if (mycolour == ""){
		mycolour = "#000000";
	}
	document.getElementById("mycol").style.backgroundColor = mycolour;
}

function setBgColour(){
	var mycolour;
	var x = document.myform.bgcolour;
	mycolour = x.value;
	if (mycolour == ""){
		mycolour = "#000000";
	}
	document.getElementById("mybgcol").style.backgroundColor = mycolour;
}

function setFont(){
	var myfont;
	var x = document.myform.font;
	myfont = x.options[x.selectedIndex].value;
	if (myfont == "None"){
		myfont = "Arial";
	}
	document.getElementById("myfont").style.fontFamily = myfont;
}

function setSize(){
	var mysize;
	var x = document.myform.size;
	mysize = x.options[x.selectedIndex].value;
	if (mysize == "None"){
		mysize = "12";
	}
	document.getElementById("mysize").style.fontSize = mysize + "px";
}

function preview(){
	var image;
	var current;
	var x = document.myform.addimage;
	image = x.options[x.selectedIndex].value;
	window.open('../includes/preview.php?id='+image,'_blank');
}

function previewbg(){
	var image;
	var current;
	var x = document.myform.bgimage;
	image = x.options[x.selectedIndex].value;
	window.open('../includes/preview.php?id='+image,'_blank');
}

function previewfile(){
	var file;
	var current;
	var x = document.myform.addfile;
	file = x.options[x.selectedIndex].value;
	window.open('../includes/previewfile.php?id='+file,'_blank');
}

// RBC Specific Functions
function showSection(id){
	hideAll();
	document.getElementById(id).style.display = "block";
}
function hideSection(id){
	document.getElementById(id).style.display = "none";
}
function hideAll(){
	hideSection("norm");
	hideSection("rtc");
	hideSection("messy");
	hideSection("play");
	hideSection("eco");
	hideSection("alpha");
}
