/*
 * contains loads of utility functions, to auto check parents if child change.
 */

/*
 * Check the element(s) whose id is given in parameter
 * only if the 2nd elem is checked itself
 */

function check(id, elem)
{
    if (elem)
    {
	if ((elem.type=='radio' || elem.type=='checkbox') && !elem.checked) {return} 
    }

    if (isArray(id))
    {
	for (var e in id) 
	{
	  document.getElementById(id[e]).checked='checked';
	}
    }
    else
    {
	document.getElementById(id).checked='checked';
    }
}



/*
 * update the country/county list
 */
function locGeo (country)
{
    var val = country.value;
    //empty current options
    var county = document.getElementById('county');
    county.innerHTML = '';

    //select which country to use: one or all?
    var sel = new Array();
    // only one (the one selected)

    if (countries[val].length>0) {sel.push(val)}
    //we need to go through all countries (UK wide)
    else {for (i in countries) {sel.push(i)}}

    var cpt = 0;
    // we need the 'All' option anyway
    county.options[cpt] = new Option('All', 0);
    cpt ++;
    for (var v in sel)
    {
	for (var i in countries[sel[v]])
	{
	    county.options[cpt] = new Option(countries[sel[v]][i], i);
	    cpt ++;
	}
    }
}

/*
 * map browser stuff
 */

// display the map browser
function popMap()
{
    window.open("/cgi-bin/browsemap.cgi", "map","height=610,width=390");
}

// when an extent has been choosed
function use_extent()
{
    if (document.getElementById('xcoord').value != undefined)
	{
	    // we have the LL values, we need to get the NG
	    var lln = document.getElementById('y2coord').value;
	    var lls = document.getElementById('ycoord').value;
	    var llw = document.getElementById('xcoord').value;
	    var lle = document.getElementById('x2coord').value;


	    //convert LL to NG
	    var sw_11 = new LatLng(lls, llw);
	    sw_11.WGS84ToOSGB36();
	    var ngsw = sw_11.toOSRef().toString().slice(1,-1).split(',');
	    ngw = parseInt(ngsw[0]).toFixed(0);
	    ngs = parseInt(ngsw[1]).toFixed(0);

	    var ne_11 = new LatLng(lln, lle);
	    ne_11.WGS84ToOSGB36();
	    var ngne = ne_11.toOSRef().toString().slice(1,-1).split(',');
	    nge = parseInt(ngne[0]).toFixed(0);
	    ngn = parseInt(ngne[1]).toFixed(0);

	    //only show NG numbers that are real
	    if ((ngw < 0) || (ngw > 700000)) { ngw = "Off NG";}
	    if ((ngs < 0) || (ngs > 1300000)){ ngs = "Off NG";}
	    if ((nge < 0) || (nge > 700000)) { nge = "Off NG";}
	    if ((ngn < 0) || (ngn > 1300000)){ ngn = "Off NG";}

	    setLL(llw, lls, lle, lln);
	    setNG(ngw, ngs, nge, ngn);
	}
    // else the map has not been touched
    self.close();
}

// set the coordinates in the main window after a map browser choice
function setLL (w, s, e, n)
{
	if( self.opener){
	    self.opener.document.getElementById('llw').value=w;   
	    self.opener.document.getElementById('lls').value=s;   
	    self.opener.document.getElementById('lle').value=e;   
	    self.opener.document.getElementById('lln').value=n;
	} else {
	    document.getElementById('llw').value=w;   
	    document.getElementById('lls').value=s;   
	    document.getElementById('lle').value=e;   
	    document.getElementById('lln').value=n;
	}
}

function setNG (w, s, e, n)
{
	if(self.opener){
	    self.opener.document.getElementById('nge').value=e;
	    self.opener.document.getElementById('ngs').value=s;
	    self.opener.document.getElementById('ngn').value=n;
	    self.opener.document.getElementById('ngw').value=w;
	} else {
	    document.getElementById('nge').value=e;
	    document.getElementById('ngs').value=s;
	    document.getElementById('ngn').value=n;
	    document.getElementById('ngw').value=w;
	}
}

// popup the gazeteer option
function popGaz ()
{
    goSubmit(0);
    qd_submit();
}