window.location.querystring = (function() {  
    // The return is a collection of key/value pairs  
    var result = {};    
    // Gets the query string with a preceeding '?'  
    var querystring = window.location.search;  
    // document.location.search is empty if a query string is absent  
    if (!querystring)  
        return result;    
    // substring(1) to remove the '?'  
    var pairs = querystring.substring(1).split("&");  
    var splitPair;  
    // Load the key/values of the return collection  
    for (var i = 0; i < pairs.length; i++) {  
        splitPair = pairs[i].split("=");  
        result[splitPair[0]] = splitPair[1];  
    }  
    return result;  
})();
function search(site) {
  var q = window.location.querystring["q"];
  var script = document.createElement('script');

  if(site=='difference') {
    site='http://www.bankofscotland.co.uk/corporate/the-difference/';
  } else if(site=='online') {
    site='http://www.bankofscotland.co.uk/corporate/online-services/';
  } else if(site=='daytoday') {
    site='http://www.bankofscotland.co.uk/corporate/day-to-day-banking/';
  } else if(site=='funding') {
    site='http://www.bankofscotland.co.uk/corporate/funding-solutions/';
  } else if(site=='finance') {
    site='http://www.bankofscotland.co.uk/corporate/finance/';
  } else if(site=='international') {
    site='http://www.bankofscotland.co.uk/corporate/international/';
  } else if(site=='introducers') {
    site='http://www.bankofscotland.co.uk/corporate/business-introducers/';
  } else if(site=='interest') {
    site='http://www.bankofscotland.co.uk/corporate/rates-charges/';
  } else if(site=='contact') {
    site='http://www.bankofscotland.co.uk/corporate/contact/';
  } else {
    site='http://www.bankofscotland.co.uk/corporate/+OR+site:http://entrepreneurs.bankofscotland.co.uk/+OR+http://nfp.bankofscotland.co.uk/';
  }
  if(site!='') {
    q='site:'+site+'+'+q;
  }
  var offset = window.location.querystring["s"];
  if(offset==undefined) {
    offset=0;
  }
  script.src = 'http://www.dashboard.co.uk/feed/xmltojs.php?q='+q+'&s='+offset;
  document.getElementsByTagName('head')[0].appendChild(script);
}
function callback(feedData) {
	var response = eval(feedData);
	var inner='';
  var targetarea=document.getElementById('result-panel-personal');
	var limit = window.location.querystring["p"];
	if(limit==''||limit==undefined) {limit=10;}
	if(response.a0) {
    if(response.a0.U) {
      var next=document.getElementById('corporatenext');
      targetarea=document.getElementById('result-panel-corporate');
    	if(area==undefined||area=='all') {limit=3;}
    	for(key in response) {
    	  if(key.indexOf(' attr')<0) {
      	  if(limit>0) {
      	    inner=inner+'<h3>'+'<a href="'+response[key].U+'">'+response[key].T+'</a></h3>';
      	    inner=inner+'<p>'+response[key].S.replace(/<br>/g,'')+'</p>';
      	    if(response[key].U.length>73) {
      	      url=response[key].U.substring(0,70)+'...';
      	    } else {
      	      url=response[key].U;
      	    }
//            inner=inner+'<p class="url">'+'<a href="'+response[key].U+'">'+url+'</a></p>';
      	  }
      	  limit--;
    	  }
    	}
  	  if(limit>0) { //Reached the end of results before writing a page-worth
        next.style.display="none";
        // inner=inner+'<p>'+'No more results in this area.</p>';
  	  }
  	}
  } else {
    if(response.aU) { //There is only 1 result 
    	if(response.aU.indexOf('www.bankofscotlandhalifax.co.uk')>0) {
    	  thisarea='personal';
    	  if(area=='personal') limit=window.location.querystring["p"];
    	} else if(response.aU.indexOf('www.bankofscotlandprivateclients.co.uk')>0) {
    	  thisarea='private';
    	  if(area=='private') limit=window.location.querystring["p"];
    	} else if(response.aU.indexOf('www.bankofscotland.co.uk/corporate')>0) {
    	  thisarea='corporate';
    	  if(area=='corporate') limit=window.location.querystring["p"];
    	} else if(response.aU.indexOf('www.bankofscotlandbusiness.co.uk')>0) {
    	  thisarea='business';
    	  if(area=='business') limit=window.location.querystring["p"];
    	}
      var first=document.getElementById(thisarea+'first');
      first.style.display="none";
      var next=document.getElementById(thisarea+'next');
      next.style.display="none";
      targetarea=document.getElementById('result-panel-'+thisarea);
    	limit=1;
    	for(key in response) {
    	  if(key.indexOf(' attr')<0) {
      	  if(limit>0) {
      	    inner=inner+'<h3>'+'<a href="'+response.aU+'">'+response.aT+'</a></h3>';
      	    inner=inner+'<p>'+response.aS.replace(/<br>/g,'')+'</p>';
      	    if(response.aU.length>73) {
      	      url=response.aU.substring(0,70)+'...';
      	    } else {
      	      url=response.aU;
      	    }
      	    if(area=='all'||thisarea==area) { //if we're on the main page or showing results from this section, write the URL
              inner=inner+'<p class="url">'+'<a href="'+response.aU+'">'+url+'</a></p>';
      	    }
            // inner=inner+'<p>'+'No more results in this area.</p>';
      	  }
      	  limit--;
    	  }
  	  }
  	}
  }
	targetarea.innerHTML=inner;
}
function noResultCallback(area) {
  targetarea=document.getElementById('result-panel-'+area);
  targetarea.innerHTML='';
  document.getElementById(area+'first').style.display='none';
  document.getElementById(area+'next').style.display='none';
}
function init() {
  if(offset>=per) {
    var prev=document.getElementById('corporateprev');
    prev.style.display="inline";
  } else {
    var prev=document.getElementById('corporateprev');
    prev.style.display="none";
  }
  if(area!=undefined&&area!='all') {
    var active=document.getElementById('result-corporate-outer');
    active.id="main-result";
    active.className="main-result-corporate";
    search(area,offset);
    var first=document.getElementById('corporatefirst');
    first.style.display="none";
  } else {
    search('corporate',offset);
  }
  document.getElementById('refineq').value=unescape(window.location.querystring["q"].replace(/\+/g,' '));
  if(window.location.querystring["with"]!=undefined) {
    if(window.location.querystring["with"]=='any') {
      document.getElementById('any').checked=true;
    } else if(window.location.querystring["with"]=='exact') {
      document.getElementById('exact').checked=true;
    }
  }
  document.getElementById('selectarea').value=area;
}
function updateQuery(withval) {
  // var originalq=window.location.querystring["q"].replace(/\+/g,' ');
  var originalq=document.getElementById('refineq').value.replace(/\s+OR\s+/g,' ');
  originalq=originalq.replace(/\"/g,'');
  switch(withval) {
    case 'all':     document.getElementById('refineq').value=originalq;break;
    case 'exact':     document.getElementById('refineq').value='"'+originalq+'"';break;
    case 'any':     document.getElementById('refineq').value=originalq.replace(/\s+/g,' OR ');break;
  }
}
function first(link) {
  offset=0;
  link.href=link.href+'?area='+area+'&s='+offset+'&p=10&q='+window.location.querystring['q'];
}
function next(link) {
  offset+=per;
  if(link.href.indexOf('&')==-1) {
    link.href=link.href+'?area='+area+'&s='+offset+'&p=10&q='+window.location.querystring['q'];
  } else {
    link.href=link.href.substring(0,link.href.indexOf('&'))+'?area='+area+'&s='+offset+'&p=10&q='+window.location.querystring['q'];
  }
}
function prev(link) {
  offset-=per;
  if(link.href.indexOf('&')==-1) {
    link.href=link.href+'?area='+area+'&s='+offset+'&p=10&q='+window.location.querystring['q'];
  } else {
    link.href=link.href.substring(0,link.href.indexOf('&'))+'?area='+area+'&s='+offset+'&p=10&q='+window.location.querystring['q'];
  }
}