var Ver4 = parseInt(navigator.appVersion) >= 4
var IE4 = ((navigator.userAgent.indexOf("MSIE") != -1) && Ver4)
var id_sel, img_sel;
var block = "formulario";

function open_veiculo(id) {
	var ww = 700;var center_y = (screen.height/2)-(ww/2);//640
	var wh = 575;var center_x = (screen.width/2)-(wh/2);//600
	/*document.location="#conteudo";
	ajaxHTML("conteudo","detalhe.php?id="+id+"&vopcoes=1");
	ContBack = document.getElementById("conteudo").innerHTML;*/ //armazena o conteúdo da página anterior a visualização
	MM_openBrWindow("detalhe.php?id="+id,"detalhe","scrollbars=yes, width="+ww+", height="+wh+", left="+center_x+",top="+center_y+"\"");
}
function open_veiculo2(id) {
//	var ww = 640;var center_y = (screen.height/2)-(ww/2);
	var ww = 700;var center_y = (screen.height/2)-(ww/2);
	var wh = 575;var center_x = (screen.width/2)-(wh/2);
	MM_openBrWindow("http://www.carrobauru.com.br/2006/detalhe.php?id="+id+"&externo=1","detalhe","scrollbars=yes, width="+ww+", height="+wh+", left="+center_x+", top="+center_y+"\"");
}
function center_obj(div_id) {
	var objDiv = document.getElementById(div_id).style;
	objDiv.left = (screen.availLeft/2)-(640/2);
	objDiv.top  = (screen.availTop/2)-(600/2);
}
function foto_ampliada(id, img) {
	var url = "foto_ampliada.php?veiculo="+id+"&img="+img;
	var win = "FotoGrande";
	var params = "height=535, width=660, top=100, left=100, toolbar=no, menubar=no, location=no, resizable=yes, scrollbars=no, status=no";
	MM_openBrWindow(url, win, params);
	return false;
}

function mostrar_div(div_id) { 
	document.getElementById(div_id).style.visibility = "visible"; 
	document.getElementById(div_id).style.display = "list-item";
}
function esconder_div(div_id) { 
	document.getElementById(div_id).style.visibility = "hidden"; 
	document.getElementById(div_id).style.display = "none";
}
function getPropertyDiv(div_id, prop) { return document.getElementById(div_id).style[prop]; }

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

function DigitoCPFCNPJ(numCIC) {
	numCIC = numCIC.replace(/\D/g,'');
	var numDois = numCIC.substring(numCIC.length-2, numCIC.length);
	var novoCIC = numCIC.substring(0, numCIC.length-2);
	switch (numCIC.length){
		case 11 :
			numLim = 11;
			break;
		case 14 :
			numLim = 9;
			break;
		case 15 :
			numLim = 9;
			break;
		default : return false;
	}
	var numSoma = 0;
	var Fator = 1;
	for (var i=novoCIC.length-1; i>=0 ; i--) {
		Fator = Fator + 1;
		if (Fator > numLim) {
			Fator = 2;
		}
		numSoma = numSoma + (Fator * Number(novoCIC.substring(i, i+1)));
	}
	numSoma = numSoma/11;
	var numResto = Math.round( 11 * (numSoma - Math.floor(numSoma)));
    if (numResto > 1) {
		numResto = 11 - numResto;
    }
    else {
		numResto = 0;
    }
    var numDigito = String(numResto);
    novoCIC = novoCIC.concat(numResto);
	numSoma = 0;
	Fator = 1;
	for (var i=novoCIC.length-1; i>=0 ; i--) {
		Fator = Fator + 1;
		if (Fator > numLim) {
			Fator = 2;
		}
		numSoma = numSoma + (Fator * Number(novoCIC.substring(i, i+1)));
	}
	numSoma = numSoma/11;
	numResto = numResto = Math.round( 11 * (numSoma - Math.floor(numSoma)));
    if (numResto > 1) {
		numResto = 11 - numResto;
    }
    else {
		numResto = 0;
    }
	numDigito = numDigito.concat(numResto);
	
	if (numDigito == numDois) {
		return true;
	}
	else {
		return false;
	}
}
/* VALIDA CPFS SEM TRACO E PONTOS */
function checkCPF() {
	var i = 0;
	var n_checked = 0;
	var error = 0;
	var error_message = "";
	var cpf = document.cadastro.cpf.value;
	
	if (cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999") {
		error_message = "Por favor digite um cpf válido!\n";
		error = 1;
	}
	soma = 0;
	for (i=0; i<9; i++) {
		soma += parseInt(cpf.charAt(i))*(10-i);
	}
	resto = 11-(soma%11);
	if (resto == 10 || resto == 11) {
		resto = 0;
	}
	if (resto != parseInt(cpf.charAt(9))) {
		error_message = "Por favor digite um cpf válido!\n";
		error = 1;
	}
	soma = 0;
	for (i=0; i<10; i++) {
		soma += parseInt(cpf.charAt(i))*(11-i);
	}
	resto = 11-(soma%11);
	if (resto == 10 || resto == 11) {
		resto = 0;
	}
	if (resto != parseInt(cpf.charAt(10))) {
		error_message = "Por favor digite um cpf válido!\n";
		error = 1;
	}
	if (error == 1) {
		alert(error_message);
		return false;
	} else {
		return true;
	}
}
function trim(str){return str.replace(/^\s*|\s*$/g,"");}
function Expande(d) {
	with (document.getElementById(d)) {
		if (style.display=='none') {
			style.display='list-item';
		} else {
			style.display='none';
		}
	}
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function formatar(src, mask) 
{
  var i = src.value.length;
  var saida = mask.substring(0,1);
  var texto = mask.substring(i);
  if (texto.substring(0,1) != saida) 
  {
	src.value += texto.substring(0,1);
  }
}
// onKeyPress="return numeros(event)"
function numeros(e) {
	key = 0;
	if (window.event) {
		key = e.keyCode;
	} else if (e.which) {
		key = e.which;
	}
	if (key!=8 || key < 48 || key > 57) return (((key > 47) && (key < 58)) || (key==8));
	return true;
}
function setcookie(name,value,acao){var curCookie=name+"="+escape(value);if (acao==true){document.cookie=curCookie;}else{document.cookie=name+"="+";expires=Thu,01-Jan-70 00:00:01 GMT";}}
function getcookie(name){
	var dc=document.cookie;
	var prefix=name+"=";
	var begin=dc.indexOf(";"+prefix);
	if (begin==-1){
		begin=dc.indexOf(prefix);
		if (begin!=0) return null;
	} else begin+=2;
	var end=document.cookie.indexOf(";",begin);
	if (end==-1) end=dc.length;
	return unescape(dc.substring(begin+prefix.length,end));
}
function deletecookie(name){document.cookie=name+"="+";expires=Thu,01-Jan-70 00:00:01 GMT";}
function formBusca(){
	_cidade2=document.getElementById('CIDADE2');
	_tipo=document.getElementById('TIPO');
	_marca=document.getElementById('MARCA');
	_modelo=document.getElementById('MODELO');
	_preco=document.getElementById('PRECO');
	_regioes=document.getElementById('regioes');
	_ano1=document.getElementById('ANO1');
	_ano2=document.getElementById('ANO2');
	qs_form = "?TIPO_BUSCA=srapida&CIDADE="+_cidade2.value+"&REGIOES="+_regioes.value+"&ANO1="+_ano1.value+"&ANO2="+_ano2.value;
	if ( _preco.selectedIndex != 0) {
		qs_form += "&PRECO="+_preco.value+"&TIPO="+_tipo.value+"&MARCA="+_marca.value+"&MODELO="+_modelo.value+"&ANO1="+_ano1.value+"&ANO2="+_ano2.value;
	} else {
		if (_tipo.selectedIndex==0) { alert('Favor selecionar um Tipo!'); return false; }
		if (_marca.selectedIndex==0) { alert('Favor selecionar uma Marca!'); return false; }
		if (_modelo.selectedIndex==0) { alert('Favor selecionar um Modelo!'); return false; }
		qs_form += "&TIPO="+_tipo.value+"&MARCA="+_marca.value+"&MODELO="+_modelo.value+"&PRECO="+_preco.value+"&ANO1="+_ano1.value+"&ANO2="+_ano2.value;
	}
	ajaxHTML('conteudo','busca.inc.php'+qs_form);
	return true;
}
function fBuscaSRapida(){
	_veiculo=document.getElementById('VEICULO');
	_rregioes=document.getElementById('rregioes');
	if (_veiculo.value=='') { alert('Favor preencher o campo Veículo!'); _veiculo.focus(); return false;}
	ajaxHTML('conteudo','busca.inc.php?REGIOES='+_rregioes.value+'&TIPO_BUSCA=srapida&VEICULO='+_veiculo.value);
	return true;
}
function formBuscaRev(){
	_revenda=document.getElementById('REVENDA');
	if (_revenda.selectedIndex==0) { alert('Favor selecionar uma Revenda!'); return false;}
	if (_revenda.value==0) { alert('Favor selecionar uma Revenda, não uma CIDADE!'); return false;}
	ajaxHTML('conteudo','busca.inc.php?CIDADE='+_cidadeId+'&REVENDA='+_revenda.value);
	return true;
}
function formBuscaCidade(){
	_cidade=document.getElementById('CIDADE');
	if(_cidade.selectedIndex>0) {
		document.location='http://www.'+_cidade.value+'/2006/';
		return true;
	} else {
		alert('Favor selecionar uma Cidade!');
	}
	return false;
}
//Funções tooltip
/*
	var ns4 = document.layers;
	var ns6 = document.getElementById && !document.all;
	var ie4 = document.all;
	offsetX = 0;
	offsetY = 20;
	var toolTipSTYLE="";
	function initToolTips()
	{
	if(ns4||ns6||ie4)
	  {
		if(ns4){ toolTipSTYLE = document.toolTipLayer; alert("ns4");}
		else if(ns6){ toolTipSTYLE = document.getElementById("toolTipLayer").style; alert("ns6");}
		else if(ie4){ toolTipSTYLE = document.all.toolTipLayer.style; alert("ie4");}
		if(ns4) document.captureEvents(Event.MOUSEMOVE);
		else
		{
		  toolTipSTYLE.visibility = "visible";
		  toolTipSTYLE.display = "none";
		}
		document.onmousemove = moveToMouseLoc;
	  } 
	}
	function toolTip(msg, fg, bg)
	{
	  if(msg=='') // hide
	
	  {
		if(ns4) toolTipSTYLE.visibility = "hidden";
		else toolTipSTYLE.display = "none";
	  }
	  else // show
	  {
		if(!fg) fg = "#000000";
		if(!bg) bg = "#FFFFE1";
		var content =
		'<table border="0" cellspacing="0" cellpadding="1" bgcolor="' + fg + '"><td>' +
		'<table border="0" cellspacing="2" cellpadding="1" bgcolor="' + bg + 
		'"><td align="left"><font face="verdana" color="' + fg +
		'" size="-2">' + msg +
		'&nbsp\;</font></td></table></td></table>';
		if(ns4)
		{
		  toolTipSTYLE.document.write(content);
		  toolTipSTYLE.document.close();
		  toolTipSTYLE.visibility = "visible";
		}
		if(ns6)
		{
		  document.getElementById("toolTipLayer").innerHTML = content;
		  toolTipSTYLE.display='block'
		}
		if(ie4)
		{
		  document.all("toolTipLayer").innerHTML=content;
		  toolTipSTYLE.display='block'
		}
	  }
	}
	function moveToMouseLoc(e)
	{
	  if(ns4||ns6)
	  {
		x = e.pageX;
		y = e.pageY;
	  }
	  else
	  {
		x = event.x + document.body.scrollLeft;
		y = event.y + document.body.scrollTop;
	  }
	  toolTipSTYLE.left = x + offsetX;
	  toolTipSTYLE.top = y + offsetY;
	  return true;
	}
*/