function get_product_id(select) {
	return select.parent().parent().children("input[id$='-product']").val();
}

function load_size() {
	$.each($("select[id$='-size']"), function(idx, select) {
		select = $('#' + select.id)
		var product_id = get_product_id(select);
		$.getJSON('/ajax/magazine_form/' + product_id + '/', {}, function(json) {
			if(json.length) {
				response = '<option value="">--- wybierz rozmiar ---</option>';
	  			for(i=0; i < json.length; i++) {
	    			response += '<option value="' +  json[i].pk + '">' + json[i].fields.name + '</option>'
	  			}
				select.html(response);
				var color_select = $('#' + select.attr('id').replace('size', 'color'));
				//if (json.length==1) {
				//	select.get(0).selectedIndex = 1;
				//	load_color(select);
				//}
				color_select.focus();
			}
			
		});	
	})
}


function load_color(select) {
	var color_select = $('#' + select.attr('id').replace('size', 'color'))
	var count_select = $('#' + select.attr('id').replace('size', 'count'))
	var alert_field = $('#' + color_select.attr('id').replace('color', 'alert'));
	alert_field.html('');
	if (select.val()) {
		var product_id = get_product_id(select);
		$.getJSON('/ajax/magazine_form/' + product_id + '/' + select.val() + '/', {}, function(json) {
    		if(json.length) {
				response = '<option value="">--- wybierz kolor ---</option>';
      			for(i=0; i < json.length; i++) {
        			response += '<option value="' +  json[i].pk + '">' + json[i].fields.name + '</option>'
      			}
				
				color_select.html(response);
				if (json.length==1) {
					color_select.get(0).selectedIndex = 1;
					load_count(color_select)
				}
				color_select.focus();
				count_select.focus();
    		}
  		});
	} else {
		color_select.html('<option value="">--- najpierw rozmiar ---</option>');
		count_select.html('<option value="">--- najpierw kolor ---</option>');
		count_select.focus();	
		color_select.focus();
	}
}

var MAX_COUNT = 10;

function load_count(color_select, wholesale) {
	
	var size_select = $('#' + color_select.attr('id').replace('color', 'size'))
	var count_select = $('#' + color_select.attr('id').replace('color', 'count'))
	var alert_field = $('#' + color_select.attr('id').replace('color', 'alert'));
	alert_field.html('');
	
	var product_id = get_product_id(color_select);
	if (size_select.val() && color_select.val()) {
		$.getJSON('/ajax/magazine_form/' + product_id + '/' + size_select.val() + '/' + color_select.val() + '/', {}, function(json) {
    		if(json.length) {
				response = '<option value="">--- wybierz ilość ---</option>';
				var max = json[0].fields.count;
				if ((!wholesale) && (max > MAX_COUNT)){
					max = MAX_COUNT;
				}
				
      			for(i=0; i < max; i++) {
					c = i + 1;
        			response += '<option value="' + c + '">' + c + '</option>'
      			}
				count_select.html(response);
				count_select.focus();
				if (json[0].fields.available_for_days > 1) {
					alert_field.html('Ten produkt zostanie wysłany w ciągu ' + json[0].fields.available_for_days + ' dni.')
				} else {
					alert_field.html('Ten produkt zostanie wysłany w ciągu 24 godzin.')
				}
				count_select.get(0).selectedIndex = 1;
				count_select.focus();
    		}
  		});
	} else {
		count_select.html('<option value="">--- najpierw kolor ---</option>');
		count_select.focus();
		if (!size_select.val()) {
			color_select.html('<option value="">--- najpierw rozmiar ---</option>');	
			color_select.focus();	
		}
	}
}


