﻿$(document).ready(function () {
    $('#leftsearch').submit(function () {
        $("#searchurl").val(GetLocation());
        return true;
    });

    $('#feedbackpost').submit(function () {
        $('.validfeed').text('');
        var $f = $(this);
        var action = $f.attr('action');
        var fd = $f.serialize();
        $.post(
            action,
            fd,
            function (data) {
                if (data.errors) {
                    $.each(data.errors, function (key, value) {
                        if (key == 'contact') {
                            $('#feedcontact').text(value);
                            return;
                        }
                        if (key == 'captcha') {
                            $('#feedcontact').text(value);
                            return;
                        }
                        $('#feed' + key + ' .validfeed').text(value);
                    });
                } else {
                    $('#feedcontact').text(data.message);
                }
                $('#captchafeed input').val('');
                $.get(
                    '/getcaptcha/',
                    function (data) {
                        $('#captchafeed .captcha').html(data);
                    }
                );
            },
            "json"
        );
        return false;
    });

    $('#city').change(function () {
        $('#leftsearch').submit();
    })

    $('#metro').change(function () {
        $('#leftsearch').submit();
    })

    $('#changepassword').submit(function () {
        var $f = $('#changepassword');
        var action = $f.attr('action');
        var fd = $f.serialize();
        $.post(
            action,
            fd,
            function (data) {
                if (data.result == "success") {
                    $("#changepasswordsummary").text("Пароль сохранен");
                } else {
                    $("#changepasswordsummary").text("Пароль не сохранен");
                }
            },
            "json"
        );
        return false;
    });
});

function reverse (s) {
    var splitext = s.split("");
    var revertext = splitext.reverse();
    var reversed = revertext.join("");
    return reversed;
}

 var phonechars = '1234567890';

 function ParsePhone(number)
        {
            var phone = "";
            for (var i = 0; i < number.length; i++) {
                if (phonechars.indexOf(number[i])!=-1) { phone += number[i]; }
            }
            return phone;
        }

        function searchbyphone(phone) {
            $('#phone').val(phone);
            $('#phonesearch').submit(); 
        }

function forumsearch(phone) {
    phone = ParsePhone(phone);
    var ln=0;
    if (phone.length >= 10) {
        ln = phone.length - 10;
        phone = phone.substring(ln, ln + 3) + '-' + phone.substring(ln + 3, ln + 6) + '-' + phone.substring(ln + 6, ln + 8) + '-' + phone.substring(ln + 8, ln + 10);
    }
    else
        if (phone.length >= 7) {
            ln = phone.length - 7;
            phone = phone.substring(ln, ln+3) + '-' + phone.substring(ln+3, ln+5) + '-' + phone.substring(ln+5, ln+7);
        }
    window.open('http://forum.amurworld.com/index.php?act=Search&CODE=01&forums=all&keywords=' + phone);
    return false;
}

function GetLocation() {
    return window.location;
}

function confirm_delete(groupid, profiles_count) {
    if (profiles_count == 0) var question = 'Удалить группу?';
    else var question = 'Удалить группу со всеми находящимися в ней анкетами (' + profiles_count + ')?';
    if (window.confirm(question)) {
        $.post(
            '/cabinet/deletegroup/',
            $.toJSON({ 'groupid': groupid }),
            function (data) {
                if (data.result == "success") {
                    $('#gd' + groupid).remove();
                }
            },
            "json"
        );   
    }
    }

function girl_up(userprofile) {
    $.post(
            '/cabinet/upgirl/',
            $.toJSON({ 'userprofile': userprofile }),
            function (data) {
                if (data.result == "success" && data.replace != 0) {
                    //location.reload();
                    $('#girl' + userprofile).insertBefore('#girl' + data.replace);
                }
            },
            "json"
        );
        }

        function rmempty(sel) {
            var id = $(sel).attr('id');
            $('#cusel-scroll-' + id).find('span[value="-1"]').remove();
            var params = {
                refreshEl: '#' + id,
                visRows: 10,
                scrollArrows: false
            };
            cuSelRefresh(params)
        }

function girl_down(userprofile) {
    $.post(
            '/cabinet/downgirl/',
            $.toJSON({ 'userprofile': userprofile }),
            function (data) {
                if (data.result == "success" && data.replace != 0) {
                    $('#girl' + userprofile).insertAfter('#girl' + data.replace);
                }
            },
            "json"
        );
}

function movetogroup(togroup, userprofile) {
    $.post(
            '/cabinet/replaceprofiletogroup/',
            $.toJSON({ 'togroup':togroup, 'userprofile': userprofile }),
            function (data) {
                if (data.result == "success") {
                    location.reload();
                }
            },
            "json"
        );
}

function girl_delete(userprofile) {
    var question = 'Удалить анкету?';
    if (window.confirm(question)) {
        $.post(
            '/cabinet/deletegirl/',
            $.toJSON({ 'userprofile': userprofile }),
            function (data) {
                if (data.result == "success") {
                    location.reload();
                }
            },
            "json"
        );
    }
}

function girlchecked_delete(checkedid) {
    var question = 'Удалить анкету?';
    if (window.confirm(question)) {
        $.post(
            '/cabinet/DeleteChecked/',
            $.toJSON({ 'checkedid': checkedid }),
            function (data) {
                if (data.result == "success") {
                    location.reload();
                }
            },
            "json"
        );
    }
}
