//<script>

var ae_saved_range;

function ae_bs_saveRange(num) {
	var sel=ae_bs_sel(aeObjects[num]);
	ae_saved_range=ae_bs_range(aeObjects[num]);
}

function ae_bs_restoreRange(num) {
	ae_bs_updateSelection(aeObjects[num], ae_saved_range);
}

function ae_bs_blockfmt(num, fmt) {
	if(fmt=='') {
		ae_bs_wrapTag(num, null, ['PRE','ADDRESS','H1','H2',
			'H3','H4','H5','H6','OL',
			'UL','DIR','MENU','DL',
			'DT','P']);
	} else {
		ae_bs_wrapTag(num, fmt, ['PRE','ADDRESS','H1','H2',
			'H3','H4','H5','H6','OL',
			'UL','DIR','MENU','DL',
			'DT','P']);
	}
}


function ae_bs_setFontSizeInPoints(instance, size) {
	instance.contentDocument.execCommand('FontSize', false, size+"pt");
}

function ae_bs_getFontSizeInPoints(instance) {
	var fs=instance.contentDocument.queryCommandValue("FontSize");
	if(fs&&fs.length>2) {
		if(fs.substring(fs.length-2,fs.length)=='px') {
			var f=parseFloat(fs.substring(0,fs.length-2));
			return Math.round(f*0.75);
		}	
	}
	return null;
}

function ae_bs_removeTags(node, removeTagArr) {
	var nodeStack=Array();
	var cnode=node.firstChild;
	nodeloop:
	while(cnode!=null) {
		for(var i=0;i<removeTagArr.length;i++) {
			if(cnode.nodeName.toLowerCase()==removeTagArr[i].toLowerCase()) {
				var first=cnode.firstChild;
				var n=first;
				while(n!=null) {
					var next=n.nextSibling;
					cnode.removeChild(n);
					cnode.parentNode.insertBefore(n, cnode);
					n=next;
				}
				var remove=cnode;
				if(first) {
					cnode=first;
				} else if(cnode.nextSibling) {
					cnode=cnode.nextSibling;
				} else {
					while(cnode!=null&&cnode.nextSibling==null) {
						cnode=nodeStack.pop();
						if(cnode!=null && cnode.nextSibling) {
							cnode=cnode.nextSibling;
							break;
						}
					}
				}
				remove.parentNode.removeChild(remove);
				continue nodeloop;
			}
		}
		
		if(cnode.firstChild) {
			nodeStack.push(cnode);
			cnode=cnode.firstChild;
		} else if(cnode.nextSibling) {
			cnode=cnode.nextSibling;
		} else {
			while(cnode!=null&&cnode.nextSibling==null) {
				cnode=nodeStack.pop();
				if(cnode!=null && cnode.nextSibling) {
					cnode=cnode.nextSibling;
					break;
				}
			}
		}
	}
}

function ae_bs_isControlSelection(num) {
	return false;
}

function ae_bs_splitTagAroundRange(node, range) {
	var nodeStack=Array();
	var r1=range.cloneRange();
	var r2=range.cloneRange();
	r1.collapse(true);
	r1.setStartBefore(node);
	r2.collapse(false);
	r2.setEndAfter(node);
	range.selectNode(node);
	var c1,c2,c3;
	if(r1.toString()!='') c1=r1.extractContents();
	if(r2.toString()!='') c2=r2.extractContents();
	c3=range.extractContents();
	var selNode=c3.firstChild;
	if(c2!=null) range.insertNode(c2);
	if(c3!=null) range.insertNode(c3);
	range.insertNode(c1);
	range.selectNode(selNode);
}

function ae_bs_wrapTag(num, tag, removeTags,attributes) {
	var instance=aeObjects[num];
	if(removeTags==null) removeTags=[tag];
	var range=ae_bs_range(instance);
	ae_bs_emptysel(instance);
	var node=range.commonAncestorContainer;
	var onlyRemove=(tag==null);
	var found;
	nodeLoop:
	while(node!=null && ae_isFormatTag(node.nodeName)) {
		if(equalsIgnoreCase(node.nodeName,tag)) {
			var parent=node.parentNode;
			ae_bs_splitTagAroundRange(node, range);
			node=parent;
			onlyRemove=true;
		} else {
			for(var i=0;i<removeTags.length;i++) {
				if(node.nodeName==removeTags[i]) {
					var parent=node.parentNode;
					ae_bs_splitTagAroundRange(node, range);
					node=parent;
					onlyRemove=true;
					continue nodeLoop;
				}
			}
			node=node.parentNode;
		}
	}
	
	var contents=range.extractContents();
	if(!onlyRemove) {
		var first=contents.firstChild;
		if(first.nodeType==Node.TEXT_NODE&&first.nodeValue=="") 
			first=first.nextSibling;
		onlyRemove=first==contents.lastChild && 
			equalsIgnoreCase(first.nodeName,
				tag);
	}
	
	ae_bs_removeTags(contents, removeTags);

	if(onlyRemove) {
		var f=contents.firstChild;
		var l=contents.lastChild;
		range.insertNode(contents);
		range.setStartBefore(f);
		range.setEndAfter(l);
	} else {
		var newNode=instance.contentDocument.createElement(tag);
		if(attributes!=null) {
			for(var attr in attributes) {
				newNode.setAttribute(attr, attributes[attr]);
			}
		}
		newNode.appendChild(contents);
		range.insertNode(newNode);
		range.selectNode(newNode);
	}
	ae_bs_updateSelection(instance, range);
}

function ae_bs_updateSelection(instance, range) {
	var sel=instance.contentWindow.getSelection();
	sel.setBaseAndExtent(range.startContainer, range.startOffset, 
		range.endContainer, range.endOffset);
}

function ae_bs_range(instance) {
	var range=instance.contentDocument.createRange();
	var sel=ae_bs_sel(instance);
	range.setStart(sel.anchorNode, sel.anchorOffset);
	range.setEnd(sel.focusNode, sel.focusOffset);
	range.parentElement=new Function("return this.commonAncestorContainer;");

	return range;
}

function ae_bs_getTextContent(instance) {
	range = instance.contentDocument.createRange();
	referenceNode = instance.contentDocument.getElementsByTagName("BODY").item(0);
	range.selectNodeContents(referenceNode);
	return range.toString();
}

function ae_bs_setTextContent(instance, text) {
	var t = document.createTextNode(text);
	instance.contentDocument.body.appendChild(t);
}

function ae_bs_emptysel(instance) {
	instance.contentWindow.getSelection().collapseToStart();
}

function ae_bs_sel(instance) {
	var sel=instance.contentWindow.getSelection();
    if (sel && sel.anchorNode && sel.focusNode && (sel.anchorNode.nodeType == 3) && 
			(sel.anchorNode == sel.focusNode) && (sel.anchorOffset == sel.focusOffset)) {
		var pos = sel.anchorOffset - ("" + sel).length;
		if (pos < 0) pos = 0;
		var found= sel.anchorNode.nodeValue.substring(pos).indexOf(sel);
		var pos2=pos+found + ("" + sel).length;
		var node=sel.anchorNode;
		if (found >= 0) {
			sel.collapseToStart();
			sel.setBaseAndExtent(node,
				pos + found,
				node,
				pos2);
		}
	}	
	return sel;
}

function ae_bs_busycheck(num) {
	return aeObjects[num].contentDocument?false:true;	
}

function ae_bs_init(num) {
	var instance=aeObjects[num];
	thisContentItem=document.getElementById('ae_tx_content'+num);
	thisForm=thisContentItem;
	while(thisForm.parentNode&&thisForm.nodeName!="FORM"&&thisForm.nodeName!="HTML") {
		thisForm=thisForm.parentNode; 
	}
	if(thisForm.nodeName!="HTML") {
		//bind onSubmit event to ae_onSubmit()
		thisForm.onsubmit = ae_onSubmit;
	}
	else {
		alert("Activedit must be contained in a form.", "", true);
		return;
	}
}

function ae_bs_load(num) {
	var instance=aeObjects[num];

	thisContentItem=document.getElementById('ae_tx_content'+num);
	if(ae_stylesheet[num].length) {
		instance.contentDocument.write("<HTML><HEAD>"+
			"<LINK rel='stylesheet' href='"+ae_stylesheet[num]+"' type='text/css'></LINK>"+
			"</HEAD><BODY></BODY></HTML>");
	}
	instance.contentDocument.body.innerHTML=thisContentItem.value;
	instance.contentDocument.designMode = "on";
	instance.contentDocument.oncontextmenu=new Function("e", "ae_contextmenu("+num+",e);");
	instance.contentDocument.onmousedown=new Function("e", "ae_mousedown("+num+",e);");
	instance.contentDocument.onmouseup=new Function("e", "ae_bs_event("+num+", e);");
	instance.contentDocument.onclick=new Function("e", "ae_bs_event("+num+", e);");
	instance.contentDocument.ondragstart=new Function("event",
		"event.preventDefault();");
	instance.contentDocument.onkeydown=new Function("e", "ae_bs_event("+num+",e);");
		
	if(oSel=document.getElementById('oQuickFormat'+num)) {
		var arr = ['','PRE','ADDRESS','H1','H2',
			'H3','H4','H5','H6','OL',
			'UL','DIR','MENU','DL',
			'DT','P'];
		for (var i=0;i<arr.length;i++) {
			if (quickformatNameArray[num][i] != "e") {
				oSel.options[oSel.options.length]=new Option(quickformatNameArray[num][i], arr[i]);
			}
		}
	}
		
	
}
var selfixed=false;
function ae_bs_event(num, e) {
	var instance=aeObjects[num];
	switch(e.type) {
	case 'click':
		for (var node=e.target; node && (node.nodeName != "BODY"); node = node.parentNode) {
			if ((node.nodeName == "A") || (node.nodeName == "AREA") || ((node.nodeName == "INPUT") && ((node.type.toUpperCase() == "BUTTON") || (node.type.toUpperCase() == "SUBMIT")))) {
				e.preventDefault();
				e.stopPropagation();
				return false;
			}
		}
		ae_onclick(num);
		break;
	case 'keydown':
		window.status=e.keyCode;
		if (e.metaKey && (e.keyCode == 65)) {
			e.preventDefault();
			e.stopPropagation();
			var sel = ae_bs_sel(instance);
			sel.setBaseAndExtent(instance.contentDocument.body, 0, instance.contentDocument.body, instance.contentDocument.body.childNodes.length);
		}
		break;
	case 'mousedown': //see if everything is selected
		
		break;
	case 'mouseup': //fix selection problems
		sel = ae_bs_sel(instance);
		var node=sel.anchorNode;
		var allSelected=true;
		
		if(sel.anchorNode.nodeType==TEXT_NODE && sel.anchorOffset!=0) {
			allSelected=false;
		} else if(sel.focusNode.nodeType==TEXT_NODE && sel.focusOffset!=sel.focusNode.nodeValue.length) {
			allSelected=false;
		} else {
			while(node.parentNode&&node.parentNode.nodeName!='BODY') {
				if(node!=node.parentNode.firstChild) {
					allSelected=false;
					break;
				}
				node=node.parentNode;
			}
		}
		if(allSelected&&node.parentNode.nodeName=='BODY') {
			while(node.parentNode&&node.parentNode.nodeName!='BODY') {
				if(node!=node.parentNode.lastChild) {
					allSelected=false;
					break;
				}
				node=node.parentNode;
			}
			if(!selfixed&&allSelected&&node.parentNode.nodeName=='BODY') {
				e.preventDefault();
				e.stopPropagation();

				sel.setBaseAndExtent(instance.contentDocument.body, 0, instance.contentDocument.body, instance.contentDocument.body.childNodes.length);
				selfixed=true;
			}
		} else {
			selfixed=false;
		}

		ae_onclick(num);
		break;
	default:
		//nothin
	}
}

function ae_bs_command(cmd, num) {
	var instance=aeObjects[num];
	instance.contentDocument.execCommand(cmd, false, null);
}

function ae_bs_getEventSrc(e) {
	return e.srcElement;
}

var simplecmds=['Undo', 'Redo', 'Cut', 'Copy', 'Paste', 'Bold','Italic','Underline','JustifyCenter','JustifyLeft','JustifyRight'];


function ae_bs_getCommandState(num, cid) {
	var instance=aeObjects[num];
	if(ae_isSimpleCommand(cid)) {
		return ae_queryCommandState(num, cid);
	} else {
		return AE_ENABLED;
	}
}


function ae_bs_pasteNode(num, node, range) {
	var instance=aeObjects[num];
	if(range==null) range=ae_bs_range(instance);
	range.extractContents();
	range.insertNode(node);
}

function ae_bs_pasteHTML(num, range, tagname, attributes, contents) {
	var instance=aeObjects[num];
	var el=instance.contentDocument.createElement(tagname);
	for(var attrname in attributes) {
		el.setAttribute(attrname, attributes[attrname]);
	}
	if(contents!=null)
		el.innerHTML=contents;
	range.extractContents();
	range.insertNode(el);
}

//</script>