var sa = false;

function s(){
	if(sa == false){
		var m=document.getElementById('b_search');
		var b = document.getElementById('b_sb');
		var st = m.value.replace(/&/g, '%26');
		if(m.value=='' || m.value.indexOf('Search our') > -1){
			alert('Please enter a search query ...');
		}else{
 			sa = true;
			m.className = 'searching';
			var w= window.location.toString();
			if(w.indexOf('archive.brent.gov.uk')>-1) {
				location.replace('http://search.brent.gov.uk/search?q=' +st +'&entqr=0&ud=1&sort=date%3AD%3AL%3Ad1&output=xml_no_dtd&oe=UTF-8&ie=UTF-8&client=archive_frontend&proxystylesheet=archive_frontend&site=archive');
				return false;
			}else{
				var t = new ax(w.substring(0, w.indexOf('.gov')) +'.gov.uk/home.nsf/pages/search+unavailable?opendocument&rnd='+Math.random(),
					function(o, s){
						//if(s==200)
						//	location.replace('http://www.google.co.uk/search?hl=en&q=site%3Abrent.gov.uk+' +st);
						//else					
							location.replace('http://search.brent.gov.uk/search?q=' +st +'&entqr=0&ud=1&sort=date%3AD%3AL%3Ad1&output=xml_no_dtd&oe=UTF-8&ie=UTF-8&client=default_frontend&proxystylesheet=default_frontend&site=default_collection');
						});
			t.dG();
			}
		}
	}
}

function checkCR(evt) { 
        var evt  = (evt) ? evt : ((event) ? event : null); 
        var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null); 
        var w = window.location.toString();
        if ((evt.keyCode == 13) && (w.indexOf("search.brent.gov.uk")>0)) {doSearch(); return false;}
        if ((evt.keyCode == 13) && (w.indexOf("10.17.220.200")>0)) {doSearch(); return false;}
        if ((evt.keyCode == 13) && (node.type=="text") && (node.id=='b_search')) { 
	        if(document.getElementById('b_search')) s();
             return false; 
       }else return true; 
} 

document.onkeypress = checkCR;
document.onload = doJS();

function doJS(){
	var bm = document.getElementById('bookmark_link');
	if(bm) bm.style.display = 'block';
	var fs = document.getElementById('form_search_block');
	if(fs) fs.style.display = 'block';
}

function ax(url, cb, opts) {
    var r = init();
    r.onreadystatechange = pR;
    function init(){ if (window.XMLHttpRequest) return new XMLHttpRequest(); else if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");};
    function pR () { if (r.readyState == 4) { if (cb) cb(r.responseText, r.status, opts);  r=null; } };
    this.dG = function() {r.open("GET", url, true);r.send(null); };
    this.dP = function(body) { r.open("POST", url, true); r.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); r.send(body);};
}

function cx(op){
	var ief = true;
	if (window.ActiveXObject){ xd = new ActiveXObject("Microsoft.XMLDOM"); xd.async=false;
	 }else if (document.implementation && document.implementation.createDocument){
	 	  xd= document.implementation.createDocument("","",null); ief = false;
	 }else alert('Your browser is not compatible with this system, please upgrade to the latest version ...');
  	if(ief) xd.loadXML(op); else{ var oR = new DOMParser();xd = oR.parseFromString(op, "text/xml");	}	
	return xd;
}

function ms_clear(o, b, t){
	if(!b) if(o.value==t)	o.value = '';
	if(b) if(o.value=='')	o.value = t;
}

function fyn(o, i, u){
 var o = document.getElementById(o);
 var i = document.getElementById(i);
 if(i.value == '')
 	alert('Please enter a postcode');
 else if (i.value.length<=4)
 	alert('Please enter a valid and complete postcode');
 else{
 	var gov = i.value;
 	var ox = u?u:o.options[o.selectedIndex].value;
      if(i.value=='e.g. HA9 9HD') gov = 'HA99HD';
	 window.location = ox.replace('p=***', 'p=' + gov.replace(/ /g, ''));
 }
}

function createCookie(name,value,days) {
                if (days) {
                                 var date = new Date();
                                 date.setTime(date.getTime()+(days*24*60*60*1000));
                                 var expires = "; expires="+date.toGMTString();
                }
                else var expires = "; expires=30/30/3000";
                document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
                var nameEQ = name + "=";
                var ca = document.cookie.split(';');
                for(var i=0;i < ca.length;i++) {
                                 var c = ca[i];
                                 while (c.charAt(0)==' ') c = c.substring(1,c.length);
                                 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
                }
                return null;
}

function eraseCookie(name) {
        createCookie(name,"",-1);
}

function addMB(d,p,t){
	var faves = readCookie('FV');
	var gol = d+'~'+p+'~'+t.replace("'", "\'"); 
	if(faves){
		if(faves.indexOf(gol) > -1){
			alert('This page is already in your My Brent favourites ...');
			return;
		}else
			createCookie('FV', faves + '(@)' + gol, 365);
	}else{
		createCookie('FV', gol, 365);
	}
	var flink = document.getElementById('b_mbfl');
     flink.href = "/mybrent#favourites";
	flink.innerHTML = 'Added to your favourites (go there now)';	
}


function otherAuth(l, i, a){
 var la = a.options[a.selectedIndex].value;
 if(la == '') alert('Please select an authority from the list before clicking this button'); else window.open('http://local.direct.gov.uk/LDGRedirect/index.jsp?LGSL=' + l + '&LGIL=' + i + '&AgencyId=' + la + '&Type=Single');
}
