function sendContact(){
	
	var form = document.forms['contact'];
	
	var name						= form['name'];
	var email						= form['email'];
	var type						= form['type'];
	var private_pratice_type 		= form['private_pratice_type'];
	var private_pratice_speciality	= form['private_pratice_speciality'];
	
	if(name.value=="" || !isMail(email.value) || !isSelected(type,'') || (isSelected(type,'private_pratice') && ( !isSelected(private_pratice_type,'') || (isSelected(private_pratice_type,'speciality') && private_pratice_speciality.value==""))) ){		
		
		msg="Veuillez remplir tous les champs précédés d'un astérisque.";
		SendFailed(msg);
		
	}else{
		return true;
	}
	
	return false;

}


jQuery(document).ready(function(){
	jQuery("#submit").click(function(){
									 
		var createsendForm = document.getElementById('formContact');
		
		if(sendContact()){
			
			var form = document.forms['contact'];
			
			var type						= form['type'];
			var private_pratice_type 		= form['private_pratice_type'];
			var private_pratice_speciality	= form['private_pratice_speciality'];
			var name						= form['name'];
			var helloHealthinterrest		= document.getElementById("helloHealthHub").checked==true ? "1" :"0";
			var mycainterrest		        = document.getElementById("mycaHub").checked==true ? "1" :"0";
			
			var other			= form['type_other'];
			var nom_sf			= form['00N400000029rTg'];	// nom 
			var type_sf			= form['00N400000029rjF'];	// Type of organisation : Primary ou Speciality
			var speciality_sf	= form['00N400000029rjK'];	// Type of organisation : Texte de scpeciality
			var other_sf		= form['00N400000029rjP'];	// Type of organisation : Other 
			
			nom_sf.value	= name.value;
			
			for(var a=0; a<type.length; a++){
				if(type[a].checked){
					type_sf.value	= type[a].value.replace('_',' ');	
				}	
			};
			
			if(type_sf.value!=""){
				type_sf.value	= ucfirst(type_sf.value);
				
				for(var a=0; a<private_pratice_type.length; a++){
					if(private_pratice_type[a].checked){
						type_sf.value	+= " : "+private_pratice_type[a].value;	
					}	
				}
			}
			
			speciality_sf.value	= private_pratice_speciality.value;
			other_sf.value	= other.value;
			
			postwith(
				 "https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8",
				 {
					 oid: "00D400000008sas",
					 retURL :"http://",
					 "00N40000001vryn": "1",
					 "00N400000029rTg": nom_sf.value,
					 title: jQuery("#title").val(),
					 company: jQuery("#company").val(),
					 street: jQuery("#street").val(),
					 city: jQuery("#city").val(),
					 state: jQuery("#state").val(),
					 zip: jQuery("#zip").val(),
					 email: jQuery("#email").val(),
					 phone: jQuery("#phone").val(),
					 "00N400000029rjF": type_sf.value,
					 "00N400000029rjK": speciality_sf.value,
					 "00N400000029rjP": other_sf.value,
					 "00N400000029rUo": helloHealthinterrest,
					 "00N400000029rT8": mycainterrest,
					 "00N40000001fuKf": jQuery("#comments").val()
				},
				"salesForceSubmit"
			);
			
			SendOk(createsendForm);
		}
	});
});

function postwith (to,p,target){
	var myForm = document.createElement("form");
	
	myForm.method="post" ;
	myForm.action = to ;
	myForm.target = target;
	
	for (var k in p){
		var myInput = document.createElement("input") ;
		myInput.setAttribute("type", "hidden") ;
		myInput.setAttribute("name", k) ;
		myInput.setAttribute("value", p[k]);
		myForm.appendChild(myInput);
	}
	document.body.appendChild(myForm);
	myForm.submit();
	document.body.removeChild(myForm);
	
}


function SendOk(form){
	
	form.style.display					= "none";
	
	var formContactMsgError 			= document.getElementById('formContactMsgError');
	formContactMsgError.style.display	= "none";
	
	var formContactMsgOk				= document.getElementById('formContactMsgOk');
	formContactMsgOk.innerHTML			= 'Merci pour votre intérêt.';
	formContactMsgOk.style.display		= "block";
}

function SendFailed(msg){
	var formContactMsgError =	document.getElementById('formContactMsgError');
	
	formContactMsgError.innerHTML = msg;
	formContactMsgError.style.display="block";

}

function isMail(email){
	
	var reg=RegExp("^([a-zA-Z0-9_-])+([.]?[a-zA-Z0-9_-]{1,})*@([a-zA-Z0-9-_]{2,}[.])+[a-zA-Z]{2,7}$");
	if(!reg.test(email))
		return false;
	else
		return true;
	
} 

function isSelected(input,choosen_value){
	for(var a=0; a<input.length; a++){
		if(choosen_value==""){
			if(input[a].checked){
				return true;
			}
		}else{
			if(input[a].value == choosen_value && input[a].checked){
				return true;
			}
		}
		
	}
	return false;
}

function resetType(){
	var form	= document.forms['contact'];
	var type	= form['type'];
	
	for(var a=0; a<type.length; a++){
		if(type[a].checked){
			if(a!=0){
				resetPrivatePractice(form);
				
			}else if(a!=5){
				resetTypeOther(form);
			}
		}
	}
	
}

function resetRadio(radio){
	for(var a=0; a<radio.length; a++){
		radio[a].checked=false;	
	}
}

function resetPrivatePractice(form){
	var private_pratice_type = form['private_pratice_type'];
	resetRadio(private_pratice_type);
	
	var private_pratice_speciality	= form['private_pratice_speciality'];
	private_pratice_speciality.value="";
}

function resetTypeOther(form){
	var type_other = form['type_other'];
	type_other.value='';
}

function resetPrivatePracticeRadio(){
	var form	= document.forms['contact'];
	var type	= form['type'];
	type[0].checked=true;
	resetTypeOther(form);

	var private_pratice_speciality	= form['private_pratice_speciality'];
	private_pratice_speciality.value="";
}

function resetPrivatePracticeSpeciality(){
	var form	= document.forms['contact'];
	var private_pratice_type = form['private_pratice_type'];
	resetRadio(private_pratice_type);
	private_pratice_type[1].checked=true;
	
	var type	= form['type'];
	type[0].checked=true;
	resetTypeOther(form);
}

function resetTypeOtherText(){
	var form	= document.forms['contact'];
	var type	= form['type'];
	type[5].checked=true;
	resetType();
}
function ucfirst (str) {
    str += '';
    var f = str.charAt(0).toUpperCase();
    return f + str.substr(1);
}

