﻿function CSFormValidationManager()
{
}

   CSFormValidationManager.prototype.isSearchEvent = false;
   
CSFormValidationManager.prototype.init = function()
{
    myThis = this;
    this.isSearchEvent = false;
    var fLookup = $('form');
    $('form').bind('submit', function() {return myThis.validateForm();});   
    $("[ShowOnOther]").bind("change", this.OnChanged);
    $("[ShowOnOtherName]").hide();
}

CSFormValidationManager.prototype.OnChanged = function(event)
{
    var sender = event.currentTarget;
    
    if (sender == null) return;
    
    var otherContainerName = sender.attributes["ShowOnOther"].value;
    
    if (sender[sender.selectedIndex].value == "Other")
    {
        $("[ShowOnOtherName="+otherContainerName+"]").show();
    }
    else
    {   
        $("[ShowOnOtherName="+otherContainerName+"]").hide();
        
        $("[ShowOnOtherName="+otherContainerName+"] textarea").each(function(index) {this.value = "";});
    }
}

CSFormValidationManager.prototype.validateForm = function(event)
{
    if(this.isSearchEvent) return true;
    
    var requiredLookup = $('[CSFormRequired=true]');
    
    var isValid = true;
    
    for(var rIndex = 0; rIndex < requiredLookup.length; rIndex++)
    {
        var inputToValidate = requiredLookup[rIndex];
        if (inputToValidate.value == '')
        {
            isValid = false;
            break;
        }
    }
    
    if (!isValid)
    {
        alert("Please fill in all required information.");
        return false;
    }
    
    /* ^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$ 
     * This is the regex from http://www.regular-expressions.info/email.html
     * please view that page for the reasons for using this regular expression
     * pattern.
     */
    var emailLookup = $('#[CSEmailValidation=true]');
    
    for(var eIndex = 0; eIndex < emailLookup.length; eIndex++)
    {
        var emailToValidate = emailLookup[eIndex].value;
        var emailIsValid = emailToValidate.match(/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i);
        
        if (!emailIsValid)
        {
            alert("Invalid Email");
            return false;
        }
    }
     
    return isValid;
}
   CSFormValidationManager.prototype.initIgnoreValidationOnSearch = function(txtID, buttonID)
   {
        var myThis = this;
        var searchTxt = {
            ClientID:txtID,
            JQuery:$(txtID),
            OnKeyPress: function(e)
            {
                if(checkKeycode(e))
                {
                    myThis.isSearchEvent = true;
                }
            }
        };
        
        var button = 
        {
            ClientID: buttonID,
            JQuery:$(buttonID),
            OnClick: function(e)
            {
                myThis.isSearchEvent = true;
            }
        };
        
        searchTxt.JQuery.bind('keypress', searchTxt.OnKeyPress);
        button.JQuery.bind('click', button.OnClick);
   }
