function ajaxSearch(updateId,formId){
    $(formId+" .ui-button-submit").click(function(){
        return loadList(this.href,updateId,formId);
    });
}
function ajaxList(updateId,formId){
    $(updateId+" .ui-pager a").click(function(){
        return loadList(this.href,updateId,formId);
    });
}
function loadList(url,updateId,formId){
    showSpinner();
    $.get(url,$(formId).serialize(),function(html){
        $(updateId).replaceWith(html);
        hideSpinner();
        scroll(0,0);
        $(updateId+" .ui-pager-counter").effect("pulsate");
    });
    return false;
}
function ajaxSubmit(formId){
   $(formId+" .ui-button-submit").click(function(){
        showSpinner();
        $.post($(formId).attr('action'),$(formId+" :input").serialize(),function(responce){
            if(responce.debug){
                $("#debug").html(responce.debug);
            }
            if(responce.status<0) {
                $(formId+" .ui-error-message").addClass("ui-widget ui-state-error ui-corner-all").html(responce.msg).effect("pulsate");
                $(formId+" em").empty();
                if(responce.data){
                    $.each(responce.data,function(key,value){
                        $("#err_"+key,formId).html(value);
                    });
                }
            }
            else{
                $(formId).clearForm();
                $(formId+"-container").replaceWith('<div class="ui-widget ui-state-highlight ui-corner-all ui-thanks-message">'+responce.msg+'</div>');
            }
            hideSpinner();
            scroll(0,0);
        },"json");
        return false;
    });
}
function showSpinner(){$("#main_spinner").show();}
function hideSpinner(){$("#main_spinner").hide();}
function setCharsLimiter(){
    $(".ui-chars-limiter").each( function(index){
        var obj = $(this);
        var limit = obj.attr("maxlength");
        obj.after('<div class="ui-note ui-chars-counter">'+$.charslimiter.charsCount.replace('%d',limit)+'</div>').keyup(function(){
            var text = obj.val();
            if(text.length > limit) {
                obj.next().removeClass('ui-note').addClass("ui-state-error").html($.charslimiter.maxChars.replace('%d',limit));
                obj.val(text.substr(0,limit));
                return false;
            } else {
                obj.next().removeClass("ui-state-error").addClass('ui-note').html($.charslimiter.charsCount.replace('%d',(limit - text.length)));
                return true;
            }
        });
    });
}
function setDateRange(fromDateId,toDateID){
    $(".ui-date-range").datepicker({beforeShow:function(input){return{minDate:(input.id == toDateID ? $("#"+fromDateId).datepicker("getDate") : null),maxDate: (input.id == fromDateId ? $("#"+toDateID).datepicker("getDate") : null)};},showOn:"both",buttonImage:"/img/calendar.gif",buttonImageOnly:true,changeMonth:true,changeYear:true,yearRange:"c-4:c+4",dateFormat:"dd-mm-yy"});
}
function setDatePicker(fromDateId,toDateID){
    $(".ui-date").datepicker({showOn:"both",buttonImage:"/img/calendar.gif",buttonImageOnly:true,changeMonth:true,changeYear:true,yearRange:"c-100:c",dateFormat:"dd-mm-yy"});
}
function setNumeric(){
    $(".ui-numeric").keypress(function(e){if(e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)){return false;}});
}
$.fn.clearForm = function() {
    return this.each(function() {
        var type = this.type, tag = this.tagName.toLowerCase();
        if (tag == "form") return $(":input",this).clearForm();
        if (type == "text" || type == "password" || tag == "textarea")
            this.value = "";
        else if (type == "checkbox" || type == "radio")
            this.checked = false;
        else if (tag == "select")
            this.selectedIndex = -1;
    });
};
function MM_openBrWindow(theURL,winName,features){window.open(theURL,winName,features);}
