
/*# IFRAME VIRTUAL #*/
function iframe_virtual(urlVC) {
	var tempIFrame = (document.getElementById('iframe_virtual')) ? document.getElementById('iframe_virtual') : document.createElement('iframe');
	
		tempIFrame.style.border		='0px';
		tempIFrame.style.width		='0px';
		tempIFrame.style.height		='0px';
		
		tempIFrame.setAttribute('id','iframe_virtual');
		tempIFrame.setAttribute('name','iframe_virtual');
		tempIFrame.setAttribute('src',urlVC);
	
	IFrameObj						= document.body.appendChild(tempIFrame);
}

/*#NUMERO#*/
function Numeros(evtKeyPress) {
	var nTecla;
	nTecla = (evtKeyPress.which) ? evtKeyPress.which : evtKeyPress.keyCode;
	
	if((nTecla > 47 && nTecla < 58) || nTecla == 8 || nTecla == 9)
		return true;
	else
		return false;
}

/*#MASCARA#*/
function mascara(vle,msk)	{
	var i	= vle.value.length;
	var s	= msk.substring(0,1);
	var t	= msk.substring(i)
	
	if (t.substring(0,1) != s)	{
		vle.value += t.substring(0,1);
	}
}

/*# VERIFICA DATA #*/
function data_verificar(campoVC) { 
	if (campoVC.value.length > 0) {  
		it_dia		= (campoVC.value.substring(0,2)); 
		it_mes		= (campoVC.value.substring(3,5)); 
		it_ano		= (campoVC.value.substring(6,10)); 
		it_erro		= 0; 
		
		if ((it_dia < 01) || (it_dia < 01 || it_dia > 30) && (it_mes == 04 || it_mes == 06 || it_mes == 09 || it_mes == 11 ) || it_dia > 31) { 
			it_erro	= 1; 
		} 

		if (it_mes < 01 || it_mes > 12 ) { 
			it_erro	= 1; 
		} 

		if (it_mes == 2 && (it_dia < 01 || it_dia > 29 || (it_dia > 28 && (parseInt(it_ano / 4) != it_ano / 4)))) { 
			it_erro	= 1; 
		} 

		if ((it_ano < 1900) || (it_ano>2078)){
			it_erro	= 1;
		}
		
		if (it_erro == 1) { 
			alert("Por favor, preencha uma data válida!"); 
			campoVC.value	= '';
			campoVC.focus(); 
		}
	} 
}

/*#IMG CHANGER#*/
<!--
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}
//-->

/*# VISUALIZAR #*/
function pop_abre(id_item,W,H,vc_arquivo) {
	var X	= Math.ceil( (window.screen.height - H) / 2 ) - 20;
	var Y	= Math.ceil( (window.screen.width - W) / 2 ) - 16;
	
	window.open('pops/'+vc_arquivo+'.asp?'+vc_arquivo+'ID='+id_item,vc_arquivo+'_'+id_item,'width='+W+',height='+H+',top='+X+',left='+Y+',location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,directories=no,toolbar=no');
}

/*# CONTA CARACTER #*/
function contaCaracter(campoVC,caracterN,limiteN) {
	if (campoVC.value.length > limiteN) {
		campoVC.value		= campoVC.value.substring(0,limiteN);
	} else {
		caracterN.value	= limiteN - campoVC.value.length;
	}
}


/*POP-UP PARA O CALENDÁRIO*/
function pop_calendario(id_loja, vc_campo,dt_now) {

	if (vc_campo == 'retiradaDT'){
		vc_loja	= 'RETIRADA';
		vc_tipo	= 'ret';
	}else{
		vc_loja	= 'DEVOLUÇÃO';
		vc_tipo	= 'dev';
	}

	if (id_loja.value > 0){
		if(dt_now != ''){
			var vc_date	= dt_now;
			}
		var W = 237;
		var H = 144;
		var X = Math.ceil( (window.screen.height - H) / 2 );
		var Y = Math.ceil( (window.screen.width - W) / 2 );
		window.open('../wm/visualizacoes/calendario.asp?vc_tipo='+vc_tipo+'&vc_campo='+vc_campo+'&date='+vc_date+'','pp_calendario','width='+W+',height='+H+',top='+X+',left='+Y+',location=no,status=yes,menubar=no,scrollbars=no,resizable=no,directories=no,toolbar=no');
	}else{

		alert('ATENÇÃO! \n\nSelecione a LOJA DE ' + vc_loja);
		id_loja.focus();
	}
}




/*# VERIFICAR #*/
function simulacao_verificar(){
	
	var mensagem1		= 'ATENÇÃO!\n\n';
	var mensagem2		= '';
	
	if(document.frm.veiculoID.value == 0){
		mensagem2	+= 'Selecione um VEÍCULO.\n';
	}
	
	if(document.frm.retiradaDT.value.length < 10){
		mensagem2	+= 'Preencha o campo DATA DE RETIRADA.\n';
	}

	if(document.frm.retiradaHR.value < 1){
		mensagem2	+= 'Informe a  HORA DE RETIRADA.\n';
	}

	if(document.frm.retirada_lojaID.value == 0){
		mensagem2	+= 'Selecione a LOJA DE RETIRADA.\n';
	}

	if(document.frm.devolucaoDT.value.length < 10){
		mensagem2	+= 'Preencha o campo DATA DE DEVOLUÇÃO.\n';
	}
	
	if(document.frm.devolucaoHR.value < 1){
		mensagem2	+= 'Informe a  HORA DE DEVOLUÇÃO.\n';
	}
	
	if(document.frm.devolucao_lojaID.value == 0){
		mensagem2	+= 'Selecione a LOJA DE DEVOLUÇÃO.\n';
	}

	if(document.frm.retiradaDT.value.length >= 10 && document.frm.devolucaoDT.value.length >= 10){
		if (!validaPeriodo(document.frm.retiradaDT, document.frm.devolucaoDT)){
			mensagem2	+= 'A data de Retirada não pode ser maior que a data de Devolução.\n';
		}
	}

	var qtd	= 0;
	for (i=0; i<4; i++)	{
		if (document.frm.quilometragemIT[i].checked == true){
			qtd++;	
		}
	}
	
	if (qtd == 0){
		mensagem2	+= 'Selecione a Quilometragem.\n';
	}

	
	if(document.frm.quilometragemIT[3].checked == true){
		if(document.frm.quilometragem_outraIT.value.length == 0){
			mensagem2	+= 'Preencha o campo KM PREVISTO.\n';
		}
	}
	
	if (mensagem2 != ''){
		alert(mensagem1 + mensagem2);
		return false;
	} else {
		document.getElementById('enviarBT').disabled	= true;
		document.getElementById('enviarBT').value		= 'enviando';		
		document.frm.submit();
	}

}

/*# EXIBIR KM OUTRA #*/
function exibirKmOutra(it_opcao){
	if (it_opcao == "outra"){
		document.getElementById('tr_km_outra').style.display	= 'block';
		document.getElementById('quilometragem_outraIT').focus();
	}else{
		document.getElementById('tr_km_outra').style.display	= 'none';
	}
}


/*# SIMULACAO IMPRIMIR ABRE O POPUP #*/
function simulacaoImprimir() {
	var W	= 570; 	
	var H	= 570;
	var X	= Math.ceil( (window.screen.height - H) / 2 ) - 20;
	var Y	= Math.ceil( (window.screen.width - W) / 2 ) - 16;
	
	window.open('','imprimir','width='+W+',height='+H+',top='+X+',left='+Y+',location=no,status=no,menubar=no,scrollbars=yes,resizable=no,directories=no,toolbar=no').focus();
	document.simulacaoFRM.action	= 'imprimir.asp';
	document.simulacaoFRM.target	= 'imprimir';
	document.simulacaoFRM.submit();
}


/*# ENVIAR EMAIL MOSTRAR - Exibe os campos nome e e-mail #*/
function enviarEmailMostrar(){
	var vc_html	= '';
	vc_html	+=	'<table width="100%" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:10px; color:#FFF;">';
	vc_html	+=	'<tr>';	
	vc_html	+=	'<td align="right"><b>Nome</b></td>';
	vc_html	+=	'<td><input type="text" name="nomeVC" id="nomeVC"  value="" class="bx_01" style="width=200px;" /></td>';
	vc_html	+=	'</tr>';	
	vc_html	+=	'<tr>';		
	vc_html	+=	'<td align="right" nowrap><b>E-mail</b></td>';
	vc_html	+=	'<td><input type="text" name="emailVC" id="emailVC"  value="" class="bx_01" style="width=200px;" /></td>';	
	vc_html	+=	'</tr>';		
	vc_html	+=	'<tr>';
	vc_html	+=	'<td></td>';
	vc_html	+=	'<td><input type="button" name="emailSubmeterBT" class="bt_01" value="Enviar" onclick="enviarEmailVerificar();" /></td>';	
	vc_html	+=	'</tr>';
	vc_html	+=	'</table>';
	
	document.getElementById('td_campos').innerHTML = vc_html;
}

/*# EFETUAR RESERVA MOSTRAR - Exibe os campos nome e e-mail #*/
function efetuarReservaMostrar(){
	var vc_html	= '';
	vc_html	+=	'<table width="100%" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:10px; color:#FFF;">';
	vc_html	+=	'<tr>';	
	vc_html	+=	'<td align="right"><b>Nome</b></td>';
	vc_html	+=	'<td><input type="text" name="nomeVC" id="nomeVC" value="" class="bx_01" style="width=200px;" /></td>';
	vc_html	+=	'</tr>';	
	vc_html	+=	'<tr>';		
	vc_html	+=	'<td align="right" nowrap><b>E-mail</b></td>';
	vc_html	+=	'<td><input type="text" name="emailVC" id="emailVC" value="" class="bx_01" style="width=200px;" /></td>';	
	vc_html	+=	'</tr>';		
	vc_html	+=	'<tr>';
	vc_html	+=	'<tr>';	
	vc_html	+=	'<td align="right"><b>Fone</b></td>';
	vc_html	+=	'<td><input type="text" name="telefoneVC" id="telefoneVC" value="" class="bx_01" style="width=200px;" /></td>';
	vc_html	+=	'</tr>';	
	vc_html	+=	'<td></td>';
	vc_html	+=	'<td><input type="button" name="reservaSubmeterBT" class="bt_01" value="Enviar" onclick="efetuarReservaVerificar();" /></td>';	
	vc_html	+=	'</tr>';
	vc_html	+=	'</table>';
	
	document.getElementById('td_campos').innerHTML = vc_html;
}


/*# ENVIAR EMAIL VERIFICAR #*/
function enviarEmailVerificar(){
	var mensagem1		= 'ATENÇÃO!\n\n';
	var mensagem2		= '';

	if(document.getElementById('nomeVC').value.length == 0){
		mensagem2	+= 'Preencha o campo NOME.\n';
	}
	
	if(document.getElementById('emailVC').value.length > 0){
		mensagem2 	+= verficaEmail(document.getElementById('emailVC').value);
	}else{
		mensagem2 	+= 'Preencha o campo E-MAIL.\n';
	}
	
	if (mensagem2 != ''){
		alert(mensagem1 + mensagem2);
		return false;
	} else {
		document.getElementById('emailSubmeterBT').disabled	= true;
		document.getElementById('emailSubmeterBT').value	= 'enviando';		
		document.simulacaoFRM.action						= 'codigos/enviar_email.asp';
		document.simulacaoFRM.target						= 'codigo';
		document.simulacaoFRM.submit();
	}
}

/*# EFETUAR RESERVA VERIFICAR #*/
function efetuarReservaVerificar(){
	var mensagem1		= 'ATENÇÃO!\n\n';
	var mensagem2		= '';

	if(document.getElementById('nomeVC').value.length == 0){
		mensagem2	+= 'Preencha o campo NOME.\n';
	}
	
	if(document.getElementById('emailVC').value.length > 0){
		mensagem2 	+= verficaEmail(document.simulacaoFRM.emailVC.value);
	}else{
		mensagem2 	+= 'Preencha o campo E-MAIL.\n';
	}
	
	if(document.getElementById('telefoneVC').value.length == 0){
		mensagem2	+= 'Preencha o campo TELEFONE.\n';
	}
	
	
	if (mensagem2 != ''){
		alert(mensagem1 + mensagem2);
		return false;
	} else {
		document.getElementById('reservaSubmeterBT').disabled	= true;
		document.getElementById('reservaSubmeterBT').value	= 'enviando';		
		document.simulacaoFRM.action						= 'codigos/efetuar_reserva.asp';
		document.simulacaoFRM.target						= 'codigo';
		document.simulacaoFRM.submit();
	}
}


/*# VALIDA E-MAIL [expressão regular] */
function verficaEmail(campo) {
 	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(campo)) {
  		return '';
	}
	return '- O E-MAIL digitado é inválido.\n';
}

/*# verifica se a data de INÍCIO é menor que a de FIM */
function validaPeriodo(dt_inicio, dt_fim) {
	var dt_inicio  	= dt_inicio.value.split('/');
	var dt_fim 		= dt_fim.value.split('/');
	var vc_inicio  	= '';
	var vc_fim 		= '';
	var tx_msg		= '';

	var inicio		= new Date(dt_inicio[2], dt_inicio[1], dt_inicio[0], 0, 0, 0, 0);
	var fim			= new Date(dt_fim[2], dt_fim[1], dt_fim[0], 0, 0, 0, 0);
	
	if (fim < inicio)
		// ERRO
		return false;
	else
		// OK
		return true;
}

/*# TRIM - Função que verifica se há espaços no início e fim de uma string, semelhante ao trim #*/
function trim(vc_string) {
 
  //Remove os espaços do Inicio
  while ((vc_string.substring(0,1) == ' ') || (vc_string.substring(0,1) == '\n') || (vc_string.substring(0,1) == '\r')) {
    vc_string = vc_string.substring(1,vc_string.length);
  }
 
  //Remove os espaços do Final
  while ((vc_string.substring(vc_string.length-1,vc_string.length) == ' ') || (vc_string.substring(vc_string.length-1,vc_string.length) == '\n') || (vc_string.substring(vc_string.length-1,vc_string.length) == '\r')) {
    vc_string = vc_string.substring(0,vc_string.length-1);
  }
 
 return vc_string;
}


/*# HORARIO VALIDAR - Verifica ao clicar no campo hora, se já foi escolhido uma Loja e Data #*/
function horarioValidar(vc_tipo, id_loja, data){

	var vc_campo	= '';
	if (vc_tipo == 'ret'){
		vc_campo	= 'RETIRADA';
	}else{
		vc_campo	= 'DEVOLUÇÃO';
	}
	
	if(id_loja.value == 0){
		alert('ATENÇÃO! \n\nSelecione a LOJA DE ' + vc_campo);
		id_loja.focus();
	}else if(trim(data.value).length < 10){
		alert('ATENÇÃO! \n\nSelecione a DATA DE ' + vc_campo);
		data.value	= '';
		data.focus();
	}
	
}

/*# PREENCHE HORARIO VALIDA - Quando digitado a data ou escolhido pelo calendário #*/
function preencheHorarioValida(obj, id_loja, data, vc_tipo){

	if (id_loja.value > 0){

		if (obj.value.length > 9){
			data_verificar(obj);
			iframe_virtual('simulacao/temp/gerar_horario.asp?id_loja='+id_loja.value+'&vc_data='+data+'&vc_tipo='+vc_tipo);
		}
		
	}else{
	
		var vc_campo	= '';
		if (vc_tipo == 'ret'){
			vc_campo	= 'RETIRADA';
		}else{
			vc_campo	= 'DEVOLUÇÃO';
		}

		alert('ATENÇÃO! \n\nSelecione a LOJA DE ' + vc_campo);
		id_loja.focus();
		obj.value	= '';
	}
}


/*# PREENCHE HORARIO - Função chamado no arquivo asp temporario. Gera as opções do combo a partir dos dados selecionados no Banco. #*/
function preencheHorario(id_hora,vc_hora, vc_tipo) {
	var array_id 					= id_hora.split("|");
	var array_nome 					= vc_hora.split("|");
	if (vc_tipo == 'ret'){
		dropdown1					= eval('document.frm.retiradaHR');
	}else{
		dropdown1					= eval('document.frm.devolucaoHR');
	}
	
	/*#LIMPAR CAIXA#*/
	for (var x = dropdown1.length; x >= 0; x--) {
		dropdown1.options[x]	= null;
	}	

	/*#INSERE NOVOS#*/
	var n = 0;

	for (var t = 0; t < (array_id.length)-1 ; t++) {
		dropdown1.options[n]	= new Option(array_nome[t], array_id[t]);
		n++;
	}
}


/*# VEICULO VALOR KM LIVRE - Verifica se o veiculo escolhido possui um valor pra km livre. Caso Seja zero, desativa a opção km livre #*/
function veiculoValorKmLivre(id_veiculo){
	//Este array armazena todos os veiculos e seus valores de km_livre (recuperado de um hidden do form)
	//Formato "<<id_veiculo>>-<<mn_valor>>"
	var ar_veiculos		= document.frm.ar_mn_veiculo_km_livre.value.split('|');
	
	for (i=0; i<ar_veiculos.length-1; i++){
		//Encontra no array o id_veículo selecionado
		if(ar_veiculos[i].split('-')[0] == id_veiculo){
			//Se o valor do km livre for zero, então desativa o campo, caso contrário ativa novamente.
			//Se o campo que vai ser desativado estiver checado. Ele é deschecado.
			if(ar_veiculos[i].split('-')[1] == 0){
				if (document.frm.quilometragemIT[0].checked == true){
					document.frm.quilometragemIT[0].checked = false;
				}
				document.frm.quilometragemIT[0].disabled = true;
				document.frm.km_livre_zerado.value	= 1;		//Sinaliza no form que o veículo tem o km livre zerado
			}else{
				document.frm.quilometragemIT[0].disabled = false;
				document.frm.km_livre_zerado.value	= 0;
			}
			break;
		}
	}
}