function instag(tag,campo){
	var input = document.getElementById(campo);
		if(typeof document.selection != 'undefined' && document.selection) {
			var str = document.selection.createRange().text;
			input.focus();
			var sel = document.selection.createRange();
			sel.text = "[" + tag + "]" + str + "[/" +tag+ "]";
			sel.select();
			return;
		}
		else if(typeof input.selectionStart != 'undefined'){
				if(typeof input.scrollTop != 'undefined'){
					var st=input.scrollTop;
					var sl=input.scrollTop;
				}
			var start = input.selectionStart;
			var end = input.selectionEnd;
			var insText = input.value.substring(start, end);
			input.value = input.value.substr(0, start) + '['+tag+']' + insText + '[/'+tag+']'+ input.value.substr(end);
			input.focus();
			if(typeof input.scrollTop != 'undefined'){
					input.scrollTop=st;
					input.scrollTop=sl;
			}
			input.setSelectionRange(start+2+tag.length+insText.length+3+tag.length,start+2+tag.length+insText.length+3+tag.length);
			return;
		}
		else{
			input.value+=' ['+tag+']Reemplace este texto[/'+tag+']';
			return;
		}
}

function inslink(campo){
	var input = document.getElementById(campo);
		if(typeof document.selection != 'undefined' && document.selection) {
			var str = document.selection.createRange().text;
			input.focus();
			var my_link = prompt("Enter URL:","http://");
				if (my_link != null) {
					if(str.length==0){
						str=my_link;
					}
					var sel = document.selection.createRange();
					sel.text = "[a href=\"" + my_link + "\"]" + str + "[/a]";
					sel.select();
					}
			return;
		}else if(typeof input.selectionStart != 'undefined'){
				if(typeof input.scrollTop != 'undefined'){
					var st=input.scrollTop;
					var sl=input.scrollTop;
				}		
					var start = input.selectionStart;
					var end = input.selectionEnd;
					var insText = input.value.substring(start, end);
					var my_link = prompt("Enter URL:","http://");
						if (my_link != null) {
							if(insText.length==0){
								insText=my_link;
							}
							input.value = input.value.substr(0, start) +"[a href=\"" + my_link +"\"]" + insText  + "[/a]"+ input.value.substr(end);
							input.focus();
							if(typeof input.scrollTop != 'undefined'){
								input.scrollTop=st;
								input.scrollTop=sl;
							}
							input.setSelectionRange(start+11+my_link.length+insText.length+4,start+11+my_link.length+insText.length+4);
						}
			return;
		}else{
			var my_link = prompt("Ingresar URL:","http://");
			var my_text = prompt("Ingresar el texto del link:","");
			input.value+=" [a href=\"" + my_link +  "\"]" + my_text + "[/a]";
			return;
		}
}

var reng=5;

function agregar(area){
	document.getElementById('quita').disabled=false;
	reng=reng+5;
	document.getElementById(area).rows=reng;
}

function quitar(area){
	reng=reng-5;
		if(reng<6){reng=5;
		}
	document.getElementById(area).rows=reng;
}

function ventana_emot(area){
	coordx=screen.width?(screen.width-300)/2:0;
	coordy=screen.height?(screen.height-150)/2:0;
	window.open("bbcode/emoticons/emoticons.html?campo="+area,"EMOTICONS","width=300,height=150,menubar=no,resizable=yes,left="+coordx+",top="+coordy);
}

function ins_imag(emot,area){
	var input = document.getElementById(area);
		if(typeof document.selection != 'undefined' && document.selection) {
			var str =document.selection.createRange().text;
			input.focus();
			var sel =document.selection.createRange();
			sel.text = str + emot;
			sel.select();
			return;
		}
		else if(typeof input.selectionStart != 'undefined'){
				if(typeof input.scrollTop != 'undefined'){
					var st=input.scrollTop;
					var sl=input.scrollTop;
				}
			var start = input.selectionStart;
			var end = input.selectionEnd;
			var insText = input.value.substring(start, end);
			input.value = input.value.substr(0, start) + insText+ emot + input.value.substr(end);
			input.focus();
			if(typeof input.scrollTop != 'undefined'){
				input.scrollTop=st;
				input.scrollTop=sl;
			}
			input.setSelectionRange(end+emot.length,end+emot.length);
			return;
		}
		else{
			input.value+=emot;
			return;
		}
}

function captura_imag(area){
	var my_link = prompt("Ingresar URL:","http://");
		if (my_link != null) {
			ins_imag('[img src=\"'+my_link+'\"]',area);
		}
}
