// JavaScript Document
function createXHR() {
	var request = false;
	try {
		request = new ActiveXObject('Msxml2.XMLHTTP');
	}
	
	catch (err2) {
		try {
			request = new ActiveXObject('Microsoft.XMLHTTP');
		}
		
		catch (err3) {
			try {
				request = new XMLHttpRequest();
			}
			
			catch (err1) {
				request = false;
			}
		}
	}
    return request;
}


ajaxField = function(fld_id) {
	this.getEltByID = function(id) {
	    if (document.getElementById)
    	    return document.getElementById(id);
	    else if (document.all)
    	    return document.all[id];
	    else if (document.layers)
    	    return document.layers[id];
		else
		    return null;
	}
	
	this.getOrgCSS = function() {
		if(this.field != null) {
			this.fld_css = this.getCSS(this.field);
		}
		
		if(this.msg != null) {
			this.msg_css = this.getCSS(this.msg);
		}
	}
	
	this.getCSS = function(object) {
		if(object.getAttribute('class') != null) {
			return object.getAttribute('class');
		} else if(object.getAttribute('className') != null) {
			return object.getAttribute('className');
		} else {
			return '';
		}
	}
	
	this.getVars = function() {
		this.field	= this.getEltByID(this.fld_id);
		this.msg 	= this.getEltByID(this.error_prefix + this.fld_id);	
	}
	
	this.enableChecking = function() {
		this.getOrgCSS();
		this.check_field = true;
	}
	
	this.setCSS = function(object, css) {
		if(navigator.appName != 'Microsoft Internet Explorer') {
			object.setAttribute('class', css);
		}
		object.setAttribute('className', css);
	}
	
	this.showError = function() {
		if(this.check_field) {
			if(this.field != null && this.error_css) {
				this.setCSS(this.field, (this.fld_css ? this.fld_css + ' ' + this.error_css : this.error_css));
			}
		
			if(this.msg != null && this.show_msg_css) {
				this.setCSS(this.msg, this.show_msg_css);
			}
		}
	}
	
	this.restoreError = function() {
		if(this.check_field) {
			if(this.field != null) {
				this.setCSS(this.field, this.fld_css);
			}
		
			if(this.msg) {
				this.setCSS(this.msg,  this.msg_css);
			}
		}
	}
	
	this.enable = function() {
		if(this.field != null) {
			this.field.disabled = false;
		}
	}
	
	this.disable = function() {
		if(this.field != null) {
			this.field.disabled = true;
		}
	}
	
	this.dumpObject = function() {
		var dump	= new String();
		dump += 'error_prefix : ' + this.error_prefix + "\n";
		dump += 'error_css : ' + this.error_css + "\n";
		dump += 'show_msg_css : ' + this.show_msg_css + "\n";
		//dump += 'form_id : ' + this.form_id + "\n";
		dump += 'fld_id : ' + this.fld_id + "\n";
		//dump += 'form : ' + this.form + "\n";
		dump += 'field : ' + this.field + "\n";
		dump += 'msg : ' + this.msg + "\n";
		dump += 'fld_css : ' + this.fld_css + "\n";
		dump += 'msg_css : ' + this.msg_css + "\n";
		//dump += 'error_css : ' + this.error_css + "\n";
		dump += 'check_field : ' + this.check_field + "\n";
		dump += 'valid : ' + this.valid + "\n";
		alert(dump);
	}
	
	this.error_prefix	= 'error_';
	this.error_css		= 'fld_error';
	this.show_msg_css	= 'msg_show';
	this.fld_id 		= fld_id;
	this.field	 		= null;
	this.msg	 		= null;
	this.fld_css		= '';
	this.msg_css		= '';
	this.check_field	= false;
	this.valid			= false;
	this.get_value		= true;
	this.getVars();
}

function getEltByID(id) {
    if (document.getElementById)
   	    return document.getElementById(id);
    else if (document.all)
   	    return document.all[id];
    else if (document.layers)
   	    return document.layers[id];
	else
	    return null;
}

function setDisplay(div_id, d) {
	var div = getEltByID(div_id);
	if(div != null) {
		div.style.display = d;
	}
}

function getRadioValue(radio) {
	for (i = 0 ; i < radio.length ; i++) {
		if (radio[i].checked) {
			return radio[i].value;
		}
	}
}

function checkFields(fldArray) {
	var is_valid	= true;
	//alert(fldArray.length);	
	//return false;
	for(i = 0 ; i < fldArray.length ; i++) {
		fldArray[i].restoreError();
		if(fldArray[i].check_field && fldArray[i].validate) {
			if(!fldArray[i].validate()) {
				is_valid = false;
				fldArray[i].showError();
			}
		}
	}
	return is_valid;
}

function disableFields(fldArray) {
	for(i = 0 ; i < fldArray.length ; i++) {
		fldArray[i].disable();
	}	
}

function enableFields(fldArray) {
	for(i = 0 ; i < fldArray.length ; i++) {
		fldArray[i].enable();
	}	
}


// Fonction trim
function trim(myString) {
	return myString.replace(/^\s+/g,'').replace(/\s+$/g,'');
}

// Fonctions de test pour validation des objets
function fieldRequired() {
	/*
	var RegEx = /^\S+$/;
	if(RegEx.exec(this.field.value) != null)
		return true;
	else
		return false;*/
	if(trim(this.field.value)) {
		return true;	
	} else {
		return false;
	}
}

function fieldEmail() {
	var RegEx = /^([A-Za-z0-9_\-\.]+)@([A-Za-z0-9_\-]+)(\.{1})([A-Za-z0-9_\-\.]+)$/;	
	if(RegEx.exec(trim(this.field.value)) != null)
		return true;
	else
		return false;
}
