try {
  xmlhttp = new XMLHttpRequest();
} catch(ee) {
  try{
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  }catch(e){
    try{
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }catch(E){
        xmlhttp = false;
    }
  }
}
fila=[]
ifila=0
function Ajax(url,id) 
{
	fila[fila.length]=[id,url];
	if((ifila+1) == fila.length)
		ajaxRun();
}
function ajaxRun() 
{
	xmlhttp.open("GET",fila[ifila][1],true);
	xmlhttp.onreadystatechange=function() 
	{
		if(xmlhttp.readyState==4)
		{
			retorno = unescape(xmlhttp.responseText)
			document.getElementById(fila[ifila][0]).innerHTML=retorno;
			document.getElementById(fila[ifila][0]).style.display = "";
			ifila++
			if(ifila<fila.length) setTimeout("ajaxRun()",10)
		}
	}
	xmlhttp.send(null)
}
function loginapls(pagina,form_id) 
{
	var string = '';
	$("#" + form_id + " input[type='hidden']").each( function() { 
		string += this.name + "=" + this.value;
	});
	$("#" + form_id + " input[type='text']").each( function() {
		string += "&" + this.name + "=" + this.value;
	});
	$("#" + form_id + " input[type='password']").each( function() {
		string += "&" + this.name + "=" + this.value;
	});
	$.ajax({
		type	: "POST",
		cache	: false,
		url		: pagina,
		data	: string,
		success : function(html) {
			var trecho = html.slice(0,8);
			if(trecho == 'Desculpe')
				$("#campo_info_log").html(html);
			else
				window.location = html;
		}
	});
}
function Mudacampos(Marcado)
{
	if( Marcado == 'tipo_novo' )
	{
		$("#tipo_seminovo").fadeOut(200,function(){
			$("#tipo_novo").fadeIn(200);
		});
	}
		else if( Marcado == 'tipo_seminovo' )
	{
		$("#tipo_novo").fadeOut(200,function(){
			$("#tipo_seminovo").fadeIn(200);
		});		
	}
}
function ImageBox(imagem){

	var html;
	html = '<a rel="example_group" href="'+imagem+'" ><img alt="" src="'+imagem+'" /></a><script>$(document).ready(function() {	$("a[rel=example_group]").fancybox({\'transitionIn\':\'none\',\'transitionOut\':\'none\',\'titlePosition\':\'over\'});});</script>';
	$("#foto_produto").fadeOut(200,function(){
		$("#foto_produto").html(html);
		$("#foto_produto").fadeIn(200);
	});
}
function MontaForm(id){
	if(id == '2')
		$("#modelos_form").fadeOut(1000,function(){$("#marcas_form").fadeIn(1000);});
	else
		$("#marcas_form").fadeOut(1000,function(){$("#modelos_form").fadeIn(1000);});
}

function Candidatos(pagina,id){
	var resposta = '';
	resposta = '<a href="javascript:;" class="btn dinbla"><span>Já sou candidato!</span></a>';
	$.ajax({
		type: "GET",
		url: pagina,
		cache: false,
		success: function(html) {
			var trecho = html.slice(0,6);
			$("#"+id).html(resposta);
			$.fancybox(html);
		}
	});
}

function CloseRd(carro)
{
	//$.fancybox.close();
	window.location = carro;	
}

function compre_aqui(pagina,form_id){
	var string = '';
	var camposBol = true;

	$("#" + form_id + " input[type='hidden']").each( function() { 
		string += "&" + this.name + "=" + this.value;
	});
	$("#" + form_id + " input[type='text']").each( function() {
		if( this.value != '' || ( 
				this.name == 'celular_cnt' || this.name == 'data_cnt' || this.name == 'hora_cnt'
			) )
		{
			string += "&" + this.name + "=" + this.value;
		}
			else
		{
			camposBol = false;
			$("#"+this.name).removeClass('texto');
			$("#"+this.name).addClass('obrigatorio');
			$("#campo_"+this.name).html('* Obrigatório');
		}
	});

	$("#" + form_id + " select").each(function() {
		if(this.value != 0)
		{
			string += "&" + this.name + "=" + this.value;
		}
			else
		{
			camposBol = false;
			$("#campo_"+this.name).html('* Obrigatório');
		}
	});

	$("#" + form_id + " input[type='radio']").each(function(){
		if(this.checked)
		{
			string += "&" + this.name + "=" + this.value;
		}
	});
	
	if(camposBol)
	{
		$.ajax({
			type: "POST",
			url: pagina,
			data: "objid=novocontato"+string,
			cache: false,
			success: function(html) {
				var trecho = html.slice(0,6);
				if(trecho != '<div i')
					$("#campo_info").html(html);
				else
					$.fancybox(html);
			}
		});
	}
}

function test_drive(pagina,form_id){
	var string = '';
	var camposBol = true;

	$("#" + form_id + " input[type='hidden']").each( function() { 
		string += "&" + this.name + "=" + this.value;
	});
	$("#" + form_id + " input[type='text']").each( function() {
		if( this.value != '' || ( 
				this.name == 'celular_test' || this.name == 'data_test' || this.name == 'hora_test'
			) )
		{
			string += "&" + this.name + "=" + this.value;
		}
			else
		{
			camposBol = false;
			$("#"+this.name).removeClass('texto');
			$("#"+this.name).addClass('obrigatorio');
			$("#cp_"+this.name).html('* Obrigatório');
		}
	});

	$("#" + form_id + " select").each(function() {
		if(this.value != 0)
		{
			string += "&" + this.name + "=" + this.value;
		}
			else
		{
			camposBol = false;
			$("#cp_"+this.name).html('* Obrigatório');
		}
	});

	$("#" + form_id + " input[type='radio']").each(function(){
		if(this.checked)
		{
			string += "&" + this.name + "=" + this.value;
		}
	});

	if(camposBol)
	{
		$.ajax({
			type: "POST",
			url: pagina,
			data: "objid=novocontato"+string,
			cache: false,
			success: function(html) {
				var trecho = html.slice(0,6);
				if(trecho != '<div i')
					$("#campo_info_test").html(html);
				else
					$.fancybox(html);
			}
		});
	}
}




function agenda_revisao(pagina, form_id){
	
	var string = '';
	var camposBol = true;
	var Interres_Text_select = true;
	var valida_texto = false;

	$("#" + form_id + " input[type='hidden']").each( function() { 
		string += "&" + this.name + "=" + this.value;
	});

	$("#" + form_id + " input[type='text']").each( function() {
		if( this.value != '' || ( 
				this.name == 'celular_test' || this.name == 'data_test' || this.name == 'hora_test' ||
				this.name == 'interesse_ag' 
			) )
		{		
			if( valida_texto )
			{
				if( this.name == 'interesse_ag' )
				{
					if( this.value != ''  )
						Interres_Text_select = false;
					else
						$("#cp_"+this.name).html('* Obrigatório');
				}
			}
			
			string += "&" + this.name + "=" + this.value;
		}
			else
		{
			camposBol = false;
			$("#"+this.name).removeClass('texto');
			$("#"+this.name).addClass('obrigatorio');
		}
	});

	if( Interres_Text_select )
	{
		$("#" + form_id + " select").each(function() {
			if(this.value != 0)
			{
				string += "&" + this.name + "=" + this.value;
			}
				else
			{
				camposBol = false;
				$("#cp_"+this.name).html('* Obrigatório');
			}
		});
	}
	
	if(camposBol)
	{
		$.ajax({
			type: "POST",
			url: pagina,
			data: "objid=novocontato"+string,
			cache: false,
			success: function(html) {
				var trecho = html.slice(0,6);
				if(trecho != '<div i')
					$("#campo_info_ag").html(html);
				else
					$.fancybox(html);
			}
		});
	}
}


function agenda_ligacao(pagina,form_id){
	var string = '';
	var camposBol = true;
	var Interres_Text_select = true;
	var valida_texto = false;

	$("#" + form_id + " input[type='hidden']").each( function() { 
		string += "&" + this.name + "=" + this.value;
	});

	$("#" + form_id + " input[type='radio']").each(function(){
		if(this.checked)
		{
			if( this.value == 2 )
				valida_texto = true;
			else
				valida_texto = false;

			string += "&" + this.name + "=" + this.value;
		}
	});
	$("#" + form_id + " input[type='text']").each( function() {
		if( this.value != '' || ( 
				this.name == 'celular_test' || this.name == 'data_test' || this.name == 'hora_test' ||
				this.name == 'interesse_ag' 
			) )
		{
			if( valida_texto )
			{
				if( this.name == 'interesse_ag' )
				{
					if( this.value != ''  )
						Interres_Text_select = false;
					else
						$("#cp_"+this.name).html('* Obrigatório');
				}
			}
			string += "&" + this.name + "=" + this.value;
		}
			else
		{
			camposBol = false;
			$("#"+this.name).removeClass('texto');
			$("#"+this.name).addClass('obrigatorio');
		}
	});

	if( Interres_Text_select )
	{
		$("#" + form_id + " select").each(function() {
			if(this.value != 0)
			{
				string += "&" + this.name + "=" + this.value;
			}
				else
			{
				camposBol = false;
				$("#cp_"+this.name).html('* Obrigatório');
			}
		});
	}
	
	if(camposBol)
	{
		$.ajax({
			type: "POST",
			url: pagina,
			data: "objid=novocontato"+string,
			cache: false,
			success: function(html) {
				var trecho = html.slice(0,6);
				if(trecho != '<div i')
					$("#campo_info_ag").html(html);
				else
					$('#ANCHOR-1').html(html);
			}
		});
	}
}
function receber_novidades(pagina,form_id){
	var string = '';

	$("#" + form_id + " input[type='hidden']").each( function() { 
		string += "&" + this.name + "=" + this.value;
	});
	$("#" + form_id + " input[type='text']").each( function() {
		string += "&" + this.name + "=" + this.value;
	});


	$.ajax({
		type		: "POST",
		cache	: false,
		url		: pagina,
		data: "objid=novocontato"+string,
		success: function(html) {
			var trecho = html.slice(0,6);
			if(trecho != '<div i')
				$("#campo_info_nv").html(html);
			else
				$.fancybox(html);
		}
	});
}
function LimpaForm(form_id){
	$("#" + form_id + " input[type='text']").each( function() {this.value = '';});
}
function CarregaBox(){
	var string = '';
	string = $("#campo_mail").val();

	$(document).ready(function() {
        $("#various5").fancybox().trigger('click');
		$("#email_user").val(string);
    });
}

function nova_senha(pagina,form_id){
	var string = '';

	$("#" + form_id + " input[type='hidden']").each( function() { 
		string += "&" + this.name + "=" + this.value;
	});
	$("#" + form_id + " input[type='text']").each( function() {
		if(this.value != 0)
		{
			string += "&" + this.name + "=" + this.value;
			camposBol = true;
		}
			else
		{
			camposBol = false;
			$("#cp_"+this.name).html('* Obrigatório');
		}
	});
	
	if( camposBol )
	{
		$.ajax({
			type	: "POST",
			cache	: false,
			url		: pagina,
			data: "objid=novasenha"+string,
			success: function(html) {
				var trecho = html.slice(0,6);
				if(trecho != '<div i')
					$("#campo_info_nv").html(html);
				else
					$.fancybox(html);
			}
		});
	}
}

$(document).ready(function() {	
	var id = '#dialog';
	var maskHeight = $(document).height();
	var maskWidth = $(window).width();

	$('#mask').css({'width':maskWidth,'height':maskHeight});
	$('#mask').fadeIn(50);

	//var winH = $(window).height();
	var winH = '400';
	//var winW = $(window).width();
	var winW = '850';
              
	$(id).css('top',  winH/2-$(id).height()/2);
	$(id).css('left', winW/2-$(id).width()/2);
	$(id).fadeIn(50); 
	
	$('.window .close').click(function (e) {
		e.preventDefault();
		$('#mask').hide();
		$('.window').hide();
	});
	
	$('#mask').click(function () {
		$(this).hide();
		$('.window').hide();
	});	
});

