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{
            var current_url = window.location.href;
            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+'?'+$.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') continue;
                    h=$.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(function(){
                //$('body').html('<div>'+$('body').html()+'</div>');
                //$('body').append('<div style="width: 200px; height: 30px;position: absolute; top:20%; left: 50%; margin-left: -100px; padding: 20px; background: #fff; border: 3px solid #222; color: #f00; font-weight: bold;">LOADING<br /><br />please wait</div>');
                //$('body :first-child').fadeTo('fast',0.8);
                //$('body :first-child').hide();
                        window.location = url;
			}, 0);
			return true;
	  	}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 ($('#pnum').get(0)){
      current=$('#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 ($(obj).is(':checked')) {
            $(obj).parent().css(css_1);
            var gr=$(obj).attr('check-group');
            if(gr!==undefined){
                $(obj).closest('tr').find('[check-group='+gr+']').not($(obj)).each(function(){
                    $(this).attr('checked',false).parent().css(css_2);
                });
            }
        }else{
            $(obj).parent().css(css_2);
        }

        if (el!==undefined && val!==undefined){
            if (reset_check_group){
                $(obj).closest('tr').find('[check-group='+$(obj).attr('check-group')+']').each(function(){
                    $(this).closest('td').find('input[type=hidden]').val('0');
                });
            }
            $(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();}
};

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

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

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

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


$(function(){
        $('.confirm').each(function(){
            var _s=$(this).attr('onclick');
            $(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;
                $(this).unbind('click').bind('click',_s).click();
            });
        });

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

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


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

        $('span.yesno').each(function(){$(this).replaceWith($(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>');});
        
        $('span.datasource').each(function(){
            eval('var datasource = '+$(this).attr('datasource')+';');
            $(this).html(datasource[$(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()];
};

