var req;
var req2;
var req3;
var column;
var hole;

function loadXMLDoc(url, a) { //for the bots
	column = a;
	req = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) {
    	try {
			req = new XMLHttpRequest();
        } catch(e) {
			req = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req = false;
        	}
		}
    }
	if(req) {
		req.open("GET", url, true);
		req.onreadystatechange = processReqChange;
		req.send("");
	}
}

function processReqChange() { //for the bots
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if ((req.status == 200) || (req.status == 0)) {
			allparts = req.responseText;
			
			newObject = document.createElement('li');
			newObject.setAttribute('class', 'botLi');
			newObject.setAttribute('className', 'botLi');
			newObject.innerHTML = allparts;
			
			column.insertBefore(newObject, column.firstChild); 
			
			if (allparts.split('customBotList').length > 1) { //check to see if we inserted the custom bot - if so, load it dynamically
				loadCustomBot();
				document.getElementById('buildtile').style.display = 'none';
			}
			if (allparts.split('weatherBot').length > 1) { //check to see if we inserted the weather bot - if so, load it dynamically
				loadXMLDoc3();
			}
			if (allparts.split('lottoBot').length > 1) { //check to see if we inserted the lotto bot - if so, load it dynamically
				loadXMLDoc4('inc/ajax/lotto_hotnumbers_data.xml');
			}
            //column.appendChild(newObject);
			Sortable.create("firstlist",
     				{dropOnEmpty:true,handle:'handle',containment:["firstlist","secondlist"],constraint:false});
   			Sortable.create("secondlist",
    			 	{dropOnEmpty:true,handle:'handle',containment:["firstlist","secondlist"],constraint:false});
			madeChange();
        } else {
            alert("There was a problem retrieving the XML data:\n" +
                req.statusText);
        }
    }
}

function loadXMLDoc2(url, a) { //for the bubbles
	hole = a;
	req2 = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) {
    	try {
			req2 = new XMLHttpRequest();
        } catch(e) {
			req2 = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	req2 = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req2 = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req2 = false;
        	}
		}
    }
	if(req2) {
		req2.open("GET", url, true);
		req2.onreadystatechange = processReqChange2;
		req2.send("");
	}
}

function processReqChange2() { //for the bubbles
    // only if req shows "loaded"
    if (req2.readyState == 4) {
        // only if "OK"
        if ((req2.status == 200) || (req2.status == 0)) {
			allparts = req2.responseText;
			partarray1 = allparts.split("<!--beginContentPull-->");
			partarray2 = partarray1[1].split("<!--endContentPull-->");
			contentpart = partarray2[0];
            document.getElementById(hole).innerHTML = contentpart;
        } else {
            alert("There was a problem retrieving the XML data:\n" +
                req.statusText);
        }
    }
}

function loadXMLDoc3() { //for the weather bot
	req3 = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) {
    	try {
			req3 = new XMLHttpRequest();
        } catch(e) {
			req3 = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	req3 = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req3 = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req3 = false;
        	}
		}
    }
	if(req3) {
		url = "inc/ajax/weather_data.xml";
		req3.open("GET", url, true);
		req3.onreadystatechange = processReqChange3;
		req3.send("");
	}
}

function processReqChange3() { //for the weather
    // only if req shows "loaded"
    if (req3.readyState == 4) {
        // only if "OK"
        if ((req3.status == 200) || (req3.status == 0)) {
			allparts = req3.responseXML;
			conditions = allparts.getElementsByTagName('weather')[0].firstChild.nodeValue; //get current conditions
			temp = allparts.getElementsByTagName('temperature_string')[0].firstChild.nodeValue; //get current temp
			wind = allparts.getElementsByTagName('wind_string')[0].firstChild.nodeValue; //get current wind conditions
			updated = allparts.getElementsByTagName('observation_time')[0].firstChild.nodeValue; //get last updated
			base_url = "img/weather/";
			//base_url = allparts.getElementsByTagName('icon_url_base')[0].firstChild.nodeValue; //get first part of icon URL
			icon_name = allparts.getElementsByTagName('icon_url_name')[0].firstChild.nodeValue; //get first part of icon URL
			icon_url = base_url + icon_name;
			document.getElementById('weather_conditions').innerHTML = conditions;
			document.getElementById('weather_temperature').innerHTML = temp;
			document.getElementById('weather_wind').innerHTML = wind;
			document.getElementById('weather_updated').innerHTML = updated;
			document.getElementById('weather_icon').setAttribute('src', icon_url);
        } else {
            alert("There was a problem retrieving the XML data:\n" +
                req3.statusText);
        }
    }
}

function loadXMLDoc4(url) { //for the lotto bot
	req4 = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) {
    	try {
			req4 = new XMLHttpRequest();
        } catch(e) {
			req4 = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	req4 = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req4 = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req4 = false;
        	}
		}
    }
	if(req4) {
		req4.open("GET", url, true);
		req4.onreadystatechange = processReqChange4;
		req4.send("");
	}
}

function processReqChange4() { //for the lotto bot
    // only if req shows "loaded"
    if (req4.readyState == 4) {
        // only if "OK"
        if ((req4.status == 200) || (req4.status == 0)) {
			var theInfo = req4.responseXML.getElementsByTagName('row')[0]; //get the single tag that comes in from the XML file. All values we need will be attributes of this tag.
			var game = theInfo.getAttribute('gamename');
			var numbers = "";
			for (i=0;i<5;i++) {
				numbers += theInfo.getAttribute('whiteball_' + (i+1)) + " ";
			}
			numbers = numbers.replace(/\s*$/, ''); //remove trailing whitespace
			if (game == 'Hot Lotto') {
				gameAbbr = 'hot';
			} else {
				gameAbbr = 'power';
			}
			document.getElementById(gameAbbr + '_numbers').innerHTML = numbers;
			document.getElementById(gameAbbr + '_ball').innerHTML = theInfo.getAttribute(gameAbbr + "ball");
			if (document.getElementById(gameAbbr + '_play')) {
				document.getElementById(gameAbbr + '_play').innerHTML = theInfo.getAttribute(gameAbbr + "play");
			}
			document.getElementById(gameAbbr + '_drawdate').innerHTML = theInfo.getAttribute('drawdate');
			if (game == 'Hot Lotto') {
				loadXMLDoc4('inc/ajax/lotto_pbnumbers_data.xml');
			}
        } else {
            alert("There was a problem retrieving the XML data:\n" +
                req.statusText);
        }
    }
}