var xmlhttp;
var can_go = false;

function trim(x)
{
	x = x.replace(/^\s+/,'');
	x = x.replace(/\s+$/,'');
	return x;
}

function isEmail(x)
{
	var atpos = x.indexOf("@");
	var dotpos = x.lastIndexOf(".");
	var spacepos = x.indexOf(" ");
	if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= x.length || spacepos > -1)
	{
		return false;
	}
	else
	{
		var illegalChars = /[\(\)\<\>\,\;\:\\\"\[\]]/
		if (x.match(illegalChars))
		{
			return false;
		}
		else
		{
			return true;
		}
	}
}


function showCatch2()
{
	$("#catch1").animate({ left: "-1000px" });
	$("#catch1").hide();
	$("#catch2").show();
	$("#catch2").animate({ left: 0 });
}

function showCatch1()
{
	$("#catch2").animate({ left: "1000px" });
	$("#catch2").hide();
	$("#catch1").show();
	$("#catch1").animate({ left: 0 });
}


function clearName()
{
	if (document.join.fName.value.length > 0)
	{
		$("#itextnamewrap").css('background-image', 'url(img/itextbg.png)');
	}
	else
	{
		$("#itextnamewrap").css('background-image', 'url(img/namebg.png)');
	}
}


function clearUsername()
{
	if (document.join.fUsername.value.length > 0)
	{
		$("#itextusernamewrap").css('background-image', 'url(img/itextbg.png)');
	}
	else
	{
		$("#itextusernamewrap").css('background-image', 'url(img/usernamebg.png)');
	}
}


function clearEmail()
{
	if (document.join.fEmail.value.length > 0)
	{
		$("#itextemailwrap").css('background-image', 'url(img/itextbg.png)');
	}
	else
	{
		$("#itextemailwrap").css('background-image', 'url(img/emailbg.png)');
	}
}


function checkName(id)
{
	v = id.value;
	v = trim(v);
	id.value = v;

	if (v.indexOf(" ") < 1)
	{
		document.getElementById("im1").src = "img/n.png";
		document.join.f1.value = 0;
		return false;
	}
	else
	{
		document.getElementById("im1").src = "img/y.png";
		document.join.f1.value = 1;
		return true;
	}
}


function checkUsername(id)
{
	if (can_go) { return true; }

	id.value = trim(id.value);
	if (id.value.length < 1)
	{
		document.getElementById("im2").src = "img/n.png";
		document.join.f2.value = 0;
		return false;
	}

	var alphaExp = /^[0-9a-zA-Z\_\-\.]+$/;
	if (!id.value.match(alphaExp))
	{
		document.getElementById("im2").src = "img/n.png";
		document.join.f2.value = 0;

		alert("Username contains invalid characters");
		id.focus();
		return false;
	}

	if (id.value == "username")
	{
		document.getElementById("im2").src = "img/n.png";
		document.join.f2.value = 0;

		id.focus();
		return false;
	}

	var usr = id.value;
	var url = "chkm.php?id=" + usr;
	xmlhttp=GetXmlHttpObject();
	xmlhttp.open("GET",url,true);
	xmlhttp.onreadystatechange=chkmDONE;
	xmlhttp.send(null);	
}


function chkmDONE()
{
	if (xmlhttp.readyState==4)
	{
		var r = xmlhttp.responseText;
		if (r == "0")
		{
			document.getElementById("im2").src = "img/n.png";
			document.join.f2.value = 0;			
		}
		else
		{
			document.getElementById("im2").src = "img/y.png";
			document.join.f2.value = 1;
		}
	}		
}



function checkEmail(id)
{
	id.value = trim(id.value);
	if (id.value.length < 1)
	{
		document.getElementById("im3").src = "img/n.png";
		document.join.f3.value = 0;
		return false;
	}

	if (!isEmail(id.value))
	{
		document.getElementById("im3").src = "img/n.png";
		document.join.f3.value = 0;
		return false;	
	}	

	var email = id.value;
	var url = "chkme.php?e=" + email;
	xmlhttp=GetXmlHttpObject();
	xmlhttp.open("GET",url,true);
	xmlhttp.onreadystatechange=chkmeDONE;
	xmlhttp.send(null);	
}


function chkmeDONE()
{
	if (xmlhttp.readyState==4)
	{
		var r = xmlhttp.responseText;
		if (r == "0")
		{
			document.getElementById("im3").src = "img/n.png";
			document.join.f3.value = 0;			
		}
		else
		{
			document.getElementById("im3").src = "img/y.png";
			document.join.f3.value = 1;
		}
	}		
}


function checkJoin(id)
{
	if (id.f1.value == 1 && id.f2.value == 1 && id.f3.value == 1)
	{
		return true;
	}
	else
	{
		return false;
	}
}


function clearLogin1(c)
{
	if (document.login.fUsername.value.length > 0)
	{
		$("#l_user_wrap").css('background-image', 'url(img/loginbg.png)');
	}
	else
	{
		$("#l_user_wrap").css('background-image', 'url(img/loginuserbg.png)');
	}
	if (c == 1) { document.getElementById("oops").innerHTML = ""; }
}


function clearLogin2(c)
{
	if (document.login.fPassword.value.length > 0)
	{
		$("#l_pwd_wrap").css('background-image', 'url(img/loginbg.png)');
	}
	else
	{
		$("#l_pwd_wrap").css('background-image', 'url(img/loginpwdbg.png)');
	}
	if (c == 1) { document.getElementById("oops").innerHTML = ""; }
}


function checkLogin(id)
{
	

	id.fUsername.value = trim(id.fUsername.value);
        var spacepos = id.fUsername.value.indexOf(" ");
	id.fPassword.value = trim(id.fPassword.value);

	if (id.fUsername.value.length < 1 || spacepos > -1)
	{
		document.getElementById("oops").innerHTML = '<img src="img/oopslogin.gif" />';
		return false;
	}

	var u = id.fUsername.value;
	var p = id.fPassword.value;
	var url = "chklgn.php?u=" + u + "&p=" + p;
	xmlhttp=GetXmlHttpObject();
	xmlhttp.open("GET",url,true);
	xmlhttp.onreadystatechange=chklgnDONE;
	xmlhttp.send(null);

	return false;
}


function chklgnDONE()
{
	if (xmlhttp.readyState==4)
	{
		var r = xmlhttp.responseText;
		if (r == "0")
		{
			document.getElementById("oops").innerHTML = '<img src="img/oopslogin.gif" />';	
		}
		else
		{
			can_go = true;
			document.login.submit();
		}
	}		
}


function checkRestore(id){

	id.fValue.value = trim(id.fValue.value);

	if (id.fValue.value.length < 1)
	{
		alert("Enter your Username or Email Address associated with your account.");
		id.fValue.focus();
		return false;
	}
}


function checkContactForm(id){
	id.fName.value = trim(id.fName.value);
	id.fEmail.value = trim(id.fEmail.value);
	id.fMsg.value = trim(id.fMsg.value);

	if (id.fName.value.length < 3)
	{
		alert("Please enter Your Name");
		id.fName.focus();
		return false;
	}

	if (id.fEmail.value.length == 0)
	{
		alert("Invalid Email Address");
		id.fEmail.focus();
		return false;
	}

	if (!id.fEmail.value.length == 0)
	{
		if (!isEmail(id.fEmail.value))
		{
			alert("Invalid Email Address");
			id.fEmail.focus();
			return false;
		}
	}

	if (id.fMsg.value.length < 10)
	{
		alert("Please type your message.");
		id.fMsg.focus();
		return false;
	}
}


function clearContactMsg()
{
	document.getElementById("contact_sent").innerHTML = "";
}


function GetXmlHttpObject()
{
	if (window.XMLHttpRequest)
	{
		// code for IE7+, Firefox, Chrome, Opera, Safari
		return new XMLHttpRequest();
	}
	if (window.ActiveXObject)
	{
		// code for IE6, IE5
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}

