﻿// JScript File
function ValidationElement(controlId, validationType, errorMessage, popUp)
{
    this.ControlId = controlId;
    this.ValidationType = validationType;
    this.ErrorMessage = errorMessage;
    this.PopUp = popUp;
    this.HookControl();
}
ValidationElement.prototype.HookControl = function()
{
    var control = document.getElementById(this.ControlId);
    if( typeof(control) == "undefined")
    {
        return;
    }
    if( typeof(control.Validator) == "undefined")
    {
        control.Validator = this;
    }

    //Very much Microsoft Validation    
    var eventHandler;
    if( control.type == "radio")
    {
        eventHandler = control.onclick;
    }
    else
    {
        eventHandler = control.onchange;
    }
    
    if( typeof(eventHandler) == "function")
    {
        eventHandler = eventHandler.toString();
        eventHandler = eventHandler.substring(eventHandler.indexOf("{") + 1, eventHandler.lastIndexOf("}"));
    }
    else
    {
        eventHandler = "";
    }
    if( control.type == "radio")
    {
        control.onclick = new Function("Control_OnChange(); " + eventHandler);
    }
    else
    {
        control.onchange = new Function("Control_OnChange(); " + eventHandler);
    }
}
ValidationElement.prototype.GetControlValue = function()
{
    var control = document.getElementById(this.ControlId);
    if( typeof(control) == "undefined")
    {
        alert("Unable to locate element " + this.ControlId);
        return "";
    }
    
    if( control.type == "text" || control.type == "textarea")
    {
        return control.value;
    }
    else if (control.type == "select-one")
    {
        return control.options[ control.selectedIndex].value;
    }
    
    return "";
}
ValidationElement.prototype.SetInlineElementDisplay = function(value)
{
    var control = document.getElementById( "v" + this.ControlId);
    if( typeof(control) == "undefined")
    {
        return;
    }
    control.style.display = value;
}
ValidationElement.prototype.ShowInlineElement = function()
{
    this.SetInlineElementDisplay("inline");
}
ValidationElement.prototype.HideInlineElement = function()
{
    this.SetInlineElementDisplay("none");
}
ValidationElement.prototype.Valid = function()
{
    var trimRegEx = /\s+/g;
    
    var controlValue = this.GetControlValue();
    
    switch( this.ValidationType )
    {
        case 1: //Value is not empty
            if( controlValue.replace( trimRegEx, "").length == 0)
            {
                return false;
            }
            break;
        case 2: //Email Address
            var emailRegEx = new RegExp("^[\\w\.=-]+@[\\w\\.-]+\\.[a-zA-Z]{2,4}$")
            if( !emailRegEx.test(controlValue) )
            {
                return false;
            }
            break;
        case 3: //Date
        
        break;
    }
    return true;
}
ValidationElement.prototype.ToLineItem = function()
{
    var li = document.createElement("LI");
    li.innerText = this.ErrorMessage;
    return li;
}

var ValidationElements;
function FormValidate(form)
{
    if (typeof(ValidationElements) == "undefined")
        return true;

    var summaryDiv = document.getElementById("validationSummary");
    var summaryUL = document.createElement("UL");
    
    
    var i, valid;
    valid = true;
    for( i = 0; i < ValidationElements.length; i++)
    {
        if( !ValidationElements[i].Valid() )
        {
            valid = false;
            ValidationElements[i].ShowInlineElement();
            if( ValidationElements[i].PopUp)
            {
                alert(ValidationElements[i].ErrorMessage);
                return false;
            }
            else
            {
                summaryUL.appendChild( ValidationElements[i].ToLineItem());
            }
        }
        else
        {
            ValidationElements[i].HideInlineElement();
        }
    }
    summaryDiv.innerHTML = "";
    if( !valid )
    {
        summaryDiv.appendChild( summaryUL );
    }
    
    return valid;
}
function Control_OnChange()
{
    var validationElement = event.srcElement.Validator;
    if( typeof(validationElement) == "undefined")
    {
        return;
    } 
    if( validationElement.Valid())
    {
        validationElement.HideInlineElement();
    }
    else
    {
        validationElement.ShowInlineElement();
    }
}
