﻿Validator.prototype.Element;
Validator.prototype.ElementTitle;

Validator.prototype.Valid;
Validator.prototype.Status;

Validator.prototype.IsValid = IsValid;
Validator.prototype.StatusMessage = StatusMessage;


Validator.prototype.IsNotEmpty = IsNotEmpty;
Validator.prototype.IsNumeric = IsNumeric;
Validator.prototype.IsDecimal = IsDecimal;
Validator.prototype.IsValidDate = IsValidDate;
Validator.prototype.IsValidCharacters = IsValidCharacters;
Validator.prototype.IsValidEmail = IsValidEmail;
Validator.prototype.IsValidPhoneNumber = IsValidPhoneNumber;
Validator.prototype.IsValidPostalCode = IsValidPostalCode;
Validator.prototype.IsValidCreditCard = IsValidCreditCard;
Validator.prototype.IsValidCvn = IsValidCvn;

function Validator(elementId, title) // ctor
{
	this.ElementTitle = title;
	
	this.Element = document.getElementById(elementId);

	this.Valid = true;
	this.Status = '';

	return this;
}

function IsValid()
{
	return this.Valid;
}

function StatusMessage()
{		
	return this.Status;
}

function IsNotEmpty()
{
	this.Valid = this.Element.value.length > 0;
	this.Status = !this.Valid ? this.ElementTitle + ' cannot be empty.' : '';
	this.Element.style.borderColor = !this.Valid ? '#f00' : '#aaa';

	return this;
}

function IsNumeric()
{
	this.Valid = this.Element.value.match(/^\d+$/) != null;
	this.Status = !this.Valid ? this.ElementTitle + ' is required and must be numeric.' : '';
	this.Element.style.borderColor = !this.Valid ? '#f00' : '#aaa';
}

function IsDecimal()
{
	this.Valid = this.Element.value.match(/^\d+\.\d{2}$/) != null;
	this.Status = !this.Valid ? this.ElementTitle + ' is required and must in the format 1.23.' : '';
	this.Element.style.borderColor = !this.Valid ? '#f00' : '#aaa';
}

function IsValidDate()
{
	this.Valid = this.Element.value.match(/^\d{2}\/\d{2}\/\d{4}$/) != null;
	this.Status = !this.Valid ? this.ElementTitle + ' is required and must in the format mm/dd/yyyy.' : '';
	this.Element.style.borderColor = !this.Valid ? '#f00' : '#aaa';
}

function IsValidCharacters(allowEmpty)
{
    this.Valid = true;
    if (this.Element.value.charAt(0) == ' ') {
        this.Element.value = trim(this.Element.value);
    }
	
	if ((!allowEmpty) || (allowEmpty && (parseInt(this.Element.value.length, 10) > 0)))
	{
		this.Valid = this.Element.value.match(/^[A-z\s\d]+$/) != null;
	}

	this.Status = !this.Valid ? this.ElementTitle + ' may only contain letters, numbers, and spaces.' : '';
	this.Element.style.borderColor = !this.Valid ? '#f00' : '#aaa';
	
	return this;
}

function IsValidEmail()
{
	this.Valid = this.Element.value.match(/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/) != null;
	this.Status = !this.Valid ? this.ElementTitle + ' is not in a valid format.' : '';
	this.Element.style.borderColor = !this.Valid ? '#f00' : '#aaa';
	
	return this;
}

function IsValidPhoneNumber()
{
	this.Valid  = this.Element.value.match(/^\D?(\d{3})\D?\D?(\d{3})\D?(\d{4})$/) != null;
	this.Status = !this.Valid ? this.ElementTitle + ' must have 10 digits.' : '';
	this.Element.style.borderColor = !this.Valid ? '#f00' : '#aaa';
	
	return this;
}

function IsValidPostalCode()
{
	this.Valid = this.Element.value.match(/^\d{5}(-\d{4})?$/) != null;
	this.Status = !this.Valid ? this.ElementTitle + ' must be in the format 12345-6789.' : '';
	this.Element.style.borderColor = !this.Valid ? '#f00' : '#aaa';
	
	return this;
}

function IsValidCreditCard()
{
	this.Valid = this.Element.value.match(/^\d{16}$/);
	
	this.Status = !this.Valid ? this.ElementTitle + ' must be 16 digits.' : '';
	this.Element.style.borderColor = !this.Valid ? '#f00' : '#aaa';
	
	return this;
}

function IsValidCvn()
{
	this.Valid = this.Element.value.match(/^\d{3,4}$/) != null;
	this.Status = !this.Valid ? this.ElementTitle + ' must be 3 or 4 digits.' : '';
	this.Element.style.borderColor = !this.Valid ? '#f00' : '#aaa';

	return this;
}
function trim(str) {
        return str.replace(/^\s\s*$/,'');
    }
