// declare a global  XMLHTTP Request object
var XmlHttpObj;

// create an instance of XMLHTTPRequest Object, varies with browser type, try for IE first then Mozilla
function CreateXmlHttpObj()
{
	// try creating for IE (note: we don't know the user's browser type here, just attempting IE first.)
	try
	{
		XmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XmlHttpObj = null;
		}
	}
	// if unable to create using IE specific code then try creating for Mozilla (FireFox) 
	if(!XmlHttpObj && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttpObj = new XMLHttpRequest();
	}
}

// functions to handle add/edit drop-downs
function SportOnChange(thisid) 
{
    var requestUrl;
    // use the following line if using asp
    //requestUrl = "xml_data_provider.asp" + "?filter=" + encodeURIComponent(selectedContinent);
    // use the following line if using php
		//alert(thisid);
     requestUrl = "giveawaysxml.php" + "?cat=" + encodeURIComponent(thisid);
	//alert ("Requesting page: " + requestUrl);
    
	CreateXmlHttpObj();
	
	// verify XmlHttpObj variable was successfully initialized
	if(XmlHttpObj)
	{
        // assign the StateChangeHandler function ( defined below in this file)
        // to be called when the state of the XmlHttpObj changes
        // receiving data back from the server is one such change
		XmlHttpObj.onreadystatechange = SportChangeHandler;
		
		// define the iteraction with the server -- true for as asynchronous.
		XmlHttpObj.open("GET", requestUrl,  true);
		
		// send request to server, null arg  when using "GET"
		XmlHttpObj.send(null);		
	}
}
function SportChangeHandler()
{
	// state ==4 indicates receiving response data from server is completed
	if(XmlHttpObj.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttpObj.status == 200)
		{			
			//alert("XML status is 200");
			PopulateNewSports(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
			alert("There was a problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		}
	}
}

// returns the node text value 
function GetInnerText (node)
{
	 return (node.textContent || node.innerText || node.text) ;
}

function PopulateNewSports(productNode)
{
	var htmlcontents = document.getElementById("postcontents");
	var productNodes = productNode.getElementsByTagName('item');
	htmlcontents.innerHTML = "";
	var idValue;
	var textValue; 
	var optionItem;
	var contentsstring = "";
	//alert(productNodes.length);
	if(productNodes.length > 0) {
		contentsstring = "<ul>";
		// populate the dropdown list with data from the xml doc
		//alert("Populating project list");
		//optionItem = new Option( 'Sport...', '',  false, false);
		//productList.options[productList.length] = optionItem;
		for (var count = 0; count < productNodes.length; count++)
		{
			textValue = GetInnerText(productNodes[count]);
			idValue = productNodes[count].getAttribute("id");
			var li = '<li><a href="javascript: init(&quot;viewgiveaway.php?height=500&width=700&modal=true&id=' + idValue + '&quot;)">' + textValue + '</a></li>';
			contentsstring += li;
		}
		contentsstring += "</ul>";
	}
	else {
		contentsstring = "No promotions and giveaways are<br />available for this sport.";
	}
	//alert(contentsstring)
	htmlcontents.innerHTML = contentsstring;
}