


var gridClass = new Array();
gridClass['tiers'] = 14;
gridClass['ptTiers'] = 10;
gridClass['half'] = 7;
gridClass['little'] = 4;
gridClass['max'] = 14;


var timer_is_on=0;
function doTimer(script, time)
{    
    if (timer_is_on == 0)
    {
        timer_is_on=1;
        t = setTimeout(script , time) ;
    }else{
        clearTimeout(t);
        t = setTimeout(script , time) ;              
    }
}
function ajaxQuery(str, doc, target)
{
    if (str=="")
    {
        document.getElementById(target).innerHTML="";
        return;
    }
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            document.getElementById(target).innerHTML=xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET",doc + str,true);
    xmlhttp.send();
    return 
    
    
}

function ajaxPostQuery(str, doc, target, selectName, elemNameId, elemNameLib)
{
    if (str=="")
    {
        document.getElementById(target).innerHTML="";
        return;
    }
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            document.getElementById(target).innerHTML=xmlhttp.responseText;
           
            inSelect = document.getElementById(selectName);
            inSelect.options[inSelect.length] = new Option(document.getElementById(elemNameId).value,document.getElementById(elemNameLib).value);
            inSelect.selectedIndex = inSelect.length-1;
        }
    }
    xmlhttp.open("POST",doc ,true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.setRequestHeader("Content-length", params.length);
    xmlhttp.setRequestHeader("Connection", "close");
    
    xmlhttp.send(str);
   
    
    
}

function limitText(limitField, limitCount, limitNum) {
    alert('limite text');
    if (limitField.value.length > limitNum) {
        limitField.value = limitField.value.substring(0, limitNum);
    } else {
        limitCount.value = limitNum - limitField.value.length;
    }
}


function storeCaret(textEl)
{
    if (textEl.createTextRange)
        textEl.caretPos = document.selection.createRange().duplicate();
}


function brButton(textearea){
    insertAtCaret(textearea,'<br />');

}

function writeText(textArea, prevSpan){
    txtA = document.getElementById(textArea);
    prev = document.getElementById(prevSpan);
    value = '<font>';
    value = value + txtA.value;
    value = value + '</font>';
    prev.innerHTML = value;

}


function visibilite(IdA, IdB)
{
    var targetElement;
    targetElementA = document.getElementById(IdA) ;
    targetElementB = document.getElementById(IdB) ;
    if (targetElementA.style.display == "none")
    {
        targetElementA.style.display = "" ;
        targetElementB.style.display = "none" ;
    } else if (targetElementB.style.display == "none"){
        targetElementB.style.display = "" ;
        targetElementA.style.display = "none" ;
    }
}

function visibilite2(IdA, IdB)
{
    targetElementA = document.getElementById(IdA) ;
    targetElementB = document.getElementById(IdB) ;

    if(targetElementA != null)
        targetElementA.style.display = "";
    if(targetElementB!= null)
        targetElementB.style.display = "none";
}

function visibilite3(IdA, IdB, IdC)
{
    var targetElement;
    targetElementA = document.getElementById(IdA) ;
    targetElementB = document.getElementById(IdB) ;
    targetElementC = document.getElementById(IdC) ;
    if(targetElementA != null){
        targetElementA.style.display = "";
        targetElementB.style.display = "none";
        targetElementC.style.display = "none";
    }
}

function multiForm(formsName, currentFormName){
    
    $('#title_' +currentFormName).addClass('BoFormTitleSelected');
                     
    document.getElementById('span_' + currentFormName).style.display ='' ;
    for(i = 0; i < formsName.length; i++){
        $('#title_' + formsName[i]).removeClass('BoFormTitleSelected');
        document.getElementBtyId('span');
        document.getElementById('span_' + formsName[i]).style.display ='none' ;
    }
   
}

function ShowOnlyOne(theOne, others){
    
    for(i = 0; i < others.length; i++){
        document.getElementById(others[i]).style.display = "none" ;
    }
    document.getElementById(theOne).style.display = "" ;

}



/*----------------------LISTE SIMPLE--------------------- */


function stringInput(cpt){
   
    string =' ';
    i = 0;
    for(i; i < cpt; i++)
    {
        string += '<input type="text" id="Liste' + i + '"><br />';
    }
    string +='<input type="hidden" value="' + cpt + ' " id="nbInputListe">';
    return string;
}

function addInput(cpt, span){

    span.innerHTML = stringInput(cpt);

}

cssFile = 'http://www.labaroufada.com/css/text.css';
        
function makeWizzy(name, spanLimit, Limit){
    if(!o('whizzytxtA' + name)){
                    
        buts ='t1 t2 t3 p';
                
        dobut['t1'] = '<H2>';
        dobut['t2'] = '<H3>';
        
        dobut['t3'] = '<H4>';
        dobut['p'] = '<p>';
        limit = Limit;
        spLimit = spanLimit;
        makeWhizzyWig('txtA' + name, 'bold italic | t1 t2 t3 | p | bullet | left center right | undo redo | link');
    }
}



function makeSimpleWizzy(name, spanLimit, Limit){
    if(!o('whizzytxtA' + name)){
                    
         buts ='t1 t2 t3 p';
                
        dobut['t1'] = '<H2>';
        dobut['t2'] = '<H3>';
        
        dobut['t3'] = '<H4>';
        dobut['p'] = '<p>';
        limit = Limit;
        spLimit = spanLimit;
        makeWhizzyWig('txtA' + name, 'bold italic | t1 t2 t3 | p | bullet | left center right | undo redo | link');
    }
}

function stringSubInput(cpt, id){

    string =' ';
    i = 0;
    for(i; i < cpt; i++)
    {
        string += '------------------------><input type="text" id="subInput'+ id + '-' + i + '"><br />';
    }
    string +='<input type="hidden" value="' + cpt + ' " id="subNb'+id+'">';
    return string;
}

function addSubInput(cpt, span, id){

    span.innerHTML = stringSubInput(cpt, id);

}


function echoRecusiveString(tableau, selectedBlock, spanToEcho, tableName){
    str = recursiveString(tableau, selectedBlock, spanToEcho, tableName);
   
    document.getElementById(spanToEcho).innerHTML= str.replace(/\\/, '');
}

//0:type
//1:id
//2class
//3 contenu
function recursiveStringForInsert(tableau){
    var str ='';
    if(tableau[0] == 0)
    {
        cnt = "";
        for(var i = 3; i < tableau.length; i++){
            cnt +=recursiveStringForInsert(tableau[i]);
        }
        
        str += cnt;
       
    }else if(tableau[0] == 1){
        if(tableau.length > 3){
            var cnt = tableau[3];
            cnt = cnt.replace(/<br>/gi, '<br />').replace(/\n/gi, '');
            cnt = cnt.replace(/<iframe (.*) \/>/, '');
           
            cnt = cnt.replace(/&nbsp;/, ' ');
        }else{
            cnt = "";
        }
        
        str +=  '<span class="' + tableau[2] + '">' + cnt + '</span>';
    }
    if(str == "") str = '<span class="block tiers"></span>';
    return str;
}

function recursiveString(tableau, selectedBlock, spanToEcho, tableName, lastId){
    
            
    var str ='';
    if(tableau[0] == 0)
    {
        cnt = "";
        str +='<span class="block all">';
       
        for(var i = 3; i < tableau.length; i++){
            cnt +=recursiveString(tableau[i], selectedBlock, spanToEcho, tableName, lastId)
        }
        
        str += cnt;
        str +='</span>';
    }else if(tableau[0] == 1){
        if(tableau.length> 3){
            var cnt = tableau[3];
            cnt = cnt.replace(/<br>/gi, '<br />').replace(/\n/gi, '');
            cnt = cnt.replace(/<iframe(.*)\/>/, '<iframe $1 >');
        
            cnt = cnt.replace(/&nbsp;/, ' ');
        }else{
            cnt ='';
        }
        
        str +=  '<span class="' + tableau[2] + '"' ;
        
        if(selectedBlock == tableau[1])
            str += 'style = "border:thin solid blue;"';
        else 
            str += 'style = "border:thin dashed white;"';
        str += 'onclick="if(dbClick){modifBlock(tableau'+tableName+',selectedBlock' + tableName + ' , \'' + tableName + '\')}setTimeout(\'atEndtimerDbClick();\',400);selectedBlock' + tableName + '='+tableau[1]+'; echoRecusiveString(tableau'+tableName+', ' +tableau[1] + ' , \'' + spanToEcho + '\', \''+tableName+'\'); dbClick = 1;" ';
  
        str += '>' + cnt + '</span>';
        
    }

    return str;
}

var dbClick = 0;
function atEndtimerDbClick(){
    dbClick = 0;
}
function modifBlock(tableau,selected , name){
    
    if(tableau[1] == selected){
        if(tableau[0] == 1){
            if(!tableau[3].match(/<img/))
                visibilite2(('gestionText' + name),('gestionFichier' + name));
            else
                alert('Pour modifier une image, supprimer la puis ajouter en une nouvelle.');
            ;
            cnt = tableau[3];
            cnt = cnt.replace(/<cnt>/, '').replace(/<\/cnt>/, '');
            //  cnt = cnt.replace(/<br \/>/gi, '\n');
            var d=o('whizzytxtA' + name).contentWindow.document;
            if (o('txtA'+ name).style.display == 'block') d.body.innerHTML=o(t).value;
            d.body.innerHTML =  cnt;
           
        }
    }else{
                
        for(var i = 3 ; i <tableau.length; i++){
            modifBlock(tableau[i], selected, name);
               
        }
    }
}


function addBlock(tableau,selected, classe, Id, nbGrid, GridTT){
    if(tableau[0] == 0){
        tableau.push(new Array(1,Id, classe));

    }
    
}

function upTableau(tableau, id){
    noFound = true;
    for (i = 3 ; i< tableau.length && noFound; i++ ){
        if(tableau[i][1] == id){
            if(i != 3){
                tab1 = tableau[i-1];
                tableau[i-1] = tableau[i];
                tableau[i] = tab1;
            }
            noFound = false;
        }
    }
    
    if(noFound){
        alert("Auqu'un bloque n'est sélectionné. Vous devez sélectionner un bloque pour pouvoir le déplacer. ");
    }
}


function downTableau(tableau, id){
    noFound = true;
    for (i = 3 ; i< tableau.length && noFound; i++ ){
        if(tableau[i][1] == id){
            
            if(tableau.length-1 > i){
                tab1 = tableau[i+1];
                tab2 = tableau[i];
                
                tableau[i+1] = tab2;
                tableau[i] = tab1;
            }
            noFound = false;
        }
    }
    
    if(noFound){
        alert("Auqu'un bloque n'est sélectionné. Vous devez sélectionner un bloque pour pouvoir le déplacer. ");
    }
}

function gridLength(classe){
    ln = -1;
    for(var i in gridClass){
        
            
        if(classe.search(/i/) > -1){
            ln = gridClass[i];
            exit;
        }
    }
    return ln;
   
}
function addBlockIn(tableau,selected, classe, Id){


    var cpt = 3;
    var ln = tableau.length;
    while((cpt < ln) && selected != tableau[cpt][1]){
        if(tableau[cpt][0] == 0)
            addBlockIn(tableau[cpt], selected, classe, Id);
        
        cpt ++;
    }
    if(cpt < ln)
        if(selected == tableau[cpt][1]){
            tableau[cpt].push(new Array(0,Id, classe, ' '));
        }

}



function addContent(tableau, selected, content, id, type){
    if(selected !=  0){
        var ret = -1;
        if(tableau[1] == selected){
            if(tableau[0] == 1){
                tableau[3] =  '<cnt>' + content + '</cnt>' ;
                ret =  selected;
            }
        }else{
         
            for(var i = 3 ; (i <tableau.length && ret == -1); i++){
            
                ret = addContent(tableau[i], selected,content, id, type);
            }
        }
    }else{
        alert('Vous devez sélèctionner un bloque pour ajouter du contenu. \n\n Vous pouvez créer un bloque via les boutons de "très grand" à "petit" ');
    }
 
    
          
    return ret;
}

function supBlock(tableau, selected){
    if(selected != tableau[1]){
        if(tableau[0] == 0){
            var i = 3;
            var found = false;
            while (!found  && i < tableau.length ){
                if(tableau[i][1] != selected) {
                    supBlock(tableau[i] , selected);
  
                    i++;
                }else{
                    found = true;
                }
            }

                
            if(found){
                if(tableau[i][1] ==  selected){
                    
                    tableau.splice(i,1);
                }
            }
        }

    }
}


