function requiredWithDefault() {
	if(trim(this.field.value) && this.field.value != defaultVal[this.field.id]) {
		return true;	
	} else {
		return false;
	}
}

function restoreOnBlur() {
	//alert(this.id);
	if(!trim(this.value)) {
		this.value = defaultVal[this.id];
	}
}

function emptyOnFocus() {
	//alert(this.id);
	if(this.value == defaultVal[this.id]) {
		this.value = '';
	}
}

function handleFocus(fld) {
	//defaultVal[fld.id] = fld.value;
	fld.onfocus = emptyOnFocus;
	fld.onblur = restoreOnBlur;
}

function showErrorMsg(count, email) {
	if(count > 0) {
		setDisplay('cr_error_bloc', 'block');
		if(count == 1) {
			setDisplay('error_single', 'block');
		} else {
			setDisplay('error_mul', 'block');
		}
		
		if(email) {
			setDisplay('error_email', 'block');
		}
	}
	//setDisplay
}

function restoreErrorMsg() {
	setDisplay('cr_error_bloc', 'none');
	setDisplay('error_single', 'none');
	setDisplay('error_mul', 'none');
	setDisplay('error_email', 'none');
}

function myCheckFields(fldArray) {
	var is_valid	= true;
	var error_count	= 0;
	var error_email	= false;
	//alert(fldArray.length);	
	//return false;
	restoreErrorMsg();
	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;
				error_count = error_count + 1;
				if(fldArray[i].fld_id == 'cr_email') {
					error_email = true;
				}
				fldArray[i].showError();
			}
		}
	}
	
	//alert(is_valid + "\n" + error_count + "\n" + error_email + "\n")
	showErrorMsg(error_count, error_email);
	return is_valid;
}

function submitDataset() {
	if(myCheckFields(fList)) {
		// On désactive les champs
		disableFields(fList);
		var xhr = createXHR();
		if(xhr) {
			//alert('OK');
			xhr.onreadystatechange  = function() {
				if(xhr.readyState  == 4) {
					if(xhr.status  == 200)  {
						//alert('OK');
						var div = getEltByID('bloc_contact_rapide');
						div.innerHTML = xhr.responseText;
					} else {
						//alert('ERROR');
						setDisplay('http_error_contact_rapide', 'block');
					}
					enableFields(fList);
					//this.reset();
				}
			}
			
			var data	 = new String();
			for(i = 0 ; i < fList.length ; i++) {
				if(fList[i].get_value) {
					data	+= fList[i].fld_id + '=' + trim(fList[i].field.value) + '&';
				}
			}
			
			//data += "profession=" + escape(getProfession_form_newsletter());

			
			//alert(data);
			
			//xhr.open('GET', 'actions/newsletter.php?' + data,  true); 
            //xhr.send(null);
            xhr.open('POST', '/consumer_quickcontact/form_validation',  true); 
			xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			xhr.send(data);
            
		}
	}
	return false;
}

var i	= 0;
fList[i]			= new ajaxField('cr_nom');
fList[i].validate	= requiredWithDefault;
fList[i].enableChecking();
handleFocus(fList[i].field);
i = i + 1;

fList[i]			= new ajaxField('cr_prenom');
fList[i].validate	= requiredWithDefault;
fList[i].enableChecking();
handleFocus(fList[i].field);
i = i + 1;

fList[i]			= new ajaxField('cr_adresse');
fList[i].validate	= requiredWithDefault;
fList[i].enableChecking();
handleFocus(fList[i].field);
i = i + 1;

fList[i]			= new ajaxField('cr_cp');
fList[i].validate	= requiredWithDefault;
fList[i].enableChecking();
handleFocus(fList[i].field);
i = i + 1;

fList[i]			= new ajaxField('cr_ville');
fList[i].validate	= requiredWithDefault;
fList[i].enableChecking();
handleFocus(fList[i].field);
i = i + 1;

fList[i]			= new ajaxField('cr_telephone');
fList[i].validate	= requiredWithDefault;
fList[i].enableChecking();
handleFocus(fList[i].field);
i = i + 1;

fList[i]			= new ajaxField('cr_email');
fList[i].validate	= fieldEmail;
fList[i].enableChecking();
handleFocus(fList[i].field);
i = i + 1;

fList[i]			= new ajaxField('cr_message');
//fList[i].validate	= fieldEmail;
//fList[i].enableChecking();
i = i + 1;

fList[i]			= new ajaxField('cr_send');
fList[i].get_value 	= false;


var form_cr = getEltByID('contact_rapide');
if(form_cr != null) {
	form_cr.onsubmit = submitDataset;
}
//*/