var xmlHttp;
var allLyrList=[];
var sort = 0 ;
var order = 0 ;

function Layer(name, id, type,fileName,visible,active,desc,lyrnew, ct,src){
   this.name = name;
   this.id = id;
   this.type = type;
   this.fileName = fileName;
   this.visible = visible;
   this.active = active;
   this.desc = desc;
   this.lyrnew = lyrnew;
   this.cate = ct;
   this.src = src;
}

function createXMLHttpRequest() {
    if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    }
}
function startRequest() {
    createXMLHttpRequest();
    xmlHttp.onreadystatechange = handleStateChange;
    xmlHttp.open("GET", "layer_desc.xml?"+new Date().getTime(), true);
    xmlHttp.send(null);
}
function handleStateChange() {
    if(xmlHttp.readyState == 4) {
        if(xmlHttp.status == 200) {
         showLayers();
        }
    }
}
function showLayers() {
   var countLyr=0;
//   var contTxt = '<table class="one" width=625><tr><th width=200><a href="#" style="color:white;" onClick="sortBy(0);return false;" title="click to sort this column">Layer Name</a></th><th width=70><a href="#" onClick="sortBy(1);return false;" title="click to sort this column" style="color:white;">Layer Type</a></th><th><a href="#" style="color:white;" onClick="sortBy(2);return false;" title="click to sort this column">Description</a></th>  <th><a href="#" style="color:white;" onClick="sortBy(3);return false;" title="click to sort this column">Category</a></th><th><a href="#" style="color:white;" onClick="sortBy(4);return false;" title="click to sort this column">Source</a></th></tr>';
   var contTxt = '<table class="one" width=625><tr><th width=200><a href="#" style="color:white;" onClick="sortBy(0);return false;" title="click to sort this column">Layer Name</a></th><th width=70><a href="#" onClick="sortBy(1);return false;" title="click to sort this column" style="color:white;">Layer Type</a></th><th><a href="#" style="color:white;" onClick="sortBy(2);return false;" title="click to sort this column">Description</a></th>  <th><a href="#" style="color:white;" onClick="sortBy(3);return false;" title="click to sort this column">Category</a></th><th><a href="#" style="color:white;" onClick="sortBy(4);return false;" title="click to sort this column">Date Added to IWRIS</a></th></tr>';
   var lyrs = xmlHttp.responseXML.getElementsByTagName("Layer");
   var lyr,dataSets;
   for(var i = 0; i < lyrs.length; i++) {
       lyr = lyrs[i];
       lyrid =  lyr.childNodes(0).firstChild.text;
       lyrName = lyr.childNodes(1).firstChild.text;
       fileName = lyr.childNodes(2).firstChild.text;
       lyrtype =  lyr.childNodes(3).firstChild.text;
       lyrdesc =  lyr.childNodes(4).firstChild.text;
       lyrnew =  lyr.childNodes(5).firstChild.text;
       lyrCate =  lyr.childNodes(6).firstChild.text;
       lyrSrc =  lyr.childNodes(7).firstChild.text;
       lyrDate =  lyr.childNodes(8).firstChild.text;
       lyrdesc = lyrdesc.replace(/%20/g," ");
       lyrdesc = lyrdesc.replace(/%27/g,"'");
       lyrdesc = lyrdesc.replace(/%28/g," ");
       lyrdesc = lyrdesc.replace(/%29/g," ");
       visible = 1 ;
       active = 1;
       allLyrList[i] = new Layer(lyrName, lyrid, lyrtype,fileName,visible,active,lyrdesc,lyrnew,lyrCate,lyrDate);
   }
   if (sort==0)
       allLyrList = allLyrList.objSort("name",order);
   else if (sort==1)
       allLyrList = allLyrList.objSort("type",order);
   else  if (sort==2)
       allLyrList = allLyrList.objSort("desc",order);
   else if (sort==3)
       allLyrList = allLyrList.objSort("cate",order);
   else  if (sort==4)
       allLyrList = allLyrList.objSort("src",order);
   for(var i = 0; i < lyrs.length; i++) {
      if (show=='all') {
       if (i%2==1)
           contTxt += '<tr onmouseover="this.style.backgroundColor=\'#CAFF70\';" onmouseout="this.style.backgroundColor=\'\';" class="odd"><td>'+allLyrList[i].name+'</td><td>'+allLyrList[i].type+'</td><td>'+allLyrList[i].desc+'</td><td>'+allLyrList[i].cate+'</td><td>'+allLyrList[i].src+'</td></tr>';
//           contTxt += '<tr onmouseover="this.style.backgroundColor=\'#CAFF70\';" onmouseout="this.style.backgroundColor=\'\';" class="odd"><td>'+allLyrList[i].name+'</td><td>'+allLyrList[i].type+'</td><td>'+allLyrList[i].desc+'</td><td>'+allLyrList[i].cate+'</td></tr>';
       else
           contTxt += '<tr onmouseover="this.style.backgroundColor=\'#CAFF70\';" onmouseout="this.style.backgroundColor=\'\';"><td>'+allLyrList[i].name+'</td><td>'+allLyrList[i].type+'</td><td>'+allLyrList[i].desc+'</td><td>'+allLyrList[i].cate+'</td><td>'+allLyrList[i].src+'</td></tr>';
//           contTxt += '<tr onmouseover="this.style.backgroundColor=\'#CAFF70\';" onmouseout="this.style.backgroundColor=\'\';"><td>'+allLyrList[i].name+'</td><td>'+allLyrList[i].type+'</td><td>'+allLyrList[i].desc+'</td><td>'+allLyrList[i].cate+'</td></tr>';
      }else {
         if(allLyrList[i].lyrnew==1) {
           countLyr++;
           if (countLyr%2==1)
              contTxt += '<tr onmouseover="this.style.backgroundColor=\'#CAFF70\';" onmouseout="this.style.backgroundColor=\'\';" class="odd"><td>'+allLyrList[i].name+'</td><td>'+allLyrList[i].type+'</td><td>'+allLyrList[i].desc+'</td><td>'+allLyrList[i].cate+'</td><td>'+allLyrList[i].src+'</td></tr>';
//              contTxt += '<tr onmouseover="this.style.backgroundColor=\'#CAFF70\';" onmouseout="this.style.backgroundColor=\'\';" class="odd"><td>'+allLyrList[i].name+'</td><td>'+allLyrList[i].type+'</td><td>'+allLyrList[i].desc+'</td><td>'+allLyrList[i].cate+'</td></tr>';
           else
              contTxt += '<tr onmouseover="this.style.backgroundColor=\'#CAFF70\';" onmouseout="this.style.backgroundColor=\'\';"><td>'+allLyrList[i].name+'</td><td>'+allLyrList[i].type+'</td><td>'+allLyrList[i].desc+'</td><td>'+allLyrList[i].cate+'</td><td>'+allLyrList[i].src+'</td></tr>';
//              contTxt += '<tr onmouseover="this.style.backgroundColor=\'#CAFF70\';" onmouseout="this.style.backgroundColor=\'\';"><td>'+allLyrList[i].name+'</td><td>'+allLyrList[i].type+'</td><td>'+allLyrList[i].desc+'</td><td>'+allLyrList[i].cate+'</td></tr>';
         }
      }
   }
   contTxt += '</table>';
   document.getElementById('contTxt').innerHTML = contTxt;
 }

function sortBy(id) {
       sort = id ;
       order= order== 0 ? -1:0 ;
       startRequest();
}
function showOpt(type) {
       show = type;
       startRequest();
}

function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}