$(document).ready(function() {
    fillObjects('');
});

function fillObjects(query) {
    $.post("rpc.php", "queryString="+query+"&typeString=objects", function(data){
        if(data.replace(/^\s+|\s+$/g, '').length > 0) {
            $("#objectsList").html(data);
        } else {
            $("#objectsList").html('');
        }
    });

}

function fillWorkType() {
    clearSelect('workType');
    clearSelect('firstQuality');
    clearSelect('secondQuality');

    $.post("rpc.php", "typeString=workType&objectsString="+$('#objectsList').val(), function(data){
        if(data.replace(/^\s+|\s+$/g, '').length > 0) {
            $("#workType").html(data);
        }
    });

    fillTable();
}

function fillFirstQuality() {
	clearSelect('firstQuality');
	clearSelect('secondQuality');

	$.post("rpc.php", "typeString=firstQuality&objectsString="+$('#objectsList').val()+
		"&workString="+$('#workType').val(), function(data){
			if(data.replace(/^\s+|\s+$/g, '').length > 0) {
				$("#firstQuality").html(data);
			}
		});

	fillTable();
}

function fillSecondQuality() {
	$.post("rpc.php", "typeString=secondQuality&objectsString="+$('#objectsList').val()+
		"&workString="+$('#workType').val()+"&qualificationString="+$('#firstQuality').val(), function(data){
			if(data.replace(/^\s+|\s+$/g, '').length > 0) {
				$("#secondQuality").html(data);
			}
		});

	clearSelect('secondQuality');
	fillTable();
}

function fillTable() {
    $.post("rpc_table.php", "objectsString="+$('#objectsList').val()+
        "&workString="+$('#workType').val()+"&cityzString="+$('#cityz').val(), function(data){

            if(data.replace(/^\s+|\s+$/g, '').length > 0) {
                $("#result").html(data);
            }
        });
}

function clearSelect(type) {
    $("#"+type).html('');
}

