/* Copyright (c) 1995-2008 Choice Hotels International, Inc. All Rights Reserved. */
var XSLTransformer=$Class.create({_ctor:function(stylesheet,preload){if(this._initStylesheet===XSLTransformer.prototype._initStylesheet){return this._morph(stylesheet,preload)}else {this.params=null;this.state=0;this.xml=null;this._setupStylesheet(stylesheet,preload)}},_setupStylesheet:function(stylesheet,preload){if((typeof stylesheet)=="string"){if(preload)this._loadStylesheet(stylesheet);else this.stylesheetToLoad=stylesheet}else this._stylesheetLoaded(stylesheet)},errorXML:function(err){this._displayError(err)},errorXSL:function(err){this._displayError(err)},errorXSLT:function(err){this._displayError(err)},inputXML:function(serial){this._displayXML("Input XML:\n",serial)},inputXSL:function(serial){this._displayXML("Input XSL:\n",serial)},handleResult:function(result){},outputResult:function(serial){this._displayXML("Transform Output:\n",serial)},setDebug:function(errors,io){this.dbgerr=errors;this.dbgio=io},transform:function(xml,params,doc){this.reset();this._setStylesheetParameters(params);this._setupXML(xml);this.state|=1;if(this.state==7){window.setTimeout(this._process.bind(this,doc),0)}else {this.resultdoc=doc;if(this.stylesheetToLoad){var url=this.stylesheetToLoad;this.stylesheetToLoad=null;this._loadStylesheet(url)}}},_setupXML:function(xml){if((typeof xml)=="string")this._loadXML(xml);else if(xml instanceof XSLTransformer){xml.handleResult=this._xmlLoaded.bind(this)}else this._xmlLoaded(xml)},reset:function(){this.state&=2;this.xml=null},_morph:function(stylesheet,preload){if(window.XSLTProcessor&&!$.browser.safari&&!$.browser.opera){return $Class.convert(this,StandardTransformer,stylesheet,preload)}else {if(window.ActiveXObject){var templ=null;var objname=["MSXML2.XSLTemplate.3.0","MSXML2.XSLTemplate"];for(var i=0;i<objname.length;i++){try{templ=new ActiveXObject(objname[i]);break}catch(error){}}if(templ!=null)return $Class.convert(this,IETransformer,stylesheet,preload,templ)}return $Class.convert(this,RemoteTransformer,stylesheet,preload)}},_setStylesheetParameters:$Class.pureVirtual,_parseXML:$Class.pureVirtual,_initStylesheet:$Class.pureVirtual,_stylesheetLoaded:function(xsldom){this.styledoc=null;if(this.dbgio||(this.inputXSL!=XSLTransformer.prototype.inputXSL))this.inputXSL(new SerializedXML(xsldom));this._initStylesheet(xsldom);if(this.params!=null){this._setStylesheetParameters(this.params);this.params=null}this._setStylesheetLoaded()},_loadXML:function(url){var request=new HttpRequest({onSuccess:this._xmlLoadSuccess.bind(this),onFailure:this._xmlLoadFailure.bind(this)});request.send(url,null)},_xmlLoadSuccess:function(request){var parsed=this._parseXML(request.responseText,false);if(parsed.errorCode)this._xmlParseFailure(parsed);else this._xmlLoaded(parsed)},_xmlLoadFailure:function(request){},_xmlParseFailure:function(err){this.errorXML(err)},_xmlLoaded:function(xmldom){this.xml=(xmldom.nodeType==11)?xmldom.firstChild:xmldom;if(this.dbgio||(this.inputXML!=XSLTransformer.prototype.inputXML))this.inputXML(new SerializedXML(this.xml));this._setXMLLoaded()},_setStylesheetLoaded:function(){this.state|=2;this._checkState()},_setXMLLoaded:function(){this.state|=4;this._checkState()},_checkState:function(){if(this.state==7){window.setTimeout(this._process.bind(this,this.resultdoc),0)}},_process:function(resultdoc){this.resultdoc=null;if(!(this.state&8)){this.state|=8;var result=null;try{this._doTransformation(resultdoc,this._resultCallback.bind(this))}catch(error){this.errorXSLT(error)}}},_resultCallback:function(result){if(result){if(this.dbgio||(this.outputResult!=XSLTransformer.prototype.outputResult))this.outputResult(new SerializedXML(result));this.handleResult(result)}},_doTransformation:$Class.pureVirtual,_displayError:function(err){if(this.dbgerr)XSLTransformer.displayError(err)},_displayXML:function(message,serial){if(this.dbgio)XSLTransformer.concatAlert(message,serial.toString())}});XSLTransformer.isAvailable=function(){return ((typeof XSLTProcessor)!="undefined")||window.ActiveXObject};XSLTransformer.fragToDocument=function(frag){var doc=$DOM.newDocument(false);if(doc.importNode){doc.appendChild(doc.importNode(frag,true));doc.readyState=4}else doc.appendChild($DOM.importNode(doc,frag,true));return doc};XSLTransformer.displayError=function(error){var buf=new StringBuffer;if(error.name){buf.append(error.name);buf.append(": ")}if(error.message)buf.append(error.message);if(error.reason&&(error.reason!=error.message)){buf.append(", ");buf.append(error.reason)}if(error.url){buf.append(", Location: ");buf.append(error.url)}if(error.line){buf.append(", at line ");buf.append(error.line)}if(error.linepos){buf.append(", column ");buf.append(error.linepos)}alert(buf.toString())};XSLTransformer.concatAlert=function(){alert($A(arguments).join(""))};var IETransformer=XSLTransformer.extend({_ctor:function(stylesheet,preload,templ){this.$super(stylesheet,preload);this.template=templ},_loadStylesheet:function(url){this.styledoc=$DOM.newDocument(true);this.styledoc.async=true;this.styledoc.onreadystatechange=this._checkReadyState.bind(this,this.styledoc);this.styledoc.load(url)},_checkReadyState:function(doc){if(doc.readyState==4)this._stylesheetLoaded(doc)},_setStylesheetParameters:function(params){if(this.processor){this.processor=this.template.createProcessor();if(params){for(var i=0;i<params.length;i++){if(params[i])this.processor.addParameter(params[i][0],params[i][1])}}this.params=null}else this.params=params},_parseXML:function(text,isxsl){var doc=$DOM.newDocument(isxsl);doc.loadXML(text);return (doc.parseError&&doc.parseError.errorCode&&(doc.parseError.errorCode!=0))?doc.parseError:doc},_initStylesheet:function(xsldom){try{this.template.stylesheet=xsldom;this.processor=this.template.createProcessor()}catch(err){this.errorXSL(err);throw err}},_doTransformation:function(resultdoc,callback){if(this.xml.nodeType!=9)this.xml=XSLTransformer.fragToDocument(this.xml);this.processor.input=this.xml;var result=null;if(!resultdoc){result=$DOM.newDocument(false);this.processor.output=result}this.processor.transform();if(resultdoc){var output=this.processor.output;if(output.substring(0,5)=="<?xml")result=HTMLParse.parseXML(resultdoc,output);else result=HTMLParse.parseHTML(resultdoc,output)}window.setTimeout(callback.bind(null,result),0)}});var StandardTransformer=XSLTransformer.extend({_loadStylesheet:function(url){this.styledoc=$DOM.newDocument(true);if(this.styledoc.load){this.styledoc.async=true;this.styledoc.onload=this._stylesheetLoaded.bind(this,this.styledoc);this.styledoc.load(url)}else {var request=new HttpRequest({onSuccess:this._stylesheetLoadSuccess.bind(this,url)});request.send(url,null)}},_stylesheetLoadSuccess:function(url,request){var parsed=this._parseXML(request.responseText,true);if(parsed.errorCode)this.errorXSL(parsed);else {parsed.documentElement.setAttributeNS("http://www.w3.org/XML/1998/namespace","xml:base",url);this._stylesheetLoaded(parsed)}},_setStylesheetParameters:function(params){if(this.processor){this.processor.clearParameters();if(params){for(var i=0;i<params.length;i++){if(params[i])this.processor.setParameter(null,params[i][0],params[i][1])}}this.params=null}else this.params=params},_parseXML:function(text,isxsl){var parser=new DOMParser();var doc=parser.parseFromString(text,"text/xml");if(doc.documentElement&&(doc.documentElement.tagName=="parseerror")){var errelem=doc.documentElement;var errobj=null;var sourcetxt=null;for(var i=errelem.childNodes.length;i>=0;i--){var child=errelem.childNodes[i];switch(child.nodeType){case 1:{if(child.tagName=="sourcetext")sourcetxt=child.firstChild.nodeValue}case 3:{var regex=/(.*)Location:(.*)Line Number (\d+), Column (\d+):/;if(regex.test(child.nodeValue)){errobj={errorCode:-1,reason:RegExp.$1,url:RegExp.$2,line:parseInt(RegExp.$3),linepos:parseInt(RegExp.$4),srcText:sourcetxt}}}}}if(sourcetxt&&errobj)errobj.srcText=sourcetxt;return errobj}return doc},_initStylesheet:function(xsldom){this.processor=new XSLTProcessor();try{this.processor.importStylesheet(xsldom)}catch(err){this.errorXSL(err);throw err}},_doTransformation:function(resultdoc,callback){window.setTimeout(callback.bind(null,resultdoc?this.processor.transformToFragment(this.xml,resultdoc):this.processor.transformToDocument(this.xml)),0)}});var RemoteTransformer=XSLTransformer.extend({_loadXML:function(xml){this.xmlURL=this._fixRelativePath(xml);this._setXMLLoaded()},_loadStylesheet:function(url){this.stylesheetURL=this._fixRelativePath(url);this._setStylesheetLoaded()},_removePathLeaf:function(path){var last=path.lastIndexOf("/");return (last>=0)?path.substring(0,last):""},_fixRelativePath:function(path){if((path.length>0)&&(path.charAt(0)!='/')&&(path.indexOf(":")<0)){var base=window.location.pathname;base=this._removePathLeaf(base);if(path.startsWith("../")){{base=this._removePathLeaf(base);path=path.substring(3)}(path.startsWith("../"))}path=base+"/"+path}return path},_initStylesheet:function(xsldom){this.stylesheetDOM=xsldom},_setStylesheetParameters:function(params){this.params=params},_doTransformation:function(resultdoc,callback){var rpc=new RPC(RemoteTransformer.transformServlet,{onSuccess:this._transformSuccess.bind(this,resultdoc,callback),onFailure:this._transformFailure.bind(this,callback),onException:this._transformException.bind(this,callback)});rpc.invoke("transform",this.stylesheetURL?this.stylesheetURL:RemoteTransformer.toRemoteNode(this.stylesheetDOM),this.xmlURL?this.xmlURL:RemoteTransformer.toRemoteNode(this.xml),this.params)},_transformSuccess:function(doc,callback,result){if(!doc)doc=$DOM.newDocument(false);callback(RemoteTransformer.fromRemoteNode(doc,result))},_transformFailure:function(callback,httprequest){callback(null)},_transformException:function(callback,ex){callback(null)},_exceptionToString:function(ex){return ex.name+": "+ex.message+(ex.cause?(" \nCaused by: "+this._exceptionToString(ex.cause)):"")}});RemoteTransformer.transformServlet="/xsltransform";RemoteTransformer.toRemoteNode=function(xmldom){if(xmldom.nodeType==9)xmldom=xmldom.firstChild;return (xmldom.nodeType==3)?xmldom.nodeValue:[xmldom.nodeName,xmldom.attributes?RemoteTransformer.toRemoteAttributes(xmldom.attributes):null,xmldom.hasChildNodes()?RemoteTransformer.toRemoteNodeList(xmldom.childNodes):null]};RemoteTransformer.fromRemoteNode=function(doc,data){if(data){if(data instanceof Array){var node=doc.createElement(data[0]);if(data[1]){var attrs=data[1];for(var i=attrs.length-1;i>=0;i--)node.setAttribute(attrs[i][0],attrs[i][1])}if(data[2]){var children=data[2];for(var i=0;i<children.length;i++)node.appendChild(RemoteTransformer.fromRemoteNode(doc,children[i]))}return node}else return doc.createTextNode(data)}return null};RemoteTransformer.toRemoteAttributes=function(attrs){var result=new Array(attrs.length);$iterate(attrs,function(index,attr){result[index]=[attr.nodeName,attr.nodeValue]});return result};RemoteTransformer.toRemoteNodeList=function(nodes){var result=new Array(nodes.length);$iterate(nodes,function(index,node){result[index]=RemoteTransformer.toRemoteNode(node)});return result};var SerializedXML=$Class.create({_ctor:function(xmldom){this.xmldom=xmldom;this.serial=null},toString:function(){if(!this.serial){this.serial=window.XMLSerializer?new XMLSerializer().serializeToString(this.xmldom,"text/xml"):this.xmldom.xml}return this.serial}});