customisation = {};
customisation.dmn = document.location.href.split("/")[2];
customisation.DOMAIN = customisation.dmn.split(":")[0];
customisation.URL = "http://" + customisation.dmn + "/#c";var _global_reference_to_this_jsod;
newsi.JsodProcess=function(loadPath, processId, feedId, feedName, jsodDataManagerListener, maxCheckTime) {
this._json = loadPath;
this._loadFunc = "onLoad";
this._dataManager = jsodDataManagerListener;
this._processId = processId;
this._feedId = feedId;
this._feedName = feedName;
this._eb = new newsi.EventBroadcaster(this);
this._maxchecktime = maxCheckTime;
};
newsi.JsodProcess.prototype.buildTag = function(){
var instance = this;
_global_reference_to_this_jsod = this;
var obj = document.createElement("script");
this.addListener(this._dataManager);
var counter = 0;
with(obj){
setAttribute("type","text/javascript");
setAttribute("charset", "utf-8");
setAttribute("id", "feedId_"+this._feedId);
setAttribute("src",this._json);
}
document.getElementsByTagName("head")[0].appendChild(obj);
window[this._loadFunc]=function(dataobj){
instance[instance._loadFunc](dataobj);
};
function loadChecker(){
counter++;
if(instance._jsodcomplete){
clearInterval(instance._intervalTimer);
instance.removeListener(instance._dataManager);
}
else if(counter == instance._maxchecktime){
clearInterval(instance._intervalTimer);
instance.generateResult(false , {});
instance.removeListener(instance._dataManager);
}
};
this._intervalTimer = setInterval(loadChecker,100);
};
newsi.JsodProcess.prototype.generateResult = function(result, obj){
var broadcastObj = new Object({"success":result,"data":obj,"processId":this._processId,"feedId":this._feedId,"feedName":this._feedName});
this.broadcastMessage('jsodComplete', broadcastObj);
};
newsi.JsodProcess.prototype.onLoad = function(obj){
_global_reference_to_this_jsod._jsodcomplete = true;
_global_reference_to_this_jsod.generateResult(true,obj);
};
function onLoad(obj){
_global_reference_to_this_jsod.onLoad(obj);
};
newsi.JsodLoadManager=function(loadArray, warnTime, failTime, oneOutAllOut) {
this._loadArray = loadArray;
this._currentlyLoadingQueuePosition = this._FEEDLISTID = 0;
this._processQueue = new Array();
this._recievedProcesses = 0;
this._listener = new Object();
this._loopInterval = 100;
this._timer = 0;
this._warnTime = Number((warnTime * 1000) / this._loopInterval);
this._failTime = Number((failTime * 1000) / this._loopInterval);
this._contentLoadComplete = false;
this._eb = new newsi.EventBroadcaster(this);
this._resultJson = new Object();
this._resultDebug = new Object();
this._elapsedTime = new Object;
this._oneOutAllOut = oneOutAllOut;
this._PROCESS_RESULT_OK = "Data Loader successfully recieved JsodProcess ID ";
this._PROCESS_RESULT_FAIL = "Data Loader failed to load JsodProcess ID ";
};
newsi.JsodLoadManager.prototype.init = function() {
var instance = this;
if(this._loadArray.length < 1){
this.broadcastResult('complete', new Object({"success":false}) );
return false;
}
this._listener.jsodComplete=function(broadcastObject){
if(broadcastObject.success){
if(instance._recievedProcesses == (instance._processQueue.length - 1)){
instance.writeElement(broadcastObject.data, broadcastObject.feedId, broadcastObject.feedName);
instance._contentLoadComplete = true;
}
else{
instance.writeElement(broadcastObject.data,broadcastObject.feedId, broadcastObject.feedName);
instance._recievedProcesses++;
if(instance._recievedProcesses < instance._processQueue.length){
instance.processLoadQueue(instance._processQueue[instance._recievedProcesses]);
}
}
}
else{
if(instance._oneOutAllOut){
clearInterval(instance._timer);
instance.broadcastResult('complete', new Object({"success":false,"data":null,"debug":instance._resultDebug}));
return false;
}
else{
instance._recievedProcesses++;
if(instance._recievedProcesses < instance._processQueue.length){
instance.processLoadQueue(instance._processQueue[instance._recievedProcesses]);
}
else if(instance._recievedProcesses == (instance._processQueue.length - 1)){
instance.writeElement(broadcastObject.data, broadcastObject.feedId, broadcastObject.feedName);
instance._contentLoadComplete = true;
}
}
}
};
this._timer = setInterval(checkLoaded,100);
var counter = 0;
function checkLoaded(){
counter++;
if(instance._contentLoadComplete){
instance.broadcastResult('complete', new Object({"success":true,"data":instance._resultJson,"debug":instance._resultDebug}) );
clearInterval(instance._timer);
return true;
}
else{
if(counter == instance._warnTime){}
else if(counter == instance._failTime){
instance.broadcastResult('complete', new Object({"success":false,"data":{}}));
clearInterval(instance._timer);
return false;
}
}
};
var pFailTime = this._failTime/this._loadArray.length;
for (var i in this._loadArray){
this._currentlyLoadingQueuePosition++;
var jsodProcess = new newsi.JsodProcess(this._loadArray[i].url,
this._currentlyLoadingQueuePosition,
this._loadArray[i].id,
this._loadArray[i].name,
this._listener,
pFailTime);
this._processQueue[this._processQueue.length] = jsodProcess;
}
this.processLoadQueue(this._processQueue[this._recievedProcesses]);
};
newsi.JsodLoadManager.prototype.broadcastResult = function( type, obj){
this.broadcastMessage(type,obj);
};
newsi.JsodLoadManager.prototype.processLoadQueue = function(jsodProcess){
this._elapsedTime = new Date();
jsodProcess.buildTag();
};
newsi.JsodLoadManager.prototype.writeElement = function(obj,feedId,feedName){
obj.feedName = feedName;
this._resultJson[feedId] = obj;
};
newsi.JsodLoadManager.prototype.cancelTimer=function(){
clearInterval(this._timer);
};var _global_reference_to_this_capability;
function panicTimeout(errorStr){
clearInterval(_global_reference_to_this_capability._panicInterval);
if(!_global_reference_to_this_capability._jsodcomplete){
_global_reference_to_this_capability.debugPrint("General failure");
_global_reference_to_this_capability.generateResult(false)
}
};
function onTestLoad(obj){
_global_reference_to_this_capability.onTestLoad(obj,true)
};
CapabilityChecker = function(useDebug){
this._isDOM  = 		false;
this._isJSOD =			false;
this._isArraySplice =  false;
this._useDebug = useDebug;
this._jsodcomplete = false;
this._json = "/js/app/customisation/capabilities/data/json.js";
this._intervalTimer = 0;
this._maxchecktime = 50;
this._counter = 0;
this._panicInterval = 0;
this._eb = new newsi.EventBroadcaster(this);
};
CapabilityChecker.prototype.init = function(){
var isSupported = false;
_global_reference_to_this_capability = this;
if(this._useDebug){
var obj = document.createElement("DIV");
obj.setAttribute("id","debug");
document.getElementsByTagName("body")[0].appendChild(obj);
}
if(!this.checkArray()){return false;}
this.checkDOM();
this.checkJSOD();
};
CapabilityChecker.prototype.checkDOM = function(){
this._panicInterval = setTimeout("panicTimeout()",5000);
if(document.getElementById){
this.debugPrint("Passed testing basic getElement functionality");
if(new Error() ){
this._isDOM = true;
this.debugPrint("Passed testing full getElement functionality");
}
else{
this.generateResult(false);
this.debugPrint("Failed testing full getElement functionality");
return false;
}
}
else{
this.debugPrint("Failed testing basic getElement functionality");
this.generateResult(false);
return false;
}
};
CapabilityChecker.prototype.checkJSOD = function(){
this._panicInterval = setTimeout("panicTimeout()",5000);
var loadfunc = 'onTestLoad';
var instance = this;
if(this._isDOM){
var obj = document.createElement("script");
with(obj){
setAttribute("type","text/javascript");
setAttribute("charset", "utf-8");
setAttribute("id", "customisation_jsod_header");
setAttribute("src",this._json);
}
document.getElementsByTagName("head")[0].appendChild(obj);
if(	document.getElementById("customisation_jsod_header")){
this.debugPrint("Passed writing header tag");
}
else{
this.debugPrint("Failed writing header tag");
this.generateResult(false);
return false;
}
window[loadfunc]=function(dataobj){
instance[loadfunc](dataobj);
};
function loadChecker(){
instance._counter++;
if(instance._jsodcomplete){
clearInterval(instance._intervalTimer);
instance.generateResult(true);
}
else if(instance._counter >= instance._maxchecktime){
clearInterval(instance._intervalTimer);
instance.generateResult(false);
}
};
this._intervalTimer = setInterval(loadChecker,100);
}
else{
this.generateResult(false);
return false;
}
};
CapabilityChecker.prototype.onTestLoad = function(obj, isCalledFromDocument ){
if(this._useDebug) {
this.debugPrint("Passed loading JSON data - content in JSON file was '" + obj.ResultSet[0] + "'" );
}
if(isCalledFromDocument){document.onLoad = null;}
this._jsodcomplete = true;
};
CapabilityChecker.prototype.generateResult = function(result){
this.broadcastMessage('complete', result);
};
CapabilityChecker.prototype.debugPrint = function(content){
if(this._useDebug) document.getElementById("debug").innerHTML += content + "<BR />";
};
CapabilityChecker.prototype.checkArray = function(){
if(Array.prototype.splice){
this._isArraySplice = true;
return true;
}
else{
this.generateResult(false);
return false;
}
};CustomisationCookie=function(document,name,hours,path,domain,secure){this._c=new newsi.Cookie({"name":name,"hours":hours,"path":path,"domain":domain,"secure":secure});};
CustomisationCookie.prototype.store=function(){var z="";for(var de in this){if(de!="content"){if((de.charAt(0)=='_')||((typeof this[de])=='function')){continue;};if(z!=""){z+="&"};if(typeof(this[de])=='object'){this[de]="["+this[de].join()+"]";};z+=de+':'+escape(this[de]);};};this._c._content=z;this._c.store();};
CustomisationCookie.prototype.load=function(cookieName){var returned=this._c.load(cookieName);for(var de in this._c.content){this[de]=this._c.content[de];};return returned;};
CustomisationCookie.prototype.remove = function(){this._c.remove();};newsi.View = function(){};
newsi.View.prototype = newsi;
newsi.View.prototype.updateState = function(msg){
this._model.change(msg);
};
newsi.View.prototype.registerModel = function(model){
this._model=model;
};newsi.Model = function(){
this._eb = new newsi.EventBroadcaster(this);
};
newsi.Model.prototype = newsi;
newsi.Model.prototype.broadcast = function(func,obj){
};
CustomisationModel=function(DOMAIN){
this._cookieManager;
this._cookieData;
this._feedList;
this.missingIds;
this._cookieExpiry = 17550;
this._newsFeedQueue;
this._cookieName = "BBCNewsCustomisation";
//this._cookieDomain = ".bbc.co.uk";
this._cookieDomain = DOMAIN;
this._custView;
this._postCoderPath = "http://www.bbc.co.uk/cgi-perl/whereilive/server/postcodedata.pl?tc=ht3:news/default&customer=news_local&loc=";
this._pcErrorTextPrefix = "Sorry, we cannot find ";
this._pcErrorTextSufix = " in our database.  \n\n Please check that you have entered your postcode correctly, e.g. W12 7RJ";
this._feedURLs = new Object({"News":"http://newsrss.bbc.co.uk/feeds/customisation/v1/newsonline_uk_edition/",
"Sport":"http://newsrss.bbc.co.uk/feeds/customisation/v1/sportonline_uk_edition/",
"Weather":"http://newsrss.bbc.co.uk/feeds/customisation/v1/weather/"});
this._testWarnTime = 30;
this._pcWarnTime = 20;
this._feedListWarnTime = 40;
this._loadFeedsWarnTime = 40;
this._testFailTime = 40;
this._pcFailTime = 30;
this._feedListFailTime = 60;
this._loadFeedsFailTime = 80;
this._mainFeedListURL = "http://news.bbc.co.uk/js/app/customisation/r2_1_0/api/feed_list.js?v1";
this._redundantFeedListURL = "http://news.bbc.co.uk/js/app/customisation/r2_1_0/api/redundant_feed_list.js";
this._loadFeeds = false;
this._lookupArr=new Array();
this._user33=false;
};
CustomisationModel.prototype = new newsi.Model();
CustomisationModel.prototype.init = function(){
this._cookieData = new CustomisationCookie(document, this._cookieName, this._cookieExpiry,"/",this._cookieDomain);
if(this._cookieData.load()){
if(!this._cookieData.postcode){
if(this._cookieData.promoPeriod){
var today = new Date().getTime();
if(this._cookieData.promoPeriod < today) {
this._cookieData.show = false;
this._cookieData.store();
}
this.broadcast("update",{state:"cookieInfo",data:this._cookieData});
this.broadcast("update",{state:"showForm"});
}
this._cookieData.ids = new Array();
}
else{
if(!this._cookieData.weatherID) this._cookieData.weather=false;
if(!this._cookieData.ids){this.change({state:"deleteCookie"})};
this.cookieCheck = true;
this.broadcast("update",{state:"cookieInfo",data:this._cookieData});
this.loadFeedsList();
this.checkLocator();
}
}
else{
var timeCookieSet = new Date().getTime();
this._cookieData.promoPeriod = timeCookieSet + 432000000;
this._cookieData.show = true;
this._cookieData.store();
if(this._cookieData.load()){
this._cookieData.ids = new Array();
this.broadcast("update",{state:"cookieInfo",data:this._cookieData});
this.broadcast("update",{state:"showForm"});
}
else{
this.broadcast("update",{state:"cookiesBlocked"});
return false;
}
}
};
CustomisationModel.prototype.checkLocator = function(){
var self = this;
var savedLocationId = locator.getSharedLocationId();
if (this._cookieData === undefined || this._cookieData === null){
return false;
}
if (this._cookieData.postcode === undefined || this._cookieData.postcode === null){
return false;
}
if (savedLocationId === null || savedLocationId === undefined) {
self.setLocatorByPostCode();
}
else {
locator.fetchFromDataSet(savedLocationId, function(data){
// Make sure that data is an object first
if (data === null || data === undefined) {
return false;
}
if (data.id !== self._cookieData.postcode) {
self.setLocatorByPostCode();
}
}, 'wid');
}
};
CustomisationModel.prototype.setLocatorByPostCode = function(){
// Search for the postcode and use the result
// from the API
locator.searchByPostcode(
this._cookieData.postcode,
function( result ) {
if ( result ) {
var loc = loc = new locator.Location(
// loc id
result.loc,
// place name
result.site_name,
// is not a fsssi? i.e. not a forecast location
( result.is_fsssi == "true" ? false : true ),
// the nation id or '0' if not a nation
( result.nation_id ? result.nation_id : 0 )
);
var wids = [];
if ( result.where_i_live ) { wids.push( result.where_i_live ); }
if ( result.where_i_live_alt ) { wids.push( result.where_i_live_alt ); }
// Set the shared loc, will clear all wids etc. in the cookie
locator.setSharedLocationId( loc );
// Set any wids in results using private method
if ( wids.length > 0 ) {
locator._setDataSet( 'WID', wids.join('~') );
}
}
}
);
};
CustomisationModel.prototype.loadFeedsList = function(){
var instance = this;
var feedName = "mainFeedList";
var loadFeedsListListener = new Object();
loadFeedsListListener.complete=function(loaderManagerObject){
if(loaderManagerObject.success){
instance._feedList = loaderManagerObject.data[feedName].ResultSet;
instance.getFeedsByIds();
}else{
instance.broadcast("update",{state:"error",data:"loadFeedsList() feedList load has failed"});
};
feedListLoader.removeListener(this);
};
var feedListLoader = new newsi.JsodLoadManager([{url:this._mainFeedListURL,name:feedName,id:feedName}],this._feedListWarnTime,this._feedListFailTime, true);
feedListLoader.addListener(loadFeedsListListener);
feedListLoader.init();
};
CustomisationModel.prototype.getFeedsByIds = function(){
anticache = function(url){
url = url+"?anticache="+(Math.floor(Math.random()*100000));
return url;
};
var ids = new Array();
for(var c in this._cookieData.ids) ids[c] = this._cookieData.ids[c];
var newsFeeds = new Array();
var instance = this;
function getIDData(node){
if(node.n == "lookup"){
var o = node.nodes;
for(var prop in o){
instance._lookupArr[o[prop].name] = o[prop].value;
}
}
if(node.nodes){
for (var i in node.nodes){
var fObj = node.nodes[i];
var fObjId = fObj.x;
var fObjFeed = fObj.f;
var idLen = ids.length;
for(var n=0; n<idLen; n++){
if(ids[n] == fObjId){
if(fObjId == "33"){
instance._user33=true;
}
if(fObj.f){
var path = fObj.f;
}
else if(fObj.s){
path = instance._lookupArr["s"]+fObj.s;
}
else if(fObj.r){
path = instance._lookupArr["r"]+fObj.r;
}
else if(fObj.c){
path = instance._lookupArr["c"]+fObj.c;
}
else{
if((node.n != "News") && (node.n != "Sport")){
var path = node.n +"/"+ fObj.n;
}
else{
var path = fObj.n;
}
}
var regEx = new RegExp(/ /g);
newsFeeds[newsFeeds.length]={name:fObj.n, id:fObj.x,url:anticache(feedURL + path.replace(regEx,"/").toLowerCase() + "/json.js")};
ids.splice(n,1);
}
}
getIDData(node.nodes[i]);
}
}
};
if(this._cookieData.ids.length > 0){
for(var j in this._feedList){
var feedURL = this._feedURLs[this._feedList[j].n];
getIDData(this._feedList[j]);
}
}
if(this._cookieData.weather){
newsFeeds[newsFeeds.length]={name:"weather",id:"weather",url:anticache(this._feedURLs["Weather"]+this._cookieData.weatherID+"/json.js")}
}
var missingIds = new Array();
if(ids.length > 0) missingIds = ids;
this._newsFeedQueue = newsFeeds;
this.broadcast("update",{state:"feedListParsed",data:this._feedList, missingIds: missingIds });
};
CustomisationModel.prototype.loadRedundantFeeds = function(missingIds){
var instance = this;
var feedName = "redundantFeedList";
var loadRedundantListener = new Object();
var missingIds = missingIds;
loadRedundantListener.complete=function(loaderManagerObject){
if(loaderManagerObject.success){
var missingFeeds = new Array();
var resultSet = loaderManagerObject.data[feedName].ResultSet;
for(var i in resultSet){
for(var j in missingIds){
if(missingIds[j] == resultSet[i].x){
missingFeeds[missingFeeds.length]=resultSet[i];
missingIds.splice(j,1);
}
}
}
if(!missingIds.length){
instance.broadcast("update",{state:"redundantFeedsLoaded",data:missingFeeds});
}else{
missingFeeds[missingFeeds.length] = {n:"Feed could not be located",id:"none"};
instance.broadcast("update",{state:"redundantFeedsLoaded",data:missingFeeds});
}
}else{
instance.broadcast("update",{state:"error", data:"retired feed list failed to load"});
}
redundantFeedsLoader.removeListener(this);
};
var redundantFeedsLoader = new newsi.JsodLoadManager([{url:this._redundantFeedListURL + "?anticache=" + (Math.floor(Math.random()*100000)),name:feedName, id:feedName}],this._feedListWarnTime, this._feedListFailTime,true);
redundantFeedsLoader.addListener(loadRedundantListener);
redundantFeedsLoader.init();
};
CustomisationModel.prototype.loadFeeds = function(){
var instance = this;
var loadFeedsListener = new Object();
instance.broadcast("update",{state:"showBannerAndLoading"});
loadFeedsListener.complete=function(loaderManagerObject){
if(loaderManagerObject.success){
instance.broadcast("update",{state:"feedsLoaded",data:loaderManagerObject.data});
}else{
instance.broadcast("update",{state:"error",data:"loadFeeds() a feed has failed"});
}
feedsLoader.removeListener(this);
};
var feedsLoader = new newsi.JsodLoadManager(this._newsFeedQueue,this._loadFeedsWarnTime,this._loadFeedsFailTime,true);
feedsLoader.addListener(loadFeedsListener);
feedsLoader.init();
};
CustomisationModel.prototype.change = function(obj){
switch(obj.state){
case "init":
this.init();
break;
case "loadFeedList":
this.loadFeedsList();
break;
case "updateCookie":
for(var i in obj.data){this._cookieData[i] = obj.data[i];}
this._cookieData.promoPeriod = undefined;
this._cookieData.store();
this._cookieData.load();
this.checkLocator();
break;
case "getPostcoderData":
this.getPostcoderData(obj.data);
break;
case "GetFeeds":
this.getFeedsByIds();
break;
case "reloadModule":
this.change({state:"updateCookie",data:this._cookieData});
this.change({state:"loadFeedList"});
break;
case "deleteCookie":
this._cookieData.remove();
this._cookieData = new CustomisationCookie(document, this._cookieName, this._cookieExpiry,"/",this._cookieDomain);
var timeCookieSet = new Date().getTime();
this._cookieData.promoPeriod = timeCookieSet + 432000000;
this._cookieData.show = true;
this._cookieData.store();
locator.clearLocation();
if(this._cookieData.load()){
this._loadFeeds = false;
this._cookieData.ids = new Array();
}
else{return false;}
break;
case "getRedundantFeeds":
this.loadRedundantFeeds(obj.data);
break;
case "loadFeeds":
this.loadFeeds();
break;
case "user33":
var newId = obj.data.id;
var tmp = new Array();
for(var id in this._cookieData.ids){
if(this._cookieData.ids[id]  !=  "33"){tmp.push(this._cookieData.ids[id]);}
}
tmp.push(newId);
this._cookieData.ids = tmp;
this._cookieData.store();
this.init();
this._user33 = false;
break;
}
};
CustomisationModel.prototype.getPostcoderData = function(pcValue){
if(pcValue){
var allOK = true;
var pc = pcValue.replace(/ /g, "").toUpperCase();
if (pc.search(/[\\\/\-\+\*\%\£\$\!\^\|\<\>\?\&\=\"\#\[\]]/g) != -1) {
allOK = false;
}
if ((pc.search(/[A-Z]/g) == -1) || (pc.search(/[0-9]/g) == -1)) {allOK = false;}
else if (!(isNaN(pc.charAt(0)))){allOK = false;}
else if ((pc.length < 5) || (pc.length > 8)) {allOK = false;}
if(allOK){
var instance = this;
var conListener = new Object();
conListener.complete=function(loaderManagerObject){
if(loaderManagerObject.success){
if(loaderManagerObject.data.pcode.error){
alert(instance._pcErrorTextPrefix + "\"" + pcValue + "\" " + instance._pcErrorTextSufix);
instance.broadcast("update",{state:"postcodeError"});
return false;
}
else{
var wilLength = -1;
for (var j=0, wilArr; wilArr = loaderManagerObject.data.pcode.wil[j]; j++){wilLength++;}
if((wilLength>=1) && (!document.getElementById("customisemorecontent"))){
instance.broadcast("update",{state:"multiWIL",data:loaderManagerObject.data});
}
else{
var wID =  loaderManagerObject.data.pcode.weatherID;
while(wID.length < 4 ) 	wID = "0"+wID;
var resultObj = {};
resultObj.weatherID =wID;
resultObj.post_town = loaderManagerObject.data.pcode.post_town;
resultObj.id = loaderManagerObject.data.pcode.wil[0].wilid;
resultObj.wilname = loaderManagerObject.data.pcode.wil[0].wilname;
resultObj.weather = true;
resultObj.postcode = loaderManagerObject.data.pcode.postcode;
instance.broadcast("update",{state:"postcodeDataReturned",data:resultObj});
}
}
}else{
instance.broadcast("update",{state:"error",data:"getPostcoderData() postcoder has failed"});
document.getElementById("postcodebutton").disabled = false;
}
postcoder.removeListener(this);
};
var postcoder = new newsi.JsodLoadManager([{url:this._postCoderPath+pcValue,name:"pcode",id:"pcode"}],this._pcWarnTime,this._pcFailTime, true);
postcoder.addListener(conListener);
postcoder.init();
};
if (!allOK) {
alert(this._pcErrorTextPrefix + "\"" + pcValue + "\" " + this._pcErrorTextSufix);
this.broadcast("update",{state:"postcodeError"});
return false;
}
return allOK;
}
};
CustomisationModel.prototype.broadcast = function(func,obj){
this.broadcastMessage(func,obj);
};CustomisationView=function(model){
this.cookieInfo = {};
this._model=model;
this._model.addListener(this);
this.registerModel(this._model);
this.changeObj={};
this.changeObj.state = "init";
this.updateState(this.changeObj);
this.feedCounter = 1;
this.errorMessage = "This service is temporarily unavailable.";
this.missingFeedTag = false;
this.isSubmitting = false;
};
CustomisationView.prototype = new newsi.View();
CustomisationView.prototype.update = function(obj){
switch(obj.state){
case "showForm":
this.makeBanner();
this.showForm();
this.showModule(this.cookieInfo.show);
document.getElementById("customise").style.display ="block";
break;
case "multiWIL":
this.showMultiWIL(obj.data.pcode);
break;
case "click":
if(this.cookieInfo.show) this.showModule(false);
else this.showModule(true);
this.updateState({state:"updateCookie",data:this.cookieInfo});
break;
case "cookieInfo":
this.cookieInfo = obj.data;
break;
case "error":
this.makeBanner();
this.showModule(this.cookieInfo.show);
this.error(obj.data);
document.getElementById("customise").style.display ="block";
break;
case "showBannerAndLoading":
this.makeBanner();
this.showModule(this.cookieInfo.show);
document.getElementById("customise").style.display ="block";
var loadingElement = document.createElement("div");
loadingElement.setAttribute("id","loading");
var loadingImage = document.createElement("img");
loadingImage.src="http://news.bbcimg.co.uk/nol/ukfs_news/bsp/hi/customisation/img/loading.gif";
loadingImage.width ="68";
loadingImage.height = "15";
loadingElement.appendChild(loadingImage);
document.getElementById("main").appendChild(loadingElement);
break;
case "feedsLoaded":
this.contentDataView(obj.data);
break;
case "feedListParsed":
if(obj.missingIds.length == this.cookieInfo.ids.length &&  !this.cookieInfo.weather){
this.updateState({state:"deleteCookie",data:this.cookieInfo});
}else{
this.updateState({state:"loadFeeds"});
}
break;
case "postcodeDataReturned":
var img = document.createElement("img");
if(this.cookieInfo.postcode){img.setAttribute("src","http://www.bbc.co.uk/go/news/int/customisation/1.0/gobuttonchange/-/f/t.gif");}
else{img.setAttribute("src","http://www.bbc.co.uk/go/news/int/customisation/1.0/gobutton/-/f/t.gif");}
img.setAttribute("style","display:none");
document.getElementById("customise").appendChild(img);
document.getElementById("customise").removeChild(img);
if(this.cookieInfo.ids.length > 0)	this.cookieInfo.ids = new Array();
this.cookieInfo.ids[this.cookieInfo.ids.length]=obj.data.id;
for (var i in obj.data) if(i != "id")this.cookieInfo[i] = obj.data[i];
this.updateState({state:"reloadModule",data:this.cookieInfo});
break;
case "postcodeError":
var pcForm = document.getElementById("postcodeform");
pcForm.style.background = "none";
this.isSubmitting=false;
document.getElementById("postcodeValue").focus();
document.getElementById("postcodebutton").disabled = false;
break;
}
};
CustomisationView.prototype.makeBanner = function(obj){
if(this.cookieInfo.postcode) this.bannerText = "YOUR LOCAL NEWS, WEATHER AND SPORT HERE";
else this.bannerText = "ADD YOUR LOCAL NEWS, WEATHER AND SPORT HERE";
var bannerContent = this.buildBanner();
var customiseDiv = document.getElementById("customise");
customiseDiv.innerHTML = bannerContent;
var stateObj = {state:"click"};
evtObj=new newsi.Event();
evtObj.addListener('onclick','displayState',this,'updateShowHide',false,stateObj);
this.mainDiv = document.getElementById("main");
};
CustomisationView.prototype.updateShowHide = function(obj){
var state = {state:obj[0].state};
this.update(state);
};
CustomisationView.prototype.showModule = function(state){
if(state){
buttonTxt="<img src='http://news.bbcimg.co.uk/nol/ukfs_news/bsp/hi/customisation/img/v4_hide.gif'  border='0' alt='HIDE' />";
boxDisp = "show";
}else{
buttonTxt="<img src='http://news.bbcimg.co.uk/nol/ukfs_news/bsp/hi/customisation/img/v4_show.gif'  border='0' alt='SHOW' />";
boxDisp = "hide";
}
newsi.html.showHide("main",boxDisp);
var displayState = document.getElementById("displayState");
displayState.innerHTML = buttonTxt;
this.cookieInfo.show = state;
this.isSubmitting=false;
};
CustomisationView.prototype.showForm = function(){
var cookiePostcode = "";
var aWhatIsThisLink = document.createElement("a");
aWhatIsThisLink.appendChild(document.createTextNode("What's this?"));
aWhatIsThisLink.setAttribute("href","http://news.bbc.co.uk/1/hi/help/5179748.stm");
if(this.cookieInfo.postcode){cookiePostcode = this.cookieInfo.postcode;aWhatIsThisLink = "";}
var enterPostCode = "<div id='postcodeWrapper'><form id='postcodeform'><p>Enter your postcode:<input type='text' name='postcode' class='text' id='postcodeValue' value='"+ cookiePostcode +"' /><input type='button' class='button' name='' value='GO' id='postcodebutton'></p></form></div>";
this.mainDiv.innerHTML = enterPostCode;
if(!this.cookieInfo.postcode)document.getElementById("postcodeform").firstChild.appendChild(aWhatIsThisLink);
document.getElementById("postcodebutton").disabled = false;
search1Obj=new newsi.Event();
search1Obj.addListener('onsubmit','postcodeform',this,'getPostcodeData',false);
search2Obj=new newsi.Event();
search2Obj.addListener('onclick','postcodebutton',this,'getPostcodeData',false);
};
CustomisationView.prototype.getPostcodeData = function(){
var pcValue = document.getElementById('postcodeValue').value;
if(pcValue && !this.isSubmitting){
document.getElementById("postcodebutton").disabled = true;
if(document.getElementById("wilMulti")){
document.getElementById("postcodeWrapper").removeChild(document.getElementById("wilMulti"));
}
else{
var postcodeform = document.getElementById("postcodeform");
postcodeform.style.background = "url(http://news.bbcimg.co.uk/nol/ukfs_news/bsp/hi/customisation/img/loading_icon.gif) no-repeat 320px 2px";
this.isSubmitting=true;
this.updateState({state:"getPostcoderData",data:pcValue});
}
}
};
CustomisationView.prototype.buildBanner = function(){
var displayState = "";
if(this.cookieInfo.postcode) displayState = "Setup";
var div;
div ="<div class='header"+displayState+"'><h3>"+this.bannerText+" <a href='#' id=\"displayState\"><img src='http://news.bbcimg.co.uk/nol/ukfs_news/bsp/hi/customisation/img/hide.gif' width='49' height='11' border='0' alt='HIDE' /></a></h3></div><div class='clear'></div><div id='main'></div>";
return div;
};
CustomisationView.prototype.contentDataView = function(obj){
if(document.getElementById("loading")){
document.getElementById("main").removeChild(document.getElementById("loading"));
}
if(document.getElementById("customise")){
for(var i in obj){
var div = document.createElement('div');
if(obj[i].feedName != "weather"){
document.getElementById("main").style.background = "none";
div.setAttribute("id","mod"+this.feedCounter);
if((this.feedCounter == "2" || this.feedCounter == "4") && (!this.cookieInfo.weather)){div.style.marginLeft = "10px";}
else if(this.feedCounter == "3" && !this.cookieInfo.weather){div.style.clear = "both";div.setAttribute("style","clear:both");}
var h4Link = document.createElement('a');
h4Link.setAttribute("href",obj[i].ResultSet.link);
var h4 = document.createElement('h4');
var br = document.createElement("br");
br.setAttribute("style","clear:both");
this.wrapperDiv().appendChild(div);
if(this.feedCounter==2 && !this.cookieInfo.weather){this.wrapperDiv().appendChild(br);}
div.appendChild(h4).appendChild(h4Link);
h4Link.appendChild(document.createTextNode(obj[i].feedName));
var ul = document.createElement('ul');
ul.setAttribute("class","headlinelist");
for(var j=0;j<obj[i].ResultSet.headlines.length;j++){
var li = document.createElement('li');
var aLink = document.createElement('a');
aLink.setAttribute("href",obj[i].ResultSet.headlines[j].url);
var headline = obj[i].ResultSet.headlines[j].title;
var regExp = ["&pound;","&amp;","&quot;","&lt;","&gt;","&eacute;"];
var headlineReplace = ['£','&','"','<','>','é'];
for (var m in regExp){
headline = headline.replace(regExp[m],headlineReplace[m]);
}
aLink.appendChild(document.createTextNode(headline));
li.appendChild(aLink);
ul.appendChild(li);
div.appendChild(ul);
}
this.feedCounter++;
var hasFeeds = true;
}
else if(obj[i].feedName == "weather"){
document.getElementById("main").style.background = "none";
days = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var today = obj[i].today;
var tomorrow;
for (k = 0;k < days.length;k++)
{
if (today == days[k])
{
if(k==6) tomorrow = "Sunday";
else tomorrow = days[k + 1];
}
}
var h4Link = document.createElement('a');
h4Link.setAttribute("href","http://www.bbc.co.uk/go/news/int/customisation/1.0/weather/pcode/-/http://www.bbc.co.uk/cgi-perl/weather/search/new_search.pl?search_query="+ this.cookieInfo.postcode.replace(/ /, "").toLowerCase());
var h4 = document.createElement('h4');
var pc = this.cookieInfo.postcode;
pc2 = pc.substring(0,pc.length-3) + " " + pc.substring(pc.length-3);
h4Link.appendChild(document.createTextNode(pc2));
var div = document.createElement('div');
if(!hasFeeds){div.setAttribute("id","weatherOnly");}
else {div.setAttribute("id","weatherWrapper");}
div.appendChild(h4).appendChild(h4Link);
var ul = document.createElement('ul');
for (var k=0, Day; Day = obj[i].ResultSet.Day[k]; k++) {
var dayVal = new Array(today,tomorrow);
var listValues = new Array("Local 5 day forecast","Weather for UK and World");
var listLinkValues = new Array("http://www.bbc.co.uk/go/news/int/customisation/1.0/weather/5day/-/http://www.bbc.co.uk/cgi-perl/weather/search/new_search.pl?search_query="+ this.cookieInfo.postcode.replace(/ /, "").toLowerCase() +"","http://www.bbc.co.uk/go/news/int/customisation/1.0/weather/world/-/http://www.bbc.co.uk/weather/");
var p = document.createElement('p');
var img = document.createElement('img');
var strong = document.createElement('strong');
var MaxTempText = document.createTextNode(" max ");
var MinTempText = document.createTextNode(" min ");
img.src = "http://www.bbc.co.uk/weather/images/symbols/57x57/"+Day.WeatherCode+".gif";
p.appendChild(strong).appendChild(document.createTextNode(dayVal[k] + ":"));
p.appendChild(img);
p.appendChild( document.createElement('strong') ).appendChild(document.createTextNode(Day.WeatherText));
p.appendChild(MaxTempText);
p.appendChild(document.createTextNode(Day.MaxTempC + "\xB0C"));
p.appendChild(document.createElement('br'));
p.appendChild(MinTempText);
p.appendChild(document.createTextNode(Day.MinTempC+ "\xB0C"));
div.appendChild(p);
var li = document.createElement('li');
var a = document.createElement('a');
a.appendChild(document.createTextNode(listValues[k]));
var clearClass = document.createElement('div');
clearClass.setAttribute("class","clear");
a.setAttribute('href', listLinkValues[k]);
li.appendChild(a);
ul.appendChild(li);
div.appendChild(ul);
}
if(!hasFeeds){this.mainDiv.appendChild(div);}
else{this.mainDiv.insertBefore(div, this.mainDiv.childNodes[this.mainDiv.childNodes.length -2]);}
this.wrapperDiv();
}
}
if(this._model._user33){this.user33PopupLayer();}
if(this.missingFeedTag){document.getElementById("customise").childNodes[0].appendChild(this.missingFeedTag);}
}
};
CustomisationView.prototype.user33PopupLayer  = function(selectIndexs){
this.dom=new newsi.HTML.DOM("customise");
var mainDiv =  document.getElementById("main");
var popUpDiv = document.createElement('div');
popUpDiv.setAttribute("id","user33popup");
var user33text = "Separate Surrey and Sussex indexes have replaced the Southern Counties index.  Please select one of the index's below.";
var p = document.createElement("p");
p.appendChild(document.createTextNode(user33text));
var aLinkSurrey = document.createElement('a');
this.dom.setId({el:aLinkSurrey,id:"surrey75"});
var surreyText = "Surrey";
aLinkSurrey.appendChild(document.createTextNode(surreyText));
var aLinkSussex = document.createElement('a');
this.dom.setId({el:aLinkSussex,id:"sussex37"});
var sussexText = "Sussex";
aLinkSussex.appendChild(document.createTextNode(sussexText));
var surreyButtonLi = document.createElement('li');
surreyButtonLi.appendChild(aLinkSurrey);
var sussexButtonLi = document.createElement('li');
var ul = document.createElement('ul');
sussexButtonLi.appendChild(aLinkSussex);
popUpDiv.appendChild(p);
popUpDiv.appendChild(ul);
ul.appendChild(surreyButtonLi);
ul.appendChild(sussexButtonLi);
mainDiv.appendChild(popUpDiv);
var evtObj75=new newsi.Event();
evtObj75.addListener('onclick',"surrey75",this,'handleUser33',false,"75");
var evtObj37=new newsi.Event();
evtObj37.addListener('onclick',"sussex37",this,'handleUser33',false,"37");
};
CustomisationView.prototype.handleUser33  = function(){
this.updateState({state:"user33",data:{"id":arguments[0][0]}});
};
CustomisationView.prototype.wrapperDiv = function(){
var wrapperDiv = document.getElementById("contentWrapper") ||  document.getElementById("contentOnly");
if (wrapperDiv) {
if (document.getElementById("weatherWrapper")) {
wrapperDiv.setAttribute("id","contentWrapper");
} else {
wrapperDiv.setAttribute("id","contentOnly");
}
return wrapperDiv;
}
wrapperDiv = document.createElement('div');
var weatherModule =  document.getElementById("weatherOnly");
if (!weatherModule) {wrapperDiv.setAttribute("id","contentOnly");}
else {wrapperDiv.setAttribute("id","contentWrapper");}
this.mainDiv.appendChild(wrapperDiv);
this.showFooter();
return wrapperDiv;
};
CustomisationView.prototype.mainDiv = function(){var mainDiv = document.getElementById("main");return mainDiv;};
CustomisationView.prototype.showFooter = function (){
var changeButton = document.createElement('div');
var changeContentButton = document.createElement('div');
var moreContentButton = document.createElement('div');
var footerDiv = document.createElement('div');
var clearClass = document.createElement('div');
var aMoreContentLink = document.createElement('a');
aMoreContentLink.appendChild(document.createTextNode("Add sport or more news"));
aMoreContentLink.setAttribute("id","contentButton");
aMoreContentLink.setAttribute("href","#");
var aChangeContentLink = document.createElement('a');
aChangeContentLink.appendChild(document.createTextNode("Change selection"));
aChangeContentLink.setAttribute("id","contentButton");
aChangeContentLink.setAttribute("href","#");
var aChangeAreaLink = document.createElement('a');
aChangeAreaLink.appendChild(document.createTextNode("Change your area"));
aChangeAreaLink.setAttribute("id","changeArea");
aChangeAreaLink.setAttribute("href","#");
clearClass.setAttribute("class","clear");
changeButton.setAttribute("id","changeOptions");
footerDiv.setAttribute("id","custFooter");
moreContentButton.setAttribute("id","moreContentButton");
changeContentButton.setAttribute("id","changeContentButton");
this.mainDiv.appendChild(clearClass);
if(this.cookieInfo.mo){
this.mainDiv.appendChild(footerDiv).appendChild(moreContentButton).appendChild(aChangeContentLink);
this.mainDiv.appendChild(clearClass);
obj1=new newsi.Event();
obj1.addListener('onclick','contentButton',this,'moreContent',false);
}
else{
this.mainDiv.appendChild(footerDiv).appendChild(moreContentButton).appendChild(aMoreContentLink);
footerDiv.appendChild(changeButton).appendChild(aChangeAreaLink);
this.mainDiv.appendChild(clearClass);
obj=new newsi.Event();
obj.addListener('onclick','changeArea',this,'showForm',false);
obj2=new newsi.Event();
obj2.addListener('onclick','contentButton',this,'moreContent',false);
}
};
CustomisationView.prototype.showMultiWIL = function (postCodeData){
var postcodeform = document.getElementById("postcodeform");
postcodeform.style.background = "none";
var multiWrapper = document.createElement("div");
multiWrapper.setAttribute("id","wilMulti");
var p = document.createElement("p");
p.appendChild(document.createTextNode("There is more than one match for your search. Please click on your preferred choice:"));
var ul = document.createElement("ul");
for (var j=0, wilArr; wilArr = postCodeData.wil[j]; j++)
{
var li = document.createElement('li');
var a = document.createElement('a');
a.appendChild(document.createTextNode(wilArr.wilname));
a.setAttribute('href', "#");
a.setAttribute('id', "WILID"+wilArr.wilid);
li.appendChild(a);
ul.appendChild(li);
}
multiWrapper.appendChild(p);
multiWrapper.appendChild(ul);
var clearClass = document.createElement('div');
clearClass.setAttribute("class","clear");
var postcodeWrapper = document.getElementById("postcodeWrapper");
postcodeWrapper.appendChild(multiWrapper);
postcodeWrapper.appendChild(clearClass);
this.newPostCodeData = {};
this.newPostCodeData.data = [];
this.newPostCodeData.postcode = postCodeData.postcode;
var wID =  postCodeData.weatherID;
if(wID.length == 3){this.newPostCodeData.weatherID = "0"+wID;}
else if(wID.length == 2){this.newPostCodeData.weatherID = "00"+wID;}
else if(wID.length == 1){this.newPostCodeData.weatherID = "000"+wID;}
else{this.newPostCodeData.weatherID = wID;}
this.newPostCodeData.post_town = postCodeData.post_town;
for (var j=0, data; data = postCodeData.wil[j]; j++) {
this.newPostCodeData.data[j] = {};
this.newPostCodeData.data[j].wilid = data.wilid;
this.newPostCodeData.data[j].wilname = data.wilname;
var wilID = data.wilid;
var wilname = data.wilname;
this["obj"+j]=new newsi.Event();
this["obj"+j].addListener('onclick','WILID'+data.wilid,this,'selectWIL',false,this.newPostCodeData,wilID,wilname);
}
document.getElementById("postcodebutton").disabled = true;
document.getElementById("postcodeValue").disabled= true;
};
CustomisationView.prototype.selectWIL = function(obj) {
var newPostCodeData = obj;
this.cookieInfo.weatherID = newPostCodeData[0].weatherID;
this.cookieInfo.post_town = newPostCodeData[0].post_town;
this.cookieInfo.postcode = newPostCodeData[0].postcode;
this.cookieInfo.ids = new Array(newPostCodeData[1]);
this.cookieInfo.wilname = newPostCodeData[2];
this.cookieInfo.weather = true;
this.updateState({state:"updateCookie",data:this.cookieInfo});
this.updateState({state:"loadFeedList"});
};
CustomisationView.prototype.error = function(errorMessage){
var p = document.createElement("p");
p.appendChild(document.createTextNode(this.errorMessage));
var img = document.createElement("img");
img.setAttribute("src","/nol/ukfs_news/bsp/hi/customisation/img/custfail.metric?"+errorMessage+"&cookieIds="+this.cookieInfo.ids+"&postcode="+this.cookieInfo.postcode+"&sh="+this.cookieInfo.show+"&wID="+this.cookieInfo.weatherID);
img.setAttribute("style","display:none");
document.getElementById("main").appendChild(p);
document.getElementById("main").appendChild(img);
document.getElementById("main").removeChild(img);
};
CustomisationView.prototype.moreContent = function(){window.location = "/nol/ukfs_news/bsp/hi/customisation/html/more_content.stm";};