// validators.js
//
// Contains routines to help validate various different fields in the login/voting UI
//

// MODIFY THE FOLLOWING LINE IF THIS VERSION OF BOOKTOR IS *NOT* INSTALLED AT THE ROOT DIRECTORY
// for instance:  if you extracted this package to a directory called "test" (accessible at http://www.booktor.com/test) then
// change the following line to
//
// $baseDocsDir = '/test/';
//
// NOTE:  make sure the last character is always a / character.  Otherwise all hell will break loose!
//
//DocumentRoot = '/booktor/';
DocumentRoot = '';

Validators = { };

Validators.checkUsername = function (strUsername) {

    var result = false;

    // username must be less than 20 characters
    // username must contain only alphanumeric letters
    if (strUsername.length < 20)
    {
        var reMatch = /[a-zA-Z0-9]+/i;
        
        if (strUsername.search(reMatch) >= 0)
            result = true;
    }
    
    return result;
}

Validators.checkPassword = function (strPassword) {
    var result = false;
    
    // password must be >= 6 characters long
    if (strPassword.length >= 6)
    {
        result = true;
    }
    
    return result;
}

Validators.checkPasswordConfirm = function (strPassword, strConfirm) {
    return strPassword == strConfirm;
}

Validators.checkEmail = function (strEmail) {
    var result = false;
    var reMatch = /^[A-Z0-9._%+-]+\@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
    
    // email must be unique, and must be a valid email address
    //
    // first we should verify that the email is in fact a valid email address
    //
    if (strEmail.search(reMatch) >= 0) {
    
        // 
        // if it's a valid address, utilize ajax to connect to the server and ensure that it's unique
        //

        result = true;    
    }
    
    return result;
}

////////////////////////////////////////////////////////////////////////////////
// individual page validators

function getDhtmlObj(name) {
  if (document.getElementById)
  {
    this.obj = document.getElementById(name);
	this.style = document.getElementById(name).style;
  }
  else if (document.all)
  {
    this.obj = document.all[name];
	this.style = document.all[name].style;
  }
  else if (document.layers)
  {
   	this.obj = document.layers[name];
   	this.style = document.layers[name];
  }
}

Reporter = { };

Reporter.clearErrors = function (where) {

    var errorsDiv = new getDhtmlObj(where);
    
    errorsDiv.obj.innerHTML = '';
}

Reporter.writeError = function (where,text) {
    // reports given error if a validator fails
    var errorsDiv = new getDhtmlObj(where);
    
    errorsDiv.obj.innerHTML = errorsDiv.obj.innerHTML + '<div class="message-error">' + text + '</div>';
}

Reporter.writeOk = function (where,text) {
    // reports OK if a validator passes
    var errorsDiv = new getDhtmlObj(where);
    
    errorsDiv.obj.innerHTML = errorsDiv.obj.innerHTML + '<div class="message-ok">' + text + '</div>';
}

ChangePwValidator = { };

ChangePwValidator.submit = function () {
    var errorsFound = false;
    
    Reporter.clearErrors('passwordErrors');
    
    if (!Validators.checkPassword(document.passwordForm.txtPw.value)) {
        errorsFound = true;
        Reporter.writeError('passwordErrors', 'Password is too short.');
    }
    
    if (!Validators.checkPasswordConfirm(document.passwordForm.txtPw.value, document.passwordForm.txtVerifyPw.value)) {
        errorsFound = true;
        Reporter.writeError('passwordErrors', 'Passwords do not match.');
    }
    
    if (!errorsFound)
	{
        //document.passwordForm.submit();
		return true;
	}
	
	return false;
}

PreferencesValidator = { };

PreferencesValidator.submit = function () {
    var errorsFound = false;
    
    Reporter.clearErrors('preferencesErrors');
    
    if (!Validators.checkPassword(document.preferencesForm.txtPw.value)) {
        errorsFound = true;
        Reporter.writeError('preferencesErrors', 'Current password not recognized.');
    }
    
    if (!Validators.checkEmail(document.preferencesForm.txtEmail.value)) {
        errorsFound = true;
        Reporter.writeError('preferencesErrors', 'Incorrect email format.');
    }
    
    if (document.preferencesForm.txtNewPw.value.length > 0) {
        if (!Validators.checkPassword(document.preferencesForm.txtNewPw.value)) {
            errorsFound = true;
            Reporter.writeError('preferencesErrors', 'New password is too short.');
        }
        
        if (!Validators.checkPasswordConfirm(document.preferencesForm.txtNewPw.value,
                                             document.preferencesForm.txtNewVerifyPw.value)) {
            errorsFound = true;
            Reporter.writeError('preferencesErrors', 'New passwords do not match.');
        }
    }
    
    if (!errorsFound)
	{
        //document.preferencesForm.submit();
		return true;
	}
	
	return false;
}

RegisterValidator = { };

RegisterValidator.submit = function () {
    var errorsFound = false;
    
    Reporter.clearErrors('registerErrors');
    
    if (!Validators.checkUsername(document.registerForm.txtUsername.value)) {
        errorsFound = true;
        Reporter.writeError('registerErrors','Username is too long, or it contains invalid characters.');
    }
    
    if (!Validators.checkEmail(document.registerForm.txtEmail.value)) {
        errorsFound = true;
        Reporter.writeError('registerErrors','Incorrect email format.');
    }
    
    if (!Validators.checkPassword(document.registerForm.txtPw.value)) {
        errorsFound = true;
        Reporter.writeError('registerErrors','Password is too short.');
    }
    
    if (!Validators.checkPasswordConfirm(document.registerForm.txtPw.value, document.registerForm.txtVerifyPw.value)) {
        errorsFound = true;
        Reporter.writeError('registerErrors','Passwords do not match.');
    }
    
    if (!errorsFound)
	{
        //document.registerForm.submit();
		return true;
	}
	
	return false;
    
}

LoginValidator = { };

LoginValidator.submit = function () {
    // submit() function:
    //
    // does validation on the login.php UI page and submits the form if everything checks out
    //
    
    var errorsFound = false;
    
    Reporter.clearErrors('loginErrors');
    
    if (!Validators.checkUsername(document.loginForm.txtUsername.value)) {
        errorsFound = true;
    }
    
    if (!Validators.checkPassword(document.loginForm.txtPw.value)) {
        errorsFound = true;
    }
    
    if (!errorsFound)
	{
        //document.loginForm.submit();
		return true;
	}
    else
	{
        Reporter.writeError('loginErrors','Username and password do not match');
		return false;
	}
}

AjaxHelper = { };

AjaxHelper.VerifyUsername = function (username) {
    if (username.length > 20) {
        $('#usernameVerify').html('<span class="message-error">Username is too long</span>');
    } else {

        $.get(DocumentRoot + "loginfiles/doverify.php", { action : "username", param : username }, function (data) {
        
            $('#usernameVerify').html(data);
        
        });
        
    }
}

AjaxHelper.VerifyEmail = function (email) {
    $.get(DocumentRoot + "loginfiles/doverify.php", { action : "email", param : email }, function (data) {
    
        $('#emailVerify').html(data);
    
    });
}