


function sendFormModifStructure(numero_gite,validation){
    var tab = genereTabFromForm($('div_onglets'),-1);
	var adr = "ajax/modifInfos.php";
    
	var sending="numero_gite="+numero_gite+"&tab="+Object.toJSON(tab);
	if(validation=='O') sending+="&validation=O";
	appelAjaxToFunc(adr,sending,function(reponseAjax){
		alertAMalibuAvecActionRetour(reponseAjax,"document.location.href=\"attente_modif_infos.php?numero_gite="+numero_gite+"\";");
	});
}


function sendModifStructureEtValider(numero_gite){
	alertAMalibuAvecSauvetave("Etes-vous sûr de valider les données saisies? Ensuite, vous ne pourrez les modifier avant validation de votre relais.","sendFormModifStructure(\""+numero_gite+"\",\"O\");");
}

function ajoutPiece(obj){
	var divAInserer=obj.nextSibling;
	var position = obj.nextSibling.lastChild.getAttribute("position");
	var indice = position;
	position++;

	//ajout de la nouvelle piece dans tous les cartouches
	$$(".liste_ancres").each(function(elt){
		if(elt.firstChild.hasClassName("elem_ancres_produit_piece")){
			var elementLi = new Element('li', {'class':'elem_ancres elem_ancres_produit_piece'});
			var chaine2 = new String(elt.firstChild.innerHTML);
			chaine2 = str_replace(chaine2,'Pièce 1','Pièce '+position);
			chaine2 = str_replace(chaine2,'ancre_produit_piece_0','ancre_produit_piece_'+indice);
			elementLi.innerHTML = chaine2;
			elt.insert(elementLi);
		}
	});
	
	var id_div="div_produit_piece_"+position;
	var elementDiv = new Element('div', {'class':'div_produit_piece div_level_1 ','id':id_div,'position':position});

	elementDiv.innerHTML=$("div_produit_piece_1").innerHTML;
	elementDiv.innerHTML=str_replace(elementDiv.innerHTML,'node_titre="Pièce 1','node_titre="Pièce '+position);

	//on met a jour tous les id a 0
	elementDiv.descendants().each(function(elt){
		if(elt.getAttribute('id')) elt.setAttribute('node_id','0');
	});
	//on met a jour le numero de la piece
	elementDiv.childNodes[1].innerHTML=str_replace(elementDiv.childNodes[1].innerHTML,'Pièce 1','Pièce '+position);

	//on met en display block le bouton supprimer
	elementDiv.lastChild.style.display="block";

	//on met actif la piece courante dans le cartouche
	var objLi=elementDiv.firstChild;
	for(var i=0; i < objLi.childNodes.length; i++) {
		var chaine_a_comparer="ancre_produit_piece_"+indice;
		if(objLi.childNodes[i].firstChild.getAttribute("name")==chaine_a_comparer){
			elementDiv.firstChild.childNodes[i].addClassName("elem_selected");
			elementDiv.firstChild.childNodes[i].firstChild.setAttribute("name",chaine_a_comparer);
		}
		else {
			elementDiv.firstChild.childNodes[i].firstChild.setAttribute("name","");
			if (elementDiv.firstChild.childNodes[i].hasClassName("elem_selected")) 
				elementDiv.firstChild.childNodes[i].removeClassName("elem_selected");
		}
	}

	$(divAInserer).insert(elementDiv);	

	//on met a jour le name dans le lien afin que le scroll fonctionne
	$$(".elem_ancres_produit_piece").each(function(elt){
		if(elt.hasClassName("elem_selected")){
			elt.firstChild.setAttribute('id',elt.firstChild.getAttribute("name"));
		}
		else {
			elt.firstChild.setAttribute("name","");
			elt.firstChild.setAttribute('id','');
		}
	});
	Effect.ScrollTo("ancre_produit_piece_"+indice,{"offset":-15});
}

/*
function str_replace(a, b, str) {
    return str_replace2(str, a, b);
}
function str_replace2(SRs, SRt, SRu) {
  
  **  Replace a token in a string
  **    s  string to be processed
  **    t  token to be found and removed
  **    u  token to be inserted
  **  returns new String
  
  SRRi = SRs.indexOf(SRt);
  SRRr = '';
  if (SRRi == -1) return SRs;
  SRRr += SRs.substring(0,SRRi) + SRu;
  if ( SRRi + SRt.length < SRs.length)
    SRRr += str_replace2(SRs.substring(SRRi + SRt.length, SRs.length), SRt, SRu);
  return SRRr;
}
*/

function supprimePiece(obj){
	var position = obj.parentNode.getAttribute("position");
	var indice=position-1;
	$$(".elem_ancres_produit_piece").each(function(elt){
		var re1 = new RegExp("ancre_produit_piece_"+indice,"g");
		if(re1.exec(elt.firstChild.getAttribute('onclick'))){
			elt.remove();
		}
	});
	obj.parentNode.remove();
}

function genereTabFromForm(elem){
   
   var tabnode = new Array();
   var radio_inputs = new Array();var radio_inputs_okvalue = new Array();//pour les input radio
   elem.childElements().each(function(child){
       if(child.nodeName == "DIV"){
           if (child.hasClassName("parent_node")){
                var child_tab = genereTabFromForm(child);
                var node_name = ""+child.getAttribute("node_name");
                var node_id = ""+child.getAttribute("node_id");
				var node_titre = ""+child.getAttribute("node_titre");
				
			    tabnode.push({"node_childs" : child_tab , "node_name" : node_name, "node_id" : node_id, "node_titre" : node_titre});
           }
           else {
               var child_tab = genereTabFromForm(child);
               if(child_tab.length>0)
                    tabnode.push(child_tab);
           }
       }
       else if( child.nodeName == "INPUT" || child.nodeName == "SELECT" || child.nodeName == "TEXTAREA" ){
            var name = child.getAttribute("name");
            var node_id = child.getAttribute("node_id");
            var value = child.getValue();
			var node_type = ""+child.getAttribute("node_type");
			var node_titre = ""+child.getAttribute("node_titre");
			var node_enum = ""+child.getAttribute("node_enum");
			var node_enum_val = ""+child.getAttribute("node_enum_val");
			var node_taille = ""+child.getAttribute("node_taille");

            if(child.getAttribute("type")=="radio"){// un peu special pour le bouton radio car il y a deux input
                if( radio_inputs.indexOf(name) >= 0 ){
                    if( ! (radio_inputs_okvalue.indexOf(name) >= 0 )){
                        tabnode.push({"value" : value , "value_name" : name, "node_id" : node_id, "node_type" : node_type, "node_titre" : node_titre, "node_enum" : node_enum, "node_enum_val" : node_enum_val, "node_taille" : node_taille});
                    }
                }
                else {
                    radio_inputs.push(name);
                    if(value) {
                        radio_inputs_okvalue.push(name);
                        tabnode.push({"value" : value , "value_name" : name, "node_id" : node_id, "node_type" : node_type, "node_titre" : node_titre, "node_enum" : node_enum, "node_enum_val" : node_enum_val, "node_taille" : node_taille});
                    }
                }  
            }
            else
                tabnode.push({"value" : value , "value_name" : name, "node_id" : node_id, "node_type" : node_type, "node_titre" : node_titre, "node_enum" : node_enum, "node_enum_val" : node_enum_val, "node_taille" : node_taille});
       }
   });
   return tabnode;
}


 function checkOngletsVisites(idDiv,funcQdFini,numero_gite){
	obj=$(idDiv);
	var tabChkInput=getTabChmpVisites(obj);
	var nbVerifAFaire = 0;
	nbChampVerifie = 0;

	for(var i = 0; i < tabChkInput.length; i++){
    	if(tabChkInput[i].getAttribute("modecheck")) nbVerifAFaire++;
	}

	for(var i = 0; i < tabChkInput.length; i++){
    	var chpInputControl = null;
    	var chpControle = tabChkInput[i].getAttribute("chpcontrol");
    	if(chpControle){
    		chpInputControl = $(chpControle);
    		if(!chpInputControl) chpInputControl = chpControle;
    	}

    	var modechk = tabChkInput[i].getAttribute("modecheck")
    	if(modechk){
    		checkByInputVisites(tabChkInput[i],modechk,chpInputControl,funcQdFini,nbVerifAFaire,numero_gite);
    	}
	}
}


function checkByInputVisites(chpInput,mode,chpInputControl,funcQdFini,nbVerifAFaire,numero_gite){
    if (!chpInput)return;
    if (!mode)return;


    var adr="/lib/ajax/checkForm.php";
    var xhr_local=creeObjHttpRequest();
    var sending="mode="+mode+"&value="+chpInput.value;

    if (chpInputControl){
    	chpInput.chpInputControl=chpInputControl;
    	if(chpInputControl.tagName=="INPUT") sending+="&valueControle="+chpInputControl.value;
    	else sending+="&valueControle="+chpInputControl;
    }

    chpInput.modecheck=mode;
    chpInput.disabled=true;

	xhr_local.onreadystatechange = function(){
		if(xhr_local.readyState == 4 && xhr_local.status == 200){
			var reponse = clean(xhr_local.responseXML.documentElement);
			var retour=reponse.childNodes[0].childNodes[0].nodeValue;
			chpInput.retourVerif=retour;
			chpInput.disabled=false;
			if (retour!="OK" ){
				if(chpInput.type=="password") chpInput.type="text";
				$(chpInput).addClassName("chpInptErreurDetecte");
				$(chpInput).removeClassName("chpInptAucuneErreur");
				chpInput.value = reponse.childNodes[1].childNodes[0].nodeValue;
				if($("div_AttenteVerifClient")) setHTMLById("div_AttenteVerifClient","");
				if($("a_validationCoordonnees") && $("a_validationCoordonnees").hasClassName("validationCoordonnees_enCours")) $("a_validationCoordonnees").removeClassName("validationCoordonnees_enCours");
				if (window.onErrorOnCheckInput)window.onErrorOnCheckInput(chpInput);

			}else{
				$(chpInput).addClassName("chpInptAucuneErreur");
				if(funcQdFini && nbVerifAFaire){
					nbChampVerifie++;
					if(nbVerifAFaire==nbChampVerifie) {
						funcQdFini(numero_gite);
					}
				}
			}
        }
    }
    xhr_local.open("POST",adr,true);
    xhr_local.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    xhr_local.send(sending);
}

function getTabChmpVisites(obj){
	var childs=obj.childNodes;

	var tabOut=new Array();
	var tabFils=new Array();

	for (var i=0;i<childs.length;i++){
		var child=childs[i];

		if (	(child.tagName=="INPUT" && child.type=="text")
			||	(child.tagName=="TEXTAREA")
			||	(child.tagName=="SELECT")
		){
			tabOut.push(child);
		}
		if ( child.childNodes.length>0 ){
			tabFils.merge(getTabChmpVisites(child));
		}
	}
	if (tabFils.length>0)tabOut.merge(tabFils);

	return tabOut;
}


