function initXHR()
{
		xhr_object = null;
   		if (window.ActiveXObject) {
			try { 
				xhr_object = new ActiveXObject("Msxml2.XMLHTTP"); 
			} 
			catch (e) { 
				try { 
					xhr_object = new ActiveXObject("Microsoft.XMLHTTP"); 
				}
				catch (e) { 
					return null;
				} 
    	   	} 
        }	
		else if (window.XMLHttpRequest) {
			xhr_object = new XMLHttpRequest();
			if (xhr_object.overrideMimeType) { xhr_object.overrideMimeType("text/xml"); }
 		} 
		return xhr_object;
}

function init()
{
	for(i=0; i < document.getElementsByTagName("input").length;i++) {
			document.getElementsByTagName("input")[i].autocomplete = 'off'; 
			document.getElementsByTagName("input")[i].onfocus = inputFocus;
			document.getElementsByTagName("input")[i].onblur = inputNoFocus;  
	}
	
	for(i=0; i < document.getElementsByTagName("textarea").length;i++) {
			document.getElementsByTagName("textarea")[i].onfocus = inputFocus;
			document.getElementsByTagName("textarea")[i].onblur = inputNoFocus;  
	}
	
	if (document.forms["inscription"] != null)
	{
		for(i=0; i < document.forms["inscription"].getElementsByTagName("input").length;i++) {
			document.forms["inscription"].getElementsByTagName("input")[i].onkeyup = check; 
		}
	}

}

function inputFocus(event) { this.style.backgroundColor = '#DDE2D3'; }
function inputNoFocus(event){ this.style.backgroundColor = ''; }

var oldResp = null;

function check(e) {
	value = this.id;
	text = this.value;
	xhr_object = initXHR();
	if (text.length == 0) {
		oldResp = null;
		document.getElementById("info" + value).innerHTML = '';
		return;
	}
	else {
		if (value == "email") {
			var reg = new RegExp("^[a-z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4}$");			
			if (reg.exec(text) == null) {
				valid(value,false,'')
				return;
			}
		}

		if(xhr_object && xhr_object.readyState != 0)
		{
			xhr_object.abort();
			delete xhr_object;
		}
		
		xhr_object = initXHR();
		xhr_object.open("POST", "./tools/inscription.php", true); 
		xhr_object.onreadystatechange = function() {  
		   if(xhr_object.readyState == 4 && xhr_object.status == 200) {
				var resp = xhr_object.responseText;
				if (value + resp != oldResp) {
					document.getElementById("info" + value).innerHTML = resp; 
					oldResp = value + resp;
				}
			}
		}  
		xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xhr_object.send(value + "="+document.getElementById(value).value+"&cas=verifieConformite");
	}
	return;
}

function valid(value,valid,message)
{
	var resp = (valid != "neutre" ? '<img src="' + (valid ? './pics/valid.gif' : './pics/invalid.gif') + '"/><i>&nbsp;' + message + '</i>' : '');
	if (value + resp != oldResp) {
		document.getElementById("info" + value).innerHTML = resp;
		oldResp = value + resp;
	}
}
