var xmlHttp = null;
function SendCreateUserRequest() {
	if(xmlHttp != null) {
		//return;
	}
	
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			// check for error codes
			
			var xmlDoc=xmlHttp.responseXML.documentElement;
			
			//alert('xmlDoc: ' + xmlDoc);
			
			var errorCode = xmlDoc.attributes['errorcode'].value;
			if(errorCode > 0) {
				if (errorCode >= 10 && errorCode <= 12) {
					
				} else {
					SetErrorMessage(decodeURI(xmlDoc.attributes['errormessage'].value));
				}
				return;
			}
			
			//alert();
			
			document.getElementById("inputWishlist").value = xmlDoc.getElementsByTagName('embedcode')[0].childNodes[0].nodeValue;
			
			document.getElementById("signup").className = "invisible";
			document.getElementById("embedcode").className = "";
		}
	}
	
	// check email
	var userEmail = document.getElementById("inputUserName").value;
	if(! CheckEmail(userEmail)) {
		SetErrorMessage("Please enter a valid email address.");
		return;
	}
	
	// check password
	var password = document.getElementById("inputPassword").value;
	var confirmpw = document.getElementById("inputConfirmPassword").value;
	
	if(password != confirmpw) {
		SetErrorMessage("Your passwords do not match.");
		return;
	}
	
	if(password.length < 5) {
		SetErrorMessage("Your password must be at least 5 characters long.");
		return;
	}
	
	SetErrorMessage("");
	
	var params = "email=" + escape(userEmail) + "&password=" + escape(password);
	
	xmlHttp.open("POST","php/create.php",true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
    xmlHttp.send(params);
}

function CheckEmail(email) {
	// check for spaces in the email
	if(email.indexOf(" ") != -1)
	{
		return false;
	}
	
	var charPos = email.indexOf("@");
	var charLastPos = email.lastIndexOf("@");
	// @ symbol check, also makes sure there is at least 1 character in front of the @
	if(charPos <= 0 || charPos != charLastPos)
	{
		return false;
	}
	
	var domain = email.slice(charPos + 1, email.length);
	charPos = domain.indexOf(".");
	charLastPos = domain.lastIndexOf(".");
	
	// only allow 2 .'s in the domain (ie site.co.kr)
	if(domain.split(".").length > 3)
	{
		return false;
	}
	
	// domain requires 1 char before . and two after (a.us)
	if(charPos < 1 || domain.length - charLastPos < 3)
	{
		return false;
	}
	
	return true;
}

function SetErrorMessage(error) {
	document.getElementById("errorText").innerHTML = error;
	document.getElementById("inputCard").className = "inputCardError";
}
function ShowProblem(error) {
	document.getElementById("apology").innerHTML = error;
	document.getElementById("signup").className = "invisible";
	document.getElementById("problem").className = "";
}
