/*
We provide a third input box, if the browser supports it
*/

var urlopener = null;
if (document.getElementsByTagName) {
    /* we need to read remote files */
    try {
	/* IE */
	urlopener = new ActiveXObject('Msxml2.XMLHTTP');
    } catch (e) {
	/* Mozilla */
	try {
		urlopener = new XMLHttpRequest();
	} catch (e) {
		// a browser that doesn't support XML requests
		urlopener = null;
	}
    }
}

if (urlopener) {
    // create a new input, in a div
    var select = document.createElement('select');
    select.name = 'town';
    select.style.position = 'absolute';
    select.style.left = '10%';
    select.style.width = '25ex';
    var option = document.createElement('option');
    option.appendChild(document.createTextNode('All towns'));
    option.value = '*';
    select.appendChild(option);
    var div = document.createElement('div');
    div.style.marginBottom = '1em';
    div.appendChild(document.createTextNode('Town:'));
    div.appendChild(select)
    document.query.insertBefore(div,document.query.submit);
    // now set an event handler for changes to the county
    document.query.county.onchange = countyChange;
    // the user may have used the back button
    if (document.query.county.options[document.query.county.selectedIndex].value != '*') {
	document.query.county.onchange();
    }
}

function countyChange() {
	// get the select elements
    var county = document.query.county;
    if (!county) {
        county = document.query.elements[1];
    }
	
	var town = document.query.town;
    if (!town) {
        town = document.query.elements[2];
    }
	
    // the county has changed. Update the towns select
    var county_value = county[county.selectedIndex].value;
    
    if (county_value && county_value != '*') {
        var url = '/service_finder/towns.py?county=' + county_value;
        urlopener.open('GET',url,false);
        urlopener.send('');
        towns_xml = urlopener.responseXML.getElementsByTagName('town');
        town.options.length = 1;
        for (i = 0; i < towns_xml.length; i++) {
            var option = document.createElement('option');
            option.text = option.value = towns_xml.item(i).firstChild.nodeValue;
            //append after the first ("All towns") entry.
            town.options[i + 1] = option;
        }
    }
}

function categoryChange() {
	// get the select elements
    var category = document.query.category;
    if (!category) {
        category = document.query.elements[0];
    }
	
	var county = document.query.county;
    if (!county) {
        county = document.query.elements[1];
    }
	
	var town = document.query.town;
    if (!town) {
        town = document.query.elements[2];
    }
	
	// the category has changed. Check if the user has selected 'Post Offices'
	var category_value = category[category.selectedIndex].value;
	
	var county_div = county.parentNode;
	var town_div = town.parentNode;
	var submit = document.query.submit;
	
	var message_area = document.getElementById('message_area');
	
	county_div.style.display = 'none';
	town_div.style.display = 'none';
	submit.style.display = 'none';
	
	message_area.style.display = '';
		
	switch (category_value)
	{
		case 'Post Offices':
			message_area.innerHTML = '<h3>Please Note</h3><p>An accurate listing (including maps) of Post Offices throughout Ireland can be found at <a href="http://www.anpost.ie/AnPost/at+your+local+Post+Office.htm">An Post.ie</a>.<p>';
			break;
		case 'Garda District Head Quarters':
			message_area.innerHTML = '<h3>Please Note</h3><p>A current listing (including maps) of Garda Headquarters and Garda Stations throughout Ireland can be found at <a href="http://www.garda.ie/Stations/Default.aspx">garda.ie</a>.<p>';
			break;
		case 'Garda Stations':
			message_area.innerHTML = '<h3>Please Note</h3><p>A current listing (including maps) of Garda Stations throughout Ireland can be found at <a href="http://www.garda.ie/Stations/Default.aspx">garda.ie</a>.<p>';
			break;
		case 'Social Welfare Local Offices':
			message_area.innerHTML = '<h3>Please Note</h3><p>A current listing (including maps) of Social Welfare Local Offices throughout Ireland can be found at <a href="http://www.welfare.ie/EN/ContactUs/Pages/localoffice.aspx">welfare.ie</a>.<p>';
			break;
		case 'Tax Offices':
			message_area.innerHTML = '<h3>Please Note</h3><p>A current listing of tax offices throughout Ireland can be found at <a href="http://www.revenue.ie/en/contact/index.html">revenue.ie</a>.<p>';
			break;
		case 'Local Health Offices':
			message_area.innerHTML = '<h3>Please Note</h3><p>A current listing of HSE local health offices throughout Ireland can be found at <a href="http://hse.ie/eng/services/Find_a_Service/LHO/">hse.ie</a>.<p>';
			break;
		default:
			county_div.style.display = '';
			town_div.style.display = '';
			submit.style.display = '';
		
			message_area.style.display = 'none';
			message_area.innerHTML = '';
	}
}

// Run both of these when the page loads
countyChange();
categoryChange();

