/* Made by Mathias Bynens <http://mathiasbynens.be/> */

var VetServicosInformacao = new Array();

//alert(navigator.appVersion);

function number_format(a, b, c, d) {
 a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
 e = a + '';
 f = e.split('.');
 if (!f[0]) {
  f[0] = '0';
 }
 if (!f[1]) {
  f[1] = '';
 }
 if (f[1].length < b) {
  g = f[1];
  for (i=f[1].length + 1; i <= b; i++) {
   g += '0';
  }
  f[1] = g;
 }
 if(d != '' && f[0].length > 3) {
  h = f[0];
  f[0] = '';
  for(j = 3; j < h.length; j+=3) {
   i = h.slice(h.length - j, h.length - j + 3);
   f[0] = d + i +  f[0] + '';
  }
  j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
  f[0] = j + f[0];
 }
 c = (b <= 0) ? '' : c;
 return f[0] + c + f[1];
}

function atualizaCarrinho()
{
	var __elemento = null;
	var __elementos = $('content_left').getElementsByTagName('input');
	var qtde_elementos = parseInt(__elementos.length);
	var valor_atual_carrinho = 0;
	for( i = 0; i < qtde_elementos; i++ )
	{
		__elemento = __elementos[i];
		if( ! __elemento.checked ) 
		{
			continue;	
		}
		valor_atual_carrinho += VetProdutosServicos[__elemento.getAttribute('cod')].valor;
	}

	var AJAX = new Ajax();
	AJAX.connect('?secao=sender&sub=calcula_orcamento','POST',serialize('form_conclui_orcamento'),'retorno_atualizaCarrinho');
}

function retorno_atualizaCarrinho(inner)
{
	$('calculo_investimento').innerHTML = inner;
}

function abreInformacaoServico(cod_servico)
{
	if( VetServicosInformacao[cod_servico] != undefined )
	{
		VetServicosInformacao[cod_servico] = VetServicosInformacao[cod_servico] ? false : true;
	}
	else
	{
		VetServicosInformacao[cod_servico] = $('tr_desc_' + cod_servico).style.display == "none" ? true : false;
	}

	if(VetServicosInformacao[cod_servico])
	{
		$('tr_desc_' + cod_servico).style.display = "block";		
		$('gif_' + cod_servico).src = "images/gif_minus.gif";
	}
	else
	{
		$('tr_desc_' + cod_servico).style.display = "none";
		$('gif_' + cod_servico).src = "images/gif_mais.gif";
	}
}

function conclui_orcamento()
{
	var flg_continue = false;
	var __elementos = $('content_left').getElementsByTagName('input');
	var qtde_elementos = parseInt(__elementos.length);
	var valor_atual_carrinho = 0;
	for( i = 0; i < qtde_elementos; i++ )
	{
		if( __elementos[i].checked ) 
		{
			flg_continue = true;
		}
	}


	if(flg_continue)
	{
		$('form_conclui_orcamento').submit();
	}
	else
	{
		alert('Selecione pelo menos um produto ou serviço para seu site!');
	}
}

function __valida_submit_once()
{
	$('mensagem').style.display = 'block';

	$('mensagem').innerHTML = __msg_save;

	var AJAX = new Ajax();
	var serialization = serialize($('dados'));
	//alert(serialization);
	AJAX.connect("?secao=sender&sub=enviaFormOrcamento",'POST',serialization,'return_enviaFormOrcamento');		
}

function return_enviaFormOrcamento(inner)
{
	try
	{
		eval(inner);
	}
	catch(e)
	{
		alert(e);
	}								

	if(typeof objeto == "object")
	{
		$('mensagem').innerHTML = objeto.msg;

		if(objeto.url.length)
		{													   
			setTimeout(function(){ window.location = objeto.url; }, 3000);
		}
	}
	else
	{
		alert('ERROR');
		alert(inner);
		$('mensagem').innerHTML = inner;
	}
}
