function getRequest ()
{
    var xmlhttp = null;
    
    try
    {
        xmlhttp = new XMLHttpRequest();
    }
    catch(ee)
    {
        try
        {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e)
        {
            try
            {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(E)
            {
                xmlhttp = false;
            }
        }
    }
    
    return xmlhttp;
}
function abreAjax(pg, local ,loader)
{	
    var pagina = new Ajax2();
	if(loader)
		pagina.setLoader($(loader));
		
	pagina.loadContent(pg,$(local));
}
//Retorna o objeto
function $(id) { 
	try { 
		var a = document.getElementById(id); 
	} catch(e) { 
		alert("Erro encontrado: "+e); 		
	} 
	return 	a;
}


var pFone = new Array();
var cFone = new Array();
function addCtFone (ct,pos) {
	pFone.push(pos)	
	cFone.push(ct);
}
function send() {
  abreJanela('', '600', '500', 'atendimento');
  $('form_atend').submit();
}
function viewMaskFone (str) {
	var aux = "";
	var save = "";
	var save = str;
	var ped = "";	
	
	for(i=0;i<cFone.length;i++) {
		aux = "";
	
		for(j=0;j <save.length;j++) {
		
			ped = save.substr(j,1);
			
			if(j == pFone[i]) {
				if(ped != cFone[i]) {
					aux += cFone[i];
				}
			}		
			
			
			
			
			aux += save.substr(j,1);														
	
		}
		save = aux;

	}
	
	return save;
}
function maskFone (e,ob) {
	if(window.event) // IE
	{
		keynum = e.keyCode;
	}
	else if(e.which) // Netscape/Firefox/Opera
	{
		keynum = e.which;
	} else {
		keynum = e.keyCode;
	}
	keychar = String.fromCharCode(keynum);
	numcheck = /\d/;

	
	ob.value = viewMaskFone(ob.value);

	var s = false;
	
	if(!isNaN(parseInt(keychar))) {
		s = true;
	} 
	
	if(keynum == 8 || keynum == 13 || keynum == 32) {
		s = true;
	}
	
	return s;
}

addCtFone("(",0);	
addCtFone(")",3);
addCtFone("-",8);

var campos = Array("nome","cidade","estado","telefone","mensagem");
var pronomes = Array("seu","sua","seu","seu","sua");

function validaCampo (campo) {
	var obj = $(campo);
	if(obj.value.length<2)	
		return false;
	else
		return true;
}

function msg (i) {
	$('erro_contato').innerHTML = "<span style='color:red'>Digite "+pronomes[i]+" "+campos[i]+"!</span>";
	$(campos[i]).focus();
}

function validaFormContato () {
	var aux = true;
	for(i=0;i<campos.length;i++) {
		if(!validaCampo(campos[i])) { aux = false; msg(i); return false; }
	}
	if(aux==true) {
		$('erro_contato').innerHTML = "Enviando mensagem...";
		$('form_contato').submit();
	}
}

function validaEmail(objCampo) {
	//Verifica se existe o arroba
    if(objCampo.value.indexOf ('@') < 1)
	{
        $('erro_novidades').innerHTML = "E-mail iválido!&nbsp;";
        objCampo.focus();
        return false;
    }
	
    objCampo.value.indexOf ('@');
    if(objCampo.value.substring((objCampo.value.indexOf ('@') + 1), objCampo.value.length).indexOf ('@') >= 0)
	{
       $('erro_novidades').innerHTML = "E-mail iválido!&nbsp;";
        objCampo.focus();
        return false;
    }
	
	//Verifica se existe o ponto
    if(objCampo.value.indexOf ('.') < 5)
	{
        $('erro_novidades').innerHTML = "E-mail iválido!&nbsp;";
        objCampo.focus();
        return false;
    }
	
    if((objCampo.value.substring((objCampo.value.indexOf ('.') + 1), objCampo.value.length).length) < 3)
	{
        $('erro_novidades').innerHTML = "E-mail iválido!&nbsp;";
        objCampo.focus();
        return false;
    }
	
    return true;
}

function novidades () {	
	var nome = $('form_nome');
	var email = $('form_email');
	
	if(nome.value.length<=2) {
		nome.focus();
		$('erro_novidades').innerHTML = "Nome iválido!&nbsp;";
		return false;
	}
	if(validaEmail(email)) {
		$('erro_novidades').innerHTML = "<span style='color:green'>processando...</span>";
		abreAjax("receba_novidades.php?name="+nome.value+"&mail="+email.value, "div_novidades");
	}
}
function _back () {
	abreAjax("novidades.php", "div_novidades");
}
function abreJanela(url, width, height, janela) {

   var width = width;
   var height = height;
   
   var left = (screen.width - width)/2;
   var top = (screen.height - height)/2;
   window.open(url,janela, 'width='+width+', height='+height+', top='+top+', left='+left+', scrollbars=yes, status=no, toolbar=no, location=no, directories=no, menubar=no, resizable=no, fullscreen=no');
}

function abrePolitica () {
	abreJanela("politica.php", 500, 600, "_politica");
}
function simular () {
	//Validando os tipos
	var obj = document.getElementById('tipo');

	if(obj.value == 0) {
		alert('Selecione o tipo do bem desejado!');
		return false;
	}
		
	//Validando o valor
	if($('valor').value=="") {
		alert("Informe o valor!");	
		$('valor').focus();
		return false;
	}
	
	//	abreJanela
	$("form_simulate").submit();
}
function addParcelas (valor) {
	if(valor=="carros"){ var inicio = 18; var fim = 80 - inicio; }
	else if(valor=="motos") { var inicio = 12; var fim = 72 - inicio; }
	else { var inicio = 100; var fim = 180 - inicio; }
	
	var sel = document.createElement("select");
	sel.className = "parcelas";
	sel.name = "parcelas";
	sel.id = "parcelas";
	
	
	if(valor != 'servicos')
	{
		for(i=0;i<fim+1;i++) {
			sel.options[i] = new Option(inicio++, inicio-1);
		}
	} else {
		sel.options[0] = new Option(24, 24);
		sel.options[1] = new Option(36, 36);
	}
	
	$('select_parcelas').innerHTML = "";
	$('select_parcelas').appendChild(sel);
}
function selectDuvida (id) {
	if(id!=0) {
		abreAjax("verDuvidas.php?id="+id, "div_duvidas", $('carregador_d'));
	}
}
function con (id) {
	var divs = document.getElementsByName("div_con");
	for(i=0;i<divs.length;i++) {		
		if(id!=divs[i].value) {
			$('consorcio_'+divs[i].value).style.display = "none";
			$('link_'+divs[i].value).className = "linkBranco";
		} else {
			$('consorcio_'+divs[i].value).style.display = "block";
			$('link_'+divs[i].value).className = "con";
		}
	}
}
function abreAtendimento () {
	   var width = 430;
	   var height = 450;
	   var left = (screen.width - width)/2;
	   var top = (screen.height - height)/2;;   
	   window.open("../atendimento/",'atendimento', 'width='+width+', height='+height+', top='+top+', left='+left+', scrollbars=no, status=no, toolbar=no, location=no, directories=no, menubar=no, resizable=no, fullscreen=no');
}