if(!window.$){
	window.$ = function(id,loc){
		if(!loc)loc = window;
		if(typeof id=='string')return loc.document.getElementById(id);
		return id;
	}
}

isGecko = (navigator.userAgent.toLowerCase().indexOf("gecko")!=-1);

function editor(f){
	this.f = f;
}
editor.prototype = {
ini:function(html){
	var html = "<html>\n<head><style type='text/css'>body {background: #ffffff;margin: 0px;padding: 8px;}\np {margin: 4px;}</style></head>\n<body>\n\n"+html+"\n\n</body>\n</html>";
	var _this = this,od = this.od();
	if(document.all){
		od.open();
		od.write(html);
		od.close();
		od.designMode = "On";
	}else{
		try{
			this.w().contentDocument.designMode = 'on';
			try{
				od.open();
				od.write(html);
				od.close();
			}catch(e){
				alert('Error preloading content.');
			}
		}catch(e){
			if(isGecko){
				setTimeout(function(){_this.ini(html);},10);
			}
		}
	}
},
write:function(c,v,t){
	c = c?c:'';
	var f = this.f,h = '';
	v = (v?v:f)+'.';
	h += '	<textarea name="e'+f+'" style="display: none;"></textarea>';
	h += '	<div class="editor">';
	h += '		<div class="bar">';
	h += '			<select onchange="'+v+'select(this,\'fontname\');"><option value="">[Fonte]</option>';
	h += '				<option value="Arial">Arial</option>';
	h += '				<option value="Courier">Courier</option>';
	h += '				<option value="Sans Serif">Sans Serif</option>';
	h += '				<option value="Tahoma">Tahoma</option>';
	h += '				<option value="Times New Roman">Times New Roman</option>';
	h += '				<option value="Trebuchet MS">Trebuchet MS</option>';
	h += '			</select>';
	h += '			<select onchange="'+v+'select(this,\'fontsize\');"><option value="">[Tam]</option>';
	h += '				<option value="1">1</option>';
	h += '				<option value="2">2</option>';
	h += '				<option value="3">3</option>';
	h += '				<option value="4">4</option>';
	h += '				<option value="5">5</option>';
	h += '				<option value="6">6</option>';
	h += '				<option value="7">7</option>';
	h += '			</select>';
	h += ' 		   <img src="'+c+'barv.gif"/>';
	h += ' 		   <img src="'+c+'neg.gif" onmouseover="'+v+'over(this);" onmouseout="'+v+'out(this);" onclick="'+v+'fmt(\'bold\');"/>';
	h += ' 		   <img src="'+c+'ita.gif" onmouseover="'+v+'over(this);" onmouseout="'+v+'out(this);" onclick="'+v+'fmt(\'italic\');"/>';
	h += '		    <img src="'+c+'sub.gif" onmouseover="'+v+'over(this);" onmouseout="'+v+'out(this);" onclick="'+v+'fmt(\'underline\');"/>';
	h += '		    <img src="'+c+'barv.gif"/>';
	h += '		    <img src="'+c+'lft.gif" onmouseover="'+v+'over(this);" onmouseout="'+v+'out(this);" onclick="'+v+'fmt(\'justifyleft\');"/>';
	h += '		    <img src="'+c+'cnt.gif" onmouseover="'+v+'over(this);" onmouseout="'+v+'out(this);" onclick="'+v+'fmt(\'justifycenter\');"/>';
	h += '		    <img src="'+c+'rgt.gif" onmouseover="'+v+'over(this);" onmouseout="'+v+'out(this);" onclick="'+v+'fmt(\'justifyright\');"/>';
	h += '		    <img src="'+c+'jus.gif" onmouseover="'+v+'over(this);" onmouseout="'+v+'out(this);" onclick="'+v+'fmt(\'justifyfull\');"/>';
	h += '		    <img src="'+c+'barv.gif"/>';
	h += '		    <img src="'+c+'lmc.gif" onmouseover="'+v+'over(this);" onmouseout="'+v+'out(this);" onclick="'+v+'fmt(\'insertunorderedlist\');"/>';
	h += '		    <img src="'+c+'lnm.gif" onmouseover="'+v+'over(this);" onmouseout="'+v+'out(this);" onclick="'+v+'fmt(\'insertorderedlist\');"/>';
	h += '		    <img src="'+c+'barv.gif"/>';
	h += '		    <select onchange="'+v+'select(this,\'forecolor\');"><option value="">[Cor]</option>';
	h += '				<option value="#ffffff">Branco</option>';
	h += '				<option value="#000000">Preto</option>';
	h += '				<option value="#000099">Azul</option>';
	h += '				<option value="#29B2FF">Azul claro</option>';
	h += '				<option value="#fe0002">Vermelho</option>';
	h += '				<option value="#044f0a">Verde</option>';
	h += '				<option value="#3d3d3d">Cinza</option>';
	h += '				<option value="#00ff00">Limão</option>';
	h += '				<option value="#ff6417">Laranja</option>';
	h += '				<option value="#dddd00">Amarelo</option>';
	h += '			</select>';
	h += '		</div>';
	h += '		<div class="fr"><iframe name="f'+f+'" id="f'+f+'" frameborder="0"></iframe></div>';
	h += '	</div>';
	if(!t)document.write(h);
	if(typeof t=='object')t.innerHTML = h;
	if(t===1)return h;
},
select:function(o,c){
	if(o.options[o.selectedIndex].value)this.fmt(c,o.options[o.selectedIndex].value);
	o.options[0].selected = true;
},
w:function(){
	var f = 'f'+this.f;
	if(document.all)return frames[f];
	return $.id(f);
},
o:function(){
	if(document.all)return this.w();
	return this.w().contentWindow;
},
od:function(){
	return this.o().document;
},
of:function(){
	this.o().focus();
	return this;
},
fmt:function(com,opt){
	var o = this.o(),od = this.od();
	if(document.all){
		var selection = od.selection; 
		if(selection!=null){
			rng = selection.createRange();
		}
	}else{
		var selection = o.getSelection();
		rng = selection.getRangeAt(selection.rangeCount-1).cloneRange();
	}
	try{
		this.of();
		od.execCommand(com,false,opt);
		this.of();
	}catch(e){
		alert(e);
	}
},
get:function(){
	return trim(this.od().body.innerHTML);
},
over:function(o){
	o.className = 'over';
},
out:function(o){
	o.className = '';
}
};

function trim(v){
	return v
	.replace(/\r?\n/g,'')
	.replace(/\<style.*\<\/style\>/gi,'')
	.replace(/(\&nbsp\;|\t)+/gi,' ')
	.replace(/\s+/g,' ')
	.replace(/\s?\<p\>\s\<\/p\>\s?/gi,'<br/>')
	.replace(/^\s/g,'')
	.replace(/\s$/g,'')
	.replace(/^(\<br\/\>)+$/gi,'')
	.replace(/^(\s?\<br\/\>\s?)+/gi,'')
	.replace(/(\s?\<br\/\>\s?)+$/gi,'');
}

function validaForm(f){
	f = f.elements;
	for(var i=0,v;v=arguments[++i];){
		if(!(f['e'+v.f].value = v.get())){
			alert('Digite algo no campo "'+v.f+'".');
			v.of();
			return false;
		}
	}
	return true;
}

