var siteUrl = 'http://www.reduisonsnosdechets.fr';

//*********************************//
// fonctions contrainte des champs//
//*********************************//
function $(elt){
if(document.getElementById(elt).id && document.getElementById(elt)) return document.getElementById(elt); else alert("objet Inexistant : "+elt);
}

function chiffresDate(event) {
	// Compatibilité IE / Firefox
	if(!event&&window.event) {
	event=window.event;
	}

	// IE
	if(event.keyCode < 47 || event.keyCode > 57) {
	event.returnValue = false;
	event.cancelBubble = true;
	}
	// DOM
	if(event.which < 47 || event.which > 57) {
	event.preventDefault();
	event.stopPropagation();
	}
}

function chiffresTel(event) {
	// Compatibilité IE / Firefox
	if(!event&&window.event) {
	event=window.event;
	}

	// IE
	if((event.keyCode < 48 || event.keyCode > 57) && event.keyCode!=8 && event.keyCode!=9 && event.keyCode!=0) {
	event.returnValue = false;
	event.cancelBubble = true;
	}
	// DOM
	if((event.which < 48 || event.which > 57) && event.which!=8 && event.which!=9 && event.which!=0) {
	event.preventDefault();
	event.stopPropagation();
	}
}

function VerificationEmail(elm)
{
	var reg = new RegExp('^[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*@[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*[\.]{1}[a-z]{2,6}$', 'i');
	if (reg.test(elm.value)) {
		return true;
	}
	return(false);
}

function limit600car(text){
if(text.value.length>length) text.value=text.value.substr(0,600);
}
//***********************************//
// FIN fonction contrainte des champs//
//***********************************//

//***********************************//
//VALIDATION DES FORMULAIRES//
//***********************************//
function validateInscriptionStep1(){
	stop=0;
	var fieldsTxtLst = new Array("user_structurename","user_structuretype_id","user_lastname","user_firstname","user_function","address_nb","address_street1","address_zipcode","address_city","user_email","user_phone","address_typestreet_id","address_region_id");

	//verification des champs non remplis
	stop = colorAndCheck(fieldsTxtLst);
	
	//on verifie le format du mail
	if( $("user_email").value && !VerificationEmail($("user_email")) ) {
		$("err_user_email").style.color="red";
		stop=1;
	}else if($("user_email").value && VerificationEmail($("user_email"))){
		$("err_user_email").style.color="";
	}
	if(stop==0){
	//ici chaques champs est renseigné et correct on peut valider
	$("form_inscription").submit();
	}else{
	$("errgeneric").style.display="block";
	location="#ancerr";
	}
}

function checkOther(val){
if(val==46) $('spanother').style.display="block";
else {
$('spanother').style.display="none";
$('action_target_other').value="";
}
}

function validateInscriptionStep2(){
	stop=0;
	var fieldsTxtLst = new Array("action_name","action_type_id","action_datestart","action_dateend","action_city","action_place","action_region_id","action_country_id","action_target_id","action_shortdescription");

	//verification des champs non remplis
	stop = colorAndCheck(fieldsTxtLst);
	
	//verification que la date de debut est bien inferieur ou egal a la date de fin
	if(getValue("action_datestart") && getValue("action_dateend")){
		if(getValue("action_datestart")>getValue("action_dateend")){
			$("err_action_datestart").style.color="red";
			$("err_action_dateend").style.color="red";
			stop=1;
		}else{
			$("err_action_datestart").style.color="";
			$("err_action_dateend").style.color="";
		}
	}
	
	//themeaction
	if(!getStateCheckbox("themeaction")){
		$("err_themeaction").style.color="red";
		stop=1;
	}else{
		$("err_themeaction").style.color="";
	}
	
	//action_target_other
	if(getValue("action_target_id")=="46" && !getValue("action_target_other")){
		$("spanother").style.color="red";
		stop=1;
	}else{
		$("spanother").style.color="";
	}
	
	//action_optin
	/*if(!$("action_optin").checked) {
		$("err_action_optin").style.color="red";
		stop=1;
	}else{
		$("err_action_optin").style.color="";
	}*/
	
	if(stop==0){
		//ici chaques champs est renseigné et correct on peut valider
		$("form_inscription").submit();
	}else{
		$("errgeneric").style.display="block";
		location="#ancerr";
	}
}

function validateInscriptionLogin(){
	stop=0;
	var fieldsTxtLst = new Array("identifiant","mdp");

	//verification des champs non remplis
	stop = colorAndCheck(fieldsTxtLst);
	
	//on verifie le format du mail
	if( $("identifiant").value && !VerificationEmail($("identifiant")) ) {
		$("err_identifiant").style.color="red";
		stop=1;
	}else if($("identifiant").value && VerificationEmail($("identifiant"))){
		$("err_identifiant").style.color="";
	}
	
	if(stop==0){
		//ici chaques champs est renseigné et correct on peut valider
		$("form_inscription").submit();
	}else{
		$("errgeneric").style.display="block";
	}
}

function validateCommandeStep1(){
	//on verifie qu'au moins un article est commandé ...
	//de product_0 a product N
	ok=0;
	i=0;

	loop=true;
	while(loop){
		if($("product_"+i).value) ok=1
		i++;
		if(!document.getElementById("product_"+i)) loop=false;
	}

	if(ok==1){
	$("form_inscription").submit();
	}else{
	$("errgeneric").style.display="block";
	location="#ancerr";
	}
}

function validateCommandeStep2(){
	stop=0;
	var fieldsTxtLst = new Array("address_structure1","address_lastname","address_firstname","address_nb","address_typestreet_id","address_street1","address_zipcode","address_city","address_email","address_phone");

	//verification des champs non remplis
	stop = colorAndCheck(fieldsTxtLst);
	
	//on verifie le format du mail
	if( $("address_email").value && !VerificationEmail($("address_email")) ) {
		$("err_address_email").style.color="red";
		stop=1;
	}else if($("address_email").value && VerificationEmail($("address_email"))){
		$("err_address_email").style.color="";
	}
	
	if(stop==0){
		//ici chaques champs est renseigné et correct on peut valider
		$("form_inscription").submit();
	}else{
		$("errgeneric").style.display="block";
		location="#ancerr";
	}
}

function validateReminder(){
	stop=0;
	var fieldsTxtLst = new Array("identifiant");
	
	
	
	//verification des champs non remplis
	stop = colorAndCheck(fieldsTxtLst);
	
	//on verifie le format du mail
	if( $("identifiant").value && !VerificationEmail($("identifiant")) ) {
		$("err_identifiant").style.color="red";
		stop=1;
	}else if($("identifiant").value && VerificationEmail($("identifiant"))){
		$("err_identifiant").style.color="";
	}
	
	if(stop==0){
		//ici chaques champs est renseigné et correct on peut valider
		$("form_inscription").submit();
	}else{
		if(document.getElementById("msgERR1")) $("msgERR1").style.display="none";
		$("errgeneric").style.display="block";
		
	}
}
	

function colorAndCheck(fieldsTxtLst){
	//champs text et listes
	for(var i =0;i<fieldsTxtLst.length;i++){
	
			if(!getValue(fieldsTxtLst[i])){
				$("err_"+fieldsTxtLst[i]).style.color="red";
				stop=1;
			}else{
				$("err_"+fieldsTxtLst[i]).style.color="";
			}
	}
	
	return stop;

}
//***********************************//
//FIN VALIDATION DES FORMULAIRES//
//***********************************//

//***********************************//
//IMPRESSION D'UNE ZONE a améliorer ...
//***********************************//
function imprime_zone(titre, obj) {
	// Définie la zone à imprimer
	var zi = document.getElementById(obj).innerHTML;
	
	var reg = new RegExp('(src="../images/)', "g");
	zi = zi.replace( reg, 'src="'+siteUrl+'/images/' );

	// Ouvre une nouvelle fenetre
	var f = window.open("", "ZoneImpr", "height=500, width=600,toolbar=0, menubar=0, scrollbars=1, resizable=1,status=0, location=0, left=10, top=10");

	// Définit le Style de la page
	f.document.body.style.color = '#000000';
	f.document.body.style.backgroundColor = '#FFFFFF';
	f.document.body.style.padding = "10px";

	// Ajoute les Données
	f.document.title = titre;
	f.document.body.innerHTML += " " + zi + " ";

	// Imprime et ferme la fenetre
	f.window.print();
	//f.window.close();
	return true;
}
//***********************************//
//FIN IMPRESSION D'UNE ZONE
//***********************************//

//***************************************//
// fonctions de recuperation de contenus //
//***************************************//
function getValue(obj){

tmp = $(obj);
if(tmp.type=="select-one") return tmp[tmp.selectedIndex].value;
else if(tmp.type=="text") return tmp.value;
else if(tmp.type=="password") return tmp.value;
else if(tmp.type=="textarea") return tmp.value;
else if(tmp.type=="hidden") return tmp.value;

else return "";

}

function getStateCheckbox(obj){
	sortie=false;
	i=0;
	loop=true;
	while(loop){
		if($(obj+i).checked) sortie=true
		i++;
		if(!document.getElementById(obj+i)) loop=false;
	}
	return sortie;
}
//***************************************//
//fin fonctions de récupération de contenus//
//***************************************//












//***************************************//
//ANCIENNE VERSION DE FORMULAIRE //
//***************************************//
function verifForm(){
	var fieldTxt = new Array("nom_structure","prenom","nom","fonction","numero","type_rue","adresse1","cp","ville","email","tel","site_web","nom_operation","lieu_operation_nom","lieu_operation_ville","description_action");
	var fieldLst = new Array("type_structure","region","type_action","debut_action","fin_action","lieu_operation_region","lieu_operation_pays","public");
	var fieldChk = new Array("chk1","chk2","chk3","chk4","chk5");
	var stop=0;
	//verification des champs obligatoire Txt
	for(var i =0;i<fieldTxt.length;i++){
			if(!document.getElementById(fieldTxt[i]).value){
				document.getElementById("msg_"+fieldTxt[i]).style.color="red";
				stop=1;
			}else{
				document.getElementById("msg_"+fieldTxt[i]).style.color="#000";
			}
	}
	//verification des champs obligatoire Lst
	for(var i =0;i<fieldLst.length;i++){
			if(!document.getElementById(fieldLst[i]).options[document.getElementById(fieldLst[i]).selectedIndex].value){
				document.getElementById("msg_"+fieldLst[i]).style.color="red";
				stop=1;
			}else{
				document.getElementById("msg_"+fieldLst[i]).style.color="#000";
			}
	}

	//verification des checkBoxs
	count=0;
	for(var i =0;i<fieldChk.length;i++){
		if(document.getElementById(fieldChk[i]).checked==true){
			count++;
		}
	}
	if(count==0) { document.getElementById("msg_theme_action").style.color="red"; stop=1; }
	else { document.getElementById("msg_theme_action").style.color="#000"; }
	
	
	//Cas particulier : Autres suivi_selection et public
	if(document.getElementById("suivi_selection").options[document.getElementById("suivi_selection").selectedIndex].value && document.getElementById("suivi_selection").options[document.getElementById("suivi_selection").selectedIndex].value=="Autres" && !document.getElementById("suivi_selection_autre").value ) {
		document.getElementById("msg_suivi_selection_autre").style.color = "red";
		stop=1;
	}
	if(document.getElementById("public").options[document.getElementById("public").selectedIndex].value && document.getElementById("public").options[document.getElementById("public").selectedIndex].value=="Autres" && !document.getElementById("public_autre").value ) {
		document.getElementById("msg_public_autre").style.color = "red";
		stop=1;
	}

	if(stop==1){
		document.getElementById("msgglobal").innerHTML = "<br>Merci de remplir tout les champs obligatoires, ceux oubli&eacute;s sont en rouge.";
		document.getElementById("msgglobal").style.display="block";
		location = "#top";
	}else{
		//Cas particulier : email
		if(!VerificationEmail(document.getElementById("email"))){
			document.getElementById("msgglobal").innerHTML = "<br>L'adresse email que vous avez saisi semble invalide.";
			document.getElementById("msg_email").style.color = "red";
			location = "#aemail";
		}else{
			document.forms['monForm'].submit();
		}
	}

}

function checkBoxs(){
var fieldChk = new Array("chk1","chk2","chk3","chk4","chk5");

	if(document.getElementById("theme_action_autre").checked==true){
		for(var i =0;i<fieldChk.length;i++){
			document.getElementById(fieldChk[i]).checked=true;
		}
	}else{
	for(var i =0;i<fieldChk.length;i++){
			document.getElementById(fieldChk[i]).checked=false;
		}
	
	}
}

function affiche_autre(objet,mdiv,field){
	if(objet.options[objet.selectedIndex].value=="Autres"){
		document.getElementById(mdiv).style.display="block";
	}else{
		document.getElementById(mdiv).style.display="none";
		document.getElementById(field).value="";
	}
}
//***************************************//
//FIN ANCIENNE VERSION DE FORMULAIRE //
//***************************************//
