/**
*	Connectix Boards 0.8, free interactive php bulletin boards.
*	Copyright (C) 2005-2007  Jasienski Martin.
*
*	This program is free software; you can redistribute it and/or modify
*	it under the terms of the GNU General Public License as published by
*	the Free Software Foundation; either version 3 of the License, or
*	(at your option) any later version.
*
*	This program is distributed in the hope that it will be useful,
*	but WITHOUT ANY WARRANTY; without even the implied warranty of
*	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*	GNU General Public License for more details.
*
*	You can find a copy of the GNU General Public License at 
*	<http://www.connectix-boards.org/license.txt>.
*/

var ajax;
var ajaxLoaded = true;
if (window.XMLHttpRequest) 
	ajax = new XMLHttpRequest();
else if (window.ActiveXObject) 
	ajax = new ActiveXObject("Microsoft.XMLHTTP");
else
	ajaxLoaded = false;

/* Elements de formulaire pour l'inscription */
function regFormCheck (field,value) {
	if (!ajaxLoaded)
		return false;
	
	if (value.length == 0)
		return false;
	
	ajax.open('POST','ajax.php',true);
	
	ajax.onreadystatechange = function() {
		if (ajax.readyState == 4) {
			var node = document.createElement('span');
			node.className = 'reg_check';
			node.innerHTML = '<span class="reg_'+((ajax.responseText.charAt(0) == '1')?'valid':'error')+'"><span>'+ajax.responseText.substr(2)+'</span></span>';
			
			var container = document.getElementById(field);
			if (container.lastChild.className == 'reg_check')
				container.replaceChild(node,container.lastChild);
			else 
				container.appendChild(node);
		}
	}
	
	ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	ajax.send('request='+escape(field)+'&value='+escape(value));
}
/* Pour le mot de passe, c'est un peu différent, car il y a deux valeurs à checker, et deux champs de formulaire... */
function regFormPass () {
	if (!ajaxLoaded)
		return false;
	
	var pass1 = document.getElementById('field_pass1').value;
	var pass2 = document.getElementById('field_pass2').value;
	
	if (pass1.length == 0 || pass2.length == 0)
		return false;
	
	ajax.open('POST','ajax.php',true);
	
	ajax.onreadystatechange = function() {
		if (ajax.readyState == 4) {
			var node = document.createElement('span');
			node.className = 'reg_check';
			node.innerHTML = '<span class="reg_'+((ajax.responseText.charAt(0) == '1')?'valid':'error')+'"><span>'+ajax.responseText.substr(2)+'</span></span>';
			
			nodeClone = node.cloneNode(true);
			
			var container = document.getElementById('reg_password');
			if (container.lastChild.className == 'reg_check')
				container.replaceChild(node,container.lastChild);
			else 
				container.appendChild(node);
			
			var container = document.getElementById('reg_password_confirm');
			if (container.lastChild.className == 'reg_check')
				container.replaceChild(nodeClone,container.lastChild);
			else 
				container.appendChild(nodeClone);
		}
	}
	
	ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	ajax.send('request=reg_password&pass1='+escape(pass1)+'&pass2='+escape(pass2));
}