var popUpWin=0;

function ShowColour() {
	pbar_bbcode('colour');	
	popUpWindow('http://www.digitalflow.org/board/colorpick.php');
	return false;
}

function popUpWindow(URLStr){
	if(popUpWin){
		if(!popUpWin.closed) popUpWin.close();
	}
	
	popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width=200,height=200');
}

function pbar_bbcode(tag) {
	
	var textarea = document.getElementById('thepost');
	var currentscroll = textarea.scrollTop;
	var topen = "";
	var tclose = "";
	var tprompt = "";

	switch(tag) {
		case 'b':
			topen = '[b]';
			tclose = '[/b]';
		break;
		case 'i':
			topen = '[i]';
			tclose = '[/i]';
		break;
		case 'u':
			topen = '[u]';
			tclose = '[/u]';
		break;
		case 'font':
			topen = '[font=font]';
			tclose = '[/font]';
		break;
		case 'h1':
			topen = '[h1]';
			tclose = '[/h1]';
		break;
		case 'strike':
			topen = '[strike]';
			tclose = '[/strike]';
		break;
		case 'colour':
			topen = '[color=#000000]';
			tclose = '[/color]';
		break;
		case 'sub':
			topen = '[sub]';
			tclose = '[/sub]';
		break;
		case 'sup':
			tclose = '[sup]';
			tclose = '[/sup]';
		break;
		case 'left':
			topen = '[left]';
			tclose = '[/left]';
		break;
		case 'center':
			topen = '[center]';
			tclose = '[/center]';
		break;
		case 'right':
			topen = '[right]';
			tclose = '[/right]';
		break;
		case 'justify':
			topen = '[justify]';
			tclose = '[/justify]';
		break;
		case 'url':
			tprompt = prompt("Enter the URL:", "http://");
			if(tprompt.length < 11) {
				alert("This URL is not valid!");
				return false;
			}
			ttprompt = prompt("Enter Text:", "My Link");
			topen = '[url=' + tprompt + ']' + ttprompt + '';
			tclose = '[/url]';
		break;
		case 'img':
			tprompt = prompt("Enter the URL of Image:", "http://");
			if(tprompt.length < 6) {
				alert("This URL is not valid!");
				return false;
			}
			topen = '[img]' + tprompt;
			tclose= '[/img]';
		break;
		case 'email':
			tprompt = prompt("Enter the email:", "");
			if(tprompt.length < 6) {
				alert("This email is not valid!");
				return false;
			}
			topen = '[url=mailto:' + tprompt + ']';
			tclose = '[/url]';
		break;
		case 'quote':
			topen = '[quote=optional]';
			tclose = '[/quote]';
		break;
		case 'code':
			topen = '[code]';
			tclose = '[/code]';
		break;
	}
	
	if(!textarea.setSelectionRange)
    {
		
		textarea.focus();
		
        var selected = document.selection.createRange(); 
        if(!selected)
        { 
            // no text was selected so prompt the user for some text
            textarea.value += topen + " " + tclose;
        }
        else
        {
            // put the code around the selected text
            selected.text = topen + selected.text + tclose; 
        }

    }
    else
    {
        // the text before the selection
        var pretext = textarea.value.substring(0, textarea.selectionStart);
        
        // the selected text with tags before and after
        var codetext = topen + textarea.value.substring(textarea.selectionStart, textarea.selectionEnd) + tclose;

        // the text after the selection
        var posttext = textarea.value.substring(textarea.selectionEnd, textarea.value.length)
        
        // check if there was a selection
        if(codetext == topen + tclose)
        {
            //prompt the user
            codetext = topen + " " + tclose;
        }

        // update the text field
        textarea.value = pretext + codetext + posttext;
    }

    // set the focus on the text field
	textarea.scrollTop = currentscroll;
    textarea.focus();

}

function pbar_smile( smile ){
	var textarea = document.getElementById('thepost');
	
	textarea.value+=' '+smile+' ';
}
