//There is a problem rendering the A4J panels in IE 7.
//This adds a catch block to the A4J Javascript
A4J.AJAX.XMLHttpRequest.prototype.updatePagePart=function(id,isLast)
{
    var newnode=this.getElementById(id);
    if(!newnode)
    {
        LOG.error("New node for ID "+id+" is not present in response");
        if(isLast){
            this.doFinish();
        }
        return;
    }
    var oldnode=window.document.getElementById(id);
    if(oldnode){
        if(window.RichFaces&&window.RichFaces.Memory){
            window.RichFaces.Memory.clean(oldnode,true);
        }
        var anchor=oldnode.parentNode;
        if(!window.opera&&!A4J.AJAX.isWebkitBreakingAmps()&&oldnode.outerHTML&&!oldnode.tagName.match(/(tbody|thead|tfoot|tr|th|td)/i)){
            LOG.debug("Replace content of node by outerHTML()");

            if(!Sarissa._SARISSA_IS_IE||oldnode.tagName.toLowerCase()!="table"){
                try{
                    oldnode.innerHTML="";
                }
                catch(e){
                    LOG.error("Error to clear node content by innerHTML "+e.message);
                    Sarissa.clearChildNodes(oldnode);
                }
            }
            try{
                oldnode.outerHTML=new XMLSerializer().serializeToString(newnode);
            }
            catch(e){
                LOG.error("Error to serializeToString "+e.message);
            }
        }else{
            Sarissa.clearChildNodes(oldnode);

            if(A4J.AJAX.isWebKit){
                try{
                    newnode.normalize();
                }catch(e){
                    LOG.error("Node normalization failed "+e.message);
                }
            }

        var importednode=window.document.importNode(newnode,true);
        LOG.debug("Replace content of node by replaceChild()");
        var oldGetElementById=null;
        A4J.AJAX.TestReplacedGetElementByIdVisibility();
        if(!A4J.AJAX._testReplacedGetElementByIdVisibility){
            LOG.debug("Temporarily substituting document.getElementById() to work around WebKit issue");
            oldGetElementById=document.getElementById;
            document.getElementById=function(id){
                var elt=oldGetElementById.apply(document,arguments);
                if(!elt){
                    var id=arguments[0];
                    LOG.debug("Element [@id='"+id+"'] was not found in document, trying to locate XPath match");
                    try{
                        var result=importednode.ownerDocument.evaluate("//*[@id='"+id+"']",importednode,null,XPathResult.ANY_UNORDERED_NODE_TYPE);
                        if(result){
                            elt=result.singleNodeValue;
                        }
                        LOG.debug("XPath located: "+elt);
                    }catch(e){
                        LOG.error("Error locating [@id='"+id+"'] element: "+e.message);
                    }
                }
            return elt;
        };

}
try{
    anchor.replaceChild(importednode,oldnode);
}finally{
    if(oldGetElementById){
        LOG.debug("Restoring document.getElementById()");
        document.getElementById=oldGetElementById;
    }
}
}
if(!A4J.AJAX._scriptEvaluated){
    this.evalScripts(newnode,isLast);
}
LOG.debug("Update part of page for Id: "+id+" successful");
}else{
    LOG.warn("Node for replace by response with id "+id+" not found in document");
    if(!A4J.AJAX._scriptEvaluated&&isLast)

    {
        this.doFinish();
    }
}
if(A4J.AJAX._scriptEvaluated&&isLast)
{
    this.doFinish();
}
}


//There is a problem with SCRIPT438: Object doesn't support this property or method
//3_3_3.Finalorg.ajax4jsf.javascript.AjaxScript, line 169 character 51
//LOG.debug("Hidden JSF state fields: "+idsSpan);	Object doesn't support this property or method	Error Object doesn't support this property or method
// Can't add a string and an object

A4J.AJAX.processResponse=function(req)
{
   
	A4J.AJAX.TestScriptEvaluation();
	var options=req.options;
	var ajaxResponse=req.getResponseHeader('Ajax-Response');
	var expiredMsg=req.getResponseHeader('Ajax-Expired');
	if(expiredMsg&&typeof(A4J.AJAX.onExpired)=='function')
	{
		var loc=A4J.AJAX.onExpired(window.location,expiredMsg);
		if(loc)
		{
			window.location=loc;
			return;
		}
	}
	if(ajaxResponse!="true")
	{
		LOG.warn("No ajax response header ");
		var loc=req.getResponseHeader("Location");
		try
		{
		if(ajaxResponse=='redirect'&&loc)
		{
		window.location=loc;
		}
		else if(ajaxResponse=="reload")
		{
		window.location.reload(true);
		}
		else
		{
		A4J.AJAX.replacePage(req);
		}
		}
		catch(e)
		{
		LOG.error("Error redirect to new location ");
		}
	}
	else
	{
		if(req.getParserStatus()==Sarissa.PARSED_OK)
		{
            if(options.onbeforedomupdate||options.queueonbeforedomupdate)
            {
                var event=req.domEvt;
                var data=req.getJSON('_ajax:data');
                LOG.debug("Call local onbeforedomupdate function before replacing elemements");
                if(options.onbeforedomupdate)
                {
                options.onbeforedomupdate(req,event,data);
                }
                if(options.queueonbeforedomupdate)
                {
                options.queueonbeforedomupdate(req,event,data);
                }
            }
            var idsFromResponse=req.getResponseHeader("Ajax-Update-Ids");
            var ids;
            var callback=function()
            {
                if(A4J.AJAX.headElementsCounter!=0)
                {
                    LOG.debug("Script "+A4J.AJAX.headElementsCounter+" was loaded");
                    --A4J.AJAX.headElementsCounter;
                }
                if(A4J.AJAX.headElementsCounter==0)
                {
                    A4J.AJAX.processResponseAfterUpdateHeadElements(req,ids);
                }
            };
            if(options.affected)
            {
                ids=options.affected;
                req.appendNewHeadElements(callback);
            }
            else if(idsFromResponse&&idsFromResponse!="")
            {
                LOG.debug("Update page by list of rendered areas from response "+idsFromResponse);
                ids=idsFromResponse.split(",");
                req.appendNewHeadElements(callback);
            }
            else
            {
                LOG.warn("No information in response about elements to replace");
                req.doFinish();
            }
            var idsSpan=req.getElementById("ajax-view-state");
                //LOG.debug("Hidden JSF state fields: "+idsSpan);
            if(idsSpan!=null)
            {
                var namespace=options.parameters['org.ajax4jsf.portlet.NAMESPACE'];
                LOG.debug("Namespace for hidden view-state input fields is "+namespace);
                var anchor=namespace?window.document.getElementById(namespace):window.document;
                var inputs=anchor.getElementsByTagName("input");
                try
                {
                    var newinputs=req.getElementsByTagName("input",idsSpan);
                    A4J.AJAX.replaceViewState(inputs,newinputs);
                }
                catch(e)
                {
                    LOG.warn("No elements 'input' in response");
                }
                try
                {
                    var newinputs=req.getElementsByTagName("INPUT",idsSpan);
                    A4J.AJAX.replaceViewState(inputs,newinputs);
                }
                catch(e)
                {
                    LOG.warn("No elements 'INPUT' in response");
                }
            }
            for(var li=0;li<A4J.AJAX._listeners.length;li++)
            {
                var listener=A4J.AJAX._listeners[li];
                if(listener.onafterajax)
                {
                    var data=req.getJSON('_ajax:data');
                    listener.onafterajax(req,req.domEvt,data);
                }
            }
            var focusId=req.getJSON("_A4J.AJAX.focus");
            if(focusId)
            {
                LOG.debug("focus must be set to control "+focusId);
                var focusElement=false;
                if(req.form)
                {
                    focusElement=req.form.elements[focusId];
                }
                if(!focusElement)
                {
                    LOG.debug("No control element "+focusId+" in submitted form");
                    focusElement=document.getElementById(focusId);
                }
                if(focusElement)
                {
                    LOG.debug("Set focus to control ");
                    focusElement.focus();
                    if(focusElement.select)focusElement.select();
                }
                else
                {
                    LOG.warn("Element for set focus not found");
                }
            }
            else
            {
                LOG.debug("No focus information in response");
            }
        }
        else
        {
            LOG.error("Error parsing XML");
            LOG.error("Parse Error: "+req.getParserStatus());
        }
    }
};
//var messages = "";
//LOG.error =function(s)
//{
//    alert('error ' +s );
//    alert(messages);
//    messages = "";
//}
//LOG.warn =function(s)
//{
//    alert('warn ' +s);
//}
//LOG.debug =function(s)
//{
//    messages += s +"\n";
//}

