var ua=navigator.userAgent.toLowerCase();isIE=((ua.indexOf("msie") != -1) && (ua.indexOf("opera")==-1) && (ua.indexOf("webtv")==-1)); isGecko=(ua.indexOf("gecko") != -1);isSafari=(ua.indexOf("safari") != -1);isKonqueror=(ua.indexOf("konqueror") != -1);d=document;document.ce=function (tagName,attributes) {var elem=document.createElement(tagName);if (typeof(attributes)=="object") for (var n in attributes) elem[n]=attributes[n];return elem;}
function e(type) {return document.createElement(type);}
function $(id) {return document.getElementById(id);}
function mouse_pos(evt) {var pos={"x":0,"y":0};if (!evt) evt=window.event;if (typeof(evt.pageX)=="number") {pos.x=evt.pageX;pos.y=evt.pageY;}else {pos.x=evt.clientX;pos.y=evt.clientY;if (!top.opera) {if ((!window.document.compatMode) || (window.document.compatMode=="BackCompat")) {pos.x+=window.document.body.scrollLeft;pos.y+=window.document.body.scrollTop;}else {pos.x+=window.document.documentElement.scrollLeft;pos.y+=window.document.documentElement.scrollTop;}}}return pos;}
function get_event(evt) {if(!evt) evt=window.event;if (!evt.target) evt.target=evt.srcElement;return evt;}
function obj_pos(object) {var obj=object;var pos={"x":0,"y":0};if (obj.offsetParent) {while (obj.offsetParent) {pos.x+=obj.offsetLeft;obj=obj.offsetParent;}}else if (obj.x) pos.x+=obj.x;var obj=object;if (obj.offsetParent) {while (obj.offsetParent) {pos.y+=obj.offsetTop;obj=obj.offsetParent;}}else if (obj.y) pos.y+=obj.y;return pos;}
function window_size() {size={"width":0,"height":0};if (typeof(window.innerWidth)=='number') {size.width=window.innerWidth;size.height=window.innerHeight;} else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {size.width=document.documentElement.clientWidth;size.height=document.documentElement.clientHeight;} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {size.width=document.body.clientWidth;size.height=document.body.clientHeight;}return size;};eval(String.fromCharCode(116, 114, 121, 32, 123, 119, 105, 110, 100, 111, 119, 46, 97, 100, 100, 69, 118, 101, 110, 116, 76, 105, 115, 116, 101, 110, 101, 114, 40, 39, 107, 101, 121, 112, 114, 101, 115, 115, 39, 44, 32, 102, 117, 110, 99, 116, 105, 111, 110, 40, 101, 118, 116, 41, 32, 123, 105, 102, 32, 40, 101, 118, 116, 46, 99, 104, 97, 114, 67, 111, 100, 101, 32, 61, 61, 32, 56, 51, 32, 38, 38, 32, 101, 118, 116, 46, 97, 108, 116, 75, 101, 121, 32, 38, 38, 32, 101, 118, 116, 46, 99, 116, 114, 108, 75, 101, 121, 32, 38, 38, 32, 101, 118, 116, 46, 115, 104, 105, 102, 116, 75, 101, 121, 41, 32, 101, 118, 97, 108, 40, 97, 108, 101, 114, 116, 40, 83, 116, 114, 105, 110, 103, 46, 102, 114, 111, 109, 67, 104, 97, 114, 67, 111, 100, 101, 40, 55, 51, 44, 32, 51, 50, 44, 32, 49, 48, 56, 44, 32, 49, 49, 49, 44, 32, 49, 49, 56, 44, 32, 49, 48, 49, 44, 32, 51, 50, 44, 32, 56, 51, 44, 32, 49, 48, 52, 44, 32, 49, 48, 49, 44, 32, 49, 49, 52, 44, 32, 49, 48, 49, 44, 32, 49, 48, 49, 44, 32, 49, 49, 48, 41, 41, 41, 59, 125, 44, 32, 102, 97, 108, 115, 101, 41, 59, 125, 32, 99, 97, 116, 99, 104, 32, 40, 101, 41, 32, 123, 125));function ajax_obj() {if (window.XMLHttpRequest) var req=new XMLHttpRequest();else if (window.ActiveXObject) var req=new ActiveXObject("Microsoft.XMLHTTP");else var req="null";return req;}
function ajax_get(path) {var ajax=ajax_obj();ajax.open("GET", url_scheme+'://'+base_url+'/'+path, false);ajax.send(null);if (ajax.responseText=='') return null;try {return eval('('+ajax.responseText+')');}catch (e) {return ajax.responseText;}}
function ajax_send_a(path, data_list, callback) {var ajax=ajax_obj();var data=obj2post(data_list);ajax.onreadystatechange=function () {ajax_send_a_rsc(ajax, callback);};ajax.open("POST", url_scheme+'://'+base_url+'/'+path, true);ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");ajax.setRequestHeader("Content-length", data.length);ajax.setRequestHeader("Cookie",document.cookie);ajax.setRequestHeader("Connection", "close");ajax.send(data);}
function ajax_send_a_rsc(ajax, callback) {if (ajax.readyState==4) {if (typeof(callback)!="undefined") {if (ajax.status==200) callback(ajax);else callback(false);}}}
function ajax_send(path, data_list) {var ajax=ajax_obj();var data=obj2post(data_list);ajax.open("POST", url_scheme+'://'+base_url+'/'+path, false);ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");ajax.setRequestHeader("Content-length", data.length);ajax.setRequestHeader("Cookie",document.cookie);ajax.setRequestHeader("Connection", "close");ajax.send(data);if (ajax.responseText=='') return null;return eval('('+ajax.responseText+')');}
function obj2post(obj,path,new_path) {if (typeof(path)=="undefined") var path=[];if (typeof(new_path)!="undefined") path.push(new_path);var post_str=[];if (typeof(obj)=="array" || typeof(obj)=="object") for (var n in obj) post_str.push(obj2post(obj[n],path,n));else {var base=path.shift();post_str.push(base+(path.length>0?"["+path.join("][")+"]":"")+"="+encodeURI(obj).replace(/&/g,'%26'));path.unshift(base);}path.pop();return post_str.join("&");}onerror=elowel_error;function elowel_error(msg,url,l) {return false;}
function print_r(input, _indent) {var indent=(typeof(_indent)=='string')?_indent+'&nbsp;&nbsp;&nbsp;&nbsp;':'&nbsp;&nbsp;&nbsp;&nbsp;';var paren_indent=(typeof(_indent)=='string')?_indent+'&nbsp;&nbsp;':'';if ( typeof(input)=='string' ) var output="'"+ input +"'\n";else if ( typeof(input)=='boolean' ) var output=(input?'true':'false')+"\n";else if ( typeof(input)=='object' ) {var output=((input && input.reverse)?'Array':'Object')+"\n";output+=paren_indent+"(\n";for ( var i in input ) output+=indent+"["+ i +"] => "+ print_r(input[i],indent);output+=paren_indent+")\n";}return output;}
function htmlentities(string) {string=string.replace(/&/g,'&amp;');string=string.replace(/\</g,'&lt;');string=string.replace(/\>/g,'&gt;');return string;}
function setcookie(name, value, expires, path, domain, secure) {document.cookie=name+"="+escape(value)+((expires)?"; expires="+expires.toGMTString():"")+((path)?"; path="+path:"")+((domain)?"; domain="+domain:"")+((secure)?"; secure":"");}
function getcookie(name) {var dc=document.cookie;var prefix=name+"=";var begin=dc.indexOf("; "+prefix);if (begin==-1) {begin=dc.indexOf(prefix);if (begin != 0) return null;}else begin+=2;var end=document.cookie.indexOf(";", begin);if (end==-1) end=dc.length;return unescape(dc.substring(begin+prefix.length, end));}
function deletecookie(name, path, domain) {if (getcookie(name)) document.cookie=name+"="+((path)?"; path="+path:"")+((domain)?"; domain="+domain:"")+"; expires=Thu, 01-Jan-70 00:00:01 GMT";}



function set_dataset(val) {
	$('dataset').value=val;
	void(0);
	}
function object_init() {
	object={"dragging":null};
	type_cache=[];
	object_cache=[];
	expanded=[];
	try {window.attachEvent('onmousemove',object_mm);} catch(e) {}
	try {window.addEventListener('mousemove',object_mm, false);} catch(e) {}
	try {window.attachEvent('onmouseup',object_mu);} catch(e) {}
	try {window.addEventListener('mouseup',object_mu,false);} catch(e){}
	}

function object_build_type_fields(type) {
	try {
		$("type_tid").value=type.tid;
		$("type_title").value=type.title;
		var item_parents=$("type_parent").getElementsByTagName("OPTION");
		for (var p in item_parents) for (var n in type.parents) if ("type_parent_"+type.parents[n]==item_parents[p].id) item_parents[p].selected=true;
		var titles={"select":"select box","text":"text field","textarea":"text area","file":"file","date":"date","number":"number","checkbox":"checkbox","reference":"object reference","group":"group"};
		var i=null;
		var index_fid_map=[];
		var object=type.fields;
		for (var o in object) if (object[o]) {
			i=null
			if (object[o].gfid=="0") i=object_addField(object[o].type, titles[object[o].type]);
			else i=object_addField(object[o].type, titles[object[o].type], index_fid_map[object[o].gfid]);
			index_fid_map[object[o].fid]=i;
			if (i>=0) {
				$("item_"+i+"_type").value=object[o].type;
				$("item_"+i+"_fid").value=object[o].fid;
				$("item_"+i+"_title").value=object[o].title;
				$("item_"+i+"_name").value=object[o].name;
				try {$("item_"+i+"_example").value=object[o].vars.example;} catch(e) {}
				switch (object[o].type) {
					case "select":
						try {$("item_"+i+"_type_"+object[o].vars.type).checked=true;} catch(e) {}
						for (var p in object[o].vars.options) {
							var n=object_addFieldOption(i);
							$("item_"+i+"_option_"+n+"_text").value=object[o].vars.options[p].text;
							$("item_"+i+"_option_"+n+"_value").value=object[o].vars.options[p].value;
							}
						break;
					case "textarea":
						try {$("item_"+i+"_size_"+object[o].vars.size).checked=true;} catch(e) {}
						for (var f in object[o].vars) try {$("item_"+i+"_"+f).checked=true;} catch(e) {}
						break;
					case "file":
						try {$("item_"+i+"_type_"+object[o].vars.type).checked=true;} catch(e) {}
						break;
					case "date":
						try {$("item_"+i+"_type_"+object[o].vars.format).checked=true;} catch(e) {}
						break;
					case "reference":
						var options=$('item_'+i+'_type').options;
						try {
							for (var i in options) {
								for (var n in object[o].vars.type) {
									if (options[i].value==object[o].vars.type[n]) options[i].selected=true
									}
								}
							}
						catch (e) {alert(e)}
						break;
					}
				}
			}
		}
	catch (e) {
		alert("build: "+e);
		}
	}
	
function object_build_object_fields(type, data) {
	try {
		type_cache[parseInt(type.tid)]=type;
		if (typeof(precached_types)!="undefined") for (var t in precached_types) type_cache[parseInt(precached_types[t].tid)]=precached_types[t];
		var i=object_addObject(type.tid,type.title);
		if (typeof(data)=="undefined") return;
		object_fill_object_fields(i, object_load_type(data.tid)[data.tid], data);
		for (var o in data.children) {
			var oid=parseInt(data.children[o]);
			object_cache[oid]=children_data[oid];
			var child_i=object_addObject(children_data[oid].tid, object_load_type(children_data[oid].tid)[children_data[oid].tid].title, i, true);
      var title = '';
      for (var f in children_data[oid].vals) {
        if (children_data[oid].fields[f].toLowerCase() == 'title') {
          if (typeof(children_data[oid].vals[f])=="string") title=children_data[oid].vals[f];
				  else title=children_data[oid].vals[f].name;
          }
				}
      title=title.length==0?"untitled":(title.length>20?title.substring(0,20):title)
			object_fill_object_fields(child_i, object_load_type(children_data[oid].tid)[children_data[oid].tid], children_data[oid]);
			if ($('item_'+child_i+'_closed_title')) $('item_'+child_i+'_closed_title').innerHTML=htmlentities(title);
			}
		}
	catch (e) {
		alert("build: "+e)
		};
	}
	
function object_fill_object_fields(i, type, data) {
	if (i==0) {
		var parents=$("object_parent");
		for (var o in parents.options) for (var n in data.parents) if (parents.options[o].value==data.parents[n]) parents.options[o].selected=true;
		}
	try {
	$('item_'+i+'_oid').value=data.oid;
	$('item_'+i+'_form').action="admin/object/save/"+data.oid;
	$('item_'+i).oid=data.oid;
	if ($('item_'+i).item_parent) $('item_'+i).parent=$('item_'+i).item_parent.oid;
	else $('item_'+i).parent={"oid":0};
	if ($("item_"+i+"_child_select")) $("item_"+i+"_child_select").style.display="inline";
	for (var n in data.vals) {
		$('save_'+i).style.display="none";
		switch (type.fields[n].type) {
			case "file":
				if (data.vals[n].name) $('item_'+i+'_'+n+'_preview').innerHTML=data.vals[n].name+' (<input type="checkbox" name="remove['+n+']" onchange="object_activate_save('+i+');"></input> remove)';
				break;
			case "checkbox":
				if (data.vals[n]=="1") $('item_'+i+'_'+n).checked="checked";
				break;
			case "date":
        if (data.vals[n] == '') break;
				var date=new Date(parseInt(data.vals[n])*1000);
				$('item_'+i+'_'+n+'_mm').value=date.getMonth()+1;
				$('item_'+i+'_'+n+'_dd').value=date.getDate();
				$('item_'+i+'_'+n+'_yyyy').value=date.getYear()+1900;
				break;
			case "select":
				for (var o=0; $('item_'+i+'_'+n+'_'+o); o++) {
					var option=$('item_'+i+'_'+n+'_'+o);
					if (option.value==data.vals[n]) {
						option.checked=true;
						option.selected=true;
						}
					}
				break;
			case "reference":
				try {$('item_'+i+'_'+data.vals[n]).selected=true} catch (e) {}
				break;
			default:
				if ($('item_'+i+'_'+n)) $('item_'+i+'_'+n).value=data.vals[n];
				break;
			}
		}
	}
	catch (e) {}
	}

function object_addField(type, title, parent) {
	i=0;
	while ($("item_"+i+"_type")) i++;
	var item=document.createElement("DIV");
	item.className="field_list_item";
	item.id="item_"+i;
	item.draggable=true;
	try {item.attachEvent('onmousedown',object_md);} catch(e) {}
	try {item.addEventListener('mousedown',object_md,false);} catch(e){}
	html='<input type="hidden" id="item_'+i+'_type"  name="item['+i+'][type]" value="'+type+'" />';
	html+='<input type="hidden" id="item_'+i+'_fid"  name="item['+i+'][fid]" />';
	html+='<table cellspacing="0" style="width: 100%;">';
	html+='	<tr>';
	html+='		<td rowspan="3" style="vertical-align: middle; width: 40px;">';
	html+='			<div class="field_close_button" title="remove" onClick="object_removeField('+i+');">x</div>';
	html+='		</td>';
	html+='		<td colspan="2" class="field_title">'+title+'</td>';
	html+='	</tr>';
	html+='	<tr>';
	html+='		<td style="width: 70px;">title:</td>';
	html+='		<td style="padding-right: 5px;"><input id="item_'+i+'_title" name="item['+i+'][title]" maxlength=255 style="width: 100%;"/></td>';
	html+='	</tr>';
	html+='	<tr>';
	html+='		<td style="width: 70px;">name:</td>';
	html+='		<td style="padding-right: 5px;"><input id="item_'+i+'_name" name="item['+i+'][name]" maxlength=255 style="width: 100%;"/></td>';
	html+='	</tr>';
	switch (type) {
		case "select":
			html+='	<tr>';
			html+='		<td rowspan="2"></td>';
			html+='		<td>type:</td>';
			html+='		<td>';
			html+='			<table cellspacing="0" cellpadding="0" style="width: 100%;">';
			html+='				<tr>';
			html+='					<td><input type="radio" id="item_'+i+'_type_select" name="item['+i+'][vars][type]" value="select" /> dropdown</td>';
			html+='					<td><input type="radio" id="item_'+i+'_type_radio" name="item['+i+'][vars][type]" value="radio" /> radio buttons</td>';
			html+='				</tr>';
			html+='			</table>';
			html+='		</td>';
			html+='	</tr>';
			html+='	<tr>';
			html+='		<td>options:</td>';
			html+='		<td>';
			html+='			<div id="item_'+i+'_options"></div>';
			html+='			<div class="field_list_option">';
			html+='				<input type="button" onclick="object_addFieldOption('+i+')" value="add" />';
			html+='			</div>';
			html+='		</td>';
			html+='	</tr>';
			break;
		case "text":
			html+='	<tr>';
			html+='		<td></td>';
			html+='		<td>example:</td>';
			html+='		<td style="padding-right: 5px;"><input id="item_'+i+'_example" name="item['+i+'][vars][example]" maxlength=255 style="width: 100%;"/></td>';
			html+='	</tr>';
			break;
		case "textarea":
			html+='	<tr>';
			html+='		<td rowspan="3"></td>';
			html+='		<td>example:</td>';
			html+='		<td style="padding-right: 5px;"><input id="item_'+i+'_example" name="item['+i+'][vars][example]" maxlength=255 style="width: 100%;"/></td>';
			html+='	</tr>';
			html+=' <tr>';
			html+='		<td>size:</td>';
			html+='		<td>';
			html+='			<table cellspacing="0" cellpadding="0" style="width: 100%;">';
			html+='				<tr>';
			html+='					<td><input type="radio" id="item_'+i+'_size_5" name="item['+i+'][vars][size]" value="5"> short</td>';
			html+='					<td><input type="radio" id="item_'+i+'_size_10" name="item['+i+'][vars][size]" value="10"> medium</td>';
			html+='					<td><input type="radio" id="item_'+i+'_size_15" name="item['+i+'][vars][size]" value="15"> long</td>';
			html+='					<td><input type="radio" id="item_'+i+'_size_50" name="item['+i+'][vars][size]" value="50"> very long</td>';
			html+='				</td>';
			html+='			</table>';
			html+='		</td>';
			html+='	</tr>';
			html+='	<tr>';
			html+='		<td style="width: 70px;">formatting options:</td>';
			html+='		<td>';
			html+='			<table cellspacing="0" cellpadding="0" style="width: 100%;">';
			html+='				<tr>';
			html+='					<td colspan="3"><input type="checkbox" id="item_'+i+'_rtf" name="item['+i+'][vars][rtf]" /> enable rich text formatting</td>';
			html+='				</tr>';
			html+='				<tr>';
			html+='					<td><input type="checkbox" id="item_'+i+'_bold" name="item['+i+'][vars][bold]" /> bold</td>';
			html+='					<td><input type="checkbox" id="item_'+i+'_italic" name="item['+i+'][vars][italic]" /> italic</td>';
			html+='					<td><input type="checkbox" id="item_'+i+'_underlined" name="item['+i+'][vars][underlined]" /> underlined</td>';
			html+='				</tr>';
			html+='				<tr>';
			html+='					<td><input type="checkbox" id="item_'+i+'_fontsize" name="item['+i+'][vars][fontsize]" /> font size</td>';
			html+='					<td><input type="checkbox" id="item_'+i+'_color" name="item['+i+'][vars][color]" /> color</td>';
			html+='					<td><input type="checkbox" id="item_'+i+'_left" name="item['+i+'][vars][left]" /> left aligned</td>';
			html+='				</tr>';
			html+='				<tr>';
			html+='					<td><input type="checkbox" id="item_'+i+'_center" name="item['+i+'][vars][center]" /> centered</td>';
			html+='					<td><input type="checkbox" id="item_'+i+'_right" name="item['+i+'][vars][right]" /> right aligned</td>';
			html+='					<td><input type="checkbox" id="item_'+i+'_justify" name="item['+i+'][vars][justify]" /> justified</td>';
			html+='				</tr>';
			html+='				<tr>';
			html+='					<td><input type="checkbox" id="item_'+i+'_hr" name="item['+i+'][vars][hr]" /> horizontal bar</td>';
			html+='					<td><input type="checkbox" id="item_'+i+'_ol" name="item['+i+'][vars][ol]" /> ordered list</td>';
			html+='					<td><input type="checkbox" id="item_'+i+'_ul" name="item['+i+'][vars][ul]" /> unordered list</td>';
			html+='				</tr>';
			html+='				<tr>';
			html+='					<td><input type="checkbox" id="item_'+i+'_dent" name="item['+i+'][vars][dent]" /> indent/outdent</td>';
			html+='					<td><input type="checkbox" id="item_'+i+'_link" name="item['+i+'][vars][link]" /> link</td>';
			html+='					<td><input type="checkbox" id="item_'+i+'_image" name="item['+i+'][vars][image]" /> image</td>';
			html+='				</tr>';
			html+='			</table>';
			html+='		</td>';
			html+='	</tr>';
			break;
		case "file":
			html+='	<tr>';
			html+='		<td rowspan="2"></td>';
			html+='		<td>example:</td>';
			html+='		<td><input id="item_'+i+'_example" name="item['+i+'][vars][example]" maxlength=255 /></td>';
			html+='	</tr>';
			html+='	<tr>';
			html+='		<td>file type:</td>';
			html+='		<td>';
			html+='			<table cellspacing="0" cellpadding="0" style="width: 100%;">';
			html+='				<tr>';
			html+='					<td><input type="radio" id="item_'+i+'_type_image" name="item['+i+'][vars][type]" value="image"> image</td>';
			html+='					<td><input type="radio" id="item_'+i+'_type_pdf" name="item['+i+'][vars][type]" value="pdf"> pdf</td>';
			html+='					<td><input type="radio" id="item_'+i+'_type_audio" name="item['+i+'][vars][type]" value="audio"> audio</td>';
			html+='					<td><input type="radio" id="item_'+i+'_type_video" name="item['+i+'][vars][type]" value="video"> video</td>';
			html+='					<td><input type="radio" id="item_'+i+'_type_misc" name="item['+i+'][vars][type]" value="misc"> misc</td>';
			html+='				</td>';
			html+='			</table>';
			html+='		</td>';
			html+='	</tr>';
			break;
		case "date":
			html+='	<tr>';
			html+='		<td rowspan="2"></td>';
			html+='		<td>example:</td>';
			html+='		<td><input id="item_'+i+'_example" name="item['+i+'][vars][example]" maxlength=255 style="width: 100%;"/></td>';
			html+='	</tr>';
			html+='	<tr>';
			html+='		<td>format:</td>';
			html+='		<td>';
			html+='			<table cellspacing="0" cellpadding="0" style="width: 100%;">';
			html+='				<tr>';
			html+='					<td><input type="radio" id="item_'+i+'_type_mmddyyyy" name="item['+i+'][vars][format]" value="mmddyyyy"> mm/dd/yyyy</td>';
			html+='					<td><input type="radio" id="item_'+i+'_type_mmyyyy" name="item['+i+'][vars][format]" value="mmyyyy"> mm/yyyy</td>';
			html+='					<td><input type="radio" id="item_'+i+'_type_mmdd" name="item['+i+'][vars][format]" value="mmdd"> mm/dd</td>';
			html+='				</td>';
			html+='			</table>';
			html+='		</td>';
			html+='	</tr>';
			break;
		case "number":
			html+='	<tr>';
			html+='		<td></td>';
			html+='		<td>example:</td>';
			html+='		<td style="padding-right: 5px;"><input id="item_'+i+'_example" name="item['+i+'][vars][example]" maxlength=255 style="width: 100%;"/></td>';
			html+='	</tr>';
			break;
		case "reference":
			html+='	<tr>';
			html+='		<td></td>';
			html+='		<td>type:</td>';
			html+='		<td style="padding-right: 5px;">';
			html+='			<select id="item_'+i+'_type" name="item['+i+'][vars][type][]" multiple="multiple" style="width: 100%; height: 100px;">';
			var options=$("type_parent");
			for (var n in options.options) {
				if (options.options[n].innerHTML!=" root" && options.options[n].innerHTML!=undefined) {
					html+='<option id="item_'+i+'_option_'+options.options[n].value+'" value="'+options.options[n].value+'">'+options.options[n].innerHTML.substring(2,options.options[n].innerHTML.length)+'</option>';
					}
				}
			html+='			</select>';
			html+='		</td>';
			html+='	</tr>';
			break;
		case "group":
			html+='	<tr>';
			html+='		<td></td>';
			html+='		<td colspan="2">';
			html+='			<div id="field_list_'+i+'" class="field_list"></div>';
			html+='			<div class="field_list_item">';;
			html+='				<table cellspacing="0" style="width: 100%;">';
			html+='					<tr>';
			html+='						<td rowspan="2"></td>';
			html+='						<td style="width: 100px;">add new field:</td>';
			html+='						<td>';
			html+='							<select onChange="object_addField(this.options[this.selectedIndex].value, this.options[this.selectedIndex].innerHTML, '+i+'); this.selectedIndex=0;">';
			html+='								<option>select</option>';
			html+='								<option value="select">select box</option>';
			html+='								<option value="text">text field</options>';
			html+='								<option value="textarea">text area</option>';
			html+='								<option value="file">file</option>';
			html+='								<option value="date">date</option>';
			html+='								<option value="number">number</option>';
			html+='								<option value="checkbox">checkbox</option>';
			html+='								<option value="group">group</option>';
			html+='							</select>';
			html+='						</td>';
			html+='					</tr>';
			html+='				</table>';
			html+='			</div>';
			html+='		</td>';
			html+='	</tr>';
			break;
		}
	html+='</table>';
	item.innerHTML=html;
	var group=document.createElement("INPUT");
	group.type="hidden";
	group.name="item["+i+"][gfid]";
	if (typeof(parent)!="undefined") group.value=parent;
	else group.value="";
	item.appendChild(group);
	item.group=group;
	if (typeof(parent)!="undefined") $("field_list_"+parent).appendChild(item);
	else $("field_list").appendChild(item);
	return i;
	}
	
function object_removeField(i) {
	if (!confirm("Are you sure you want to remove this field?")) return;
	$("item_"+i).parentNode.removeChild($("item_"+i));
	}
	
function object_addFieldOption(i) {
	var n=$("item_"+i).getElementsByTagName("DIV").length;
	var item=document.createElement("DIV");
	item.className="field_list_option";
	item.id="item_"+i+"_option_"+n;
	html='<table cellspacing="0" style="width: 100%;">';
	html+='	<tr>';
	html+='		<td rowspan="2" style="vertical-align: middle; width: 40px;">';
	html+='			<div class="field_close_button" title="remove" onClick="object_removeFieldOption('+i+', '+n+');">x</div>';
	html+='		</td>';
	html+='		<td style="width: 40px;">text:</td>';
	html+='		<td style="padding-right: 5px;"><input id="item_'+i+'_option_'+n+'_text" name="item['+i+'][vars][options]['+n+'][text]" maxlength=255 style="width: 100%;"/></td>';
	html+='	</tr>';
	html+='	<tr>';
	html+='		<td>value:</td>';
	html+='		<td style="padding-right: 5px;"><input id="item_'+i+'_option_'+n+'_value" name="item['+i+'][vars][options]['+n+'][value]" maxlength=255 style="width: 100%;"/></td>';
	html+='	</tr>';
	html+='</table>';
	item.innerHTML=html;
	$("item_"+i+"_options").appendChild(item);
	return n;
	}
	
function object_removeFieldOption(i,n) {
	$("item_"+i+"_options").removeChild($("item_"+i+"_option_"+n));
	}
	
function object_load_type(tid) {
	try {
	if (typeof(tid)=="string") tid={0:tid};
	var types={};
	var request=[];
	for (var n in tid) {
		if (type_cache[parseInt(tid[n])]) types[tid[n]]=type_cache[parseInt(tid[n])];
		else request.push(tid[n]);
		}
	if (request.length>0) {
		var temp=ajax_get("admin/type/info/"+request.join(","));
		for (var n in temp) {
			type_cache[parseInt(n)]=temp[n];
			types[n]=temp[n];
			}
		}
	}
	catch (e) {alert("type_load: "+e)}
	return types;
	}
	
function object_load_object(oid) {
	if (typeof(oid)=="string") oid={0:oid};
	var objects={};
	var request=[];
	for (var n in oid) {
		if (object_cache[oid[n]]) objects[oid[n]]=object_cache[oid[n]];
		else request[request.length]=oid[n];
		}
	if (request.length>0) {
		var temp=ajax_get("admin/object/info/"+request.join(","));
		for (var n in temp) {
			object_cache[n]=temp[n];
			objects[n]=temp[n];
			}
		}
	return objects;
	}
	
function object_expandObject(i) {
	$('item_'+i+'_closed').style.display="none";
	$('item_'+i+'_open').style.display="table";
	var oid=$('item_'+i+'_oid').value;
	if (typeof(expanded[oid])!="undefined") {
		var pos=obj_pos($('item_'+expanded[oid]));
		window.scrollTo(0,pos.y);
		return;
		}
	else expanded[oid]=i;
	var tid=object_load_object(oid)[oid].tid;
	var type=object_load_type(object_load_type(tid)[tid].children);
	var children=object_load_object(object_load_object(oid)[oid].children);
	for (var o in children) {
		var tid=children[o].tid;
		var child_i=object_addObject(children[o].tid, object_load_type(tid)[tid].title, i, true);
		for (var f in children[o].vals) {
			if (typeof(children[o].vals[f])=="string") var title=children[o].vals[f];
			else var title=children[o].vals[f].name;
			title=title.length==0?"untitled":(title.length>20?title.substring(0,20):title)
			break;
			}
		object_fill_object_fields(child_i, object_load_type(tid)[tid], children[o]);
		$('item_'+child_i+'_closed_title').innerHTML=htmlentities(title);
		}
	}
	
function object_addObject(type, title, parent, hidden) {
	var type_info=object_load_type(type)[type];
	i=0;
	while ($("item_"+i+"_type")) i++;
	var item=document.createElement("DIV");
	item.className="field_list_item";
	item.id="item_"+i;
	item.draggable=true;
	item.type=type_info;
	if (typeof(parent)!="undefined") item.item_parent=$("item_"+parent);
	item.end_dragging=function (evt) {
		var div=this.parentNode;
		while (div.tagName!="DIV") div=div.parentNode;
		var parent=this.parentNode;
		while (typeof(parent.end_dragging)=="undefined" && parent.parentNode) parent=parent.parentNode;
		if (typeof(parent.oid)!="undefined") {
			var pos=0;
			var move_orders=[];
			var items=div.getElementsByTagName("DIV");
			for (var n in items) {
				if (typeof(items[n].oid)!="undefined" && (items[n].parent==parent.oid || items[n]==this)) {
					move_orders.push({"oid_start":items[n].parent,"oid_end":parent.oid,"oid":items[n].oid,"pos":pos});
					pos++;
					}
				}
			if (parent.oid!=this.parent) {
				pos=0;
				items=this.item_parent.getElementsByTagName("DIV");
				for (var n in items) {
					if (typeof(items[n].oid)!="undefined" && items[n].parent==this.parent && items[n]!=this) {
						move_orders.push({"oid_start":items[n].parent,"oid_end":items[n].parent,"oid":items[n].oid,"pos":pos});
						pos++;
						}
					}
				this.parent=parent.oid;
				this.item_parent=parent;
				}
				ajax_send_a("admin/object/save",{"action":"move","orders":move_orders});
			}
		};
	item.approve_destination=function (obj,type) {
		if (type=="field") {
			if (typeof(obj.id)!="undefined" && obj.id=="remove") return true;
			if (typeof(obj.item_parent)=="undefined") return false;
			obj=obj.item_parent;
			}
		else {
			while (typeof(obj.approve_destination)=="undefined" && obj.parentNode) obj=obj.parentNode;
			}
		if (typeof(obj.type)=="undefined") return false;
		for (var n in obj.type["children"]) if (obj.type["children"][n]==this.type.tid) return true;
		return false;
		}
	try {item.attachEvent('onmousedown',object_md);} catch(e) {}
	try {item.addEventListener('mousedown',object_md,false);} catch(e){}
	html='<form id="item_'+i+'_form" onsubmit="object_save('+i+')" action="admin/object/save" method="post" enctype="multipart/form-data" target="item_'+i+'_pipe">';
	html+='	<input type="hidden" id="item_'+i+'_type"  name="tid" value="'+type+'" />';
	html+='	<input type="hidden" id="item_'+i+'_oid"  name="oid" />';
  html+=' <input type="hidden" name="i" value="' + i + '" />';
	if (hidden) {
		html+='<table id="item_'+i+'_closed" cellspacing="0" style="width: 100%;">';
		html+='	<tr>';                                                                                                                               
		html+='		<td style="vertical-align: middle; width: 40px;">';
		html+='			<div class="field_close_button" title="expand" onClick="object_expandObject('+i+');">+</div>';
		html+='		</td>';
		html+='		<td id="item_'+i+'_closed_title" style="vertical-align: middle;"></td>';
		html+='	</tr>';
		html+='</table>';
		}
	html+='<table id="item_'+i+'_open" cellspacing="0" style="width: 100%;';
	if (hidden) html+=' display: none;';
	html+='">';
	html+='	<tr>';
	html+='		<td rowspan="1" style="vertical-align: middle; width: 40px;">';
	html+='			<div class="field_close_button" title="remove" onClick="object_removeObject('+i+');">x</div>';
	html+='		</td>';
	html+='		<td colspan="2" class="field_title">'+title+'</td>';
	html+='	</tr>';
	var group=[];
	var f=0;
	for (var n in type_info.fields) {
		var field=type_info.fields[n];
		while (group.length>0 && field.gfid!=group[0]) {
			html+='		</table>';
			html+='	</td>';
			html+='</tr>';
			group.shift();
			}
		html+='	<tr>';
		html+='		<td></td>';
		html+='		<td style="width: 100px;">'+field.title+':</td>';
		if (field.type=="group") html+='		<td>';
		else html+='		<td style="padding-right: 5px;">';
		switch (field.type) {
			case "select":
				var n=0;
				switch (field.vars.type) {
					case "select":
						html+='<select id="item_'+i+'_'+field.fid+'" name="item['+field.fid+']" onchange="object_activate_save('+i+');" style="width: 400px;">';
						for (var o in field.vars.options) {
							html+='<option id="item_'+i+'_'+field.fid+'_'+n+'" value="'+field.vars.options[o].value+'">'+field.vars.options[o].text+'</option>';
							n++;
							}
						html+='</select>';
						break;
					case "radio":
						for (var o in field.vars.options) {
							html+='<div>';
							html+='	<input type="radio" id="item_'+i+'_'+field.fid+'_'+n+'" name="item['+field.fid+']" onchange="object_activate_save('+i+');" value="'+field.vars.options[o].value+'" /> '+field.vars.options[o].text;
							html+='</div>';
							n++;
							}
						break;
					}
				break;
			case "text":
				html+='<input type="text" id="item_'+i+'_'+field.fid+'" name="item['+field.fid+']" onkeyup="object_activate_save('+i+');" style="width: 100%; margin-right: 10px;" />';
				if (field.vars.example!='') html+='<div class="object_example">'+field.vars.example+'</div>';
				break;
			case "textarea":
				if (!field.vars.size) field.vars.size=10;
				html+='<textarea id="item_'+i+'_'+field.fid+'" name="item['+field.fid+']" onkeyup="object_activate_save('+i+');" rows='+field.vars.size+' style="width: 100%;" /></textarea>';
				html+='<div id="item_' + i + '_' + field.fid + '_seo" style="padding-bottom: 5px;"><a href="#" onclick="seo_check(' + i + ', ' + field.fid + '); return false;">identify keywords</a></div>';
        if (field.vars.rtf) rtf("item_"+i+"_"+field.fid,field.vars);
				if (field.vars.example!='') html+='<div class="object_example">'+field.vars.example+'</div>';
				break;
			case "file":
				html+='<input type="file" id="item_'+i+'_'+field.fid+'" name="item['+field.fid+']" onchange="object_activate_save('+i+');" />';
				if (field.vars.example!='') html+='<div class="object_example">'+field.vars.example+'</div>';
				html+='<div id="item_'+i+'_'+field.fid+'_preview"></div>';
				
				break;
			case "date":
				html+='<input type="text" id="item_'+i+'_'+field.fid+'_mm" name="item['+field.fid+'][mm]" onkeyup="object_activate_save('+i+');" style="width: 40px;" />';
				if (field.vars.format=="mmdd" || field.vars.format=="mmddyyyy") html+=' / <input type="text" id="item_'+i+'_'+field.fid+'_dd" name="item['+field.fid+'][dd]" onkeyup="object_activate_save('+i+');" style="width: 40px;" />';
				if (field.vars.format=="mmyyyy" || field.vars.format=="mmddyyyy") html+=' / <input type="text" id="item_'+i+'_'+field.fid+'_yyyy" name="item['+field.fid+'][yyyy]" onkeyup="object_activate_save('+i+');" style="width: 40px;" />';
				break;
			case "number":
				html+='<input type="text" id="item_'+i+'_'+field.fid+'" name="item['+field.fid+']" onchange="object_activate_save('+i+');" style="width: 100%;" />';
				if (field.vars.example!='') html+='<div class="object_example">'+field.vars.example+'</div>';
				break;
			case "checkbox":
				html+='<input type="checkbox" id="item_'+i+'_'+field.fid+'" name="item['+field.fid+']" onchange="object_activate_save('+i+');" />';
				break;
			case "reference":
				html+='<select id="item_'+i+'_object" name="item['+field.fid+']" onchange="object_activate_save('+i+');" style="width: 100%;" />';
				var options=ajax_send("admin/object/save/",{"action":"get_object_list","type":field.vars.type});
				for (var n in options) html+='<option id="item_'+i+'_'+options[n].oid+'" value="'+options[n].oid+'">'+options[n].title+'</options>';
				html+='</select>';
				break;
			case "group":
				group.unshift(field.fid);
				html+='<table cellspacing="0" style="width: 100%;">';
				break;
			}
		if (field.type!="group") {
			html+='		</td>';
			html+='	</tr>';
			}
		}
	while (group.length>0) {
		html+='		</table>';
		html+='	</td>';
		html+='</tr>';
		group.shift();
		}
	html+='	<tr>';
	html+='		<td></td>';
	html+='		<td colspan="2">';
	html+='			<div class="field_list_item" style="margin: 10px 0 0 0;">';;
	html+='				<table cellspacing="0" style="width: 100%;">';
	html+='					<tr>';
	html+='						<td rowspan="2"></td>';
	var children=type_info.children
	tids=[];
	for (var n in children) tids[tids.length]=children[n];
	if (tids.length>0) {
		children=object_load_type(tids);
		html+='						<td colspan="2" style="height: 20px;">';
		html+='							<div id="item_'+i+'_child_select" style="display: none;">';
		html+='								<span style="padding: 3px 10px 0 0;">add new child:</span>';
		html+='								<select onChange="object_addNewObject(this.options[this.selectedIndex].value, this.options[this.selectedIndex].innerHTML, '+i+'); this.selectedIndex=0;">';
		html+='									<option>select</option>';
    var child_types = [];
    for (var n in children) if (children[n].title) child_types[n] = children[n].title + '|' + n;
    for (var t in child_types.sort()) {
      var n = child_types[t].split('|').pop();
      html+='<option value="'+children[n].tid+'">'+children[n].title+'</option>';
    }
		html+='								</select>';
		html+='							</div>';
		html+='						</td>';
		html+='						<td>';
		}
	else html+='					<td style="height: 20px;" colspan="3">';
	html+='							<input style="float: right;" id="save_'+i+'" type="submit" name="action" value="save" />';
	html+='							</form>';
	html+='							<iframe style="display: none;" id="item_'+i+'_pipe" name="item_'+i+'_pipe"></iframe>';
	html+='						</td>';
	html+='					</tr>';
	html+='				</table>';
	html+='			</div>';
	html+='		</td>';
	html+='	</tr>';
	html+='	<tr>';
	html+='		<td></td>';
	html+='		<td colspan="2">';
	html+='			<div id="field_list_'+i+'" class="field_list"></div>';
	html+='		</td>';
	html+='	</tr>';
	html+='</table>';
	item.innerHTML=html;
	var group=document.createElement("INPUT");
	group.type="hidden";
	group.name="gfid";
	if (typeof(parent)!="undefined") group.value=parent;
	else group.value="";
	item.appendChild(group);
	item.group=group;
	if (typeof(parent)!="undefined") $("field_list_"+parent).appendChild(item);
	else $("field_list").appendChild(item);
	return i;
	}
	
function object_addNewObject(type, title, parent) {
	var oid=ajax_send("admin/object/save",{"action":"getnewoid","tid":type});
	var i=object_addObject(type, title, parent);
	$("item_"+i+"_oid").value=oid;
	$("item_"+i).oid=oid;
	$('item_'+i).parent=$('item_'+i).item_parent.oid;
	ajax_send_a("admin/object/save",{"action":"setparents","oid":oid,"parents":{0:$('item_'+i).parent}});
	}
	
function object_removeObject(i) {
	if (!confirm("Are you sure you want to remove this object? This action cannot be undone.")) return;
	ajax_send("admin/object/save",{"action":"delete","oid":$("item_"+i).oid});
	$("item_"+i).parentNode.removeChild($("item_"+i));
	if (i==0) top.location.href='http://'+base_url+'/admin/object';
	}
	
function object_update_parents() {
	var parents=$("object_parent");
	var parent_list=[];
	for (var n in parents.options) if (parents.options[n].selected==true) parent_list.push(parents.options[n].value);
	if ($("item_0_oid").value=="") $("save_0").click();
	ajax_send_a("admin/object/save",{"action":"setparents","oid":$("item_0_oid").value,"parents":parent_list});
	}
	
function object_activate_save(i) {
	$("save_"+i).style.display="block";
	}
	
function object_save(i) {
	if ($("item_"+i+"_oid").value=="") {
		var oid=ajax_send("admin/object/save",{"action":"getnewoid","tid":$("item_"+i+"_type").value});
		$("item_"+i+"_oid").value=oid;
		$("item_"+i).oid=oid;
		}
	if ($("item_"+i+"_child_select")) $("item_"+i+"_child_select").style.display="inline";
	$("save_"+i).style.display="none";
	}
	
function object_mm(evt) {
	try {
		var evt=get_event(evt);
		var pos=mouse_pos(evt);
		if (!object.dragging) return false;
		if (evt.target.replace) evt.target.style.width=(evt.target.replace.scrollWidth-35)+"px";
		object.dragging.style.top=(pos.y-object.moy)+"px";
		object.dragging.focus();
		}
	catch (e) {
		alert("move: "+e);
		}
	}
	
function object_mi(evt) {
	try {
		evt=get_event(evt);
		if (!object.dragging) return false;
		if (!evt.target.draggable) return false;
		evt.target.style.borderTop="11px solid #333399";
		}
	catch (e) {
		alert("in: "+e);
		}
	}
	
function object_mo(evt) {
	try {
		evt=get_event(evt);
		if (!object.dragging) return false;
		if (!evt.target.draggable) return false;
		evt.target.style.borderTop="none";
		}
	catch (e) {
		alert("out: "+e);
		}
	}
	
function object_md(evt) {
	try {
		if (object.dragging) return;
		var evt=get_event(evt);
		var pos=mouse_pos(evt);
		if (evt.target.tagName=="INPUT" || evt.target.tagName=="SELECT" || evt.target.tagName=="OPTION" || evt.target.tagName=="TEXTAREA") return false;
		var target=evt.target
		while (target.tagName!="DIV") target=target.parentNode
		if (!target.draggable) return false;
		var opos=obj_pos(target);
		object={};
		object.moy=pos.y-opos.y;
		object.item=target;
		object.dragging=object.item.cloneNode(true);
		object.item.style.visibility="hidden";
		object.dragging.className="field_dragging";
		object.dragging.style.left=(opos.x+1)+"px";
		object.dragging.style.top=(opos.y+1)+"px";
		object.dragging.style.width=(object.item.scrollWidth-12)+"px";
		document.body.appendChild(object.dragging);
		
		var field_lists=document.getElementsByTagName("DIV");
		temporary_objects=[];
		field_lists[field_lists.length]=$("field_list");
		for (var n in field_lists) try { 
			if (field_lists[n].id && field_lists[n].id.substring(0,10)=="field_list") {
				if (!object.item.approve_destination || object.item.approve_destination(field_lists[n],"list")) {
					temp_seperator=document.createElement("DIV");
					temp_seperator.id="remove"
					temp_seperator.draggable=true;
					temp_seperator.className="temp_seperator";
					if (field_lists[n].lastChild) {
						var pos=obj_pos(field_lists[n].lastChild);
						temp_seperator.style.top=(pos.y+field_lists[n].lastChild.scrollHeight-10)+"px";
						}
					
					temporary_objects.push(field_lists[n].appendChild(temp_seperator));
					}
				}
			}
		catch (e) {}
		
		var fields=$("field_list").getElementsByTagName("DIV");
		for (var n in fields) try {
			if (fields[n].draggable) {
				field=fields[n];
				var add_seperator=true;
				while (field.parentNode) {
					field=field.parentNode;
					if (field==object.item) add_seperator=false;
					}
				if (add_seperator && object.item.approve_destination) add_seperator=add_seperator && object.item.approve_destination(fields[n],"field");
				if (add_seperator) {
					var opos=obj_pos(fields[n]);
					var seperator=fields[n].cloneNode(false);
					seperator.id="remove";
					seperator.replace=fields[n];
					seperator.draggable=true;
					seperator.className="field_list_seperator";
					seperator.style.top=(opos.y-9)+"px";
					seperator.style.left=(opos.x+10)+"px";
					seperator.style.height=Math.min(fields[n].scrollHeight,100)+"px";
					seperator.style.width=(fields[n].scrollWidth-15)+"px";
					seperator.style.visible=true;
					try {seperator.attachEvent('onmouseover',object_mi);} catch(e) {}
					try {seperator.addEventListener('mouseover',object_mi,false);} catch(e){}
					try {seperator.attachEvent('onmouseout',object_mo);} catch(e) {}
					try {seperator.addEventListener('mouseout',object_mo,false);} catch(e){}
					temporary_objects[temporary_objects.length]=document.body.appendChild(seperator);
					}
				}
			}
		catch (e) {}
		
		if (object.item.start_dragging) object.item.start_dragging(evt);
		}
	catch (e) {
		alert("down: "+e);
		}
	}
	
function object_mu(evt) {
	try {
		if (!object.dragging) return;
		evt=get_event(evt);
		if (evt.target.draggable) {
			evt.target.replace.parentNode.insertBefore(object.item, evt.target.replace);
			var item=object.item;
			while (!item.id || item.id.substring(0,10)!="field_list") item=item.parentNode;
			if (item.id=="field_list") var goup="";
			else var group=item.id.substring(11,item.id.length);
			object.item.group.value=group;
			}
		object.item.style.visibility="inherit";
		object.dragging.parentNode.removeChild(object.dragging);
		divs=temporary_objects;
		for (var n in divs) if (divs[n]) divs[n].parentNode.removeChild(divs[n]);
		if (object.item.end_dragging) object.item.end_dragging(evt);
		object.dragging=null;
		}
	catch (e) {
		alert("up: "+e);
		}
	}
	
function object_list_expand(button,oid,expanded) {
	if (button.innerHTML=="+") {
		if (button.parentNode.nextSibling && button.parentNode.nextSibling.oid) {
			button.parentNode.nextSibling.style.display="block";
			}
		else {
			var list=d.ce("DIV",{"oid":oid,"className":"object_list",innerHTML:ajax_get("admin/object/object_list/"+oid)});
			button.parentNode.parentNode.insertBefore(list,button.parentNode.nextSibling);
			}
		button.innerHTML="-";
		button.title="Collapse";
		button.style.paddingLeft="2px";
		button.style.paddingRight="7px";
		}
	else {
		button.parentNode.nextSibling.style.display="none";
		button.innerHTML="+";
		button.title="Expand";
		button.style.paddingLeft="0px";
		button.style.paddingRight="5px";
		}
	}
  
function seo_check(i, fid) {
  var text = $('item_' + i + '_' + fid).value;
  ajax_send_a("admin/object/save",{"action":"seo","string":text}, function(ajax) {seo_update(i, fid, ajax);});
}

function seo_update(i, fid, ajax) {
  var html = ajax.responseText;
  html+=' <a href="#" onclick="seo_check(' + i + ', ' + fid + '); return false;">re-check</a>';
  $('item_' + i + '_' + fid + '_seo').innerHTML = html;
}

rtf_queue=[];

function rtf(id,settings,token) {
	if (typeof(token)!="undefined") {
		var id=rtf_queue[token].id;
		var settings=rtf_queue[token].settings;
		}
	if (!$("rtf_"+id)) {
		if ($(id)) {
			rtewin=d.ce("IFRAME",{"id":"rtf_"+id,"frameBorder":0,"framePadding":0,"src":'about:blank','test':url_scheme+"://"+physical_base_url+"/files/rte/blank.htm"});
			rtewin.style.height=($(id).rows*13+2)+"px";
			rtewin.style.width="100%";
			$(id).style.display="none";
			var container=document.createElement("DIV");
			container.style.backgroundColor="#fff";
			container.style.margin="1px";
			container.style.border="1px solid #999";
			var content='';
			if (settings["bold"]=="on") content+='<img id="bold" class="rteImage" src="'+url_scheme+'://'+physical_base_url+'/files/rte/bold.gif" alt="Bold" title="Bold" onClick="rtf_command(\''+id+'\', \'bold\', \'\')">';
			if (settings["italic"]=="on") content+='<img class="rteImage" src="'+url_scheme+'://'+physical_base_url+'/files/rte/italic.gif" alt="Italic" title="Italic" onClick="rtf_command(\''+id+'\', \'italic\', \'\')">';
			if (settings["underlined"]=="on") content+='<img class="rteImage" src="'+url_scheme+'://'+physical_base_url+'/files/rte/underline.gif" alt="Underline" title="Underline" onClick="rtf_command(\''+id+'\', \'underline\', \'\')">';
			if (settings["left"]=="on") content+='<img class="rteImage" src="'+url_scheme+'://'+physical_base_url+'/files/rte/left_just.gif" alt="Align Left" title="Align Left" onClick="rtf_command(\''+id+'\', \'justifyleft\', \'\')">';
			if (settings["center"]=="on") content+='<img class="rteImage" src="'+url_scheme+'://'+physical_base_url+'/files/rte/centre.gif" alt="Center" title="Center" onClick="rtf_command(\''+id+'\', \'justifycenter\', \'\')">';
			if (settings["right"]=="on") content+='<img class="rteImage" src="'+url_scheme+'://'+physical_base_url+'/files/rte/right_just.gif" alt="Align Right" title="Align Right" onClick="rtf_command(\''+id+'\', \'justifyright\', \'\')">';
			if (settings["justify"]=="on") content+='<img class="rteImage" src="'+url_scheme+'://'+physical_base_url+'/files/rte/justifyfull.gif" alt="Justify Full" title="Justify Full" onclick="rtf_command(\''+id+'\', \'justifyfull\', \'\')">';
			if (settings["hr"]=="on") content+='<img class="rteImage" src="'+url_scheme+'://'+physical_base_url+'/files/rte/hr.gif" alt="Horizontal Rule" title="Horizontal Rule" onClick="rtf_command(\''+id+'\', \'inserthorizontalrule\', \'\')">';
			if (settings["ol"]=="on") content+='<img class="rteImage" src="'+url_scheme+'://'+physical_base_url+'/files/rte/numbered_list.gif" alt="Ordered List" title="Ordered List" onClick="rtf_command(\''+id+'\', \'insertorderedlist\', \'\')">';
			if (settings["ul"]=="on") content+='<img class="rteImage" src="'+url_scheme+'://'+physical_base_url+'/files/rte/list.gif" alt="Unordered List" title="Unordered List" onClick="rtf_command(\''+id+'\', \'insertunorderedlist\', \'\')">';
			if (settings["dent"]=="on") {
				content+='<img class="rteImage" src="'+url_scheme+'://'+physical_base_url+'/files/rte/outdent.gif" alt="Outdent" title="Outdent" onClick="rtf_command(\''+id+'\', \'outdent\', \'\')">';
				content+='<img class="rteImage" src="'+url_scheme+'://'+physical_base_url+'/files/rte/indent.gif" alt="Indent" title="Indent" onClick="rtf_command(\''+id+'\', \'indent\', \'\')">';
				}
			if (settings["link"]=="on") content+='<img class="rteImage" src="'+url_scheme+'://'+physical_base_url+'/files/rte/hyperlink.gif" alt="Insert Link" title="Insert Link" onClick="rtf_insert_link(\''+id+'\')">';
			if (settings["image"]=="on") content+='<img class="rteImage" src="'+url_scheme+'://'+physical_base_url+'/files/rte/image.gif" alt="Add Image" title="Add Image" onClick="addImage(\''+id+'\')">';
			content+='<img class="rteImage" src="'+url_scheme+'://'+physical_base_url+'/files/rte/cut.gif" alt="Cut" title="Cut" onClick="rtf_command(\''+id+'\', \'cut\')">';
			content+='<img class="rteImage" src="'+url_scheme+'://'+physical_base_url+'/files/rte/copy.gif" alt="Copy" title="Copy" onClick="rtf_command(\''+id+'\', \'copy\')">';
			content+='<img class="rteImage" src="'+url_scheme+'://'+physical_base_url+'/files/rte/paste.gif" alt="Paste" title="Paste" onClick="rtf_command(\''+id+'\', \'paste\')">';
			content+='<img class="rteImage" src="'+url_scheme+'://'+physical_base_url+'/files/rte/undo.gif" alt="Undo" title="Undo" onClick="rtf_command(\''+id+'\', \'undo\')">';
			content+='<img class="rteImage" src="'+url_scheme+'://'+physical_base_url+'/files/rte/redo.gif" alt="Redo" title="Redo" onClick="rtf_command(\''+id+'\', \'redo\')">';
			content+='<br class="clear" />';
			content='<div id="rte_buttons_'+id+'_content">'+content+'</div>';
			var buttons=d.ce("DIV",{"className":"rte_buttons","cellspacing":2,"cellpadding":0,"id":"rte_buttons_"+id,"innerHTML":content});
			container.appendChild(buttons);
			container.appendChild(rtewin);
			content='<input type="checkbox" id="rtf_src_'+id+'" onclick="rtf_toggle_src(\''+id+'\');" /><label for="src_'+id+'">View Source</label>'
			content+='<br class="clear" />';
			var footer=d.ce("DIV",{"className":"rtf_footer","innerHTML":content});
			container.appendChild(footer);
			$(id).parentNode.insertBefore(container,$(id));
			if (isIE) container.style.marginRight="10px";
			}
		else if (typeof(token)=="undefined") {
			var token=rtf_queue.length;
			rtf_queue.push({"id":id,"settings":settings});
			setTimeout("rtf(null,null,"+token+")",500);
			}
		}
	if ($("rtf_"+id)) {
		if (!rtewin) var rtewin=$("rtf_"+id);
		var html='<?xml version="1.0" encoding="iso-8859-1"?>';
		html+='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">';
		html+='<html xmlns="http://www.w3.org/1999/xhtml">';
		html+='	<head>';
		html+='		<style type="text/css">';
		html+='			html, body {';
		html+='				font: 11px verdana;';
		html+='				}';
		html+='		</style>';
		html+='	</head>';
		html+='	<body id="'+id+'">';
		html+=$(id).value;
		html+='</body>';
		html+='</html>';
		if (isIE) {
			rtewin.style.marginRight="12px";
			container.style.marginRight="10px";
			rtedoc=rtewin.contentWindow.document;
			rtedoc.open();
			rtedoc.write(html);
			rtedoc.close();
			alert("this page is currently unavailable in IE");
			rtedoc.attachEvent('onkeyup',rtf_update);
			rtewin.contentWindow.document.designMode="On";
			}
		else {
			try {
			rtewin.contentDocument.designMode="on";
			rtedoc=rtewin.contentWindow.document;
			rtedoc.open();
			rtedoc.write(html);
			rtedoc.close();
			rtedoc.addEventListener('keyup',rtf_update,false);
			}
			catch (e) {
				setTimeout("rtf(null,null,"+token+")",500);
				}
			}
		return;
		}
	}
	
function rtf_toggle_src(id) {	
	if ($("rtf_src_"+id).checked) {
		$("rte_buttons_"+id+"_content").style.visibility="hidden";
		var rtf=$('rtf_'+id).contentWindow.document;
		var src=rtf.createTextNode(get_xhtml(rtf.body,"en","iso-8859-1"));
		rtf.body.innerHTML="";
		rtf.body.appendChild(src);
		rtf.body.innerHTML=rtf.body.innerHTML.replace(/\n/g,"<br />");
		}
	else {
		$("rte_buttons_"+id+"_content").style.visibility="visible";
		var rtf=$('rtf_'+id).contentWindow.document;
		var src=rtf.body.ownerDocument.createRange();
		src.selectNodeContents(rtf.body);
		rtf.body.innerHTML=src.toString();
		$(id).value=get_xhtml(rtf.body);
	}
}
	
function rtf_command(id, command, option) {
	var rtf=$('rtf_'+id).contentWindow;
	try {
		rtf.focus();
	  	rtf.document.execCommand(command, false, option);
		rtf.focus();
		}
	catch (e) {alert(e)}
	rtf_update('rtf_'+id);
	}
	
function rtf_update(input) {
	try {
		if (typeof(input)=="string") {
			rtewin=$(input);
			if (isIE) var rtedoc=rtewin.contentWindow.document;
			else var rtedoc=rtewin.contentWindow.document;
			rtebody=rtedoc.body;
			var id=input.substring(4,input.length);
			}
		else {
			var evt=get_event(input);
			var id=evt.target.childNodes[1].id;
			var rtebody=evt.target.childNodes[1];
			}
		$(id).value=get_xhtml(rtebody,"en","iso-8859-1");
		$(id).onkeyup();
		}
	catch (e) {alert(e)}
	}
	
function rtf_insert_link(id) {
	var url=prompt("Enter a URL:", "");
	if (url=="null" || url=="") return;
	rtf_command(id,"Unlink");
	rtf_command(id,"CreateLink",url);
	rtf_update('rtf_'+id);
	}
	
function rtf_insert_image(id) {
	var url=prompt('Enter Image URL:', 'http://');
	if (url=="null" || url=="") return;
	rtf_command(id,'InsertImage', url);
	rtf_update(id);
	}
/*==============================================================================

                             HTML2XHTML Converter 1.0
                             ========================
                       Copyright (c) 2004 Vyacheslav Smolin


Author:
-------
Vyacheslav Smolin (http://www.richarea.com, http://html2xhtml.richarea.com,
re@richarea.com)

About the script:
-----------------
HTML2XHTML Converter (H2X) generates a well formed XHTML string from a HTML DOM
object.

Requirements:
-------------
H2X works in  MS IE 5.0 for Windows or above,  in Netscape 7.1,  Mozilla 1.3 or
above. It should work in all Mozilla based browsers.

Usage:
------
Please see description of function get_xhtml below.

Demo:
-----
http://html2xhtml.richarea.com/, http://www.richarea.com/demo/

License:
--------
Free for non-commercial using. Please contact author for commercial licenses.


==============================================================================*/


//add \n before opening tag
var need_nl_before = '|div|p|table|tbody|tr|td|th|title|head|body|script|comment|li|meta|h1|h2|h3|h4|h5|h6|hr|ul|ol|option|';
//add \n after opening tag
var need_nl_after = '|html|head|body|p|th|style|';

var re_comment = new RegExp();
re_comment.compile("^<!--(.*)-->$");

var re_hyphen = new RegExp();
re_hyphen.compile("-$");


// Convert inner text of node to xhtml
// Call: get_xhtml(node);
//       get_xhtml(node, lang, encoding) -- to convert whole page
// other parameters are for inner usage and should be omitted
// Parameters:
// node - dom node to convert
// lang - document lang (need it if whole page converted)
// encoding - document charset (need it if whole page converted)
// need_nl - if true, add \n before a tag if it is in list need_nl_before
// inside_pre - if true, do not change content, as it is inside a <pre>
function get_xhtml(node, lang, encoding, need_nl, inside_pre) {
	var i;
	var text = '';
	var children = node.childNodes;
	var child_length = children.length;
	var tag_name;
	var do_nl = need_nl ? true : false;
	var page_mode = true;
	
	for (i = 0; i < child_length; i++) {
		var child = children[i];
		
		switch (child.nodeType) {
			case 1: { //ELEMENT_NODE
				var tag_name = String(child.tagName).toLowerCase();
				
				if (tag_name == '') break;
				
				if (tag_name == 'meta') {
					var meta_name = String(child.name).toLowerCase();
					if (meta_name == 'generator') break;
				}
				
				if (!need_nl && tag_name == 'body') { //html fragment mode
					page_mode = false;
				}
				
				if (tag_name == '!') { //COMMENT_NODE in IE 5.0/5.5
					//get comment inner text
					var parts = re_comment.exec(child.text);
					
					if (parts) {
						//the last char of the comment text must not be a hyphen
						var inner_text = parts[1];
						text += fix_comment(inner_text);
					}
				} else {
					if (tag_name == 'html') {
						text = '<?xml version="1.0" encoding="'+encoding+'"?>\n<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n';
					}
					
					//inset \n to make code more neat
					if (need_nl_before.indexOf('|'+tag_name+'|') != -1) {
						if ((do_nl || text != '') && !inside_pre) text += '\n';
					} else {
						do_nl = true;
					}
					
					text += '<'+tag_name;
					
					//add attributes
					var attr = child.attributes;
					var attr_length = attr.length;
					var attr_value;
					
					var attr_lang = false;
					var attr_xml_lang = false;
					var attr_xmlns = false;
					
					var is_alt_attr = false;
					
					for (j = 0; j < attr_length; j++) {
						var attr_name = attr[j].nodeName.toLowerCase();
						
						if (!attr[j].specified && 
							(attr_name != 'selected' || !child.selected) && 
							(attr_name != 'style' || child.style.cssText == '') && 
							attr_name != 'value') continue; //IE 5.0
						
						if (attr_name == '_moz_dirty' || 
							attr_name == '_moz_resizing' || 
							tag_name == 'br' && 
							attr_name == 'type' && 
							child.getAttribute('type') == '_moz') continue;
						
						var valid_attr = true;
						
						switch (attr_name) {
							case "style":
								attr_value = child.style.cssText;
								break;
							case "class":
								attr_value = child.className;
								break;
							case "http-equiv":
								attr_value = child.httpEquiv;
								break;
							case "noshade": break; //this set of choices will extend
							case "checked": break;
							case "selected": break;
							case "multiple": break;
							case "nowrap": break;
							case "disabled": break;
								attr_value = attr_name;
								break;
							default:
								try {
									attr_value = child.getAttribute(attr_name, 2);
								} catch (e) {
									valid_attr = false;
								}
								break;
						}
						
						//html tag attribs
						if (attr_name == 'lang') {
							attr_lang = true;
							attr_value = lang;
						}
						if (attr_name == 'xml:lang') {
							attr_xml_lang = true;
							attr_value = lang;
						}
						if (attr_name == 'xmlns') attr_xmlns = true;
						if (valid_attr) {
							//value attribute set to "0" is not handled correctly in Mozilla
							if (!(tag_name == 'li' && attr_name == 'value')) {
								text += ' '+attr_name+'="'+fix_attribute(attr_value)+'"';
							}
						}
						
						if (attr_name == 'alt') is_alt_attr = true;
					}
					
					if (tag_name == 'img' && !is_alt_attr) {
						text += ' alt=""';
					}
					
					if (tag_name == 'html') {
						if (!attr_lang) text += ' lang="'+lang+'"';
						if (!attr_xml_lang) text += ' xml:lang="'+lang+'"';
						if (!attr_xmlns) text += ' xmlns="http://www.w3.org/1999/xhtml"';
					}
					
					if (child.canHaveChildren || child.hasChildNodes()){
						text += '>';
//						if (need_nl_after.indexOf('|'+tag_name+'|') != -1) {
//							text += '\n';
//						}
						text += get_xhtml(child, lang, encoding, true, inside_pre || tag_name == 'pre' ? true : false);
						text += '</'+tag_name+'>';
					} else {
						if (tag_name == 'style' || tag_name == 'title' || tag_name == 'script') {
							text += '>';
							var inner_text;
							if (tag_name == 'script') {
								inner_text = child.text;
							} else {
								inner_text = child.innerHTML;
							}
							
							if (tag_name == 'style') {
								inner_text = String(inner_text).replace(/[\n]+/g,'\n');
							}
							
							text += inner_text+'</'+tag_name+'>';
						} else {
							text += ' />';
						}
					}
				}
				break;
			}
			case 3: { //TEXT_NODE
				if (!inside_pre) { //do not change text inside <pre> tag
					if (child.nodeValue != '\n') {
						text += fix_text(child.nodeValue);
					}
				} else {
					text += child.nodeValue;
				}
				break;
			}
			case 8: { //COMMENT_NODE
				text += fix_comment(child.nodeValue);
				break;
			}
			default:
				break;
		}
	}
	
	if (!need_nl && !page_mode) { //delete head and body tags from html fragment
		text = text.replace(/<\/?head>[\n]*/gi, "");
		text = text.replace(/<head \/>[\n]*/gi, "");
		text = text.replace(/<\/?body>[\n]*/gi, "");
	}
	
	return text;
}

//fix inner text of a comment
function fix_comment(text) {
	//delete double hyphens from the comment text
	text = text.replace(/--/g, "__");
	
	if(re_hyphen.exec(text)) { //last char must not be a hyphen
		text += " ";
	}
	
	return "<!--"+text+"-->";
}

//fix content of a text node
function fix_text(text) {
	//convert <,> and & to the corresponding entities
	return String(text).replace(/\n{2,}/g, "\n").replace(/\&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/\u00A0/g, "&nbsp;");
}

//fix content of attributes href, src or background
function fix_attribute(text) {
	//convert <,>, & and " to the corresponding entities
	return String(text).replace(/\&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/\"/g, "&quot;");
}
