
function storeCaret(obj)
{if(typeof(obj.createTextRange)!="undefined")
{obj.caretPos=document.selection.createRange().duplicate();}}
function replaceText(text,obj)
{if(typeof(obj.caretPos)!="undefined"&&obj.createTextRange)
{var caretPos=obj.caretPos;caretPos.text=caretPos.text.charAt(caretPos.text.length-1)==' '?text+' ':text;caretPos.select();}
else if(typeof(obj.selectionStart)!="undefined")
{var begin=obj.value.substr(0,obj.selectionStart);var end=obj.value.substr(obj.selectionEnd);var scrollPos=obj.scrollTop;obj.value=begin+text+end;if(obj.setSelectionRange)
{obj.focus();obj.setSelectionRange(begin.length+text.length,begin.length+text.length);}
obj.scrollTop=scrollPos;}
else
{obj.value+=text;obj.focus(obj.value.length-1);}}
function surroundText(text1,text2,obj)
{if(typeof(obj.caretPos)!="undefined"&&obj.createTextRange)
{var caretPos=obj.caretPos,temp_length=caretPos.text.length;caretPos.text=caretPos.text.charAt(caretPos.text.length-1)==' '?text1+caretPos.text+text2+' ':text1+caretPos.text+text2;caretPos.moveStart("character",0);caretPos.moveEnd("character",0);caretPos.select();obj.focus(caretPos.selectionEnd);}
else if(typeof(obj.selectionStart)!="undefined")
{var begin=obj.value.substr(0,obj.selectionStart);var selection=obj.value.substr(obj.selectionStart,obj.selectionEnd-obj.selectionStart);var end=obj.value.substr(obj.selectionEnd);var newCursorPos=obj.selectionStart;var scrollPos=obj.scrollTop;obj.value=begin+text1+selection+text2+end;if(obj.setSelectionRange)
{if(selection.length==0)
{obj.setSelectionRange(newCursorPos+text1.length,newCursorPos+text1.length);}
else
{obj.setSelectionRange(newCursorPos+text1.length+selection.length+text2.length,newCursorPos+text1.length+selection.length+text2.length);}}
obj.focus();}
else
{obj.value+=text1+text2;obj.focus(obj.value.length-1);}}
function surroundTextPrompt(tag,obj)
{if(obj.createTextRange)
{var noTextSelected=(document.selection.createRange().text=="");}
else
{var noTextSelected=(obj.selectionStart==obj.selectionEnd);}
if(noTextSelected)
{alert((tag=='url')?'Please select a text for your link.':'Please select a text to quote.');}
else
{userInput=prompt((tag=='url')?'Enter a location: (www.pokerroom.com)':'Enter quote headline: ',(tag=='url')?'www.pokah.com':'Headline');if(userInput)
{switch(tag)
{case'url':if(userInput)
{if(userInput.indexOf("://")==-1)
{userInput="http://"+userInput;}
startTag='[URL=%s]';startTag=startTag.replace(/%s/i,userInput);}
else
{startTag='[URL]';}
endTag='[/URL]';break;case'quote':startTag='\n[QUOTE=%s]\n';endTag='\n[/QUOTE]\n';(userInput)?startTag=startTag.replace(/%s/i,userInput):startTag=startTag.replace(/%s/i,'Quote:');break;default:return false;}
surroundText(startTag,endTag,obj);}}
obj.focus();}
