
// WO AJAX HTML Calendar version 1.0
// Copyright 2006 - Arnold
// Creator - Apurva Goswami

var isIE = false;
var req;


function processing(obj, calType){
   //obj.disabled = true;
   calStatusDIV = document.getElementById(calType+"PBStatusDIV");
   selectDatesDIV = document.getElementById(calType+"SelectDatesDIV");
   divStopSearch = document.getElementById(calType+"StopSearchDIV");
   //obj.style.backgroundColor="#9B92C1";
   obj.className='processing';
   obj.focus();
   if (calStatusDIV !=null) calStatusDIV.style.display="block";
   if (divStopSearch !=null) divStopSearch.style.display="block";
   if (selectDatesDIV !=null) selectDatesDIV.style.display="none";
}


function pollTaskmaster() {
   document.forms[0].submit();
}

function initRequest(url) {
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        isIE = true;
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    req.open("GET", url, true);
}

var calType;
var month;
var left; 
var right;
var flag;
var ajax;
var obj;
var stop;
function cal_searchAvail(_calType,date) {
  if (ajax!=null) {
    return;
  }
  calType = _calType;
  month = date.month;
  left = right = date.day;
  flag = false;
  stop = false;
  cal_searchRequest();
}

function cal_searchRequest() {
  flag = !flag;
  if (left == null && right == null) {
    document.forms[0].submit();
    return false;
  }
  day = (flag) ? left : right;
  obj = $(calType + '_' + pad(day,2) + '_' + pad(month,2));
  if (obj==null || obj.className == 'maynotbeused') {
    if (flag) left = null; else right = null;
    return cal_searchRequest();
  }
  if (obj.className != 'maybeused' && obj.className != 'seatavail' && left != right) {
    if (flag) left--; else right++;
    return cal_searchRequest();
  }
  processing(obj, calType);
  url = obj.getAttribute('url');
  url += '^' + document.Form.NoAdult.value + document.Form.NoChild.value + document.Form.NoInfant.value + document.Form.fromPort.value + document.Form.toPort.value;
  ajax = new Ajax.Request(
          url
          ,{
              asynchronous: true
              ,method: 'get' 
              ,onComplete: cal_handleRequest
          });
  return true;
}

function cal_handleRequest(request) {
  if (ajax==null) return;
  switch (request.responseText.substring(0,1)) {
    case 'Y':
      obj.className = 'seatavail';
      ajax = null;
      document.forms[0].submit();
      text = (calType == 'OUT') ? 'Departing' : 'Returning';
      text += ' on ' + day; 
      $(calType+"SelectDatesDIV").innerHTML = text;
      $(calType+"SelectDatesDIV").style.backgroundColor = '#74b841';
      $(calType+'SelectDatesDIV').style.fontWeight = 'bold';
      break;
    case 'E':
    case 'N':
      obj.className = 'seatnotavail';
      ajax = null;
      if (flag) left--; else right++;
      if (stop) {
        document.forms[0].submit();
      } else {
        result = cal_searchRequest();
      }
      break;
    default:
      break;
  }
  if (ajax == null) {
      calStatusDIV = document.getElementById(calType+"PBStatusDIV");
      selectDatesDIV = document.getElementById(calType+"SelectDatesDIV");
      divStopSearch = document.getElementById(calType+"StopSearchDIV");
      if (calStatusDIV !=null) calStatusDIV.style.display="none";
      if (divStopSearch !=null) divStopSearch.style.display="none";
      if (selectDatesDIV !=null) selectDatesDIV.style.display="block";
  }
}

/*
function cal_handleRequest(request) {
  if (ajax==null) return;
  switch (request.responseText.substring(0,1)) {
    case 'Y':
      obj.className = 'seatavail';
      ajax = null;
      document.forms[0].submit();
      break;
    case 'E':
    case 'N':
      obj.className = 'seatnotavail';
      //if (flag) left--; else right++;
	stop = true;
      if (stop) {
        document.forms[0].submit();
      } else {
        //result = cal_searchRequest();
      }
      break;
    default:
      break;
  }
  if (ajax == null) {
      calStatusDIV = document.getElementById(calType+"PBStatusDIV");
      selectDatesDIV = document.getElementById(calType+"SelectDatesDIV");
      divStopSearch = document.getElementById(calType+"StopSearchDIV");
      if (calStatusDIV !=null) calStatusDIV.style.display="none";
      if (divStopSearch !=null) divStopSearch.style.display="none";
      if (selectDatesDIV !=null) selectDatesDIV.style.display="block";
  }
}
*/

function cal_abort() {
  stop = true;
  calStatusDIV = document.getElementById(calType+"PBStatusDIV");
  selectDatesDIV = document.getElementById(calType+"SelectDatesDIV");
  divStopSearch = document.getElementById(calType+"StopSearchDIV");
//  if (calStatusDIV !=null) calStatusDIV.style.display="none";
//  if (divStopSearch !=null) divStopSearch.style.display="none";
//  if (selectDatesDIV !=null) selectDatesDIV.style.display="block";
}

function pad(number,length) 
{
    var str = '' + number;
    while (str.length < length)
        str = '0' + str;
    return str;
}



function ajaxStuff(obj, calType, _url) {
    var url = _url;
    url += '^' + document.Form.NoAdult.value + document.Form.NoChild.value + document.Form.NoInfant.value + document.Form.fromPort.value + document.Form.toPort.value;
    //alert(url);
    suspendCalButtons();
    processing(obj, calType);
    initRequest(url);
    req.onreadystatechange = processInitialRequest;
    req.send(null);
}

function suspendCalButtons(){
	for (var i = 0; i < document.forms.length; i++) {
		var curForm = document.forms[i];

		for (var j = 0; j < curForm.elements.length; j++) {
				var curElement = curForm.elements[j];
				//curElement.style.backgroundcolor = "red"; 
				curElement.disabled=true;
		}
	}
}

function processInitialRequest() {
    if (req.readyState == 4) {
        if (req.status == 200) {
            window.status = "";
        }
        setTimeout("pollTaskmaster()", 2000);
    }
}


// xp_progressbar
// Copyright 2004 Brian Gosselin of ScriptAsylum.com
//
// v1.0 - Initial release
// v1.1 - Added ability to pause the scrolling action (requires you to assign
//        the bar to a unique arbitrary variable).
//      - Added ability to specify an action to perform after a x amount of
//      - bar scrolls. This requires two added arguments.
// v1.2 - Added ability to hide/show each bar (requires you to assign the bar
//        to a unique arbitrary variable).

// var xyz = createBar(
// total_width,
// total_height,
// background_color,
// border_width,
// border_color,
// block_color,
// scroll_speed,
// block_count,
// scroll_count,
// action_to_perform_after_scrolled_n_times
// )

var w3c=(document.getElementById)?true:false;
var ie=(document.all)?true:false;
var N=-1;

function createBar(w,h,bgc,brdW,brdC,blkC,speed,blocks,count,action){
if(ie||w3c){
var t='<div id="_xpbar'+(++N)+'" style="visibility:visible; position:relative; overflow:hidden; width:'+w+'px; height:'+h+'px; background-color:'+bgc+'; border-color:'+brdC+'; border-width:'+brdW+'px; border-style:solid; font-size:1px;">';
t+='<span id="blocks'+N+'" style="left:-'+(h*2+1)+'px; position:absolute; font-size:1px">Processing...';
for(i=0;i<blocks;i++){
t+='<span style="background-color:'+blkC+'; left:-'+((h*i)+i)+'px; font-size:1px; position:absolute; width:'+h+'px; height:'+h+'px; '
t+=(ie)?'filter:alpha(opacity='+(100-i*(100/blocks))+')':'-Moz-opacity:'+((100-i*(100/blocks))/100);
t+='"></span>';
}
t+='</span></div>';
document.write(t);
var bA=(ie)?document.all['blocks'+N]:document.getElementById('blocks'+N);
bA.bar=(ie)?document.all['_xpbar'+N]:document.getElementById('_xpbar'+N);
bA.blocks=blocks;
bA.N=N;
bA.w=w;
bA.h=h;
bA.speed=speed;
bA.ctr=0;
bA.count=count;
bA.action=action;
bA.togglePause=togglePause;
bA.showBar=function(){
this.bar.style.visibility="visible";
}
bA.hideBar=function(){
this.bar.style.visibility="hidden";
}
bA.tid=setInterval('startBar('+N+')',speed);
return bA;
}}

function startBar(bn){
var t=(ie)?document.all['blocks'+bn]:document.getElementById('blocks'+bn);
if(parseInt(t.style.left)+t.h+1-(t.blocks*t.h+t.blocks)>t.w){
t.style.left=-(t.h*2+1)+'px';
t.ctr++;
if(t.ctr>=t.count){
eval(t.action);
t.ctr=0;
}}else t.style.left=(parseInt(t.style.left)+t.h+1)+'px';
}

function togglePause(){
if(this.tid==0){
this.tid=setInterval('startBar('+this.N+')',this.speed);
}else{
clearInterval(this.tid);
this.tid=0;
}}

function togglePause(){
if(this.tid==0){
this.tid=setInterval('startBar('+this.N+')',this.speed);
}else{
clearInterval(this.tid);
this.tid=0;
}}
