$(document).ready(function ()
{
setInterval(function() { $('#sess_img').attr('src', '/blank.php?s='+Math.random());}, 900000);
//alert('1');
});


function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}

function setCookie(name, value, expiredays, path, domain, secure) {
   if (expiredays) {
      var exdate=new Date();
      exdate.setDate(exdate.getDate()+expiredays);
      var expires = exdate.toGMTString();
   }
   document.cookie = name + "=" + escape(value) +
   ((expiredays) ? "; expires=" + expires : "") +
   ((path) ? "; path=" + path : "") +
   ((domain) ? "; domain=" + domain : "") +
   ((secure) ? "; secure" : "");
}


function getParam(name) {
	var query = window.location.href.substring(window.location.href.indexOf('?') + 1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == name) {
			return pair[1].replace(/\%2C/, ',');
		}
	}
	return null;
}



/* a2t **/
function t2a()
{
document.write('<div id="t2a_form"><form method="post" enctype="multipart/form-data" name=qformauto onsubmit="return false"><input type="hidden" name="ln" value="">');
document.write('<div class="title_form"><b><a href="http://shina.ru/selection_of_the_car.php">Подбор шин и дисков по авто:</a></b></div>');

document.write('<div class="manuf">Марка:<br><select name="qmanufacture" id="qmanufacture" onchange="qload_models();"  style="width:230px"><option></option>');
for(var k in manuft2a)
{
	document.write('<option value="'+k+'">'+manuft2a[k]+'</option>');
}
document.write('</select></div>');

document.write('<div id="qmodels" style="display:none">Модель:<br><select name="qmodel" id="qmodel" onchange="qload_years();"  style="width:230px" disabled><option></option></select></div>');

document.write('<div id="qyears" style="display:none">Год выпуска:<br><select name="qyear" id="qyear" onchange="qload_modification();" style="width:230px" disabled><option></option></select></div>');

document.write('<div id="qmodif" style="display:none">Комплектация:<br><select name="qmodification" id="qmodification" onchange="qload_auto();" style="width:230px" disabled><option></option></select></div>');

document.write('<span id="qwait" style="display:none"><img src="/img/wait_small.gif" alt="" border=0 width=16 height=16 /></span>  <div class="button_form"><input type="Submit" value="перейти" disabled name=sb onclick="qclick();"></div>');
document.write('</form></div>');




var save_auto = getCookie('save_auto');

if(save_auto)
{
	var save_auto_models_ = getCookie('save_auto_models');
	var save_auto_years_ = getCookie('save_auto_years');
	var save_auto_compls_ = getCookie('save_auto_compls');
	var save_auto_data_ = getCookie('save_auto_data');

	var save_auto_models = save_auto_models_.split('~');
	var save_auto_years = save_auto_years_.split('~');
	var save_auto_compls = save_auto_compls_.split('~');
	var save_auto_data = save_auto_data_.split('~');

	//manuf
	for(a=0; a<document.qformauto.qmanufacture.options.length; a++)
	{
		if(document.qformauto.qmanufacture.options[a].value==save_auto_data[2])document.qformauto.qmanufacture.options[a].selected=true;
	}

    //models
	var objSel = document.getElementById('qmodel');
	for(a=0; a<save_auto_models.length; a++)
	{
		mod = save_auto_models[a].split(';');
		qaddOption(objSel, mod[1].replace(/\+/g, ' '), mod[0], '', ((save_auto_data[0]==mod[0]) ? 1 : 0));
	}
 	objSel.disabled = false;
	document.getElementById('qmodels').style.display='';

	//year
	var objSel = document.getElementById('qyear');
	for(a=0; a<save_auto_years.length; a++)
	{
		qaddOption(objSel, save_auto_years[a], save_auto_years[a], '', ((save_auto_data[1]==save_auto_years[a]) ? 1 : 0));

	}
 	objSel.disabled = false;
	document.getElementById('qyears').style.display='';

    //modification
	var objSel = document.getElementById('qmodification');
	for(a=0; a<save_auto_compls.length; a++)
	{
		mod = save_auto_compls[a].split(';');
		qaddOption(objSel, mod[1].replace(/\+/g, ' '), mod[0], '', ((save_auto==mod[0]) ? 1 : 0));
	}
 	objSel.disabled = false;
	document.getElementById('qmodif').style.display='';

	qload_auto();

}
}


    function qload_models() {
if(document.qformauto.qmanufacture.selectedIndex==0)return;
    	//document.getElementById('autofinish').innerHTML='';
document.qformauto.sb.disabled=true;
document.qformauto.qmanufacture.options[0].disabled=true;
qAllEnabled();

    	var objSel = document.getElementById('qmodel');
    	objSel.options.length=1;
    	objSel.disabled=true;
    	var objSel2 = document.getElementById('qyear');
    	objSel2.options.length=1;
    	objSel2.disabled=true;
    	var objSel3 = document.getElementById('qmodification');
    	objSel3.options.length=1;
    	objSel3.disabled=true;
    	document.getElementById('qwait').style.display='';
        JsHttpRequest.query(
            '/services/selection_of_the_car/ajax.php',
            {
		'GetModels': document.qformauto.qmanufacture.options[document.qformauto.qmanufacture.selectedIndex].value
            },
            function(result, errors) {
                if (result) {
                    for(a=0; a<result['models'].length; a++)
                    {

                    	qaddOption(objSel, result['models'][a][1], result['models'][a][0]);

                    }
                    objSel.disabled = false;
document.getElementById('qmodels').style.display='';
                    document.getElementById('qwait').style.display='none';
                }
            },
            false
        );
    }


    function qload_years() {
if(document.qformauto.qmodel.selectedIndex==0)return;
qAllEnabled();
    	//document.getElementById('model').remove(0);
document.qformauto.sb.disabled=true;
    	//document.getElementById('autofinish').innerHTML='';
    	var objSel2 = document.getElementById('qyear');
    	objSel2.options.length=1;
    	objSel2.disabled=true;
    	var objSel3 = document.getElementById('qmodification');
    	objSel3.options.length=1;
    	objSel3.disabled=true;

		document.getElementById('qwait').style.display='';
        JsHttpRequest.query(
            '/services/selection_of_the_car/ajax.php',
            {

                'Manufacture': document.qformauto.qmanufacture.options[document.qformauto.qmanufacture.selectedIndex].value,
                'GetYears': document.qformauto.qmodel.options[document.qformauto.qmodel.selectedIndex].value
            },
            function(result, errors) {
                if (result) {
                    var objSel = document.getElementById('qyear');
                    objSel.options.length=1;
                    for(a=0; a<result['years'].length; a++)
                    {

                    	qaddOption(objSel, result['years'][a], result['years'][a]);
                    }
                    objSel.disabled = false;
document.getElementById('qyears').style.display='';
                    document.getElementById('qwait').style.display='none';
document.qformauto.qmodel.options[0].disabled=true;
                }
            },
            false
        );
    }



    function qload_modification() {
if(document.qformauto.qyear.selectedIndex==0)return;

    	qAllEnabled();
    	document.getElementById('qwait').style.display='';
document.qformauto.sb.disabled=true;
        JsHttpRequest.query(
            '/services/selection_of_the_car/ajax.php',
            {
                'Manufacture': document.qformauto.qmanufacture.options[document.qformauto.qmanufacture.selectedIndex].value,
                'Model': document.qformauto.qmodel.options[document.qformauto.qmodel.selectedIndex].value,
                'GetModification': document.qformauto.qyear.options[document.qformauto.qyear.selectedIndex].value




            },
            function(result, errors) {
                if (result) {
                    var objSel = document.getElementById('qmodification');
                    objSel.options.length=1;
                    for(a=0; a<result['modifications'].length; a++)
                    {

                    	qaddOption(objSel, result['modifications'][a][1], result['modifications'][a][0]);
                    }
                    objSel.disabled = false;
document.getElementById('qmodif').style.display='';
                    document.getElementById('qwait').style.display='none';
document.qformauto.qyear.options[0].disabled=true;
                }
            },
            false
        );
    }

    function qload_auto() {
if(document.qformauto.qmodification.selectedIndex==0)return;
document.qformauto.qmodification.options[0].disabled=true;
document.qformauto.ln.value=document.qformauto.qmodification.options[document.qformauto.qmodification.selectedIndex].value;
document.qformauto.sb.disabled=false;

    }

function qclick()
{
window.location.href='/selection_of_the_car.php?automobile='+document.qformauto.ln.value;
}


function qaddOption (oListbox, text, value, isDefaultSelected, isSelected)
{
  var oOption = document.createElement('option');
  oOption.appendChild(document.createTextNode(text));
  oOption.setAttribute('value', value);

  if (isDefaultSelected) oOption.defaultSelected = true;
  else if (isSelected) oOption.selected = true;

  oListbox.appendChild(oOption);
}

function qAllEnabled()
{
document.qformauto.qyear.options[0].disabled=false;
document.qformauto.qmodel.options[0].disabled=false;
document.qformauto.qmodification.options[0].disabled=false;
}





/*Forms**/
function wheel_form()
{
var wke='';
getwM = getParam('manufacture');
getwW = getParam('w');
getwD = getParam('d');
getwP = getParam('pcd');
getwD2 = getParam('d2');
getwD1 = getParam('d1');
getwCat = getParam('iscat');

document.write('<div id="wheel_form"><form name=fast_search_wheel action="http://www.shina.ru/price.shtml" method=get onsubmit="return check(\'wheel\');"><input type="hidden" name="wheel" value="1"><input type=hidden name=id value="43"><div class="title_form"><b>Подбор дисков по размеру:</b></div>');

document.write('<div class="manuf">Марка <select name="manufacture" title="Производитель дисков">');
for(var k in wmanuf_k)
{
	document.write('<option value="'+wmanuf_k[k]+'"'+((getwM==wmanuf_k[k])?' selected':'')+'>'+wmanuf_v[k]+'</option>');
}
document.write('</select></div>');

document.write('<div class="size"><select name="w" title="ширина диска">');
for(var k in wwidth)
{
if(wwidth[k]=='-')wke='all';
else wke = wwidth[k];
	if(wwidth[k]=='-')document.write('<optgroup label="Ширина">');
	document.write('<option value="'+wke+'"'+((getwW== wwidth[k])?' selected':'')+'>'+wwidth[k]+'</option>');
}

document.write('</optgroup></select>&nbsp;&nbsp;X&nbsp;&nbsp;');

document.write('<select name="d">');
for(var k in wdiameter)
{
if(wdiameter[k]=='-')wke='all';
else wke = wdiameter[k];
	if(wdiameter[k]=='-')document.write('<optgroup label="Диам.">');
	document.write('<option value="'+wke+'"'+((getwD==wdiameter[k])?' selected':'')+'>'+wdiameter[k]+'</option>');
}
document.write('</optgroup></select> ');

document.write('<select name="pcd" title="Крепление (PCD)">');
for(var k in pcd)
{
if(pcd[k]=='-')wke='all';
else wke = pcd[k];
	if(pcd[k]=='-')document.write('<optgroup label="Крепл.">');
	document.write('<option value="'+wke+'"'+((getwP==pcd[k])?' selected':'')+'>'+pcd[k]+'</option>');
}
document.write('</optgroup></select></div>');

document.write('<div class="type"><label><input name="d2" value="1" type="checkbox"'+((getwD2==1)?' checked':'')+'>&nbsp;Легкосплавный</label>&nbsp;&nbsp;');
document.write('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<label><input name="d1" value="1" type="checkbox"'+((getwD1==1)?' checked':'')+'>&nbsp;Штампованный</label>&nbsp;&nbsp;');
document.write('</div>');
document.write('<div class="iscat"><label for="iscat"><input type=checkbox name=iscat value=1'+((getwCat=='1' && getParam('wheel')=='1')?' checked':'')+' id=iscat> с картинками</label></div>');
document.write('<div class="button_form"><input type="Submit" value="искать"></div>');
document.write('</form></div>');
}

function tyre_form()
{
var wke='';
getM = getParam('manufacture');
getW = getParam('sh1');
getP = getParam('sh2');
getR = getParam('shr');
getS1 = getParam('s1');
getS2 = getParam('s2');
getS3 = getParam('s3');
getS4 = getParam('s4');
getS4 = getParam('s4');
getCat = getParam('iscat');

document.write('<div id="tyre_form"><form name=fast_search_tyre action="http://www.shina.ru/price.shtml" method=get onsubmit="return check(\'tyre\');"><input type="hidden" name="tyre" value="1"><input type=hidden name=id value="43"><div class="title_form"><b>Подбор шин по размеру:</b></div>');

document.write('<div class="manuf">Марка <select name="manufacture" title="Производитель шин">');
for(var k in manuf_k)
{
	document.write('<option value="'+manuf_k[k]+'"'+((getM==manuf_k[k])?' selected':'')+'>'+manuf_v[k]+'</option>');
}
document.write('</select></div>');

document.write('<div class="size"><select name="sh1" title="ширина шины в мм. для Европейских шин и в дюймах для Американских">');
for(var k in width_1)
{
if(width_1[k]=='-')wke='all';
else wke = width_1[k];
	document.write('<option value="'+wke+'"'+((getW==width_1[k])?' selected':'')+'>'+width_1[k]+'</option>');
	if(width_1[k]=='-')document.write('<optgroup label="Euro">');
}
document.write('</optgroup><optgroup label="Usa">');
for(var k in width_2)
{

	document.write('<option value="'+width_2[k]+'"'+((getW==width_2[k])?' selected':'')+'>'+width_2[k]+'</option>');
}
document.write('</optgroup></select> / ');

document.write('<select name="sh2">');
for(var k in profile_1)
{
if(profile_1[k]=='-')wke='all';
else wke = profile_1[k];
	document.write('<option value="'+wke+'"'+((getP==profile_1[k])?' selected':'')+'>'+profile_1[k]+'</option>');
	if(profile_1[k]=='-')document.write('<optgroup label="Euro">');
}
document.write('</optgroup><optgroup label="Usa">');
for(var k in profile_2)
{
	document.write('<option value="'+profile_2[k]+'"'+((getP==profile_2[k])?' selected':'')+'>'+profile_2[k]+'</option>');
}
document.write('</optgroup></select> R ');

document.write('<select name="shr" title="Диаметр">');
for(var k in diametr)
{
if(diametr[k]=='-')wke='all';
else wke = diametr[k];
	document.write('<option value="'+wke+'"'+((getR==diametr[k])?' selected':'')+'>'+diametr[k]+'</option>');
}
document.write('</select></div>');

document.write('<div class="seasons"><label><input name="s1" value="1" type="checkbox"'+((getS1==1)?' checked':'')+'><img src="/img/ico/sun.gif" width=20 height=20 alt="Летние"></label>&nbsp;&nbsp;');
document.write('<label><input name="s2" value="1" type="checkbox"'+((getS2==1)?' checked':'')+'><img src="/img/ico/all.gif" width=20 height=21 alt="Всесезонные"></label>&nbsp;&nbsp;');
document.write('<label><input name="s3" value="1" type="checkbox"'+((getS3==1)?' checked':'')+'><img src="/img/ico/snow.gif" width=20 height=21 alt="Зимние"></label>&nbsp;&nbsp;');
document.write('<label><input name="s4" value="1" type="checkbox"'+((getS4==1)?' checked':'')+'><img src="/img/ico/ship.gif" width=20 height=18 alt="Шипованные">');
document.write('</div>');
document.write('<div class="iscat"><label for="iscat1"><input type=checkbox name=iscat value=1'+((getCat=='1' && getParam('tyre')=='1')?' checked':'')+' id=iscat1> с картинками</label></div>');
document.write('<div class="button_form"><input type="Submit" value="искать"></div>');
document.write('</form></div>');
}



function check(type)
{
if(type=='wheel')
{
	f = document.fast_search_wheel;

	if(f.manufacture.options[f.manufacture.selectedIndex].value=='all' &&
	f.w.options[f.w.selectedIndex].value=='all' &&
	f.d.options[f.d.selectedIndex].value=='all' &&
	f.pcd.options[f.pcd.selectedIndex].value=='all' &&
	f.d2.checked != true  &&
	f.d1.checked != true  )
	{
		alert('Нужно выбрать хотя бы одно свойство.');
		return false;
	}
}
else
{
	f = document.fast_search_tyre;

	if(f.manufacture.options[f.manufacture.selectedIndex].value=='all' &&
	f.sh1.options[f.sh1.selectedIndex].value=='all' &&
	f.sh2.options[f.sh2.selectedIndex].value=='all' &&
	f.shr.options[f.shr.selectedIndex].value=='all'   &&
	f.s1.checked != true &&
	f.s3.checked != true  &&
	f.s4.checked != true  &&
	f.s2.checked != true)
	{
		alert('Нужно выбрать хотя бы одно свойство.');
		return false;
	}
}
}



function ch_city(city)
{
	//alert(city);
	$('#changeregion option').each(function (i)
	{
		$('#price_'+$(this).val()).hide();
		$('#store_'+$(this).val()).hide();
$('#delivery_'+$(this).val()).hide();

	});
	/*.each(function ()
	{
		alert(this.val());
		});
		*/
	$('#price_'+city).show();
	$('#store_'+city).show();
$('#delivery_'+city).show();


	if(city == 78 || city == 77)
	{
		$('#tyre_where2buy').show();
		$('#wheel_where2buy').show();
		if(city == 78)
		{
			$('#w2b_78').show();
			$('#w2b_77').hide();
		}
		else
		{
			$('#w2b_77').show();
			$('#w2b_78').hide();
		}
	}
	else $('#tyre_where2buy').hide();


	if(city == 78 && TextButton[78] != '0')
		$('#buy_button').val('Купить со скидкой '+(TextButton[78]*$('#tobasketselect').val())+' р.');
	else
		$('#buy_button').val('Купить');


setCookie('reg', city, 400, "/");


//	alert();
}