/* 
Web-Innova AJAX Register script
Copyright 2006 - Web-Innova, LLC
register.js
6/2006
*/

//  Function to check if username is already been taken
function checkDup(frm,fld) {
	var user;
	var msg;

	//  Get the username value from the register form
	//user = document.getElementById("username");
	user = window.document.forms[frm].elements[fld];
	//alert(user);

	//  Generate unique URL to solve IE caching issue
	url = "util/dups.php?username=" + user.value + "&key=" + Math.random() * Date.parse(new Date());
	//alert(url);

	//  Create AJAX object
	createXMLHttpRequest();
	
	//  Process AJAX request
	xmlHttp.onreadystatechange = handleDuplicate;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
	
	return true;
	
}

//  Function to handle the AJAX return message
function handleDuplicate() {
	if(xmlHttp.readyState == 4) {
		if(xmlHttp.status == 200) {
			if(xmlHttp.responseText == "true") {
				//  Display the message if username is already taken
				msg = "Username already in use.  Please select a different one.";
				document.register.username.focus();
				document.register.username.select();
			} else {
				//  Display no message if username is okay.
				msg = "";
			}
			//  Assign message to the proper area of the page
			document.getElementById("validate_un").innerHTML = msg;
		}
	}
}

//  Function to check if passwords match
function checkPasswords(frm) {
	var msg;
	//var pass = document.getElementById("password");
	var	pass = window.document.forms[frm].elements["password"];

	//var confirm_pass = document.getElementById("confirm_password");
	var	confirm_pass = window.document.forms[frm].elements["confirm_password"];
	
	if(pass.value != confirm_pass.value) {
		//  Display the message if passwords to not match
		msg = "Passwords do not match.  Please re-enter the passwords.";
		document.register.password.value = "";
		document.register.confirm_password.value = "";
		document.register.password.focus();
		//document.register.password.select();
	} else {
		//  Display no message if passwords are okay
		msg = "";
	}

	//  Assign message to the proper area of the page
	document.getElementById("validate_ps").innerHTML = msg;
}
