var fname = "";
var lname = "";
var email = "";
var cemail = "";
var pw = "";
var cpw = "";


function CheckSignup() {
	fname = document.getElementById("fname").value;
	lname = document.getElementById("lname").value;
	email = document.getElementById("email").value;
	cemail = document.getElementById("cemail").value;
	pw = document.getElementById("pw").value;
	cpw = document.getElementById("cpw").value;
	
	var err = "";
	var error_blank = "";
	var error_match_pw = "";
	var error_match_email = "";
	var error_email = "";
	
	if (email.indexOf("@")<1 || email.indexOf(".")<1) error_email = "Invalid Email. ";
	if (cemail=='') error_match_email = "Emails must match.  ";
	if (cemail.indexOf("@")<1 || cemail.indexOf(".")<1) error_email = "Invalid Email. ";
	if (email!=cemail) error_match_email = "Emails must match. ";
	if (pw!=cpw) error_match_pw = "Passwords must match. ";
	
	if (fname==""|| lname=="" || email=="" || cemail=="" || pw=="" || cpw=="") {
		error_blank = "All fields are required. "
		error_match_pw = "";
		error_match_email = "";
		error_email = "";
	}
	
	if (error_blank!="" ||error_match_pw!="" || error_match_email!="" || error_email!="" ) {
		document.getElementById("error").innerHTML = "<span class='small'><img src='images/iknowgo_icon_caution.gif' align='absbottom' border='0' alt='All fields are required' />&nbsp;" + error_blank+error_match_pw+error_match_email+error_email+"</span>";
		return false;
	} else {
		Signup();
		return false;
	}
	
}


function Signup() {
//alert('yes');
	var ajaxRequest;  // The variable that makes Ajax possible!
	var values = "";
	
		try{
			// Opera 8.0+, Firefox, Safari
			ajaxRequest = new XMLHttpRequest();
		} catch (e){
			// Internet Explorer Browsers
			try{
				ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try{
					ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e){
					// Something went wrong
					alert("Your browser broke!");
					return false;
				}
			}
		}
		
		// Create a function that will receive data sent from the server
		ajaxRequest.onreadystatechange = function(){
			if(ajaxRequest.readyState == 4){
			
				var ajaxDisplay = document.getElementById('signup_content');
				var theresponse = ajaxRequest.responseText;
				if (theresponse.indexOf("***crs2007***user_already_exists")>-1) {
					document.getElementById("email").value = "";
					document.getElementById("cemail").value = "";
					document.getElementById("pw").value = "";
					document.getElementById("cpw").value = "";
					document.getElementById("email").focus();
					document.getElementById("error").innerHTML = "<span class='small'><img src='images/iknowgo_icon_caution.gif' align='absbottom' border='0' alt='All fields are required' />&nbsp;email has already been registered</span>";
				} else {
					ajaxDisplay.innerHTML = theresponse;
				}
			}
		}
		
		if (fname=="" || lname=="" || email=="" || cemail=="" || cpw=="" || pw=="") {
			document.getElementById("error").innerHTML = "please fill in all fields";
			return;
		}
		var q = "?action=signup&fname="+fname+"&lname="+lname+"&email="+email+"&pw="+pw;
		//alert(q);
		ajaxRequest.open("GET", "includes/signup_complete.php" + q, true);
		ajaxRequest.send(null); 
	
}