function order_by(orderby, direction) {
    $('table#manage_items').parent().before('<form id="frm_manage_items" action="" method="post"><input type="hidden" name="orderby" value="'+orderby+'" /><input type="hidden" name="direction" value="'+direction+'" /></form>');
    $('#frm_manage_items').submit();
}
function ask_delete(link) {
    if (window.confirm('Are you sure you want to delete this record?'))
        document.location = link;
}

function ajax_vote(id, type, url) {
    $.post(domain + 'ajax_vote', {
        id:id,
        type:type
    }, function(data) {
        //if (data['error']) {
        alert(data['error_message']);
    //            return;
    //        }
    //document.location.href = domain + url;
    }, 'json');
}

function fix_find_clear() {
    if ($('#find_button').length == 0 || $('#search_record').length == 0) return;
    if ($('#search_record').val() != '') $('#find_button').hide(); else $('#clear_button').hide();
}

function fade_error() {
    if ($('.error').length > 0) setTimeout("$('.error').fadeOut(2000)", '5000');
}
function show_required() {
    labels = $('label').filter(function() {
        return $(this).text().charAt(0) == '*';
    });
    if (labels.length > 0) labels.each(function() {
        $(this).parent().prepend('<div class="explanation">(required field)</div>');
    });
}

function check_words_left(obj, id, limit) {
    words = obj.value.split(' ');
    if (obj.value == '') words_now = 0;
    else words_now = words.length;
    words_left = limit - words_now;

    if (words_left < 0) {
        counter = 0;
        ttext = '';
        for (index in words) {
            if (++counter > limit) break;
            ttext += words[index] + ' ';
        }
        ttext = ttext.substr(0, ttext.length - 1);
        words_left = 0;
        obj.value = ttext;
    }
    
    $('#' + id).text(words_left);
}

function ajax_login() {
    // ajax form
    if ($('#frm_login').length > 0) {
        $('#frm_login').ajaxForm({
            dataType: 'json',
            timeout: 3000,
            beforeSubmit:  function() {
                //$('#submit_login input').hide();
                $('#submit_error').append('<div id="loader" ><img src="' + base + 'images/ajax-loader.gif"> Logging in..</div>');
            },
            success:       function(data) {
                if (data['error']) {
                    $('#submit_login #loader').remove();
                    $('#submit_login input').show();
                    $('#submit_error').html('<span style="color: #f06da9">'+data['error_message']+'</span>');
                }
                //else window.location.reload();
                else document.location.href=base + 'admin';
            }
        });
    }
}

function submit_search(obj) {
    if (typeof(obj) == 'undefined') obj = this;
    search_word = $(obj).find('.search_field').val();
    search_word = trim(search_word);
    if (search_word == '') return false;
    if (isFinite(search_word)) search_word = 'results-' + search_word;

    search_word = urlencode(search_word);
    $(obj).attr('action', domain + 'brokers/' + search_word);
    return true;
}

function urlencode (str) {
    str = (str+'').toString();
    return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').
    replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
}

function trim (str, charlist) {
    var whitespace, l = 0, i = 0;
    str += '';

    if (!charlist) {
        // default list
        whitespace = " \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000";
    } else {
        // preg_quote custom list
        charlist += '';
        whitespace = charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '$1');
    }

    l = str.length;
    for (i = 0; i < l; i++) {
        if (whitespace.indexOf(str.charAt(i)) === -1) {
            str = str.substring(i);
            break;
        }
    }

    l = str.length;
    for (i = l - 1; i >= 0; i--) {
        if (whitespace.indexOf(str.charAt(i)) === -1) {
            str = str.substring(0, i + 1);
            break;
        }
    }

    return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
}

function limit_text() {
    if ($('#phone_area').length > 0)
        $('#phone_area').keyup(limit_field).keydown(limit_field);
    if ($('#phone_number').length > 0)
        $('#phone_number').keyup(limit_field).keydown(limit_field);
}

//    if (limitField.value.length > limitNum) {
//        limitField.value = limitField.value.substring(0, limitNum);
//    } else {
//        limitCount.value = limitNum - limitField.value.length;
//    }

function limit_field() {
    if ($(this).attr('id') == 'phone_area')
        length_limit = 4;
    else
    if ($(this).attr('id') == 'phone_number')
        length_limit = 10;
    else return;

    if ($(this).val().length > length_limit) {
        $(this).attr('value', $(this).val().substr(0, length_limit));
    }
}
