	
	function ajaxinit() {
		var xmlhttp=false;
		if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
			xmlhttp = new XMLHttpRequest();
		}
		return xmlhttp;
	}
	
	function available(username){
		var xmlhttp = ajaxinit();
		xmlhttp.open('GET', '/checkuser/username/'+username+'/', false);   //true
		xmlhttp.send(null);  
		
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			return (xmlhttp.responseText == "1") ? true : false;
		}
	}
	
	function validate_async(form) {
	  errors = {};
	  console.log(form);
	  try{
		  var username_available = available(form.username);
		  if (!username_available) {
		    errors.username = "This username is taken.";
		  }
		  if (!form.agree) {
		    errors.agree = "You must agree to the Terms of Service";
		  }
		  if (!form.password) {
		    errors.password = "You must create a password";
		  }
		  
		  var dt = new Date(), expiryTime = dt.setTime( dt.getTime() + 1000*5 );
		  document.cookie = 'password='+form.password+';expires='+dt.toGMTString();
		}
		catch(e){
			console.log(e.message);
		}
	  return errors;
	}
