jQuery.tii={
    /**
		 * RUP: Replace URL Parameter.
		 * Changes the url paramters that are passed
		 * with json object It keeps the other parameters if they are not set in
		 * the json string Then it redirects the page with GotoURL function
		 *
		 * If the bReturn is set as true, then the function will return the
		 * modified URL hash and will not redirect the page.
         * 
         * h is alternative url to be used instead of the current url
         * 
         * If combine_qs is to true, the querystring from url in h parameter,
         * and the querystring from the current url will be combined, and new url will
         * be parsed and returned. This is good to pass the current qs-parameters to the new url
         * without having to type them all in the new url.
		 *
		 * @param {Object}
		 *            json
         *            Ex-1: [{p:'param1',v:'value1'},{p:'param2',v:'value2'}]
         *            Ex-2: {p1:v1,p2:v2,p3:v3}
		 * @param {boolean}
		 *            bReturn optional
         * @param {string}
         *            h optional
         * @param {boolean}
         *            combine_qs optional
		 */
    RUP : function(json, bReturn, h, combine_qs){
        /* json : [{p:'param1',v:'value1'},{p:'param2',v:'value2'}] */
        if (h==undefined || h=='') {
            h=window.location.href;
        }
        else if(combine_qs !== undefined && combine_qs){
            var current_url = window.location.href.split('#')[0];
            var tmp = current_url.split('?');
            var current_qs = tmp.length>1 ? '&'+tmp[1] : '';  
            tmp = h.split('?');
            var new_url = tmp[0];
            var new_qs = tmp.length>1 ? '&'+tmp[1] : '';
            h = new_url+'?'+jQuery.tii.TrimAll(current_qs+new_qs,'&');
        }
		
        if (json.p!==undefined)
        {
            h=jQuery.tii.SUP(json.p, json.v, h);
        }
        else
        {
            if(json.length !== undefined){
                var v='', p='';
                for(var i=0; i<json.length; i++)
                {
                    v=json[i].v+'';
                    p=json[i].p;
                    //h=jQuery.tii.SUP(p, escape(v.replace('&','&amp;')), h);
                    h=jQuery.tii.SUP(p, escape(v), h);
                }
            }else{
                for(var p in json){
                    if(typeof(p) !== 'undefined') {
                        h=jQuery.tii.SUP(p,escape(json[p]),h);
                    }
                }
            }
        }
        if(bReturn!==undefined && bReturn) return h;
        else return jQuery.tii.GotoURL(h);
    },
    /**
		 * SUP: Set URL Parameter.
		 * Sets the url parameter If there is a set
		 * variable found then updates it otherwise, append the href with name
		 * and value
		 *
		 * @return (string) href
		 */
    SUP:
    function(name,value,h){
        if (typeof h === 'undefined') h=window.location.href;

        var hash='', tmp='';
        if (h.indexOf('#')){
            tmp=h.split('#');
            hash=tmp[1];
            h=tmp[0];
        }
        var oldvalue=jQuery.tii.GUP(name,h);
        if (oldvalue==null){
            if (h.indexOf('?')<0)h+='?';
            h+='&'+name+'='+value;
        }else{
            h=h.replace(name+'='+oldvalue,name+'='+value);
        }
        return h;
    },

    /**
	   * Get URL Parameter
	   * Returns the requested url parameter.
	   * @return (string)
	   */
    GUP:
    function(name,h){
        if (typeof h === 'undefined') h=window.location.href;
        var hash='', tmp='';
        if (h.indexOf('#')){
            tmp=h.split('#');
            hash=tmp[1];
            h=tmp[0];
        }
        var name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
        var regexS = "[\\?&]"+name+"=([^&#]*)";
        var regex = new RegExp( regexS );
        var results = regex.exec( h );
        if( results == null )
            return null;
        else
            return results[1];
    },
      
    /**
      * Returns the QuieryString part of the url
      * @return string
      */
    QS:
    function(h){
        if (typeof h === 'undefined') h=window.location.href;
        if (h.indexOf('#')){
            tmp=h.split('#');
            hash=tmp[1];
            h=tmp[0];
        }
        return h.split('?')[1];
    },

    /**
	   * Redirects the page to the requested URL.
	   */
    GotoURL:
    function(url,new_window,params){
        if (new_window==undefined) new_window=false;

        if (!new_window){
            setTimeout('window.location = "'+url+'"', '0');
            return false;
        }else{
            var w=window.open(url,'popup',params);
            w.focus();
        }
    },

    /**
   * @param {integer} pnum Page Number
   * @param {integer} tnum Total Page Number
   */
    ChangePagination:
    function(pnum,tnum){
        var current='';
        if (jQuery('#pnum').get(0)){
            current=jQuery('#pnum').val();
        }else{
            current=jQuery.tii.GUP('pnum');
        }
        if(current=='')current=1;
        switch(pnum){
            case 'prev':
                pnum=parseInt(current)-1;
                if (pnum<1) return;
                break;
            case 'next':
                pnum=parseInt(current)+1;
                if (tnum!==undefined && tnum<pnum) return;
                break;
        }
        window.location=jQuery.tii.SUP('pnum',pnum);
    },

    ToggleCheckBox:
    function(obj, el, val, reset_check_group){
        var css_1={
            'background-color':'#efefef',
            'color':'#369',
            'border':'1px solid #369'
        };
        var css_2={
            'background-color':'none',
            'color':'#000',
            'border':'none'
        };
        if (jQuery(obj).is(':checked')) {
            jQuery(obj).parent().css(css_1);
            var gr=jQuery(obj).attr('check-group');
            if(gr!==undefined){
                jQuery(obj).closest('tr').find('[check-group='+gr+']').not(jQuery(obj)).each(function(){
                    jQuery(this).attr('checked',false).parent().css(css_2);
                });
            }
        }else{
            jQuery(obj).parent().css(css_2);
        }

        if (el!==undefined && val!==undefined){
            if (reset_check_group){
                jQuery(obj).closest('tr').find('[check-group='+jQuery(obj).attr('check-group')+']').each(function(){
                    jQuery(this).closest('td').find('input[type=hidden]').val('0');
                });
            }
            jQuery(el).val(val);
        }
    },
    
    TrimAll:
    function(str, trim_char) {
        if(typeof trim_char == 'undefined') trim_char = '\s';
        var re = new RegExp('^'+trim_char+'+');
        str = str.replace(re, '');
        
        re = new RegExp('[^'+trim_char+']');
        for (var i = str.length - 1; i >= 0; i--) {
            if (re.test(str.charAt(i))) {
                str = str.substring(0, i + 1);
                break;
            }
        }
        return str;
    },
    
    Reload:
    function(){
        window.location.reload();
    }
};

    jQuery.fn.moveUp = function(callback) {
        var before = jQuery(this).prev();
        jQuery(this).insertBefore(before);
        if(typeof callback == 'function') callback();
    }

    jQuery.fn.moveDown = function(callback) {
        var after = jQuery(this).next();
        jQuery(this).insertAfter(after);
        if(typeof callback == 'function') callback();
    }

    jQuery.fn.outerHTML = function(s) {
        return (s)
        ? this.before(s).remove()
        : jQuery("<p>").append(this.eq(0).clone()).html();
    }

    jQuery.fn.print=
    function(){
        var win = window.open("","printWebPart");
        win.document.open();
        win.document.write('<html><head>'+jQuery(document).find('head').html() + '</head><body>' + jQuery(this).html() + '</body>');
        win.document.close();
        win.print();
        win.close();
    },


    jQuery(function(){
        jQuery('.confirm').each(function(){
            var _s=jQuery(this).attr('onclick');
            jQuery(this).attr('onclick','').bind('click',function(){
                if ( ! confirm('The action you are going to take requires confirmation. Are you sure you want to continue?'))
                    return false;
                jQuery(this).unbind('click').bind('click',_s).click();
            });
        });

        jQuery('button:submit').click(function(){
            if (jQuery(this).parents('form').hasClass('validate') && ! jQuery(this).hasClass('-1')){
                var form = jQuery(this).closest('form.validate').get(0);
                if (! jQuery(form).valid()) return false;

                jQuery(this).parent().append('<input type="hidden" name="'+jQuery(this).attr('name')+'" value="'+jQuery(this).attr('class')+'" />');
                jQuery('button:submit', form).each(function(){
                    jQuery(this).get(0).disabled=true;
                });
            }
            jQuery(form).submit();
        });


        jQuery('td','.drow').hover(function(){
            jQuery(this).parent().addClass('drow_over');
        },function(){
            jQuery(this).parent().removeClass('drow_over');
        });

        jQuery('span.yesno').each(function(){
            jQuery(this).replaceWith(jQuery(this).html()==1 ? '<span style="color: #fff; background-color: #3c3; border: 1px solid #3c3; padding: 2px; font-size: 8px">Yes</span>' : '<span style="color: #fff; background-color: #C33; border: 1px solid #c33; padding: 2px; font-size: 8px">NO</span>');
        });
        
        jQuery('span.datasource').each(function(){
            eval('var datasource = '+jQuery(this).attr('datasource')+';');
            jQuery(this).html(datasource[jQuery(this).html()]);
        });
    });


    Date.prototype.getDayName = function() {
        var d = ['Sunday','Monday','Tuesday','Wednesday',
        'Thursday','Friday','Saturday'];
        return d[this.getDay()];
    };
    Date.prototype.getMonthName = function() {
        var m = ['January','February','March','April','May','June','July',
        'August','September','October','November','December'];
        return m[this.getMonth()];
    };
