//<script>

function ae_bs_saveRange(num) {}

function ae_bs_restoreRange(num) {}

function ae_bs_blockfmt(instance, fmt) {
	switch(fmt) {
	case '':
		instance.contentDocument.execCommand('RemoveFormat', false, null);
	case '<ul>':
		instance.contentDocument.execCommand('InsertUnorderedList', false, null);
		break;
	case '<ol>':
		instance.contentDocument.execCommand('InsertOrderedList', false, null);
		break;
	default:
		instance.contentDocument.execCommand('FormatBlock', false, fmt);
		break;
	}
}

var fontmap=[8,10,12,14,18,24,36];

function ae_bs_setFontSizeInPoints(instance, size) {
	for(var i=0;i<6;i++) {
		if(i==6||size<fontmap[i+1]) {
			instance.contentDocument.execCommand('FontSize', false, i+1);
			return;
		}
	}
}

function ae_bs_getFontSizeInPoints(instance) {
	var fs=instance.contentDocument.queryCommandValue("FontSize");
	if(fs>0) {
		return fontmap[fs-1];
	}
}


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=false;
	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) {
		onlyRemove=equalsIgnoreCase(contents.firstChild.nodeName.toLowerCase(),tag.toLowerCase())&&contents.firstChild==contents.lastChild;
	}
	
	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) {
	instance.contentWindow.getSelection().removeAllRanges();
	instance.contentWindow.getSelection().addRange(range);
}

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

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_removetags(node, remove) {
	var n=node.firstChild;
	while(n!=null) {
		if(n.nodeType==Node.ELEMENT_NODE) {
			if(n.nodeName==remove) {
				var n1=n;
				while(n1.firstChild) {
					node.insertBefore(ae_bs_removetags(n1.removeChild(n1.firstChild), remove), n1);
				}
				n=n1.nextSibling();
				node.removeChild(n1);
			} else {
				ae_bs_removetags(n, remove);
			}
		} else {
			n=n.nextSibling;
		}
	}
	return node;
}

function ae_bs_range(instance) {
	var sel=instance.contentWindow.getSelection();
	var range=sel.getRangeAt(0);
	range.parentElement=new Function("return this.commonAncestorContainer;");
	return range;
}

function ae_bs_sel(instance) {
	var sel=instance.contentWindow.getSelection();
	return sel;
}

function ae_bs_busycheck(num) {
	if(aeObjects[num].contentDocument.body) {
		return false;
	} else {
		return true;
	}
}

function ae_bs_init(num) {
	var instance=aeObjects[num];
    window.captureEvents(Event.SUBMIT);
    window.onsubmit=ae_onSubmit;
}

function ae_bs_load(num) {
	var instance=aeObjects[num];
	var d=instance.contentDocument;
	if(ae_stylesheet[num].length) {
		var objCSS = d.createElement('link');
		objCSS.rel = 'stylesheet';
		objCSS.href = ae_stylesheet[num];
		objCSS.type = 'text/css';
		d.getElementsByTagName('head')[0].appendChild(objCSS);
	}
	
	thisContentItem=document.getElementById('ae_tx_content'+num);
	d.body.innerHTML=thisContentItem.value;
	d.designMode = "on";
	instance.contentDocument.addEventListener("contextmenu",new Function("e", "ae_contextmenu("+num+",e)"), true);
	instance.contentDocument.addEventListener("mousedown",new Function("e", "ae_mousedown("+num+",e)"), true);
	instance.contentDocument.addEventListener("click",new Function("ae_refreshToolbar("+num+");"), true);
	instance.contentDocument.addEventListener("mouseup",new Function("ae_refreshToolbar("+num+");"), true);
	instance.contentDocument.addEventListener("keypress",new Function("ae_refreshToolbar("+num+");"), true);

	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]);
			}
		}
	}

}

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

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

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);
}

function ae_bs_getSelectedImage(num) {
	var instance=aeObjects[num];
	var nodeStack=Array();
	var range=ae_bs_range(instance);
	var imgs=instance.contentDocument.body.getElementsByTagName('IMG');
	for(var i=0;i<imgs.length;i++) {
		if(range.intersectsNode(imgs[i])) {
			return imgs[i];
		}
	}
	return null;
}

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

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


	
//</script>