// JavaScript Document
var path = 'http://www.nossaweb.net/novaera/wwwroot/';


// Troca img para swap de imgs
function trocaImagem(objImg, img1, img2) {
	if (objImg.src != img2)
		objImg.src = img2;
	else
		objImg.src = img1;
}

function sublinha (obj) {
	obj.style.textDecoration = 'underline';
	obj.style.cursor = 'hand';
} 

function volta (obj){
	obj.style.textDecoration = 'none';
	obj.style.cursor = 'default';
}

// Teste de form
function testaForm(objForm) {
		var x, texto;
		for (x in objForm)
			texto += x + '\t\t'
		alert(texto);
}

// Abre um popup
function newPopup (URL, nome, altura, largura) {
	window.open (URL,'','height=' + altura + ', width=' + largura);
}

// Abre um popup com scrolling
function newPopupScrolling (URL, nome, altura, largura, scrolling) {
	window.open (URL,'','height=' + altura + ', width=' + largura + ', scrollbars=1');
}

// Serve para a paginaçao de dados
function vaiParaPagina (objForm, valor) {
	objForm.paginaAtual.value = valor;
	buscarForm(objForm);
}

function mudaItensPorPagina (objForm, valor) {
	objForm.itensPorPagina.value = valor;
	objForm.paginaAtual.value = 1;
	buscarForm(objForm);
}

function buscarForm_Total(objForm) {
	objForm.Acao.value = "Buscar_Total_Produto";
	objForm.submit();
}

function buscarForm(objForm) {
	objForm.Acao.value = "Buscar";
	objForm.submit();
}

function inserirForm(objForm) {
	objForm.Acao.value = "Inserir";
	objForm.submit();
}

function enviarForm(objForm) {
	objForm.Acao.value = "Enviar";
	objForm.submit();
}

function confirmarForm(objForm) {
	objForm.Acao.value = "Confirmar";
	objForm.submit();
}

function excluirForm(objForm) {
	if (confirm('Deseja realmente excluir os itens selecionados?')) {
		objForm.Acao.value = "Excluir";
		objForm.submit();
	}
	else {
		return;
	}
}

function alterarForm(objForm) {
	form.Acao.value = "Alterar";
	form.submit();
}

function obtemAltura(id) {
	return document.getElementById(id).clientHeight;
}

function obtemLargura(id) {
	return document.getElementById(id).clientWidth;
}

// Retorna o valor de um popup
function retornaValorPopup(nomeForm, campo, valor, nome) {
	var objValor = eval('window.opener.' + nomeForm + '.' + campo);
	objValor.value = nome;
	var objNome = eval('window.opener.' + nomeForm + '.' + campo + 'Id');
	objNome.value = valor;
	window.close();
}

function abreJanela (caminho) {
	document.location = caminho;
}

function mostraDiv (obj) {
	if (obj.style.display != 'block')
		obj.style.display = 'block';
	else
		obj.style.display = 'none';
}

// Funções utilizadas no menu de categorias, para economizar kb´s
function mD(obj) { 
	mostraDiv(obj);
}

function ti(obj) {
	trocaImagem(obj, path + 'imagens/bt_Mais.jpg', path + 'imagens/bt_Menos.jpg'); 
}

function escolheCategoria(nome, id) {
	document.form.CategoriaProdutoId.value = id;
	document.form.CategoriaProduto.value = nome;
}

var maxTextArea = 5000;
// A variável campo deve ser um div com valor inicial visível indicando o limite máximo de caracteres no formato:
// maxTextArea + " caracteres restantes." 
function atualizaMax(objTA, campo) {
	if (objTA.value.toString().length <= maxTextArea)
		campo.innerHTML = (maxTextArea - objTA.value.toString().length) + ' caracteres restantes.';
	else {
		objTA.value = objTA.value.toString().substring(0, maxTextArea);
	}
}

// Recebe um obj de imagem e valores maximos de X e Y
function redimensionaImg(objImg, maxX, maxY) {
	var imgX = objImg.clientWidth, imgY = objImg.clientHeight, prop;
	if (imgX > imgY) {
		prop = imgX / imgY;
		objImg.width = maxX;
		objImg.height = maxX / prop;
	}
	if (imgX < imgY) {
		prop = imgY / imgX;
		objImg.height = maxY;
		objImg.width = maxY / prop;
	}
	if (imgX == imgY) {
		if (maxX  > maxY) objImg.height = objImg.width = maxY;
		if (maxX  < maxY) objImg.height = objImg.width = maxX;
		if (maxX == maxY) objImg.height = objImg.width = maxX;
	}
}

function formatCurrency(num, moeda) {
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
    num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    if(cents<10) cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+'.'+
    num.substring(num.length-(4*i+3));
    return (((sign)?'':'-') + num + ',' + cents);
}


