function openWin(val, w, h, x, y){
	if(!w){w=600};
	if(!h){h=400};
	if(!x){x=50};
	if(!y){y=50};
	options = 'toolbar=no,scrollbars=yes,width='+w+',height='+h
				+',resizable=no,screenX=3,screenY=3,top='+x+',left='+y;
	newWin = window.open(val,'tcb',options);
	newWin.focus();
}

function update(textbox){
	storeCaret(textbox);
   	pv = document.getElementById('preview');
	title = document.getElementById('paragraphtitle');
   	var HTML = textbox.value;
	HTML = HTML.replace(/\n/g, "<br />\n");
	HTML = '<h3>' + title.value + '</h3>' + HTML;
   	pv.innerHTML = HTML;
}

function createExternallink(){
    linkdiv = document.getElementById('elink');
    linkdiv.style.display = 'block';
    return false;
}

function insertElink(){
    newurl = document.getElementById('eurl').value;
    re = new RegExp("^http:\\/\\/","g");
    //newurl = newurl.replace(/^http:\/\//g,'');
    newurl = newurl.replace(re,'');
    //alert(newurl);
    AddSelText('<a href="http://'+newurl+'" target="_blank">',"</a>");
    linkdiv = document.getElementById('elink');
    linkdiv.style.display = 'none';
}

function createInternallink(){
    linkdiv = document.getElementById('ilink');
    linkdiv.style.display = 'block';
    return false;
}

function insertIlink(){
    //alert('test?');
    newurl = document.getElementById('iurl').value;
    AddSelText('<a href="page.php?id='+newurl+'">',"</a>");
    div = document.getElementById('ilink');
    div.style.display = 'none';
}

function storeCaret(textbox) { 
  if (textbox.createTextRange) 
    textbox.caretPos = document.selection.createRange().duplicate();
}

function AddText(text){
  var textbox = document.getElementById('paragraphtext');
  if (textbox.caretPos){
    textbox.caretPos.text=text;
  } else if (textbox.textLength!=undefined 
	     && textbox.selectionStart!=undefined 
	     && textbox.selectionEnd!=undefined) {
    var selLength = textbox.textLength;
    var selStart = textbox.selectionStart;
    var selEnd = textbox.selectionEnd;
    if (selEnd==1 || selEnd==2){
      selEnd=selLength;
    }
    var s1 = textbox.value.substring(0,selStart);
    var s2 = textbox.value.substring(selStart, selEnd);
    var s3 = textbox.value.substring(selEnd, selLength);
    textbox.value = s1 + text + s3;
  } else {
    textbox.value+=text;
  }
  textbox.focus();
}

function AddSelText(bbopen, bbclose) {
  var textbox=document.getElementById('paragraphtext');
  if (textbox.caretPos && textbox.caretPos.text!=''){
    textbox.caretPos.text = bbopen + textbox.caretPos.text + bbclose;
  } else if(textbox.textLength!=undefined 
	    && textbox.selectionStart!=undefined 
	    && textbox.selectionEnd!=undefined) {
    var selLength = textbox.textLength;
    var selStart = textbox.selectionStart;
    var selEnd = textbox.selectionEnd;
    if (selEnd==1 || selEnd==2){
      selEnd=selLength;
    }
    var s1 = textbox.value.substring(0,selStart);
    var s2 = textbox.value.substring(selStart, selEnd);
    var s3 = textbox.value.substring(selEnd, selLength);
    textbox.value = s1 + bbopen + s2 + bbclose + s3;
  } else {
    textbox.value += bbopen + bbclose;
  }
  textbox.focus();
}

function bold(){
    AddSelText('<b>','</b>');
}

function listitem(){
    AddSelText('<li>','</li>');
}

function italic(){
    AddSelText('<i>','</i>');
}

function underline(){
    AddSelText('<u>','</u>');
}

function blockquote(){
    AddSelText('<blockquote>','</blockquote>');
}

function blockquote(){
    AddSelText('<blockquote>','</blockquote>');
}

function center(){
    AddSelText('<div style="text-align:center;">\n','\n</div>');
}

function right(){
    AddSelText('<div style="text-align:right;">\n','\n</div>');
}

function getSelectedRadioValue(){
 	var n = 0;
  	while (document.getElementById('titlecolor' + n)) {
	    if(document.getElementById('titlecolor' + n).checked){
			return document.getElementById('titlecolor' + n).value;
		}
    	n++;
	}
	return false;
}