var xmlhttp;
var destination;
var aDomain = document.domain;

function setSourceID(aSourceID)
{
  if ( aSourceID )
  {
    set_cookie ( "sourceID", aSourceID, 30, '/' );
  }
}


function set_cookie ( name, value, days, path, domain, secure )
{
  var cookie_string = name + "=" + escape ( value );

  if ( days )
  {
    var expires =new Date();
    expires.setDate(expires.getDate()+days);
    cookie_string += "; expires=" + expires.toGMTString();
  }

  if ( path )
        cookie_string += "; path=" + escape ( path );

  if ( domain )
        cookie_string += "; domain=" + escape ( domain );
  
  if ( secure )
        cookie_string += "; secure";
  
  //alert ( cookie_string );
  document.cookie = cookie_string;
}


function get_cookie ( cookie_name )
{
  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

  if ( results )
    return ( unescape ( results[2] ) );
  else
    return null;
}




function getBookingAvailability(aTourCode, aDepPoint, aPaxNum)
{
//document.getElementById('paymentbutton').style.display='none';

destination = document.getElementById('bookingAvailability');
document.getElementById('submit1').disabled=true;

//if (aTourCode.length==0 || aPaxNum.length==0)
if (aTourCode.length==0 || aDepPoint.length==0 || aPaxNum.length==0)
  {
  document.getElementById('bookingAvailability').innerHTML="<h3>Please select a departure point</h3>";
  return;
  }
depArr = aDepPoint.split("|");
aDepPoint = depArr[0];
  document.getElementById('bookingAvailability').innerHTML="<span><img src='/_client/scripts/core/ajaxlayer/loading.gif'/> </span> <span> Checking Availablity... Please Wait</span>";
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Your browser does not support XMLHTTP!");
  return;
  }
var url="https://"+aDomain+"/_client/scripts/core/phplayer/getBookingAvailability.php";
url=url+"?tourCode="+aTourCode;
url=url+"&depPoint="+aDepPoint;
url=url+"&paxNum="+aPaxNum;
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("POST",url,true);
xmlhttp.send(null);
}


function getRooms(aTourID, aNumPax)
{
aTourID = aTourID.split('|');
aTourID = aTourID[0];
destination = document.getElementById('rooms');
document.getElementById('submit2').disabled=true;
if (aTourID.length==0 || aNumPax.length==0)
  {
  document.getElementById('rooms').innerHTML="<h3>Please select a departure point</h3>";
  return;
  }
  document.getElementById('rooms').innerHTML="<span><img src='/_client/scripts/core/ajaxlayer/loading.gif'/> </span> <span> Checking Room Availablity... Please Wait</span>";
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Your browser does not support XMLHTTP!");
  return;
  }
var url="https://"+aDomain+"/_client/scripts/core/phplayer/getRooms.php";
url=url+"?tourID="+aTourID;
url=url+"&numPax="+aNumPax;
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("POST",url,true);
xmlhttp.send(null);
}

function verifyRoomAllocation(aNumPax,aNumRoomTypes){

  var aCalcAlloc = 0;
  //alert(aNumPax + ' ' + aNumRoomTypes);
  for($i=0;$i<aNumRoomTypes;$i++){
    allocRoomQty = document.getElementById('room'+$i).value.split("|");
    aCalcAlloc = aCalcAlloc + (parseInt(allocRoomQty[0]) * parseInt(document.getElementById('room'+$i+'capacity').value));
  }
  //alert(aNumPax + " " + aCalcAlloc);
  if(aNumPax==aCalcAlloc){
    //alert('true');
    document.getElementById('submit2').disabled=false;
  } else {
    //alert('false');
    document.getElementById('submit2').disabled=true;
  }
}



function makeBooking()
{
  tourIDs = document.getElementsByName('dateTime');
  for (i = 0; i < tourIDs.length; i++) {
    if (tourIDs[i].checked) { 
     tourID = tourIDs[i].value;
    }
  }
  tourID = tourID.split('|');
  tourID = tourID[0];

  depArr = document.getElementById('depPoint').value.split("|");
  airportID="";
  pickupID="";
  if(depArr[1]=="A"){airportID = depArr[0]} else {pickupID = depArr[0]}
  airportID = airportID;
  //sourceID = document.getElementById('sourceID').value;
  sourceID = get_cookie ( "sourceID" );

  //customerArr = '[';
  //customerArr = '{"customer": [';
  
  customerArr = '{"Title": "'+document.getElementById('title').value+'", "FirstName": "'+document.getElementById('firstName').value+'", "LastName": "'+document.getElementById('lastName').value+'", "EmailAddress": "'+document.getElementById('email').value+'", "TelephoneDay": "'+document.getElementById('phone').value+'", "TelephoneHome": "", "Address1": "'+document.getElementById('address1').value+ " " + document.getElementById('address2').value +'", "Address2": "'+ document.getElementById('city').value+'", "Address3": "'+document.getElementById('county').value+'", "PostCode": "'+document.getElementById('postcode').value+'", "AllowContact": "0"}';
    
  
  //customerArr += "]";  
  //customerArr += "]}";  
  //alert(customerArr );

  qtyRoomTypes = document.getElementById('qtyRoomTypes').value;

  x=-1;
  htlRoom = new Array();


  for(i=0;i<qtyRoomTypes;i++){
    
    selRoomData = document.getElementById('room'+i).value.split("|");
    allocQtyNumber = selRoomData[0];
    allocRoomID = selRoomData[1];  
    allocMaxOcc = selRoomData[2]; 
    
    if(allocRoomID){
      for(z=0;z<allocQtyNumber;z++){
      //if(){
        x++;
        htlRoom[x] = new Array(4);
        htlRoom[x][0] = x+1;
        htlRoom[x][1] = allocRoomID;
        htlRoom[x][2] = allocMaxOcc;
        htlRoom[x][3] = allocQtyNumber;
      

      }
    }
  }
  //alert(htlRoom.length);
  roomArr = '{"room" : [';
  for(i=0;i<htlRoom.length;i++){
    roomArr += '{"number": "'+htlRoom[i][0]+'", "RoomCode": "'+htlRoom[i][1]+'"}';
    if(i!=htlRoom.length-1){
      roomArr += ',';
    }
  }
  roomArr += "]}";  
  //alert(roomArr);

  curRoom = 1;
  roomAlloc = 0;

  qtyPax = document.getElementById('depPax').value;
  paxData = new Array();
  paxData[0] = new Array(8);
  paxData[0][0] = document.getElementById('title').value;
  paxData[0][1] = document.getElementById('firstName').value;
  paxData[0][2] = document.getElementById('lastName').value;
  paxData[0][3] = document.getElementById('insurance').checked;
  paxData[0][4] = document.getElementById('dobDay').value;
  paxData[0][5] = document.getElementById('dobMonth').value;
  paxData[0][6] = document.getElementById('dobYear').value;
  paxData[0][7] = curRoom;
  roomAlloc ++;
//alert(paxData[0][3]);
  for(i=1;i<qtyPax;i++){
      paxData[i] = new Array(8);
      x=i+1;
      paxData[i][0] = document.getElementById('addTitle'+x).value;
      paxData[i][1] = document.getElementById('addFirstName'+x).value;
      paxData[i][2] = document.getElementById('addLastName'+x).value;
      paxData[i][3] = document.getElementById('addInsurance'+x).checked;
      paxData[i][4] = document.getElementById('dobDay'+x).value;
      paxData[i][5] = document.getElementById('dobMonth'+x).value;
      paxData[i][6] = document.getElementById('dobYear'+x).value;
      
      //for(x=0;x<htlRoom.length;x++){
        //alert(htlRoom[curRoom-1][2]*htlRoom[curRoom-1][3]);
        if(htlRoom[curRoom-1][2]==roomAlloc){curRoom++; roomAlloc=0;}
        paxData[i][7]=curRoom;
        roomAlloc++;
      //}
  }
  //alert(paxData);

  paxArr = '{"pax" : [';
  for(i=0;i<qtyPax;i++){
    if(paxData[i][3]==false){paxData[i][3]=1;} else {paxData[i][3]=0;}

    paxArr += '{"Title": "'+paxData[i][0]+'", "FirstName": "'+paxData[i][1]+'", "LastName": "'+paxData[i][2]+'", "DOB": "'+paxData[i][6]+'-'+paxData[i][5]+'-'+paxData[i][4]+'", "Sex": "M", "Passport": "", "Nationality":"", "PickupID":"", "OwnInsurance":"'+paxData[i][3]+'","room":"'+paxData[i][7]+'" }';
    if(i!=qtyPax-1){
      paxArr += ',';
    }
  }
  paxArr += "]}";
  
  
//alert ("here1");
  document.getElementById('securePayment').style.display='none';
//alert ("here2");
  destination = document.getElementById('makingBooking');
//alert ("here3");
  destination.style.display='block';
//alert ("here4");
  document.getElementById('makingBooking').innerHTML="<span><img src='/_client/scripts/core/ajaxlayer/loading.gif'/> </span> <span> Processing Booking... Please Wait</span>";
//alert ("here5");
  xmlhttp=GetXmlHttpObject();
  if (xmlhttp==null)
  {
  alert ("Your browser does not support XMLHTTP!");
  return;
  }
  var url="https://"+aDomain+"/_client/scripts/core/phplayer/makeBooking.php";
  url=url+"?tourID="+tourID;
  url=url+"&airportID="+airportID;
  url=url+"&pickupID="+pickupID;
  url=url+"&sourceID="+sourceID;
  url=url+"&customerArr="+customerArr;
  url=url+"&roomArr="+roomArr;
  url=url+"&paxArr="+paxArr;
  xmlhttp.onreadystatechange=stateChanged;
  xmlhttp.open("POST",url,true);
  xmlhttp.send(null);


 }


function makePayment()
{
destination = document.getElementById('paymentInfo');
  document.getElementById('paymentSubmit').disabled=true;
  document.getElementById('paymentInfo').innerHTML="<span><img src='/_client/scripts/core/ajaxlayer/loading.gif'/> </span> <span> Processing Payment... Please Wait</span>";

aMode="Payment";
aCardHolder=document.getElementById('nameOnCard').value;
aHolderName = aCardHolder.split(" ");
aHolderFirstName = aHolderName[0];
//alert(aHolderFirstName);
aHolderSurname = aHolderName[aHolderName.length -1];
//alert(aHolderSurname);
aCardNumber=document.getElementById('cardNumber').value;
//aValid=document.getElementById('validFrom').value+document.getElementById('valYear').value;
aExpiry=document.getElementById('expMon').value+document.getElementById('expYear').value;
aCV2=document.getElementById('securityNumber').value;
//aBuildingNumber="";
//aBuildingAddress="";
aAddress1=document.getElementById('billAddress1').value;
aAddress2=document.getElementById('billAddress2').value;
aAddress3=document.getElementById('billCity').value;
aState=document.getElementById('billCounty').value;
aPostcode=document.getElementById('billPostcode').value;
aCountry=document.getElementById('billCountry').value;
aEmail=document.getElementById('email').value;
aCurrency="GBP";
aAmount=document.getElementById('paymentAmount').value;
aCardType=document.getElementById('cardType').value;
aReference=document.getElementById('reference').value;


xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Your browser does not support XMLHTTP!");
  return;
  }
//var url="https://"+aDomain+"/_client/scripts/core/phplayer/makePayment.php";
var url="https://"+aDomain+"/_client/scripts/core/phplayer/makeAuthNetPayment.php";
//url=url+"?mode="+aMode;
url=url+"?cardHolder="+aCardHolder;
url=url+"&cardNumber="+aCardNumber;
//url=url+"&valid="+aValid;
url=url+"&expiry="+aExpiry;
url=url+"&cv2="+aCV2;
//url=url+"&issue="+aIssue;
url=url+"&holderfirstname="+aHolderFirstName;
url=url+"&holdersurname="+aHolderSurname;
url=url+"&address1="+aAddress1;
url=url+"&address2="+aAddress2;
url=url+"&address3="+aAddress3;
url=url+"&state="+aState;
url=url+"&postcode="+aPostcode;
//url=url+"&country="+aCountry;
//url=url+"&currency="+aCurrency;
url=url+"&amount="+aAmount;
//url=url+"&cardType="+aCardType;
url=url+"&reference="+aReference;
url=url+"&email="+aEmail;
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("POST",url,true);
xmlhttp.send(null);

}




function stateChanged()
{

  if (xmlhttp.readyState==4 && destination)
  {
    destination.innerHTML=xmlhttp.responseText;
    //alert('here');

 


    if(destination==document.getElementById('makingBooking')){
      if(!destination.innerHTML.match('There was a problem processing the booking:')){
        destination.style.display='none';  
        //document.getElementById('paymentbutton').style.display='block';
        aDataString = destination.innerHTML.split('|');
        document.getElementById('securePayment').style.display='block';  
        document.getElementById('reference').value=aDataString[0];
        document.getElementById('paymentAmount').value=aDataString[2];
        document.getElementById('totalAmount').innerHTML=aDataString[3];
        document.getElementById('dueFrom').innerHTML=aDataString[2];
        document.getElementById('dueTo').innerHTML=aDataString[3];
        document.getElementById('nameOnCard').value=aDataString[1];
        document.getElementById('billAddress1').value=aDataString[4];
        document.getElementById('billCity').value=aDataString[5];
        document.getElementById('billCounty').value=aDataString[6];
        document.getElementById('billPostcode').value=aDataString[7];
        

//alert("1");
        //  document.getElementById('item_name_1').value = aDataString[0];
//alert(document.getElementById('item_name_1').value );
        //  document.getElementById('item_price_1').value = aDataString[2];
//alert(document.getElementById('item_price_1').value );

  
        
      }      
    }
    if(destination==document.getElementById('paymentInfo')){
      document.getElementById('paymentSubmit').disabled=false;
      
      if(destination.innerHTML.match('Payment Accepted... Please Wait, Redirecting...')){
        window.location='/index.php/book/response';
        //alert('complete');
      }
    }
    
  }

}

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}
