//I've been searching for such a possibility recently, didn't find a way to do it with selectors only. However, using the "ends with" selector with a .filter() method seems to do the trick, e.g. the following piece of code will show id of all elements which have the id beginning with "foo" and ending with "bar":

//$("[id^=foo]").filter("[id$=bar]").each( function() {
//  alert($(this).attr('id'));
//});

//    element.find(":contains('yourstring')")   // conatins
//    $( 'select[name^=yourstring]' );          // starts with
//    $( 'select[name$="yourstring"]' );        // ends with

$(document).ready(function(){
    
    fixMarquee();

    fixTextAreaMaxLength();

    activateSearch();

});

function activateSearch(){

    var done = false;

    $('input[name=searchItem]').focus(function() {

        if( $(this).val() == "search" ){
            $(this).val("");
        }

        $(this).keydown(function(e){
            if(e.keyCode == 13) {
                if(!done){
                    done = true;
                    search();
                }
            }
        });

    });

    $('input[name=searchItem]').focusout(function() {
        if( $(this).val() == null || $(this).val() == "" ){
            $(this).val("search");
        }
    });

}

function search(){

    var searchItem = $( 'input[name=searchItem]' ).val();

    //alert(searchItem+'');

    searchItem = encodeURI( searchItem );

    //alert(searchItem+'');

    if(searchItem != null ){
        location.href = "?page=search_action&action=0&searchItem="+searchItem;
    }
}

function fixMarquee(){

    $('marquee').marquee();

    $('marquee').marquee('pointer').mouseover(function () {
        $(this).trigger('stop');
    }).mouseout(function () {
        $(this).trigger('start');
    }).mousemove(function (event) {
        if ($(this).data('drag') == true) {
            this.scrollLeft = $(this).data('scrollX') + ($(this).data('x') - event.clientX);
        }
    }).mousedown(function (event) {
        $(this).data('drag', true).data('x', event.clientX).data('scrollX', this.scrollLeft);
    }).mouseup(function () {
        $(this).data('drag', false);
    });
}

function fixTextAreaMaxLength(){

    $('textarea[maxlength]').keydown(function(){
        var max = parseInt($(this).attr('maxlength'));
        if($(this).val().length > max){
            alert( getDictionaryText('MaxLengthExceeded_txt') );
            $(this).val($(this).val().substr(0, $(this).attr('maxlength')));
        }

        $(this).parent().find('.charsRemaining').html('You have ' + (max - $(this).val().length) + ' characters remaining');
    });

}
function switchToLang(lang){

    var url = '';
    var newUrl = '';

    if( window.location.search ){

        var queryString = window.location.search.substring(1);
        var pairs = queryString.split("&");
        var pair;

        for (i=0;i<pairs.length;i++) {

            pair = pairs[i].split("=");

            if (pair[0] != "lang" && pair[0] != undefined && pair[1] != undefined ) {
                if(i>0){
                    url += "&";
                }
                url += pair[0]+"="+pair[1];
            }
        }

    }

    newUrl += "?"+url+"&lang="+lang;

    //window.location.href = newUrl;
    window.location.search = newUrl;
}

function setPageDirection(dir) {

    if( dir == null ){
        dir = (lang == "ar") ? "rtl" : "ltr";
    }

}

function getVariable(item) {

    if( !window.location.search ){
        return "";
    }

    var queryString = window.location.search.substring(1);
    var pairs = queryString.split("&");
    var pair;

    for (i=0;i<pairs.length;i++) {

        pair = pairs[i].split("=");

        if (pair[0] == item) {
            return pair[1];
        }
    }

    return "";
}


function encode_utf8(s) {
    if (window.encodeURIComponent) {//check fn present in old browser
        return unescape(encodeURIComponent(s));
    } else {
        return escape(s);
    }
}

function decode_utf8(s) {
    if (window.decodeURIComponent) {//check fn present in old browser
        return decodeURIComponent(escape(s));
    } else {
        return unescape(s);
    }
}

function getBriefText(str, tLength){

    if( tLength == null || tLength <= 0 ){
        tLength = 23;
    }

    if( str.length > tLength ){
        str = str.substr(0, tLength-3)+"...";
    }

    return str;
}

function replaceAll(str, find, replace){

    while( str.indexOf(find) != -1 ){
        str = str.replace(find, replace);
    }

    return str;
}

String.prototype.replaceAll = function(find, replace){

    var temp = this;

    var index = temp.indexOf( find );

    while(index != -1){

        temp = temp.replace( find, replace );
        index = temp.indexOf( find );

    }

    return temp;
}

String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

