function ThickboxRegister() {
    this.file_php = '/ajax/';
    this.url = this.getDomain();
    this.ruser = ''; 
    this.email = '';

    var thickbox_reg = $('#thickbox_reg_now');
    var tmp = this;
    thickbox_reg.click(function() {
                         var res = tmp.wod_validateform();
                         if (res) {
                         tmp['register'](this);
                         }
                       });

}

ThickboxRegister.prototype = new General();

ThickboxRegister.prototype.reg_showerror = function(container,f,errorlist) {
    var correct = 'formrow';
    var incorrect = 'formrow formrowerror';
    if(f && errorlist){
        document.getElementById(container).className = incorrect;
        var merrors = document.getElementById(errorlist).getElementsByTagName('LI');
        for (i=0;i<merrors.length;i++){
            var tt = merrors[i].getAttribute('title');
            if (tt == f){ 
                merrors[i].style.display = 'block';
            }else{
                merrors[i].style.display = 'none';
            }
        }
    }else{
        var merrors = document.getElementById(errorlist).getElementsByTagName('LI');
        for (i=0;i<merrors.length;i++){
          merrors[i].style.display = 'none';
        }
        document.getElementById(container).className = correct;
    }
}

ThickboxRegister.prototype.wod_ltbox_check = function(str) {
    var at="@"
        var dot="."
        var lat=str.indexOf(at)
        var lstr=str.length
        var ldot=str.indexOf(dot)
        var checker
        var errorcode
        if (str.indexOf(at)==-1){checker = false; errorcode = 'invalid'; return [checker,errorcode];}
    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){checker = false; errorcode = 'invalid'; return [checker,errorcode];}
    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){checker = false; errorcode = 'invalid'; return [checker,errorcode];}
    if (str.indexOf(at,(lat+1))!=-1){checker = false; errorcode = 'invalid'; return [checker,errorcode];}
    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){checker = false; errorcode = 'invalid'; return [checker,errorcode];}
    if (str.indexOf(dot,(lat+2))==-1){checker = false; errorcode = 'invalid'; return [checker,errorcode];}
    if (str.indexOf(" ")!=-1){checker = false; errorcode = 'empty'; return [checker,errorcode];}
    return true
}

ThickboxRegister.prototype.wod_validateform = function() {
    var errorID = 0;
    var emailID = document.ltbox_joinform.email;
    var userID = document.ltbox_joinform.username;
    this.reg_showerror('tb_join_form_email','','error_r_email');
    this.reg_showerror('tb_join_form_user','','error_r_user');
    this.reg_showerror('tb_join_form_18','','error_r_18age');

    if ((userID.value==null)||(userID.value=="")){
        this.reg_showerror('tb_join_form_user','empty','error_r_user')
            userID.focus()
            errorID = 1;
    }

    if ((emailID.value==null)||(emailID.value=="")){
        this.reg_showerror('tb_join_form_email','empty','error_r_email')
            emailID.focus()
            errorID = 1;
    }

    if (!($('#lbage').is(':checked'))) {
        this.reg_showerror('tb_join_form_18', 'empty', 'error_r_18age');
        errorID = 1;
    } 


    var wod_checkemail = this.wod_ltbox_check(emailID.value)
        if (wod_checkemail[0]==false){
            this.reg_showerror('tb_join_form_email',wod_checkemail[1],'error_r_email')
                emailID.value=""
                emailID.focus()
                errorID = wod_checkemail[1];
        }

    if (errorID != 0){
        return false
    }

    return true
}


// Summary: Setup proper registration data to pass via ajax
ThickboxRegister.prototype.register = function(eve) {
    this.ruser = $('#lbusername:first').attr('value');
    this.email = $('#lbemail:first').attr('value');
    var url = this.url + this.file_php + 'RUSER/' + this.ruser + '/EMAIL/' + this.email;
    this.ajaxCall(url,['checkRegStatus']);
}

// Summary: Check the registration status
ThickboxRegister.prototype.checkRegStatus = function(msg) {
   var success = msg['success'];

   success = parseInt(success);
   if (success == 0) {
     this.redirectReg2(success); 
   } else {
     this.showError(msg); 
   }
}

// Summary: Show error message for registration
// params: msg - json object
ThickboxRegister.prototype.showError = function(msg) {
    var email = msg['error_r_email'] || '';
    var ruser = msg['error_r_user'] || '';
   
    var e_len = email.length;
    var r_len = ruser.length;

    // set email errors
    for (var x = 0; x < e_len; x++) {
      var email_err = email[x];
      this.reg_showerror('tb_join_form_email',email_err,'error_r_email');
    }

    // set user errors
    for (var y = 0; y < r_len; y++) {
      var user_err = ruser[y];
      this.reg_showerror('tb_join_form_user',user_err,'error_r_user');
    }
}

// Summary: Redirect to registration when successful
ThickboxRegister.prototype.redirectReg2 = function() {
   var url = this.url;
   // second page to register
   var file_php = '/reg2/';
   var ruser = this.ruser;
   var email = this.email;

   url = url + file_php + 'ruser/' + ruser + '/email/' + email;
   // remove the thick box right after registration
   // function is from the thickbox.js
   tb_remove();
   window.location.href = url;
}


ThickboxRegister.prototype.checkUsername = function(elem) {
  var username = elem.value;
  // do not include blank space
  var pattern = /^[\s ]*$/ig;

  if (pattern.test(username)) {
    alert('username is invalid');
    return false;
  } 
}

// Summary: Check email validation
// params: email value to check
ThickboxRegister.prototype.checkEmail = function(elem) {
    // Make sure that something was entered and that it looks like
    // a valid email address
    var stat = !elem.value || /^[a-z0-9_+.-]+\@([a-z0-9-]+\.)+[a-z0-9]{2,4}$/i.test( elem.value );

    if (!stat) {
        alert('incorrect email formatting');
        return stat; 
    }
}

new ThickboxRegister();



