/* SEARCH PNEU */
function fillSearchPneuForm(){
	var obdobie = jQuery('#searchObdobie').val();
	var typ = jQuery('#searchTyp').val();
	var url = "/search/fill-search-pneu-form/obdobie/"+obdobie+"/typ/"+typ; 
	jQuery.getJSON(url ,function(data) {
		populatePneuForm(data);
	});	
}

function populatePneuForm(data){		
	//sirky
	var widths = data['widths'];	
	var value = jQuery('#searchWidth').val();
	var options = jQuery('#searchWidth').attr('options');
	options.length = 0;
	var i = 0; 
	jQuery.each(widths, function(key, val) {				
		options[i++] = new Option(val,key);		
	});
	jQuery('#searchWidth').val(value);
		
	//vysky
	var heigths = data['heights'];		
	var value = jQuery('#searchHeight').val();
	options = jQuery('#searchHeight').attr('options');
	options.length = 0;
	i = 0; 
	jQuery.each(heigths, function(key, val) {				
		options[i++] = new Option(val,key);		
	});
	jQuery('#searchHeight').val(value);
	
	//diameters
	var diameters = data['diameters'];	
	var value = jQuery('#searchDiameter').val();
	options = jQuery('#searchDiameter').attr('options');
	options.length = 0;
	i = 0; 
	jQuery.each(diameters, function(key, val) {				
		options[i++] = new Option(val,key);		
	});
	jQuery('#searchDiameter').val(value);

	//zat. index
	var weights = data['weights'];	
	var value = jQuery('#searchWeight').val();
	options = jQuery('#searchWeight').attr('options');
	options.length = 0;
	i = 0; 
	jQuery.each(weights, function(key, val) {				
		options[i++] = new Option(val,key);		
	});	
	jQuery('#searchWeight').val(value);
		
	//rych. index
	var speeds = data['speeds'];	
	var value = jQuery('#searchSpeed').val();
	options = jQuery('#searchSpeed').attr('options');
	options.length = 0;
	i = 0; 
	jQuery.each(speeds, function(key, val) {				
		options[i++] = new Option(val,key);		
	});	
	jQuery('#searchSpeed').val(value);
	
	//znacky
	var brands = data['brands'];	
	var value = jQuery('#searchBrand').val();
	options = jQuery('#searchBrand').attr('options');
	options.length = 0;
	i = 0; 
	jQuery.each(brands, function(key, val) {				
		options[i++] = new Option(val,key);		
	});	
	jQuery('#searchBrand').val(value);
}

/* SEARCH PNEU BY AUTO */

function loadModels(){
	fillSearchPneuByAutoForm('model');
}

function loadYears(){
	fillSearchPneuByAutoForm('year');
}

function fillSearchPneuByAutoForm(box){
	var cat1 = 0;
	var cat2 = 0;
	if(box=='model'){
		cat1 =jQuery('#manufacturer').val();
		if(cat1==0){
			clearPneuByAutoForm('model');
			return;
		}
	}
	if(box=='year'){
		cat1 = jQuery('#manufacturer').val();
		cat2 = jQuery('#model').val();
		if(cat2==0){
			clearPneuByAutoForm('year');
			return;
		}
	}	
	var url = "/search/fill-search-pneu-by-auto-form/cat1/"+cat1+"/cat2/"+cat2;
	jQuery.getJSON(url ,function(data) {
		populatePneuByAutoForm(data,box);
	});	
}

function populatePneuByAutoForm(data, box){	
	options = jQuery('#'+box).attr('options');
	options.length = 0;
	var i = 0; 
	jQuery.each(data, function(key, val) {				
		options[i++] = new Option(val,key);		
	});	
}

function validatePneuByAutoForm(){
	var cat1 = jQuery('#manufacturer').val();
	var cat2 = jQuery('#model').val();
	var cat3 = jQuery('#year').val();
	if(cat1==0){		
		alert("Vyberte značku auta.");
		return false;
	}
	if(cat2==0){		
		alert("Vyberte model auta.");
		return false;
	}
	if(cat3==0){		
		alert("Vyberte rok výroby auta.");
		return false;
	}
	return true;
}

function clearPneuByAutoForm(box){
	var data = {0:"Vyber"};	
	if(box=='model'){
		populatePneuByAutoForm(data,'model');
		populatePneuByAutoForm(data,'year');		
	}
	if(box=='year'){
		populatePneuByAutoForm(data,'year');
	}
}

/* 
 *
 * 	SEARCH DISC 
 * 
 */
function fillSearchDiscForm(element, box){
	cleanDiscForm(box);
	var selectedValue = jQuery('#'+element).val();
	var typdiskuValue = jQuery('#typdisku').val();
	var autoValue = jQuery('#auto').val();
	var modelValue = jQuery('#model').val();
	var rokValue = jQuery('#rok').val();
	
	var url = "/search/fill-search-disc-form/option/"+element;
	var params = {typdisku: typdiskuValue, auto:autoValue, model:modelValue, rok:rokValue, selectedvalue:selectedValue};
	
	jQuery.getJSON(url, params, function(data) {
		populateDiscForm(data);
	});	
}

function populateDiscForm(data){		
	jQuery.each(data, function(s) {		

		options = jQuery('#'+s).attr('options');
		options.length = 0;
		var i = 0;
		var partData = data[s]; 
		jQuery.each(partData, function(key, val) {			
			//if((key!=null && key!='') && (val!=null && val!='')){				
			options[i] = new Option(val,key+"");		
			//}
			i++;
		});
		
	});
		
}

function cleanDiscForm(selectBox) {	
	var auto = document.getElementById("auto");
	var model = document.getElementById("model");
	var rok = document.getElementById("rok");
	var priemer = document.getElementById("priemer");
	var roztec = document.getElementById("roztec");	
	if(selectBox<=1){
		auto.selectedIndex = 0;			
	}
	if(selectBox<=2){
		model.selectedIndex = 0;		
	}	
	if(selectBox<=3){
		if(rok!=null){
			rok.selectedIndex = 0;
		}		
	}	
	if(selectBox<=4){
		if(priemer!=null){
			priemer.selectedIndex = 0;
		}		
	}	
	if(selectBox<=5){
		if(roztec!=null){
			roztec.selectedIndex = 0;
		}		
	}		
}

function checkValues(f){
	var auto = document.getElementById("auto");
	var model = document.getElementById("model");
	var rok = document.getElementById("rok");
	var priemer = document.getElementById("priemer");
	var roztec = document.getElementById("roztec");
	var valid_ok = document.getElementById("valid_ok");
	
	if(auto.selectedIndex==0){
		alert("Vyberte typ auta");
		return false;
	}
	if(model.selectedIndex==0){
		alert("Vyberte model auta");
		return false;
	}
	if(rok!=null && rok.selectedIndex==0){
		alert("Vyberte rok");
		return false;
	}
	/*
	if(priemer!=null && priemer.selectedIndex==0){
		alert("Vyberte priemer");
		return;
	}
	if(roztec!=null && roztec.selectedIndex==0){
		alert("Vyberte roztec");
		return;

	valid_ok.value = 1;
	*/
	f.submit();	
}

/* TOGGLE - rozsireny zoznam pneu/disc */

function toggle_v1(element) {
	if (document.getElementById(element).style.display!='none') {
		$('#' + element).slideUp('normal');
	}
	else {
		$('#' + element).slideDown('normal');
	}
}

function toggle_v2(element) {
	if (document.getElementById(element).style.display!='none') {
		$('#' + element).slideUp('slow');
		$('#a-' + element).removeClass("selected");
	}
	else {
		$('#' + element).slideDown('slow');
		$('#a-' + element).addClass("selected");
	}
}

function toggle_v3(element) {
	if (document.getElementById(element).style.display!='none') {
		$('.toggle').slideUp('slow');
	}
	else {
		$('.toggle').slideUp('slow');
		$('#' + element).slideDown('slow');
	}
}

/*
	$('#s-' + element).removeClass('open');
	$('#s-' + element).addClass('closed');
*/