// test menu for boutik... 090510

 var header, output,mainTable, prodTable,info, footer, backBtn ;  // html elements

var aX,aP;
var xDoc;

curArtName='';
curArtNo='';

var artNo = new Array();
var artNames=new Array();
var artNamesSorted=new Array();

var nameListOn=true;

var welcomeStr1='<h2> Liste des artisans </h2> <div class="cTitle">année 2009</div>';
var welcomeStr2='<h2> Consignation </h2> <div class="cTitle">année 2009</div>';

function initAll(){
 header = document.getElementById('header');
 info = document.getElementById('info');
 caption = document.getElementById('caption');
 output = document.getElementById('output');
 mainTable=document.getElementById('mainTable');
 prodTable=document.getElementById('prodTable');
 pageOut= document.getElementById('pageOut');
 footer = document.getElementById('footer');
// prevBtn = document.getElementById('prevBtn');
// nextBtn = document.getElementById('nextBtn');
 backBtn = document.getElementById('backBtn');
 
 xDoc=loadXMLDoc("artList.xml");
// xDoc=loadXMLDoc("testA.xml");
 aX=xDoc.getElementsByTagName("artID");
 aP=xDoc.getElementsByTagName("product");
 //output.innerHTML=setArtNames();
 FTinit();
 setArtNames();

 //applyXSL('bookMap','artList.xml','artList.xsl')
}

var ids=new Array();
function FTinit(){
var tids=document.getElementsByTagName('div');
  for(i=0;i<tids.length;i++)
     if(tids[i].className=="artNo")
        ids[ids.length]=tids[i];

  for(i=0;i<ids.length;i++){
    //ids[i].onmouseup=setstate;
    ids[i].onmouseover=setCursor;
    ids[i].onmouseout=outCursor;
    ids[i].onmouseup=showArtID;
    }
//output.innerHTML= "number of artisans is: " + tids.length;
}


///////////////// general functions /////////////////
// should be moved to xmlUtils...
// keeps undefined childNodes quiet...
function getElmByTag(node , tag){
var res='';
if( node != null ){
    var x = node.getElementsByTagName(tag);
      if ((x[0]!=undefined)&&(x[0].childNodes[0]!=undefined))
           res=x[0].childNodes[0].nodeValue;

     }
return res;
}

function setArtNames(){
  for (i=0;i<aX.length;i++) {
   artNames[i] = getElmByTag(aX[i],'nom')+ ' ' + getElmByTag(aX[i],'prenom');
   artNo[i] = getElmByTag(aX[i],'artNo') ;
   }
  return artNames.join();
  /*
   var tmp = aX[i].getElementsByTagName("nom")[0].childNodes[0].nodeValue; // this is good too
  */
}




/////////////////////////////////////////////////////
function setArtTable(){
 header.innerHTML= welcomeStr1;
 caption.innerHTML = 'No d\'artisans:' + artNames.length;

 var maxArt = artNames.length;
 if (maxArt>20) mainTable.style.width='80%'
   else mainTable.style.width='60%';
/*
 if (maxArt>20) mainTable.display.width=tWidth='100%'
   else tWidth='60%';
   var outStr='<table border=1 width=' + tWidth + ' align=center><tr><td vAlign=top>'
*/

var outStr='<table border=1 width=100% align=center><tr><td vAlign=top>'

 setArtNamesSorted() ;
 for (i=0;i<maxArt;i++) {
     if (nameListOn) fName = artNamesSorted[i]
       else fName = artNames[i]

      outStr += (i+1) + ' - <a href="javaScript:showArtID('+ i + ')">' +  fName + '</a><br/>';
      x=(i+1)%20;
      if ((i>1)&&(x==0)) outStr +='</td><td vAlign=top>';
     }
  outStr += '</td></tr></table>'
  mainTable.innerHTML=outStr;
 //mainInfo.innerHTML= 'No of arts:' + len
 // mainInfo.innerHTML= artNames.join() + '<br/>' + artNamesSorted.join();
}


function setArtNamesSorted(){
  artNamesSorted.length = 0;
 for(i=0;i<artNames.length;i++){
     artNamesSorted.push(artNames[i]);
     //var label = artNames[i] + ' (' + artNo[i] + ')';
     //    artNamesSorted.push(label);
         }
   artNamesSorted.sort();
}

function indexFromName(n){
 var res=-1;
 fName = artNamesSorted[n]
 for(i=0;i<artNames.length;i++){
    if (fName==artNames[i]) res=i;
    }
 return res;
}

////////////////////////////////////////////////////
//////////// artID ////////////////////////////////
function showArtID(n){
 var ind = n;

 // when called from xsl
 if (ind==undefined){
    for(i=0;i<ids.length;i++)
      if( ids[i]==this )  ind = i ;
      }

    if ( nameListOn ) ind = indexFromName(ind);
    
    var adStr = getElmByTag(aX[ind],'adresse')
    var adr = adStr.split(';');
    adStr='';
    for (i=0;i<adr.length;i++){
      tmp = adr[i].replace('"','');
      if (tmp !='')
         adStr +=  tmp + '<br />'
      }

    var tmp =
          '<div class="artID">'
          +  '<div class="artNo">' + getElmByTag(aX[ind],'artNo') + '</div>'
          +  '<div class="artName">' + getElmByTag(aX[ind],'nom')
          + '&nbsp;' + getElmByTag(aX[ind],'prenom') + '</div>'
          +  '<div class="eMail">' + getElmByTag(aX[ind],'eMail')+ '</div>'
          +  '<div class="artAddr">' + adStr + '</div>'
          + '</div>'
          ;

   //alert( 'art is no ' + ind);
    //mainTable.innerHTML= '' ;
    
    header.innerHTML= welcomeStr2;
    mainTable.style.display="none";
    output.innerHTML = '';
    info.innerHTML = tmp
    
    prodTable=document.getElementById('prodTable');
    prodTable.innerHTML=setProdTable(ind);
   //applyXSL2Node('output',aX[ind],'artID.xsl')
}



function setProdTable(ind){
  var a= aP[ind].getElementsByTagName("item");
var tmp = '<h4>Liste des produits <span class="note"> (' + a.length + ' items)</span> </h4>'
    + '<table border="0"  align="center" class="itemDATA">'
    + '<tr><th>No</th><th>Descript</th><th>Valeur</th><th>Prix</th><th>Qnt</th></tr>'

   for(i=0;i<a.length;i++) {
     tmp += '<tr>'
         +  '<td class="pNo">' + a[i].getAttribute('id') + '</td>'
         +  '<td class="descript">' + a[i].getAttribute('dsc') + '</td>'
         +  '<td align="right" class="valeur">' + getElmByTag(a[i],"valeur") + '</td>'
         +  '<td align="right" class="prix">' + getElmByTag(a[i],'prix') + '</td>'
         +  '<td align="center" class="qnt">' + getElmByTag(a[i],'totQnt') + '</td>'
         +  '</tr>'
   }

    return  tmp + '</table>';
}

function getBack(){
 with (mainTable.style){
    //if(display=="block")document.location='index.htm';
    if(display=="none") { // back to main level
      display="block"
      header.innerHTML=welcomeStr1;
      info.innerHTML='';
      prodTable.innerHTML='';
      }
      else document.location='index.htm';
    }
}


///// entry
function setArtPage(){
 //applyXSL('pageOut','artList.xml','artList.xsl')  // works...
 initAll();
 setArtTable();
}

///////////////navigation

function setCursor(){
   for(i=0;i<ids.length;i++){
      //if (i!=curBInd)  resetBook(i);
      if(ids[i]==this )  ind=i ;
      }

   with(ids[ind].style) {
       cursor="pointer";
       //color="white";
       textDecoration="underline";
   }
}

function outCursor(){
 for(i=0;i<ids.length;i++)
   if( ids[i]==this )  ind = i ;
 with(ids[ind].style) {
     cursor="default";
    // color="darkslateblue";
     textDecoration="none";
   }
// if (curBInd>-1) setSelected();
}



/////////////////////////////////////////////
/////////////////
//  init sequence

window.onload=initAll;
