﻿// JScript File
//function Format(){
//    var txtCode=document.getElementById('<%=txtCode.ClientID %>');
//    var body = "\n"+txtCode.value;
//    body = body.replace(/ |　/ig,"");
//    body = body.replace(/\r\n/ig,"\n");
//    body = body.replace(/\n\n/ig,"\n");
//    body = body.replace(/\n\n/ig,"\n");
//    body = body.replace(/\n\n/ig,"\n");
//    body = body.replace(/\n\n/ig,"\n");
//    body = body.replace(/\n/ig,"\n\n　　");
//    body = body.replace("\n\n","");
//    txtCode.value=body;
//}
function html2ubb(str) {
	str = str.replace(/\r/g,"");
	str = str.replace(/on(load|click|dbclick|mouseover|mousedown|mouseup)="[^"]+"/ig,"");
	str = str.replace(/<script[^>]*?>([\w\W]*?)<\/script>/ig,"");
	str = str.replace(/<a[^>]+href="([^"]+)"[^>]*>(.*?)<\/a>/ig,"[url=$1]$2[/url]");
	str = str.replace(/<font[^>]+color=([^ >]+)[^>]*>(.*?)<\/font>/ig,"[color=$1]$2[/color]");
	str = str.replace(/<img[^>]+src="([^"]+)"[^>]*>/ig,"[img]$1[/img]");
    str = str.replace(/<([\/]?)b>/ig,"[$1b]");
	str = str.replace(/<([\/]?)b>/ig,"[$1b]");
	str = str.replace(/<([\/]?)strong>/ig,"[$1b]");
	str = str.replace(/<([\/]?)u>/ig,"[$1u]");
	str = str.replace(/<([\/]?)i>/ig,"[$1i]");
	str = str.replace(/<([\/]?)s>/ig,"[$1s]");

	str = str.replace(/&nbsp;/g," ");
	str = str.replace(/&amp;/g,"&");
	str = str.replace(/&quot;/g,"\"");
	str = str.replace(/&lt;/g,"<");
	str = str.replace(/&gt;/g,">");

	str = str.replace(/<br \/>/ig,"\n");
	str = str.replace(/<br>/ig,"\n");
	str = str.replace(/<[^>]*?>/g,"");
	str = str.replace(/\[url=([^\]]+)\]\n(\[img\]\1\[\/img\])\n\[\/url\]/g,"$2");
	str = str.replace(/\n+/g,"\n");
	return str;
}
function AddUbb(openTag,closeTag,Text){
    txtCode.focus();
    var theSelection = document.selection.createRange();
    var selectLength = theSelection.text.length;
    var code = '';
    if(Text && Text.length > 0){
        selectLength = Text.length;
        code = openTag + Text + closeTag;
    }else{
        code = openTag + theSelection.text + closeTag;
    }
    theSelection.text = code;
    theSelection.moveStart('character',-selectLength-closeTag.length);
    theSelection.moveEnd('character',-closeTag.length);
    theSelection.select();
}
function RegularTag(tag,value){
    tag=tag.toUpperCase();
    if(value)
        AddUbb('['+tag+']','[/'+tag+']',value);
    else
        AddUbb('['+tag+']','[/'+tag+']');
}
function ValueTag(tag,value){
    tag = tag.toUpperCase();
    if(value){
        value = value.toUpperCase();
        AddUbb('['+tag+'='+value+']','[/'+tag+']');
    }else{
        AddUbb('['+tag+']','[/'+tag+']');
    }
}
function ForeColor(){
    var color = showModalDialog(selColor, "color", "dialogWidth:20em; dialogHeight:22em; status:0; help:0");
    if (color && color.length == 7)
        ValueTag('color',color);
}
function BGColor(){
    var color = showModalDialog(selColor, "color", "dialogWidth:20em; dialogHeight:22em; status:0; help:0");
    if (color && color.length == 7)
        ValueTag('bgcolor',color);
}
function DoCommand(command){
    txtCode.focus();
    document.execCommand(command);
}
function DoPaste(){
    var pos = document.selection.createRange();
    itm.focus();
    itm.document.body.innerHTML = '';
    itm.document.execCommand('paste');
    pos.text = html2ubb(itm.document.body.innerHTML);
    return false;
}
function AddImage(){
    var myPic = prompt('输入图片的地址', 'http:\/\/');
    if (myPic && myPic != 'http://')
        RegularTag('img',myPic);
}
function AddLink(){
    var myUrl = prompt('请输入超链接地址',  'http:\/\/');
    if (myUrl && myUrl != 'http://'){
        txtCode.focus();
        if (document.selection.createRange().text.length > 0){
            ValueTag('url',myUrl);
        }else{
            AddUbb('[URL=' + myUrl + ']','[/URL]',myUrl);
        }
    }
}
function RemoveLink(){
    txtCode.focus();
    var theSelection = document.selection.createRange();
    var selectText = theSelection.text;
    selectText = selectText.replace(/\[URL(=.[^\]]*)?\]/ig,'');
    selectText = selectText.replace(/\[\/URL\]/ig,'');
    theSelection.text = selectText;
}
function AddEmail(){
    var myMail = prompt('请输入邮件地址',  '@');
    if (myMail && myMail.indexOf('@') > 0){
        txtCode.focus();
        if (document.selection.createRange().text.length > 0){
            ValueTag('email',myMail);
        }else{
            RegularTag('email',myMail);
        }
    }
}
