function forms_validation() {
    error_message = 'Please complete all mandatory fields.';

    forms = [];
    /*
    forms['frm_forgot_password'] = 'email';
    forms['frm_contact_us'] = 'subject,message';
    forms['frm_login'] = 'email,password';
     */

    // associate the validation functions
    for (form_id in forms)
        if ($('#'+form_id).length > 0)
            $('#'+form_id).submit(function()
            {
                form_id = $(this).attr('id');
                found_all_values = validate_form(form_id, forms[form_id], false);
                if ( ! found_all_values)
                {
                    alert(error_message);
                    return false;
                }
                return true;
            });
}

function validate_form(form_id, the_fields, show_message) {
    error_message = 'Please complete all mandatory fields.';
    fields = the_fields.split(',');

    found_all_values = true;
    for (key in fields)
    {
        field_name = fields[key].trim();
        field_names = [];
        field_names.push('input[name="' + field_name + '"]');
        field_names.push('textarea[name="' + field_name + '"]');
        field_names.push('select[name="' + field_name + '"]');

        value_found = false;
        for (key_field_names in field_names)
        {
            input_field = $('#' + form_id).find(field_names[key_field_names]);
            if (input_field.length > 0) {
                if (input_field.attr('value').trim() != '') value_found = true;
                else input_field.focus();
                break;
            }
        }

        if ( ! value_found) {
            found_all_values = false;
            break;
        }
    }
    if (show_message && ! found_all_values) alert(error_message);
    return found_all_values;
}


String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g,"");
}

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 randomString(len)
{
    //var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
    var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZ";
    var string_length = len;
    var randomstring = '';
    for (var i=0; i<string_length; i++) {
        var rnum = Math.floor(Math.random() * chars.length);
        randomstring += chars.substring(rnum,rnum+1);
    }
    return randomstring;
}


function imgover() {
    img_src = $(this).attr('src');
    if (img_src.indexOf('over') >= 0) return;
    img_src = img_src.substr(0, img_src.length - 4)+'_over.png';
    $(this).attr('src', img_src);
    $(this).addClass('over');
}

function imgout() {
    img_src = $(this).attr('src');
    if (img_src.indexOf('over') < 0) return;
    img_src = img_src.substr(0, img_src.length - 9)+'.png';
    $(this).attr('src', img_src);
    $(this).removeClass('over');
}

function bgover() {
    img_src = $(this).css('background-image');
    img_src = img_src.substr(4, img_src.length - 5);
    if (img_src.indexOf('over') >= 0) return;
    img_src = img_src.substr(0, img_src.length - 4)+'_over.png';
    $(this).css('background-image', 'url('+img_src+')');
}

function bgout() {
    img_src = $(this).css('background-image');
    img_src = img_src.substr(4, img_src.length - 5);

    if (img_src.indexOf('over') < 0) return;
    img_src = img_src.substr(0, img_src.length - 9)+'.png';
    $(this).css('background-image', 'url('+img_src+')');
}

function ask_delete(link) {
    if (window.confirm('Are you sure you want to delete this item?'))
        document.location = link;
}

function select_form_row() {
    $(this).addClass('selected');
    if ($(this).parent().hasClass('row'))
        $(this).parent().addClass('selected');
    else
        $(this).parent().parent().addClass('selected');
}

function deselect_form_row() {
    $(this).removeClass('selected');
    if ($(this).parent().hasClass('row'))
        $(this).parent().removeClass('selected');
    else
        $(this).parent().parent().removeClass('selected');
}

function repeat_item(item_name)
{
    obj_sample = $('#'+item_name+'_sample');
    obj_count = $('#'+item_name+'_count');
    if (obj_sample.length == 0 || obj_count.length == 0) return;
    the_count = parseInt(obj_count.val()) + 1;
    obj_count.attr('value', the_count);

    obj_instance = obj_sample.after(obj_sample.clone());
    obj_instance.attr('id', item_name + the_count).show();
    obj_instance.focus(select_form_row).blur(deselect_form_row);
//$('#'.item_name.'_sample').after(current_sample);
}

function remove_manager(the_etf_id, obj1, obj2) {
    the_manager_name = obj2.attr('manager_name');
    $.post(domain+'etfs/ajax_delete_manager', {
        etf_id: the_etf_id,
        manager_name: the_manager_name
    }, function (data) {
        if (data['error']) {
            alert(data['error_message']); return;
        }
        $(obj1).remove();
        $(obj2).remove();
    }, 'JSON');
}

//-------------------- [ PROJECT SPECIFIC ] ------------------------


function check_search_symbol() {
    if ($('#search_symbol').val() != '') $('#frm_symbol').submit();
}
function check_search_text() {
    if ($('#search_text').val() != '') $('#cse-search-box').submit();
}

function ajaxcall(url, reload) {
    if ($('#base_url').length > 0)
        domain = $('#base_url').val();
    else domain = '';
    $.getJSON(domain+url, {}, function(data) {
        if (data['error']) {
            alert(data['error_message']); return;
        }
        if (reload) window.location.reload(false);
    });
}

function report_spyware(soft_id) {
    ajaxcall('software/ajax_report/'+soft_id);
}

function submit_multiple(action) {
    base = $('#base_url').val();
    check_boxes = $('#manage_items input[type=checkbox]:checked');
    if (check_boxes.length == 0)
        alert('Please select at least one record to ' + action);
    else
    if (window.confirm('Are you sure you want to '+action+' the selected items?')) {
        module = document.location.href.substr(base.length);
        $('#manage_items').wrap('<form method="POST" id="multi_'+action+'" action="'+
            base+module+'/multi_'+action+'"></form>');
        $('#multi_'+action).submit();
    }
}

function add_multiple_item() {
    sample = $(this).parent().find('.sample_row').html();
    $(this).before('<div class="row">'+sample+'</div>');
    $('.multiple_fields a.delete_multiple_field').click(delete_multiple_item);

    $('.frm_update input')
    .focus(function() {
        $(this).parent().addClass('selected')
    })
    .blur(function() {
        $(this).parent().removeClass('selected')
    });
}

function delete_multiple_item() {
    if ($(this).parent().find('input').val() != '' &&
        ! window.confirm('Are you sure you want to delete this item?')) return;
    orig_item = $(this).parent().find('input[orig_id]');
    if (orig_item.length > 0) {
        base = $('#base_url').val();
        orig_id_val = orig_item.attr('orig_id');
        this_obj = $(this);
        $.getJSON(base + 'software/ajax_delete/' + orig_id_val, {}, function(data) {
            if (data['error']) {
                alert(data['error_message']);
                return;
            }
            container = this_obj.parent().parent();
            this_obj.parent().remove();
            if (container.find('.row').not('.sample_row').length == 0)
                container.find('a.indent').each(add_multiple_item);
        });
    }
    else {
        container = $(this).parent().parent();
        $(this).parent().remove();
        if (container.find('.row').not('.sample_row').length == 0)
            container.find('a.indent').each(add_multiple_item);
    }
}


function create_dialog(selector) {
    $(selector).dialog({
        autoOpen: false,
        bgiframe: true,
        width: 500,
        modal: true,
        buttons: {
            'Submit': function() {
                $.post($('#base_url').val()+'software/referto', {
                    soft_id:$('#page_soft_id').val(),
                    email: $('#friend_email').val(),
                    captcha: $('#captcha').val()
                }, function(data) {
                    if (data['error'] == false) $('#friend_email').attr('value', '');
                    alert(data['message']);
                }, 'json');
                $(this).dialog('close');
                $('#captcha').attr('value', '');
            },
            Cancel: function() {
                $(this).dialog('close');
            }
        }
    });
   
}