//--------------------------------------------
// Set up our simple tag open values
//--------------------------------------------

var B_open2 = 0;
var I_open2 = 0;
var U_open2 = 0;
var bbtags2   = new Array();
var fombj    = document.REPLIER;


//==========================================
// Set the help bar status
//==========================================

function hstat2(msg)
{
	fombj.helpbox2.value = eval( "help_" + msg );
}

//==========================================
// Set the number of tags open box
//==========================================

function cstat2()
{
	var c = stacksize2(bbtags2);
	
	if ( (c < 1) || (c == null) ) {
		c = 0;
	}
	
	if ( ! bbtags2[0] ) {
		c = 0;
	}
	
	fombj.tagcount2.value = c;
}


//==========================================
// Close all tags
//==========================================

function closeall2()
{
	if (bbtags2[0])
	{
		while (bbtags2[0])
		{
			tagRemove2 = popstack2(bbtags2)
			fombj.Post2.value += "[/" + tagRemove2 + "]";
			
			//--------------------------------------------
			// Change the button status
			// Ensure we're not looking for FONT, SIZE or COLOR as these
			// buttons don't exist, they are select lists instead.
			//--------------------------------------------
			
			if ( (tagRemove2 != 'FONT') && (tagRemove2 != 'SIZE') && (tagRemove2 != 'COLOR') )
			{
				eval("fombj." + tagRemove2 + ".value = ' " + tagRemove2 + " '");
				eval(tagRemove2 + "_open2 = 0");
			}
		}
	}
	
	//--------------------------------------------
	// Ensure we got them all
	//--------------------------------------------
	
	fombj.tagcount2.value = 0;
	bbtags2 = new Array();
	fombj.Post2.focus();
}


//==========================================
// ALTER FONT
//==========================================

function alterfont2(theval2, thetag2)
{
    if (theval2 == 0)
    	return;
	
	if(doInsert2("[" + thetag2 + "=" + theval2 + "]", "[/" + thetag2 + "]", true))
		pushstack2(bbtags2, thetag2);
	
    fombj.ffont2.selectedIndex  = 0;
    fombj.fsize2.selectedIndex  = 0;
    fombj.fcolor2.selectedIndex = 0;
    
    cstat2();	
}


//==========================================
// SIMPLE TAGS (such as B, I U, etc)
//==========================================

function simpletag2(thetag2)
{
	var tagOpen2 = eval(thetag2 + "_open2");

	if (tagOpen2 == 0)
	{
		if(doInsert2("[" + thetag2 + "]", "[/" + thetag2 + "]", true))
		{
			eval(thetag2 + "_open2 = 1");
			
			//--------------------------------------------
			// Change the button status
			//--------------------------------------------
			
			eval("fombj." + thetag2 + "2.value += '*'");
	
			pushstack2(bbtags2, thetag2);
			cstat2();
			hstat2('click_close');
		}
}
	else
	{
		//--------------------------------------------
		// Find the last occurance of the opened tag
		//--------------------------------------------
		lastindex2 = 0;
		
		for (i = 0 ; i < bbtags2.length; i++ )
		{
			if ( bbtags2[i] == thetag2 )
			{
				lastindex2 = i;
			}
		}
		
		//--------------------------------------------
		// Close all tags opened up to that tag was opened
		//--------------------------------------------
		
		while (bbtags2[lastindex2])
		{
			tagRemove2 = popstack2(bbtags2);
			doInsert2("[/" + tagRemove2 + "]", "", false)
			
			//--------------------------------------------
			// Change the button status
			//--------------------------------------------
			
			if ( (tagRemove2 != 'FONT') && (tagRemove2 != 'SIZE') && (tagRemove2 != 'COLOR') )
			{
				eval("fombj." + tagRemove2 + "2.value = ' " + tagRemove2 + " '");
				eval(tagRemove2 + "_open2 = 0");
			}
		}
		
		cstat2();
	}
}

//==========================================
// URL tag
//==========================================

function tag_url2()
{
    var FoundErrors = '';
    var enterURL   = prompt(text_enter_url, "http://");
    var enterTITLE = prompt(text_enter_url_name, "My Webpage");

    if (!enterURL) {
        FoundErrors += " " + error_no_url;
    }
    if (!enterTITLE) {
        FoundErrors += " " + error_no_title;
    }

    if (FoundErrors) {
        alert("Error!"+FoundErrors);
        return;
    }

	doInsert2("[URL="+enterURL+"]"+enterTITLE+"[/URL]", "", false);
}

//==========================================
// Image tag
//==========================================

function tag_image2()
{
    var FoundErrors = '';
    var enterURL   = prompt(text_enter_image, "http://");

    if (!enterURL) {
        FoundErrors += " " + error_no_url;
    }

    if (FoundErrors) {
        alert("Error!"+FoundErrors);
        return;
    }

	doInsert2("[IMG]"+enterURL+"[/IMG]", "", false);
}

function tag_email2()
{
    var emailAddress = prompt(text_enter_email, "");

    if (!emailAddress) { 
		alert(error_no_email); 
		return; 
	}

	doInsert2("[EMAIL]"+emailAddress+"[/EMAIL]", "", false);
}

//--------------------------------------------
// GENERAL INSERT FUNCTION
//--------------------------------------------
// ibTag: opening tag
// ibClsTag: closing tag, used if we have selected text
// isSingle: true if we do not close the tag right now
// return value: true if the tag needs to be closed later

//

function doInsert2(ibTag, ibClsTag, isSingle)
{
	var isClose = false;
	var obj_ta = fombj.Post2;
	
	//----------------------------------------
	// It's IE!
	//----------------------------------------
	if ( (ua_vers >= 4) && is_ie && is_win)
	{
		if (obj_ta.isTextEdit)
		{
			obj_ta.focus();
			var sel = document.selection;
			var rng = sel.createRange();
			rng.colapse;
			if((sel.type == "Text" || sel.type == "None") && rng != null)
			{
				if(ibClsTag != "" && rng.text.length > 0)
					ibTag += rng.text + ibClsTag;
				else if(isSingle)
					isClose = true;
	
				rng.text = ibTag;
			}
		}
		else
		{
			if(isSingle)
			{
				isClose = true;
			}
			
			obj_ta.value += ibTag;
		}
	}
	//----------------------------------------
	// It's MOZZY!
	//----------------------------------------
	
	else if ( obj_ta.selectionEnd )
	{ 
		var ss = obj_ta.selectionStart;
		var st = obj_ta.scrollTop;
		var es = obj_ta.selectionEnd;
		
		if (es <= 2)
		{
			es = obj_ta.textLength;
		}
		
		var start  = (obj_ta.value).substring(0, ss);
		var middle = (obj_ta.value).substring(ss, es);
		var end    = (obj_ta.value).substring(es, obj_ta.textLength);
		
		//-----------------------------------
		// text range?
		//-----------------------------------
		
		if (obj_ta.selectionEnd - obj_ta.selectionStart > 0)
		{
			middle = ibTag + middle + ibClsTag;
		}
		else
		{
			middle = ibTag + middle;
			
			if (isSingle)
			{
				isClose = true;
			}
		}
		
		obj_ta.value = start + middle + end;
		
		var cpos = ss + (middle.length);
		
		obj_ta.selectionStart = cpos;
		obj_ta.selectionEnd   = cpos;
		obj_ta.scrollTop      = st;


	}
	//----------------------------------------
	// It's CRAPPY!
	//----------------------------------------
	else
	{
		if (isSingle)
		{
			isClose = true;
		}
		
		obj_ta.value += ibTag;
	}
	
	obj_ta.focus();

	return isClose;
}	




var uagent    = navigator.userAgent.toLowerCase();
var is_safari = ( (uagent.indexOf('safari') != -1) || (navigator.vendor == "Apple Computer, Inc.") );
var is_ie     = ( (uagent.indexOf('msie') != -1) && (!is_opera) && (!is_safari) && (!is_webtv) );
var is_ie4    = ( (is_ie) && (uagent.indexOf("msie 4.") != -1) );
var is_moz    = (navigator.product == 'Gecko');
var is_ns     = ( (uagent.indexOf('compatible') == -1) && (uagent.indexOf('mozilla') != -1) && (!is_opera) && (!is_webtv) && (!is_safari) );
var is_ns4    = ( (is_ns) && (parseInt(navigator.appVersion) == 4) );
var is_opera  = (uagent.indexOf('opera') != -1);
var is_kon    = (uagent.indexOf('konqueror') != -1);
var is_webtv  = (uagent.indexOf('webtv') != -1);

var is_win    =  ( (uagent.indexOf("win") != -1) || (uagent.indexOf("16bit") !=- 1) );
var is_mac    = ( (uagent.indexOf("mac") != -1) || (navigator.vendor == "Apple Computer, Inc.") );
var ua_vers   = parseInt(navigator.appVersion);

//==========================================
// Get cookie
//==========================================

function my_getcookie( name )
{
	cname = rac_var_cookieid + name + '=';
	cpos  = document.cookie.indexOf( cname );
	
	if ( cpos != -1 )
	{
		cstart = cpos + cname.length;
		cend   = document.cookie.indexOf(";", cstart);
		
		if (cend == -1)
		{
			cend = document.cookie.length;
		}
		
		return unescape( document.cookie.substring(cstart, cend) );
	}
	
	return null;
}

//==========================================
// Set cookie
//==========================================

function my_setcookie( name, value, sticky )
{
	expire = "";
	domain = "";
	path   = "/";
	
	if ( sticky )
	{
		expire = "; expires=Wed, 1 Jan 2020 00:00:00 GMT";
	}
	
	if ( rac_var_cookie_domain != "" )
	{
		domain = '; domain=' + rac_var_cookie_domain;
	}
	
	if ( rac_var_cookie_path != "" )
	{
		path = rac_var_cookie_path;
	}
	
	document.cookie = rac_var_cookieid + name + "=" + value + "; path=" + path + expire + domain + ';';
}


//==========================================
// Array: Get stack size
//==========================================

function stacksize2(thearray2)
{
	for (i = 0 ; i < thearray2.length; i++ )
	{
		if ( (thearray2[i] == "") || (thearray2[i] == null) || (thearray2 == 'undefined') )
		{
			return i;
		}
	}
	
	return thearray2.length;
}

//==========================================
// Array: Push stack
//==========================================

function pushstack2(thearray2, newval)
{
	arraysize = stacksize2(thearray2);
	thearray2[arraysize] = newval;
}

//==========================================
// Array: Pop stack
//==========================================

function popstack2(thearray2)
{
	arraysize = stacksize2(thearray2);
	theval2 = thearray2[arraysize - 1];
	delete thearray2[arraysize - 1];
	return theval2;
}
