// JavaScript Document

	 
$().ready(function() {
	$("select[@name=SelectLoja]").change(function(){
		$('select[@name=SelectMarca]').html('<option value="">carregando ...</option>');
		$.post('http://www.celsomultimarcas.com.br/scripts/carrega_marcas.asp', 
			{ valor : $(this).val() }, 
			function(resposta){
				
				$('select[@name=SelectMarca]').html(resposta);
			}
			
		);
	});
});

$().ready(function() {
	$("select[@name=SelectMarca]").change(function(){
		$('select[@name=SelectModelo]').html('<option value="">carregando ...</option>');
		$.post('http://www.celsomultimarcas.com.br/scripts/carrega_modelos.asp', 
			{ valor : $(this).val(), valor2 : $("select[@name=SelectLoja]").val() }, 
			function(resposta){
				$('select[@name=SelectModelo]').html(resposta);
			}
		);
	});
});


$(document).ready(function() {
	
	// Expand Panel
	$("#open").click(function(){
		$("div#panel").slideDown("slow");
	
	});	
	
	// Collapse Panel
	$("#close").click(function(){
		$("div#panel").slideUp("slow");	
	});		
	
	// Switch buttons from "Log In | Register" to "Close Panel" on click
	$("#toggle a").click(function () {
		$("#toggle a").toggle();
	});		
		
});

$(function(){
	$('#a a')
		.css( {backgroundPosition: "0px 0px"} )
		.mouseover(function(){
			$(this).stop().animate({backgroundPosition:"(0px 50px)"}, {duration:200})
		})
		.mouseout(function(){
			$(this).stop().animate({backgroundPosition:"(0px 0px)"}, {duration:200, complete:function(){
				$(this).css({backgroundPosition: "0px 0px"})
			}})
		})
});



$(document).ready(function() {

	$(".tab_content").hide();
	$("ul.tabs li:first").addClass("active").show();
	$(".tab_content:first").show();
	
	$("ul.tabs li").click(function() {
		$("ul.tabs li").removeClass("active");
		$(this).addClass("active");
		$(".tab_content").hide();
		var activeTab = $(this).find("a").attr("href");
		$(activeTab).fadeIn();
		return false;
	});

});

function showtips(Marcador, StringHTML) {
$(document).ready(function(){

$('#destaque_'+Marcador).tooltip({
		track: true,     
		delay: 0, 
		opacity: 1, 
		showURL: false, 
		fixPNG: true, 
		showBody: " - ", 
		extraClass: "pretty fancy", 
		top: 5, 
		left: 5,
		bodyHandler: function() {
			return ""+StringHTML;
		}

	});
});
}


function get_rss_feed() {
	$(document).ready(function(){	
	$("#feedContent").empty();
	$("#imagem_site").empty();
	$.get('http://www.celsomultimarcas.com.br/scripts/proxy.asp?url='+$('#diffFeeds').val(), function(d) {
	$(d).find('item').each(function() {
	var $item = $(this);
	var title = $item.find('title').text();
	var link = $item.find('link').text();
	var description = $item.find('description').text();
	var pubDate = $item.find('pubDate').text();
	var imagem =  $item.find('imagem').text();
	var html = "<div title='"+ title +"'><a href='"+ link +"' target='news_site'>"+ pubDate +" - "+ title +"</a></div>"
	$('#imagem_site').html("<img src='"+imagem+"'>");  
	$('#feedContent').append($(html));  
	});
	});
	});
}

function auto_get_rss_feed() {
	$(document).ready(function(){		
	$("#feedContent").empty();
	$.get('http://www.celsomultimarcas.com.br/scripts/proxy.asp?url=webmotors.com.br', function(d) {
	$(d).find('item').each(function() {
	var $item = $(this);
	var title = $item.find('title').text();
	var link = $item.find('link').text();
	var description = $item.find('description').text();
	var pubDate = $item.find('pubDate').text();
	var imagem =  $item.find('imagem').text();
	var html = "<div title='"+ title +"'><a href='"+ link +"' target='news_site'>"+ pubDate +" - "+ title +"</a></div>"
	$('#imagem_site').html("<img src='"+imagem+"'>");  
	$('#feedContent').append($(html)); 
	});
	});
	});
};


function showvideo(arquivo){
	$(document).ready(function(){		
		$("#videoplayer").empty();
		$.get('http://www.celsomultimarcas.com.br/scripts/videoplayer.asp?url='+ arquivo, function(d) {
		$('#videoplayer').html(d); 
		});
	});
};

function financiamento(valor1, valor2, valor3){
	$(document).ready(function(){		
		$("#financiamento").empty();
		$.get('http://www.celsomultimarcas.com.br/scripts/financiamento.asp?valor1='+ valor1+'&valor2='+ valor2 +'&valor3='+ valor3, function(d) {
		$('#financiamento').html(d); 
		});
	});
	return false;
};

function popup_financiamento(){
	window.open('http://www.celsomultimarcas.com.br/simulador.asp', '_new', 'width=700, height=580, scrollbars=yes');
	}

function login(url){
	tb_show('','http://www.celsomultimarcas.com.br/scripts/popup_login.asp?url='+ url +'&TB_iframe=true&height=300&width=650','comparativo');
}  

function valida_cadastro(){
nome = document.formCadastro.nome.value;
email = document.formCadastro.email.value;
pass = document.formCadastro.senha.value;
url = document.formCadastro.url.value;
if (nome == ""){
	document.formCadastro.nome.focus();
	alert('Necessário o preenchimento de seu NOME DE USUÁRIO\n\nVerifique e tente novamente!');
	return false;	
	}else{
		if (email == ""){
		document.formCadastro.email.focus();
		alert('Necessário o preenchimento de seu E-MAIL\n\nVerifique e tente novamente!');
		return false;
		}else{
			if (pass == ""){
			document.formCadastro.senha.focus();
			alert('Necessário o preenchimento de sua SENHA\n\nVerifique e tente novamente!');
			return false;	
			}else{
				$("div#panel").slideUp("slow");
				tb_show('','http://www.celsomultimarcas.com.br/scripts/cadastro.asp?nome='+nome+'&email='+email+'&pass='+pass+'&url='+ url +'&TB_iframe=true&height=200&width=500','log_in');
				return false;
			}
		}
	}
}


/* INICIO: Validacao de Campos*/

var objBrow,LAST_ERR_VALUE="";
var ERRO=REPET_ERR=false;
var LAST_FIELD=CURRENT_FIELD=LAST_ERR_FIELD=null;
var SZ_DATE=8,
SZ_CEP=8,
SZ_MONEY=10,
SZ_FLOAT=10,
SZ_CPF=11,
SZ_CNPJ=14,
SZ_CPF_CNPJ=SZ_CNPJ,
SZ_MONTH_YEAR=6,
SZ_TIME=4,
MAX_VALUE=9999999.99;

function bloqueiaEnter(ev) {
	if (ev.keyCode==13) {
		ev.returnValue=false;
		}
}


function removeCaracs(f,type){
	var vr=unformatField(f.value,type);
	if(f.value!=vr)f.value=vr;
	focusNetscape(f);
}

function formatType(f,tp,msgErr,adarg1,adarg2){
var vr=unformatField(f.value,tp);
LAST_FIELD=f;
var ret=isValidValue(vr,tp,adarg1,adarg2);
if(!ret){
	showError(tp,msgErr);
	return false;
}else f.value=getFmtValue((typeof ret=="boolean")?vr:ret,tp);
ERRO=false;
return true;
}
function getFmtValue(vr,tp){
	if(tp=="cpf_cnpj")tp=(vr.length <=SZ_CPF)?"cpf":"cnpj";
	if(/^(neg_)?money/.test(tp))return fmtMoney(vr,_getNDec(tp));
	else{
		switch(tp){
			case "time":return vr.slice(0,2)+":"+vr.slice(2,4); 
			case "date":return vr.getDateValue();
			case "month_year":return vr.getMonthDateValue();
			case "cep":return vr.slice(0,5)+"-"+vr.slice(5,8);
			case "uppercase":return vr.toUpperCase();
			case "cpf":return vr.slice(0,3)+"."+vr.slice(3,6)+"."+ vr.slice(6,9)+ "-"+vr.slice(9,11);
			case "cnpj":return vr.slice(0,2)+"."+vr.slice(2,5)+"."+vr.slice(5,8)+"/"+vr.slice(8,12)+"-"+vr.slice(12,14);
			default:return vr;
		}
	}
}
function fmtMoney(vr,ndec){
	var neg=vr.indexOf("-")==0;
	if(verifyMaxValue(vr)){ERRO=true;return "0,00";}
	vr=toFloat(vr,ndec);
	var vraux="",p,pDec=vr.indexOf(","),vrDec=vr.slice(pDec+1);
	for(var i=pDec;i>(neg?1:0);i--){
		p=i-pDec;
		if(i!=pDec&&(p%3==0))vraux+=".";
		vraux+=vr.charAt(i-1);
	}
	return (neg?"-":"")+invertStr(vraux)+","+vrDec;
}
function focusNetscape(f){
CURRENT_FIELD=f;
var b=brow();
if(b.isNetscape()){
	if(ERRO){LAST_FIELD.focus();ERRO=false;}
}else if(b.isIE()&& parseInt(b.getMajorver(),10)>4)if(f.select)f.select();
}

function unformatField(valor,tipo){
	var vr=(typeof valor=="object")?valor.value:String(valor);
	var t=(arguments.length<1)?"default":String(tipo);
	if(!trim(vr)||trim(vr).length==0) return "";
	if(/^(date|month_year|time|cep|(neg_)?numeric|interval)$/.test(t))
		vr=(/^neg_/.test(t)&&vr.indexOf("-")==0?"-":"")+justNumbersStr(vr);
	else if(/^((neg_)?(float(\d{0,1})|money(\d{0,1})))$/.test(t))vr=toFloat(vr,_getNDec(t));
	else{
		if(/^(cpf|cnpj|cpf_cnpj)$/.test(t)){
			vr=justNumbersStr(vr);
			var isCPF=tipo=="cpf"||(tipo=="cpf_cnpj" && vr.length<=SZ_CPF);
			vr=repeatStr(vr,"0",isCPF?SZ_CPF:SZ_CNPJ);
			if(parseInt(vr,10)==0)vr="";
		}else if(t=="email")vr=trim(vr)
	}
	if(typeof valor=="object")valor.value=vr;
	else return vr;
}

function formatCamp(campo,tp,par1,par2,par3){
var ie=(brow().isIE()&&!brow().isMac()),v=brow().getVersion();

var nArg=formatCamp.arguments.length;
ERRO=false;
var vr=trim(campo.value);
if(!campo|| !vr ||vr.length==0){
//	if(/^(neg_)?(money|money2)$/.test(tp))campo.value="0,00";
	return false;
}
if(nArg==2)par1="";
if(tp=="interval"){
	if(nArg<=4) par3="";
	return formatType(campo,tp,par3,par1,par2);
}else return formatType(campo,tp,par1);
}

function validaConteudo(event,el,tp)
{
var t=(typeof event.which!="undefined"&& event.which!=null?event.which:event.keyCode),key=String.fromCharCode(t);
if(t<20)return true;
var tp_sp=/^sp_/.test(tp);
if(/^((neg_)?(numeric|float(\d{0,1})|money(\d{0,1})))$/.test(tp)){
	return isNumeric(key)||(!/numeric/.test(tp)&&key==","&&el.value.indexOf(",")==-1)||(/^neg_/.test(tp)&&key=="-" && el.value.indexOf("-")==-1);
}else if(/^(sp_)?alfanumeric$/.test(tp))
	return isAlfaNumeric(key)||(tp_sp && key==" ");
else if(/^(sp_)?textnumber$/.test(tp))
	return isTextNumber(key)||(tp_sp && key==" ");
else{
	switch(tp){
		case "email":
		case "uppercase":return true;
		case "text":return isAlfa(key)|| /[ ]/.test(key);
		case "text_entry":return isTextNumber(key)||/[\.\-\/\,=]|\s/.test(key);
		case "default":return !/'|"/.test(key);
		default:return isNumeric(key);
	}
}
}

function saltaCampo(ev,field,tp,size){
	var tc,max,nargs=saltaCampo.arguments.length;
	if(/^(neg_)?float/.test(tp))max=(nargs>3)?size:SZ_FLOAT;
	else if(/^(neg_)?money/.test(tp)){
		if(!verifyMaxValue(field.value))max=(nargs>3)?size:SZ_MONEY;
		else field.value="0,00";
	}else if(/^(date|month_year|time|cpf|cnpj|cpf_cnpj|cep)$/.test(tp))
		max=eval("SZ_"+tp.toUpperCase());
	else if(/^(text|text_entry|uppercase|interval|(sp_)?alfanumeric|(sp_)?textnumber|default|(neg_)?numeric|email)$/.test(tp))
		max=size;
	tc=brow().isNetscape()?ev.which:ev.keyCode;
	if(String(field.value).length>=max && tc>=48){autoSkip(field);return true;}
	else return false;
}

function isNumeric(v){return /^[0-9]+$/.test(v);}
function isFloatNumber(n){return /^\-?\d+(,\d+|\d*)$/.test(n);}
function brow(){if(typeof objBrow!="object")objBrow=new Browser();return objBrow;}

function Browser(){
this.name=this.platform="Unknown";
this.majorver=this.version=this.minorver="";
this.mozilla=false;
this.init=_Init;
this.getName=function(){return this.name};
this.getMinorver=function(){return this.minorver};
this.getMajorver=function(){return this.majorver};
this.getVersion=function(){return parseFloat(this.version,10)};
this.getPlatform=function(){return this.platform}; 
this.isIE=function(){return(this.name=="IE")};
this.isNetscape=function(){return(this.name=="Netscape")};
this.isMozilla=function(){return this.mozilla};
this.isWindows=function(){return _has(this.platform,["Windows","WinNT"])};
this.isWinNT=function(){return _has(this.platform,["WinNT","Windows NT"])};
this.isWin95=function(){return _has(this.platform,["Win95","Windows 95"])};
this.isWin98=function(){return _has(this.platform,["Win98","Windows 98"])};
this.isLinux=function(){return _has(this.platform,"Unix")};
this.isMac=function(){return _has(this.platform,"Mac");};
this.init();
}

function _Init(){
var ua=navigator.userAgent,t="",ts="",i,bv;
bv=ua.slice(0,ua.indexOf("("));
ts=ua.slice(ua.indexOf("(")+1,ua.indexOf(")")).split(";");
for(i=0;i<ts.length;i++){
	t=ts[i].trim();
	if(_has(t,["MSIE","Opera"]))bv=t;
	else if(_has(t,["X11","SunOS","Linux"]))this.platform="Unix";
	else if(_has(t,["Mac","PPC","Win"]))this.platform=t;
}
var idx=bv.indexOf("MSIE"),lo="";
if(idx>=0)bv=bv.slice(idx);
if(bv.slice(0,7)=="Mozilla"){
	lo="";
	this.name="Netscape";
	if(ua.indexOf("Gecko/")!=-1){
		if(/Netscape/.test(ua)){
			var v=/([^\/]+)\s*$/.exec(ua);
			if(v&&v.length>1)lo=v[1]+" ";
		}else{
			this.mozilla=true;
			var v=/rv:([^\)]+)\)/.exec(ua);
			if(v&&v.length>1)lo=v[1]+" ";
		}
	}
	if(lo=="")lo=bv.slice(8);
}else if (bv.slice(0,4)=="MSIE"){
	this.name="IE";lo=bv.slice(5);
}else if (bv.slice(0,27)=="Microsoft Internet Explorer"){
	this.name="IE";lo=bv.slice(28);
}else if (bv.slice(0,5)=="Opera"){
	this.name="Opera";lo=bv.slice(6);
}
lo=lo.trim();
i=lo.indexOf(" ");
if(i>=0)this.version=lo.slice(0,i);
else this.version=lo;
j=this.version.indexOf(".");
if(j>=0){
	this.majorver=this.version.slice(0,j);
	this.minorver=this.version.slice(j+1);
}else this.majorver=this.version;
}

function _has(s,a){
s=String(s);
if(typeof(a)=="string")return s.indexOf(a)!=-1;
else{
	for(var i=0;i<a.length;i++)if(s.indexOf(a[i])!=-1)return true;
	return false;
}
}

function trim(s){return String(s).replace(/^\s+/,"").replace(/\s+$/,"");}
function _TRIM(){
var s=0,e=this.length;
while(s<e&&this.charAt(s)==' ')s++;
while(e>0&&this.charAt(e-1)==' ')e--;
return this.slice(s,e);
}
String.prototype.trim=_TRIM;

function justNumbersStr(s){return String(s).replace(/\D*/g,"");}
function onlySameNumber(s){return isNumeric(s)&& (new RegExp("^("+s.charAt(0)+")(\\1)*$")).test(s);}

function toFloat(src,ndec){
   src=trim(src);
	if(!/^\-?([0-9]|\.)*\,{0,1}[0-9]*$/.test(src)||src.charAt(0)==".")return src;
	var tam=src.length,pDec=src.indexOf(",");
	if(src.length==0)src="0";
	if(pDec==-1){
		var p=src.indexOf(".");
		if(p!=-1&&p==(tam-ndec-1))src=src.replace(/\.(\d*)$/,",$1");
		else return removeStr(src,".")+","+repeatNStr("0",ndec);
		pDec=src.indexOf(",");
	}
	src=removeStr(src,".");
	if(pDec==0)return "0"+src+repeatNStr("0",ndec+1-src.length);
	else{
		if(pDec>(tam-ndec-1))src+=repeatNStr("0",pDec-(tam-ndec-1));
		pDec=src.indexOf(",");
		return parseInt(src.slice(0,pDec),10)+src.slice(pDec,pDec+ndec+1);
	}
}

function _getNDec(t){
	var arr=t.match(/(\d+)\s*$/);
	return arr?parseInt(arr[1],10):2;
}

function invertStr(s){
	var t="",i;
	for(i=0;i<s.length;i++)t=s.charAt(i)+t;
	return t;
}

function removeStr(src,arg){
	var v=(typeof arg=="string")?[arg]:arg;
	var r="";
	for(var i=0;i<v.length;i++)r=changeStr(src,v[i],"");
	return r;
}

function repeatStr(src,str,size,orient){
	var r=String(src);
	if(!orient)orient="left";
	while(r.length < size)r=orient.toLowerCase()=="right"?(r+str):(str+r);
	return r;
}

function repeatNStr(vr,n){
var r="",i;
for(i=0;i<n;i++)r+=vr;
return r;
}

function changeStr(src,from,to)
{
	src=String(src);
	var i,li=0,lFrom= from.length,dst="";
	while((i=src.indexOf(from,li))!=-1){
		dst+=src.substring(li,i)+to;
		li=i+lFrom;
	}
	dst+=src.substring(li);
	return dst;
}
function verifyMaxValue(vr){return vr.length>0 &&(parseFloat(vr)>MAX_VALUE);}

function isValidValue(vr,tp,adarg1,adarg2){
var re,isNum=isNumeric(vr);
if(/^(cep)$/.test(tp))
	return isNum && vr.length==eval("SZ_"+tp.toUpperCase());
else if(/^((neg_)?(float(\d{0,1})|money(\d{0,1})))$/.test(tp))
	return (!/^neg_/.test(tp)&&vr.indexOf("-")!=-1?false:isFloatNumber(vr));
else if(/textnumber$/.test(tp))
	return isTextNumber((tp.indexOf("sp_")==0)?removeStr(vr," "):vr);
else if(/text_entry$/.test(tp))
	return isTextNumber(vr.replace(/[\.\-\/\,=]|\s/g,""));
else if(/alfanumeric$/.test(tp))
	return isAlfaNumeric((tp.indexOf("sp_")==0)?removeStr(vr," "):vr);
else{
	switch(tp){
		case "time":
			switch(vr.length){
				case 1:vr="0"+vr+"00";break;
				case 2:vr+="00";break;
				case 3:vr="0"+vr+"0";break;
			}
			vr=repeatStr(vr,"0",4,"right");
			return(isNum && /^([0-1]\d[0-5]\d)|(2[0-3][0-5]\d)$/.test(vr))?vr:null;
		case "date":
			var obj=new DateValidation(vr);
			return (isNum && obj.isDate())?obj:null;
		case "month_year":
			var obj=new DateValidation("01"+vr);
			return (isNum && obj.isDate())?obj:null;
		case "text":return isAlfa(vr.replace(/[ ]/g,""));
		case "email":return isEmail(vr);
		case "cpf":return isCPF(vr);
		case "cnpj":return isCNPJ(vr);
		case "cpf_cnpj":return (vr.length <=SZ_CPF)?isCPF(vr):isCNPJ(vr);
		case "interval":
			vr=parseInt(vr,10);
			return vr>=adarg1 && vr<=adarg2;
		case "default":return !/'|"/.test(vr);
		default:return true;
	}
}
}

/* FIM: Validacao de Campos*/
