//  application js
// Author		: David Goddard
// Date			: 3 September 2007
// Purpose	:  Application specific javascript code for DMS online

Event.addBehavior({

  'body': function() {
	  //  This gets executed when the HTML page loads.
	  // We add a DIV tag to display a message when an async call is made to OI.
	  // This will work for all pages where this .js file is called.

    var busy = $div({id:'busy'}, "Loading...");
    busy.hide();
    // NOTE You could also use 'this' here
    //      since it is the document.body.
    document.body.appendChild(busy);

	// you can put an async call in here if you want to track what pages are loaded in an OI table
    //new Ajax.Request('/cgi-bin/oecgi2.exe/inet_sometracking_procedure');
  },

  // WorldWide Public Holidays
    'body#wwpublicholidays': function() {
    // return the public holidays news item from the NEW file = row #100
    new Ajax.Updater('datafromoi', "/dms/oecgi2.exe/inet_wwPublicHolidays");
	// returning false terminates the event chain, just like OI
    return false;
  },

	// WWCN Countries
    'body#wwcncountries': function() {
    // return the public holidays news item from the NEW file = row #100
    new Ajax.Updater('datafromoi', "/dms/oecgi2.exe/inet_wwcn_countries");
	// returning false terminates the event chain, just like OI
    return false;
  },

	// WWRN Countries
    'body#wWrncountries': function() {
    // return the public holidays news item from the NEW file = row #100
    new Ajax.Updater('datafromoi', "/dms/oecgi2.exe/inet_wwrn_countries");
	// returning false terminates the event chain, just like OI
    return false;
  },

	// example 2 code
	 'form#tcl:submit': function() {
	// this = equals the form itself. Prototype adds a special request method to a form
	// which performs an async POST to the URL in the ACTION property of the form.
    this.request({
      onComplete: function(transport) {
		  // transport contains what ever is returned back from OpenInsight. We get the text version
		  // of this and update the datafromoi tag.
		  var data = transport.responseText ;
		 $('datafromoi').innerHTML = data ;
      }
    });
    return false;
  },

  // example 3 code
  'body#form_page': function() {
	// this code only gets executed when the pages body tag has has id = form_page

  // loads the html form from OI into the tag with the id of datafromoi.
  new Ajax.Updater('datafromoi',"/cgi-bin/oecgi2.exe/inet_repos?doc_id=INET_WEB_CUST_ENTRY");
  },

  'form input[value=Enter]:click': function() {
	  // change the form action to use  OECGI2 instead of oecgi.
	  this.form.action = "/cgi-bin/oecgi2.exe/inet_web_cust_entry"
	  // submit the form asyncronously, then return false to stop the form submitting itself
	  // in the old fashioned html way.
    this.form.request({
      onComplete: function(transport) {
		  // The data sent back from OI is dispayed in the tag with an id of datafromoi
		var data = transport.responseText;
		$('datafromoi').innerHTML = data;
      }
    });
    return false;
  },

  // example 4 code

  'form input#id:change': function() {
      var url = "/cgi-bin/oecgi2.exe/inet_oi_read?filename=customers&id=" + $('id').value
	  new Ajax.Request(url, {
			method: 'get',
			onSuccess: function(transport) {
			var data = transport.responseText.split(",");
			if (data[0].substr(0,6)=="<HTML>")
			{
				$('datafromoi') = data[0]
			}else{
				$('fname').value = data[1]
				$('lname').value = data[2]
				$('company').value = data[12]
			}
			}});
    return false;
  },

	'form#customers:submit': function() {
     this.action = "/cgi-bin/oecgi2.exe/inet_formwrite?form_id=CUST_ENTRY"
	 this.request({
      onComplete: function(transport) {
		  // The data sent back from OI is dispayed in the tag with an id of datafromoi
		var data = transport.responseText;
		$('datafromoi').innerHTML = data;
      }
    });
    return false;
  }

});

/* From http://mir.aculo.us/2005/11/14/ajax-activity-indicators-with-rails-0-14-3 */
// These functions will be called when ever an async call is made. One for
// create and one on complete. This displays the "hey Im processing message" stuff.
Ajax.Responders.register({

  onCreate: function() {
    if($('busy') && Ajax.activeRequestCount > 0)
      $('busy').show()
  },

  onComplete: function() {
    if($('busy') && Ajax.activeRequestCount == 0)
      $('busy').hide()
  }

});