window.onload = function() {
  // Lista do caminho e nome dos arquivos de imagem. Pode ter quantas você quiser :)
  var imagem_lista = Array('skype.jpg','msn.jpg');
  for(var i=0; i<imagem_lista.length; i++) {
    var preload = new Image();
    preload.src = "images/"+imagem_lista[i];
  }
}

function addEvent(obj, evType, fn){
	if (obj.addEventListener){
		obj.addEventListener(evType, fn, true);
		return true;
	} else if (obj.attachEvent){
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	} else {
		return false;
	}
}

function validaBrowser(e){
	//Correção para eventos quebrados da Microsoft
	if(typeof(e)=='undefined') {
		var f = window.event;
		var source = f.target?f.target:f.srcElement;
	} else var source = e.target?e.target:e.srcElement;

	//Correção para o bug do Konqueror/Safari
	if(source.nodeType==3) source = source.parentNode;

	if(source.id=="cpf") isCPFCNPJ(source.id,0);
	if(source.id=="telefone"||source.id=="celular"||source.id=="fax"||source.id=="telempresa") isPhone(source.id);
	if(source.id=="cep") verificaCEP();
}

function verificaBairro(p) { /* p = tipo de pessoa */
	var pessoa = p=="f" ? "seu bairro" : "o bairro de sua empresa";
	if(isEmpty(document.getElementById("bairro").value)){
		alert("Por favor informe "+pessoa+"!");
		document.getElementById("bairro").focus();
		return false;
	}
}

function verificaCEP() {
	var CEP = document.getElementById("cep");
	if(isEmpty(CEP.value)){
		alert("O CEP possui 8 dígitos.\nDigite somente números!");
		CEP.focus();
	} else isCEP();
}

function verificaCidade(p) {
	var pessoa = p=="f" ? "sua cidade" : "a cidade de sua empresa";
	if(isEmpty(document.getElementById("cidade").value)){
		alert("Por favor informe "+pessoa+"!");
		document.getElementById("cidade").focus();
		return false;
	}
}

function verificaCPFCNPJ(p) {
	var pessoa = p=="cpf" ? "CPF" : "CNPJ";
	if(!isCPFCNPJ(p,0)){
		alert("Por favor informe um "+pessoa+" válido!");
		document.getElementById(p).focus();
		return false;
	}
}

function verificaData() {
	if(isEmpty(document.getElementById("data").value)) {
		alert("Por favor informe a data do serviço!");
		document.getElementById("data").focus();
		return false;
	}
}

function verificaDebito() {
	if(isEmpty(document.getElementById("debito").value)) {
		alert("Por favor informe para quem deve ser debitado!");
		document.getElementById("debito").focus();
		return false;
	}
}

function verificaDestino() {
	if(isEmpty(document.getElementById("destino").value)) {
		alert("Por favor informe seu local de destino!");
		document.getElementById("destino").focus();
		return false;
	}
}

function verificaEmail() {
	if(!isEmail(document.getElementById("email").value)){
		alert("Por favor informe um email válido!");
		document.getElementById("email").value = "";
		document.getElementById("email").focus();
		return false;
	}
}

function verificaEmpresa() {
	if(isEmpty(document.getElementById("empresa").value)){
		alert("Por favor preencha o Nome/Razão Social de sua empresa");
		document.getElementById("empresa").focus();
		return false;
	}
}

function verificaEndereco(p) {
	if(isEmpty(document.getElementById("endereco").value)){
		var pessoa = p=="f" ? "seu endereço" : "o endereço de sua empresa";
		alert("Por favor informe "+pessoa+"!");
		document.getElementById("endereco").focus();
		return false;
	}
}

function verificaEstado(p) {
	if(isEmpty(document.getElementById("estado").value)){
		var pessoa = p=="f" ? "seu estado" : "o estado de sua empresa";
		alert("Por favor informe "+pessoa+"!");
		document.getElementById("estado").focus();
		return false;
	}
}

function verificaInicio() {
	if(isEmpty(document.getElementById("inicio").value)) {
		alert("Por favor informe a hora de início do serviço!");
		document.getElementById("inicio").focus();
		return false;
	}
}

function verificaInscricao(p) {
	if(!isNumber(document.getElementById("insc"+p).value)) {
		alert("Por favor informe somente números!");
		document.getElementById("insc"+p).value = "";
		document.getElementById("insc"+p).focus();
    return false;
	}
}

function verificaNome(p) {
	if(isEmpty(document.getElementById("nome").value)){
		var pessoa = p=="f" ? "seu nome" : "o nome do responsavel";
		alert("Por favor informe "+pessoa+"!");
		document.getElementById("nome").focus();
		return false;
	}
}

function verificaOBS () {
  if(isEmpty(document.getElementById("obs").value)) {
    alert("Por favor informe o nome dos passageiros!");
    document.getElementById("obs").focus();
    return false;
  }
}

function verificaOrigem() {
	if(isEmpty(document.getElementById("origem").value)) {
		alert("Por favor informe o local da partida!");
		document.getElementById("origem").focus();
		return false;
	}
}

function verificaPagamento() {
	if(isEmpty(document.getElementById("pagam").value)) {
		alert("Por favor informe a forma de pagamento!");
		document.getElementById("pagam").focus();
		return false;
	}
}

function verificaPassageiros() {
	var pass = document.getElementById("passageiros");
	if(!isNumber(pass.value) || isEmpty(pass.value) ){
		alert("Por favor informe o número de passageiros!");
		pass.value = "";
		pass.focus();
		return false;
	}
}

function verificaProfissao() {
	if(isEmpty(document.getElementById("profissao").value)){
		alert("Por favor informe sua profissao!");
		document.getElementById("profissao").focus();
		return false;
	}
}

function verificaServico() {
	if(isEmpty(document.getElementById("servico").value)) {
		alert("Por favor informe o tipo de serviço desejado!");
		document.getElementById("servico").focus();
		return false;
	}
}

function verificaTelefone(p) {
	if(isEmpty(document.getElementById(p).value)) {
		alert("Por favor informe um Telefone válido!");
		document.getElementById(p).value = "";
		document.getElementById(p).focus();
		return false;
	}
}

function verificaVeiculo() {
	if(isEmpty(document.getElementById("veiculo").value)) {
		alert("Por favor informe o tipo de veículo desejado!");
		document.getElementById("veiculo").focus();
		return false;
	}
}

/* Verifica o e-mail fornecido */
function isEmail(pStr){
var reEmail = /^[\w-]+(\.[\w-]+)*@(([A-Za-z\d][A-Za-z\d-]{0,61}[A-Za-z\d]\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])/;
alert(pStr.value)
	if (reEmail.test(pStr)) {
		return true;
	} else if (pStr != null && pStr != "") {
		alert(pStr + " NÃO é um endereço de e-mail válido!");
		return false;
	}
}

/* Verifica se um campo de telefone */
function isPhone(tel) {
	var phone = document.getElementById(tel);

	var digits = phone.value.replace(/[^0-9]/ig, '');
	if (!digits) {
		phone.value = '';
		return false;
	}
	if (digits.length == 10) {
		phone.value = '(' + digits.substring(0, 2) + ') ' +
		digits.substring(2, 6) + '-' +
		digits.substring(6, 10);
	} else {
		phone.value = '(21) ' +
		digits.substring(0, 4) + '-' +
		digits.substring(4, 8);
	}
	return true;
}

/* Verifica se o campo fornecido esta vazio */
function isEmpty(pStrText) {
	var	len = pStrText.length;
	var pos;
	var vStrnewtext = "";

	for (pos=0; pos<len; pos++){
		if (pStrText.substring(pos, (pos+1)) != " "){
			vStrnewtext = vStrnewtext + pStrText.substring(pos, (pos+1));
		}
	}

	if (vStrnewtext.length > 0)
		return false;
	else
		return true;
}

/* Verifica o CPF / CNPJ */
function isCPFCNPJ(cpfcnpj,pType){
	var field = document.getElementById(cpfcnpj);
	var campo = field.value;

	if( isEmpty( campo ) ){return false;}

	var campo_filtrado = "", valor_1 = " ", valor_2 = " ", ch = "";
	var valido = false;
	for (var i = 0; i < campo.length; i++){
		var ch = campo.substring(i, i + 1);
		if (ch >= "0" && ch <= "9"){
			campo_filtrado = campo_filtrado.toString() + ch.toString()
			valor_1 = valor_2;
			valor_2 = ch;
		}
		if ((valor_1 != " ") && (!valido)) valido = !(valor_1 == valor_2);
	}
	if (!valido) campo_filtrado = "12345678912";

	if (campo_filtrado.length < 11){
		for (i = 1; i <= (11 - campo_filtrado.length); i++){campo_filtrado = "0" + campo_filtrado;}
	}

	if(pType <= 1){
		if ( ( campo_filtrado.substring(9,11) == checkCPF( campo_filtrado.substring(0,9) ) ) && ( campo_filtrado.substring(11,12)=="") ){
			field.value = campo_filtrado.substring(0, 3) + '.' +
			campo_filtrado.substring(3, 6) + '.' +
			campo_filtrado.substring(6, 9) + '-' +
			campo_filtrado.substring(9, 11);
			return true;
		}
	}

	if((pType == 2) || (pType == 0)){
		if (campo_filtrado.length >= 14){
			if ( campo_filtrado.substring(12,14) == checkCNPJ( campo_filtrado.substring(0,12) ) ){ return true;}
		}
	}

	return false;
}

/* Verifica o CNPJ fornecido */
function checkCNPJ(vCNPJ){
	var mDigito;
	var mControle = "";
	var aTabCNPJ = new Array(5,4,3,2,9,8,7,6,5,4,3,2);
	for (var i = 1 ; i <= 2 ; i++){
		var mSoma = 0;
		for (var j = 0 ; j < vCNPJ.length ; j++)
		mSoma = mSoma + (vCNPJ.substring(j,j+1) * aTabCNPJ[j]);
		if (i == 2 ) mSoma = mSoma + ( 2 * mDigito );
		mDigito = ( mSoma * 10 ) % 11;
		if (mDigito == 10 ) mDigito = 0;
		var mControle1 = mControle ;
		mControle = mDigito;
		aTabCNPJ = new Array(6,5,4,3,2,9,8,7,6,5,4,3);
	}
	return( (mControle1 * 10) + mControle );
}

/* Verifica o CPF fornecido */
function checkCPF(vCPF){
	var mControle = "";
	var mContIni = 2, mContFim = 10, mDigito = 0;
	for (var j = 1 ; j <= 2 ; j++){
		var mSoma = 0;
		for (var i = mContIni ; i <= mContFim ; i++)
			mSoma = mSoma + (vCPF.substring((i-j-1),(i-j)) * (mContFim + 1 + j - i));
		if (j == 2 ) mSoma = mSoma + ( 2 * mDigito );
		mDigito = ( mSoma * 10 ) % 11;
		if (mDigito == 10) mDigito = 0;
		var mControle1 = mControle;
		mControle = mDigito;
		mContIni = 3;
		mContFim = 11;
	}
	return( (mControle1 * 10) + mControle );
}

/* Verfica o CEP */
function isCEP(){
	var cep = document.getElementById("cep");
	var CEP = cep.value;
	var valorCEP = isNaN(CEP);//verifica se só possui números
	if(valorCEP || CEP.length!=8){
		alert("O CEP possui 8 dígitos.\nDigite somente números.");
		cep.value = "";
		cep.focus();
	} else {
		var prefixo = CEP.substr(0, 5);
		var sufixo = CEP.substr(5, 7);
		cep.value = prefixo+"-"+sufixo+"";
	}
}

/* Verifica se o campo so contem numeros */
function isNumber(numero){
	var CaractereInvalido = false;
	for (var i=0; i < numero.length; i++){
		var Caractere = numero.charAt(i);
		if(Caractere != "." && Caractere != "," && Caractere != "-"){
			if (isNaN(parseInt(Caractere))) CaractereInvalido = true;
		}
	}
	return !CaractereInvalido;
}

