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 target="_blank" href="http://'+newurl+'">',"</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 createDoclink(){
    linkdiv = document.getElementById('doclink');
    linkdiv.style.display = 'block';
    return false;
}

function insertDoclink(){
    //alert('test?');
    newurl = document.getElementById('docurl').value;
    if(newurl.substring(0,5) == "dir::"){
        if(confirm('Eventuele wijzigingen in de text gaan verloren. Wilt u doorgaan? \n\n '
                   + ' Sla de tekst eerst op \n navigeer dan naar de gewenste map \n'
                   + ' selecteer de tekst voor de link en \n klik dan op link.')){
           updateDocLink(newurl.substring(5));
            return;
        } else {
           return;
        }
    }
    AddSelText('<a href="uploads.php?file=/documents/uploads'+newurl+'">',"</a>");
    div = document.getElementById('doclink');
    div.style.display = 'none';
}

function updateDocLink(path){
    //xalert('A dir! Oh dear! How queer');
    var s = window.location.href;
    var pos = s.indexOf("&dir=");
    if(pos>=0){
        s = s.substring(0, pos);
    } 
    window.location = s + "&dir=" + path;
}

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 italic(){
    AddSelText('<i>','</i>');
}

function underline(){
    AddSelText('<u>','</u>');
}

function blockquote(){
    AddSelText('<blockquote>','</blockquote>');
}

function listitem(){
    AddSelText('<li>','</li>');
}

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;
}

function createEmaillink(){
    linkdiv = document.getElementById('emaillink');
    linkdiv.style.display = 'block';
    return false;
}

function insertEmaillink(){
    newurl = document.getElementById('emailurl').value;
    re = new RegExp("^mailto:\\/\\/","g");
    newurl = newurl.replace(re,'');
    AddSelText('<a href="mailto:'+newurl+'">',"</a>");
    linkdiv = document.getElementById('emaillink');
    linkdiv.style.display = 'none';
}
