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 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 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 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 PopUp(b,h,n,r,s,u,w)
{
	var l = (screen.width/2)-(w/2);
	var t = (screen.height/2)-(h/2);
	
	window.open(u, n, "height=" + h + ", left=" + l + ", resizable=" + r + ", scrollbars=" + s + " , status=" + b + ", top=" + t + ", width=" + w);
}

function ReadCookie(x)
{
	var y = "" + document.cookie;
	var i = y.indexOf(x);
	if (i == -1 || x == "")
		return "";
	var j = y.indexOf(';',i);
	if (j == -1)
		j = y.length;
	return unescape(y.substring(i + x.length + 1, j));
}

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://dork.com/code/?key=" + y + "&cache=" + new Date().getTime();
	
	z = document.getElementById(z);
	z.value = "Type code here...";
}

function Theme(i)
{
	document.getElementById("theme").href = "http://dork.com/styles/themes/" + i + ".css";
	AjaxGet("http://dork.com/theme/?t=" + i,false,true);
}

function WhatIsADork()
{
	OpenVeil("530px","What is a Dork?","<div id=\"dork\"><strong>Dork</strong> [<em>Dôrk</em>] noun - a person with a heightened sense of humor, strong proclivity for entertainment, or passion for living.</div>");
}
