var xmlDoc;
var file=0;
var tabIdx=-1;
var imgPath="images/thumbs/";
var xmlFiles=new Array();
var newWin;
var rnd=Math.random();
var moz = (typeof document.implementation != 'undefined') && 
	          (typeof document.implementation.createDocument != 'undefined');
var ie = (typeof window.ActiveXObject != 'undefined');

xmlFiles[0]="english_tours.xml";
xmlFiles[1]="scottish_tours.xml";
xmlFiles[2]="welsh_tours.xml";
xmlFiles[3]="irish_tours.xml";
xmlFiles[4]="european_tours.xml";
xmlFiles[5]="theatre.xml";
xmlFiles[6]="sports.xml";
xmlFiles[7]="concerts.xml";
	
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;
  var datelist="";
  var datesAdded=0;
  var matchDuration=0;
  var month=0;
  var tours=xmlDoc.getElementsByTagName("tours");
  var tour;
  var addCnt=0;

  if(minOpt != null){
    minSel=parseInt(minOpt.options[minOpt.selectedIndex].value);
    maxSel=parseInt(maxOpt.options[maxOpt.selectedIndex].value);
  } else {
    minSel=-1;
    maxSel=-1;
  }

  tbl=document.createElement('table');
  tbl.cellPadding="2";
  tbl.cellSpacing="0";
  tbl.style.width="90%";
  tbl.align="center";
  bdy=document.createElement('tbody');
  tbl.appendChild(bdy);
  
  tour=tours[0].getElementsByTagName("tour");				
  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(minSel == -1) minSel=0;
    if(maxSel == -1) maxSel=99;

    if(dates.length){		  
      aDate=dates[0].getElementsByTagName("date");			
      for(loop=0;loop<aDate.length; loop++){
        tmpDate=new Date(aDate[loop].firstChild.nodeValue);
        aDuration=parseInt(aDate[loop].getAttribute("duration"));

        if(monOpt.selectedIndex==0) 
          month=tmpDate.getMonth()+1;
        else 
          month=parseInt(monOpt.options[monOpt.selectedIndex].value);

        if((aDuration >= minSel) && (aDuration <= maxSel))
          matchDuration = 1;
        else
          matchDuration = 0;

        if((((tmpDate.getMonth()+1) == month) && (matchDuration)) && (tmpDate.getTime() > currentDate.getTime())){
          datesAdded++;
          if(datelist.length==0){
            datelist="<table width='80%' "+
                     "align='center' cellpadding=0 cellspacing=0 style='font-size:10pt;cursor: pointer; cursor: hand;'>";
          }		
          datelist=datelist+
                   "<tr><td style=\"font-size: 8pt;\">"+
                   tmpDate.toLocaleDateString()+"&#160;for&#160;"+aDuration+"&#160;days</td></tr>";
        }
      }			
      if(datesAdded>0) datelist=datelist+"</table>";
    }

    if(datesAdded>0){
      row=document.createElement('tr');
      col=document.createElement('td');
      if(image[0].firstChild)			
        col.innerHTML="<img border=1 src='images/thumbs/"+image[0].firstChild.nodeValue+"'>";
      else
        col.innerHTML="<img border=1 src='' alt='No Image'>";
      col.style.borderBottom="1px solid blue";
      col.style.Padding="0px"
      col.style.textAlign="center";			
      row.appendChild(col);

      col=document.createElement("td");
      col.innerHTML="<table style='font-size: 10pt;width:100%;' align='center' ><tbody>"+
                    "<tr><td><b><u>"+title[0].firstChild.nodeValue+"</a></u></b></td><tr>"+
                    "<tr><td style='font-size: 8pt;'>"+text[0].firstChild.nodeValue.substr(0,70)+
                    "...<br><br>"+
                    "<table id='t"+id+"' style='border: 1px solid green; font-size:10pt;"+
                    "width:98%;background-color:white;' onClick='openTour(this);'><tbody><tr>"+
                    "<td align=center>"+datelist+"</td><td align='right' style='cursor:pointer;"+
                    "cursor:hand;'><b>Click for more...</a></b></td></tr></tbody></table>"+
                    "</td></tr></tbody></table>";
      col.style.borderBottom="1px solid blue";
      col.style.Padding="0px"
      row.appendChild(col);
      bdy.appendChild(row);
      addCnt++; 
    }
  }
  if(addCnt>0)
    info.appendChild(tbl);
  else
    info.innerHTML="Sorry.  We didn't find any tours for this period.  Please try again.";
}

function initialTours(){
  var info=document.getElementById('info');
  if(tabIdx==-1)
    tabIdx=0;
  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.load(xmlFiles[0]);
  } 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;
  var x,y;

  url="tourinfo.html?t="+tour+"&f=";
  if(tabIdx<0)
    url=url+file;
  else
    url=url+tabIdx;
	
  newWin=window.open(url,"detailWin","width=600,height=500,addressbar=no,scrollbars=yes");  	
}	


function pickTour(){
  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 minSel, maxSel;
  var datelist="";
  var datesAdded=0;
  var matchDuration=0;
  var month=0;
  var tours=xmlDoc.getElementsByTagName("tours");
  var info=document.getElementById('info');
  var tour;
  var addCnt=0;
				
  tbl=document.createElement('table');
  tbl.cellPadding="2";
  tbl.cellSpacing="0";
  tbl.style.width="98%";
  tbl.style.border="1px solid blue";
  tbl.align="center";
	
  bdy=document.createElement('tbody');
  tbl.appendChild(bdy);
  
  tour=tours[0].getElementsByTagName("tour");				
  datesAdded=0;

  while(datesAdded==0){
    if(rnd > (tour.length-1)) rnd=tour.length-1;
    datelist="";
    id=tour[rnd].getAttribute("id");
    image=tour[rnd].getElementsByTagName("thumb");
    title=tour[rnd].getElementsByTagName("title");
    text=tour[rnd].getElementsByTagName("text");
    dates=tour[rnd].getElementsByTagName("dates");
    if(dates.length){		  
      aDate=dates[0].getElementsByTagName("date");			
      for(loop=0;loop<aDate.length; loop++){
        tmpDate=new Date(aDate[loop].firstChild.nodeValue);
        aDuration=parseInt(aDate[loop].getAttribute("duration"));					  	
        if(currentDate.getTime() <= tmpDate.getTime()){ 
          datesAdded++;
          if(loop==0){
            datelist="<table width='90%' align='center' cellpadding=0 cellspacing=0 style='font-size:10pt;"+
                     "cursor: pointer; cursor: hand;'>";
          }
          datelist=datelist+"<tr><td>"+tmpDate.toLocaleDateString()+"&#160;for&#160;"+aDuration+"&#160;days</td></tr>";
        }
      }			
      if(datesAdded>0) datelist=datelist+"</table>";
    }

    if(datesAdded>0){
      row=document.createElement('tr');
      col=document.createElement('td');			
      col.innerHTML="<img border=1 src='images/thumbs/"+image[0].firstChild.nodeValue+"'>";
      col.style.Padding="0px"
      col.style.textAlign="center";			
      row.appendChild(col);

      col=document.createElement("td");
      col.innerHTML="<table cellpadding=0 cellspacing=0 style='font-size: 10pt;width:100%;' align='center' >"+
                    "<tbody>"+
                    "<tr><td>"+
                    "<b><u>"+title[0].firstChild.nodeValue+"</u></b>"+
                    "</td><tr>"+
                    "<tr><td style='font-size: 8pt;'>"+
                    text[0].firstChild.nodeValue.substring(0,70)+
                    "...<br><br>"+
                    "<table id='t"+id+"'"+
                    " style='border: 1px solid green; font-size:10pt;width:98%;background-color:white;'"+
                    " onClick='openTour(this);'><tbody><tr>"+
                    "<td width='70%' align=center>"+datelist+"</td><td align='right' style='cursor:pointer;"+
                    "cursor:hand;'><b>Click for more...</a></b></td></tr></tbody></table>"+
                    "</td></tr></tbody></table>";
      col.style.Padding="0px"
      row.appendChild(col);
      bdy.appendChild(row);

      info.appendChild(tbl);
      addCnt++; 
    } else {
      rnd=Math.random();
      rnd=parseInt(String(rnd).substr(3,1));
    }		
  }
}


function randomTour(){
  rnd=Math.random();
  rnd=rnd*10;
  rnd=Math.round(rnd);
  if(moz){
    xmlDoc=document.implementation.createDocument("", "doc", null);        
    xmlDoc.onload = pickTour;
  } else if (ie) {
    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async = false;
    xmlDoc.onreadystatechange=function () {	if (xmlDoc.readyState == 4) pickTour();	};
  }
  while((file<1 ) || (file > 4)){
    file=Math.random();
    file=file*10;
    file=Math.round(file);
    if(file>4){
      file=file%4;
    }
  }
  xmlDoc.load(xmlFiles[file]);  		
}
