	//--------------------------------------------------------------------
	// filename: main.js
	//--------------------------------------------------------------------
	
	var show_debug = false;

	//====================================================================
	// Shortcuts
	
	function debug(text) {

		if(show_debug)
	 		$("debug").innerHTML += text + "<br />";
	
	} // function debug


	function go(url) {

		document.location.href = url;

	} // function go

	function confirmDelete(location) {
		
		debug("showing confirm");
		
		if(confirm("Voce realmente quer apagar este registro?")) {
			debug("user accepted, going to " + location);
			document.location.href = location;
		}
		else
			debug("user cancelled, quitting.");
			
	} // function confirmDelete
		
	//====================================================================
	// Form control

	function removeHiddenField(id, location) {

		$(form).removeChild($(name));
				
		debug("removing hidden field " + name + " from " + form);
	} // function addHiddenField

	
	function addHiddenField(form, name, value) {
		
		var myValue = document.createElement('input');
		myValue.setAttribute('type', "hidden");
		myValue.setAttribute('id', name);
		myValue.setAttribute('name', name);
		myValue.setAttribute('value', value);

		$(form).appendChild(myValue);
		
		debug("adding hidden field " + name + " to " + form + " with value " + value);
	} // function addHiddenField
	
	//====================================================================
	// Validation control
	
	/** Overriding function overriding :-)
	function validateAndSubmit(form_name, validator_name) {
		
		data = $(form_name).serialize();
		
		var options = {
			
			method: 'post',
			postBody: data,
			onComplete: function(response) {
				debug("parameters are: " + data);
				debug("this is the response: " + response.responseText);
				var result = response.responseText;
				var result_row = result.split("\n");
				debug(result_row.length + " items to show");

				if(result_row.length > 1) {
					$('warning').style.display = 'block';					
					errors = $('errors').childElements();
	
					for(i = 0; i < errors.length; i++) {
						$(errors[i].id).style.display = 'none';
						debug("hiding " + errors[i].id);
					} // for
				
					for(i = 0; i < result_row.length; i++) {
						$(result_row[i]).style.display = 'list-item';
						debug("showing " + result_row[i]);
					} // for

				} // if
				else {
					debug("i'll submit " + form_name);
					$(form_name).submit();
				}
			} // function anonymous
		};
		
		debug("calling " + validator_name);
		new Ajax.Request(validator_name, options);
		
	}
	
	*/
	
	function validateAndSubmit(form_name, validator_name) {
		//alert("dsadsa "+form_name);
		data = $(form_name).serialize();
		
		var options = {
			
			method: 'post',
			postBody: data,
			onComplete: function(response) {
				debug("parameters are: " + data);
				debug("this is the response: " + response.responseText);

				if(response.responseText.length > 1) {
					$('warning').style.display = 'block';
					debug('showing response');
					//23-06-2008 update from paulo campos
					$("warning").innerHTML = response.responseText;
					
					$('formLabelDropBox') = response.responseText;
	
				} // if
				else {
					debug("i'll submit " + form_name);

					// add check to prevent unvalidated submissions (against a script who wants
					// to override this check, for istance)
					addHiddenField(form_name, "validatorStamp", "1");
					$(form_name).submit();
				}
			} // function anonymous
		};
		
		debug("calling " + validator_name);
		new Ajax.Request(validator_name, options);
	
	}
	
	function controlFields (field) {
		
		if (field.value == 1) {
			
			$("razao_social").disabled = 1;
			$("razao_social").style.border = "none";
			$("razao_social").style.background = 'url(\'img/bkg_input_desativado.png\') no-repeat';
			$("cnpj").disabled = 1;
			$("cnpj").style.border = "none";
			$("cnpj").style.background = 'url(\'img/bkg_input_desativado.png\') no-repeat';
			$("cpf").disabled = 0;
			$("cpf").style.background = 'url(\'img/bkg_input.png\') no-repeat';
			
		}
		
		if (field.value == 2) {
			
			$("razao_social").disabled = 0;
			$("razao_social").style.background = 'url(\'img/bkg_input.png\') no-repeat';
			$("cnpj").disabled = 0;
			$("cnpj").style.background = 'url(\'img/bkg_input.png\') no-repeat';
			$("cpf").disabled = 1;
			$("cpf").style.border = "none";	
			$("cpf").style.background = 'url(\'img/bkg_input_desativado.png\') no-repeat';
			
		}
		
		if (field.value == 3) {
			
			$("razao_social").disabled = 1;
			$("razao_social").style.border = "none";
			$("cnpj").disabled = 1;
			$("cnpj").style.border = "none";
			$("cpf").disabled = 1;
			$("cpf").style.border = "none";			
			
		}
		
		if (field.value == 4) {
			
			$("razao_social").disabled = 1;
			$("razao_social").style.border = "none";
			$("cnpj").disabled = 1;
			$("cnpj").style.border = "none";
			$("cpf").disabled = 1;
			$("cpf").style.border = "none";			
			
		}	
		
	}