var nCar=0;
var nCarNew=0;
var oPrice=0;
var nPriceNew=0;
var nIntervalId=0;
var bDoNotUpdate=false;

function jsSetHelpClass(obj){
	li=document.getElementById('help_cat').getElementsByTagName('LI');
	for(i=0;i<li.length;i++){
		if(li[i].className.match('this')){
			if(li[i].className.match('first')){
				li[i].className='first';
			}else{
				li[i].className='';
			}
		}
	}
	obj.parentNode.className+=' this';
}

function jsPopUp(URL,width,height){
	if(!width){
		width=800;
	}
	if(!height){
		height=600;
	}
	window.open('/popup.html?image='+URL,'popup','toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width='+width+',height='+height);
//	popup.focus();
}

function jsShowHideHelpCat(cat,title){
	if(title.style.display=='none'){
		title.style.display='block';
		cat.setAttribute('class','block');
		cat.className='block';
	}else{
		title.style.display='none';
		cat.setAttribute('class','none');
		cat.className='none';
	}
}

function jaAlertAdd(nCompare){
	nCompare++;
	sMsg="Автомобиль добавлен в список сравнения.\nВсего в списке сравнения автомобилей: "+nCompare+".\nМаксимальное число автомобилей в списке сравнения 3.\n";
	if(nCompare==3){
		sMsg+="Вы больше не можете добавлять автомобили в список сравнения";
	}else{
		sMsg+="Мы можете добавить в список сравнения еще "+(3-nCompare)+" автомобил";
		if(nCompare==1){
			sMsg+='я';
		}else{
			sMsg+='ь';
		}
	}
	alert(sMsg);
}

function jsPrice(){
	if(oPrice){
		price=1*oPrice.innerHTML;
		if(price!=nPriceNew){
			if(Math.abs(price-nPriceNew)>1000){
				dx=500;
			}else{
				if(Math.abs(price-nPriceNew)>100){
					dx=100;
				}else{
					if(Math.abs(price-nPriceNew)>50){
						dx=50;
					}else{
						dx=1;
					}
				}
			}
			if(price<nPriceNew){
				oPrice.innerHTML=price+dx;
			}else{
				oPrice.innerHTML=price-dx;
			}
		}else{
			clearInterval(nIntervalId);
			nIntervalId=0;
			oPrice.style.fontWeight="";
			oPrice.style.color="";
		}
	}
}

function jsDoClear(){
	var aObj=new Array('price0','brand0','v0','capacity0','tonnage0','fuel0','body0','type0');
	bDoNotUpdate=true;
	for(i in aObj){
		if(document.getElementById(aObj[i]) && !document.getElementById(aObj[i]).checked){
			document.getElementById(aObj[i]).checked=true;
			document.getElementById(aObj[i]).setAttribute("checked","checked");
			jsDisableCategory(document.getElementById(aObj[i]));
		}
	}
	bDoNotUpdate=false;
	jsGetCarsN(document.getElementById('price0').form);
}

function jsCarN(){
	if(nCar!=nCarNew){
		if(nCarNew){
			document.getElementById('do_search').style.display='block';
			document.getElementById('do_clear').style.display='none';
		}else{
			document.getElementById('do_clear').style.display='block';
			document.getElementById('do_search').style.display='none';
		}
		if(Math.abs(nCar-nCarNew)>1000){
			dx=455;
		}else{
			if(Math.abs(nCar-nCarNew)>100){
				dx=95;
			}else{
				if(Math.abs(nCar-nCarNew)>50){
					dx=45;
				}else{
					dx=1;
				}
			}
		}
		if(nCar<nCarNew){
			nCar=nCar+dx;
			document.getElementById('ncar').innerHTML=nCar;
		}
		if(nCar>nCarNew){
			nCar=nCar-dx;
			document.getElementById('ncar').innerHTML=nCar;
		}
	}
}
function jsDisableCategory(obj){
	if(obj.checked){	//uncheck category
		for(i=1;i<obj.parentNode.parentNode.parentNode.getElementsByTagName('input').length;i++){
			if(obj.parentNode.parentNode.parentNode.getElementsByTagName('input')[i].checked){
				obj.parentNode.parentNode.parentNode.getElementsByTagName('input')[i].removeAttribute("checked");
			}
			obj.parentNode.parentNode.parentNode.getElementsByTagName('input')[i].checked=false;
		}
	}else{
		jsCheckRoot(obj);
	}
	jsGetCarsN(obj.form);
}
function jsDisableRoot(obj){
	if(obj.checked){	//unckeck root checkbox
		obj.parentNode.parentNode.parentNode.getElementsByTagName('input')[0].removeAttribute("checked");
		obj.parentNode.parentNode.parentNode.getElementsByTagName('input')[0].checked=false;
	}else{
//		alert(obj.parentNode.parentNode.parentNode);
		jsCheckRoot(obj.parentNode.parentNode.parentNode.getElementsByTagName('input')[0]);
	}
	jsGetCarsN(obj.form);
}
function jsCheckRoot(obj){
	if(!obj.checked){	//check root checkbox
		bFlag=true;
		for(i=1;i<obj.parentNode.parentNode.parentNode.getElementsByTagName('input').length;i++){
			if(obj.parentNode.parentNode.parentNode.getElementsByTagName('input')[i].checked){
				bFlag=false;
			}
		}
		if(bFlag){
			obj.parentNode.parentNode.parentNode.getElementsByTagName('input')[0].setAttribute("checked","checked");
			obj.parentNode.parentNode.parentNode.getElementsByTagName('input')[0].checked=true;
		}
	}
}
function jsGetCarsN(form){
	if(bDoNotUpdate){
		return false;
	}
	sData='';
	for(i=0;i<form.getElementsByTagName('input').length;i++){
		if(form.getElementsByTagName('input')[i].checked){
			if(sData!=''){
				sData+='&';
			}
			//sData+=form.getElementsByTagName('input')[i].getAttribute('name')+'='+encodeURIComponent(form.getElementsByTagName('input')[i].value);
			sData+=form.getElementsByTagName('input')[i].getAttribute('name')+'=1';
		}
	}
//	document.getElementById('ncar').innerHTML='?';
	postData('script','/popup/data/search_ncar/',sData)
}

