function GetKeyCode(e)
{ var keyCode;
   switch (emod) {
   case "IE4+":
      e = window.event;
      alt = (e.altKey) ? true : false;
      ctrl = (e.ctrlKey) ? true : false;
      shift = (e.shiftKey) ? true : false;
      if ((e.keyCode<16)||(e.keyCode>18)) {
         keyCode = e.keyCode;
         }
   break;
   case "NN4":
      alt = ((e.modifiers & Event.ALT_MASK) == Event.ALT_MASK);
      ctrl = ((e.modifiers & Event.CTRL_MASK) == Event.CTRL_MASK);
      shift = ((e.modifiers & Event.SHIFT_MASK) == Event.SHIFT_MASK);
      if ((e.which)&&(e.which!=0))
      {
         keyCode = e.which;
      }
   break;
   case "W3C":
      alt = (e.altKey) ? true : false;
      ctrl = (e.ctrlKey) ? true : false;
      shift = (e.shiftKey) ? true : false;
      if ((e.which<16)||(e.which>18))
      {
         keyCode = e.which;
      }
   break; }
   return keyCode;
}

function onKeyUpH(e)
{ 
   var keyCode = GetKeyCode(e);
   if  (ctrl && keyCode == 13) { document.post.post.click(); }
   return false;
}

function onloadH(e)
{
   /*get the event model*/
   emod = (e) ? (e.eventPhase) ? "W3C" : "NN4" : (window.event) ? "IE4+" : "unknown";
   if (emod == "NN4") {
      document.captureEvents(Event.KEYUP); }
   document.onkeyup = onKeyUpH;
   return true;
}

function insert_name(name) {
	check_visibility('qr_open');
	emoticon('[b]' + name + '[/b]');
}

function check_visibility(id) {
	var item = null;

	if (document.getElementById) {
		item = document.getElementById(id);
	} else if (document.all) {
		item = document.all[id];
	}
	if (item && item.style) {
		if (item.style.display == "none") {
			item.style.display = "";
		} else if (item) {
		item.visibility = "show";
		}
	}
}

function insert_bbtag(control, bbtag) {
	if (control.value.substr(0,1) == '*') {
	 bbtag = '/' + bbtag;
	 control.value = control.value.substr(1,control.value.length);
	} else {
	 control.value = '*' + control.value;
	}
	emoticon('[' + bbtag + ']');
}

function emoticon(text) {
	var NotInIEmode = false;
	var url = window.location.href;
	var basedoc_name = (url.substr(url.length-12, url.length) == 'mode=smilies') ? "opener.document" : "document";
	var basedoc = eval(basedoc_name);

	    var obj = basedoc.post.input;
	    if (typeof(obj) != "object") { obj = basedoc.post.message; }

	if(basedoc.selection) {

 		obj.focus();
		var range = basedoc.selection.createRange();
		if(range.parentElement() != obj) {
			return false;
		}
		if(range.parentElement) {
			NotInIEmode = true;
		}
			range.text = text;
			range.select();
    	} else if(obj.selectionStart || obj.selectionStart === 0 || NotInIEmode) { 
        	// Go the Gecko way 
		/* Find the Start and End Position */ 
			var start = obj.selectionStart; 
			var end   = obj.selectionEnd; 

		/* Remember obj is a textarea or input field */ 
			obj.value = obj.value.substr(0, start) 
			+ text 
			+ obj.value.substr(end, obj.value.length);
	 		obj.focus();
 			setCaretTo(obj, start + text.length);
    	} else { 
        // Fallback for any other browser
	obj.value += text;
	} 
}

function QuoteSelectedAtCursor(name) {
	check_visibility('qr_open');
	var NotInIEmode = false;
	var obj = document.post.input;
	var quote1 = name != '' ? '[quote="' + name + '"]' : '[quote]';
	var quote2 = '[/quote]';

	if(document.selection) {
		/* Get selected text */
		var text = quote1 + document.selection.createRange().text + quote2;
 		obj.focus();
		var range = document.selection.createRange();
		if(range.parentElement() != obj) {
			return false;
		}
		if(range.parentElement) {
			NotInIEmode = true;
		}
			range.text = text;
			range.select();
    	} else if ((obj.selectionStart === 0) || obj.selectionStart || NotInIEmode) { 
        	// Go the Gecko way 
		/* Get selected text */
		var text = window.getSelection().toString();
		text = quote1 + text + quote2;
		/* Find the Start and End Position */ 
			var start = obj.selectionStart; 
			var end   = obj.selectionEnd; 
		/* Remember obj is a textarea or input field */ 
			obj.value = obj.value.substr(0, start) 
			+ text 
			+ obj.value.substr(end, obj.value.length);
	 		obj.focus();
			setCaretTo(obj, start + text.length);
	} else { 
		// Fallback for any other browser
	 	obj.value += quote1 + quote2;
	}
}

function  setCaretTo(obj, pos){ 
    if(obj.createTextRange) { 
        /* Create a TextRange, set the internal pointer to
           a specified position and show the cursor at this
           position
        */ 
        var range = obj.createTextRange(); 
        range.move("character", pos); 
        range.select(); 
    } else if(obj.selectionStart) { 
        /* Gecko is a little bit shorter on that. Simply
           focus the element and set the selection to a
           specified position
        */ 
        obj.focus(); 
        obj.setSelectionRange(pos, pos); 
    } 
}