// JavaScript Document
// Troca img para swap de imgs
function trocaImagem(objImg, img1, img2) {
	if (objImg.src != img2)
		objImg.src = img2;
	else
		objImg.src = img1;
}
function altera(){
var altura = document.mainImage.height;
var largura = document.mainImage.width;
  if (largura > 251){ 
document.mainImage.width = 251;
var porcenLarg = (251*100)/ largura;
var difPorcentLarg = 100 - porcenLarg;
var defAltura = (altura * difPorcentLarg)/ 100
document.mainImage.height = altura - defAltura
}
}

function NumberMask()
{
	//Se for caracter de controle retorna
	if (event.keyCode < 32)
	{
		event.returnValue = true;
		return;
	}

	//Verifica se foi digitado um número
	if ((String.fromCharCode(event.keyCode) < '0') || (String.fromCharCode(event.keyCode) > '9'))
		event.returnValue = (false)
	else
		event.returnValue = (true);
}

function DateMask(objObject)
{
  if(objObject.value.length == 2 || objObject.value.length == 5)
		objObject.value += "/";

	if (objObject != '[object]')
		return;

	if (event.keyCode < 32)
	{
		event.returnValue = true;
		return;
	}
 
	if (String.fromCharCode(event.keyCode) == '/')
	{
	  //if (objObject.value.indexOf('/') == objObject.value.lastIndexOf("/"))
	  //  event.returnValue = true;
	  //else
	    event.returnValue = false;	  
	    
	  return;
	}

	if ((String.fromCharCode(event.keyCode) < '0') || 
		(String.fromCharCode(event.keyCode) > '9'))
		event.returnValue = (false)
	else
		event.returnValue = (true);
}

// 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, status=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(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 solicitaBrinde(objForm) {
	objForm.Acao.value = "Solicitar";
	objForm.submit();
}


function acaoForm(objForm, acao) {
    objForm.Acao.value = acao;
    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 enviarEmail(objForm) {
	form.Acao.value = "Email";
	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';
}

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);
}

function EnterInserir() {
    if(event.keyCode==13){
       inserirForm(form);
    }

}

function EnterBuscar() {
    if(event.keyCode==13){
       buscarForm(form);
    }

}


function sublinha (obj) {
	obj.style.textDecoration = 'underline';
	obj.style.cursor = 'hand';
} 

function volta (obj){
	obj.style.textDecoration = 'none';
	obj.style.cursor = 'default';
}

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 obtemImagem(nomeCampo, pasta) {
    newPopupScrolling ('../../arquivo/interface/selecionaImagem.asp?campoRetorno=' + nomeCampo + '&pasta=' + pasta, 'imagemSelect', 450, 620)
}

function abreImagem(nomeArquivo, pasta) {
    if (nomeArquivo)
        newPopupScrolling ('../../../imagens/' + pasta + '/' + nomeArquivo, 'imagemView', 450, 600)
    else
        alert('Erro: Selecione uma imagem válida para visualizá-la');
}

function excluirCarrinhoItem(carrinhoItemId) {
    if (confirm('Confirma a exclusão do item selecionado?')) {
        formCarrinho.carrinhoItemId.value = carrinhoItemId;
        acaoForm(formCarrinho, 'ExcluirItemCarrinho');
    }
}

function insereCarrinhoItem(objForm) {
    if (objForm.quantidade.value) {
        acaoForm(objForm, 'InserirItemCarrinho');
    } else {
        alert('Erro: Selecione a quantidade.');
    }
}

function insereCarrinhoItemLista(normaId, qtd, objForm) {
    if (isNaN(qtd)) {
        alert('Erro: Selecione uma quantidade válida');    
    } else {
        if (qtd < 1) {
            alert('Erro: Selecione uma quantidade válida');
        } else {
            objForm.quantidade.value = qtd;
            objForm.normaId.value = normaId;
            acaoForm(objForm, 'InserirItemCarrinhoLista');
        }
    }

}
