var XMLHttpRequestObject = false; 
var result;

window.onload = initialForm;
window.onunload = function () {};


function initialForm(){
	
	// Build XMLHttpRequestObject
	try{
		// Opera 8.0+, Firefox, Safari
		XMLHttpRequestObject = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			XMLHttpRequestObject = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	
	
	document.getElementById ("press").onclick = validForm;
	
}

function validForm(){
	// Set to default every time we hit the button
	document.getElementById ("username").className = "reqd1";
	document.getElementById ("password").className = "reqd2"
	document.getElementById ("password2").className = "reqd2"
	document.getElementById ("terms").className = "reg";
	
	
	// if return false the form wouldn't be submitted
	// return false;
	
	// Chceck the required fields
	
	var username = document.getElementById ("username").value;

	username = removeSpaces(username);
	if (username == ""){
		document.getElementById ("username").className = "invalid";
		document.getElementById ("username").focus();
		document.getElementById ("Message").innerHTML = "Missing a required field.";
		return false;		
	} // end if
	
	if (username.length < 6){
		document.getElementById ("username").className = "invalid";
		document.getElementById ("username").focus();
		document.getElementById ("Message").innerHTML = "Please enter 6 or more characters.";
		return false;		
	} // end if
	
	var password = document.getElementById ("password").value;
	password = removeSpaces(password);
	if (password == ""){
		document.getElementById ("password").className = "invalid";
		document.getElementById ("password").focus();
		document.getElementById ("Message").innerHTML = "Missing a required field.";
		return false;		
	} // end if
	
	if (password.length < 6){
		document.getElementById ("password").className = "invalid";
		document.getElementById ("password").focus();
		document.getElementById ("Message").innerHTML = "Please enter 6 or more characters.";
		return false;		
	} // end if
	
	var password2 = document.getElementById ("password2").value;
	password2 = removeSpaces(password2);
	if (password2 == ""){
		document.getElementById ("password2").className = "invalid";
		document.getElementById ("password2").focus();
		document.getElementById ("Message").innerHTML = "Missing a required field.";
		return false;		
	} // end if
	
	if ( password != password2){
		document.getElementById ("password2").className = "invalid";
		document.getElementById ("password2").focus();
		document.getElementById ("Message").innerHTML = "Passwords do not match.";
		return false;		
	} // end if
	
	var aCheck = document.getElementById ("terms");
	if ( !aCheck.checked){
		document.getElementById ("terms").className = "invalid";
		document.getElementById ("password2").focus();
		document.getElementById ("Message").innerHTML = "You have to read and agree with the terms and conditions.";
		return false;		
	} // end if
	
	
	//Ajax to check the username if exists
	var aParam = "username=" + username	
	
	if(XMLHttpRequestObject) {
		
		
		var obj = document.getElementById('Message'); 
        var url = "username.php";
        
        XMLHttpRequestObject.open("POST", url, true); 
          XMLHttpRequestObject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
          

          XMLHttpRequestObject.onreadystatechange = function() 
          { 
            if (XMLHttpRequestObject.readyState == 4 && 
              XMLHttpRequestObject.status == 200) { 
              	
              	result = XMLHttpRequestObject.responseText;
              	
              	if (result != ""){
              		
                	obj.innerHTML = result;
                	document.getElementById ("username").className = "invalid";
					document.getElementById ("username").focus();
					
              	} else {
              		document.getElementById ("MyForm").submit();
              		
              	} // end if

            } 
          } 

          XMLHttpRequestObject.send(aParam); 
	} // if for the XMLHttpRequestObject
	
}


function removeSpaces(string) {
	var tstring = "";
	string = '' + string;
	splitstring = string.split(" ");
	for(i = 0; i < splitstring.length; i++)
	tstring += splitstring[i];
	return tstring;
}


