/*  Hibrid Web Services
 *  2007 (c) A.J.N. van Breemen
/*--------------------------------------------------------------*/

/*
 * AJAX functionality
 */
// global variables to keep track of the request
// and the function to call when done
var ajaxreq=false, ajaxCallback;

// ajaxRequest: Sets up a request
function ajaxRequest(filename) {
   try {
    // Firefox / IE7 / Others
    ajaxreq= new XMLHttpRequest();
   } catch (error) {
    try {
      // IE 5 / IE 6
      ajaxreq = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (error) {
      return false;
    }
   }
   try {
      ajaxreq.open("GET", filename, true);
      ajaxreq.onreadystatechange = ajaxResponse;
      ajaxreq.send(null);
   } catch (error) {
      alert( "Oops, AJAX error while sending GET request." );
   }
}

// ajaxResponse: Waits for response and calls a function
function ajaxResponse() {
   if (ajaxreq.readyState !=4) return;
   if (ajaxreq.status==200) {
      // if the request succeeded...
      if (ajaxCallback) ajaxCallback();
   } else alert("Request failed: " + ajaxreq.statusText);
   return true;
}


function sendMessage() {
   var http = null;
   if(window.XMLHttpRequest)
      http = new XMLHttpRequest();
   else if (window.ActiveXObject)
      http = new ActiveXObject("Microsoft.XMLHTTP");

   http.onreadystatechange = function() {
      if(http.readyState == 4) {
         //alert("Server Response Was: " + http.responseText);
	  }
   };

   http.open('POST', 'setmsg.php', true);
   http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
   var data = "id=" + encodeURIComponent( document.getElementById('id').value ) +
              "&msg=" + encodeURIComponent( document.getElementById('msg').value );
   //alert( "Check this: " + data );
   http.send( data );
   alert( "Thank you!" );
}

/*
 * Create PHIBRID namespace
 */ 
var PHIBRID = (function() {

   // MessageBox class
   // Friend can send messages to your TextDisplays
   function MessageBox( id, title ) {
      document.write( "<table cellpadding=2 style='font-family: arial; font-variant: small-caps; font-size: 12; font-weight: bold' border='0' cellspacing='0' bgcolor='#ffff00' width='180'>" );
      document.write( "<tr><td align='center' style='color: #FFFFFF; background-color: #53418D' height='20'>Phibrid Text</td></tr>" );
      document.write( "<tr><td style='font-family: arial; font-variant: normal; font-size: 12; font-weight: normal; color: #53418B; background-color: #D7EBCF'>" );
      document.write( title + "<center><form style='margin: 4' action='http://www.personalrobotics.nl/phibrid/setmsg.php' method='POST'><input type=hidden name='id' id='id' value='" + id + "'><input type=text name='msg' id='msg' size='44'><br><input style='border: 1px solid; font-family: arial; font-variant: small-caps; font-size: 12; font-weight: bold' type=button name='submit' value='Send message' onClick='sendMessage();'></form></center>" );
      document.write( "</table>" );
   } 
   
   // Display Phibrid
   // TextDisplay to show messages from friends
   function TextDisplay( id ) {
      document.write( "<table cellpadding=2 style='font-family: arial; font-variant: small-caps; font-size: 12; font-weight: bold' border='0' cellspacing='0' bgcolor='#ffff00' width='180'>" );
      document.write( "<tr><td align='center' style='color: #FFFFFF; background-color: #53418D' height='20'>Phibrid Text</td></tr>" );
      document.write( "<tr><td style='font-family: arial; font-variant: normal; font-size: 12; font-weight: normal; color: #53418B; background-color: #D7EBCF'>" );
      document.write( "<iframe frameborder='0' scrolling='no' height='15' marginheight='0' src='http://www.personalrobotics.nl/phibrid/getmsg.php?id=" + id + "'></iframe></td></tr>" );
      document.write( "</table>" );
   }

   // Website visitor alert Phibrid
   // Put this Phibrid on your website and it sends a message to you
   // each time your page is visited
   function VisitorAlert( id ) {
      document.write( "<iframe style='visibility: hidden' height='0' width='0' src='http://www.personalrobotics.nl/phibrid/setalert.php?id=" + id + "'></iframe>" );
   }
   
   return {
      MessageBox : MessageBox,
	  TextDisplay : TextDisplay,
	  VisitorAlert : VisitorAlert
   }
})();


