function getAJAX(querytype)
{
	showLoading(querytype);
	
	if (querytype == 'countdown')
	{
		var date = arguments[1];
		var time = arguments[2];
		var show = arguments[3];
		pURL = 'ajaxcountdown.php?date=' + date + "&time=" + time + '&showint=' + show;
		endFunction = loadCountdown;
	}
	else if (querytype == 'randomevent')
	{
		var method = arguments[1];
		pURL = 'randomevent.php?method=' + method;
		endFunction = setRandomEvent;
	}
/*	else if (querytype == 'recent')
	{
		pURL = 'recentcountdowns.php';
		endFunction = setRecentCountdowns;
	}
*/
	else if (querytype == 'countdowndata')
	{
		var id = arguments[1];
		pURL = 'loadcountdowndata.php?s=' + id;
		endFunction = setCountdownData;
	}
	else if (querytype == 'screennames')
	{
		pURL = 'screennames.php';
		endFunction = setScreenNames;
	}
	else if (querytype == 'storecookie' || querytype == 'save')
	{
		var SN = arguments[1];
		var event = escape(arguments[2]);	// escaped to allow '&' and other characters
		var date = arguments[3];
		var time = arguments[4];
		var font = arguments[5];
		var background = arguments[6];
		var textcolor = arguments[7];
		var showint = arguments[8];
		
		var dataString = '?sn=' + SN + '&event=' + event + '&date=' + date + '&time=' + time + '&font=' + font + '&background=' + background + '&textcolor=' + textcolor + '&showint=' + showint;
		
		if (querytype == 'storecookie')
		{
			file = 'storecookie.php';
			endFunction = null;
		}
		else if (querytype == 'save')
		{
			file = 'savecountdown.php';
			endFunction = setLinkCodes
		}
			
		pURL = file + dataString;
		//alert(pURL);
	}
	else if (querytype == 'loadcookie')
	{
		pURL = 'loadcookie.php';
		endFunction = setCountdownData;
		hideLoading(querytype);
	}
	
	xmlhttp = Array;
	
	//create the Cross-browser XMLHttpRequest object
	if (window.XMLHttpRequest)
	{	// code for Mozilla, Safari, etc
		xmlhttp[querytype]=new XMLHttpRequest();
		if (xmlhttp[querytype].overrideMimeType)
		{
			xmlhttp[querytype].overrideMimeType('text/plain');
		}      
		xmlhttp[querytype].onreadystatechange=endFunction;
		xmlhttp[querytype].open("GET", pURL, true);
		xmlhttp[querytype].send(null);
	}
	else if (window.ActiveXObject)
	{	//IE
		xmlhttp[querytype]=new ActiveXObject('Microsoft.XMLHTTP');
		if (xmlhttp[querytype])
		{
			xmlhttp[querytype].onreadystatechange=endFunction;
			xmlhttp[querytype].open('GET', pURL, false);
			xmlhttp[querytype].send();
		}
	}
}



function loadCountdown()
{
	var querytype = 'countdown';
	if (xmlhttp[querytype].readyState==4)
	{
		if (true || xmlhttp[querytype].status==200)
		{
			document.getElementById('preview_time').innerHTML=xmlhttp[querytype].responseText;
		}
		hideLoading(querytype);
	}
}

function setRandomEvent()
{
	var querytype = 'randomevent'
	if (xmlhttp[querytype].readyState==4)
	{
		if (xmlhttp[querytype].status==200)
		{			
			var sections = xmlhttp[querytype].responseText.split("\n");
			var date = sections[1].split("/");
			var event = sections[0];
			
			setEvent(event);
			setDate(date[0], date[1], date[2]);
		}
		
		hideLoading(querytype);
	}
}
/*
function setRecentCountdowns()
{
	var querytype = 'recent';
	if (xmlhttp[querytype].readyState==4)
	{
		if (xmlhttp[querytype].status==200)
		{
			var recent = xmlhttp[querytype].responseText;
			setRecent(recent)
		}
		hideLoading(querytype);
	}
}
*/

function setCountdownData()
{
	var querytype = 'countdowndata';
	if (xmlhttp[querytype].readyState==4)
	{
		if (xmlhttp[querytype].status==200)
		{
			//alert(xmlhttp[querytype].responseText.length);
			
			if (xmlhttp[querytype].responseText.length >= 20)		// if more than just line breaks
			{
				var sections = xmlhttp[querytype].responseText.split("\n");
				
				var sn = sections[0];
				var event = sections[1];
				var date = sections[2].split("/");
				var time = sections[3].split(":");
				var font = sections[4];
				var background = sections[5];
				var textcolor = sections[6];
				var showint = sections[7];
				
				// if sn cannot be changed (existing countdown)
				if(sn.indexOf('!') != -1)
				{
					sn = sn.substr(0, sn.length - 1);
					disableSN();
				}
				
				//setSN(sn);
				setDisplayOptions(showint);			// done before date is set (ajax call to update)
				setEvent(event);
				setTime(time[0], time[1]);			// set time before date so it is updated with the time
				setDate(date[0], date[1], date[2]);	// when setDate requests countdown
				setFont(font);
				setBackgroundColor(background);
				setTextColor(textcolor);
			}
			
			else
			{
				defaultSettings();
			}	
		}
		
		hideLoading(querytype);
	}
}

function setScreenNames()
{
	var querytype = 'screennames';
	if (xmlhttp[querytype].readyState==4)
	{
		if (xmlhttp[querytype].status==200)
		{
			var screennames = xmlhttp[querytype].responseText.split("\n");
			new AutoSuggest(document.getElementById('sn'), screennames);
			if (document.getElementById('sn').value.length == 0)
				document.getElementById('sn').value = screennames[0];
		}
		
		hideLoading(querytype);
	}
}

function setLinkCodes()
{
	var querytype = 'save';
	if (xmlhttp[querytype].readyState==4)
	{
		if (xmlhttp[querytype].status==200)
		{
			countdownid = xmlhttp[querytype].responseText;
			
			var linkbase = 'http://aimcountdown.com/v?';
			var separator = ':';
			var endchar = '$';
			var href = linkbase + countdownid + separator + '%n' + endchar;
			
			showLink(href);
		}
		
		hideLoading(querytype);
	}
}