﻿/*
Signup.js
Created 1/21/2009 by Dan Roentsch - Ogilvy
Validate form controls.
*/
Signup = {
    "ResolveControls":function()
    {
        this.Fields = [];
        
        this.elm = new elm();
        
        this.elm.make(this.FirstName, "su_first_name","textbox","required","test").setRegex(this.REGEX_NAME);
        this.elm.make(this.FirstNameLabel, "su_first_name","label");
        this.Fields.push("su_first_name");
        
        this.elm.make(this.LastName, "su_last_name","textbox","required","test").setRegex(this.REGEX_NAME);
        this.elm.make(this.LastNameLabel, "su_last_name","label");
        this.Fields.push("su_last_name");

        this.elm.make(this.Address1, "su_address1","textbox","required","test").setRegex(this.REGEX_ADDRESS);
        this.elm.make(this.Address1Label, "su_address1","label");
        this.Fields.push("su_address1");

        //this.elm.make(this.Address2, "su_address2","textbox","optional","test").setRegex(this.REGEX_ADDRESS2);
        //this.elm.make(this.Address2Label, "su_address2","label");
        //this.Fields.push("su_address2");

        this.elm.make(this.City, "su_city","textbox","required","test").setRegex(this.REGEX_NAME);
        this.elm.make(this.CityLabel, "su_city","label");
        this.Fields.push("su_city");

        this.elm.make(this.State, "su_state","drop_down","required","test");
        this.elm.make(this.StateLabel, "su_state","label");
        this.Fields.push("su_state");

        this.elm.make(this.Zip, "su_zip","textbox","required","test").setRegex(this.REGEX_ZIP);
        this.elm.make(this.ZipLabel, "su_zip","label");
        this.Fields.push("su_zip");

        this.elm.make(this.Email, "su_email","textbox","required","test").setRegex(this.REGEX_EMAIL);
        this.elm.make(this.EmailLabel, "su_email","label");
        this.Fields.push("su_email");

        this.elm.make(this.EmailConfirm, "su_email_confirm","textbox","required","test","pred:su_email","must_match").setRegex(this.REGEX_EMAIL);
        this.elm.make(this.EmailConfirmLabel, "su_email_confirm","label");
        this.Fields.push("su_email_confirm");

        this.elm.make(this.Phone, "su_phone","textbox","optional","test").setRegex(this.REGEX_PHONE);
        this.elm.make(this.PhoneLabel, "su_phone","label");
        this.Fields.push("su_phone");

        this.elm.make(this.DOB, "su_DOB","textbox","required","test").setRegex(this.REGEX_DATE);
        this.elm.make(this.DOBLabel, "su_DOB","label");
        this.Fields.push("su_DOB");

        this.elm.make(this.Male, "male","radio","optional","test","is_group","su_gender","either_or");
        this.elm.make(this.Female, "female","radio","optional","test","is_group","su_gender","either_or");
        this.elm.make(this.GenderLabel, "su_gender","label");
        this.Fields.push("su_gender");

        this.elm.make(this.OAB, "su_OAB","drop_down","required","test");
        this.elm.make(this.OABLabel, "su_OAB","label","bold_on_error");
        this.Fields.push("su_OAB");

        this.elm.make(this.LoseControlYes, "lose_control_yes","radio","required","test","is_group","su_lose_control","either_or");
        this.elm.make(this.LoseControlNo, "lose_control_no","radio","required","test","is_group","su_lose_control","either_or");
        this.elm.make(this.LoseControlLabel, "su_lose_control","label","bold_on_error");
        this.Fields.push("su_lose_control");

        this.elm.make(this.Frequently, "su_frequently","drop_down","required","test");
        this.elm.make(this.FrequentlyLabel, "su_frequently","label","bold_on_error");
        this.Fields.push("su_frequently");
        
        this.elm.make(this.TreatedYes, "treated_yes","radio","required","test","is_group","su_treated","either_or");
        this.elm.make(this.TreatedNo, "treated_no","radio","required","test","is_group","su_treated","either_or");
        this.elm.make(this.TreatedLabel,"su_treated","label","bold_on_error","bold_on_error");        
        this.Fields.push("su_treated");
        
        this.elm.make(this.WithWhat,"su_with_what","drop_down","required","test","dependent","pred:treated_yes");
        this.elm.make(this.WithWhatLabel,"su_with_what","label");
        this.Fields.push("su_with_what");

        this.elm.make(this.HowLong, "su_how_long","drop_down","required","test","dependent","pred:treated_yes");
        this.elm.make(this.HowLongLabel, "su_how_long","label");
        this.Fields.push("su_how_long");

        this.elm.make(this.InPastYes, "in_past_yes","radio","required","test","is_group","su_in_past","either_or","dependent","pred:treated_no");
        this.elm.make(this.InPastNo, "in_past_no","radio","required","test","is_group","su_in_past","either_or","dependent","pred:treated_no");
        this.elm.make(this.InPastLabel, "su_in_past","label","bold_on_error");
        this.Fields.push("su_in_past");

        this.elm.make(this.SendMeInfo, "su_send_me_info","check_box","required","test");
        this.elm.make(this.SendMeInfoLabel, "su_send_me_info","label");
        this.Fields.push("su_send_me_info");
    }  //end ResolveControls()
    ,
    "Reset":function()
    {
        document.forms[0].reset();
        // remove the existing error messge
        if (!this.elm.IsEmpty(this.theBanner))
        {
            this.theBanner.remove();
        }

        for (var i = 0;i<this.elm.elms.length;i++)
        {
            if (this.elm.elms[i].hasField("label"))
            {
                this.elm.elms[i].setStyle("color:#000000;font-weight:500");
            }
        }                
    }
    ,
    "ValidateForm":function()
    {
        var HasErrors = false;
        this.IsValid = true;        
        try
        {
            this.ResolveControls();
            
            // remove the existing error messge
            if (!this.elm.IsEmpty(this.theBanner))
            {
                this.theBanner.remove();
            }

        //check controls for errors
            for (var i = 0; i < this.Fields.length; i++)
            {
                this.IsValid = this.ValidateControl(this.Fields[i]);
            }
        }
        catch(err)
        {
            HasErrors = true;
            this.theBanner = this.elm.make("HeaderText").addText(this.MSG_ERROR,"div","color:#ff0000;font-size:xx-small");
        }
        
        //the verdict
        if (this.IsValid == false)
        {
            this.theBanner = this.elm.make("HeaderText").addText(this.MSG_BANNER,"div","color:#ff0000;font-size:xx-small");
        }
        return (this.IsValid && !HasErrors);
    }//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;
    }
    ,
    //the flyweight manager
    "elm":null,
    //arrays
    "Fields":null,
    //flags
    "IsValid":true,
    //variables
    "theBanner":null,
    "FirstName":null,
    "FirstNameLabel":null,
    "LastName":null,
    "LastNameLabel":null,
    "Address1":null,
    "Address1Label":null,
    "Address2":null,
    "Address2Label":null,
    "City":null,
    "CityLabel":null,
    "State":null,
    "StateLabel":null,
    "Zip":null,
    "ZipLabel":null,
    "Email":null,
    "EmailLabel":null,
    "EmailConfirm":null,
    "EmailConfirmLabel":null,
    "Phone":null,
    "PhoneLabel":null,
    "DOB":null,
    "DOBLabel":null,
    "Male":null,
    "FemaleLabel":null,
    "Gender":null,
    "OAB":null,
    "OABLabel":null,
    "LoseControlYes":null,
    "LoseControlNo":null,
    "LoseControlLabel":null,
    "Frequently":null,
    "FrequentlyLabel":null,
    "TreatedYes":null,
    "TreatedNo":null,
    "TreatedLabel":null,
    "WithWhat":null,
    "WithWhatLabel":null,
    "HowLong":null,
    "HowLongLabel":null,
    "InPastYes":null,
    "InPastNo":null,
    "InPastLabel":null,
    "SendMeInfo":null,
    "SendMeInfoLabel":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":/\w+/,
    "REGEX_ADDRESS2":/\w./,
    "REGEX_NAME":/^[a-zA-Z]+/,
    "REGEX_ZIP":/^\d{5}$/,
    "REGEX_PHONE":/^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$/,
    "REGEX_DATE":/^([1-9]|0[1-9]|1[012])[/](0[1-9]|[12][0-9]|3[01])[/]\d\d/,
    //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.",
    "MSG_ERROR":"We're sorry, but an error occurred while trying to submit this form."
};

