WaspApp = {
    registerBehaviour: function() {
        $$('.hide_elements').each(function(element) {
            new Element.ClassNames(element).each(function(dialog_name) {
              if (dialog_name!='hide_elements') {
                element.onclick = function() {
                  new Effect.Fade(dialog_name);
                  return false;
                }
              }
            });
        });
        $$('.show_elements').each(function(element) {
            new Element.ClassNames(element).each(function(dialog_name) {
              if (dialog_name!='show_elements') {
                element.onclick = function() {
                  new Effect.Appear(dialog_name, 
                    {
                        afterFinish: function() { 
                            Form.focusFirstElement($$('#' + dialog_name + ' form').first());
                            new Effect.ScrollTo($$('#' + dialog_name + ' form').first());
                        }
                    }
                  );
                  return false;
                }
              }
            });
        });
        // all forms with class ajax_form automatically get an onsubmit handler installed that submits the form via xhr
        $$('form.ajax_form').each(function(form) {
            $(form).observe('submit', function(ev) {
              Form.request(form, {onComplete: function() {Form.enable(form);}});
              Form.disable(form);

              Event.stop(ev);
              return false;
            });
        });
        // all forms with class ajax_link automatically get an onclick handler installed that submits to the href via xhr
        $$('a.ajax_link').each(function(link) {
            Event.observe(link, 'click', function() {
              new Ajax.Request(link.href, {asynchronous:true, evalScripts:true});
              return false;
            });
        });
    },

	randomPassword: function() {
		var password = "";
		var max_lenghth = 8;

	    for (i=0; i < max_lenghth; i++) {
			// between 0 - 1 
			var rndNum = Math.random() 

			// rndNum from 0 - 1000 
			rndNum = parseInt(rndNum * 1000); 

			// rndNum from 33 - 127 
			rndNum = (rndNum % 94) + 33;
			
			if (WaspApp.checkPunc(rndNum)) {
				max_lenghth += 1;
				continue;
			}
			
			password += String.fromCharCode(rndNum);
		}
		
		return password;
	},
	
	checkPunc: function (num) {
	    if ((num >=33) && (num <=47)) { return true; }
	    if ((num >=58) && (num <=64)) { return true; }
	    if ((num >=91) && (num <=96)) { return true; }
	    if ((num >=123) && (num <=126)) { return true; }

	    return false;
	},
	
	switchToEuView: function(doIt) {
		// Method id called via page.update form public controller.
		// Do something sensible, eg. switch image source accordingly
		//alert(doIt ? 'EU' : 'Non EU');
		if (doIt) {
			//$('sign_up_button').setStyle({border: '3px solid blue'});
			$('sign_up_button').src = $('sign_up_button').src.replace("Dollar.png", ".gif")
		}
		else {
			$('sign_up_button').src = $('sign_up_button').src.replace(".gif", "Dollar.png")
		}
	},
	
	setIsEuRequest: function(country) {
		// this url needs to be proxied to http://api.hostip.info/
		new Ajax.Request(window.location.protocol + "//" + window.location.host + "/public/is_eu_request?country=" + encodeURIComponent(country));
	},
	
	setIsEuRequestFromHostIp: function() {
		new Ajax.Request(window.location.protocol + "//" + window.location.host + "/host_ip/index.php", {
			method: 'get',
			onComplete: function(transport) {
				var country = transport.responseText.match(/Country\:\s+([^\(]+)\s+.*/)[1];
				WaspApp.setIsEuRequest(country);
			}
		});
	}
}

/**
 * Automagically displays an image when AJAX Requests are active.
 * Create an element with id='busy_throbber' on the page that should be displayed while AJAX Requests are active.
 */
Ajax.Responders.register({
  onCreate: function() {
    if($('busy_throbber') && Ajax.activeRequestCount>0)
      Effect.Appear('busy_throbber',{duration:0.5,queue:'end'});
  },
  onComplete: function() {
    if($('busy_throbber') && Ajax.activeRequestCount==0)
      Effect.Fade('busy_throbber',{duration:0.5,queue:'end'});
  }
});

/**
* Output given string in element with id=debugjs
*/
function debug(string) {
    new Insertion.Top($('debugjs'), 
                         '[' + new Date().toString() + '] : ' + 
                         string.escapeHTML() + '<br />');
}
