// Vignette Generated Content Marker - DO NOT REMOVE
// stores reference to XMLHttpRequest object
var xmlHttp = createXmlHttpRequestObjectRecipes();
	
//Retrieves XMLHttpRequest object
function createXmlHttpRequestObjectRecipes() {
	//will store the reference to the XMLHttpRequest object
	var xmlHttp;
	//if running Internet Explorer
	if(window.ActiveXObject) {
       try {
           xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
       }
		catch (e) {
		xmlHttp = false;
		}
	} else {
		// if running Mozilla or other
		try {
			xmlHttp = new XMLHttpRequest();
		}
		catch (e) {
			xmlHttp = false;
		}
	}
	//return the created object or display an error message
	if (!xmlHttp) {
		alert("Error here");
	} else {
		return xmlHttp;
	}
}

//Make asynchronous HTTP request using the XMLHttpRequest object
function toptenrecipes(pageName) {
//alert("page is "+page);
	//proceed only if xmlHttp object isn't busy
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0) { 
	   	xmlHttp.open("GET", "http://www.ivillage.co.uk/toptenrecipes.xml?page=" + pageName, true);
		//define the method to handle server responses
		xmlHttp.onreadystatechange = handleServerResponseRecipes;
		//make the server request
		xmlHttp.send(null);
	} else {
		alert("page is "+page);
		//if the connection is busy try again after 1 second
		setTimeout('toptenrecipes("'+pageName+'");', 1000);
	}
}
	
//executed automatically when a message is received from the server
function handleServerResponseRecipes() {
	//alert("in handler");
	//move forward only if transaction has completed
	if (xmlHttp.readyState == 4) {
		//status of 200 indicates the transaction completed successfully
		if (xmlHttp.status == 200) {
			//extract the XML received from the server
			xmlResponse = xmlHttp.responseXML;
			//obtain the document element (the root element) of the XML structure
			xmlDocumentElement = xmlResponse.documentElement;
			linkArray = xmlDocumentElement.getElementsByTagName("link");
			pageArray = xmlDocumentElement.getElementsByTagName("page");
			var page = pageArray.item(0).firstChild.data;
			if (page != "hp") {
				var html = "<h2>Top Ten Recipes</h2><ol>";
			} else {
				var html = "<ol>";
			}
			//iterate through arrays to create html structure
			for (var i=0; i<linkArray.length; i++) {
					html += "<li class=\"list_item\" style=\"font-size:12px;\">"+linkArray.item(i).firstChild.data +"</li>";
			}
			
			html += "</ol><span class=\"go_line\"><a href=\"http://www.ivillage.co.uk/food/tools/recipefinder\" class=\"more\">MORE RECIPES...</a></span>";
			//update the client display with data received from the server
			var divRecipes = document.getElementById("DE_subdiv4");
			divRecipes.innerHTML = html;
			
			//document.getElementById('divMessage').style.display = 'block';
			//restart sequence
			//alert("pagearray is: "+page);
			setTimeout('toptenrecipes("'+page+'");', 900000);
		} else {
			//a http status different that 200 signals an error
			alert("problem: " + xmlHttp.statusText);
		}
	}
}