if (!window.ephip)
    window.ephip = {};

window.ephip.api = {

    showValidationErrorMsg: function (errList) {
        var errorHtml = $('<div style="color:red;font-weight:bold;">Data Validation Failed:</div>');
        for (var i = 0; i < errList.length; i++) {
            $(errorHtml).append('<div>- ' + errList[i] + '</div>');
        }
        wesreid.util.ui.modalWindow.showModalContent({
            html: $(errorHtml).html()
        }, { width: 700, "margin-left": -350 }
        );
        $('html, body').animate({ scrollTop: ($(wesreid.util.ui.modalWindow.modalContentContainer).offset().top - 20) }, 1000);
    },

    // this object to be set by response from successful login
    getUser: function () {
        var usr;
        if (!this.currentAdminEdit)
            usr = eval("(" + $.cookie("USR") + ")");
        else
            usr = this.currentAdminEdit;
        if (!usr)
            usr = { IsArtist: false, IsClient: false, IsAdmin: false, IsPublic: true };
        return usr;
    },

    // this object to be set on bootstrap response
    resources: {
        membershipreneworjoin: "membershipreneworjoin",
        membershipinquiry: "membershipinquiry",
        productrequest: "productrequest"
    },

    membershipRenewOrJoin: function (args) {
        var errList;
        try {
            var frm = $("#" + this.resources.membershipreneworjoin + "Form");
            var requestData = {
                data: ephip.gateway.convertFormToJson(frm[0]),
                resourceName: this.resources.membershipreneworjoin,
                resourceMethod: ephip.gateway.httpMethodType.post
            };
            errList = ephip.api.validation.membershipRenewOrJoin(requestData.data);
            if (errList.length > 0)
                throw new Error(errList);
            requestData.data.PrimaryPersonInfo.BirthDateTime = '\/Date(' + Date.parse(requestData.data.PrimaryPersonInfo.BirthDateTime) + ')\/';
            if (requestData.data.SecondaryPersonInfo.BirthDateTime && requestData.data.SecondaryPersonInfo.BirthDateTime != "")
                requestData.data.SecondaryPersonInfo.BirthDateTime = '\/Date(' + Date.parse(requestData.data.SecondaryPersonInfo.BirthDateTime) + ')\/';
            else
                requestData.data.SecondaryPersonInfo.BirthDateTime = null;

            //frm.attr("resourceMethod", "POST");
            ephip.gateway.makeRequest(
	        ephip.gateway.getActionAjaxOptions(
				requestData,
                null,
                args.success || ephip.gateway.defaultSuccessCallback,
                args.error || ephip.gateway.defaultErrorCallback
	            )
	        );
        }
        catch (err) {
            if (errList)
                ephip.api.showValidationErrorMsg(errList);
        }
    },

    membershipInquiry: function (args) {
        var errList;
        try {
            var frm = $("#" + this.resources.membershipinquiry + "Form");
            var requestData = {
                data: ephip.gateway.convertFormToJson(frm[0]),
                resourceName: this.resources.membershipinquiry,
                resourceMethod: ephip.gateway.httpMethodType.post
            };
            //errList = ephip.api.validation.membershipInquiry(requestData.data);
            if (errList && errList.length > 0)
                throw new Error(errList);
            if (requestData.data.BirthDateTime && requestData.data.BirthDateTime != "")
                requestData.data.BirthDateTime = '\/Date(' + Date.parse(requestData.data.BirthDateTime) + ')\/';
            else
                requestData.data.BirthDateTime = null;

            //frm.attr("resourceMethod", "POST");
            ephip.gateway.makeRequest(
	        ephip.gateway.getActionAjaxOptions(
				requestData,
                null,
                args.success || ephip.gateway.defaultSuccessCallback,
                args.error || ephip.gateway.defaultErrorCallback
	            )
	        );
        }
        catch (err) {
            if (errList)
                ephip.api.showValidationErrorMsg(errList);
        }
    },

    productRequest: function (args) {
        var errList;
        try {
            var frm = $("#" + this.resources.productrequest + "Form");
            var requestData = {
                data: ephip.gateway.convertFormToJson(frm[0]),
                resourceName: this.resources.productrequest,
                resourceMethod: ephip.gateway.httpMethodType.post
            };
            errList = ephip.api.validation.productRequest(requestData.data);
            if (errList.length > 0)
                throw new Error(errList);
            requestData.data.BirthDateTime = '\/Date(' + Date.parse(requestData.data.BirthDateTime) + ')\/';
            if (requestData.data.MedicarePartBDate && requestData.data.MedicarePartBDate != "")
                requestData.data.MedicarePartBDate = '\/Date(' + Date.parse(requestData.data.MedicarePartBDate) + ')\/';
            else
                requestData.data.MedicarePartBDate = null;

            //frm.attr("resourceMethod", "POST");
            ephip.gateway.makeRequest(
	        ephip.gateway.getActionAjaxOptions(
				requestData,
                null,
                args.success || ephip.gateway.defaultSuccessCallback,
                args.error || ephip.gateway.defaultErrorCallback
	            )
	        );
        }
        catch (err) {
            if (errList)
                ephip.api.showValidationErrorMsg(errList);
        }
    },

    validation: {

        regExp: {
            patterns: {
                email: /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/,
                telAreaCode: /^\d{3}$/,
                telExchAndNumber: /^\d{7}$/,
                usZipCode: /^\d{5}\-?(\d{4})?$/,
                ccExpMM: /^\d{2}$/,
                ccExpYYYY: /^\d{4}$/,
                dateMMDDYYYY: /^\d{2}\/\d{2}\/\d{4}$/
            }
        },

        membershipRenewOrJoin: function (data) {
            var errList = [];
            if (data) {
                var o = null;
                if (data.MembershipInfo) {
                    o = data.MembershipInfo;
                    if (data.IsRenewal && $.trim(o.AccountNumber) == "")
                        errList.push("MembershipInfo: AccountNumber must not be empty for a renewal.");
                    if (o.Term != 1 && o.Term != 2 && o.Term != 3)
                        errList.push("MembershipInfo: Term must be 1,2 or 3 [1=OneYear, 2=ThreeYear, 3=FiveYear].");
                    if (data.IsRenewal && $.trim(o.NameOnCard) == "")
                        errList.push("MembershipInfo: NameOnCard must not be empty for a renewal.");
                }
                if (data.AddressInfo) {
                    o = data.AddressInfo;
                    if ($.trim(o.AddressLine1) == "")
                        errList.push("AddressInfo: AddressLine1 must not be empty.");
                    if ($.trim(o.CityName) == "")
                        errList.push("AddressInfo: CityName must not be empty.");
                    if ($.trim(o.CountrySubdivisionCode) == "")
                        errList.push("AddressInfo: CountrySubdivisionCode must not be empty.");
                    if ($.trim(o.PostalCode) == "")
                        errList.push("AddressInfo: PostalCode must not be empty.");
                    if ($.trim(o.CountryCode) == "")
                        errList.push("AddressInfo: CountryCode must not be empty.");
                    if ($.trim(o.TelephoneAreaCode) == "" || (!ephip.api.validation.regExp.patterns.telAreaCode.test(o.TelephoneAreaCode)))
                        errList.push("AddressInfo: TelephoneAreaCode must not be empty and must be 3 digits.");
                    if ($.trim(o.TelephoneExchAndNumber) == "" || (!ephip.api.validation.regExp.patterns.telExchAndNumber.test(o.TelephoneExchAndNumber)))
                        errList.push("AddressInfo: TelephoneExchAndNumber must not be empty and must be 7 digits.");
                }
                if (data.PaymentInfo) {
                    o = data.PaymentInfo;
                    if ($.trim(o.CCNumber) == "")
                        errList.push("PaymentInfo: CCNumber must not be empty.");
                    if ($.trim(o.Type) == "")
                        errList.push("PaymentInfo: Type must not be empty.");
                    if ($.trim(o.NameOnAccount) == "")
                        errList.push("PaymentInfo: NameOnAccount must not be empty.");
                    if ($.trim(o.CardCode) == "")
                        errList.push("PaymentInfo: CardCode must not be empty.");
                    if (!ephip.api.validation.regExp.patterns.ccExpMM.test(o.ExpMM))
                        errList.push("PaymentInfo: ExpMM must be valid format MM.");
                    if (!ephip.api.validation.regExp.patterns.ccExpYYYY.test(o.ExpYYYY))
                        errList.push("PaymentInfo: ExpYYYY must be valid format YYYY.");
                    if (isNaN(o.Amount) || (o.Amount != 16.00 && o.Amount != 43.00 && o.Amount != 63.00))
                        errList.push("PaymentInfo: Amount must be valid number and must be either 16.00, 43.00 or 63.00");

                    if ($.trim(o.BillingAddress.AddressLine1) == "")
                        errList.push("PaymentInfo.BillingAddress: AddressLine1 must not be empty.");
                    if ($.trim(o.BillingAddress.CityName) == "")
                        errList.push("PaymentInfo.BillingAddress: CityName must not be empty.");
                    if ($.trim(o.BillingAddress.CountrySubdivisionCode) == "")
                        errList.push("PaymentInfo.BillingAddress: CountrySubdivisionCode must not be empty.");
                    if ($.trim(o.BillingAddress.PostalCode) == "")
                        errList.push("PaymentInfo.BillingAddress: PostalCode must not be empty.");
                    if ($.trim(o.BillingAddress.CountryCode) == "")
                        errList.push("PaymentInfo.BillingAddress: CountryCode must not be empty.");
                }
                if (data.PrimaryPersonInfo) {
                    o = data.PrimaryPersonInfo;
                    if ($.trim(o.GivenName) == "")
                        errList.push("PrimaryPersonInfo: GivenName must not be empty.");
                    if ($.trim(o.FamilyName) == "")
                        errList.push("PrimaryPersonInfo: FamilyName must not be empty.");
                    if ($.trim(o.BirthDateTime) == "" || isNaN(Date.parse(o.BirthDateTime)))
                        errList.push("PrimaryPersonInfo: BirthDateTime must be MM/DD/YYYY.");
                    if (!ephip.api.validation.regExp.patterns.email.test(o.Email))
                        errList.push("PrimaryPersonInfo: Email must be valid format.");
                }
            }
            return errList;
        },

        membershipInquiry: function (data) {
            var errList = [];
            if (data) {
                var o = null;
                o = data;
                /*
                if (data.IsRenewal && $.trim(o.AccountNumber) == "")
                errList.push("AccountNumber must not be empty for a renewal.");
                */
                if ($.trim(o.GivenName) == "")
                    errList.push("GivenName must not be empty.");
                if ($.trim(o.FamilyName) == "")
                    errList.push("FamilyName must not be empty.");
                if ($.trim(o.BirthDateTime) == "" || isNaN(Date.parse(o.BirthDateTime)))
                    errList.push("BirthDateTime must be MM/DD/YYYY.");
                if ($.trim(o.PostalCode) == "")
                    errList.push("PostalCode must not be empty.");
            }
            return errList;
        },

        productRequest: function (data) {
            var errList = [];
            if (data) {
                var o = null;
                o = data;
                /*
                if (data.IsRenewal && $.trim(o.AccountNumber) == "")
                errList.push("AccountNumber must not be empty for a renewal.");
                */
                if ($.trim(o.GivenName) == "")
                    errList.push("GivenName must not be empty.");
                if ($.trim(o.FamilyName) == "")
                    errList.push("FamilyName must not be empty.");
                if ($.trim(o.BirthDateTime) == "" || isNaN(Date.parse(o.BirthDateTime)))
                    errList.push("BirthDateTime must be MM/DD/YYYY.");
                //if ($.trim(o.MedicarePartBDate) == "" || isNaN(Date.parse(o.MedicarePartBDate)))
                //    errList.push("MedicarePartBDate must be MM/DD/YYYY.");
                //if ($.trim(o.Gender) == "")
                //    errList.push("Gender must be M, F or U.");
                if ($.trim(o.InsuredStatusCode) == "")
                    errList.push("InsuredStatusCode must be IM, UM or NM.");
                if ($.trim(o.AddressLine1) == "")
                    errList.push("AddressLine1 must not be empty.");
                if ($.trim(o.City) == "")
                    errList.push("City must not be empty.");
                if ($.trim(o.StateOrProvince) == "")
                    errList.push("StateOrProvince must not be empty.");
                if ($.trim(o.PostalCode) == "")
                    errList.push("PostalCode must not be empty.");
            }
            return errList;
        }
    }

}
