var ie = document.all;
var ns = document.layers;
var w3 = (document.getElementById && !ie);
var ClockRefresh = 60;
var BubbleName;
var BubbleNew = false;
var HttpPost = false;
var MyStats = "";

Array.prototype.getUniqueValues = function()
{
	var x = new Object();
	for (j = 0; j < this.length; j++) {x[this[j]] = true}
	var a = new Array();
	for (value in x) {a.push(value)};
	return a;
}

function AddPossessive(a)
{
	var x = a.charAt(a.length-1);
	return (x == "s" || x == "S") ? "'" : "'s";
}

function AjaxGet(u,a,c)
{
	// In most cases you must set c=true, otherwise it caches the first request and keeps returning the same result.
	if (c == true)
		u = (u.search("\\?") == -1) ? u + "?cache=" + new Date().getTime() : u + "&cache=" + new Date().getTime();
	
	var r = false;
	
	if (window.XMLHttpRequest)
	{
		r = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		try
		{
			r = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e)
		{
			try
			{
				r = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
			}
		}
	}
	else
	{
		return "err=1";
	}
	
	// open(Method, Url, Async);
	r.open("GET",u,a);
	r.send(null);
	
	if (a == false)
	{
		if ((window.location.href.indexOf("http") == -1) || (r.status == 200))
		{
			return r.responseText;
		}
		else
		{
			return "err=2";
		}
	}
}

function AjaxPost(u, p, z)
{
	HttpPost = false;
	if (window.XMLHttpRequest)// Mozilla/Safari
	{
		HttpPost = new XMLHttpRequest();
		if (HttpPost.overrideMimeType)
			HttpPost.overrideMimeType("text/html");
	}
	else if (window.ActiveXObject)// IE
	{
		try
		{
			HttpPost = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				HttpPost = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
			}
		}
	}
	
	if (!HttpPost)
	{
		alert("An unexpected connection error occurred. Please try your request again. If you continue to encounter problems, please contact Customer Support.");
		return false;
	}
	
	HttpPost.onreadystatechange = z;
	HttpPost.open("POST", u, true);
	HttpPost.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	HttpPost.setRequestHeader("Content-length", p.length);
	HttpPost.setRequestHeader("Connection", "close");
	HttpPost.send(p);
}

function BubbleClick(x)
{
	if (BubbleName != x)
	{
		BubbleClose();
		BubbleNew = false;
	}
	
	var a = document.getElementById(x + "Text");
	var b = document.getElementById(x + "Image");
	var c = document.getElementById(x + "Bubble");
	
	if (c.style.display == "block" || c.style.display == "inline")
	{
		a.className = "textOut";
		b.className = "imageOut";
		c.style.display = "none";
	}
	else
	{
		a.className = "textOver";
		b.className = "imageOver";
		
		if (x == "Status")
			c.style.display = "block";
		else
			c.style.display = "inline";
		BubbleName = x;
	}
}

function BubbleClose()
{
	if (BubbleName != null && BubbleNew == true)
	{
		var a = document.getElementById(BubbleName + "Text");
		var b = document.getElementById(BubbleName + "Image");
		var c = document.getElementById(BubbleName + "Bubble");
		
		a.className = "textOut";
		b.className = "imageOut";
		c.style.display = "none";
		
		BubbleName = null;
	}
	BubbleNew = true;
}

function BubbleOut(o,x,i)
{
	var y = (i == 1) ? "text" : "image";
	if (x == "Status")
		o.className = (document.getElementById(x + "Bubble").style.display == "block") ? y + "Over" : y + "Out";
	else
		o.className = (document.getElementById(x + "Bubble").style.display == "inline") ? y + "Over" : y + "Out";
}

function BubbleOver(o,i)
{
	var y = (i == 1) ? "text" : "image";
	o.className = y + "Over";
}

function CharRemain(o,s,m)
{
	if (o.value.length > m)
	{
		alert("Your entry cannot be no more than " + FormatNumber(m,0,0) + " characters in length.");
		o.value = o.value.substring(0,m);
	}
	s.innerHTML = FormatNumber((m-o.value.length),0,0) + " Characters Remaining";
}

function ClockCountDown(Id,Sec,Url,Refresh)
{
	// The JavaScript count down appears to fall behind the real time by a ratio of 1 second for every 1 minute that passes.
	// Use the Url to grab via AJAX the most recent Sec every 60 seconds.
	// Always set the Refresh = ClockRefresh (constant declared above).
	
	if (Sec >= 0)
	{
		if (Refresh <= 0)
		{
			Sec = AjaxGet(Url,false,true);
			Refresh = ClockRefresh;
		}
		else
			Refresh--;
		
		document.getElementById(Id).innerHTML = ClockValue(Sec);
		Sec--;
		setTimeout("ClockCountDown('" + Id + "'," + Sec + ",'" + Url + "'," + Refresh + ")", 1000);
	}
	else
		document.getElementById(Id).innerHTML = ClockValue(0);
}

function ClockValue(Ss)
{
	var Mi=0, Hh=0, Dd=0, x=""
	
	if (isNaN(Ss) == true)
		Ss = 0;
	else if (Ss <= 0)
		Ss = 0
	else if (Ss >= 86400)
	{
		Dd = parseInt(Ss / 86400);
		Ss = (Ss - (Dd * 86400));
	}
	
	if (Ss >= 3600)
	{
		Hh = parseInt(Ss / 3600);
		Ss = (Ss - (Hh * 3600));
	}
	
	if (Ss >= 60)
	{
		Mi = parseInt(Ss / 60);
		Ss = (Ss - (Mi * 60));
	}
	
	return LeadZero(Dd) + "<span>day" + Plural(Dd) + "</span>" + LeadZero(Hh) + "<span>hour" + Plural(Hh) + "</span>" + LeadZero(Mi) + "<span>minute" + Plural(Mi) + "</span>" + LeadZero(Ss) + "<span>second" + Plural(Ss) + "</span>"
}

function CountLetters(x)
{
	var y = 0;
	for (var i = 0; i < x.length; i++)
	{
		var a = x.charAt(i);
		if (a=="a"||a=="b"||a=="c"||a=="d"||a=="e"||a=="f"||a=="g"||a=="h"||a=="i"||a=="j"||a=="k"||a=="l"||a=="m"||a=="n"||a=="o"||a=="p"||a=="q"||a=="r"||a=="s"||a=="t"||a=="u"||a=="v"||a=="w"||a=="x"||a=="y"||a=="z"||a=="A"||a=="B"||a=="C"||a=="D"||a=="E"||a=="F"||a=="G"||a=="H"||a=="I"||a=="J"||a=="K"||a=="L"||a=="M"||a=="N"||a=="O"||a=="P"||a=="Q"||a=="R"||a=="S"||a=="T"||a=="U"||a=="V"||a=="W"||a=="X"||a=="Y"||a=="Z")
			y++;
	}
	return y;
}

function CountNumbers(x)
{
	var y = 0;
	for (var i = 0; i < x.length; i++)
	{
		var a = x.charAt(i);
		if (a=="0"||a=="1"||a=="2"||a=="3"||a=="4"||a=="5"||a=="6"||a=="7"||a=="8"||a=="9")
			y++;
	}
	return y;
}

function Display(id,t)
{
	var o = document.getElementById(id);
	var x = (t == 0) ? "block" : "inline";
	o.style.display = (o.style.display == x) ? "none" : x;
}

function EscapeFromHTML(s)
{
	do {s = s.replace("&lt;","<");} while (s.search("&lt;") >= 0);
	do {s = s.replace("&gt;",">");} while (s.search("&gt;") >= 0);
	do {s = s.replace("&amp;","&");} while (s.search("&amp;") >= 0);
	do {s = s.replace("&quot;",'"');} while (s.search("&quot;") >= 0);
	do {s = s.replace("&agrave;","à");} while (s.search("&agrave;") >= 0);
	do {s = s.replace("&Agrave;","À");} while (s.search("&Agrave;") >= 0);
	do {s = s.replace("&acirc;","â");} while (s.search("&acirc;") >= 0);
	do {s = s.replace("&Acirc;","Â");} while (s.search("&Acirc;") >= 0);
	do {s = s.replace("&auml;","ä");} while (s.search("&auml;") >= 0);
	do {s = s.replace("&Auml;","Ä");} while (s.search("&Auml;") >= 0);
	do {s = s.replace("&aring;","å");} while (s.search("&aring;") >= 0);
	do {s = s.replace("&Aring;","Å");} while (s.search("&Aring;") >= 0);
	do {s = s.replace("&aelig;","æ");} while (s.search("&aelig;") >= 0);
	do {s = s.replace("&AElig;","Æ");} while (s.search("&AElig;") >= 0);
	do {s = s.replace("&ccedil;","ç");} while (s.search("&ccedil;") >= 0);
	do {s = s.replace("&Ccedil;","Ç");} while (s.search("&Ccedil;") >= 0);
	do {s = s.replace("&eacute;","é");} while (s.search("&eacute;") >= 0);
	do {s = s.replace("&Eacute;","É");} while (s.search("&Eacute;") >= 0);
	do {s = s.replace("&egrave;","è");} while (s.search("&egrave;") >= 0);
	do {s = s.replace("&Egrave;","È");} while (s.search("&Egrave;") >= 0);
	do {s = s.replace("&ecirc;","ê");} while (s.search("&ecirc;") >= 0);
	do {s = s.replace("&Ecirc;","Ê");} while (s.search("&Ecirc;") >= 0);
	do {s = s.replace("&euml;","ë");} while (s.search("&euml;") >= 0);
	do {s = s.replace("&Euml;","Ë");} while (s.search("&Euml;") >= 0);
	do {s = s.replace("&iuml;","ï");} while (s.search("&iuml;") >= 0);
	do {s = s.replace("&Iuml;","Ï");} while (s.search("&Iuml;") >= 0);
	do {s = s.replace("&ocirc;","ô");} while (s.search("&ocirc;") >= 0);
	do {s = s.replace("&Ocirc;","Ô");} while (s.search("&Ocirc;") >= 0);
	do {s = s.replace("&ouml;","ö");} while (s.search("&ouml;") >= 0);
	do {s = s.replace("&Ouml;","Ö");} while (s.search("&Ouml;") >= 0);
	do {s = s.replace("&oslash;","ø");} while (s.search("&oslash;") >= 0);
	do {s = s.replace("&Oslash;","Ø");} while (s.search("&Oslash;") >= 0);
	do {s = s.replace("&szlig;","ß");} while (s.search("&szlig;") >= 0);
	do {s = s.replace("&ugrave;","ù");} while (s.search("&ugrave;") >= 0);
	do {s = s.replace("&Ugrave;","Ù");} while (s.search("&Ugrave;") >= 0);
	do {s = s.replace("&ucirc;","û");} while (s.search("&ucirc;") >= 0);
	do {s = s.replace("&Ucirc;","Û");} while (s.search("&Ucirc;") >= 0);
	do {s = s.replace("&uuml;","ü");} while (s.search("&uuml;") >= 0);
	do {s = s.replace("&Uuml;","Ü");} while (s.search("&Uuml;") >= 0);
	do {s = s.replace("&reg;","®");} while (s.search("&reg;") >= 0);
	do {s = s.replace("&copy;","©");} while (s.search("&copy;") >= 0);
	do {s = s.replace("&euro;","€");} while (s.search("&euro;") >= 0);
	return s;
}

function EscapeHTML(s)
{
	var x = "";
	for (var i = 0; i < s.length; i++)
	{
		var c = s.charAt(i);
		switch (c)
		{
			case "<" : x += "&lt;"; break;
			case ">" : x += "&gt;"; break;
			case "&" : x += "&amp;"; break;
			case '"' : x += "&quot;"; break;
			case "à" : x += "&agrave;"; break;
			case "À" : x += "&Agrave;"; break;
			case "â" : x += "&acirc;"; break;
			case "Â" : x += "&Acirc;"; break;
			case "ä" : x += "&auml;"; break;
			case "Ä" : x += "&Auml;"; break;
			case "å" : x += "&aring;"; break;
			case "Å" : x += "&Aring;"; break;
			case "æ" : x += "&aelig;"; break;
			case "Æ" : x += "&AElig;"; break;
			case "ç" : x += "&ccedil;"; break;
			case "Ç" : x += "&Ccedil;"; break;
			case "é" : x += "&eacute;"; break;
			case "É" : x += "&Eacute;"; break;
			case "è" : x += "&egrave;"; break;
			case "È" : x += "&Egrave;"; break;
			case "ê" : x += "&ecirc;"; break;
			case "Ê" : x += "&Ecirc;"; break;
			case "ë" : x += "&euml;"; break;
			case "Ë" : x += "&Euml;"; break;
			case "ï" : x += "&iuml;"; break;
			case "Ï" : x += "&Iuml;"; break;
			case "ô" : x += "&ocirc;"; break;
			case "Ô" : x += "&Ocirc;"; break;
			case "ö" : x += "&ouml;"; break;
			case "Ö" : x += "&Ouml;"; break;
			case "ø" : x += "&oslash;"; break;
			case "Ø" : x += "&Oslash;"; break;
			case "ß" : x += "&szlig;"; break;
			case "ù" : x += "&ugrave;"; break;
			case "Ù" : x += "&Ugrave;"; break;
			case "û" : x += "&ucirc;"; break;
			case "Û" : x += "&Ucirc;"; break;
			case "ü" : x += "&uuml;"; break;
			case "Ü" : x += "&Uuml;"; break;
			case "®" : x += "&reg;"; break;
			case "©" : x += "&copy;"; break;
			case "€" : x += "&euro;"; break;
			default : x += c; break;
		}
	}
	return x;
}

function FormatNumber(x,y,z)
{
	x = x.toString().replace(/\$|\,/g,"");
	if (isNaN(x) == true) {x = 0;}
	if ((isNaN(y) == true) || (y < 0)) {y = 2;}
	if (z == 1) {z = 1;} else {z = 0;}
	var a = (x == (x = Math.abs(x)));
	var b = "1";
	for (var i = 0; i < y; i++) {b = (b + "0");}
	x = Math.floor(x * b + 0.50000000001);
	var c = x % b;
	x = Math.floor(x / b).toString();	
	if ((c < 10) & (y >= 2)) {c = "0" + c;}
	if (z == 0) {for (var i = 0; i < Math.floor((x.length - (1 + i)) / 3); i++) {x = x.substring(0,x.length - (4 * i + 3)) + "," +	x.substring(x.length - (4 * i + 3));}}
	if (y == 0) {return ((a ? "" : "-") + x);}
	else {return ((a ? "" : "-") + x + "." + c);}
}

function Init()
{
	document.onclick=BubbleClose;
}

function LeadZero(i)
{
	if (i <= 9)
		return "0" + i;
	else
		return i
}

function NewSearch(i)
{
	var o = document.getElementById("SearchText");
	var p = document.getElementById("searchForm");
	var s = document.getElementById("searchField");
	
	switch (i)
	{
		case 0 : o.innerHTML = "Search Blogs"; p.action = "http://www.debate.org/blogs/"; break;
		case 2 : o.innerHTML = "Search Forums"; p.action = "http://www.debate.org/forums/"; break;
		case 3 : o.innerHTML = "Search Groups"; p.action = "http://www.debate.org/groups/"; break;
		case 4 : o.innerHTML = "Search People"; p.action = "http://www.debate.org/people/"; s.name = "u"; break;
		case 5 : o.innerHTML = "Search Polls"; p.action = "http://www.debate.org/polls/"; break;
		default : o.innerHTML = "Search Debates"; p.action = "http://www.debate.org/debates/"; s.name = "keywords";
	}
}

function Online(s)
{
	var i = 0;
	OnlineTimer(i,s);
}

function OnlineTimer(i,s)
{
	i++;
	if (i >= s)
	{
		i = 0;
		var x = AjaxGet("http://www.debate.org/online/",false,true);
		
		if (x != "" && x != "err=1" && x != "err=2")
			StatsLoad(x);
	}
	setTimeout("OnlineTimer(" + i + "," + s + ")", 1000);
}

function ParentLocation(u)
{
	if (parent.window.opener.closed)
		window.open(u, "newTop");
	else
		parent.window.opener.location = u;
}

function PasswordStrength(x,y,z)
{
	y = document.getElementById(y);
	var Letters = CountLetters(x.value);
	var Numbers = CountNumbers(x.value);
	var Others = x.value.length-Letters-Numbers;
	
	if (x.value.length == 0)
		y.innerHTML = "Password Strength";
	else if (x.value.length >= 8 && Letters > 0 && Numbers > 0 && Others > 0)
		y.innerHTML = "Password Strength: <span style=\"color: #008000; font-weight: bold;\">Strong</span>";
	else if (x.value.length >= 8)
		y.innerHTML = "Password Strength: <span style=\"color: #0000ff; font-weight: bold;\">Medium</span>";
	else if (x.value.length >= z && Letters > 0 && Numbers > 0)
		y.innerHTML = "Password Strength: <span style=\"color: #0000ff; font-weight: bold;\">Medium</span>";
	else if (x.value.length >= z)
		y.innerHTML = "Password Strength: <span style=\"color: #ff0000; font-weight: bold;\">Weak</span>";
	else if (x.value.length < z)
		y.innerHTML = "Password Strength: <span style=\"color: #ff8c00; font-weight: bold;\">Too Short</span>";
}

function Plural(i)
{
	return (i == 1) ? "" : "s" ;
}

function PopContacts(z)
{
	var h = 500;
	var w = 600;
	var x = (screen.width/2)-(w/2);
	var y = (screen.height/2)-(h/2);
	
	window.open("http://www.debate.org/contacts/?f=" + z, "contacts", "height=500, left=" + x + ", resizable=0, scrollbars=1, status=1, top=" + y + ", width=600");
}

function SecCodeFocus(o)
{
	if (o.value == "Type code here...")
	{
		o.value = "";
		o.select();
	}
	else if (o.value == "")
		o.value = "Type code here...";
}

function SecCodeReload(x,y,z)
{
	x = document.getElementById(x);
	x.src = "http://www.debate.org/code/?key=" + y + "&cache=" + new Date().getTime();
	
	z = document.getElementById(z);
	z.value = "Type code here...";
}

function SpellCheck(x)
{
	window.open('http://www.debate.org/spellcheck/default.asp?fields=' + x,'spellcheck','width=460, height=290, scrollbars=no, resizable=no, toolbar=no, location=no, directories=no, status=no, menubar=no, copyhistory=no');
}

function StatsLoad(x)
{
	x = x.split(";");
	x[2] = parseInt(x[2]);
	x[3] = parseInt(x[3]);
	x[4] = parseInt(x[4]);
	x[5] = parseInt(x[5]);
	x[6] = parseInt(x[6]);
	x[7] = parseInt(x[7]);
	x[8] = parseFloat(x[8]);
	x[9] = parseInt(x[9]);
	x[10] = parseInt(x[10]);
	
	MyStats = x;
	
	var y = x[2]+x[3]+x[4]+x[5]+x[6];
	
	if (y == 0)
	{
		document.getElementById("homeCountWrap").style.display = "none";
		document.getElementById("homeCount").innerHTML = "";
	}
	else
	{
		document.getElementById("homeCountWrap").style.display = "inline";
		document.getElementById("homeCount").innerHTML = FormatNumber(y,0,1);
	}
	
	if (x[7] == 0)
		document.getElementById("msgCount").innerHTML = "";
	else
		document.getElementById("msgCount").innerHTML = "(<span class=\"value\">" + FormatNumber(x[7],0,1) + "</span>)";
	
	StatusChanged(x[9]);
	
	if (x[10] != 0)
		document.location = "http://www.debate.org/logout/";
	else if (x[11].toUpperCase() != "FALSE")
		alert(x[11]);
}

function StatusChange(i)
{
	var x = AjaxGet("http://www.debate.org/online/status/?x=" + i,false,true);
	
	if (x.toUpperCase() == "TRUE")
		StatusChanged(i);
	else
		alert("Your request to change your status was not completed. Please try again.");
}

function StatusChanged(i)
{
	i = parseInt(i);
	var o = document.getElementById("myStatus");
	switch (i)
	{
		case 0 : o.innerHTML = "Offline"; break;
		case 2 : o.innerHTML = "Away"; break;
		case 3 : o.innerHTML = "Busy"; break;
		default : o.innerHTML = "Online"; break;
	}
}

function Trim(x,y)
{
	while (x.substring(0,1) == y){x = x.substring(1,x.length);}
	while (x.substring(x.length-1,x.length) == y){x = x.substring(0,x.length-1);}
	return x;
}
