function requestComplete( children, index )
{
    var child;
    var index;

    for (index=0; index<children.length; ++index)
    {
        child= children[index];
        if (child.tagName && "SCRIPT"==child.tagName)
        {
            eval( child.innerHTML );
        }
    }
}

function handleMethod()
{
	// TODO should start the progress
	$.getJSON(this.href, {IsAsynPostbak:true},
	function(data) {
		// TODO should end the progress
  			$.each(data.panels, function(i,panel){
  				var panelID = "#" + panel.id;
				//alert(panel.id);
				//alert(panel.html);
				//alert(panel.type);
  				var lastChildID = 0;
  				switch(panel.type)
  				{
  					case "AFTER":
            		$(panelID)
            		.after(panel.html)
            		.each(function(){
						requestComplete(this.previousSibling.childNodes, 0, this.previousSibling.childNodes.length);
            		});
            		break;
            	case "BEFORE":
            		$(panelID)
            		.before(panel.html)
            		.each(function(){
						requestComplete(this.nextSibling.childNodes, 0, this.nextSibling.childNodes.length);
            		});
            		break;
            	case "APPEND":
            		$(panelID)
            		.each(function(){
            			lastChildID = this.childNodes.length;
            		})
            		.append(panel.html)
            		.each(function(){
						requestComplete(this.childNodes, lastChildID, this.childNodes.length);
            		});
            		break;
            	case "REPLACE":
            		$(panelID).replaceWith(panel.html);
            		$(panelID).each(function(){
						requestComplete(this.childNodes, 0, this.childNodes.length);
            		});
            		break;
            	case "SET":
            		$(panelID)
            		.html(panel.html)
					.each(function(){
						requestComplete(this.childNodes, 0, this.childNodes.length);
            		});
            		break;
            }
          });
          
          $(document).ready(function(){
			$(".updatable").partialUpdate(true);
			});
	});
			
	return false;
}
	
$.fn.partialUpdate = function(bind) {
	if(bind)
		return this.bind('click', handleMethod);
	else
		return this.unbind('click', handleMethod);
 };
 