var valorTipo = -1
var tipos = new Array();

emailRE = new RegExp("^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$");
function validaEmail() {
  var email = document.getElementById("email").value;
  if (emailRE.test(email)) {
	  return true;
  }
  return false;
}

function show(div) {
	var temp;
	if (div == 1) {
		temp = document.getElementById('veiculo');
		temp.style.display = '';
		temp.innerHtml = 1;

		temp = document.getElementById('acessorio');
		temp.style.display = 'none';
	} else {
		temp = document.getElementById('acessorio');
		temp.style.display = '';

		temp = document.getElementById('veiculo');
		temp.style.display = 'none';
	}
}

function isNumber(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789.,-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
}

function atualizar(tipo) {
   if (tipo > 0) {
		valorTipo = tipo - 1;
		atualizarMarcas();
		atualizarMotores();
		atualizarAcessorios(tipo);
   }
}

function atualizarMarcas() {
   var marca = document.getElementById('marca');
   var modelo = document.getElementById('modelo');
   
   if (marca.options.length > 1) {
      for (var i = marca.options.length - 1; i > 0; i--) {
         marca.options[i] = null;
      }

		marca.selectedIndex = 0;
   }

   if (modelo.options.length > 1) {
      for (var i = modelo.options.length - 1; i > 0; i--) {
         modelo.options[i] = null;
      }
	  modelo.selectedIndex = 0;
   }

	for (var i = 0; i < list[valorTipo].length; i++) {
		marca.options[i + 1] = new Option(list[valorTipo][i][1], list[valorTipo][i][0]);
   }
}


function atualizarModelos() {
   var marca = document.getElementById('marca').selectedIndex - 1;
   var modelo = document.getElementById('modelo');

   if (marca < 0) {
      return;
   }

   if (modelo.options.length > 1) {
      for (var i = modelo.options.length - 1; i > 0; i--) {
         modelo.options[i] = null;
      }
   }

   for (var i = 0; i < list[valorTipo][marca][2].length / 2; i++) {
      modelo.options[i + 1] = new Option(list[valorTipo][marca][2][i * 2 + 1], list[valorTipo][marca][2][i * 2]);
   }
}

function atualizarMotores(tipo) {
   var motor = document.getElementById('motor');

   if (motor == null)
   {
		return;
   }
   
   if (motor.options.length > 1) {
      for (var i = motor.options.length - 1; i > 0; i--) {
         motor.options[i] = null;
      }

		motor.selectedIndex = 0;
   }
   
   if (motor.options.length > 1) {
      for (var i = motor.options.length - 1; i > 0; i--) {
         motor.options[i] = null;
      }
	  motor.selectedIndex = 0;
   }

  for (var i = 0; i < list2[valorTipo].length / 2; i++) {
	 motor.options[i + 1] = new Option(list2[valorTipo][i * 2 + 1], list2[valorTipo][i * 2]);
   }
}

function atualizarAcessorios(tipoSelected) {
	var tipo = document.getElementById("tipo").options.length - 1;

	var temp;

	if (tipos.length == 0) {
		tipos[0] = tipoSelected
	} else {
		var found = false;
		for (i = 0; i <= tipos.length; i++) {
			if (tipos[i] == tipoSelected) {
				found = true;
			}
		}
		if (!found)	{
			tipos[tipos.length] = tipoSelected;
		}
	}
	for (i = 0; i <= tipos.length; i++) {
		temp = document.getElementById("acessorio" + tipos[i]);
		if (temp) {
			temp.style.display = "none";
		}
	}

	temp = document.getElementById("acessorio" + tipoSelected);
	if (temp)
	{
		temp.style.display = "";
	}

}

function doValidaSendForm() {
	var campo;

	campo = document.getElementById('texto');
	if (campo.value.length == 0) {
		alert("Digite um texto");
		return false;
	}

	campo = document.getElementById('nome');
	if (campo.value.length == 0) {
		alert("Digite um nome");
		return false;
	}


	campo = document.getElementById('email');
	if (campo.value.length == 0) {
		alert("Digite um e-mail");
		return false;
	}

	if (!validaEmail()) {
		alert("E-mail inválido");
		return false;
	}

	campo = document.getElementById('ddd');
	if (campo.value.length == 0) {
		alert("Digite um ddd");
		return false;
	}

	if (!isNumber(campo.value)) {
		alert("Digite apenas números no DDD");
		return false;
	}

	campo = document.getElementById('telefone');
	if (campo.value.length == 0 || campo.value.length < 7) {
		alert("Digite um telefone válido");
		return false;
	}

	if (!isNumber(campo.value)) {
		alert("Digite apenas números no telefone");
		return false;
	}
	return true;
}