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

// Helpline messages
url_help = "Insert URL: [url]http://url[/url] or [url=http://url]URL text[/url] (alt+u)";
image_help = "Insert image: [img]http://image_url[/img] (alt+m)";
email_help = "Insert e-mail: [email]e-mail[/email] (alt+e)";
bold_help = "Bold text: [b]text[/b] (alt+b)";
italic_help = "Italic text: [i]text[/i] (alt+i)";
color_help = "Font color: [color=red]text[/color] Tip: you can also use color=#FF0000 (alt+c)";

// q_help = "Quote text: [quote]text[/quote]  (alt+q)";
// c_help = "Code display: [code]code[/code]  (alt+c)";
// l_help = "List: [list]text[/list] (alt+l)";
// o_help = "Ordered list: [list=]text[/list]  (alt+o)";
// a_help = "Close all open bbCode tags";
// f_help = "Font size: [size=x-small]small text[/size]";
// u_help = "Underline text: [u]text[/u]  (alt+u)";

// Set the help bar status
function helpline(help) {
  document.myform.helpbox.value = eval(help + "_help");
}

function mozWrap(textArea, open, close)
{
	var selLength = textArea.textLength;
	var selStart = textArea.selectionStart;
	var selEnd = textArea.selectionEnd;
	if (selEnd == 1 || selEnd == 2) 
		selEnd = selLength;

	var s1 = (textArea.value).substring(0, selStart);
	var s2 = (textArea.value).substring(selStart, selEnd)
	var s3 = (textArea.value).substring(selEnd, selLength);
	textArea.value = s1 + open + s2 + close + s3;
	return;
}

function checkSelection(textArea, openTag, closeTag) {
  var theSelection = '';
  if ((clientVer >= 4) && is_ie && is_win) {
		theSelection = document.selection.createRange().text; // Get text selection
	}
  
  if (theSelection) {
    document.selection.createRange().text = openTag + theSelection + closeTag;
    textArea.focus();
    theSelection = '';
    return true;
  }
  else if (textArea.selectionEnd && (textArea.selectionEnd - textArea.selectionStart > 0)) {
    mozWrap(textArea, openTag, closeTag);
    return true;
  }
  
  return false;
}

// Add tag  
function addColorTag(action, fieldName) {
  var newText;
	var textArea = document.myform.elements[fieldName];
  var currentText = textArea.value;

  if (action == "color") {
    var myColor = "red";
    if (myColor != "" && myColor != null) {
      if (checkSelection(textArea, "[color=" + myColor + "]", "[/color]")) {
        textArea.focus();
        return;
      }
      else {      
        var myText = prompt("Enter the text you want to color.", "");
        
        if (myText != "" && myText != null) {
          var BBCode = "[color=" + myColor + "]" + myText + "[/color]";
          newText = currentText + BBCode;
          textArea.value = newText;
        }
      }
    }
    textArea.focus();
    return;
  }
}

// Add tag  
function addTag(action, fieldName) {
  var newText;
	var textArea = document.myform.elements[fieldName];
  var currentText = textArea.value;
  
  if (action == "url") {
    var myUrl = prompt("Enter the URL for the link you want to add.", "http://");
    if (myUrl != "" && myUrl != null) {
      var myTitle = prompt("Enter the web site title", "Page Title");
      
      if (myTitle != null) {
        if (myTitle == "") {
          myTitle = myUrl;
        }
      
        var BBCode = "[url=" + myUrl + "]" + myTitle + "[/url]";
        newText = currentText + BBCode;
        textArea.value = newText;
      }
    }
    textArea.focus();
    return;
  }
  
  if (action == "image") {
    if (checkSelection(textArea, "[img]", "[/img]")) {
      return;
    }
    
    var myImage = prompt("Enter the URL for the image you want to display.","http://");
    if (myImage != "" && myImage != null) {
      var BBCode = "[img]"+myImage+"[/img]";
      newText =currentText + BBCode;
      textArea.value = newText;
    }
    textArea.focus();
    return;
  }
  
  if (action == "email") {
    if (checkSelection(textArea, "[email]", "[/email]")) {
      return;
    }
    
    var myEmail = prompt("Enter the e-mail address you want to add.", "");
    if (myEmail != "" && myEmail != null) {
      var BBCode = "[email]" + myEmail + "[/email]";
      newText = currentText + BBCode;
      textArea.value = newText;
    }
    textArea.focus();
    return;
  }
  
  if (action == "bold") {
    if (checkSelection(textArea, "[b]", "[/b]")) {
      return;
    }
        
    var myBold = prompt("Enter the text that you want to make bold.", "");
    if (myBold != "" && myBold != null) {
      var BBCode = "[b]" + myBold + "[/b]";
      newText = currentText + BBCode;
      textArea.value = newText;
    }
    textArea.focus();
    return;
  }
  
  if (action == "italic") {
    if (checkSelection(textArea, "[i]", "[/i]")) {
      return;
    }
    
    var myItalic = prompt("Enter the text that you want to make italic.", "");
    if (myItalic != "" && myItalic != null) {
      var BBCode = "[i]" + myItalic + "[/i]";
      newText = currentText + BBCode;
      textArea.value = newText;
    }
    textArea.focus();
    return;
  }
  
  if (action == "color") {
    var myColor = prompt("Enter the color you want to use.", "");
    if (myColor != "" && myColor != null) {
      if (checkSelection(textArea, "[color=" + myColor + "]", "[/color]")) {
        textArea.focus();
        return;
      }
      else {      
        var myText = prompt("Enter the text you want to color.", "");
        
        if (myText != "" && myText != null) {
          var BBCode = "[color=" + myColor + "]" + myText + "[/color]";
          newText = currentText + BBCode;
          textArea.value = newText;
        }
      }
    }
    textArea.focus();
    return;
  }
/*  if (action == "underline") {
    var myUnderl = prompt("Enter the text that you want to be underlined.", "");
    var BBCode = "[U]" + myUnderl + "[/U]";
    newText = currentText + BBCode;
    textArea.value = newText;
    textArea.focus();
    return;
  }
  */
/*if (action == "quote") {
   var quoteBBCode = "[QUOTE]  [/QUOTE]";    
   newText = currentText+quoteBBCode;   
textArea.value=newText;   
textArea.focus();    
return;  
}  
if (action == "code") { 
  var codeBBCode = "[CODE]  [/CODE]";    
  newText = currentText+codeBBCode;   
textArea.value=newText;   
textArea.focus();    
return;  
}  
if (action =="listopen") {   
 var liststartBBCode = "[LIST]";    
 newText =currentText+liststartBBCode;   
textArea.value=newText;   
textArea.focus();    
return;  
}  
if (action =="listclose") {    
var listendBBCode = "[/LIST]";    
newText = currentText+listendBBCode;   
textArea.value=newText;   
textArea.focus();    
return;  
}  
if (action =="listitem") {    
var myItem = prompt("Enter the new list item. Note that each list group must be preceeded by a List Close and must be ended with List Close.", "");    
var itemBBCode = "[*]"+myItem;    
newText =currentText+itemBBCode;   
textArea.value=newText;   
textArea.focus();    
return;  
}*/
}

function popUp(lnk, w, h, scroll, res) {
  lm  = (screen.width - w) / 2;
  tm  = (screen.height - h) / 2;
  vars  = 'scrollbars='+scroll+', width='+w+', height='+h+', top='+tm+', left='+lm+', resizable='+res;

  var win = window.open(lnk, '_blank', vars);
  win.focus();
  return false;
}

