/*
  Main Javascript for the Department of Health and Children
*/

// alternately coloured rows in the table
function colour_rows() {
    var tables = document.getElementsByTagName("table");
    for (var i = 0; i < tables.length; i++) {
	var table = tables.item(i);
	var rows = table.getElementsByTagName("tr");
	var count = 1;
	for (var j = 0; j < rows.length; j++) {
	    var row = rows.item(j);
	    if (row.className) {
		continue;
	    }
	    if (count % 2 == 0) {
		row.className = "even";
	    } else {
		row.className = "odd";
	    }
	    count++;
	}
    }
}

function remove_self_referential_links() {
    // remove any links in the document that point to itself.
    var self_referential = new Array();
    var nodes = document.getElementsByTagName('a');
    for (i = 0; i < nodes.length; i++) {
	var link = nodes.item(i).href;
	if (link == document.location.href && link.indexOf('#') == -1 ) {
	    /* 
	       we have a self-referential link.
	       IE5 doesn't support push()
	    */	    
	    self_referential[self_referential.length] = nodes.item(i);
	}
    }

    for (j=0; j < self_referential.length; j++) {
	/* we have a self-referential link. We could
	   a. set a css style to make it display 'disabled', or
	   b. remove it
	*/
	var node = self_referential[j];
	var newnode = document.createElement('span');
	newnode.className = 'linkdisabled';
	var children = node.childNodes;
	for (k = 0; k < children.length; k++) {
	    var added = newnode.appendChild(children.item(k).cloneNode(true));
	}
	node.parentNode.replaceChild(newnode,node);
    }
}

/*
 * This function parses comma-separated name=value 
 * argument pairs from the query string of the URL. 
 * It stores the name=value pairs in 
 * properties of an object and returns that object.
 *
 * TAKEN FROM http://www.oreillynet.com/pub/a/javascript/excerpt/jstdg_ch13/?page=6
 */
function getArgs(  ) {
    var args = new Object(  );
    var query = location.search.substring(1);     
      // Get query string
    var pairs = query.split("&");
     // Break at &
    for(var i = 0; i < pairs.length; i++) {
        var pos = pairs[i].indexOf('=');
          // Look for "name=value"
        if (pos == -1) continue;
          // If not found, skip
        var argname = pairs[i].substring(0,pos);
          // Extract the name
        var value = pairs[i].substring(pos+1);
          // Extract the value
        args[argname] = unescape(value);
         // Store as a property
       // In JavaScript 1.5, use decodeURIComponent(  ) 
       // instead of escape(  )
    }
    return args;     // Return the object
}

function set_search_text() {
    // if the user has 
    var args = getArgs();
    // do we have a q parameter?
    var searchstring = args['q'];
    if (searchstring) {
	searchstring = searchstring.split('+').join(' ');
	var input = document.getElementById('q');
	input.setAttribute("value",searchstring);
    }
}

function abort_empty_searches() {
    // don't submit the search if no search term has been given
    var input = document.getElementById('q');
    if (!input.value) {
	return false;
    }
    return true;
}

function set_event_handlers() {
    // set all event handlers
    document.getElementById('search').getElementsByTagName('form').item(0).onsubmit = abort_empty_searches;
}

// functions to run on load
function onload_functions() {
    if (document.getElementById) {
	/* browser supports DOM */
	remove_self_referential_links();
	//colour_rows();
	set_search_text();
	set_event_handlers();
    }
}

try {
    // only run this on DOM-supporting browsers
    document.getElementsByTagName;
    // set event handlers
    window.onload = onload_functions;

} catch (e) {
    /* 
       don't load javascripts. Any javascript in use should not affect the core
       functionality of the site.
    */
}

