﻿
function validaEmail(stEMail)
{
    if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(stEMail)){
       return false
    }else{
       return true
    }
}

function newAjaxObject(){
	var objAjax;

	try{
		objAjax = new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch(e){
		try {
			objAjax = new ActiveXObject("Msxml2.XMLHTTP");
			objAjax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		}
		catch(ex){
			try{
				objAjax = new XMLHttpRequest();
			}
			catch(exc){
				objAjax = null;
			}
		}
	}
	return objAjax;
}

function RunAjaxRequest(stMethod, stUrl, stParameters, stPosFunctionName){
	
	objXmlRequest = newAjaxObject()
	process       = stPosFunctionName;

	objXmlRequest.onreadystatechange = function()
	{
		try{
			process(objXmlRequest);
		}
		catch(objException){
		}
	}

	if (!stMethod) {
		stMethod = "POST";
	}

	stMethod = stMethod.toUpperCase();

	objXmlRequest.open(stMethod, stUrl, true);

	if (stMethod == "POST") {
		//objXmlRequest.setRequestHeader("Connection", "close");
		objXmlRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		objXmlRequest.setRequestHeader("Method", "POST" + stUrl + "HTTP/1.1");
		
	}

	objXmlRequest.send(stParameters);
	return objXmlRequest;
}


function retornoSendMail(objXmlRequest){	
	if(objXmlRequest.readyState == 4){
		if (objXmlRequest.status == 200){
			var retorno = "";
			retorno = objXmlRequest.responseText;			
			if(retorno == 0){
				alert('N&atilde;o foi poss&iacute;vel enviar o Email nesse momento!');
			}else{

				document.getElementById('01').style.display='none';
				document.getElementById('02').style.display='none';
				document.getElementById('03').style.display='block';
				document.getElementById('04').style.display='none';
				document.frmHome.stComb.value='';
				document.frmHome.fgTipoVeiculo.value='';

			}
		}
	}
}
function dataInspecao(placa,tipoVeiculo){
	var arrPlaca=new Array();

	if(tipoVeiculo != 3){
		//Veiculos Leves / Motocicletas / Onibus
		arrPlaca[0] = '03/Outubro </br>a</br> 31/Dezembro'
		arrPlaca[1] = '01/Fevereiro </br>a</br> 30/Abril'
		arrPlaca[2] = '03/Mar&ccedil;o </br>a</br> 31/Maio'
		arrPlaca[3] = '02/Abril </br>a</br> 30/Junho'
		arrPlaca[4] = '03/Maio </br>a</br> 31/Julho'
		arrPlaca[5] = '03/Junho </br>a</br> 31/Agosto'
		arrPlaca[6] = '03/Junho </br>a</br> 31/Agosto'
		arrPlaca[7] = '03/Julho </br>a</br> 30/Setembro'
		arrPlaca[8] = '03/Agosto </br>a</br> 31/Outubro'
		arrPlaca[9] = '02/Setembro </br>a</br> 30/Novembro'
		
		document.getElementById('data').innerHTML = arrPlaca[placa];
		document.frmHome.stPeriodoMail.value 	  = arrPlaca[placa];
		return true
		
	}else{
		//Caminhoes
		arrPlaca[0] = '01/Outubro </br>a</br> 31/Dezembro'
		arrPlaca[1] = '01/Julho </br>a</br> 30/Setembro'
		arrPlaca[2] = '01/Julho </br>a</br> 30/Setembro'
		arrPlaca[3] = '01/Agosto </br>a</br> 31/Outubro'
		arrPlaca[4] = '01/Agosto </br>a</br> 31/Outubro'
		arrPlaca[5] = '01/Agosto </br>a</br> 31/Outubro'
		arrPlaca[6] = '01/Setembro </br>a</br> 30/Novembro'
		arrPlaca[7] = '01/Setembro </br>a</br> 30/Novembro'
		arrPlaca[8] = '01/Setembro </br>a</br> 30/Novembro'
		arrPlaca[9] = '01/Outubro </br>a</br> 31/Dezembro'
		
		document.getElementById('data').innerHTML = arrPlaca[placa];
		document.frmHome.stPeriodoMail.value 	  = arrPlaca[placa];
		return true		
	}
	return false
}

function zeraConsulta(){
	document.getElementById('01').style.display='block';
	document.getElementById('02').style.display='none';
	document.getElementById('03').style.display='none';
	document.getElementById('04').style.display='none';	
	
	document.frmHome.nmFinalPlaca.value='';
	document.frmHome.stPeriodoMail.value='';
	document.frmHome.nmAno.value='';
	document.frmHome.stEmail.value='';
	document.frmHome.fgTipoVeiculo.value='';
	document.frmHome.stComb.value='';

}

function inspecao(nmPasso){
	
	var msgErro  = 'Aten&ccedil;&atilde;o para o(s) seguinte(s) erro(s):\n\n';
	var erro 	 = 0;
	var isento = 0;
	var placa    = document.frmHome.nmFinalPlaca;
	var ano      = document.frmHome.nmAno;
	var tipoVeiculo = document.frmHome.fgTipoVeiculo;
	var combustivel = document.frmHome.stComb;
	
	if(nmPasso == 1){
		
		if (!placa.value){
			erro ++;
			msgErro += '- Final da Placa \n';
		}
		
		if (ano.value.length < 4){
			erro ++;
			msgErro += '- Ano \n';
		}
		
		if (!tipoVeiculo.value){
			erro ++;
			msgErro += '- Tipo do ve&iacute;culos \n';
		}
		
		if (!combustivel.value){
			erro ++;
			msgErro += '- Combust&iacute;vel';
		}

		if(erro == 0){
			//		
			
			if(tipoVeiculo.value == 1 || ano.value  == 2010){
				
				document.getElementById('01').style.display='none';
				document.getElementById('02').style.display='none';
				document.getElementById('03').style.display='none';
				document.getElementById('04').style.display='block';
			
				if(ano.value == 2010){
					isento = 1;
					document.getElementById('isento').innerHTML = "Ve&iacute;culos com ano de fabrica&ccedil;&atilde;o 2010 est&atilde;o isentos da  inspe&ccedil;&atilde;o no exerc&iacute;cio 2010.";
				}

				if(tipoVeiculo.value == 1){
					isento = 1;
					document.getElementById('isento').innerHTML = "Motocicletas equipadas com motor de 2 tempos est&atilde;o isentas da inspe&ccedil;&atilde;o no ano de 2010.";
				}
			}
			

			if (isento == 0){

				document.getElementById('01').style.display='none';
				document.getElementById('02').style.display='block';

				dataInspecao(placa.value,tipoVeiculo.value);				
			}
			

			//

		}else{
			alert(msgErro);
		}	
	
	}
	
	if(nmPasso == 2){
		
		var periodo = document.frmHome.stPeriodoMail.value;
		var email 	= document.frmHome.stEmail.value;		
		var str     = 'stPeriodoMail='+periodo+'&stEmail='+email+'';		
		
		if(validaEmail(email) == true){
			if(periodo != ""){
				RunAjaxRequest('POST','inc/enviaMail.php',str, retornoSendMail);
				document.getElementById('mailf').innerHTML = email;
			}		 
		}else{		
			alert('Prencha corretamente o seu E-mail.');
		}
	}
}

jQuery(function($){
	$("#inmFinalPlaca").mask("9");
	$("#inmAno").mask("9999");
});
