/* mootools */
function ucfirst(str)
{
    //return str.substr(0, 1).toUpperCase() + str.substr(1, str.length);
    //return str.capitalize();
    return str;
}

function checkforeign()
{
    if ($('foreign_address').get('checked') == false)
    {
        $('nladdr').setStyle('display', 'block');
        $('foreignaddr').setStyle('display', 'none');
    }
    else
    {
        $('nladdr').setStyle('display', 'none');
        $('foreignaddr').setStyle('display', 'block');
    }
}

window.addEvent('domready', function()
{

  $$("form .numeric").each(function(el)
  {
    el.addEvent('keyup', function()
    {
      // strip last character, if not numeric
      var val = el.get('value');
      if (val.test("^([0-9]+)$") == false)
      {
        el.set('value', val.substr(0, val.length-1));
      }
    });
  });
  
  if ($('signup_form'))
  {
    var signup_form = $('signup_form');
    
    if ($('tf_account_firstname'))
    {
        $('tf_account_firstname').addEvent('keyup', function()
        {   
            this.set('value', ucfirst(this.get('value')));
        });
        
        $('tf_account_linkname').addEvent('keyup', function()
        {
            this.set('value', this.get('value').toLowerCase());
        });
        
        $('tf_account_lastname').addEvent('keyup', function()
        {
            this.set('value', ucfirst(this.get('value')));
        });
    }
    
    if ($('foreign_address'))
    {
        $('foreign_address').addEvent('click', function()
        {
            checkforeign();
        });
        
        checkforeign();
    }
    
    if ($('tf_account_zipcode'))
    {
        var zipcode = $('tf_account_zipcode');
        zipcode.addEvent('keyup', function()
        {
            this.set('value', this.get('value').toUpperCase());
        });
    }
    
    if ($('tf_account_addr_zipcode'))
    {
        $('tf_account_addr_zipcode').addEvent('keyup', function()
        {
            this.set('value', this.get('value').toUpperCase());
        });
    }
  
    if ($('tf_account_street_number'))
    { 
        var street_number = $('tf_account_street_number');
        var resultdiv = $('resultdiv');
        
        var req = new Request.HTML({
          'url': site_base + '/resources/scripts/address.php'
         ,'method': 'post'
         ,'update': resultdiv
        });

        zipcode.addEvent('keyup', function()
        {
            req.post(signup_form);
        });
        
        street_number.addEvent('keyup', function()
        {
            req.post(signup_form);
        });
        
        req.post(signup_form);
    }
  }

});

