var lang = 'en';

function fillSelect(elem, data, addBlank)
{
	elem = $(elem);
	
	elem.empty();
	if (addBlank)
		elem.append('<option value=""></option>');
		
	var op;
	for (var i = 0; i < data.length; i++)
	{
		op = data[i];
		elem.append('<option value="' + op.value + '">' + op.text + '</option>');
	}
}

$(document).ready(function(){
	$("#systemWorking").ajaxStart(function(){
		$(this).show();
	});
	$("#systemWorking").ajaxStop(function(){
		$(this).hide();
	});

	$('#partNumberTr').hide();
	$('#documentTypesTr').hide();
	
	$.ajax({
        type: 'GET',
        url: 'docman_ajax.php',
        contentType: 'application/json;utf-8',
        dataType: 'json',
        data: 'lang=' + lang,
        success: function(json) {
        	fillSelect('#prodLine', json, true);
        },
        error: function(xml,status){
            alert(status);
        }
    });
	
	$('#prodLine').change(function(){
		$('#partNumberTr').hide();
		$('#documentTypesTr').hide();
		if ($(this).val() != '')
		{
			$.ajax({
		        type: 'GET',
		        url: 'docman_ajax.php',
		        contentType: 'application/json;utf-8',
	        	dataType: 'json',
	        	data: 'lang=' + lang + '&prodline=' + $(this).val(),
		        success: function(json) {
		            fillSelect('#partNumber', json, true);
		            $('#partNumberTr').show();
		        },
		        error: function(xml,status){
		            alert(status);
		        }
		    });
	    }
	});
	
	$('#partNumber').change(function(){
		$('#documentTypesTr').hide();
		if ($(this).val() != '')
		{
			$.ajax({
		        type: 'GET',
		        url: 'docman_ajax.php',
		        contentType: 'application/json;utf-8',
	        	dataType: 'json',
	        	data: 'lang=' + lang + '&partnum=' + $(this).val(),
		        success: function(json) {
		            fillSelect('#documentType', json, false);
		            $('#documentTypesTr').show();
		        },
		        error: function(xml,status){
		            alert(status);
		        }
		    });
	    }
	});
});

