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=='personal') {
    site='http://www.bankofscotlandhalifax.co.uk';
  } else if(site=='private') {
    site='http://www.bankofscotlandprivateclients.co.uk';
  } else if(site=='business') {
    site='http://www.bankofscotlandbusiness.co.uk';
  } else if(site=='corporate') {
    site='http://www.bankofscotland.co.uk/corporate/';
  }
  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 = 1;
  if(response.a0) {
  	if(response.a0.U.indexOf('www.bankofscotlandhalifax.co.uk')>0) {
  	  if(area=='personal') limit=window.location.querystring["p"];
      targetarea=document.getElementById('result-panel-personal');
  	} else if(response.a0.U.indexOf('www.bankofscotlandprivateclients.co.uk')>0) {
  	  if(area=='private') limit=window.location.querystring["p"];
      targetarea=document.getElementById('result-panel-private');
  	} else if(response.a0.U.indexOf('www.bankofscotland.co.uk/corporate')>0) {
  	  if(area=='corporate') limit=window.location.querystring["p"];
      targetarea=document.getElementById('result-panel-corporate');
  	} else if(response.a0.U.indexOf('www.bankofscotlandbusiness.co.uk')>0) {
  	  if(area=='business') limit=window.location.querystring["p"];
      targetarea=document.getElementById('result-panel-business');
  	}
  	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+'</p>';
          // inner=inner+'<p class="url">'+'<a href="'+response[key].U+'">'+response[key].U+'</a></p>';
    	  }
    	  limit--;
  	  }
  	}
  } else if(response.aU) { //There is only 1 result - Current Not working
  	if(response.aU.indexOf('www.bankofscotlandhalifax.co.uk')>0) {
  	  if(area=='personal') limit=1;
      targetarea=document.getElementById('result-panel-personal');
  	} else if(response.aU.indexOf('www.bankofscotlandprivateclients.co.uk')>0) {
  	  if(area=='private') limit=1;
      targetarea=document.getElementById('result-panel-private');
  	} else if(response.aU.indexOf('www.bankofscotland.co.uk/corporate')>0) {
  	  if(area=='corporate') limit=1;
      targetarea=document.getElementById('result-panel-corporate');
  	} else if(response.aU.indexOf('www.bankofscotlandbusiness.co.uk')>0) {
  	  if(area=='business') limit=1;
      targetarea=document.getElementById('result-panel-business');
  	}
  	if(area==undefined) {limit=3;}
  	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+'</p>';
          // inner=inner+'<p class="url">'+'<a href="'+response[key].U+'">'+response[key].U+'</a></p>';
    	  }
    	  limit--;
  	  }
  	}
  }
	targetarea.innerHTML=inner;
}
function noResultCallback(area) {
  targetarea=document.getElementById('result-panel-'+area);
  targetarea.innerHTML='<p>No results</p>';
}
function init() {
  if(area!=undefined&&area!='all') {
    var active=document.getElementById('result-'+area+'-outer');
    var off=document.getElementById('result-personal-outer');
    off.id="result-personal-side";
    var off=document.getElementById('result-private-outer');
    off.id="result-private-side";
    var off=document.getElementById('result-business-outer');
    off.id="result-business-side";
    var off=document.getElementById('result-corporate-outer');
    off.id="result-corporate-side";
    active.id="main-result";
    switch(area) {
      case 'personal' : search('personal',offset);search('private',0);search('business',0);search('corporate',0);
      case 'private' : search('personal',0);search('private',offset);search('business',0);search('corporate',0);
      case 'business' : search('personal',0);search('private',0);search('business',offset);search('corporate',0);
      case 'corporate' : search('personal',0);search('private',0);search('business',0);search('corporate',offset);
    }
  } else {
    search('personal',offset);
    search('private',offset);
    search('business',offset);
    search('corporate',offset);
  }

  //document.getElementById('quickq').value=unescape(window.location.querystring["q"].replace(/\+/g,' '));
  //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;
    }
  }
}
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;
  }
}