﻿/*
Unsubscribe.js
Created 1/26/2009 by Dan Roentsch - Ogilvy
Validate form controls.
*/
Unsubscribe = {
    "ResolveControls":function(
                                txtEmail
                              )
    {
        this.Fields = [];
        
        this.elm = new elm();
        
        this.elm.make(txtEmail, "unsub_email","textbox","required","test").setRegex(this.REGEX_EMAIL);
        this.elm.make("lblUnsubEmail", "unsub_email","label");
        this.Fields.push("unsub_email");
    }  //end ResolveControls()
    ,
    "Reset":function()
    {
        document.forms[0].reset();
        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()
    {
        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 = 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,
    //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})(\]?)$/
};

function trackLink(link_obj,link_name)
{
    if (typeof(link_obj)=== "undefined" || link_obj === null) return;
    var l=s_gi(s_account);
    
    var linkName = null;
    var linkType = null;
    var temp_page_name = null;
    
    if (typeof(tracker) !== "undefined" && tracker != null)
    {
        temp_page_name = tracker.pageName;
    }
    else
    {
        temp_page_name = new String(s.pageName);
    }
        
        alert("HERE!!!");
    switch(link_name.toLowerCase())
    {
		
        case "3_oab_progress_journal.aspx":
			
            linkName = temp_page_name+" | Download OAB Progress Journal PDF";
            l.linkTrackVars = "events";
            linkType = "d";
            l.events = "event7";
            break;
        case "print":
            l.linkTrackVars = "events,prop13,eVar10";
            l.prop13 = temp_page_name+" | Print";
            l.eVar10 = temp_page_name+" | Print";
            linkName = temp_page_name+" | print";
            l.events = "event19";
            linkType = "o";
            break;
        case "send_to_friend":
            l.linkTrackVars = "events,prop13,eVar10";
            l.prop13 = temp_page_name+" | Send to friend form";
            l.eVar10 = temp_page_name+" | Send to friend form";
            linkName = temp_page_name+" | Send to friend form";
            l.events = "";
            linkType = "o";
            break;
        case "send_to_friend_submit":
            link_obj = new Object();
            l.linkTrackVars = "events,prop13,eVar10";
            l.prop13 = temp_page_name+" | Send to friend submitted";
            l.eVar10 = temp_page_name+" | Send to friend submitted";
            linkName = temp_page_name+" | Send to friend sent";
            l.events = "event14";
            linkType = "o";
            break;
    }
    try
    {
        l.tl(link_obj,linkType,linkName);
    }
    catch(Error)
    {
        ;
    }
    return;
}

