var xmlDoc;
var imgPath="images/thumbs/";
var newWin;
var rnd=Math.random();
var moz = (typeof document.implementation != 'undefined') && 
          (typeof document.implementation.createDocument != 'undefined');
var ie = (typeof window.ActiveXObject != 'undefined');
	

function readXML(){  
  var tbl, bdy, row, col;
  var loop=0, tourloop=0;
  var dates;
  var image, title, text, days, dates, location, aDate, aDuration, id;
  var currentDate=new Date();
  var tmpDate;
  var minOpt=document.getElementById('min'), 
      maxOpt=document.getElementById('max'),
      monOpt=document.getElementById('mon');
  var minSel, maxSel, colCnt=0;
  var datelist="";
  var datesAdded=0;
  var matchDuration=0;
  var month=0;
  var tours=xmlDoc.getElementsByTagName("tours");
  var tour;
  var addCnt=0;
		
  tbl=document.createElement('table');
  tbl.cellPadding="2";
  tbl.cellSpacing="0";
  tbl.style.width="90%";
  tbl.align="center";
	
  bdy=document.createElement('tbody');
  tbl.appendChild(bdy);
  if(tours != null){
    tour=tours[0].getElementsByTagName("tour");

    row=document.createElement('tr');		  						
    for(tourLoop=0;tourLoop<tour.length;tourLoop++){	  	
      datesAdded=0;
      datelist="";
      id=tour[tourLoop].getAttribute("id");
      image=tour[tourLoop].getElementsByTagName("thumb");
      title=tour[tourLoop].getElementsByTagName("title");
      text=tour[tourLoop].getElementsByTagName("text");
      days=tour[tourLoop].getElementsByTagName("durations");
      dates=tour[tourLoop].getElementsByTagName("dates");			
									
      if(dates.length){		  
        aDate=dates[0].getElementsByTagName("date");			
        for(loop=0;loop<aDate.length; loop++){
          tmpDate=new Date(aDate[loop].firstChild.nodeValue);
				
          if(monOpt.selectedIndex==0)
            month=tmpDate.getMonth()+1;
          else
            month=parseInt(monOpt.options[monOpt.selectedIndex].value);
          if(((tmpDate.getMonth()+1) == month) && (currentDate.getTime() < tmpDate.getTime())){
          //if((tmpDate.getMonth()+1) == month){
            datesAdded++;
            if(datelist.length==0){
            datelist="<table width='98%' align='center' cellpadding=0 cellspacing=0 "+
                     "style='font-size:8pt;background-color:white;border-right: 1px solid green;'>";
            }		
            datelist=datelist+"<tr><td>"+tmpDate.toLocaleDateString()+"</td></tr>";
          }
        }			
        if(datesAdded>0) datelist=datelist+"</table>";
      }

      if(datesAdded>0){	  				
        col=document.createElement('td');
        col.style.borderBottom="1px solid blue";
        col.style.Padding="0px"
        col.style.textAlign="center";						
        col.innerHTML="<table style=\"width: 350px; height:155px;background-color:white;border: 2px double blue;\" "+
                      "align=center cellpadding=0 cellspacing=0>"+
                      "<tr><td align=right><table width='90%' align=center><tr><td style=\"border-right: 2px solid black;\" "+
                      " cellpadding=8>"+
                      "<img border=1 src='images/thumbs/"+image[0].firstChild.nodeValue+"'>"+
                      "</td><td>"+
                      "<table style=\"font-size: 8pt;width:90%;font-family: 'Arial';\" align='center' "+
                      "cellpadding=0 cellspacing=0>"+
                      "<tr><td style=\"font-family: 'Comic Sans MS';font-size:10pt;text-align:center;\"><b><u>"+
                      title[0].firstChild.nodeValue+"</a></u></b></td></tr>"+
                      "<tr><td>"+text[0].firstChild.nodeValue.substr(0,80)+"...</td></tr>"+
                      "<tr><td>"+
                      "<table id='t"+id+"' "+
                      "style='border: 1px solid green; font-size:10pt;width:98%;background-color:white;' "+
                      "onClick='openTour(this);' cellpadding=0 cellspacing=0><tbody><tr>"+
                      "<td width='70%' align=center>"+datelist+
                      "</td><td align='right' style='cursor:pointer; cursor:hand;'><b>more...</a></b></td></tr>"+
                      "</tbody></table>"+
                      "</td></tr></table></td></tr></table></td></tr></table>";
        col.style.borderBottom="1px solid blue";
        if(colCnt==1)
          col.style.borderLeft="1px solid blue";
        col.style.Padding="0px"
        row.appendChild(col);
        addCnt++;
        colCnt++; 
        if(colCnt>1){
          bdy.appendChild(row);
          row=document.createElement('tr');
          colCnt=0;
        }
      }
    }
    if(addCnt>0){
      if(colCnt>0){
        if(addCnt>1)
          col.style.borderRight="1px solid blue";
        bdy.appendChild(row);
      }
      info.appendChild(tbl);
    } else
      info.innerHTML="Sorry.  We were unable to find any theatre breaks during the period selected.";
  } else
    info.innerHTML="Sorry.  We were unable to find any theatre breaks during the period selected.";
}


function initialTours(){
  var info=document.getElementById('info');

  if(info.firstChild){
    info.removeChild(info.firstChild);
  }
	
  if(xmlDoc == null){	 	   
    if(moz){
      xmlDoc=document.implementation.createDocument("", "doc", null);        
      xmlDoc.onload = readXML;
    } else if (ie) {
      xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
      xmlDoc.async = false;
      xmlDoc.onreadystatechange=function () {	if (xmlDoc.readyState == 4) readXML();	};
      //xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
      //xmlDoc.async = false;
      //xmlDoc.onreadystatechange=function(){if (xmlDoc.readyState == 4)readXML();};
    }
    xmlDoc.load("theatre.xml");
  } else {
    readXML();
  } 
}


function changeTab(clickedTab){
  var newIdx=clickedTab.id.substring(1);
  var info=document.getElementById('info');
  if(info.firstChild){
    info.removeChild(info.firstChild);
  }

  document.getElementById('t'+tabIdx).className="unselectedTab";
  document.getElementById('t'+newIdx).className="selectedTab";
  tabIdx=parseInt(newIdx);
  xmlDoc.load(xmlFiles[tabIdx]);
}


function openTour(item){  
  var tour=item.id.substring(1);
  var url="theatreinfo.html?t="+tour;
  var x,y;	

  newWin=window.open(url,"bill","addressbar=no");  	
}	
