var cYear=new Date();
var coachDays=new Array();
var coachPickups=new Array();
var accResort;
var accViewed;
var currency;
var showName;
var showHdr;
var tabs=["rentTab", "rent2Tab"];

function readSeasons(){
  var pricing=priceXML.responseXML.getElementsByTagName("pricing");
  //var pricing=start[0].getElementsByTagName("pricing");
  var seasons=pricing[0].getElementsByTagName("season");
  var seasonLoop=0;
  var y=document.getElementById('year');
  var itm;
  var cdate=new Date();
  var tab;

  while(y.hasChildNodes())
    y.removeChild(y.firstChild);
 
 
  for(seasonLoop=0; seasonLoop<seasons.length; seasonLoop++){
    itm=document.createElement("option");
    itm.value=seasons[seasonLoop].getAttribute("id");
    itm.text=seasons[seasonLoop].getAttribute("id");
    tab=document.getElementById(tabs[seasonLoop]);
    tab.innerHTML=" "+seasons[seasonLoop].getAttribute("id") + "<br> Prices";
    if(document.all){
      y.options.add(itm);
    } else {
      y.appendChild(itm);
    } 
  }
}

function readPrices(){
  var pricing=priceXML.responseXML.getElementsByTagName("pricing");
  //var pricing=start[0].getElementsByTagName("pricing");
  var seasons=pricing[0].getElementsByTagName("season");
  var rate=parseFloat(pricing[0].getElementsByTagName("rate")[0].getAttribute("value"));
  var nightly=0;
  var resorts;
  var accom=pricing[0].getElementsByTagName("accommodation");
  var band;
  var sharing,sValue;
  var seasonLoop, rIdx, accIdx, rFound, accFound, bandLoop;
  var tIdx;
  var tBody, tRow, tCol;
  var rTab2=document.getElementById('rent2Tab');
  var seasonTxt;
  var numAcc;
  var accLoop=0;
  var bookAcc=document.getElementById('accRow');
  var accFld, accValue;

  sValue=accom[0].getAttribute("max");
  if(accom[0].getAttribute("id").substring(3)!="TEN")
    sValue=parseInt(sValue)-2;
  sharing=document.getElementById('ms1');
  sharing.innerHTML=sValue;
  sharing=document.getElementById('ms2');
  sharing.innerHTML=sValue;

  for(seasonLoop=0;seasonLoop<seasons.length;seasonLoop++){
    showHdr=1;
    
    //if(bookAcc!=null){
      bookAcc.innerHTML="<input type='hidden' id='acc' name='acc' value='"+accom[0].getAttribute('id')+"'>"+
                        accom[0].getAttribute('fullname');
    //} else {
    /*  accFld=document.getElementById('acc');
      accValue=document.createElement('option');
      accValue.value=accom[0].getAttribute('id');
      accValue.text=accom[0].getAttribute('fullname');
      if(accFld!=null){
        if(ie){
          accFld.options.add(accValue);
        } else {
          accFld.appendChild(accValue);
        }
        accFld.style.width='100%';
      }
    }*/
        

    band=seasons[seasonLoop].getElementsByTagName("band");
    tBody=document.getElementById('r'+(seasonLoop+1));
    if(showHdr==1){
      tRow=document.createElement('tr');
      tRow.style.backgroundColor="#ccccff";
      tRow.style.fontSize="8pt";
      tRow.style.fontWeight="bold";
      if(showName>0){
        tCol=document.createElement('td');
        tCol.innerHTML="&#160;";
        tRow.appendChild(tCol);
      }
      
      for(bandLoop=0; bandLoop<band.length; bandLoop++){
        tCol=document.createElement('td');
        tCol.innerHTML=band[bandLoop].getAttribute("range");
        tRow.appendChild(tCol); 
      }    
      tBody.appendChild(tRow);
    }

    tRow=document.createElement('tr');
    tRow.style.fontSize="8pt";
    tRow.style.textAlign="center";
    tRow.style.backgroundColor="white";
    if(showName>0){
      tCol=document.createElement('td');
      tCol.innerHTML=accom[0].getAttribute("fullname");
      tRow.appendChild(tCol);
    }
    for(bandLoop=0; bandLoop<band.length; bandLoop++){
      tCol=document.createElement('td');
      nightly=parseFloat(band[bandLoop].getAttribute("value"));
      if(parseInt(nightly)>0){
        if(currency==1){
          nightly=nightly*rate;
        }
        tCol.innerHTML=nightly.toFixed();
      } else {
        tCol.innerHTML="N/A";
      }
      tRow.appendChild(tCol); 
    }
    tBody.appendChild(tRow);
    showHdr=0;
  }
  if(seasons.length<2){
    rTab2.className='empty';
    rTab2.onClick=null;
    rTab2.innerHTML="&#160;";
  }
  showHdr=0;
}


function processCoachDetails(){
  if(priceXML.readyState==4 && priceXML.status==200){
    readSeasons();
    readPrices();
  }
}


function initPrices(res, acc, curr, show){
  var r=document.getElementById("res");
  var a=document.getElementById("acc");
  var loop=0;
  var url='/php/pricing.php?acccode='+acc;

  accResort=res;
  accViewed=acc;
  currency=curr;
  showName=show;
  
  if(r!=null) r.value=res;

  priceXML=new XMLHttpRequest();
  priceXML.onreadystatechange = processCoachDetails;
  priceXML.open('GET', url, true);
  priceXML.send(null);
}


function prepareDepartures(){
  if(moz){
    priceXML=document.implementation.createDocument("", "doc", null);        
    priceXML.onload = processCoachDetails;
  } else if (ie) {
    priceXML = new ActiveXObject("Microsoft.XMLDOM");
    priceXML.async = false;
    priceXML.onreadystatechange=function () {	if (priceXML.readyState == 4) processCoachDetails();	};
  }
  priceXML.load("/coachDetails.xml");
}


function maxDays(month){
  var lastDay=31;
  var testMonth=month;
  var dateTest=new Date();

  dateTest.setYear(cYear.getYear());
  
  dateTest.setMonth(testMonth);
  dateTest.setDate(lastDay);

  while(dateTest.getMonth() != month){
    lastDay--;
    dateTest.setYear(cYear.getYear());
    dateTest.setMonth(testMonth);
    dateTest.setDate(lastDay);
  }

  return lastDay;
}

function populatePickups(){
  var coachHoliday=document.getElementById('coach');
  var hYear=document.getElementById('year');
  var hDay=document.getElementById('day');
  var hMonth=document.getElementById('month');
  var pck=document.getElementById('pickup');
  var day;
  var itm;
  var hDate;

  hDate=new Date(parseInt(hYear.options[hYear.selectedIndex].value),
                 parseInt(hMonth.options[hMonth.selectedIndex].value),
                 parseInt(hDay.options[hDay.selectedIndex].value));

  day=daysOfWeek[hDate.getDay()];
  
  while(pck.hasChildNodes())
    pck.removeChild(pck.firstChild);

  for(loop=0;loop<coachPickups[day].length;loop++){
    itm=document.createElement("OPTION");
    itm.value=coachPickups[day][loop]['code'];
    itm.text=coachPickups[day][loop]['description'];
    if(document.all){
      pck.options.add(itm);
    } else {
      pck.appendChild(itm);
    }
  }
}

function populateDays(dateFld){
  var coachYear=document.getElementById('year');
  var dayField=document.getElementById('day');
  var pck=document.getElementById('pickup');
  var dayLoop=0;
  var dayItm;
  var tmpItm;
  var monthField=document.getElementById('month');
  var loop=0, max=0;
  var optTxt="";
  var loop=0;
  var lastDay=0;
  var expTxt="";

  lastDay=dayField.selectedIndex;
  if(dayField.options.length){
    dayItm=dayField.firstChild;
    while(dayItm){
      tmpItm=dayItm.nextSibling;
      dayField.removeChild(dayItm);
      dayItm=tmpItm;
    }
  }

  max=maxDays(monthField.options[monthField.selectedIndex].value);
  for(loop=0;loop<max;loop++){
    dayItm=document.createElement("OPTION");
    dayItm.value=loop+1;
    dayItm.text=loop+1;
    if(document.all){
      dayField.options.add(dayItm);
    } else {
      dayField.appendChild(dayItm);
    }
  }

  if(lastDay >= 0)
    dayField.selectedIndex=lastDay;
}


function pricingSetup(){
  var durationTxt=document.getElementById('duration');
  var currMonth=cYear.getMonth(); 
  var n=document.getElementById('nights');
  var years=document.getElementById('year');
  var m=document.getElementById('month');
  var loop=0;
  var dayItm;

  populateDays();
  while(n.hasChildNodes())
    n.removeChild(n.firstChild);
  durationTxt.innerHTML='<b>Nights&#160;</b>';
  for(loop=0;loop<21;loop++){
    dayItm=document.createElement('option');
    dayItm.value=loop+5;
    dayItm.text=loop+5;
    if(document.all){
      n.options.add(dayItm);
    } else {
      n.appendChild(dayItm);
    }
  }
  m.selectedIndex=currMonth-2;
}

function initBookings(){
  var opts;
  var dayFld=document.getElementById('day');

  pricingSetup();
}


function submitAccDetails(){
  var frm=document.getElementById('bookacc');
  var r=document.getElementById("res");
  var a=document.getElementById("acc");
  var y=document.getElementById('year');
  var m=document.getElementById('month');
  var d=document.getElementById('day');
  var p=document.getElementById('sp');
  var tstDate,currentDate=new Date();

  //if(c.checked){
  //  p.value=coachDayOfMonth[y.selectedIndex][m.selectedIndex][d.selectedIndex]['price'];
  //} else {
  p.value="0";
  //}

  tstDate=new Date(y.options[y.selectedIndex].value, m.options[m.selectedIndex].value, d.options[d.selectedIndex].value);
  if(tstDate.getTime()<currentDate.getTime()){
    alert("Sorry you have selected an invalid holiday date.\nPlease try again.");
    return;
  }
  frm.submit();
}
