// JavaScript Document

//validaNMoto(frm)
function validaNMoto(frm){
	var msg = 'Preencha corretamente os campos abaixo:\n';
	var erro = false;
	
	//marca
	if(frm.marca.value == '' ){
		msg += ' - Marca\n';
		erro = true;
		frm.marca.focus();
	} 
	
	//modelo
	if(frm.modelo.value == ''){
		msg += ' - Modelo\n';
		if(!erro){
			erro = true;
			frm.modelo.focus();
		}
	}
	
	//ano
	if(frm.ano.value == ''){
		msg += ' - Ano (08/08)\n';
		if(!erro){
			erro = true;
			frm.ano.focus();
		}
	}
	
	/*
	//preço
	if(frm.preco.value == ''){
		msg += ' - Preço\n';
		if(!erro){
			erro = true;
			frm.preco.focus();
		}
	}
	*/
	
	if(erro){
		alert(msg);
		return false;
	}else{
		frm.acao.value = 'gravar';	
		return true;
	}
}

//validaLogin()
function validaLogin(frm){
	var msg = 'Preencha corretamente os campos:\n';
	var erro = false;
	
	//Usuário
	if(frm.usuario.value.length == 0){
		msg += '- Usuário\n';
		erro = true;
		frm.usuario.focus();
	}
	
	//Senha
	if(frm.senha.value.length == 0){
		msg += '- Senha\n';
		if(!erro){
			erro = true;
			frm.senha.focus();
		}
	}
	
	if(erro){
		alert(msg);
		return false;
	}else{
		frm.acao.value = 'logar';
		return true;
	}
}

//alterarMoto(idMoto)
function alterarMoto(idMoto){
	document.form.acao.value = 'alterar';
	document.form.idMoto.value = idMoto;
	document.form.action = 'NMoto.asp';
	document.form.submit();
}

//excluirMoto(idMoto)
function excluirMoto(idMoto){
	if(confirm('Deseja realmente excluir esta Moto?')){
		document.form.acao.value = 'excluir';
		document.form.idMoto.value = idMoto;
		document.form.action = 'procDados.asp';
		document.form.submit();
	}
}

//popup(imagem, wdh, hgt)
function popup(imagem, wdh, hgt){
	posL = screen.availWidth/2 - wdh/2;
	posT = screen.availHeight/2 - hgt/2;
	str = 'width='+wdh+', height='+hgt+', left='+posL+', top='+posT+', scrollbars=no';
	window.open('popupResize.asp?file='+ imagem, '', str);
}

//destaque(img, idMoto)
function destaque(img, idMoto){
	var pagina = document.getElementById('pagina').value;
	
	if(img.src.indexOf('Mdestaque.gif') > -1){
		sendXmlHttpRequest('/gerent/procDados.asp?acao=destaque&pagina='+pagina+'&idMoto='+idMoto+'&destaque=0', false);
		img.src = '/gerent/imagens/icones/destaque.gif';
	}else{
		sendXmlHttpRequest('/gerent/procDados.asp?acao=destaque&pagina='+pagina+'&idMoto='+idMoto+'&destaque=1', false);
		img.src = '/gerent/imagens/icones/Mdestaque.gif';
	}
}

//cadastroUsuario()
function cadastroUsuario(){
	var nome = document.getElementById('nome').value;
	var email = document.getElementById('email').value;
	
	if(nome == 'Digite seu nome!') nome = '';
	if(email == 'Digite seu email!') email = '';
	
	if(nome.length > 0 || nome.length > 0){	
		sendXmlHttpRequest('/procDados.asp?acao=cadastrarUsuario&nome='+nome+'&email='+email, true);
		alert(returnValue);
	}else{
		alert('Preencha corretamente o nome e o e-mail!');	
	}
}

//detalheMoto(idMoto)
function detalheMoto(idMoto){
	window.open('detalheMoto.asp?idMoto='+idMoto, 'Detalhe', 'width=478, height=500, scrollbars=yes');
}

//toPage()
function toPage(page){
	var curPage = document.getElementById('curPage').value;
	var spnCurPage = 'spnPage'+curPage;

	curPage = 'pagina'+curPage;
	
	var nxtPage = 'pagina'+page;
	var spnNxtPage = 'spnPage'+page;

/*
	document.getElementById(spnCurPage).style.font = 'normal';
	document.getElementById(spnNxtPage).style.font = 'bold';


	document.getElementById(spnCurPage).style.cursor = 'default';
	document.getElementById(spnNxtPage).style.cursor = 'pointer';
*/
	document.getElementById(curPage).style.display = 'none';
	document.getElementById(nxtPage).style.display = 'block';

	document.getElementById('curPage').value = page;
}

//valorMonetario(campo, milSep, decSep, e)
function valorMonetario(campo, 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;

	key = String.fromCharCode(whichCode);// Valor para o código da Chave

	if(key.charCodeAt(0) == 0 || key.charCodeAt(0) == 8){
		campo.value = '';
		return false;
	}

	if (strCheck.indexOf(key) == -1) return false; // Chave inválida

	len = campo.value.length;

	for(i = 0; i < len; i++){
		if ((campo.value.charAt(i) != '0') && (campo.value.charAt(i) != decSep)) break;
	}

	aux = '';

	for(; i < len; i++){
		if (strCheck.indexOf(campo.value.charAt(i))!=-1) aux += campo.value.charAt(i);
	}

	aux += key;

	len = aux.length;

	if (len == 0) campo.value = '';
	if (len == 1) campo.value = '0'+ decSep + '0' + aux;
	if (len == 2) campo.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++;
		}

		campo.value = '';

		len2 = aux2.length;

		for (i = len2 - 1; i >= 0; i--){
			campo.value += aux2.charAt(i);
		}

		campo.value += decSep + aux.substr(len - 2, len);
	}

	return false;
}
