var allowajax=true;
var showbooking=false;
var showonlinett=true;



//---------------TIMETABLE
function getXMLtimetable(s,d,m,y){
 if(allowajax){
  var Vars={
   sailing:s.ajaxCleanString(),
   day:d,
   month:m,
   year:y};
  //for(x in Vars)alert(x+", "+eval("Vars."+x));
  
  getObject("timetableDiv").innerHTML='<div id="ttpleasewaitDiv"><p>Please Wait</p><img src="graphics/loading.gif" alt="please wait, retrieving timetable details" class="wait" /><p class="cancel"><a href="javascript:canceltt()">cancel loading</a></p><p>Retrieving timetable details...</p></div>';
  getStyle("timetableDiv").display="block";
  ajaxCaller.postForXML("ofsailingsXML.asp",Vars,onXMLgettimetable);
 }
}
function canceltt(){
 getStyle("timetableDiv").display="none";
 getObject("timetableDiv").innerHTML='';
}
function onXMLgettimetable(xml, headers, callingContext){
 try{
  var xmlcheck=xml.getElementsByTagName("ofrecords")[0].firstChild;
 }catch(e){
  return true;
 }
 var err=nValue(xml,"error");

 if(getObject("timetableDiv").innerHTML==''){
  return false;
 }else if(err=="true"){
  var st='<h1>Timetable Details</h1>';
  st+="<h2>Outward and Return Sailings</h2>";
  st+='<p>No details are available for these sailings.</p>';
  st+='<p>Please view our <a href="timetables.php">published timetables</a>.</p>';
  getObject("timetableDiv").innerHTML=st;
 }else{
  var st='<h1>Timetable Details</h1>';
  var o=nValue(xml,"outward");
  var r=nValue(xml,"return");
  var n=nValue(xml,"nextdate");
  var p=nValue(xml,"previousdate");
  var d=getObject("date_onlinett_cal").value;
  var nodetails=0;
  osplit=o.split("|");
  rsplit=r.split("|");
  var ocheck=(osplit.length==1 && osplit[0].indexOf("No Details")>-1);
  var rcheck=(rsplit.length==1 && rsplit[0].indexOf("No Details")>-1);
 
  //CHECK FOR NO DETAILS AT ALL
  if(ocheck && rcheck){
   st+="<h2>Outward and Return Sailings</h2>";  
   st+='<p>No details are available for these sailings.</p>';
   st+='<p>Please view our <a href="timetables.php">published timetables</a>.</p>';
   getObject("timetableDiv").innerHTML=st;
  }else{
  
   //CHECK FORMATTING - OUTWARD
   st+="<h2>Outward Sailings</h2>";  
   if(ocheck){
    st+='<p>No outward details are available for this sailing.</p>';
    st+='<p>Please view our <a href="timetables.php">published timetables</a>.</p>';
    nodetails=1;
   }else{
    st+=showsailingtimes(osplit,d);
   }
   
   //DISPLAY PREV/NEXT
   if(p!="NA" || n!="NA"){
    st+='<table id="ttpnDiv"><tr><td class="p">';
    if(p!="NA"){
     st+='<input type="button" name="psailing" id="psailing" value="< Prev Sailing" onclick="npsailing(\''+p+'\')" />';
    }else{
     st+='&nbsp;';
    }
    st+='</td><td class="n">';
    if(n!="NA"){
     st+='<input type="button" name="nsailing" id="nsailing" value="Next Sailing >" onclick="npsailing(\''+n+'\')" />';
    }else{
     st+='&nbsp;';
    }
    st+='</td></tr></table>';
   }
   
   //CHECK FORMATTING - RETURN
   st+="<h2>Return Sailings</h2>";
   if(rcheck){
    st+='<p>No return details are available for this sailing.</p>';
    if(nodetails==0)st+='<p>Please view our <a href="timetables.php">published timetables</a>.</p>';
   }else{
    st+=showsailingtimes(rsplit,d);
   } 
  }
  getObject("timetableDiv").innerHTML=st;
 }
}
function zpad(t){
 t+="";
 z="0000";
 return z.substr(0,4-t.length)+t;
}
function showsailingtimes(a,d){
 var st='<table class="ttdetailstbl"><thead><tr><th>Date</th><th>From</th><th>To</th><th>Duration</th><th>&nbsp;</th></tr></thead><tbody>';
 var oldd=newd='';

 for(var i=0; i<a.length; i++){
  ns=a[i].split("#");
  st+='<tr><td>';
  
  newd=ns[0];
  if(newd.indexOf("No Sailings")>-1){
   var m=(i==(a.length-1))?'Timetable Details Not Yet Published<br />Please contact Orkney Ferries':'No Sailings Scheduled For Today';
   st+=d+'</td><td colspan="3" class="ns">'+m+'</td><td>&nbsp;</td>';
  }else{
   if(oldd!=newd){
    st+=newd;
    oldd=newd;
   }else{
    st+='&nbsp;';
   }
   var t1=zpad(ns[1]);
   var t2=zpad(ns[2]);
   var v=ns[3].toLowerCase();
   v=(v.indexOf("via")>-1)?"Via Sailing":"&nbsp;";
   st+='</td><td>'+t1+'</td><td>'+t2+'</td><td>'+calcduration(t1,t2)+'</td><td>'+v+'</td>';   
  }
  st+='</tr>';
 }
 st+='</tbody></table>';
 return st;
}
function calcduration(t1,t2){
 var h1=(parseInt(t1.substr(0,2),10)*60)+parseInt(t1.substr(2,2),10);
 var h2=(parseInt(t2.substr(0,2),10)*60)+parseInt(t2.substr(2,2),10);
 var dur=h2-h1;
 var m=dur%60;
 var h=(dur-m)/60;
 return parseInt(h,10)+'h '+m+'m';
}





//------------------QUICK PRICE

function getquickXMLprice(){
 if(allowajax){
  var route=ddv("island");
  var sailing=ddv("sailing"); 
  if(route!="-1" && sailing!="-1"){
   var Vars={
    route:route.ajaxCleanString(),
    sailing:sailing.ajaxCleanString()};
   //for(x in Vars)alert(x+", "+eval("Vars."+x));
   getObject("quickpriceResults").innerHTML='<div id="homeloading"><p>Please Wait</p><img src="graphics/loading.gif" alt="please wait, retrieving price details" class="wait" /><p class="cancel"><a href="javascript:cancelqp()">cancel loading</a></p><p>Retrieving price details...</p></div>';
   hsquickpriceResults(1);
   ajaxCaller.postForXML("getquickXMLprice.asp",Vars,onXMLgetquickXMLprice);
   
   //ALSO VIEW TIMETABLES
   if(showonlinett && ddv("island_onlinett")){
    setquicktt();
   }
  }
 }
}
function cancelqp(){
 getStyle("quickpriceResults").display="none";
 getObject("quickpriceResults").innerHTML='';
}
function onXMLgetquickXMLprice(xml, headers, callingContext){
 try{
  var xmlcheck=xml.getElementsByTagName("ofrecords")[0].firstChild;
 }catch(e){
  return true;
 }
 var err=nValue(xml,"error");
 var route=ddv("island").ajaxCleanString();
 var sailing=ddv("sailing").ajaxCleanString();
  
 if(getObject("quickpriceResults").innerHTML==''){
  return false;
 }else if(err=="true"){
  var st='<h1>Quick Price Results</h1>';
  st+="<p>Prices could not be retrieved. Please wait a few moments and try again.</p>";
  st+=bookingOptions(route,sailing);
  getObject("quickpriceResults").innerHTML=st;
 }else{
  var st='<h1>Quick Price Results</h1>';
  
  if(xmlcheck.nodeName=="adult"){
   st+='<p>The unit prices for '+ddt("sailing")+' sailings are:</p>';
   st+='<ul><li><b>Adult: </b>&pound;'+nValue(xml,"adult")+'</li>';
   st+='<li><b>Concession: </b>&pound;'+nValue(xml,"child")+'</li>';
   st+='<li><b>Infant: </b>&pound;'+nValue(xml,"infant")+'</li>';
   st+='<li><b>Car: </b>&pound;'+nValue(xml,"car")+'</li></ul>';
  }else{
   st+="<p>Prices could not be retrieved. Please wait a few moments and try again.</p>";
  }
  st+=bookingOptions(route,sailing);
  getObject("quickpriceResults").innerHTML=st;
  hsquickpriceResults(1);
  return true;
 }
}
function bookingOptions(r,s){
 var st="";
 if(showbooking)st+='<p>You can <a href="https://'+getObject("bookingpath").value+'web100.asp?route='+r+'&sailing='+s+'" target="_blank">Book Tickets</a> for this route if you are travelling with a car.<p>';
 if(showonlinett){
  if(ddv("island_onlinett")){
   st+='<p>View <a href="javascript:setquicktt()">Online Timetables</a> for this route.<p>';
  }else{
   st+='<p>View <a href="onlinetimetables.php?route='+r+'&sailing='+s+'">Online Timetables</a> for this route.<p>';
  }
 }
 return st;
}
function setquicktt(){
 setSelect("island_onlinett",ddv("island"));
 getttSailing(getObject("island_onlinett"));
 setSelect("sailing_onlinett",ddv("sailing"));
 getsailingtimes();
}



function nValue(xml,n){
 var v;

 try{
  v=xml.getElementsByTagName(n)[0];
 }catch(e){
  return "";
 }
 if(!v){
  return "error";
 }else{
  var w=v.firstChild.nodeValue;
  return (w=="null" || w=="")?"":w;
 }
}

