function processForm(formIdent)
{
	var invalid_elements = new Array();
  	var form, elements, i, j, elm; 
  	form = document.getElementById 
    ? document.getElementById(formIdent) 
    : document.forms[formIdent]; 

	document.getElementById("error_response").innerHTML = "";	
	
	elements = form.elements;
	j = 0;
	for( i=0, elm; elm=elements[i++]; )
	{

		if ((elm.type == "text" || elm.type == "password") 
			&& elm.name.substring(0, 7) != "address"
			&& elm.name.substring(0, 3) != "fax" 
			&& elm.name.substring(0, 11) != "register_id"
			&& elm.name.substring(0, 6) != "tax_id")
		{
			if (elm.value == '')
			{
				invalid_elements[j++] = elm;
			}
			else
			{
				setBorder(elm, "#AFAFAF", 1);
			}
		}
		else if (elm.type == "select-one" 
				 &&  elm.name.substring(0, 7) != "address"
				 && elm.name.substring(0, 11) != "countrycode")
		{
			
			if ((elm.selectedIndex == 0) || (elm.selectedIndex == 1))
			{
				invalid_elements[j++] = elm;
			}
			else
			{
				setBorder(elm, "#AFAFAF", 1);
			}
		}

	}
	

	if (invalid_elements.length < 1){
		check = checkSyntax(form);	
		check1 = get_address_data(form);
	}

	if ((invalid_elements.length < 1) && check && check1)
	{
		form.submit();
	}
	else
	{
		for (x in invalid_elements)
		{
			setBorder(invalid_elements[x], "red", 1);
		}
		document.getElementById("error_response").innerHTML = "<img src='image/warning_bubble/error.png' style='position:relative;top:3px;left:0px;'>&nbsp;Veuillez corriger les champs encadr&eacute;s en rouge.";
	}
}

function checkSyntax(form)
{
	mail = form.email;
	oldpwd = form.password;
	newpwd = form.conf_password;
	oldmail = form.newemail;
	newmail = form.confirmemail;
	tel = form.tel;
	fax = form.fax;
	company_tel = form.company_tel;
	countrycode = form.countrycode;
	tin = form.tax_id;
	countrycode = form.countrycode;
	register_id = form.register_id;
	name_first = form.name_first;
	name_last = form.name_last;


  	var tmp_x = false, tmp_m = '';
	if (mail)
	{
		tmp_x = true;
		tmp_m = mail.value;
	}
	
	var tmp_x1 = false, tmp_m1 = '';
	if (newmail)
	{
		tmp_x1 = true;
		tmp_m1 = newmail.value;
	}

	if (name_first)
	{
		if (!CheckName(name_first.value) && form.name_first)
		{
			showWarning('name_first', 'Votre Prénom ne peut pas contenir de chiffres');
			setBorder(name_first, "#E3886B", 0);
			return false;
		}
	}

	if (name_last)
	{
		if (!CheckName(name_last.value) && form.name_last)
		{
			showWarning('name_last', 'Votre Nom ne peut pas contenir de chiffres');
			setBorder(name_last, "#E3886B", 0);
			return false;
		}
	}

	if (oldmail && newmail)
	{
		if (!mail_check(oldmail.value, newmail.value) && form.newemail)
		{
			showWarning('confirmemail', "Votre adresse email ne correspond pas à votre email d'inscription. Veuillez essayer à nouveau.");
			setBorder(newmail, "#E3886B", 0);
			return false;
		}
		if (!is_email(tmp_m1) && tmp_x1)
		{
			showWarning('newemail', 'Votre adresse email devrait être de la forme aaa@bbb.ccc');
			setBorder(oldmail, "#E3886B", 0);
			return false;
		}
	}

	if (!is_email(tmp_m) && tmp_x)
	{
		showWarning('email', 'Votre adresse email devrait être de la forme aaa@bbb.ccc');
		setBorder(mail, "#E3886B", 0);
		return false;
	}
	if(oldpwd){
		if (!validLogin(oldpwd) && form.password)
		{
			showWarning('password', 'Mot de passe invalide (Au moins 5 caractères [a-z], [A-Z] et/ou [0-9])');
			setBorder(oldpwd, "#E3886B", 0);
			return false;
		}		
	}
	
	if (oldpwd && newpwd)
	{
		if (!pass_check(oldpwd.value, newpwd.value) && form.password)
		{
			showWarning('conf_password', 'Votre mot de passe de confirmation ne correspond pas. Veuillez essayer à nouveau.');
			setBorder(newpwd, "#E3886B", 0);
			return false;
		}
		if (!validLogin(oldpwd) && form.password)
		{
			showWarning('password', 'Mot de passe invalide (Au moins 5 caractères [a-z], [A-Z] et/ou [0-9])');
			setBorder(oldpwd, "#E3886B", 0);
			return false;
		}
	}
	if(tel){	
		if(tel.value.substring(0, 2) != "00"){
			if(!Phonecheck(tel.value)){
				showWarning('tel', 'Veuillez entrer un numéro de téléphone valide (Pas d\'espace entre les chiffres.)');
				setBorder(tel, "#E3886B", 0);
				return false;
			}
		}
	}
	if(fax && fax.value != ""){	
		if(fax.value.substring(0, 2) != "00"){
			if(!Phonecheck(fax.value)){
				showWarning('company_fax', 'Veuillez entrer un numéro de fax valide');
				setBorder(fax, "#E3886B", 0);
				return false;
			}
		}
	}
	if(company_tel && company_tel.value != 0){	
		if(company_tel.value.substring(0, 2) != "00"){
			if(!Phonecheck(company_tel.value)){
				showWarning('company_tel', 'Veuillez entrer un numéro de téléphone valide');
				setBorder(company_tel, "#E3886B", 0);
				return false;
			}
		}
	}
	
	if (tin)
	{
		if (tin.value == '' && (countrycode.selectedIndex != 0 && countrycode.selectedIndex != 1 && countrycode.options[countrycode.selectedIndex].value != "FR"))
		{
			showWarning('tax_id', 'Veuillez remplir les champs nécessaires pour votre numéro de TVA intra-communautaire');
			setBorder(tin, "#E3886B", 0);
			return false;
		}	
				
		if (tin.value != '' && (countrycode.selectedIndex == 0 || countrycode.selectedIndex == 1))
		{
			showWarning('tax_id', 'Veuillez indiquer votre code pays pour votre numéro de TVA intra-communautaire');
			setBorder(tin, "#E3886B", 0);
			return false;
		}

		if (tin.value != ''){
			if (!TINcheck(tin.value) && form.tax_id && countrycode.options[countrycode.selectedIndex].value == "FR")
			{
				showWarning('tax_id', 'Votre numéro de TVA intra-communautaire devrait être de la forme XX-XXXXXXXXX. Veuillez essayer à nouveau.');
				setBorder(tin, "#E3886B", 0);
				return false;
			}
		}
		
		if (tin.value == ''){
			countrycode.selectedIndex = 0;
		}
		
	}
	
	if (register_id && register_id.value != "")
	{
		if (!isRegister_id(register_id.value) && form.register_id)
		{
			showWarning('register_id', 'Votre code client doit etre au format XXXX');
			setBorder(register_id, "#E3886B", 0);
			return false;
		}
	}
	
	/*if(!get_address_data())
		return false;*/
	
	return true;
}

function get_address_data(form){
	var address = new Array();
	
	for (var cpt=1; cpt < 5;cpt++)
		address[cpt] = new Array(); 
		
	var i, test_address; 
	
	if(form.address1_type){
	
		for (i=1; i < 5;i++){

			address[i]["type"] = document.getElementById("address"+i+"_type");
			address[i]["name"] = document.getElementById("address"+i+"_name");
			address[i]["line1"] = document.getElementById("address"+i+"_line1");
			address[i]["zip"] = document.getElementById("address"+i+"_zip");
			address[i]["city"] = document.getElementById("address"+i+"_city");
			address[i]["country_invoice"] = document.getElementById("address"+i+"_country_invoice");
			
			if (address[i]["name"].value == '' && address[i]["line1"].value == '' &&
					 address[i]["zip"].value == '' && address[i]["city"].value == '' && (address[i]["type"].selectedIndex == 0 || address[i]["type"].selectedIndex == 1)
					 && (address[i]["country_invoice"].style.display == 'block' && (address[i]["country_invoice"].selectedIndex == 0 || address[i]["country_invoice"].selectedIndex == 1))){	
						
	
			}
			else if (address[i]["name"].value != '' && address[i]["line1"].value != '' &&
					 address[i]["zip"].value != '' &&
								address[i]["city"].value != '' &&
									(address[i]["type"].selectedIndex != 0 && address[i]["type"].selectedIndex !=  1)
					&& (address[i]["country_invoice"].style.display == 'block' && address[i]["country_invoice"].selectedIndex != 0 && address[i]["country_invoice"].selectedIndex != 1) || address[i]["country_invoice"].style.display == 'none'){
				
				if(address[i]["zip"] != '' && address[i]["country_invoice"].style.display == 'none' && !isZip(address[i]["zip"].value)){
					showWarning('address'+i+'_zip', 'Veuillez entrer un code postal valide');
					setBorder(address[i]["zip"], "#E3886B", 0);
					return false;
				}

			}
			else{
				if (address[i]["type"])
					setBorder(address[i]["type"], "red", 1);
					
				if (address[i]["name"].value == '')
					setBorder(address[i]["name"], "red", 1);
				else
					setBorder(address[i]["type"], "#AFAFAF", 1);
					
				if (address[i]["line1"].value == '')
					setBorder(address[i]["line1"], "red", 1);
				else
					setBorder(address[i]["line1"], "#AFAFAF", 1);
										
				if (address[i]["zip"].value == '')
					setBorder(address[i]["zip"], "red", 1);
				else
					setBorder(address[i]["zip"], "#AFAFAF", 1);
															
				if (address[i]["city"].value == '')
					setBorder(address[i]["city"], "red", 1);
				else
					setBorder(address[i]["city"], "#AFAFAF", 1);
															
				if (address[i]["country_invoice"].style.display == 'block' && (address[i]["country_invoice"].selectedIndex == 0 || address[i]["country_invoice"].selectedIndex == 1))
					setBorder(address[i]["country_invoice"], "red", 1);
				else
					setBorder(address[i]["country_invoice"], "#AFAFAF", 1);
														
				return false;
			} 
		}
	}
	return true;
	
}

function is_email(str)
{
	if (!str)
		return false;

	var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/;
	var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;

	if (!(!reg1.test(str) && reg2.test(str)))
		return false;

	return true;
}

function validLogin(password)
{
	reg = /^[a-zA-Z0-9]+$/;

	if (!reg.test(password.value)  || (password.value.length <= 4))
		return false;
	return true;
}

function pass_check(oldpwd, newpwd)
{
	return (oldpwd == newpwd);
}

function mail_check(oldmail, newmail)
{
	return (oldmail == newmail);
}

function isZip(zip){
	reg = /^[0-9]{5}$/;
	return reg.test(zip);	
}

function isRegister_id(register_id){
	reg = /^[0-9]{4}$/;
	return reg.test(register_id);	
}

function CheckName(name){
	reg =/^[0-9]+$/;
	return !reg.test(name);
}

function TINcheck(tin)
{
	reg = /^[0-9]{2}-[0-9]{9}$/;
	return reg.test(tin);
}

function Phonecheck(tel){
	reg = /^[0]{1}[0-9]{9}$/;
	reg1 = /^[+]{1}[0-9]$/;
	reg2 = /^[00]{2}[0-9]$/;
	
	if (!reg1.test(tel) && !reg2.test(tel) && !reg.test(tel))
		return false;

	return true;
}

function setBorder(elm, color, allBorders)
{
	/*if (elm.id == "company_type")
	{
		elm = document.getElementById("company_type_label");
		//elm.style.color = color;
	}*/
	if (elm && elm.style)
	{
		elmStyle = elm.style;
		if (allBorders == 1)
		{
			elmStyle.border = "1px solid " + color;
			if ((elm.id == "company_type") || (elm.id == "title"))
				elmStyle.background = color;
		}
		else
		{
			elmStyle.border = "1px solid " + color;
			if ((elm.id == "company_type") || (elm.id == "title"))
				elmStyle.background = color;
		}
	}
}

function showWarning(posId, warning)
{
	posImg = document.getElementById 
		? document.getElementById(posId) 
		: document.all[posId];
	
	warnStyle = document.getElementById 
		? document.getElementById('popup_warning').style 
		: document.all['popup_warning'].style;
	
	warnText = document.getElementById 
		? document.getElementById('popup_warning_content')
		: document.all['popup_warning_content'];
	position = get_pos(posImg);
	
	warnText.innerHTML = warning;
	//warnStyle.position = "absolute";
	//warnStyle.zIndex = "99";
	warnStyle.left = position[0] + 250 + "px";
	warnStyle.top = position[1] + "px";
	warnStyle.visibility = "visible";
}

function hideWarning()
{
	warnStyle = document.getElementById 
		? document.getElementById('popup_warning').style 
		: document.all['popup_warning'].style;
	
	warnStyle.visibility = "hidden";
}