function LoadMakes(){
	jQuery.getJSON('/ajax/getMakes.php',{},function(res){
		jQuery('#'+elid).hide(200);
		var elid = 'SearchOil_make';
		var el = '';
		document.getElementById(elid).innerHTML = '<select onchange="LoadModels();" id="SearchOil_make_sel" name="SearchOil_make"><option value="">Выберите марку</option></select>';
		jQuery('#'+elid+' select').each(function(){ el = this; } );
		jQuery(el).empty();
		jQuery(el).append(new Option('Выберите марку', ''));
		for(i=0; i < res.length; i++){
		var a = new Option(res[i].name, res[i].id);
		a.title = res[i].name;
		jQuery(el).append(a);
		}
		jQuery(el).styleSelect({styleClass: "changess"});
		jQuery('#'+elid).show(200);
	});
}

function LoadModels(){
	var makeId = jQuery('#SearchOil_make select').val();
	if (!makeId){ alert('Выберите марку.'); return false; }
	jQuery.getJSON('/ajax/getModels.php',{"makeId": makeId},function(res){
		jQuery('#'+elid).hide(200);
		var elid = 'SearchOil_model';
		var el = '';
		document.getElementById(elid).innerHTML = '<select onchange="LoadCars();" id="SearchOil_model_sel" name="SearchOil_model"><option value="">Выберите модель</option></select>';
		jQuery('#'+elid+' select').each(function(){ el = this; } );
		jQuery(el).empty();
		jQuery(el).append(new Option('Выберите модель', ''));
		for(i=0; i < res.length; i++){
		var a = new Option(res[i].name, res[i].id);
		a.title = res[i].name;
		jQuery(el).append(a);
		}
		jQuery(el).styleSelect({styleClass: "changess"});
		jQuery('#'+elid).show(200);
	});
}

function LoadCars(){
	var makeId  = jQuery('#SearchOil_make select').val();
	var modelId = jQuery('#SearchOil_model select').val();
	if (!makeId){  alert('Выберите марку.');  return false; }
	if (!modelId){ alert('Выберите модель.'); return false; }
	jQuery.getJSON('/ajax/getCars.php',{"makeId": makeId, "modelId": modelId},function(res){
		jQuery('#'+elid).hide(200);
		var elid = 'SearchOil_cars';
		var el = '';
		document.getElementById(elid).innerHTML = '<select onchange="SearchOil();" id="SearchOil_car_sel" name="SearchOil_car"><option value=""></option></select>';
		jQuery('#'+elid+' select').each(function(){ el = this; } );
		jQuery(el).empty();
		jQuery(el).append(new Option('Выберите двигатель', ''));
		for(i=0; i < res.length; i++){
		var a = new Option(res[i].name, res[i].id);
		a.title = res[i].name;
		jQuery(el).append(a);
		}
		jQuery(el).styleSelect({styleClass: "changess"});
		jQuery('#'+elid).show(200);
	});
}

function SearchOil(){
	var elid = 'SearchOil_cars';
	var el = '';
	jQuery('#'+elid+' select').each(function(){ el = this; } );
	document.location = '/search/bycar.html?carId='+jQuery(el).val();
}

/* PART 2 */
function getScrollY(){
	scrollY = 0;
	if (typeof window.pageYOffset == "number") {
		scrollY = window.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {
		scrollY = document.documentElement.scrollTop;
	}  else if (document.body && document.body.scrollTop) {
		scrollY = document.body.scrollTop;
	} else if (window.scrollY) {
		scrollY = window.scrollY;
	}
	return scrollY;
}

function getInnerHeight(){
	height = 0;
	if (window.innerHeight) {
		height = window.innerHeight - 18;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		height = document.documentElement.clientHeight;
	} else if (document.body && document.body.clientHeight) {
		height = document.body.clientHeight;
	}
	return height;
}
function getInnerWidth(){
	width = 0;
	if (window.innerWidth) {
		width = window.innerWidth - 18;
	} else if (document.documentElement && document.documentElement.clientWidth) {
		width = document.documentElement.clientWidth;
	} else if (document.body && document.body.clientWidth) {
		width = document.body.clientWidth;
	}
	return width;
}


function posAjax(){
	var conf = document.getElementById('confirm_form');
	conf.style.top = getInnerHeight()/2-43+getScrollY()+"px";
	conf.style.left = getInnerWidth()/2-conf.style.width.replace(/[^0-9]/g,'')/2+120+"px";
	setTimeout("posAjax()", 100);

	var bg = document.getElementById('confirm_bg');
	bg.style.left = '0px';
	bg.style.top = '0px';
	bg.style.width = getInnerWidth()+'px';
	bg.style.height = getInnerHeight()+getScrollY()+20+"px";
}

function showAjax(){
	jQuery('#confirm_form').fadeIn(500);
	jQuery('#confirm_bg').fadeIn(500);
}

function hideAjax(){
	jQuery('#confirm_form').fadeOut(500);
	jQuery('#confirm_bg').fadeOut(500);
}

function sendAjax(url){
	jQuery.get(url,{}, function(request) {
		if(request != ''){
			jQuery('#basket_cont').html(request);
		}
		posAjax();
		showAjax();
		//setTimeout("hideAjax()", 4000);
	});
	return false;
}

/* PART 3 */
function getElementPosition(elemId){
    var elem = document.getElementById(elemId);

    var w = elem.offsetWidth;
    var h = elem.offsetHeight;

    var l = 0;
    var t = 0;

    while (elem)
    {
        l += elem.offsetLeft;
        t += elem.offsetTop;
        elem = elem.offsetParent;
    }

    return {"left":l, "top":t, "width": w, "height":h};
}

function showFun() {
    if(visible == false) {
        document.getElementById('showblock').style.display = 'block';
        document.getElementById('showblock').style.top = top - 1 + 'px';
        document.getElementById('showblock').style.left = left - 171 + 'px';
        visible = true;
    } else {
        document.getElementById('showblock').style.display = 'none';
        visible = false;
    }
}
