function PopulateDropdown(lists,ddlName,selectedValue,options){
    ddlName = $(ddlName.$ID());
    if(options){
        for(var i=0;i< options.length;i++){
            if(options[i].constructor.toString().indexOf("Array")>-1){
                if(selectedValue && selectedValue.toString().length>0 && selectedValue==options[i][0]){
                    ddlName.append(Create("option",options[i][1],{
                        value:options[i][0],
                        selected:true
                    }));
                }else{
                    ddlName.append(Create("option",options[i][1],{
                        value:options[i][0]
                    }));
                }
            }else{
                if(selectedValue && selectedValue.toString().length>0 && selectedValue==options[i]){
                    ddlName.append(Create("option",options[i],{
                        selected:true
                    }));
                }else{
                    ddlName.append(Create("option",options[i]));
                }
            }
        }
    }
    for(i=0;i< lists.length;i++){
        if(lists[i].constructor.toString().indexOf("Array") > -1){
            if(selectedValue && selectedValue.toString().length>0 && selectedValue==lists[i][0]){
                ddlName.append(Create("option",lists[i][1],{
                    value:lists[i][0],
                    selected:true
                }));
            }else{
                ddlName.append(Create("option",lists[i][1],{
                    value:lists[i][0]
                }));
            }
        }else{
            if(selectedValue && selectedValue.toString().length>0 && selectedValue==lists[i]){
                ddlName.append(Create("option",lists[i],{
                    selected:true
                }));
            }else{
                ddlName.append(Create("option",lists[i]));
            }
        }
    }
}
function ClearDropdown(ddlName,removeAll){
    $("option",ddlName.$ID()).each(function(){
        if(removeAll || $(this).val().length>0){
            $(this).remove();
        }
    });
}
function Create(element,content,attributes){
    var build = '<' + element;
    if(attributes){
        jQuery.each(attributes,function(n,v){
            //if(v && v.toString().length>0)
            build += ' ' + n + '="' + v + '"'
        });
    }
    if(content){
        build += '>' + content + '</' + element + '>';
    }else{
        build += '/>'
    }
    return build;
}
function ShowError(message,messageId){
    messageId = (messageId||"message").$ID();
    $(messageId).html('<b>'+message+'</b>').fadeIn("normal",function(){
        $(this).fadeOut(5000);
    });
}

function AjaxStartup(loaderId,callback){
    loaderId = (loaderId||"loader").$ID();
    $(loaderId).ajaxStart(function(){
        $("img",this).show();
        $(this).css({
            "left":GetLeft(116)+"px",
            "top":GetTop($(this))+"px",
            "width":"116px"
        });
        var txt = $('#loaderText',this);
        txt.html("Loading...")
        if(txt.hasClass("error")){
            txt.removeClass("error");
        }
        $(this).show();
    });
    $(loaderId).ajaxStop(function(){
        $(this).hide();
    });
    $(loaderId).ajaxError(function(event, request, settings){      
        $(this).animate({
            left:GetLeft(300)+"px",
            width:"300px"
        },"normal","linear",function(){
            $("img",this).hide();
            $('#loaderText',this).html("");
            $('#loaderText',this).addClass("error");
            $('#loaderText',this).html("Error requesting data ...");
            $(loaderId).fadeOut(5000);
        });
        if(callback && typeof(callback)=='function') callback();
    });
    GetTop = function(O){
        return ($(window).scrollTop() +  (($(window).height()/2) -  (O.height()/2)));
    }
    GetLeft = function(W){
        return ($("#storeArea").length>0) ? ($("#storeArea").position().left+(($("#storeArea").width()/2))-(W/2)) :  ((($(document).width()/2)) - (W/2))
    }
}
function ShowLoader(loaderId,message,width,hideImage,isAnimate,textColor){
    loaderId = (loaderId||"loader").$ID();
    textColor = textColor || "#666666";
    if(hideImage){
        $("img",$(loaderId)).hide();
    } else {
        $("img",$(loaderId)).show();
    }
    var left =($(document).width()/2) - (width/2)
    var top = $(window).scrollTop() +  (($(window).height()/2) -  ($(loaderId).height()/2));
    if(isAnimate){
        $(loaderId).animate({
            "left":left,
            "top":top,
            "width":width+"px"
        },"normal","linear",function(){
            $('#loaderText',this).html(message).css({
                "color":textColor
            });
            $(this).fadeOut(5000);
        });
    }else{
        $(loaderId).css({
            "left":left,
            "top":top,
            "width":width+"px"
        });
        $('#loaderText',$(loaderId)).html(message).css({
            "color":textColor
        });
    }
    $(loaderId).show();
}
var PAGESIZE = 10;
function CreatePagination(CurrentPage,DataPerPage,TotalNoOfData){
    var result = {
        "LeftText":'',
        "RightText":'',
        "CurrentPage":CurrentPage,
        "DataPerPage":DataPerPage,
        "TotalNoOfData":TotalNoOfData,
        "TotalNoOfPage":Math.ceil(TotalNoOfData / DataPerPage),
        "StartRowNumber" : ((CurrentPage-1)*DataPerPage)+1,
        "EndRowNumber" : ((CurrentPage*DataPerPage)>TotalNoOfData ? TotalNoOfData : (CurrentPage*DataPerPage))
    };
    var StartPage = (Math.floor((CurrentPage-1)/PAGESIZE) * PAGESIZE);
    var EndPage = ((StartPage+PAGESIZE)>result.TotalNoOfPage ? result.TotalNoOfPage : StartPage+PAGESIZE );
    var html='';
    if(CurrentPage>PAGESIZE){
        var back = StartPage; //(parseInt(CurrentPage / PAGESIZE) * PAGESIZE); // WHICH one I need .....
        html+=Create("a","Back",{
            "onclick":"ShowFinalOutput("+back+");",
            "title":"Goto Back Page : "+back,
            "href":"javascript:void(0);"
        });
    }
    for(var i=StartPage;i< EndPage;i++){
        var PageNo=i+1;
        if(CurrentPage==PageNo){
            html+=Create("span",PageNo,{
                "class":"on"
            });
        }else{
            html+=Create("a",PageNo,{
                "onclick":"ShowFinalOutput("+PageNo+");",
                "title":"Goto Page : "+PageNo,
                "href":"javascript:void(0);"
            });
        }
    }
    if(result.TotalNoOfPage>EndPage){
        var next = EndPage+1;
        html+=Create("a","Next",{
            "onclick":"ShowFinalOutput("+next+");",
            "title":"Goto Next Page : "+next,
            "href":"javascript:void(0);"
        });
    }
    result.LeftText = '<ul><li><b>Find Result :</b> </li><li>' + result.StartRowNumber + '-' + result.EndRowNumber + ' of ' + result.TotalNoOfData  + '</li></ul>';
    result.RightText = '<div id="page"><b>Go To Page : </b>' + html + '</div>';
    return result;
}
function switchClass(obj,a,r){
    if(a && !obj.hasClass(a)) obj.addClass(a);
    if(r && obj.hasClass(r)) obj.removeClass(r);
}
String.prototype.$ID = function(){
    return ((this.indexOf("#")==0) ? "" : "#" ) + this;
}
/****************************************************************************************************/
/****************************************************************************************************************/
String.prototype.trim = function(){
    return (this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""));
}
String.prototype.startsWith = function(value) {
    return (this.match("^"+value)==value);
}
String.prototype.endsWith = function(value) {
    return (this.match(value+"$")==value);
}

