﻿function Applications_System_BasicUserInfoForm_BasicUserInfoForm(id)
{
    this.Inherits(Reflective.Web.UI.UserControl, id, "Applications_System_BasicUserInfoForm_BasicUserInfoForm");    
    this.IncludeMailingAddress = false;
    this.IncludePhoneNumber = true;
    this.IncludeSource = true;
}

Core.setInheritance(Applications_System_BasicUserInfoForm_BasicUserInfoForm, 
    Reflective.Web.UI.UserControl);

Core.extend(Applications_System_BasicUserInfoForm_BasicUserInfoForm,
    {
        OnInit : function()
        {
            this.UserInfoForm.BindEvent("onvalidate", this.id, "Validate");
            if (this.IncludeSource)
            {
                this.UserInfoForm.GetInput("Source").BindEvent("onchange", this.id, "SourceChanged");
            }
                
        },
        SourceChanged : function(sender)
        {
            this.UserInfoForm.GetInput("OtherSource").SetVisible(false);
            this.UserInfoForm.GetInput("DoctorName").SetVisible(false);
            
            if (sender.GetValue() == "Other")
            {
                this.UserInfoForm.GetInput("OtherSource").SetVisible(true, true, 500);
            }
            else if (sender.GetValue() == "Doctor")
            {
                this.UserInfoForm.GetInput("DoctorName").SetVisible(true, true, 500);
            }
            
        },
        Validate : function(sender, args)
        {
            if (this.UserInfoForm.GetInput("FirstName").GetValue() == ""
                || this.UserInfoForm.GetInput("LastName").GetValue() == ""
                || this.UserInfoForm.GetInput("EmailAddress").GetValue() == ""
                || (this.IncludePhoneNumber && this.UserInfoForm.GetInput("PhoneNumber").GetValue() == "")
                || !this.ValidateAddress()
                )
            {
                alert("Please fill out all fields on the form so that we can process your request.");
                args.IsValid = false;
                return;
            }
            else
            {
                var email = this.UserInfoForm.GetInput("EmailAddress").GetValue();
                
                if (email.indexOf("@") == -1 || email.indexOf(".") == -1)
                {
                    alert("It appears you have entered an invalid email address. Please check your entry and try again.");
                    args.IsValid = false;
                    return;
                }
            }
            
            this.FireEvent("onvalidate", args);
        },
        ValidateAddress : function()
        {
            if (!this.IncludeMailingAddress) return true;
            
            return (this.UserInfoForm.GetInput("StreetAddress").GetValue() != ""
                && this.UserInfoForm.GetInput("City").GetValue() != ""
                && this.UserInfoForm.GetInput("State").GetValue() != ""
                && this.UserInfoForm.GetInput("Zip").GetValue() != "");
        },
        GetInput : function(name)
        {
            return this.UserInfoForm.GetInput(name);
        }
        
    }
);
