//--------------------------------------------------------------------
	// filename: main.js

	//2009-03-09 - paulo: adicionei placeholder_id no acionarDev. Pego este parametro de um campo hidden com id = placeholder_id
	//este parametro serve para refinar o filtro das tags caso surja a necessidade de filtrar utilizando um id de uma categoria
	//16-02-2009 Paulo: Funcao validateAndSubmit - mudei o action do form e passo parametro de um campo hidden "validator".
	//16-02-2009 Paulo: Funcao validateAndSubmit - Apago div se ele ja existir para nÃ£o repetir o aviso.
	//16-02-2009 Paulo: Funcao triggeHappy - Insiro novas chamadas para distribuidor e plataforma
	//--------------------------------------------------------------------


function triggerHappy(){
	//Validar();
}


function addNew(new_value,campo,url_insert){
	if(confirm("Deseja mesmo adicionar o novo registro?")){
		new Request.HTML({
			url:url_insert,
			onComplete:function(response){
				$("lista"+campo).empty().adopt(response)
			}
		}).get({"id":0,"nome":new_value,"tipo":campo})
	}
	return false
}

function editInPlace(id,url_insert,campo){
	//criaÃ§Ã£o da Ã¡rea para modificaÃ§Ã£o de texto...
	new Element("span").empty().adopt(
		new Element("input",{
			"type":"text",
			"id":id,
			"value":$(id).get("html")
		}),
		new Element("input",{
			"value":"Modificar",
			"type":"button",
			"events":{
				//ainda nÃ£o arrisquei a jogar um submit, mas seria legal para o usuÃ¡rio poder dar enter de uma vez.
				"click":function(){
					new Request.HTML({
						url:url_insert,
						onComplete:function(html){
							//por enquanto simplesmentre colocando o valor passado, mas caso precise, pode ser substituÃ­do por algo voltando pelo response, como mensagens de erro, etc.
							var a=new Element("a", {
								"id":id,
								"href":""
							}).set("html",$(id).value)
							a.replaces($(id).parentNode).removeEvents("dblclick").addEvent("dblclick", function(){editInPlace(this.id,url_insert,campo)})
							$$("#lista"+campo+" a").removeEvents("click").addEvent("click", function(){$(campo).value=this.get("html");if($chk($("add"+campo)))$("add"+campo).destroy();return false})
						}
					}).get({"id":id.split(campo+"_")[1], "nome":$(id).value,"tipo":campo})
				}
			}
		})
	).replaces($(id))
	$(id).focus()
	return false
}

function acionarDev(campo,url_search,url_insert,editable){
	editable = typeof(editable) != 'undefined' ? editable : true;
	//2009-03-09 - paulo: adicionei placeholder_id pego
	placeholder_id = ($chk($("placeholder_id"))) ? $("placeholder_id").get("value") : 0;
	var D=$(campo)
	var L=new Element("div", {"id":"lista"+campo, "styles":{"padding":"0px"}});
	L.inject(D,"after")
	D.addEvent("keyup",function(){
		if (D.value!=""){
			new Request.HTML({
				url:url_search,
				onComplete:function(html){
					L.empty().appendText("Sugestões: ").adopt(html)
					if ($chk($("add"+campo))){
						$("add"+campo).addEvent("click",function(){
							addNew(D.value,campo,url_insert)
						})
					}
					if (editable){
						$$("#lista"+campo+" a").addEvent("dblclick", function(){
							id=this.id
							editInPlace(id,url_insert,campo)
						})
					}
					$$("#lista"+campo+" a").addEvent("click", function(){D.value=this.get("html");if($chk($("add"+campo)))$("add"+campo).destroy();return false})
				}
			//2009-03-09 - paulo: adicionei placeholder_id no get
			}).get({"placeholder_id":placeholder_id,"nome":D.value,"tipo":campo});
		} else {
			if (L.get("html")!=""){L.empty()}
		}
	})
}
	var show_debug = false;

	//====================================================================
	// Shortcuts

	function debug(text) {

		if(show_debug)
	 		$("debug").set("html", text + "<br />");

	} // function debug


	function go(url) {

		document.location.href = url;

	} // function go


	function addHiddenField(form, name, value) {

		var myValue=new Element("input", {
			"type":"hidden",
			"id":name,
			"name":name,
			"value":value
		})
		$(form).adopt(myValue)
		debug("adding hidden field " + name + " to " + form + " with value " + value);


	} // function addHiddenField