function rate() {
    $('#rateError').hide();
    if (!$('.star:checked').val()) {
        $('#rateError').show();
    } else {
        $.post('rate.php', {
            'data[Rate][lunch_id]': $('#lunch_id').val(),
            'data[Rate][rated_id]': $('#rated_id').val(),
            'data[Rate][rate]': $('.star:checked').val(),
            'data[Rate][comment]': $('#comment').val()
        },
        function(result) {
            if (result == '1')
            {
                parent.tb_remove();
                parent.setTimeout("tb_show('', 'rated.php?height=250&width=400', null)", 1000);
                parent.setTimeout('parent.document.location.reload()', 3000);
            } else {
                alert(result);
            }
        });
    }
}
function saverating() {
    $('#rateError').hide();
    if (!$('.star3:checked').val()) {
        $('#rateError').show();
    } else {
        $.post('editrating.php', {
            'data[Rating][id]': $('#ratingid').val(),
            'data[Rating][rate]': $('.star3:checked').val(),
            'data[Rating][comment]': $('#comment').val()
        },
        function(result) {
            if (result == '1')
            {
                parent.tb_remove();
                parent.setTimeout("tb_show('', 'ratingsaved.php?height=250&width=400', null)", 1000);
                parent.setTimeout('parent.document.location.reload()', 3000);
            } else {
                alert(result);
            }
        });
    }
}
function deleteuser() {
    if (confirm('You are about to delete this user. Do you want to proceed?')) {
        $.post('deleteuser.php', {
            'data[User][id]': $('#userid').val()
        },
        function(result) {
            if (result == '1')
            {
                parent.tb_remove();
                parent.setTimeout("tb_show('', 'userdeleted.php?height=250&width=400', null)", 1000);
                parent.setTimeout('parent.document.location.reload()', 3000);
            } else {
                alert(result);
            }
        });
    }
}
function submitenter(callback, e)
{
    var keycode;
    if (window.event) {
        keycode = window.event.keyCode;
    } else if (e) {
        keycode = e.which;
    } else {
        return true;
    }
    if (keycode == 13) {
        callback();
        return false;
    }
    else {
        return true;
    }
}
function getcookie(name) {
    var start = document.cookie.indexOf(name + "=");
    var len = start + name.length + 1;
    if ((!start) && (name != document.cookie.substring(0, name.length))) {
        return null;
    }
    if (start == -1)
        return null;
    var end = document.cookie.indexOf(';', len);
    if (end == -1)
        end = document.cookie.length;
    return unescape(document.cookie.substring(len, end));
}
function retrieve()
{
    $('#inputError').hide();
    $('#retrieveError').hide();
    $('#email').addClass('form').removeClass('formerror');
    var email = $('#email').val();
    if (email.length == 0)
    {
        $('#inputError').show();
        $('#email').removeClass('form').addClass('formerror');
    }
    else
    {
        $.post('retrieve.php', {
            'data[Retrieve][email]': email
        },
        function(result) {
            if (result == '0')
            {
                $('#retrieveError').show();
            }
            else if (result == '1')
            {
                tb_show('', 'retrieved.php?height=250&width=400', null);
            }
        });
    }
}


function movetogroup(group_id)
{
    var user_id = $('#user_id').val();
    $.post('movetogroup.php', {
        'data[Favourite][group_id]': group_id,
        'data[Favourite][member_id]': user_id
    },
    function(result) {
        if (result == '1')
        {
            parent.tb_remove();
            parent.setTimeout("tb_show('', 'moved.php?height=250&width=400', null)", 1000);
            parent.setTimeout('parent.document.location.reload()', 3000);
        }
    });
}


function login()
{
    $('#emailError').hide();
    $('#passwordError').hide();
    $('#loginError').hide();
    var email = $('#email').val();
    var password = $('#password').val();
    var TB_show = $('#TB_show').val();
    var TB_width = $('#TB_width').val();
    var TB_height = $('#TB_height').val();
    var TB_ifr = $('#TB_ifr').val();
    if ((email.length == 0) || (password.length == 0))
    {
        if (email.length == 0) {
            $('#emailError').show();
        }
        if (password.length == 0) {
            $('#passwordError').show();
        }
    }
    else
    {
        $.post('login.php', {
            'data[Login][email]': email,
            'data[Login][password]': password
        },
        function(result) {
            if (result == '0')
            {
                $('#loginError').show();
            }
            else if (result == '1')
            {
                if (TB_show.length != 0) {
                    parent.document.cookie = 'TB_show=' + TB_show
                    if(TB_width.length != 0) {
                        parent.document.cookie = 'TB_width=' + TB_width
                    }
                    if(TB_height.length != 0) {
                        parent.document.cookie = 'TB_height=' + TB_height
                    }
                    if(TB_ifr.length != 0) {
                        parent.document.cookie = 'TB_ifr=' + TB_ifr
                    }
                }
                parent.document.location.href = 'memberpage.php';
                parent.tb_remove();
            }
        });
    }
}
function toggleAdvSearch() {
    if (document.getElementById('advSearch').style.display == 'none') {
        $('#advSearch').fadeIn(500, null);
        $('#toggleLink').html('[-] Advanced search');
    } else {
        $('#advSearch').fadeOut(500, null);
        $('#toggleLink').html('[+] Advanced search');
    }
}
function toggleOptFeatures() {
    if (document.getElementById('optFeatures').style.display == 'none') {
        $('optFeatures').fadeIn(500, null);
        $('toggleLink').html('[-] Optional features');
    } else {
        $('optFeatures').fadeOut(500, null);
        $('toggleLink').html('[+] Optional features');
    }
}
function inviteandremove() {
    var data = '';
    var a = $('.removecheckbox:checked');
    var b = $('.invitecheckbox:checked');
    var group_id = $('#group_id').val();
    if ((a.length > 0) || (b.length > 0)) {
        if (a.length > 0) {
            if (confirm('You are about to delete these users from your favorites. Do you want to proceed?')) {
                for (i = 0; i < a.length; i++) {
                    data = data + 'data[Remove][user_id][]=' + a[i].value + '&';
                }
                data = data + 'data[Remove][group_id]=' + group_id + '&';
                tb_show('', 'favremove.php?' + data + 'height=250&width=400', null);
                setTimeout('document.location.reload()', 2000);
            }
        } else {
            for (i = 0; i < b.length; i++) {
                data = data + 'data[Lunch][user_id][]=' + b[i].value + '&';
            }
            tb_show('', 'inviteform.php?' + data + 'height=500&width=700', null);
        }
    } else {
        alert('Please check at least one partner');
    }
}
function sendinvitation() {
    $('#timeError').hide();
    $('#dateError').hide();
    $('#placeError').hide();
    $('#addressError').hide();
    var error = false;
    var hour = $('#hour').removeClass('formshorterror').addClass('formshort').val();
    var minute = $('#minute').removeClass('formshorterror').addClass('formshort').val();
    var ampm = $('#ampm').removeClass('formshorterror').addClass('formshort').val();
    var date = $('#date').removeClass('formerror').addClass('form').val();
    var place = $('#place').removeClass('formerror').addClass('form').val();
    var address = $('#address').removeClass('formerror').addClass('form').val();
    var message = $('#message').val();
    if((hour == 'Hour') || (minute == 'Minute') || (ampm == 'AM/PM')) {
        $('#timeError').show();
        error = true;
        $('#hour').removeClass('formshort').addClass('formshorterror');
        $('#minute').removeClass('formshort').addClass('formshorterror');
        $('#ampm').removeClass('formshort').addClass('formshorterror');
    }
    if(date.length == 0) {
        $('#dateError').show();
        error = true;
        $('#date').removeClass('form').addClass('formerror');
    }
    if(place.length == 0) {
        $('#placeError').show();
        error = true;
        $('#place').removeClass('form').addClass('formerror');
    }
    if(address.length == 0) {
        $('#addressError').show();
        error = true;
        $('#address').removeClass('form').addClass('formerror');
    }
    if(!error) {
        var ids = ''
        $('.user_id').each(
            function() {
                ids += this.value + ',';
            });
        $.post('invite.php', {
            'data[Lunch][user_ids]': ids,
            'data[Lunch][hour]': hour,
            'data[Lunch][minute]': minute,
            'data[Lunch][ampm]': ampm,
            'data[Lunch][date]': date,
            'data[Lunch][place]': place,
            'data[Lunch][address]': address,
            'data[Lunch][message]': message
        },
        function(result) {
            if (result == '1') {
                parent.tb_remove();
                parent.setTimeout('parent.document.location.reload()', 1000);
            } else {
                alert(result);
            }
        });
    }
}
function invite() {
    var data = '';
    var a = $('.useridcheckbox:checked');
    if (a.length > 0) {
        for (var i = 0; i < a.length; i++) {
            data = data + 'data[Lunch][user_id][]=' + a[i].value + '&';
        }
        tb_show('', 'inviteform.php?' + data + 'height=500&width=700', null);
    } else {
        alert('Please check at least one partner to invite');
    }
}