function profileEdit() {
	$.get( '/ajax/User/getProvinces', profileFillProvinces);
	var fields = ['name', 'email', 'birth_date'];
	var names = ['Imię i nazwisko', 'E-mail', 'Data urodzenia'];
	var classes = ['', 'required email', 'date'];
	for (var i=0; i<fields.length; i++) {
		var field = $('#'+fields[i]);
		field.attr( 'class', field.text()).html( '<label for="'+fields[i]+'Field">'+names[i]+': </label> <input type="text" name="'+fields[i]+'" id="'+fields[i]+'Field" value="'+escapeVal( field.text())+'" class="'+classes[i]+'" />');
	}
	var field = $('#height');
	field.html( '<label for="heightField">Wzrost [cm]: </label> <input type="text" name="height" id="heightField" value="'+escapeVal( field.attr( 'class'))+'" />');
	var other = '<label for="password">Hasło: </label> <input type="password" name="password" id="password" /><br />';
	other += '<label for="password2">Powtórz hasło: </label> <input type="password" name="password2" id="password2" /><br />';
	other += '<label for="showName">Pokazuj imię i nazwisko: </label><input type="checkbox" name="show_name" id="showName" '+((profileShowName) ? ' checked="checked"' : '')+' /><br />';
	other += '<label for="showBmi">Pokazuj moje BMI: </label><input type="checkbox" name="show_bmi" id="showBmi" '+((profileShowBmi) ? ' checked="checked"' : '')+' /><br />';
	
	other += '<label for="avatarFile">Avatar: </label> <input type="file" name="avatar" id="avatarFile" /><br />';
	$('#profileOther').html( other);
	$('#mainSave').removeClass( 'hidden');
	$('#mainCancel').removeClass( 'hidden');
	$('#mainEdit').addClass( 'hidden');
	$('#profileMain .online').addClass( 'hidden');
	$('.editHide').addClass( 'hidden');
}
function profileFillProvinces( msg) {
	var provinces = $('province', msg);
	var out = '<label for="provinceField">Województwo: </label> <select name="province_id" id="provinceField" class="required"><option value="0">wybierz</option>';
	var sel = $('#province').attr( 'class');
	for (var i=0; i<provinces.length; i++) {
		out += '<option value="'+$(provinces[i]).attr( 'id')+'"';
		if (sel == $(provinces[i]).text()) {
			out += ' selected="selected"';
		}
		out += '>'+$(provinces[i]).text()+'</option>';
	}
	out += '</select>';
	$('#province').html( out);
}
function profileCancel() {
	var fields = ['name', 'email', 'birth_date', 'province'];
	for (var i=0; i<fields.length; i++) {
		$('#'+fields[i]).empty().text( $('#'+fields[i]).attr( 'class'));
	}
	$('#height').text( 'Wzrost '+$('#height').attr( 'class')+' cm');
	$('#profileOther').text( '');
	$('#mainSave').addClass( 'hidden');
	$('#mainCancel').addClass( 'hidden');
	$('#mainEdit').removeClass( 'hidden');
	$('#profileMain .online').removeClass( 'hidden');
	$('.editHide').removeClass( 'hidden');
	$('#profileMsg').empty();
}
function profileValidate( form) {
	var valid = formValidate( form);
	var msg = '';
	if (false == valid) {
		msg = 'Pola oznaczone na czerwono zostały wypełnione błędnie<br />';
	}
	$('#password2').removeClass( 'invalid');
	if ($('#password').val() != $('#password2').val()) {
		msg += 'Potwierdzenie hasła się nie zgadza';
		$('#password2').addClass( 'invalid');
		valid = false;
	}
	if (false == valid) {
		$('#profileMsg').html( msg);
		return( false);
	}
	$('#profileMsg').empty();
	return( true);
}

function profileAddDesc() {
	if (false == formValidate( document.getElementById( 'profileAddDescForm'))) {
		return( false);
	}
	var data = {};
	data['descType'] = $('#descType').val();
	data['description'] = $('#description').val();
	$.post( '/ajax/User/addDesc', data, profileAddDescClb);
}

function profileAddDescClb( msg) {
	if ($('error', msg).length > 0) {
		$('#err_msg').html( $('error', msg).text());
		return( true);
	}
	$('#profileInfo').html( $('content', msg).text());
}
function profileDelDesc( item) {
	$.get( '/ajax/User/delDesc/'+item.id.replace( 'desc', ''), profileDelDescClb);
}
function profileDelDescClb( msg) {
	if ($('error', msg).length > 0) {
		$('#err_msg').html( $('error', msg).text());
		return( true);
	}
	$('#profileInfo').html( $('content', msg).text());
}
function profileEditDesc( item) {
	var desc = $(item).find( '.desc');
	$(desc).addClass( 'hidden');
	desc.after( '<textarea cols="50" rows="7">'+$(desc).text()+'</textarea>');
	$(item).find( '.delete, .edit').addClass( 'hidden');
	$(item).find( '.cancel, .save').removeClass( 'hidden');
}
function profileSaveDesc( item) {
	$(item).find( 'textarea').removeClass( 'invalid');
	if ($(item).find( 'textarea').val() == '') {
		$(item).find( 'textarea').addClass( 'invalid');
		return( false);
	}
	var data = {};
	data.descType = item.id.replace( 'desc', '');
	data.description = $(item).find( 'textarea').val();
	$.post( '/ajax/User/saveDesc', data, profileSaveDescClb);
}
function profileSaveDescClb( msg) {
	if ($('error', msg).length > 0) {
		$('#err_msg').html( $('error', msg).text());
		return( true);
	}
	$('#profileInfo').html( $('content', msg).text());
}
function profileCancelDesc( item) {
	$(item).find( 'textarea').remove();
	$(item).find( '.delete, .edit, .desc').removeClass( 'hidden');
	$(item).find( '.cancel, .save').addClass( 'hidden');
}
function profileDelImg( id) {
	var fileId = $('#imagePreview img').attr( 'src').substr( 9);
	$.get( '/ajax/User/delImage/'+fileId, profileDelImgClb);
	$('#imagePreview').empty();
	$('#imageDescription').empty();
	$('#imgDelBtn').hide();
}
function profileDelImgClb( msg) {
	if ($('result', msg).length == 0) {
		return( false);
	}
	var img = parseInt( $('result', msg).text());
	if (img == 0) {
		return( false);
	}
	$('#img'+img).remove();
	if ($('#profileGallery img').length < 3) {
		$('#profieAddImg').removeClass( 'hidden');
	}
}
function profileSaveDiet() {
	var fields = ['weight', 'hip', 'waist', 'bosom', 'thigh', 'arm'];//, 'calf'];
	var data = {};
	for (var i=0; i<fields.length; i++) {
		data[fields[i]] = $('#'+fields[i]).val().replace( ',', '.');
	}
	$.post( '/ajax/User/saveDiet', data, profileSaveDietClb);
}
function profileSaveDietClb( msg) {
	if ($('result', msg).length == 0) {
		return( true);
	}
	if ($('result', msg).text() == '1') {
		$('#dietResults').replaceWith( $('content', msg).text());
		$('#bmi span').text( $('bmi', msg).text());
		$('#current_weight span').text( $('current_weight', msg).text());
	} else {
		$('#err_msg').text( 'Wystąpił błąd');
	}
}
function profileSwitchChart( user, type) {
	var o = $('#chartContainer object');
	if (o.length > 0) {
		$('#chartContainer').html( '<object class="chart" data="/DietChart/'+user+'/'+type+'" type="image/svg+xml" width="'+$(o).attr( 'width')+'" height="'+$(o).attr( 'height')+'"></object>');
	} else {
		$('#chartContainer img').attr( 'src', '/DietChart/'+user+'/'+type);
	}
	$('#chartSwitch .high').removeClass( 'high');
	$('#chartSwitcher'+type).addClass( 'high');
}

// friends grid
function searchOffset( i) {
	var data = {offset:i};
	$.post( '/ajax/User/getFriends', data, searchSubmitClb);
}
function searchSubmitClb( msg) {
	if (!msg) {
		return( false);
	}
	$('#searchResults').html( $('content', msg).text());
}
function userDelFormCheck( form) {
	if (false == formValidate( form)) {
		$('#err_msg').text( 'Wszystkie pola na tym fomularzu są wymagane');
		return( false);
	}
	return( true);
}
