Core=function(){this.status="init";var registeredModules=[],scripts=[],stylesheets=[],requiredPlugins={},requiredCss={},ajaxCache={},ajaxPolling={},size=function(obj){var i=0;for(var k in obj){i++}return i};this.absPathToFramework="/static/www/js/framework";this.ratesFeeds={hero:"/XMLFeed/portal/hero/xml",personalLoan:"/XMLFeed/portal/personalloans/xml",homeLoan:"/XMLFeed/portal/homeloans/xml",creditCard:"/XMLFeed/portal/creditcards/xml",transactionAccount:"/XMLFeed/portal/transactionaccount/xml",foreignCurrency:"/XMLFeed/portal/fx/xml",foreignCurrencyCallAccount:"/XMLFeed/portal/fccar/xml",termInvestment:"/XMLFeed/portal/terminvestments/xml",savingsCallAccount:"/XMLFeed/portal/savingsaccounts/xml",foreignCurrencyTermDeposit:"/XMLFeed/portal/fcti/xml",foreignCurrencySelect:"/XMLFeed/portal/fcs/xml",unitPrices:"/XMLFeed/portal/unitprices/xml",transactionAccount:"/XMLFeed/portal/transactionaccounts/xml",custom:"/XMLFeed/portal/custom/json"};this.loadScriptFile=function(strUrl,fctCallback){fctCallback=fctCallback||function(){};if(strUrl in scripts&&scripts[strUrl].status in {loaded:"",processing:""}){fctCallback();return}if(!(strUrl in scripts)){scripts[strUrl]={status:"loading",callbacks:[]};var objScript=document.createElement("script");objScript.type="text/javascript";objScript.src=strUrl;var onload=function(){scripts[strUrl].status="processing";scripts[strUrl].callbacks.reverse();var len=scripts[strUrl].callbacks.length;for(var i=0;i<len;i++){var callback=scripts[strUrl].callbacks.pop();callback()}scripts[strUrl].status="loaded"};if(objScript.readyState){objScript.onreadystatechange=function(){if(objScript.readyState=="loaded"||objScript.readyState=="complete"){core.broadcast("core","script-file-loaded","url="+strUrl);objScript.onreadystatechange=null;onload()}}}else{objScript.onload=onload}document.getElementsByTagName("head")[0].appendChild(objScript)}scripts[strUrl].callbacks.push(fctCallback)};var IDCounter=1000;this.getContainerId=function(strPrefix){var scripts=document.getElementsByTagName("script");var parent=scripts[scripts.length-1].parentNode;var id=parent.id;if(id==""){if(strPrefix==null||strPrefix==""){strPrefix="RAND-ID-"}id=strPrefix+IDCounter++;parent.id=id}return id};this.listeners={all:{all:[]}};this.registerListener=function(objParams){var id=objParams.broadcasterId||"all";var eventType=objParams.eventType||"all";this.listeners[eventType]=this.listeners[eventType]||{all:[]};this.listeners[eventType][id]=this.listeners[eventType][id]||[];this.listeners[eventType][id].push(objParams.onTrigger)};this.broadcast=function(broadcasterId,eventType,payload){if(this.listeners[eventType]){if(this.listeners[eventType]){for(var k in this.listeners[eventType].all){this.listeners[eventType].all[k](broadcasterId,eventType,payload)}if(this.listeners[eventType][broadcasterId]){for(var k in this.listeners[eventType][broadcasterId]){this.listeners[eventType][broadcasterId][k](broadcasterId,eventType,payload)}}}}if(this.listeners.all){for(var k in this.listeners.all.all){this.listeners.all.all[k](broadcasterId,eventType,payload)}if(this.listeners.all[broadcasterId]){for(var k in this.listeners.all[broadcasterId]){this.listeners.all[broadcasterId][k](broadcasterId,eventType,payload)}}}};var makeAjaxRequest=function(url,success,error){var request;var success=(success?success:function(){});var error=(error?error:function(){});if(window.XMLHttpRequest){request=new XMLHttpRequest()}else{request=new ActiveXObject("Microsoft.XMLHTTP")}request.onreadystatechange=function(){if(request.readyState==4){if(request.status!=200){error()}else{success(request.responseText)}}};request.open("GET",url,true);request.send()};var makeOptimisedAjaxRequest=function(url,success,error,dataType,skipCache){if(!(url in ajaxCache)){ajaxCache[url]={status:"init",onSuccess:[],onError:[],result:null}}if(!skipCache&&ajaxCache[url].result!=null){success(ajaxCache[url].result)}else{ajaxCache[url].status="pending";makeAjaxRequest(url,function(result){if(dataType=="json"){result=eval("("+result+")")}ajaxCache[url].status="completed";var successCallbacks=ajaxCache[url].onSuccess;for(var i=0;i<successCallbacks.length;i++){successCallbacks[i](result)}ajaxCache[url].result=result;ajaxCache[url].onSuccess=[];ajaxCache[url].onError=[]},function(){ajaxCache[url].status="error";var errorCallbacks=ajaxCache[url].onError;for(var i=0;i<errorCallbacks.length;i++){errorCallbacks[i]()}ajaxCache[url].onError=[];ajaxCache[url].onSuccess=[]})}ajaxCache[url].onSuccess.push(success);ajaxCache[url].onError.push(error)};this.cancelPolling=function(objToken){delete ajaxPolling[objToken.pollingToken.url].registrations[objToken.pollingToken.registrationPosition]};this.ajax=function(objParam){var url=objParam.url,onSuccess=objParam.onSuccess,onError=objParam.onError,dataType=(objParam.dataType?objParam.dataType:"xml"),skipCache=(typeof objParam.skipCache!="undefined"?objParam.skipCache:false),pollingInterval=(objParam.pollingInterval?objParam.pollingInterval:0);if(pollingInterval==0){makeOptimisedAjaxRequest(url,onSuccess,onError,dataType,skipCache);return}if(!(url in ajaxPolling)){ajaxPolling[url]={registrations:[]}}var registrationPosition=ajaxPolling[url].registrations.length;var getReturnValue=function(){return{pollingToken:{url:url,registrationPosition:registrationPosition},cancel:function(){if(ajaxPolling[this.pollingToken.url]){delete ajaxPolling[this.pollingToken.url].registrations[this.pollingToken.registrationPosition]}}}};ajaxPolling[url].registrations.push({onPoll:onSuccess,onError:onError,pollingInterval:pollingInterval});if(ajaxPolling[url].pollingProcess!=null){return getReturnValue()}ajaxPolling[url].pollingInterval=pollingInterval;(function(){var procId;var minPollInterval=pollingInterval;var cancelToken={canceled:false,cancel:function(){clearInterval(procId);this.canceled=true;core.broadcast("ajax","stop","url="+url+",reason=cancel")}};var poll=function(){procId=setTimeout(function(){if(cancelToken.canceled){return}makeOptimisedAjaxRequest(url,function(result){if(cancelToken.canceled){return}var registrationsExist=false;minPollInterval=Number.POSITIVE_INFINITY;for(var k in ajaxPolling[url].registrations){var r=ajaxPolling[url].registrations[k];r.onPoll(result);minPollInterval=minPollInterval<r.pollingInterval?minPollInterval:r.pollingInterval}if(minPollInterval<Number.POSITIVE_INFINITY){poll()}else{core.broadcast("ajax","stop","url="+url+",reason=no listeners")}},function(){if(cancelToken.canceled){return}for(var k in ajaxPolling[url].registrations){ajaxPolling[url].registrations[k].onError()}},dataType,true)},minPollInterval)};ajaxPolling[url].pollingProcess=cancelToken;poll()})();return getReturnValue()};this.registerForCustomRateUpdates=function(objParams){var isFirstCall=false;var isCoreLoaded=core.status=="running";if(typeof this.customRatesUpdater=="undefined"){isFirstCall=true;this.customRatesUpdater={registrations:[],tokens:[],codes:{},url:"",status:"pending"}}var executeCustomRates=function(index){var generateUrl=function(){core.customRatesUpdater.codes={};for(var i in core.customRatesUpdater.registrations){core.customRatesUpdater.codes[core.customRatesUpdater.registrations[i].heroCode]=1}var qry="?getproductsbyheropos=";for(var k in core.customRatesUpdater.codes){qry+=k+","}qry=qry.replace(/(,)$/,"");var url=core.ratesFeeds.custom+qry;core.broadcast("custom-rates-updater","url-generated",url);return url};var cancelAll=function(){core.customRatesUpdater.status="pending";for(var i in core.customRatesUpdater.tokens){core.customRatesUpdater.tokens[i].cancel()}};var registerAjax=function(i){var onPoll=core.customRatesUpdater.registrations[i].onPoll;var onLoad=core.customRatesUpdater.registrations[i].onLoad;var onError=core.customRatesUpdater.registrations[i].onError;var heroCode=core.customRatesUpdater.registrations[i].heroCode;var firstCall=true;var token=core.ajax({url:core.customRatesUpdater.url,dataType:"json",onSuccess:function(data){var payload=data[heroCode];if(typeof payload=="undefined"){onError("rate not found");token.cancel();return}payload._server_time=data.server_time;payload._load_time_epoch_ms=new Date().getTime();if(firstCall){onLoad(payload);firstCall=false}else{onPoll(payload)}},onError:function(){onError("could not load rate")},pollingInterval:objParams.pollingInterval});return token};var registerAll=function(){core.customRatesUpdater.url=generateUrl();for(var i in core.customRatesUpdater.registrations){core.customRatesUpdater.tokens[i]=registerAjax(i)}core.customRatesUpdater.status="loaded"};if(index){var heroCode=core.customRatesUpdater.registrations[index].heroCode;if(heroCode in core.customRatesUpdater.codes){core.customRatesUpdater.tokens[index]=registerAjax(index);return}else{cancelAll();registerAll()}}else{registerAll()}};var index=this.customRatesUpdater.registrations.length;this.customRatesUpdater.registrations.push(objParams);if(isCoreLoaded&&this.customRatesUpdater.status=="loaded"){executeCustomRates(index)}else{if(isFirstCall){core.registerListener({eventType:"loaded",broadcasterId:"core",onTrigger:function(){executeCustomRates()}})}}core.broadcast("custom-rates-updater","module-registered","rate="+objParams.heroCode+",index="+index);return{id:index,cancel:function(){var cru=core.customRatesUpdater;if(cru.tokens[this.id]){cru.tokens[this.id].cancel();cru.tokens[this.id]=null;cru.registrations[this.id]=null;core.broadcast("custom-rates-updater","cancel-registration","pos="+this.id)}else{core.broadcast("custom-rates-updater","cancel-registration-error","pos="+this.id)}}}};this.register=function(objModule){objModule.name=objModule.name||"Anonymous Module";objModule.requiresPlugin=objModule.requiresPlugin||[];objModule.requiresCss=objModule.requiresCss||"";Debug.assertIsString(objModule.id,objModule.name+": Have you given the module an id?");Debug.assertIsString(objModule.name);Debug.assertIsString(objModule.requiresCss);Debug.assert(typeof objModule.requiresPlugin in {string:"",object:""},"ASSERT IS STRING OR ARRAY");Debug.assertIsFunction(objModule.init,objModule.name+": Have you given the module an init() function?");switch(typeof objModule.requiresPlugin){case"string":requiredPlugins[objModule.requiresPlugin]=1;break;case"object":for(var k in objModule.requiresPlugin){requiredPlugins[objModule.requiresPlugin[k]]=1}break}if(objModule.requiresCss!=""){requiredCss[objModule.requiresCss]=1}objModule._status="registered";registeredModules.push(objModule);core.broadcast("core","modules-registered",objModule.id)};var loadCss=function(strCssName){var strUrl=core.absPathToFramework+"/plugins/css/"+strCssName+".css";if(strUrl in stylesheets){return}if(!(strUrl in stylesheets)){stylesheets[strUrl]=1;var objLink=document.createElement("link");objLink.type="text/css";objLink.rel="stylesheet";objLink.href=strUrl;document.getElementsByTagName("head")[0].appendChild(objLink);core.broadcast("core","css-loading","name="+strCssName+",path="+strUrl)}};var loadPlugin=function(strPluginName,fctCallback){var strPluginPath=core.absPathToFramework+"/plugins/js/"+strPluginName+".js";core.loadScriptFile(strPluginPath,function(){fctCallback(strPluginName);core.broadcast("core","plugin-loaded","name="+strPluginName+",path="+strPluginPath)})};this.execute=function(){if(size(registeredModules)==0){return}var fctExecuteModules=function(){for(var key in registeredModules){var module=registeredModules[key];if(module._status=="executed"||module._status=="setup"){continue}module._sandbox=new Sandbox(module.name,module.id);if(module.setup){module.setup(module._sandbox)}module._status="setup"}core.broadcast("core","modules-setup");for(var key in registeredModules){var module=registeredModules[key];if(module._status=="executed"){continue}module.init(module._sandbox);module._status="executed"}core.broadcast("core","modules-executed");core.broadcast("core","loaded");core.status="running"};var fctLoadCss=function(){for(var k in requiredCss){loadCss(k);delete requiredCss[name]}};var fctLoadPlugins=function(fctCallback){if(size(requiredPlugins)==0){fctCallback();return}for(var k in requiredPlugins){loadPlugin(k,function(name){delete requiredPlugins[name];if(size(requiredPlugins)==0){fctCallback()}})}};fctLoadCss();core.loadScriptFile(core.absPathToFramework+"/sandbox.js",function(){core.broadcast("core","sandbox-loaded");Sandbox.setup(function(){core.broadcast("core","sandbox-setup");fctLoadPlugins(function(){core.broadcast("core","plugins-loaded");fctExecuteModules()})})})};this.handleError=function(strMessage,strUrl,intLine){};window.onload=function(){core.execute()};window.onerror=this.handleError};core=new Core();if(typeof Debug=="undefined"){Debug={assert:function(a,b){},assertIsString:function(a,b){},assertIsObject:function(a,b){},assertIsDate:function(a,b){},assertIsFunction:function(a,b){},assertIsNumber:function(a,b){},assertNotEmpty:function(a,b){}}}else{Debug.assert=function(a,b){};Debug.assertIsString=function(a,b){};Debug.assertIsObject=function(a,b){};Debug.assertIsDate=function(a,b){};Debug.assertIsFunction=function(a,b){};Debug.assertIsNumber=function(a,b){};Debug.assertNotEmpty=function(a,b){}};
