
// minimum AJAX functionality

function getXMLHttpRequest() {
	if (window.XMLHttpRequest) {return new XMLHttpRequest();}
	else if (window.ActiveXObject) { 
		try {return new ActiveXObject("Msxml2.XMLHTTP");} 
		catch (e) {
			try {return new ActiveXObject("Microsoft.XMLHTTP");}
			catch (e) {}
		}
	}
	return null;
}

var ajax;

function gotit() {
	if (ajax.readyState == 4) {
		if (ajax.status == 200) {
			e = document.getElementById("reply");
			var reply = ajax.responseText;
			if (reply == 'OK')
			{
				window.location = "registered.html";
			}
			else
			{
				var items = reply.split(",");
				if (items[0] == "ERROR")
				{
					err(items[1]);
				}
				else
				{
					err(999);
				}
			}
		}
		else
		{
			err(998);
		}
	}
}

function send() {
	var params = "";
	ajax = getXMLHttpRequest();
	ajax.onreadystatechange = gotit;
	
	nm = document.getElementById('nm');
	email = document.getElementById('email');
	pwd = document.getElementById('pwd');
	pwd2 = document.getElementById('pwd2');
	av = document.getElementById('av');
	
	params += "nm="    + encodeURIComponent(nm.value) + "&";
	params += "email=" + encodeURIComponent(email.value) + "&";
	params += "pwd=" + encodeURIComponent(pwd.value) + "&";
	params += "pwd2=" + encodeURIComponent(pwd2.value) + "&";
	params += "av=" + encodeURIComponent(av.value);

	ajax.open('POST','signup.php',true);
	ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	
	ajax.send(params);
}

function validate()
{	
	e = document.getElementById('av');
	if (vrange(e.value,1,9999) == false) return err(1);
	e = document.getElementById('nm');
	if (vlength(e.value,4,9999) == false) return err(2);
	else if (vlength(e.value,4,12) == false) return err(2);
	else if (valphanum(e.value) == false) return err(2);
	e = document.getElementById('email');
	if (vemail(e.value) == false) return err(3);
	p1 = document.getElementById('pwd');
	if (vlength(p1.value,6,9999) == false) return err(4);
	else if (vlength(p1.value,6,12) == false) return err(4);
	else if (valphanum(p1.value) == false) return err(4);
	p2 = document.getElementById('pwd2');
	if (p1.value != p2.value) return err(5);
	
	return true;
}

function validateLogin()
{	
	e = document.getElementById('nm');
	if (valphanum(e.value) == false) return err(201);

	e = document.getElementById('pwd');
	if (valphanum(e.value) == false) return err(202);
		
	return true;
}

function validateOrderLogin()
{
	var q = document.location.search;
	q = q.substring(1);
	var pairs = q.split("&");
	var pair = "";
	var planname = "";
	var months = "";
	for (var i=0; i<pairs.length; i++)
	{
		pair = pairs[i].split("=");
		if (pair[0] == "plan")
		{
			planname = pair[1];
		}
		if (pair[0] == "months")
		{
			months = pair[1];
		}
	}
	e = document.getElementById('plan');
	e.value = planname+'-'+months;
	
	return validateLogin();
}

function validateName()
{	
	e = document.getElementById('nametaginput');

	if (e.value == "Type Your Name Here") return err(11);
	if (vlength(e.value,6,9999) == false) return err(12);
	else if (vlength(e.value,6,12) == false) return err(13);
	else if (valphanum(e.value) == false) return err(14);

	return true;
}

var errs = {
	1 : 'Please select a character',
	2 : 'Please enter a valid name for your character',
	3 : 'Please enter a valid email address',
	4 : 'Please enter a valid password',
	5 : 'Please confirm your password',
	6 : 'Sorry, that username is already taken',
	7 : 'Sorry, that character name is already taken',
	8 : 'Sorry, please choose another character name',
	9 : 'This account has not been activated',
	10 : 'Error: too many banned accounts',
	11 : 'Please enter a name!',
	12 : 'That name is too short.',
	13 : 'That name is too long.',
	14 : 'Letters and numbers only, please.',
	101 : 'Please select a subject for your message',
	102 : 'Please enter a valid email address (or leave email blank)',
	103 : 'Please enter a message',
	201 : 'Please enter a valid username',
	202 : 'Please enter a valid password',
	203 : 'Sorry, that login didn\'t work!',
	301 : 'Sorry, your login has timed out. Please login again.',
	302 : 'Sorry, there was an unexpected error logging in.',
	998 : 'Error contacting server',
	999 : 'Unknown error'
};

function err(msg)
{
	e = document.getElementById('error1');
	if (typeof errs[msg] == 'undefined') e.innerHTML = "Unknown error";
	else e.innerHTML = errs[msg];
	e.className = 'error_on';
	
	return false;
}

function vrange(n, min, max)
{
	if (/^\d+$/.test(n))
	{
		var num = new Number(n);
		var val = num.valueOf();
		if (val >= min)
		{
			if (val <= max) return true;
		}
	}
	
	return false;
}

function vlength(s, min, max)
{
	n=(s.length);
	return (n>=min && n<=max);
}

function valphanum(s)
{
	return /^[a-zA-Z0-9_]+[a-zA-Z0-9_ ]*[a-zA-Z0-9_]+$/.test(s);
}

function vemail(s)
{
	var qtext          = "[^\\x0d\\x22\\x5c\\x80-\\xff]";
	var dtext          = "[^\\x0d\\x5b-\\x5d\\x80-\\xff]";
	var atom           = "[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c"+
						 "\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+";
	var quoted_pair    = "\\x5c[\\x00-\\x7f]";
	var domain_literal = "\\x5b("+dtext+"|"+quoted_pair+")*\\x5d";
	var quoted_string  = "\\x22("+qtext+"|"+quoted_pair+")*\\x22";
	var domain_ref     = atom;
	var sub_domain     = "("+domain_ref+"|"+domain_literal+")";
	var word           = "("+atom+"|"+quoted_string+")";
	var domain         = sub_domain+"(\\x2e"+sub_domain+")+";
	var local_part     = word+"(\\x2e"+word+")*";
	var addr_spec      = local_part+"\\x40"+domain;
	
	re = new RegExp("^"+addr_spec+"$");
	return re.test(s);
}

function launch(accountid, charid, warn)
{
	var e = document.getElementById("t");
	var t = e.value;
	
	var width = Math.max(800, window.screen.availWidth);
	var height = Math.max(600, window.screen.availHeight);

	var wleft = (screen.width -  width) / 2;
  	var wtop = (screen.height - height) / 2;
  		
	t = encodeURIComponent(t);
	
	var small = "0";
	if ((width < 801) || (height < 601)) small = "1";
	
	var win = window.open("http://web.sifakaworld.com/world.php?t="+t+"&a="+accountid+"&c="+charid+"&w="+warn+"&s="+small,"SifakaWorld","width="+width+",height="+height+",toolbar=0,location=0,status=1,resizable=0");
	document.location.replace("http://world.sifakaworld.com/launched.html");
	win.focus();
  	
  	win.moveTo(wleft, wtop);
}

function justopen()
{	
	var width = Math.max(800, window.screen.availWidth);
	var height = Math.max(600, window.screen.availHeight);

	var wleft = (screen.width -  width) / 2;
  	var wtop = (screen.height - height) / 2;
	
	var small = "0";
	if ((width < 801) || (height < 601)) small = "1";
	
	var win = window.open("about:blank","SifakaWorld","width="+width+",height="+height+",toolbar=0,location=0,status=1,resizable=0");
	win.focus();
  	win.moveTo(wleft, wtop);
}

function launch2(accountid, charid, warn)
{
	var e = document.getElementById("t");
	var t = e.value;
	
	var width = Math.max(800, window.screen.availWidth);
	var height = Math.max(600, window.screen.availHeight);

	var wleft = (screen.width -  width) / 2;
  	var wtop = (screen.height - height) / 2;
  		
	t = encodeURIComponent(t);
	
	var small = "0";
	if ((width < 801) || (height < 601)) small = "1";
	
	var win = window.open("http://web.sifakaworld.com/world2.php?t="+t+"&a="+accountid+"&c="+charid+"&w="+warn+"&s="+small,"SifakaWorld","width="+width+",height="+height+",toolbar=0,location=0,status=1,resizable=0");
	document.location.replace("http://world.sifakaworld.com/launched.html");
	win.focus();
  	
  	win.moveTo(wleft, wtop);
}

function launchdirect()
{	
	var width = Math.max(800, window.screen.availWidth);
	var height = Math.max(600, window.screen.availHeight);

	var wleft = (screen.width -  width) / 2;
  	var wtop = (screen.height - height) / 2;
	
	var small = "0";
	if ((width < 801) || (height < 601)) small = "1";
	
	var win = window.open("http://web.sifakaworld.com/world3.php?s="+small,"SifakaWorld","width="+width+",height="+height+",scrollbars=1,toolbar=0,location=0,status=1,resizable=0");
	win.focus();
  	
  	win.moveTo(wleft, wtop);
}

