
var div_name = "ticker-updates";
var xml_uri = "/motoring_advice/news/data/news_ticker.xml";
var is_ie = false;

function show_news() {
	set_browser();
	request_xml();
}

function set_browser() {
	is_ie = (navigator.userAgent.indexOf("MSIE") != -1);
	is_saf = (navigator.userAgent.indexOf("Apple") != -1);// Apple Safari
}

function request_xml() {
	// retrieve the XML from the server
	var xmlReq = get_request();
	xmlReq.open("GET",xml_uri,true);
	xmlReq.onreadystatechange=function() {
		if (xmlReq.readyState == 4) {
			if(is_ie) {
				xmlReq.responseXML.load(xmlReq.responseStream);
			}
			parse_news(xmlReq.responseXML);
		}
	}
	xmlReq.send(null);
}

function parse_news(xml) {
	var div = document.getElementById(div_name);// get the news ticker div
	div.innerHTML = "<p>Loading...</p>";
	var links = get_link_tags(xml);
	for(var i = 0;i < links.length;i++) {
		if(i == 0) div.innerHTML = "";
		var title = (is_ie||is_saf) ? links[i].firstChild.nodeValue : links[i].textContent;
		var url = links[i].getAttribute("url");
		div.innerHTML += "<p class='small'><a href='" + url + "'>" + title + "</a></p>";
	}
}

/**
 * Get the link elements from the news feed XML and return as an array
 **/
function get_link_tags(xml) {
	return xml.getElementsByTagName("link");// get the links from our xml as an array
}

/**
 * Returns an XMLHttpRequest object based on the client's browser
 * Thanks to quirksmode.org
 **/
function get_request() {
	var xmlhttp;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	// and security blocked creation of the objects.
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}
	if (!xmlhttp && window.createRequest) {
		try {
			xmlhttp = window.createRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}
	
	return xmlhttp;
}

    var req;

    function initRequest() {

        if (window.XMLHttpRequest) {

            req = new XMLHttpRequest();

        } else if (window.ActiveXObject) {

            req = new ActiveXObject("Microsoft.XMLHTTP");

        }

    }

    function processCampaignRequest() {

        if (req.readyState == 4) {

            if (req.status == 200) {

                document.getElementById('campaign').innerHTML = req.responseText;

            }

        }

    }

    function getCampaign() {

        initRequest();

        req.onreadystatechange = processCampaignRequest;

        var url = "/campaign/HP_Promo_Banner/null/cache";

        req.open("GET", url, true);

        req.send(null);

    }


