var global_disease_type = 0;


// ==================== DANE UZYTKOWNIKA ====================================================
// zapisuje dane do obiektu w php

function dc_save()
{
	// parametry POST
	var params = "kcal="+ $('#dc_kcal').val();
	params += "&user_name=" + URLEncode($('#dc_your_name').val());
	
	params += "&user_weight=" + $('#dc_your_weight').val();
	params += "&user_age=" + $('#dc_your_age').val();
	$(function()
	{
		if($('#dc_user_month').val())
		{
			params += "&user_month=" + $('#dc_user_month').val();
		}
	});
	
	params += "&protein=" + $('#dc_protein').val();

    //params += "&phe=" + $('#dc_phe').val();
    params += "&protein_pku=" + $('#dc_protein_pku').val();

    params += "&disease=" + $('#disease').val();
    params += "&additional_param_" + $('#disease').val() + "=" + $('#additional_param_' + $('#disease').val()).val();

    calculate_protein();
	
	json_get('set_user_data', dc_get_valid_menu_list_proc, params)
}



function dc_save_proc(status)
{
	alert(status['msg']);
}


// odczytuje dane uzytkownika z obiektu w php
function dc_read()
{
	json_get('get_user_data', dc_read_proc, '');	
}

// umieszcza odczytane dane w formularzu
function dc_read_proc(response)
{
	if(response['user_age'] != null)
	{
		$('#dc_your_name').val(URLDecode(response['user_name']) );
		$('#dc_your_age').val(response['user_age']);
		if($('#dc_user_month'))
		{
			$('#dc_user_month').val(response['user_month']);
		}
		$('#dc_kcal').val(response['max_kcal']);		
		$('#dc_your_weight').val(response['user_weight']);
		$('#dc_protein').val(response['max_protein']);
		$('#disease').val(response['disease']);
        var add_param_input = 'additional_param_' + response['disease'];
        if( $('#' + add_param_input) ){
        	$('#' + add_param_input).val( response['additional_data'] );
        }
        //document.getElementById('dc_phe').value = response['max_phe'];
        //document.getElementById('dc_protein_pku').value = response['max_protein_pku'];
		
		check_user_data();
	}
	dc_read_user_data(response);
	make_user_month_form();
}


function dc_read_user_data(user)
{
	document.getElementById('dc_user_user_name').value = URLDecode(user['user_name']);
	document.getElementById('dc_user_user_age').value = user['user_age'];
	document.getElementById('dc_user_max_kcal').value = user['max_kcal'];
	document.getElementById('dc_user_user_weight').value = user['user_weight'];
	document.getElementById('dc_user_max_protein').value = user['max_protein'];
	document.getElementById('dc_user_max_protein_pku').value = user['max_protein_pku'];
	document.getElementById('dc_user_max_phe').value = user['max_additional'];
	document.getElementById('dc_user_max_fat').value = user['max_fat'];
	document.getElementById('dc_user_max_carbohydrate').value = user['max_carbohydrate'];

	document.getElementById('dc_your_kcal').innerHTML = user['max_kcal'];
	if(user['user_weight'] != null)
	{
		document.getElementById('dc_your_weight').value = user['user_weight'];
	}
	if (user['max_protein'] > 0) 
	{
		document.getElementById('dc_your_protein').innerHTML = user['max_protein'] + " g";
		document.getElementById('dc_print_user_protein').innerHTML = user['max_protein'];
	}
	else
	{
		document.getElementById('dc_your_protein').innerHTML = '';
		document.getElementById('dc_print_user_protein').innerHTML = '';
	}

	document.getElementById('dc_print_user_name').innerHTML = URLDecode(user['user_name']);
	document.getElementById('dc_print_user_age').innerHTML = user['user_age'] + ' l.';

	if(user['user_age'] == 1 && user['user_month'])
	{
		document.getElementById('dc_print_user_age').style.display = 'none';
		document.getElementById('dc_print_user_month').style.display = 'block';
		document.getElementById('dc_print_user_month').innerHTML = user['user_month'] + ' mc.';
	}
	else
	{
		document.getElementById('dc_print_user_age').style.display = 'block';
		document.getElementById('dc_print_user_month').style.display = 'none';
	}
	document.getElementById('dc_print_user_kcal').innerHTML = user['max_kcal'];
	document.getElementById('dc_print_user_weight').innerHTML = user['user_weight'] + ' kg';

    /*if(user['max_phe'] > 0)
    {
        document.getElementById('dc_your_phe').innerHTML = user['max_phe'] + " mg";
        document.getElementById('dc_print_user_phe').innerHTML = user['max_phe'];
    }
    else
    {
        document.getElementById('dc_your_phe').innerHTML = '';
        document.getElementById('dc_print_user_phe').innerHTML = '';
    }*/



    if($('#additional_param_' + $('#disease').val()).val() > 0)
    {
        var additional_param = parseFloat($('#additional_param_' + $('#disease').val()).val());
        document.getElementById('dc_your_phe').innerHTML = additional_param.toFixed(2) + " g";
        document.getElementById('dc_print_user_phe').innerHTML = additional_param.toFixed(2);
    }
    else
    {
        document.getElementById('dc_your_phe').innerHTML = '';
        document.getElementById('dc_print_user_phe').innerHTML = '';
    }

	
	
	
	$(function()
	{
		if(user['max_phe'] > 0 || user['max_protein'] > 0 || user['max_protein_pku'] > 0)
		{
			$('#dc_user_values_empty').hide();
			$('#dc_user_values_table').show();
		}
		else
		{
			$('#dc_user_values_table').hide();
			$('#dc_user_values_empty').show();
		}
	});
}

function dc_validate_data(val, min_val, max_val, name)
{
	if(val > max_val || val < min_val)
	{
		document.getElementById('dc_' + name).style.color = '#FF0000';
		document.getElementById('dc_overflow_' + name).innerHTML = ' max.: ' + max_val;
		return false;
	}
	else
	{
		document.getElementById('dc_' + name).style.color = '#000000';
		document.getElementById('dc_overflow_' + name).innerHTML = '';
		return true;
	}
}


function check_user_data()
{
	
	user_name = document.getElementById('dc_your_name').value;
	user_age = document.getElementById('dc_your_age').value;
	if(document.getElementById('dc_your_age').value == 1)
	{
		user_month = document.getElementById('dc_user_month').value;
		var check_month = true;
	}
	else
	{
		var check_month = false;
	}
	
	kcal = document.getElementById('dc_kcal').value;
	user_weight = document.getElementById('dc_your_weight').value;
	protein = document.getElementById('dc_protein').value;
	
	age_valid = false;
	
	//kcal_valid = false;
	weight_valid = false;
	protein_valid = false;
	
	//if(kcal) kcal_valid = dc_validate_data(kcal, 5000, 'kcal');
	if(protein) protein_valid = dc_validate_data(protein, 1, 1500, 'protein');
	
	if(user_weight)
	{
		max_val = 250;
		if(user_age > max_val)
		{
			document.getElementById('dc_your_weight').style.color = '#FF0000';
			document.getElementById('dc_overflow_weight').innerHTML = ' max.: ' + max_val;
			age_valid = false;
		}
		else
		{
			document.getElementById('dc_your_weight').style.color = '#000000';
			document.getElementById('dc_overflow_weight').innerHTML = '';
			age_valid = true;
		}
	}
	
	if(user_age)
	{
		max_val = 100;
		if(user_age > max_val)
		{
			document.getElementById('dc_your_age').style.color = '#FF0000';
			document.getElementById('dc_overflow_age').innerHTML = ' max.: ' + max_val;
			age_valid = false;
		}
		else
		{
			document.getElementById('dc_your_age').style.color = '#000000';
			document.getElementById('dc_overflow_age').innerHTML = '';
			age_valid = true;
		}
	}
	if(check_month)
	{
		user_month_valid = dc_validate_data(user_month, 0, 24, 'user_month');
	}
	else
	{
		user_month_valid = true;
	}
	

    //protein_pku = document.getElementById('dc_protein_pku').value;
    //phe = document.getElementById('dc_phe').value;

    //protein_pku_valid = false;
    phe_valid = false;


    var additional_param = $('#additional_param_' + $('#disease').val()).val()
    var additional_param_valid = false;

    //if(protein_pku) protein_pku_valid = dc_validate_data(protein_pku, 1500, 'protein_pku');
    //if(phe) phe_valid = dc_validate_data(phe, 1500, 'phe');
    //if(additional_param) additional_param = dc_validate_data(additional_param, 1500, 'additional_param');


    var disease_valid = false;
    if($('#disease').val() != "")
    {
        disease_valid = true;
    }
	

    if(user_name && user_age && age_valid && user_month_valid && user_weight && protein_valid && disease_valid)
    {
        document.getElementById('next_2').style.backgroundColor = '#e10017';
        document.getElementById('next_2').style.color = '#ffffff';
        document.getElementById('next_2').disabled = false;
        return true;
    }
    else
    {
        document.getElementById('next_2').style.backgroundColor = '#bbbbbb';
        document.getElementById('next_2').style.color= '#ffffff';
        document.getElementById('next_2').disabled = true;
        return false;
    }
	
	
}

$(function()
{
	$('#dc_your_age').keyup(function()
	{
		if($('#dc_your_age').val() == 1)
		{
			setTimeout(make_user_month_form, 2000);
		}
		else
		{
			$('#dc_your_age').css('width', '95%');
			$('#dc_user_month').hide();
			$('#dc_user_month_label').hide();
		}
	});
});

function make_user_month_form()
{
	$(function()
	{
		if($('#dc_your_age').val() == 1)
		{
			$('#dc_your_age').css('width', '154px');
			$('#dc_user_month').show();
			$('#dc_user_month_label').show();
		}
	});
}


function additional_param(val)
{
    $('.additional_param_box').hide();
    $('#additional_param_box_' + val).show();

    $('.preparation_additional_param_box').hide();
    $('#preparation_additional_param_box_' + val).show();

    if(val == 1)
    {
        $('.additional_param_name').text('fenyloalanina');
    }
    else if(val == 2)
    {
        $('.additional_param_name').text('leucyna');
    }

    check_user_data();
    global_disease_type = val;
}
