// JavaScript Document
//# =================================================================================================
//# function Name  : The functions below are for communications with the AJAX client
//# Purpose        : They are based on the code produced by the CGI::AJAX module 
//# Author         : 
//# Copyright      :  
//# Date created   :  
//# Date updated   : 
//#                
//# Script Usage   : 
//#
//# Comments       : 
//# =================================================================================================

var ajax = [];
function pjx(args,fname,method) { 
   this.target=args[1]; 
   this.args=args[0]; 
   method=(method)?method:'GET'; 
   if(method=='post'){method='POST';} 
   this.method = method; 
   this.r=ghr(); 
   this.url = this.getURL(fname);
}
   
function formDump(){ 
   var all = []; 
   var fL = document.forms.length; 
   for(var f = 0;f<fL;f++)
   { 
      var els = document.forms[f].elements; 
      for(var e in els)
      { 
         var tmp = els[e].id || els[e].name; 
         if(typeof tmp != 'string'){continue;} 
         if(tmp){ all[all.length]=tmp}
      } 
   } 
   return all;
}
   
function getVal(id) { 
   if (id.constructor == Function ) { return id(); } 
   if (typeof(id)!= 'string') { return id; } 
   var element = document.getElementById(id) || document.forms[0].elements[id]; 
   if(!element)
   { 
      alert('ERROR: Cant find HTML element with id or name: ' + id+'. Check that an element with name or id='+id+' exists'); 
      return 0; 
   } 
   if(element.type == 'select-one') { return element[element.selectedIndex].value } 
   if (element.type == 'select-multiple') 
   { 
      var ans = []; 
      var k =0; 
      for (var i=0;i<element.length;i++) 
      { 
         if (element[i].selected || element[i].checked ) { ans[k++]=element[i].value; } 
      } 
      return ans; 
   } 
   if(element.type == 'radio' || element.type == 'checkbox')
   { 
      var ans =[]; 
      var elms = document.getElementsByTagName('input'); 
      var endk = elms.length; 
      var i =0; 
      for(var k=0;k<endk;k++)
      { 
         if(elms[k].type== element.type && elms[k].checked && elms[k].id==id){ ans[i++]=elms[k].value; } 
      } 
      return ans; 
   } 
   if( element.value == undefined )
   { 
      return element.innerHTML; 
   }else{ 
      return element.value; 
   }
}

function fnsplit(arg) 
{ 
   var url=""; 
   if(arg=='NO_CACHE'){return '&pjxrand='+Math.random()} 
   if (arg.indexOf('__') != -1) 
   { 
      arga = arg.split(/__/); 
      url += '&' + arga[0] +'='+ escape(arga[1]); 
   } else { 
      var res = getVal(arg) || ''; 
      if(res.constructor != Array){ res = [res] } 
      for(var i=0;i<res.length;i++) 
      { 
         url += '&args=' + escape(res[i]) + '&' + arg + '=' + escape(res[i]); 
      } 
   } 
   return url;
}

pjx.prototype = { 
   send2perl : function(){ 
                            var r = this.r; 
                            var dt = this.target; 
                            this.pjxInitialized(dt); 
                            var url=this.url; 
                            var postdata; 
                            if(this.method=="POST")
                            { 
                               var idx=url.indexOf('?'); postdata = url.substr(idx+1); 
                               url = url.substr(0,idx); 
                            } 
                            r.open(this.method,url,true); ; 
                            if(this.method=="POST")
                            { 
                               r.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
                               r.send(postdata); 
                            } 
                            if(this.method=="GET"){ r.send(null); } 
                            r.onreadystatechange = handleReturn; 
                            }, 
   pjxInitialized : function(){}, 
   pjxCompleted : function(){}, 
   readyState4 : function(){ 
                              var rsp = unescape(this.r.responseText); 
                              /* the response from perl */ 
                              var splitval = '__pjx__'; 
                              /* to split text */ 
                              /* fix IE problems with undef values in an Array getting squashed*/ 
                              rsp = rsp.replace(splitval+splitval+'g',splitval+" "+splitval); 
                              var data = rsp.split(splitval); 
                              dt = this.target; 
                              if (dt.constructor != Array) { dt=[dt]; } 
                              if (data.constructor != Array) { data=[data]; } 
                              if (typeof(dt[0])!='function') 
                              { 
                                 for ( var i=0; i<dt.length; i++ ) 
                                 { 
                                    var div = document.getElementById(dt[i]); 
                                    if (div.type =='text' || div.type=='textarea' || div.type=='hidden' ) 
                                    { 
                                       div.value=data[i]; 
                                    } else{ 
                                       div.innerHTML = data[i]; 
                                    } 
                                 } 
                              } else if (typeof(dt[0])=='function') { 
                                 dt[0].apply(this,data); 
                              } 
                              this.pjxCompleted(dt); }, 
   getURL : function(fname) { 
                              var args = this.args; 
                              var url= 'fname=' + fname; 
                              for (var i=0;i<args.length;i++) { url=url + args[i]; } 
                              return url; }
};

handleReturn = function() { 
                             for( var k=0; k<ajax.length; k++ ) 
                             { 
                                if (ajax[k].r==null) { ajax.splice(k--,1); continue; } 
                                if ( ajax[k].r.readyState== 4) { ajax[k].readyState4(); ajax.splice(k--,1); continue; } 
                             }
                          };
                          
var ghr=getghr();
function getghr(){ 
   if(typeof XMLHttpRequest != "undefined") 
   { 
      return function(){return new XMLHttpRequest();} 
   } 
   var msv= ["Msxml2.XMLHTTP.7.0", "Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"]; 
   for(var j=0;j<=msv.length;j++)
   { 
      try { 
            A = new ActiveXObject(msv[j]); 
            if(A){ return function(){return new ActiveXObject(msv[j]);} } 
          } 
          catch(e) { } 
   } 
   return false;
}

function jsdebug(){ 
   var tmp = document.getElementById('pjxdebugrequest').innerHTML = "<br><pre>"; 
   for( var i=0; i < ajax.length; i++ ) 
   { 
      tmp += '<a href= '+ ajax[i].url +' target=_blank>' + decodeURI(ajax[i].url) + ' </a><br>'; 
   } 
   document.getElementById('pjxdebugrequest').innerHTML = tmp + "</pre>";
}


function SaveBooking() {
  var nogo=1;
  var args = SaveBooking.arguments;
  for( var i=0; i<args[0].length;i++ ) {
    args[0][i] = fnsplit(args[0][i]);
  }

  var l = ajax.length;
  ajax[l]= new pjx(args,"SaveBooking",args[2]);
  ajax[l].url = 'SaveBooking.pl?' + ajax[l].url;
//  alert(ajax[l].url);
  try{
     ajax[l].send2perl();
  } catch (err) {
     txt="There was an error saving booking data.\n\n"
     txt+="Error description: " + err.description + "\n\n"
     txt+="                 : "+ ajax[l].url +"\n\n"
     txt+="Page URL         : IBKAJAX.JS\n\n"
     txt+="Error location   : SaveBooking function\n\n"
     txt+="Click OK to continue.\n\n"
     alert(txt)
     document.getElementById('ClientBooking').style.visibility='hidden';
  }
//  jsdebug();
}

function SaveBooking_return() {

  try{

//    alert(arguments[0]);
    var arr = arguments[0].split("]|[");
    
    var input1 = arr[0]
    var input2 = arr[1];

    if (input1=="Error") 
    {

//            # part 1 = Error - this is a flag to the JS code to treat this data as an error
//            # part 2 = Application Name were error ocurred
//            # part 3 = Line number where error happended
//            # part 4 = Error Message (either ODBC or Perl )


       alert (arr[1]+"\n"+arr[2]+"\n"+arr[3]);
//       document.getElementById('ClientBooking').style.visibility='hidden';
       return 0;
    }

    if (input1=="ErrorMessage") 
    {

//       document.getElementById('ClientBooking').style.visibility='hidden';
//            # part 1 = Error - this is a flag to the JS code to treat this data as an error
//            # part 2 = Application Name were error ocurred
//            # part 3 = Line number where error happended
//            # part 4 = Error Message (either ODBC or Perl )


          alert ("A Problem has occured\n\n"+arr[3]);
//       document.getElementById('ClientBooking').style.visibility='hidden';
       return 0;
    }
    // do something and return results, or set HTML divs using
    // innerHTML
    
    alert(arr[0])
       document.getElementById('ClientBooking').style.visibility='hidden';

  } catch (err) {
     txt="There was an error saving lead data.\n\n"
     txt+="Error description: " + err.description + "\n\n"
     txt+="                 : "+ arguments[0] +"\n\n"
     txt+="Page URL         : IBK.JS\n\n"
     txt+="Error location   : SaveEdits_Return function\n\n"
     txt+="Click OK to continue.\n\n"
     alert(txt)
     document.getElementById('ClientBooking').style.visibility='hidden';
  }

}


function DeleteBooking() {
  var args = DeleteBooking.arguments;
  for( var i=0; i<args[0].length;i++ ) {
    args[0][i] = fnsplit(args[0][i]);
  }

  var l = ajax.length;
  ajax[l]= new pjx(args,"DeleteBooking",args[2]);
  ajax[l].url = 'DeleteBooking.pl?' + ajax[l].url;
//  alert(ajax[l].url);
  try{
     ajax[l].send2perl();
  } catch (err) {
     txt="There was an error saving booking data.\n\n"
     txt+="Error description: " + err.description + "\n\n"
     txt+="                 : "+ ajax[l].url +"\n\n"
     txt+="Page URL         : IBKAJAX.JS\n\n"
     txt+="Error location   : DeleteBooking function\n\n"
     txt+="Click OK to continue.\n\n"
     alert(txt)
  }
//  jsdebug();
}

function DeleteBooking_return() {

  try{

//    alert(arguments[0]);
    var arr = arguments[0].split("]|[");
    
    var input1 = arr[0]
    var input2 = arr[1];

    if (input1=="Error") 
    {

//            # part 1 = Error - this is a flag to the JS code to treat this data as an error
//            # part 2 = Application Name were error ocurred
//            # part 3 = Line number where error happended
//            # part 4 = Error Message (either ODBC or Perl )


       alert (arr[1]+"\n"+arr[2]+"\n"+arr[3]);
//       document.getElementById('ClientBooking').style.visibility='hidden';
       return 0;
    }

    if (input1=="ErrorMessage") 
    {

//       document.getElementById('ClientBooking').style.visibility='hidden';
//            # part 1 = Error - this is a flag to the JS code to treat this data as an error
//            # part 2 = Application Name were error ocurred
//            # part 3 = Line number where error happended
//            # part 4 = Error Message (either ODBC or Perl )


          alert ("A Problem has occured\n\n"+arr[3]);
//       document.getElementById('ClientBooking').style.visibility='hidden';
       return 0;
    }
    // do something and return results, or set HTML divs using
    // innerHTML
    
    alert(arr[0])
    document.location.href = sURL;

//       document.getElementById('ClientBooking').style.visibility='hidden';

  } catch (err) {
     txt="There was an error saving lead data.\n\n"
     txt+="Error description: " + err.description + "\n\n"
     txt+="                 : "+ arguments[0] +"\n\n"
     txt+="Page URL         : IBK.JS\n\n"
     txt+="Error location   : DeleteBooking_Return function\n\n"
     txt+="Click OK to continue.\n\n"
     alert(txt)
//     document.getElementById('ClientBooking').style.visibility='hidden';
  }

}
