function checkUsername()
{
	// preload images
	img1 = new Image(100,25);
	img1.src="images/icon_working.gif";
	img2 = new Image(100,25);
	img2.src="images/icon_available.gif";
	img3 = new Image(100,25);
	img3.src="images/icon_unavailable.gif";
	img4 = new Image(100,25);
	img4.src="images/icon_error.gif";


	var i;
	// first, set the working images
	for (i = 1; i <= gnArSize; i++)
	{
		var el = document.getElementById("ind_" + i);
		if (el != null)
			el.src = "images/icon_working.gif";

		el = document.getElementById("stat_" + i);
		if (el != null)
			el.innerHTML = "Checking...";
	}

	// create AJAX connections
/*	for (i = 1; i <= gnArSize; i++)
	{
		var el = document.getElementById("ind_" + i);
		if (el != null)
		{
			var checker = new ajaxChecker(i);
			checker.update();
		}
	} */

	for (i = 1; i <= gnArSize; i++)
	{
		var site = new ajaxObject(i);
		site.update();
	}

// ori:
//	var i = 0;
//	for (i = 1; i < sites.length; i++)
//	{
//		var site = new ajaxObject(sites[i]['sitename']);
//		site.update("username="+document.getElementById('username').value + "&site=" + sites[i]['id'],  sites[i]['link'])
//	}
//	checkDomain(document.getElementById('username'));

}


function ajaxChecker(ndx)
{
	this.id = ndx;
	this.imgEl = document.getElementById("ind_" + ndx);
	this.statEl = document.getElementById("stat_" + ndx);
	this.AJAX = null;

	// stateChange -- called from the AJAX object when the state changes
	this.stateChange = function()
	{
alert("inside stateChange " + this.id);
		if (this.AJAX.readyState == 4)
		{
			var response = this.AJAX.responseText;
			if (response == "Available")
				this.imgEl.src = "images/icon_available.gif";
			else
				this.imgEl.src = "images/icon_unavailable.gif";
			this.statEl.innerHTML = response;
		}
	}


	// update - updates the state of each of the social network sites
	this.update = function()
	{
		if (window.XMLHttpRequest)
			this.AJAX = new XMLHttpRequest();
		else
			this.AJAX = new ActiveXObject("Microsoft.XMLHTTP");

		if (this.AJAX == null)
		{
			alert("Your browser doesn't support AJAX.");
			return false;
		}
		else
		{
/*
			function statechanged()
			{
				if (AJAX.readyState == 4)
				{
					var response = AJAX.responseText;
					if (response == "Available")
						this.imgEl.src = "images/icon_available.gif";
					else
						this.imgEl.src = "images/icon_unavailable.gif";
					this.statEl.innerHTML = response;
				}
			}
*/
//			AJAX.onreadystatechange = statechanged;
			this.AJAX.onreadystatechange = this.stateChange;

			var sUri = "checksite.php?id=" + this.id + "&username=" + gsUserName;
//alert("checking [" + sUri + "]");
			this.AJAX.open("GET", sUri)
			this.AJAX.send(null);
			return true;
		}
	}
}


function ajaxObject(nId)
{
	this.update = function()
	{
		var AJAX = null;

		if (window.XMLHttpRequest)
			AJAX=new XMLHttpRequest();
		else
			AJAX=new ActiveXObject("Microsoft.XMLHTTP");

		if (AJAX==null)
		{
			alert("Your browser doesn't support AJAX.");
			return false;
		}
		else
		{
			function stateChanged()
			{
				if (AJAX.readyState == 4)
				{
					var sHref = AJAX.responseText;
					var sResult = sHref.substring(0, 1);
					switch (parseInt(sResult))
					{
					case 1:
						if (sHref.length > 1)
							sText = '<a href="http://' + sHref.substring(1) + '" target="network">Available</a>';
						else
							sText = 'Available';
						sImg = 'available';
						break;
					case 2:
						sText = 'Unavailable';
						sImg = 'unavailable';
						break;
					case 0:
					default:
						sText = 'Error';
						sImg = 'error';
						break;
					}
					if (m_elImg != null)
					{
						m_elImg.src = "images/icon_" + sImg + ".gif";
						m_elStat.innerHTML = "<br/>" + sText;
					}
				}
			}
			AJAX.onreadystatechange = stateChanged;

			var uri = "checkname.php?username=" + gsUserName + "&id=" + nId;
//alert("checking [" + uri + "]");
			AJAX.open("GET", uri)
			AJAX.send(null);
			return true;
		}
	}

	var m_nId = nId;
	var m_elImg = document.getElementById('ind_' + nId);
	var m_elStat = document.getElementById('stat_' + nId);
//	var LayerID = document.getElementById(layer);
}


// setupResize - set up a handler to be called when the page is resized
function setupResize()
{
	// onresize, call the function
	if (window.addEventListener)
		window.addEventListener("resize", handleResize, "false");
	else if (window.attachEvent)
		window.attachEvent("onresize", handleResize);
	handleResize();

	// if there is a user name, call the check function
	if (gsUserName.length > 0)
		checkUsername();
}


// handleResize - handles resize events
function handleResize()
{
	var elBanner = document.getElementById("banner");
	if (elBanner != null)
	{
		var nWid = getWindowWidth();
		if (nWid < 822)
			nWid = 822;
		nWid -= 40; // 28;
		var nHgt = 150;

//		var nRatio = nWid / 950;
//		var nHgt = Math.floor(nHgt * nRatio);
////alert('resizing image to ' + nWid + "x" + nHgt);
//
//		elBanner.style.width = nWid + "px";
//		elBanner.style.height = nHgt + "px";

		var nRegWid = 250;
		var nRegHgt = 153;

		var nCook = parseInt(readCookie("registered"));
//alert("registered [" + nCook + "]");
		var elReg = document.getElementById("register");
		if (elReg != null)
		{
			if (readCookie("registered") != 1)
			{
//				alert("setting left to [" + (nWid - 200) + "]");
				elReg.style.display = "block";
				elReg.style.left = (nWid - nRegWid) + "px";
				var nNewTop = (7 + ((nHgt - nRegHgt) / 2));
				if (nNewTop < 7)
					nNewTop = 7;
				elReg.style.top = nNewTop + "px";
//alert("pos: " + elReg.style.left + "x" + elReg.style.top);
			}
		}
	}
}


// isIE - return [true] if the browser is Internet Explorer
function isIE()
{
	var nav = navigator.appVersion.indexOf("MSIE");
	return (nav>0) && (parseInt(navigator.appVersion.substring(nav+5, nav+6)) >= 4);
}


// isNS - return [true] if the browser is a netscape browser
function isNS()
{
	return ((navigator.appName == "Netscape") &&
				(parseInt(navigator.appVersion.substring(0, 1)) >= 5) || navigator.appName == "Mozilla")
    var ns = navigator.appName
}


// getWindowWidth - return width of working part of the window
function getWindowWidth()
{
	try
	{
		if (this.isIE())
			return (document.body.offsetWidth);
		if (this.isNS())
			return (window.innerWidth);
	}
	catch (e)
	{
	}
	return (false);
}

// readCookie - get a cookie's value
function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for (var i = 0; i < ca.length; i++)
	{
		var c = ca[i];
		while (c.charAt(0) == ' ')
			c = c.substring(1);
		if (c.indexOf(nameEQ)==0)
			return c.substring(nameEQ.length);
	}
	return "";
}

// writeCookie - write a cookie's value
function writeCookie(sName, value, expiredays)
{
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie = sName + "=" + escape(value) +
		((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}

// exitPopup - called when the page is shutting down; used to display exit popup form
function exitPopup()
{
	if (g_fShowForm && parseInt(readCookie("registered")) != 1)
	{
		var nWid = 480;
		var nHgt = 360;
		var sSpecs = "width=" + nWid + ",height=" + nHgt + ",status=no,menubar=no,location=no," +
			"resizable=no,titlebar=no,toolbar=no,scrollbars=no,location=no,top=" +
			(screen.height/2-(nHgt/2)) + ",left=" + (screen.width/2-(nWid/2));

		win2 = window.open("exitform.php", "cyu_popup", sSpecs);
	}
}
var g_fShowForm = true;


// noExitForm - called when submitting forms so the exit popup doesn't appear
function noExitForm()
{
	g_fShowForm = false;
	return (true);
}

// tell the browser to show the exit popup form
window.onunload = exitPopup;
//writeCookie("registered", 0, 1);