﻿/*
SendToAFriend.js
Created 2/2/2009 by Dan Roentsch - Ogilvy
Validate form controls.
*/
var SendToAFriend = {
    "ResolveControls":function()
    {
        if (typeof(this.Name) == "undefined" && this.Name === null) return;
        if (document.getElementById(this.Name)===null) return;
        
        this.Fields = [];
        
        this.elm = new elm();
        
        this.elm.make(this.Name, "name","textbox","required","test","can_clear").setRegex(this.REGEX_NAME);
        this.elm.make("lblName", "name","label");
        this.Fields.push("name");
        
        this.elm.make(this.Email, "email","textbox","required","test","can_clear").setRegex(this.REGEX_EMAIL);
        this.elm.make("lblEmail", "email","label");
        this.Fields.push("email");

        this.elm.make(this.FriendName, "friend_name","textbox","required","test","can_clear").setRegex(this.REGEX_NAME);
        this.elm.make("lblFriendName", "friend_name","label");
        this.Fields.push("friend_name");

        this.elm.make(this.FriendEmail, "friend_email","textbox","required","test","can_clear").setRegex(this.REGEX_EMAIL);
        this.elm.make("lblFriendEmail", "friend_email","label");
        this.Fields.push("friend_email");

        //Status exercises control over the visibility of a control that needs to be set at the server
        //  and cleared in the client.
        this.elm.make(this.Status,"send_status","can_clear");
    }  //end ResolveControls()
    ,
    "ValidateForm":function()
    {
        this.ResolveControls();
        
        this.IsValid = true;
                
        //check controls for errors
        for (var i = 0; i < this.Fields.length; i++)
        {
            this.IsValid = this.ValidateControl(this.Fields[i]);
        }
        
        return this.IsValid;
    }//end ValidateForm()
    ,
    "ValidateControl":function(data_field_label)
    {
        if (this.elm.IsEmpty(data_field_label)) {return false;}
        var coll = [];
        var IsValid = true;
        
        coll=this.elm.select(data_field_label);
        if (coll.length == 2)
        {
            IsValid = this.ValidateSingleControl(coll);
        }
        if (coll.length > 2)
        {
            IsValid = this.ValidateGroupControl(coll);
        }
        if (IsValid === true)
        {
            coll.select("label").setStyle("color:#000000;font-weight:500");
        }
        else
        {
            var lbl = coll.select("label");
            lbl.setStyle("color:#FF0000");
            if (lbl.hasField("bold_on_error"))
            {                
                lbl.setStyle("font-weight:700");
            }
        }
        return (IsValid === true && this.IsValid === true);
    }
    ,
    "ValidateSingleControl":function(collection)
    {
        if (this.elm.IsEmpty(collection)) {return false;}
        var input = null;
        var IsRequired = true;
        var coll = collection;
        
        input = coll.select("test");
        IsRequired = input.hasField("required");
        
        if (input.hasField("must_match"))
        {
            if (!this.MatchMade(input))
            {
                return false;
            }
        }
        if (input.hasField("dependent"))
        {
            if (!this.MustExecute(input))
            {
                return true;
            }
        }
        return input.test(IsRequired);
    }
    ,
    "MustExecute":function(field)
    {
        if (this.elm.IsEmpty(field)) return false;
        
        var name = field.getPredicate();
        if (this.elm.IsEmpty(name)) return false;

        var predicate = this.elm.select(name);
        if (this.elm.IsEmpty(predicate)) return false;
        
        return (predicate.test());       
    }
    ,
    "MatchMade":function(field)
    {
        if (this.elm.IsEmpty(field)) return false;
        
        var name = field.getPredicate();
        if (this.elm.IsEmpty(name)) return false;

        var predicate = this.elm.select(name).select("test");
        if (this.elm.IsEmpty(predicate)) return false;
        
        return (predicate.getValue() == field.getValue());       
    }
    ,
    "ValidateGroupControl":function(collection)
    {
        if (this.elm.IsEmpty(collection)) {return false;}
        var input = null;
        var IsValid = true;
        var IsRequired = true;
        var coll = collection;
        
        input = coll.select("test");

        if (input[0].hasField("must_match"))
        {
            if (!this.MatchMade(input[0]))
            {
                return false;
            }
        }
        if (input[0].hasField("dependent"))
        {
            if (!this.MustExecute(input[0]))
            {
                return true;
            }
        }
        
        IsRequired = input[0].hasField("required");
        if (input[0].hasField("either_or"))
        {
            for (var i = 0; i < input.length; i++)
            {
                if (input[i].test(IsRequired) === true) return true;
            }
        }
        return false;
    }
    ,
    "Reset":function()
    {
        this.ResolveControls();
        this.elm.select("label").setStyle("color:#000000;font-weight:500");
        this.elm.select("can_clear").setValue("");
    }
    ,
    //the flyweight manager
    "elm":null,
    //arrays
    "Fields":null,
    //flags
    "IsValid":true,
    //variables
    "Name":null,
    "Email":null,
    "FriendName":null,
    "FriendEmail":null,
    "Status":null,
    //Regex patterns
    "REGEX_EMAIL":/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/,
    "REGEX_ADDRESS":/^[0-9a-zA-Z\s\.\-\#]+$/,
    "REGEX_NAME":/^[a-zA-Z]+/,
    "REGEX_ZIP":/^\d{5}$/,
    "REGEX_PHONE":/^\D?(\d{3})\D?\D?(\d{3})\D?(\d{4})$/,
    "REGEX_DATE":/^\d{1,2}\/\d{1,2}\/\d{2}$/,
    //error messages
    "MSG_BANNER":"We're sorry, but there are problems with your submission. Some pieces of information are invalid or missing. The problem areas are highlighted in red. Please correct the problems and click \"Submit\" again."
};
