var text = "";
AddTxt = "";
function copete_getActiveText(selectedtext) {
        text = (document.all) ? document.selection.createRange().text : document.getSelection();
        if (selectedtext.createTextRange) {
            selectedtext.caretPos = document.selection.createRange().duplicate();
        }
        return true;
}

function replace(string,text,by) {
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;
    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;
    var newstr = string.substring(0,i) + by;
    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);
    return newstr;
}

function copete_NonStyle() {
			string = document.selection.createRange().text;
			stringint = form.txt_copete.value;
			text2 = string
var Tags = new Array();
Tags[1]='<b>';
Tags[2]='</b>';
Tags[3]='<i>';
Tags[4]='</i>';
Tags[5]='<u>';
Tags[6]='</u>';
for (a in Tags) {
			text = Tags[a];
			by = '';
			text2 = replace(text2,text,by);
}
            form.txt_copete.value=replace(stringint,string,text2);
			form.txt_copete.focus();
}

function copete_TextStyle(instyle, outstyle) {
        if (form.txt_copete.createTextRange && form.txt_copete.caretPos) {
                var caretPos = form.txt_copete.caretPos;
                caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? "<"+instyle+">"+caretPos.text+"</"+outstyle+">" : "<"+instyle+">"+caretPos.text+"</"+outstyle+">";
        } else {
				alert('Selección no válida');
                //form.txt_copete.value+="<"+instyle+">"+caretPos.text+"</"+outstyle+">";
        }
        form.txt_copete.focus();
		AddTxt = "";
}
