$(document).ready(function() {
   // For <a>'s with a class of blank, open in new window
   $("a.blank").click(function(){
      window.open(this.href);
      return false;
   });
   // Set focus to first form field on page load
   if (typeof formFocus == 'undefined') {
     $("input[type='text'],textarea").eq(0).focus();
   }
   // Replace all submit buttons with links
   $("input[type='submit']").each(function(el) {
      $(this).replaceWith('<a href="#" class="button" id="repl' + el + '">' + $(this).val()  + '</a>');
      $('#repl' + el).click(function() {
        // Call the submit function on the parent form
        $(this).parents('form').eq(0).submit();
      });
   });
   // Enable the hitting of return in forms to submit
   $('input').keypress(function(e) {
      if (e.which == 13) {
         $(this).parents('form').eq(0).submit();
         return false;
      }
   });
   // Initialise
   if (typeof disableIMCheck == 'undefined') wlrIMCheck.showMe();
   
   // Prevent session timeout
   setInterval("$.get('refresh.htm')", 1200000);
});

var wlrIMCheck = {
   showMe: function() {
      if (typeof disableIMCheck == 'undefined') {
         $.getJSON("quickcheck.php", { }, function(data){
            if(data.member != '' && data.member != null) {
               // Insert the popup div
               $('#impopup').remove();
               $('body').append('<div id="impopup"><strong>' + data.member + '</strong> would like to chat to you.<br /><br />Would you like to start chatting now?</p></div>');
               
               // Display the popup
               $("#impopup").dialog({
                  bgiframe: true,
                  resizable: false,
                  height:140,
                  modal: true,
                  title: "One-to-One Chat",
                  overlay: {
                     backgroundColor: '#000',
                     opacity: 0.5
                  },
                  buttons: {
                     Cancel: function() {
                        $(this).dialog('close');
                     },
                     'Start Chatting': function() {
                        window.open("im.htm?member=" + data.member + "&join=1", "imwin" + data.member, "width=600,height=582,status=yes,resizable=no");
                        $(this).dialog('close');
                     }
                  }
               });
            }
         });
      }
      setTimeout("wlrIMCheck.showMe()", 30000);
   }
}