﻿var ShowSMpIndex = 0;
var vRetBt = false;
var vSMDone = false;

function ShowSM(pIndex) {
    ShowSMpIndex = pIndex;
    var i;              
    if (isNaN(pIndex)) {pIndex = 1;}                
    for (i=1; i<=7; i++) {document.getElementById('ctl00_spnSM'+i).style.display = 'none';}
    document.getElementById('ctl00_spnSM'+pIndex).style.display = '';
}

function fitPic(obj) {
	if ( obj.height >= screen.height-100 ) {
	    obj.height = Math.round(obj.height / 2);
	}    
	
	if ( obj.width >= screen.width-100 ) {
	    obj.width  = Math.round(obj.width / 2);
	}
}

function LimitTextarea(pTA, pMax) {
  pTA.value = pTA.value.slice(0, pMax)
  
  if(document.getElementById('spnNbCar')){document.getElementById('spnNbCar').innerHTML = 'Caractères restants : <b>' + (pMax - pTA.value.length) + '</b>';}
}

function cmdArticle(pCode) {
    var varURL = "pAddAchat.aspx?CODE=" + pCode;
    window.open(varURL, "AddAchat", "width=20, height=20, directories=no, menubar=no, status=no, location=no");
}

function Timer(pNomSpan) {
    if (document.getElementById(pNomSpan)) {
        if (document.getElementById(pNomSpan).style.display == 'none' ) {
            nd();
            window.clearTimeout(chang);
        } else {
            chang=window.setTimeout("Timer('" + pNomSpan + "');",200);
        }
    }					
}

function checkQte(pObject, pMax, pMin) {
    var pIsBorne = true;
    
    if (typeof pMax == "undefined") {
        pMax = 9999;
        pIsBorne = false;
    }
    
    if (typeof pMin == "undefined") {
        pMin = 0;
    }

    if (pObject.value == '') {
        pObject.value=pMin;
        pObject.focus();
        return false;
    }
    if ( isNaN(pObject.value) || eval(pObject.value) < pMin || eval(pObject.value) > pMax ) {
        if (pIsBorne) {
            switch (pMax) {
                case 0:
                    window.alert("Il n'y a aucun article en stock.\nImpossible de commander cet article.");
                    pObject.value = '0';
                break;
                
                case 1:
                    window.alert("Vous ne pouvez commander que " + pMax + " article");
                    pObject.value = pMax;
                break;
                
                default:
                    window.alert("Vous ne pouvez commander que " + pMax + " articles");
                    pObject.value = pMax;
            }
            
        } else {
            window.alert("La Quantité doit être comprise entre " + pMin + " et 9999 ?");
            pObject.value='1';
        }        
        pObject.select();
        return false;
    }
    return true;
}

function ChangeQte(NomChamps, PlusMoins, pMax) {
    var pIsBorne = true;
    
    if (typeof pMax == "undefined") {
        pMax = 9999;
        pIsBorne = false;
    }

    if (PlusMoins == '+') {
        NomChamps[0].value = parseInt(NomChamps[0].value) + 1;
    } else {
        NomChamps[0].value = parseInt(NomChamps[0].value) - 1;
    }
            
    if (NomChamps[0].value < 0) {
        NomChamps[0].value = 0;
    }        
    if (NomChamps[0].value > pMax) {
        NomChamps[0].value = pMax;
        if (pIsBorne) {
            switch (pMax) {
                case 0:
                    window.alert("Il n'y a aucun article en stock.\nImpossible de commander cet article.");
                    NomChamps[0].value = '0';
                break;
                
                case 1:
                    window.alert("Vous ne pouvez commander que " + pMax + " article");
                    NomChamps[0].value = pMax;
                break;
                
                default:
                    window.alert("Vous ne pouvez commander que " + pMax + " articles");
                    NomChamps[0].value = pMax;
            }
            
        }
    }
    
    NomChamps[0].select();
}
    
function ChangeQtePrinc(NomChamps, PlusMoins, pMax, pMin) {
    var pIsBorne = true;
    
    if (typeof pMax == "undefined") {
        pMax = 9999;
        pIsBorne = false;
    }
    
    if (typeof pMin == "undefined") {
        pMin = 0;
    }
    
    if (PlusMoins == '+') {
        NomChamps.value = parseInt(NomChamps.value) + 1;
    } else {
        NomChamps.value = parseInt(NomChamps.value) - 1;
    }
            
    if (NomChamps.value < pMin) {
        NomChamps.value = pMin;
    }        
    if (NomChamps.value > pMax) {
        NomChamps.value = pMax;
        if (pIsBorne) {
            switch (pMax) {
                case 0:
                    window.alert("Il n'y a aucun article en stock.\nImpossible de commander cet article.");
                    NomChamps.value = '0';
                break;
                
                case 1:
                    window.alert("Vous ne pouvez commander que " + pMax + " article");
                    NomChamps.value = pMax;
                break;
                
                default:
                    window.alert("Vous ne pouvez commander que " + pMax + " articles");
                    NomChamps.value = pMax;
            }
        }
    }
}

function ShowProgress() {
    var pTexte = arguments[0];
    var pNomSpan = arguments[1];
    var pNiveau = arguments[2];
    
    if (!pNomSpan) {
        pNomSpan = 'spnProgress';
    }
    
    if (!pNiveau) {
        pNiveau = '';
    }
   
    overlib("<table class='fondColor5' cellpadding='8' cellspacing='8' style='border-style:solid;border-width:1px;border-color:#000000;filter:alpha(style=0,opacity=80);' width='400' height='100'><tr><td align='center'><img src='" + pNiveau + "images/progressbar.gif'></td></tr><tr><td align='center'><font class='smallColor6'><b>" + pTexte + "</b></font></td></tr></table>", FULLHTML, CENTERPOPUP);
    document.getElementById(pNomSpan).style.display = '';
    Timer(pNomSpan);
} 

function Trim(s) {
    return s.replace(/^\s+/, '').replace(/\s+$/, '');
}

function ScrollLegende(pId, pPosTop) {
    var vLegende = document.getElementById(pId);
    var vScrollTop, vRes, vPx;
    
    if (vLegende) {
        var largeur = 0;
        if( typeof( window.innerWidth ) == 'number' ) {
          largeur = window.innerWidth;
          }
        else if( document.documentElement && document.documentElement.clientWidth ) {
          largeur = document.documentElement.clientWidth;
          }
        else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
          largeur = document.body.clientWidth;
          }
        else {
          largeur = -1;
        }
        
        vRes = largeur - parseInt(vLegende.style.width.replace('px','')) - 6;
        
        vPx = '';
        switch(navigator.appName) {
            case "Netscape":
                vRes = vRes - 18;
                if (navigator.appCodeName == "Mozilla") {
                    vPx = 'px';                    
                }
            break;
            case "Opera":
                vRes = vRes - 18;
                vPx = 'px';
            break;
        }
        
        vLegende.style.left = vRes + vPx;
        
        vScrollTop = document.documentElement.scrollTop + 1;
        
        if (vScrollTop <= pPosTop) {
            vLegende.style.top = pPosTop + vPx;
        } else {
            vLegende.style.top = vScrollTop + vPx;
        }
    }
}
         
function PageBlanche(pOn, pFilter) {
    if ( navigator.userAgent.indexOf("MSIE 6") != -1 )
    {
        var i;
        var elm;
        var max = document.forms[0].elements.length
        var pVisible;
        var pFiltre;
        
        if (typeof pFilter == "undefined") { pFilter = true; }
        
        if (pOn) {
            pVisible = 'hidden';
            pFiltre = 'Alpha(opacity=25)';
        } else {
            pVisible = 'visible';
            pFiltre = '';            
        }
        
        if (max > 20) { max = 20; }
        
        for(i = 0; i < max; i++) {
            elm = document.forms[0].elements[i];
            if (elm.type == 'select-one' || elm.id.indexOf('pnlLegende') > 0) {
                elm.style.visibility = pVisible;
            }
        }
        
        if (document.getElementById('ctl00_PagePrincipale_pnlLegende')) {            
            document.getElementById('ctl00_PagePrincipale_pnlLegende').style.visibility = pVisible;
        }
        
        if (pFilter)
        {
            if ( document.getElementById('bodyPage') ) { document.getElementById('bodyPage').style.filter = pFiltre; }
            if ( document.getElementById('tabContenuMenu') ) { document.getElementById('tabContenuMenu').style.filter = pFiltre; }
            if ( document.getElementById('ctl00_dvContenu') ) { document.getElementById('ctl00_dvContenu').style.background = ''; }
            if ( document.getElementById('ctl00_PagePrincipale_trDesc') ) { document.getElementById('ctl00_PagePrincipale_trDesc').style.filter = pFiltre; }
            if ( document.getElementById('ctl00_PagePrincipale_pnlTitreArticle') ) { document.getElementById('ctl00_PagePrincipale_pnlTitreArticle').style.filter = pFiltre; }
            if ( document.getElementById('ctl00_PagePrincipale_pnlArticle') ) { document.getElementById('ctl00_PagePrincipale_pnlArticle').style.filter = pFiltre; }
            if ( document.getElementById('ctl00_PagePrincipale_dvBoite') ) { document.getElementById('ctl00_PagePrincipale_dvBoite').style.filter = pFiltre; }
            if ( document.getElementById('tblDesc') ) { document.getElementById('tblDesc').style.filter = pFiltre; }
            if ( document.getElementById('tabPnlInfo') ) { document.getElementById('tabPnlInfo').style.filter = pFiltre; }
            if ( document.getElementById('ctl00_PagePrincipale_pnlMini') ) { document.getElementById('ctl00_PagePrincipale_pnlMini').style.filter = pFiltre; }
            for (i = 0; i <= 5; i++) {
                if ( document.getElementById('tabArtAccessoire'+i) ) {
                    document.getElementById('tabArtAccessoire'+i).style.filter = pFiltre;
                }
            }
        }
    }
    else
    {
        if ( navigator.userAgent.indexOf("MSIE 7") == -1 && navigator.userAgent.indexOf("MSIE 8") == -1 ) {
            if ( document.getElementById('tblGENERAL').style.opacity != '0.25' ) {
                document.getElementById('tblGENERAL').style.opacity = 25 / 100;
                    document.getElementById('tblGENERAL').style.filter = 'alpha(opacity:25)';
                document.getElementById('ctl00_dvContenu').style.opacity = 25 / 100;
                    document.getElementById('ctl00_dvContenu').style.filter = 'alpha(opacity:25)'; 
            } else {
                document.getElementById('tblGENERAL').style.opacity = 1;
                    document.getElementById('tblGENERAL').style.filter = 'alpha(opacity=100)';
                document.getElementById('ctl00_dvContenu').style.opacity = 1;
                    document.getElementById('ctl00_dvContenu').style.filter = 'alpha(opacity=100)';                 
            }
        }  
    }
}

function SetFocus(pId) {
    setTimeout("document.getElementById('" + pId + "').focus()", 1);
}
        
function SlideImg(pSens, pNom) {
    var i;
    var vSelected = 0;
    var vNbImg = 0;
    
    pNom = pNom.substr(0, pNom.length - 1)
    
    for(i=0; i<=2; i++) {
        if (document.getElementById(pNom+i)) {
            if (document.getElementById(pNom+i).style.display == '') {
                vSelected = i;
                document.getElementById(pNom+i).style.display = 'none';
            }
            vNbImg = i;
        }
    }
    
    if (pSens == '+') {
        vSelected = vSelected + 1;                
        if (vSelected > vNbImg) {
            vSelected = 0;
        }
    } else {
        vSelected = vSelected - 1;
        if (vSelected < 0) {
            vSelected = vNbImg;
        }
    }
    
    document.getElementById(pNom+vSelected).style.display = '';
}

function RedirectCateg(pNiveau, pIdC1, pIdC2, pIdC3, pIdC4, pIdC5) {
    var vC = new Array();
    var i;
    var vTmp;
    
    if (typeof pNiveau == "undefined") {
        pNiveau = 5;
    }
    
    if (document.getElementById(pIdC1)) {
        vC[1] = document.getElementById(pIdC1).options[document.getElementById(pIdC1).selectedIndex].value;
    } else {vC[1] = '';}
    
    if (document.getElementById(pIdC2)) {
        vC[2] = document.getElementById(pIdC2).options[document.getElementById(pIdC2).selectedIndex].value;
    } else {vC[2] = '';}
    
    if (document.getElementById(pIdC3)) {
        vC[3] = document.getElementById(pIdC3).options[document.getElementById(pIdC3).selectedIndex].value;
    } else {vC[3] = '';}
    
    if (document.getElementById(pIdC4)) {
        vC[4] = document.getElementById(pIdC4).options[document.getElementById(pIdC4).selectedIndex].value;
    } else {vC[4] = '';}
    
    if (document.getElementById(pIdC5)) {
        vC[5] = document.getElementById(pIdC5).options[document.getElementById(pIdC5).selectedIndex].value;
    } else {vC[5] = '';}
    
    vTmp = '?C1=' + encodeURIComponent(vC[1]);
    
    for (i=2; i<=pNiveau; i++) {
        if (vC[i] != '') {
            vTmp = vTmp + '&C' + i + '=' + encodeURIComponent(vC[i]);
        } else {
            i = 6;
        }
    }
    
    window.location.href = 'pGroupe.aspx' + vTmp;
}

function CheckCaddie(pCode, pNb, pListe) {
    var vValideCodePrincipal, vValide, vAcc, vFin, vCpt;
    
    vValideCodePrincipal = true;
    if (pCode != 'NO') {
        if (pListe.value.indexOf(pCode) > -1) {
            if (parseInt(pNb.value) > 0) {
                vValideCodePrincipal = confirm('Votre caddie comporte déjà l\'article ' + pCode + ', êtes-vous certains de vouloir en rajouter ' + pNb.value + ' en plus ?');
            }
        }
    }
    
    vAcc = false;
    vFin = false;
    vCpt = 0;
    while (!vFin) {
        if (document.getElementById('tbxQTEART' + vCpt)) {
            if (parseInt(document.getElementById('tbxQTEART' + vCpt).value) > 0) {
                if (pListe.value.indexOf(document.getElementById('hdCODE' + vCpt).value) > -1) {
                    if (!confirm('Votre caddie comporte déjà l\'article ' + document.getElementById('hdCODE' + vCpt).value + ', êtes-vous certains de vouloir en rajouter ' + document.getElementById('tbxQTEART' + vCpt).value + ' en plus ?')) {
                        document.getElementById('tbxQTEART' + vCpt).value = 0;
                    } else {
                        vAcc = true;
                    }
                }
            }
            
            vCpt = vCpt + 1;
        } else {
            vFin = true;
        }
    }
    
    if (!vValideCodePrincipal && vAcc) {
        pNb.value = 0;
    }

    return vValideCodePrincipal || vAcc;
}

// FONCTION QUI PERMET LA CREATION D'UN COOKIE (UTILISABLE AUSSI EN VB.NET)
// EX: set_cookie('NOM_DU_COOKIE', 'VALEUR_DU_COOKIE');
function set_cookie(name, value, exp_y, exp_m, exp_d, path, domain, secure) {
    var cookie_string = name + "=" + escape ( value );

    if ( exp_y ) {
        var expires = new Date ( exp_y, exp_m, exp_d );
        cookie_string += "; expires=" + expires.toGMTString();
    }
    if ( path )
        cookie_string += "; path=" + escape ( path );
    if ( domain )
        cookie_string += "; domain=" + escape ( domain );
    if ( secure )
        cookie_string += "; secure";

    document.cookie = cookie_string;
}

// FONCTION QUI PERMET DE LIRE UN COOKIE
// EX: get_cookie('NOM_DU_COOKIE');
function get_cookie(cookie_name)
{
    var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

    if ( results )
        return ( unescape ( results[2] ) );
    else
        return null;
}

// FONCTION XMLHTTPREQUEST (AJAX)
// METHODE -> POST ou GET  ¦  URL -> Adresse aspx avec le traitement vb  ¦  PARAM -> (En option) Paramètres à passer, peux aussi se faire dans URL  ¦  CADRE -> Element html (div, span...) ou l'objet sera afficher
function xhr(methode, url, param, cadre)
{
    var xhr = null;

    // instance xmlHttpRequest selon le navigateur
	if(window.XMLHttpRequest) // Firefox
		xhr = new XMLHttpRequest();
	else if(window.ActiveXObject) // Internet Explorer
		xhr = new ActiveXObject("Microsoft.XMLHTTP");
	else { // XMLHttpRequest non supporté par le navigateur
		//alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
		return;
	}

    // page de chargement
	if(xhr != null){
	    document.getElementById(cadre).innerHTML = " ";
	}

	// envoie de la requête, methode plus url
	xhr.open(methode, url, true);
	
	// on teste si GET (envoi rien) ou POST (envoi du header et des paramètres)
	if(methode=='POST') {
	    xhr.setRequestHeader("Content-Type", "plain/text; charset=UTF-8");
	    xhr.send(param);
	} else {
	    xhr.send(null);
	}

	// on guette les changements d'état de l'objet
	xhr.onreadystatechange = function attente() {
	    // l'état est à 4, requête reçu !
	    if(xhr.readyState == 4)
	    {
	        if(xhr.status == 200)
	        {
                document.getElementById(cadre).innerHTML = xhr.responseText;
	            document.getElementById(cadre).style.top = posy + 'px';
		    }
	    }
	}

	return; // le travail est terminé
}
