/*----------------------------------------------------------------Business objects--------------------------------------------------------------------*/
/*******************Explicit method's name's *******************/
function count_substring(haystack, needle,offset,length){
    haystack += '';
    needle += '';
    var cnt=0;
    if (isNaN(offset)) {offset = 0;}
    if (isNaN(length)) {length = 0;}
    offset--; 
    while ((offset = haystack.indexOf(needle, offset+1)) != -1){
        if (length > 0 && (offset+needle.length) > length){
            return false;
        } else{            cnt++;
        }
    }
    return cnt;
}
/******************Idem***************************************/
function getBody(content) 
{ 
   var bodyStart = content.indexOf("<body");
   bodyStart = content.indexOf(">", bodyStart);    
   var bodyEnd = content.lastIndexOf("</body>"); 
   return content.slice(bodyStart + 1, bodyEnd);
}
/********************Idem^^*********************************/
function decode_utf8( s )
{
  return decodeURIComponent( escape( s ) );
}
/*****************Function used to create an url which could'nt be set in cache*******************/
function uniqueUrl(sep) 
{ 
  var d = new Date(); 
  var trick = d.getYear() + "ie" + d.getMonth() + "t" + d.getDate() + "r" + d.getHours() + "i" + d.getMinutes() + "c" + d.getSeconds() + "k" + d.getMilliseconds();   
  if (sep != "?") 
  { 
    sep = "&"; 
  }     
return sep + "noCache=" + trick; 
}
/******************Class used to parse and create an array will be containt strings in function of parameters limits***************/
function mise_en_forme(contenuAanalyser,nbreElemPremiereSequence,nbreSequences,nbreMiniParSequence){
  /*/!\contenuAanalyser represent the text must be parsed to return the array,
    /!\nbreElemPremiereSequence represent the number of parseds sub-string in the first row of the array,
    /!\nbreSequences represent the number of parsed substring in all others row,
    /!\nbreMiniParSequence is the most little number of sub-string could be accept in the other rows
  */
  /*++++++++++++++++++++++privates members++++++++++++++++++++++++++*/
  var nbre_slash_n = count_substring(contenuAanalyser,"\n");
  var nbreElements = 0;
  /*++++++++++++++++++++++privates methods++++++++++++++++++++++++++*/
  var nbreLignes=0;
  function set_nombre_lignes(){
                    if((nbre_slash_n-nbreElemPremiereSequence)<0){
                        nbreElements=1;
                        nbreElemPremiereSequence=nbre_slash_n+1;
                        return 1;
                    }
                    else if((nbre_slash_n-nbreElemPremiereSequence)<nbreMiniParSequence){
                        nbreElements = Math.ceil(nbre_slash_n -nbreElemPremiereSequence); 
                        return 2;
                    }
                    else if(((nbre_slash_n-nbreElemPremiereSequence)/nbreSequences)<nbreMiniParSequence){
                        var i=0;
                        while((i*nbreMiniParSequence)<(nbre_slash_n-nbreElemPremiereSequence)){
                            i++;
                        }
                        nbreElements = Math.ceil((nbre_slash_n-nbreElemPremiereSequence)/nbreSequences);
                        return i+1;
                    }else if(((nbre_slash_n-nbreElemPremiereSequence)/nbreSequences)>nbreMiniParSequence){
                        nbreElements = Math.ceil((nbre_slash_n-nbreElemPremiereSequence)/(nbreSequences-1));
                        return nbreSequences;
                    }
                    else{
                        nbreElements=1;
                        return 1;
                    }
                  }

  nbreLignes=set_nombre_lignes();
/*++++++++++++++++++++++publics methods++++++++++++++++++++++++++*/
 //[objectName].get_tableauChaines() returns the array which containt parsed string
  this.get_tableauChaines = function(){
                        var i=0;
                        var count = 0;
                        var tableau=new Array(nbreLignes);
                        var limite=nbreElemPremiereSequence;
                        var limiteInferieur=0;
                        var tableauTransit = contenuAanalyser.split("\n");
                        for(i=0;i<nbreLignes;i++){
                          if(i>0){
                            limiteInferieur=limite;
                            limite=nbreElemPremiereSequence+(i*nbreElements);
                          }
                          tableau[i]="";
                          for(count=limiteInferieur;count<limite;count++){
                             tableau[i]=tableau[i]+tableauTransit[count]+"\n";
                          }
                          //alert("tableau["+i+"]=>"+tableau[i]+",limiteInferieur="+limiteInferieur+",limite=>"+limite);
                        }
                        return tableau;
    }
  }
  /*___________________________________________________________________End of Class mise_en_forme______________________________________________________________________*/
var xhr_post=function(data,url){
            this._xhr_object = window.XMLHttpRequest?  new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
            this._xhr_object.open('POST', url+uniqueUrl('?'), true);
            var that = this;
            this._xhr_object.onreadystatechange = function() {
                if(that._xhr_object.readyState == 4){
                    var temp=document.body;
                    var body=getBody(that._xhr_object.responseText);
                    temp.innerHTML=body;
                    }
                }
            this._xhr_object.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	    if(this._xhr_object.overrideMimeType){
		this._xhr_object.overrideMimeType('text/html; charset=ISO-8859-15');			
	    }
	    this._xhr_object.send(data);
            }
//is ok
/*________________________________________________________________END OF BUSINESS OBJECTS_________________________________________________________________*/
  /*---------------------------------------------------------------Interfaces Objects------------------------------------------------------------------------*/
  /*+++++++++++++++++++++++++++++++++++++++CLass timerDecompte is a timestamp used to display the time before the next request+++++++++++++++++++++++++++++*/
  function timerDecompte(timeOf,msg) {
        var conteneurCompteur=null;
	var that=this;
        var decompte;
	var stout=null;
        this.launch=function(){
            if(timeOf > 0) {
		timeOf--;
		stout = setTimeout(function(){that.launch();},1000);//every seconds
            } else {
		timeOf = "Finished";
		if(stout!=null){
		    clearTimeout(stout);   
		}
            }
            if(!document.getElementById('compteurConteneur')){
                conteneurCompteur=document.createElement('div');
                conteneurCompteur.id='compteurConteneur';
                conteneurCompteur.style.width="auto";
                conteneurCompteur.style.height="auto";
                conteneurCompteur.style.zIndex=10;
                conteneurCompteur.style.position="absolute";
                conteneurCompteur.style.textAllign="center";
                conteneurCompteur.style.right="50%";
                conteneurCompteur.style.backgroundColor="yellow";
                conteneurCompteur.style.top="200px";	
		//conteneurCompteur.style.fontStyle="bold";
                document.body.appendChild(conteneurCompteur);
            }
            document.getElementById('compteurConteneur').innerHTML='<b>'+msg+timeOf+"sec </b>";
        }
} 
/*+++++++++++++++++++++++++++class formTicketsInAjax must be used in the ticket's form+++++++++++++++++++++++++++++++++++*/
/*/!\param form is the element form will be sent in nbreRequests param 2,
  /!\nbreMiniPerRequest is the minimal number of lines could be send in the request next to the first request,
  /!\nbreFirstReqiest is the number of lines will be send in the first request,
  /!\url is the target page*/
function formTicketsInAjax(form,nbreRequests,nbreMiniPerRequest,nbreFirstRequest,t,url){
  /*++++++++++++++++++++privates Members+++++++++++++++++++++++++*/
  var contenu = form.value;
  var obj = new mise_en_forme(contenu,nbreFirstRequest,nbreRequests,nbreMiniPerRequest);
  var arrayWillBeSend = obj.get_tableauChaines();
 // alert(arrayWillBeSend[0]);
  var inputForPosts =get_Input();
  /*++++++++++++++++++++privates methods+++++++++++++++++++++++++*/
  function get_Input(){
        var parent = form.parentNode;
        var listInput = parent.getElementsByTagName('input');
        var stringForPosts="";
        var i=0;
        for(i=0;i<listInput.length;i++){
              stringForPosts+=listInput[i].name+'='+listInput[i].value+"&";
        }
        return stringForPosts;
  }
  function oneRequest(datasForSend){
    var stringForPosts=inputForPosts+form.name+"="+datasForSend;
    var request = new xhr_post(stringForPosts,url);
  }
  /*++++++++++++++++++++publics methods++++++++++++++++++++++++++*/
  this.sendDatas=function(){
    var i=0;
    for(i in arrayWillBeSend){
      oneRequest(arrayWillBeSend[i]+"&t="+(t*i));
    }
    var decompte = new timerDecompte((t*i),"Temps restant avant l'encaissement total de vos points :: ");
    decompte.launch();
  }
}  
/*_____________________________________________________________________End of class FormTicketInAjax________________________________________________________________*/
/*This pluggin use to divide string of an text area form and send them in multiples ajax posts requets.
  pluggin/add on developped by yardev (web developper of ITSTORM company)
  contact: yardevil@hotmail.fr or on website http://www.itstorm.fr,
  if you want to use that for free, thanks to put a link of itstorm's website in your webpage and speak about us */


