﻿function BBCodeAddTag( tag, textArea ) {
	var strStartCode = "";
	var strEndCode = "";
	switch(tag) {
		case 'B':
			strStartCode = "[b]"
			strEndCode = "[/b]";
			break;
		case 'I':
			strStartCode = "[i]"
			strEndCode = "[/i]";
			break;
		case 'U':
			strStartCode = "[u]"
			strEndCode = "[/u]";
			break;
	}
	if(strStartCode != "") {
		BBCodeInsertCode( strStartCode, strEndCode, textArea );
	}
}

function BBCodeAddImg(textArea) {
    jPrompt('Klistra in länken till bilden inkl. (http://)', 'http://', 'Infoga bild', function(r) {
        if( r ) 
        {
        	var strImg = r + "";
	        if(strImg != "") {
		        BBCodeInsertCodeEnd("[img]" + strImg + "[/img]", textArea);
	        }
        }
    });
}
function BBCodeAddYouTube(textArea) {
    jPrompt('Klistra in länken till YouTube inkl. (http://)', 'http://', 'Infoga YouTube film', function(r) {
        if( r ) 
        {
        	var strMovie = r + "";
	        if(strMovie != "") {
	            if(strMovie.indexOf("?v=") > -1) {
	                strMovie = strMovie.substr(strMovie.indexOf("?v=") + 3, strMovie.length);
	            }
	            if(strMovie.indexOf("&v=") > -1) {
	                strMovie = strMovie.substr(strMovie.indexOf("&v=") + 3, strMovie.length);
	            }
	            if(strMovie.indexOf("&") > -1) {
	                strMovie = strMovie.substr(0, strMovie.indexOf("&"));
	            }
		        BBCodeInsertCodeEnd("[youtube]" + strMovie + "[/youtube]", textArea);
	        }
        }
    });
}
function BBCodeAddLink(textArea) {
    jPrompt('Klistra in länken inkl. (http://)', 'http://', 'Infoga länk steg 1', function(r) {
        if( r ) 
        {
            var strUrl = "" + r;
            if(strUrl != "http://")
            {
                jPrompt('Ange rubrik för länken', '', 'Infoga länk steg 2', function(r) {
                    if( r ) {
                        var strDesc = "" + r;
        			    BBCodeInsertCodeEnd("[url=" + strUrl + "]" + strDesc + "[/url]", textArea);
                    } else {
    			        BBCodeInsertCodeEnd("[url]" + strUrl + "[/url]", textArea);
                    }
                });
            }
        }
    });
}
function BBCodeAddEmail(textArea) {
    jPrompt('Ange e-postadress', '', 'Infoga e-postadress', function(r) {
        if( r ) 
        {
            var strUrl = "" + r;
			BBCodeInsertCodeEnd("[email]" + strUrl + "[/email]", textArea);
        }
    });
}
function BBCodeInsertCode( strStart, strEnd, textArea) {
	var txtInfo = document.getElementById(textArea);
	// IE
	if (document.selection) {
		txtInfo.focus();
		sel = document.selection.createRange();
		var strText = sel.text;
		if(strText != "") {
			strText = strStart + strText + strEnd;
		} else {
			strText = strStart + strEnd;
		}
		sel.text = strText;
	}
	
	//MOZILLA/NETSCAPE
	else if (txtInfo.selectionStart || txtInfo.selectionStart == '0') {
		var startPos = txtInfo.selectionStart;
		var endPos = txtInfo.selectionEnd;
		txtInfo.value = txtInfo.value.substring(0, startPos) + strStart + txtInfo.value.substring(startPos, endPos) + strEnd + txtInfo.value.substring(endPos, txtInfo.value.length);
	} else {
		txtInfo.value += myValue;
	}
	txtInfo.focus();
}

function BBCodeInsertCodeEnd( strCode, textArea) {
	var txtInfo = document.getElementById(textArea);
	txtInfo.value += "\n\n" + strCode;
	txtInfo.focus();
}