function hide_elements() {
  $('progress').hide();
}

function processform() {
      $('b_register').disable();
      $('b_reset').disable();

      clear_form_errors();

      form_validates = true;

      /****************************************/
      /* Check serial is not blank and starts with b */
      /****************************************/
      if ($F('serial').blank()) {
        $('serial').setStyle({border: '1px solid red', background: '#ff3f3f'});
        form_validates = false;
      }
      else {
        if ($F('serial').charAt(0) != 'b' && $F('serial').charAt(0) != 'B') {
          $('serial').setStyle({border: '1px solid red', background: '#ff3f3f'});
          form_validates = false;
        }
      }

      /****************************************/
      /* Day must not be blank and must be between 1 and 30 */
      /****************************************/
      if ($F('purch_date_d').blank()) {
        $('purch_date_d').setStyle({border: '1px solid red', background: '#ff3f3f'});
        form_validates = false;
      }
      else {
        var day = $F('purch_date_d');
        if (day > 31 || day < 1) {
          $('purch_date_d').setStyle({border: '1px solid red', background: '#ff3f3f'});
          form_validates = false; 
        } 
      }

      /****************************************/
      /* Month must not be blank */
      /* Check num days for month as well */
      /****************************************/
      var dt = new Date();
      var thismonth = dt.getMonth();
      var thisday = dt.getDay();
      var thisyear = dt.getFullYear();

      if ($F('purch_date_m').blank()) {
        $('purch_date_m').setStyle({border: '1px solid red', background: '#ff3f3f'});
        form_validates = false;
      }
      else {
        var day = $F('purch_date_d');
        var tmonth = $F('purch_date_m');
        var tyr = $F('purch_date_y');
        var month = '';

        if (tmonth == 'January') month = 0;
        if (tmonth == 'February') month = 1;
        if (tmonth == 'March') month = 2;
        if (tmonth == 'April') month = 3;
        if (tmonth == 'May') month = 4;
        if (tmonth == 'June') month = 5;
        if (tmonth == 'July') month = 6;
        if (tmonth == 'August') month = 7;
        if (tmonth == 'September') month = 8;
        if (tmonth == 'October') month = 9;
        if (tmonth == 'November') month = 10;
        if (tmonth == 'December') month = 11;

        if (month == 'April' || month == 'June' || month == 'September' || month == 'November') { 
          if (day > 30) {
            form_validates = false; 
            $('purch_date_m').setStyle({border: '1px solid red', background: '#ff3f3f'});
          }
        }
        else if (month == 'February') {
          if (day > 29) {
            form_validates = false; 
            $('purch_date_m').setStyle({border: '1px solid red', background: '#ff3f3f'});
          }
        }
        else if ((month > thismonth) && (tyr == thisyear)) {
            form_validates = false; 
            $('purch_date_m').setStyle({border: '1px solid red', background: '#ff3f3f'});
        }
      }

      /****************************************/
      /* Year must not be blank */
      /* Also must not be > this year */
      /****************************************/
      if ($F('purch_date_y').blank()) {
        $('purch_date_y').setStyle({border: '1px solid red', background: '#ff3f3f'});
        form_validates = false;
      }
      else {
        var yr = $F('purch_date_y');

        if (yr > thisyear) {
          form_validates = false; 
          $('purch_date_y').setStyle({border: '1px solid red', background: '#ff3f3f'});
        }
        else {
          var leapyear = false;
          if (yr % 4 == 0 && yr % 100 != 0) {
            leapyear = true;  
          }
          else if (yr % 4 == 0 && yr % 100 == 0) {     

            if (thisyear % 400 == 0) {
              leapyear = true;
            }
            else {
              leapyear = false;  
            }
          }
          else {
            leapyear = false;
          }

          if (!leapyear) {
            var day = $F('purch_date_d');  
            var month = $F('purch_date_m');  

            if (month == 'February') {
              if (day > 28) {
                form_validates = false;
                $('purch_date_d').setStyle({border: '1px solid red', background: '#ff3f3f'});
              }
            }
          }
        }
      }

      if ($F('fname').blank()) {
        $('fname').setStyle({border: '1px solid red', background: '#ff3f3f'});
        form_validates = false;
      }

      // Check if form validates
      if (!form_validates) {
        $('errmsg').setStyle({color: 'red', fontWeight: 'bold'}).update('Please fix the highlighted fields, they are in error');
        $('b_register').enable();
        $('b_reset').enable();
        return false;
      }

      // Updater for finish form process
      var upd = new Ajax.Updater(
        'errmsg',
        '/en/doregister.php?' + new Date(),
        {       
          method: 'post',
          parameters: $('registerform').serialize(true),
          onCreate:function(transport)
          {       
            // Element.setOpacity('registerform', 0.5);
            var tmp = document.getElementById('errmsg');
            tmp.innerHTML = '<img src="loadprogress.gif">Processing ...';
			Effect.Fade('registerform');
          },
          onComplete:function(transport) 
          {
            Element.setOpacity('registerform', 1.0);
            $('b_register').enable();
            $('b_reset').enable();
            $('registerform').reset(); 
          }
        });
}

function clear_form_errors() {
  $('serial').setStyle({border: '1px solid #D7E5F2', background: '#ffffff'});
  $('purch_date_d').setStyle({border: '1px solid #D7E5F2', background: '#ffffff'});
  $('purch_date_m').setStyle({border: '1px solid #D7E5F2', background: '#ffffff'});
  $('purch_date_y').setStyle({border: '1px solid #D7E5F2', background: '#ffffff'});
  $('purch_date_y').setStyle({border: '1px solid #D7E5F2', background: '#ffffff'});
  $('fname').setStyle({border: '1px solid #D7E5F2', background: '#ffffff'});
  $('errmsg').update('');
}
