/**
 * Fonction permettant la création d'un cookie en fonction d'un formulaire dynamique
 */
function SetCookie (name, TabCookie)
{
	  var str="";
	  for(var id in TabCookie){
	      str +="-" + id + "#" + TabCookie[id];
	  }
	  
	  str = str + ";";
	  
	  // nouvel objet date
	  var aujourdhui = new Date() ;
	   
	  // nouvel objet date
	  var expdate = new Date() ;
	   
	  // Date de fin du cookie : 1 an à partir de la date courante
	  expdate.setTime( aujourdhui.getTime() + ( 365*24*60*60*1000 ) )
	 
	  // Création du cookie
	  document.cookie = "CCSTMALO_"+ name +"=" + name + str +";path=/;expires=" + expdate.toGMTString() +";";
}

/* Variable contenant le cookie pour les formulaire */
var cookieValue = "";

/* Variable contenant le cookie pour l'annuaire concessionnaire */
var cookieValueAnnuaire = "";

/**
 * Fonction JS permettant le récupération d'un cookie Formulaire
 */
function GetCookie (name) {
    
    var prefix = name + '=';
    var c = document.cookie;
    
    var nullstring = '';
    var cookieStartIndex = c.indexOf(prefix);
    
    if (cookieStartIndex == -1)
        return nullstring;
    
    var cookieEndIndex = c.indexOf(";", cookieStartIndex + prefix.length);
    if (cookieEndIndex == -1)
        cookieEndIndex = c.length;
    
    cookieValue = unescape(c.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}


/**
 * Fonction JS permettant le récupération d'un cookie pour l'annuaire
 */
function GetCookieAnnuaire (name) {
    
    var prefix = name + '=';
    var c = document.cookie;
    
    var nullstring = '';
    var cookieStartIndex = c.indexOf(prefix);
    
    if (cookieStartIndex == -1)
        return nullstring;
    
    var cookieEndIndex = c.indexOf(";", cookieStartIndex + prefix.length);
    if (cookieEndIndex == -1)
        cookieEndIndex = c.length;
    
    cookieValueAnnuaire = unescape(c.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}


/**
 * Méthode permettant la récupération des données pour les champ de type text, area, combo.
 */
function GetCookieParam(formId, champId){
    
    var MonTabSplit = cookieValue.split("-");
    
    for(var i=1; i < MonTabSplit.length; i++){
        
        var tempParam = MonTabSplit[i];
        var tempParamPosition = tempParam.indexOf(champId+"#");
        
        if (tempParamPosition != -1){
            var monNewText = replaceAllBy('/','-', tempParam.substring((champId+'#').length) );
            document[formId][champId].value = monNewText;
        }
    }
}


/**
 * Méthode permettant la récupération des données pour un champ de type Radio
 */
function GetCookieRadioParam(formId, champId){
    
    var MonTabSplit = cookieValue.split("-");
    
    for(var i=1; i < MonTabSplit.length; i++){
        
        var tempParam = MonTabSplit[i];
        var tempParamPosition = tempParam.indexOf(champId+"#");
        
        if (tempParamPosition != -1){
            var monNewTextRadio = replaceAllBy('/','-', tempParam.substring((champId+'#').length) );
            document[formId][monNewTextRadio].checked = true;
        }
    }
}


/**
 * Méthode permettant la récupération des données pour un champ de type CheckBox
 */
function GetCookieCheckboxParam(formId, champId){
    
    var MonTabSplit = cookieValue.split("-");
    
    for(var i=1; i < MonTabSplit.length; i++){
        
        var tempParam = MonTabSplit[i];
        var tempParamPosition = tempParam.indexOf(champId+"#");
        
        if (tempParamPosition != -1){
            if(tempParam.substring((champId+'#').length) == '1'){
                document[formId][champId].checked = true;
            }
        }
    }
}


/**
 * Méthode permettant la récupération des données pour les champ de type date, car mofi sur le text.
 */
function GetCookieDateParam(formId, champId){
    
    var MonTabSplit = cookieValue.split("-");
    
    for(var i=1; i < MonTabSplit.length; i++){
        
        var tempParam = MonTabSplit[i];
        var tempParamPosition = tempParam.indexOf(champId+"#");
        
        if (tempParamPosition != -1)
						var maNewDat = replaceAllBy('/','-', tempParam.substring((champId+'#').length) );
            document[formId][champId].value = maNewDat;
    }
}


/**
 * Méthode permettant la récupération des données pour les annuaires.
 */
function GetCookieParamDealer(position){
    
    var MonTabSplit = cookieValueAnnuaire.split("-");
    var compteur = 0;
    
    for(var i=0; i < MonTabSplit.length; i++){

        if(compteur == position){

		        var tempParam = MonTabSplit[i];
		        var tempParamPosition = tempParam.indexOf("#");
		        
		        if (tempParamPosition != -1){
		            var monNewText = replaceAllBy('*','-', tempParam.substring(tempParamPosition+1));
		            
		            return monNewText;
		        }
        }
        compteur = compteur + 1;
    }
}


/**
 * Fonction permettant de remplacer un caratere par un autre 
 */
function replaceAllBy(sReplaceThis, sWithThis, sString) {
    
	if (sReplaceThis != "" && sReplaceThis != sWithThis) {
	    
	  var counter = 0;
	  var start = 0;
	  var before = "";
	  var after = "";
	  
	  while (counter<sString.length) {
	      
		  start = sString.indexOf(sReplaceThis, counter);
		  
		  if (start == -1) {
		      break;
		  } else {
			  before = sString.substr(0, start);
			  after = sString.substr(start + sReplaceThis.length, sString.length);
			  sString = before + sWithThis + after;
			  counter = before.length + sWithThis.length;
		  }
	  }
  }
  return sString;
}  

