// JavaScript Document

// 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);

//alert("Client: "+clientPC);
//alert("is_ie: "+is_ie+", is_nav: "+is_nav+", is_moz:"+is_moz+", is_win: "+is_win+", is_mac: "+is_mac);
//alert(document.selection);

function add_tags(beginTag, closeTag, txtarea){
	
	if ((clientVer >= 4) && is_ie && is_win){
			
			theSelection = document.selection.createRange().text; // Get text selection
			if (theSelection) {
				// Add tags around selection
				document.selection.createRange().text = beginTag + theSelection + closeTag;
				txtarea.focus();
				theSelection = '';
				return;
			}else{
				//no text has been selected, so just add the tag at the end of the textarea
				txtarea.innerHTML = txtarea.innerHTML + beginTag + closeTag;	
			}
			
	}else if (txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart > 0))	{
		
		mozWrap(txtarea, beginTag, closeTag);
		return;
		
	}else{
		//no text has been selected, so just add the tag at the end of the textarea
		txtarea.innerHTML = txtarea.innerHTML + beginTag + closeTag;
				
	}
	
}//end funtion
	
	
	// From http://www.massless.org/mozedit/
function mozWrap(txtarea, openTag, closeTag){
	
	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 + openTag + s2 + closeTag + s3;
	return;
	
}//end funtion


//Displays the hint for a given BB tag
function show_bb_tag_hint(tag){
	var hint = "";
	
	switch(tag){
		case "[b]":
			hint = "Bold text: [b]text[/b]";		
			break;
			
		case "[i]":
			hint = "Italic text: [i]text[/i]";		
			break;
			
		case "[u]":
			hint = "Underline text: [u]text[/u]";		
			break;
			
		case "[left]":
			hint = "Left-align text: [left]text[/left]";		
			break;
			
		case "[center]":
			hint = "Center-align text: [center]text[/center]";		
			break;
			
		case "[right]":
			hint = "Right-align text: [right]text[/right]";		
			break;
		
		case "[quote]":
			hint = "Quote text: [quote]text[/quote]";		
			break;
			
		case "[url]":
			hint = "Insert URL: [url]http://url[/url]";		
			break;
		
	}
	
	hintDiv = document.getElementById('bb_hintDiv');
	hintDiv.innerHTML = hint;
	
}//end function


function clear_bb_tag_hint(){
	hintDiv = document.getElementById('bb_hintDiv');
	hintDiv.innerHTML = "&nbsp;";

}//end function





var xmlHttp_loadPostPreview;
//loads the preview of a post
function loadPostPreview(subject, text){
	if(subject=="" || text==""){
		return;	
	}
	//alert(subject + " : " + text);
	xmlHttp_loadPostPreview=GetXmlHttpObject();
	if (xmlHttp_loadPostPreview==null){
		alert ("Browser does not support HTTP Request");
		return;
	} 
	//replace any return carriages
	//text = text.replace("\n","<br />");
	
	var url="includes/xmlHttp.php?action=get_posts_preview&posts_text="+encodeURIComponent(text)+"&posts_subject="+encodeURIComponent(subject);
	url=url+"&sid="+Math.random();
	xmlHttp_loadPostPreview.onreadystatechange=stateChanged_loadPostPreview;
	xmlHttp_loadPostPreview.open("GET",url,true);
	xmlHttp_loadPostPreview.send(null);	
}
	
function stateChanged_loadPostPreview(){ 
	var pDiv= document.getElementById('divPostsPreview');
	pDiv.innerHTML = "Loading....";
	
	if (xmlHttp_loadPostPreview.readyState==4 || xmlHttp_loadPostPreview.readyState=="complete"){ 
		pDiv.innerHTML = xmlHttp_loadPostPreview.responseText;
		//modelField.options[0] = new Option("Loading...", "");
	} 
}



function GetXmlHttpObject()
{ 
var objXMLHttp=null
if (window.XMLHttpRequest)
{
objXMLHttp=new XMLHttpRequest()
}
else if (window.ActiveXObject){
	objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
}
return objXMLHttp
} 

