var xml;
var offerDetails=new Array();
var priorSeason="";


function hideInfo(){
  var infoPnl=document.getElementById('fullInfo');
  infoPnl.style.visibility="hidden";
}

  
function showOfferDetail(item){  
  var infoPnl=document.getElementById('fullInfo');
  var title=document.getElementById('hdr');
  var headerPnl=document.getElementById('sub');
  var detail=document.getElementById('detail');
  var hCenter=document.body.clientWidth/2;
  var vCenter=document.body.clientHeight/2;
  var org;

  infoPnl.style.visibility="visible";
  if(ie){
    org=event.srcElement.id.substring(5);  
  } else {
    org=item.target.id.substring(5);  
  }
  infoPnl.style.top=(vCenter-120)+"px";
  infoPnl.style.left=(hCenter-250)+"px";
  title.innerHTML=offerDetails[org].offerTitle;
  headerPnl.innerHTML=offerDetails[org].offerHeading;
  detail.innerHTML=offerDetails[org].offerText;
}


function addContainerTbl(itm, parent, clickable, tagLine, subHeading){
  var tbl, bdy, row, col;
  var bg;

  if((itm % 2) > 0 ){
    bg="#ddffdd";
  } else {
    bg="#ddddff";
  }
 
  tbl=document.createElement('table');
  tbl.cellPadding="0";
  tbl.cellSpacing="0";
  tbl.style.width="100%";
  tbl.align="left";
  tbl.style.backgroundColor=bg;
  bdy=document.createElement('tbody');
  tbl.appendChild(bdy);
  tbl.cellSpacing="6"

  row=document.createElement('tr');
  row.id="offer"+itm;
  col=document.createElement('td');
  col.style.width="5%";
  col.id="stars"+itm;
  col.innerHTML="<img src=\"images/bullets/stars.gif\" align=\"absmiddle\">";
  if(clickable==1) col.onclick=showOfferDetail;
  row.appendChild(col);

  col=document.createElement('td');
  col.id="catch"+itm;
  col.style.height="45px";
  col.style.fontFamily="Arial";
  col.style.textAlign="left";
  col.style.fontSize="14pt";
  if(clickable==1){
    col.style.width="85%";
    col.onclick=showOfferDetail;
  }else{
    col.style.width="95%";
    col.colSpan="2";
  }

  if(subHeading.length){
    col.innerHTML="<b><u>"+tagLine+"</u></b><br>"+subHeading;
  } else {
    col.innerHTML="<b>"+tagLine+"</b>";
  }
  
  row.appendChild(col);
  if(clickable==1){
    col=document.createElement('td');
    col.id="newwn"+itm;
    col.innerHTML="<input type=\"button\" style=\"width:100%; height:100%;background-Color: #4041fe;color:white;font-weight:bold;\" id=\"morec"+
                  itm+"\" value=\"More...\">";
    col.width="10%";
    col.onclick=showOfferDetail;
    row.appendChild(col);
  }
  
  if(clickable==1){
    row.onclick=showOfferDetail;
    if(ie){
      tbl.style.cursor="hand";
    } else {
      tbl.style.cursor="pointer";
    }
  }

  bdy.appendChild(row);

  parent.appendChild(tbl);
  return tbl;
}


function createDepositTable(mainTbl){
  var tbl,dby,row,col;

  tbl=document.createElement('table');
  tbl.style.width="580px";
  tbl.cellPadding="0";
  tbl.cellSpacing="0";
  tbl.align="center";
  tbl.style.fontSize="10pt";
  bdy=document.createElement('tbody');

  row=document.createElement('tr');
  col=document.createElement('td');
  col.innerHTML="<p style=\"margin: 0px 60px 0px 60px;\"><B>Deposits</B><br>"+
                "<b>Accommodation Only</b><br>&#163;100 per week rental or part week, all accommodation except the Elite, "+
                "Super Phoenix and Medas 8 which are &#163;150.<b>Coach Deposits</b><br>&#163;60 per person<br><br>";
               // "<b>Early Deposits</b><br>Pay 50% of your deposit now and the remaining 50% by 28th February 2007.</p>";
  row.appendChild(col);
  bdy.appendChild(row);
  tbl.appendChild(bdy);

  mainTbl.appendChild(tbl);
}


function seasonClick(evt){
  var season;
  var itm;
  var lastTab;
 
  if(ie){
    itm=event.srcElement;
  } else {
    itm=evt.target;
  }
  season=itm.id.substring(1);

  lastTab=document.getElementById('s'+priorSeason);
  lastTab.style.backgroundColor="#ccffcc";
  lastTab.style.color="green";
  lastTab.style.borderBottom="1px solid green";
  
  itm.style.borderBottom="0px solid green";
  itm.style.backgroundColor="white";
  itm.style.color="blue";
  
  if(season==priorSeason)
    return;
  priorSeason=season;
  buildOffers(season);
}


function buildOffers(season){
  var tbl, bdy, row, col, mainTbl, innerTbl, parent;
  var mRow, mCol;
  var loop=0;
  var colData;

  if(offerDetails.length){
    mTable=document.getElementById('seasonoffers');
    mTable.innerHTML="";
    tbl=document.createElement('table');
    bdy=document.createElement("tbody");
    tbl.appendChild(bdy);
    mRow=document.createElement("tr");
    mTable.appendChild(tbl);
    for(loop=0;loop<offerDetails.length;loop++){
      if(offerDetails[loop].offerSeason==season){
        mCol=document.createElement('td');
        parent=addContainerTbl(loop,mCol,offerDetails[loop].offerClickable,offerDetails[loop].offerCatchLine,offerDetails[loop].offerHeading);
        mRow.appendChild(mCol);
        bdy.appendChild(mRow);
        mRow=document.createElement('tr');
      }
    }
  }
}


function readOffers(){  
  var tbl, bdy, row, col, mainTbl, innerTbl, parent;
  var mRow, mCol;
  var loop=0, offerLoop=0;
  var catchTag, title, subHeading, body;
  var currentDate=new Date();
  var tmpDate;
  var month=0;
  var offerData=xml.getElementsByTagName("offers");
  var seasonData=xml.getElementsByTagName("seasons");
  var season;
  var seasonLoop=0;
  var offer;
  var addCnt=0;
  var colData;
  var cCnt=0;
  var maxCols=0;
  var moreInfo;
  var currentSeason;
  var coach="<img src=\"images/bullets/stars.gif\" align=\"absmiddle\">";
  var accom="<img src=\"images/bullets/stars.gif\" align=\"absmiddle\">";
  
  var btn;
		
  mainTbl=document.getElementById('offerTable');
  mRow=document.createElement('tr');
  mainTbl.appendChild(mRow);
  mCol=document.createElement('td');
  cCnt=0;
  if(seasonData!=null){
    season=seasonData[0].getElementsByTagName("season");
    tbl=document.createElement("table");
    tbl.align="center";
    tbl.cellPadding="0";
    tbl.cellSpacing="0";
    tbl.style.width="740px";
    bdy=document.createElement("tbody");
    tbl.appendChild(bdy);
    row=document.createElement("tr");
    col=document.createElement("td");
    col.style.borderBottom="1px solid green";
    col.innerHTML="&#160;";
    col.style.width="4px";
    row.appendChild(col);
    for(seasonLoop=0;seasonLoop<season.length;seasonLoop++){
      col=document.createElement("td");
      col.style.borderLeft="1px solid green";
      col.style.borderTop="1px solid green";
      col.style.borderRight="1px solid green";
      col.style.width="80px";
      col.style.fontFamily="Arial";
      col.style.fontSize="16pt";
      col.id="s"+season[seasonLoop].firstChild.nodeValue;
      if(seasonLoop==0){
        col.style.backgroundColor="white";
        col.style.color="blue";
        currentSeason=season[seasonLoop].firstChild.nodeValue;
        priorSeason=currentSeason;
      } else {
        col.style.backgroundColor="#ccffcc";
        col.style.color="green";
        col.style.borderBottom="1px solid green";
      }
      if(ie)
        col.style.cursor="hand";
      else
        col.style.cursor="pointer";
      col.onclick=seasonClick;
      col.innerHTML="&#160;"+season[seasonLoop].firstChild.nodeValue;
      row.appendChild(col);
    }
    col=document.createElement("td");
    col.style.borderBottom="1px solid green";
    col.innerHTML="&#160;";
    if(season.length>1)
      col.style.width="572px";
    else
      col.style.width="652px";
    row.appendChild(col);
    col=document.createElement("td");
    col.style.borderBottom="1px solid green";
    col.innerHTML="&#160;";
    col.style.width="4px";
    row.appendChild(col);
    bdy.appendChild(row);

    row=document.createElement("tr");
    col=document.createElement("td");
    col.style.borderLeft="1px solid green";
    col.innerHTML="&#160;";
    row.appendChild(col);
    col=document.createElement("td");
    col.colSpan=String(season.length+1);
    col.id="seasonoffers";
    row.appendChild(col);
    col=document.createElement("td");
    col.innerHTML="&#160;";
    col.style.borderRight="1px solid green";
    row.appendChild(col);
    bdy.appendChild(row);

    row=document.createElement("tr");
    col=document.createElement("td");
    col.style.borderTop="1px solid green";
    col.innerHTML="&#160;";
    row.appendChild(col);

    col=document.createElement("td");
    col.style.borderTop="1px solid green";
    col.innerHTML="&#160;";
    col.colSpan=String(season.length+1);
    row.appendChild(col);

    col=document.createElement("td");
    col.style.borderTop="1px solid green";
    col.innerHTML="&#160;";
    row.appendChild(col);
    bdy.appendChild(row);
    mCol.appendChild(tbl);
    mRow.appendChild(mCol);
  }
  
  if(offerData != null){
    offer=offerData[0].getElementsByTagName("offer");
    for(offerLoop=0;offerLoop<offer.length;offerLoop++){
      catchTag=offer[offerLoop].getElementsByTagName("catchline");
      title=offer[offerLoop].getElementsByTagName("title");
      subHeading=offer[offerLoop].getElementsByTagName("subheading");
      body=offer[offerLoop].getElementsByTagName("text");
      offerDetails[offerLoop]=new offerClass();
      offerDetails[offerLoop].offerSeason=offer[offerLoop].getAttribute("season");
      offerDetails[offerLoop].offerCatchLine=catchTag[0].firstChild.nodeValue;
      offerDetails[offerLoop].offerClickable=catchTag[0].getAttribute("clickable");
      if(offerDetails[offerLoop].offerClickable == 1){
        offerDetails[offerLoop].offerTitle=title[0].firstChild.nodeValue;
      }
      if(subHeading[0].firstChild != null){
        offerDetails[offerLoop].offerHeading=subHeading[0].firstChild.nodeValue;
      }
      if(body[0].firstChild != null){
        offerDetails[offerLoop].offerText=body[0].firstChild.nodeValue;
      }
    }
  }
  buildOffers(currentSeason);
}


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

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