String.prototype.trim = function(){
	return this.replace(/^\s*/, "").replace(/\s*$/, "");
}


function travaCampo(element, num, e) {	
	var whichCode = (window.Event) ? e.which : e.keyCode;
	if (whichCode == 13) return true;  // Enter
	if (whichCode == 8) return true;  // Delete (Bug fixed)
	if (element.value.length >= 100) {
		
	   return false;
	}
}


function numberFormat(e) {
	var key;
	var keychar;
	var reg;
	
	if(window.event)  //IE
		key = e.keyCode;
	else if(e.which) 	{//Netscape
			key = e.which;
	}
	else
		return true;
	if(key > 46 && key < 58 || key == 8) return true;
	else return false;
}


function currencyFormat(fld, milSep, decSep, e) {
	var sep = 0;
	var key = '';
	var i = j = 0;
	var len = len2 = 0;
	var strCheck = '0123456789';
	var aux = aux2 = '';
	var whichCode = (window.Event) ? e.which : e.keyCode;
	if (whichCode == 13) return true;  // Enter
	if (whichCode == 8) return true;  // Delete (Bug fixed)
	key = String.fromCharCode(whichCode);  // Get key value from key code
	if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
	len = fld.value.length;
	for(i = 0; i < len; i++)
	if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
	aux = '';
	for(; i < len; i++)
	if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
	aux += key;
	len = aux.length;
	if (len == 0) fld.value = '';
	if (len == 1) fld.value = '0'+ decSep + '0' + aux;
	if (len == 2) fld.value = '0'+ decSep + aux;
	if (len > 2) {
		aux2 = '';
		for (j = 0, i = len - 3; i >= 0; i--) {
			if (j == 3) {
				aux2 += milSep;
				j = 0;
			}
			aux2 += aux.charAt(i);
			j++;
		}
		fld.value = '';
		len2 = aux2.length;
		for (i = len2 - 1; i >= 0; i--)
		fld.value += aux2.charAt(i);
		fld.value += decSep + aux.substr(len - 2, len);
	}
	return false;
}


function updateListaCotacoes() {
	$(".periodo").editInPlace({				
	    url: "adminCotacao.updatePeriodo.logic",
	    show_buttons: true,
	    saving_image: "img/loader.gif"
	});

	$(".valor").editInPlace({
	    url: "adminCotacao.updateValor.logic",
	    show_buttons: true,
	    saving_image: "img/loader.gif"
	});

	$("#cotacaoPeriodo").autocomplete('adminCotacao.autoCompletePeriodo.logic', {
	   extraParams: {
	       idCultura: function() { return $("#idCultura").val(); },
	       idMercado: function() { return $("#idMercado").val(); }
	   }
	});
	
	$('#cotacaoValor').priceFormat({
		centsLimit: 2,
	    prefix: '',
	    centsSeparator: ',',
	    thousandsSeparator: '.'
	});
	
	$("#idMercado").change(function() {
		$("#mercado_loader").show();
		if(this.value != "") {
			var time = new Date().getTime(); 
			$.post("adminCotacao.selectMercado.ajax.logic", {idMercado:  this.value, time: time}, 
			function(data){
				var jsonObject = eval('(' + data + ')');
				var mercadoFuturo = jsonObject.mercadoFuturo;
				mercadoFuturo ? $(".container_periodo").show() : $(".container_periodo").hide();
				$("#mercado_loader").hide();
			});
		}
		else {
			$(".container_periodo").hide();
			$("#mercado_loader").hide();
		}							
	});		
	
	$("#btn_incluir_cotacao").click(function() {
		$("#loader_incluir_cotacao").show();
		$.post("adminCotacao.insere.logic", {idCultura:  $("#idCultura").val(), 
						idMercado:  $("#idMercado").val(), data:  $("#data").val(),
						cotacaoPeriodo:  $("#cotacaoPeriodo").val(), cotacaoValor:  $("#cotacaoValor").val()
						}, 
			function(data){
				$("#listagem").attr({innerHTML: data});	
				$("#loader_incluir_cotacao").hide();
				updateListaCotacoes();
			});
	});	
}


//INICIO FUNCAO BUSCAR COTAÇÕES
function searchCotacoes(selectCultura) {
	$("#listagem").attr({innerHTML: ""});	
	if (selectCultura.value != "") {
		var time = new Date().getTime(); 
		$("#load_busca").show();		
		$.post("adminCotacao.search.logic", {data:  $("#data").val(), idCultura: $("#idCultura").val()}, 
			function(data){			
				$("#listagem").attr({innerHTML: data});	
				$("#load_busca").hide();
							
				updateListaCotacoes();									
			});	
	}
}
//FIM FUNCAO BUSCAR COTAÇÕES

//INICIO FUNCAO EXCLUIR COTAÇÃO
function deletaCotacao(id) {
	var time = new Date().getTime(); 
	
	$.post("adminCotacao.deleta.logic", {idCotacao: id}, 
		function(data){			
			$("#listagem").attr({innerHTML: data});	
			updateListaCotacoes();
		});	
}
//FIM FUNCAO EXCLUIR COTAÇÃO
