    function rateComment(rating, pid, mid) {
        $.ajax({
            type: "POST",
            url: ajaxThumbsURL,
            data: "rating=" + rating + "&pid=" + pid + "&mid=" + mid,
            success: function(msg) {
                if(msg!='FALSE') $('#rating_' + pid).html(msg);
            }
        });
    }

    function addReplyForm(id, anchor, avatar) {
        var html = '';
        html += '<div class="oneReview replyForm">';
        html += '<form name="reply_' + id + '" id="reply_' + id + '" method="POST" action="#' + anchor + '">';
        html += '<input type="hidden" name="addReview" value="true" />';
        html += '<input type="hidden" name="pid" value="' + id + '" />';
        html += '<div class="userInfo">';
        html += avatar;
        html += '</div>';
        html += '<textarea name="text" id="text_' + id + '" style="width:385px;"></textarea>';
        html += '<a class="greyBtn" style="margin:10px 0 0; float:right;" onClick="$(\'#reply_' + id + '\').submit();">';
        html += '<div class="l"></div>';
        html += '<div class="c">SUBMIT REPLY</div>';
        html += '<div class="r"></div>';
        html += '</a>';
        html += '</form>';
        html += '</div>';
        $('#review_' + id).after(html);
        $('#text_' + id).elastic().focus();
    }

    function addEditForm(id, anchor, avatar) {
        var html = '';
        html += '<div class="oneReview replyForm">';
        html += '<form name="edit_' + id + '" id="edit_' + id + '" method="POST" action="#' + anchor + '">';
        html += '<input type="hidden" name="editReview" value="true" />';
        html += '<input type="hidden" name="id" value="' + id + '" />';
        html += '<div class="userInfo">';
        html += avatar;
        html += '</div>';
        html += '<textarea name="text" id="text_' + id + '" style="width:385px;"></textarea>';
        html += '<a class="greyBtn" style="margin:10px 0 0; float:right;" onClick="$(\'#edit_' + id + '\').submit();">';
        html += '<div class="l"></div>';
        html += '<div class="c">EDIT COMMENT</div>';
        html += '<div class="r"></div>';
        html += '</a>';
        html += '</form>';
        html += '</div>';
        $('#review_' + id).after(html);
        var text = '';
        
        $('#review_' + id + ' .reviewText p').each(function(index) {
            text += $(this).html() + '\n';
        });
        text = $.trim(text);
        $('#text_' + id).html(text);
        $('#text_' + id).elastic().focus();
    }
    
    function deleteComment(id) {
        var html = '';
        html += '<form name="delete_' + id + '" id="delete_' + id + '" method="POST">';
        html += '<input type="hidden" name="deleteReview" value="true" />';
        html += '<input type="hidden" name="id" value="' + id + '" />';
        html += '</form>';
        $('#review_' + id).after(html);
        var popupText = '';
        popupText += 'Are you sure you want to delete this comment?<br/><br/>';
        popupText += '<a href="#" onClick="$(\'#delete_' + id + '\').submit();">YES, I\'M SURE.</a>'
        $.facebox(popupText);
    }

    function draw_video_player(youtube_id) {
        if (navigator.appVersion.indexOf("Mac")!=-1) {
            var v1 = "";
            var v2 = "";
        } else {
            var v1 = "<param name=\"wmode\" value=\"transparent\"></param>";
            var v2 = "wmode=\"transparent\" ";
        }
        var video = "<object width=\"425\" height=\"344\">"+v1+"<param name=\"movie\" value=\"http://www.youtube.com/v/"+youtube_id+"&autoplay=1&hl=en&rel=0\"></param><embed src=\"http://www.youtube.com/v/"+youtube_id+"&autoplay=1&hl=en&rel=0\" type=\"application/x-shockwave-flash\" "+v2+"width=\"425\" height=\"344\"></embed></object>";
        return video;
    }

    function showLoader(elem) {
        var width = $(elem).width();
        var height = $(elem).height();
        var className = $(elem).attr('class');
        var html = '<div class="loader ' + className + '" style="width:' + width + 'px; height:' + height + 'px"></div>';
        $(elem).replaceWith(html);
    }

    function loadSearchForm() {
        $.Tache.Get({
            type: "POST",
            url: searchFormUrl,
            data: '',
            success: function(msg) {
                $('body').prepend(msg);
            }
        });
    }
    
    function prefetch() {
        var serialized = $('#advancedSearchForm').serialize();
        $.Tache.Get({
            type: "POST",
            url: prefetchSearchUrl,
            data: serialized,
            success: function(msg) {
                handleScreens(msg);
            }
        });
    }
    
    function handleScreens(msg) {
        var screen1 = (msg=='0' ? 'no results' : msg + ' results');
        var screen2 = (msg=='0' ? 'SUBMIT' : 'DISPLAY ' + msg + ' RESULTS');
        if (msg==0) {
            $('#sideResults').addClass('resBarDisabled').removeClass('resBar');
        } else {
            $('#sideResults').addClass('resBar').removeClass('resBarDisabled');
        }
        $('#screen1').html(screen1);
        $('#screen2').html(screen2);
    }

    function clearText(elem) {
        if ($(elem).val()==defaultTexts.keywords) $(elem).val('');
    }

    function restoreText(elem) {
        if ($(elem).val()=='') $(elem).val(defaultTexts.keywords);
    }

    function instrumentInfo(e) {
        var message = '<div style="padding:10px; height:auto; overflow:hidden;">';
        message += '<div style="float:left; width:30px;">';
        message += '<img src="' + LH_PATH + '/images/info.png" />';
        message += '</div>';
        message += 'By changing instrument group all other search form parameters you might have set will be discarded.';
        message += '</div>';
        message += '<div style="color:#999; padding:0 10px 0 40px">';
        message += '<input type="checkbox" value="123" id="instrumentInfoCheck" checked> Don\'t show this info again.';
        message += '</div>';
        message += '</div>';
        if(!readCookie('instrumentInfo')){
            jQuery.facebox(message);
            $('#instrumentInfoCheck').click(function(){
                giveCookie = ($('#instrumentInfoCheck').is(':checked')) ? true : false;
            });
        }
    }

    function changeInstrument(obj) {
        var id = $(obj).val();
        if (id=='') return null;
        window.location = LH_PATH + "/" + id + "/";
        return false;
    }

    function loadSeries(obj) {
        var loadSeriesCache = new Array();
        var id = $(obj).val();
        if (id=='') return null;
        if (loadSeriesCache[id]) {
            $('#series').html(loadSeriesCache[id]);
        } else {
            $.ajax({
                type: "POST",
                url: loadSeriesUrl,
                data: "id=" + id,
                success: function(msg) {
                    loadSeriesCache[id] = '<option>' + defaultTexts.series + '</option>' + msg;
                    $('#series').html(loadSeriesCache[id]);
                }
            });
        }
        return true;
    }

    function loadMultiBox(elem, name, event) {
        if ($('.ddrop').length) return false;
        var html = '';
        var formName = $(elem).prop("id");
        html += '<div class="ddrop">';
        html += '<a href="javascript://" onClick="deselectRows(this, \'' + formName + '\'); return false;" style="font-weight:bold;"><input type="checkbox" onClick="event.stopPropagation(); deselectRows(this, \'' + formName + '\')"/>Deselect All</a>';
        for (var key in multiBoxes[name]) {
            if (multiBoxes[name].hasOwnProperty(key)) {
                html += '<a href="javascript://" onClick="selectRow(this, \'' + formName + '\'); return false;"><input type="checkbox" value="' + key + '" onClick="event.stopPropagation(); updateInput($(this).parent(), \'' + formName + '\')"';
                if (!(mbValues[formName] === null || mbValues[formName] === undefined)) {
                    $.each(mbValues[formName]['idsObj'], function(index, value){
                        html += ('id' + value==key) ? ' checked' : '';
                    });
                }
                html += '/>' + multiBoxes[name][key] + '</a>';
            }
        }
        html += '</div>';
        var offset = $(elem).offset();
        $('body').prepend(html);
        $('.ddrop').css("left", offset.left);
        $('.ddrop').css("top", offset.top + 22);
        $('.ddrop').show();
        event.stopPropagation();
        if (template=='_ajax_search_form') {
            $('body').unbind('click');
            $('body').bind('click', function(event) {
                if(!$(event.target).closest('#sideResults, .centerCol, .ddrop').length) {
                    removeSearchForm();
                    event.stopPropagation();
                };
            });
            $('.centerCol').bind('click', function(event) {
                if(!$(event.target).closest('.ddrop').length) {
                    $('.ddrop').remove();
                    $('.centerCol').unbind('click');
                    event.stopPropagation();
                };
            });
        } else {
            $('body').bind('click', function(event) {
                if(!$(event.target).closest('.ddrop').length) {
                    $('.ddrop').remove();
                    $('body').unbind('click');
                    event.stopPropagation();
                };
            });
        }
        return true;
    }

    function selectRow(elem, name) {
        var input = $(elem).find('input');
        input.is(':checked') ? input.prop("checked", false) : input.prop("checked", true);
        updateInput(elem, name);
        return false;
    }

    function deselectRows(elem, name) {
        $(elem).parent().find('a').each(function(){
            $(this).find('input').prop("checked", false);
        });
        delete mbValues[name];
        $('#' + name).val(defaultTexts[name]);
        if ($('#' + name).val()=='') $('#' + name).val('Select Value');
        $('#' + name + 'Ids').val('');
        prefetch();
    }

    function updateInput(elem, name) {
        var tmpIds = '';
        var tmpIdsObj = {};
        var tmpName = '';
        var id = '';
        var c = 0;
        $(elem).parent().find('a').each(function(){
            if ($(this).find('input').is(':checked')) {
                tmpName += $(this).text() + ', ';
                id = $(this).find('input').val();
                tmpIds += id.substring(2, id.length) + ',';
                tmpIdsObj[c] = id.substring(2, id.length);
                c+=1;
            }
        });
        mbValues[name] = {};
        mbValues[name]['names'] = tmpName.substring(0, tmpName.length-2);
        mbValues[name]['ids'] = tmpIds.substring(0, tmpIds.length-1);
        mbValues[name]['idsObj'] = tmpIdsObj;
        if (!(defaultTexts[name] === null || defaultTexts[name] === undefined)) var defaultVal = defaultTexts[name]; else var defaultVal = 'Select Value';
        mbValues[name]['names']=='' ? $('#' + name).val(defaultVal) : $('#' + name).val(mbValues[name]['names']);
        $('#' + name + 'Ids').val(mbValues[name]['ids']);
        prefetch();
    }

    function jumpToPage(elem, event) {
        var goToBoxHtml = '';
        goToBoxHtml += '<div class="goToBox">';
        goToBoxHtml += '<div class="title">Jump to page</div>';
        goToBoxHtml += '<input type="text" name="goToBoxInput" id="goToBoxInput" value="" />';
        goToBoxHtml += '<a href="#" onClick="jumpToPageGo(); return false;">GO</a>';
        goToBoxHtml += '</div>';
        var offset = $(elem).offset();
        $('body').prepend(goToBoxHtml);
        $('.goToBox').css("left", offset.left + 10);
        $('.goToBox').css("top", offset.top + 10);
        $('.goToBox').show();
        $('#goToBoxInput').focus();
        event.stopPropagation();
        $('body').bind('click', function(event) {
            if(!$(event.target).closest('.goToBox').length) {
                $('.goToBox').remove();
                $('body').unbind('click');
                event.stopPropagation();
            };
        });
        $('body').bind('keypress', function(event) {
                if(event.keyCode == 27) {
                    $('.goToBox').remove();
                    $('body').unbind('keypress');
                    event.stopPropagation();
                }
                if(event.keyCode == 13) {
                    jumpToPageGo();
                    $('body').unbind('keypress');
                    event.stopPropagation();
                }
        });
    }
    
    function jumpToPageGo() {
        var numberRegex = /^[+-]?\d+(\.\d+)?([eE][+-]?\d+)?$/;
        var str = $('#goToBoxInput').val();
        if(numberRegex.test(str)) window.location = jsUrl + str + '/';
    }

    function createCookie(name, value, days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime()+(days*24*60*60*1000));
            var expires = "; expires="+date.toGMTString();
        }
        else var expires = "";
        document.cookie = name+"="+value+expires+"; path=/";
    }

    function readCookie(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++) {
            var c = ca[i];
            while (c.charAt(0)==' ') c = c.substring(1,c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
        }
        return null;
    }

    function eraseCookie(name) {
        createCookie(name,"",-1);
    }
    
    function checkRegisterVars(elem) {
        var val = $(elem).val();
        var name = $(elem).attr('name');
        if(val!='') {
            
            if(name=='email' && !val.match(/^.+@.+\..{2,4}$/)) {
                displayRegError(name, errorMsgs.email);
                return false;
            }
            var url = checkUrl + '&do=' + remap[name];
            var nameVar = name;
            if (nameVar=='username') nameVar = 'name';
            $.ajax({
                type: "POST",
                url: url,
                data: nameVar + "=" + val + "&secure_key=" + form_hash,
                success: function(msg) {
                    if (msg=='notfound') removeRegError(name, 'Available!');
                    else displayRegError(name, msg);
                }
            });
        }
    }
    
    function checkPasswordValid() {
        if($('#password').val().length<3) displayRegError('password', errorMsgs.password);
        else removeRegError('password');
        if ($('#password_control').val()!='') checkPasswordControl();
    }

    function checkPasswordControl() {
        if($('#password').val()!=$('#password_control').val()) displayRegError('password_control', errorMsgs.password_control);
        else removeRegError('password_control');
    }

    function displayRegError(elem, error) {
        $('#' + elem + '_message').fadeOut('fast');
        $('#' + elem + '_message').remove();
        var html = '<div class="message red" id="' + elem + '_message" style="display:none;">' + error + '</div>';
        $('#' + elem).after(html);
        $('#' + elem + '_message').fadeIn('fast');
    }
    
    function removeRegError(elem, message) {
        if (message===undefined) message = '';
        $('#' + elem + '_message').fadeOut('fast');
        $('#' + elem + '_message').remove();
        var html = '<div class="message" id="' + elem + '_message" style="display:none;">' + message + '</div>';
        $('#' + elem).after(html);
        $('#' + elem + '_message').fadeIn('fast');
    }
    
    function hideRegisterForm() {
        $('.rOverlay').remove();
        $('.rOContent').remove();
        $('body').unbind('click');
        $('body').unbind('keyup');
    }
    
    function showRegisterForm(elem) {
        $('body').prepend(rp);
        $(".rOverlay").height($(document).height());
        $('body').bind('click', function(event) {
            if(!$(event.target).closest('.register').length) {
                if(!$(event.target).closest(elem).length) {
                    hideRegisterForm();
                    event.stopPropagation();
                }
            };
        });
        $(document).keyup(function(e) {
            if (e.keyCode==27) hideRegisterForm();
        });
        $('.rOverlay').show();
        $('.rOContent').css('top', $(window).scrollTop());
        $('.rOContent').show();
    }
    
    function switchPopupForms(name) {
        $('#registerDiv').hide();
        $('#loginDiv').hide();
        $('#' + name).show();
        return false;
    }
    
    function starLoader(elem) {
        var width = $(elem).width();
        var height = $(elem).height();
        var html = '<div class="loader" style="width:' + width + 'px; height:' + height + 'px"></div>';
        $(elem).after(html);
        $(elem).hide();
    }
    
    function rate(rating, rid, mid) {
        starLoader($('#rating_' + rid + ' .stars'));
        $.ajax({
            type: "POST",
            url: ajaxRateURL,
            data: "rating=" + rating + "&rid=" + rid + "&mid=" + mid,
            success: function(msg) {
                msg = $.parseJSON(msg);
                    $('#rating_' + rid + ' .numeric').html(msg['rating']);
                    $('#rating_' + rid + ' .current-rating').css('width', msg['rating'] * 25);
                    $('#rating_' + rid + ' .ratedThis').remove();
                if(msg['userRating']!==null) {
                    var plural = '';
                    if (msg['userRating']>1) plural = 's';
                    html = '<div class="ratedThis"><span>You rated ' + msg['userRating'] + ' star' + plural + ' out of 5, ' + msg['timestamp'] + '</span>. <a href="#" onClick="rate(0,' + rid + ',' + mid + '); return false;">Remove rating</a></div>';
                    $('#rating_' + rid).append(html);
                }
                $('#rating_' + rid + ' .loader').remove();
                $('#rating_' + rid + ' .stars').show();
            }
        });
    }
    
    function expLoader(elem) {
        var width = $(elem).width();
        var height = $(elem).height();
        var cssFloat = $(elem).css('float');
        var marginRight = $(elem).css('margin-right');
        var html = '<div class="loader" style="width:' + width + 'px; height:' + height + 'px; float:' + cssFloat + '; margin-right:' + marginRight + '"></div>';
        $(elem).after(html);
        $(elem).hide();
    }
    
    function experience(mid, eid) {
        if(inProgress) return false;
        inProgress = true;
        expLoader($('#exp_' + eid));
        $.ajax({
            type: "POST",
            url: ajaxExperienceURL,
            data: "&mid=" + mid + "&eid=" + eid,
            success: function(msg) {
                $('.expBox .button').removeClass('buttonActive');
                if (msg!='0') $('#exp_' + msg).addClass('buttonActive');
                $('.expBox .loader').remove();
                $('#exp_' + eid).show();
                inProgress = false;
                return true;
            }
        });
        return false;
    }
    
    function createInfographicsLinkBox() {
        var data = '';
        data += '<div class="embed">';
        data += '<p>Embed this image on your site:</p>';
        data += '<textarea rows="3" onclick="this.select();"><a href="' + document.URL + '"><img src="' + $('#infographic').attr('src') + '" alt="' + $('#infographic').attr('alt') + '" border="0" /></a><br />Via: <a href="http://www.chorder.com/">Chorder.com</a></textarea>';
        data += '</div>';
        $(data).insertAfter('#infographic');
    }
