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.
		 *
		 * @param {Object}
		 *            json
		 * @param {boolean}
		 *            bReturn optional
		 */
	RUP : function(json, bReturn, h){
	  	/* json : [{p:'param1',v:'value1'},{p:'param2',v:'value2'}] */
	  	if (h==undefined || h=='') {
	  		h=window.location.href;
	  	}
		
	  	if (json.p!==undefined)
	  	{
	  		url=jQuery.tii.SUP(json.p, json.v, h);
	  	}
	  	else
	  	{
	  		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);
	  		}
	  	}
	  	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='';
	  	if (h.indexOf('#')){tmp=h.split('#');hash=tmp[1];h=tmp[0];}
	    var oldvalue=jQuery.tii.GUP(name);
	    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){
	  	var h=window.location.href;var hash='';
	  	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];
	  },

	  /**
	   * 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(){
				window.location = url;
			}, 0);
			//return true;
	  	}else{
	    	var w=window.open(url,'popup',params);
	    	w.focus();
	  	}
		return false;
	  },

  /**
   * @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);
        }
    }
};

$(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>');});
});