﻿// Note: The ‘path’ variable gets set to the appropriate folder holding the plain login pages.
var ZimpleMoney = ZimpleMoney ? ZimpleMoney :
{
    API: {

        // api properties: path & stylesheet to be set by client
        path: "",
        stylesheet: "",

        // display the page using URL parsing
        display: function(id, world, qsopt) {
            var page = ZimpleMoney.API.getQueryString("p");
            ZimpleMoney.API.setZimpleSource(id, page, world, qsopt);
        },
        // display the login page
        displayLogin: function(id, world, qsopt) {
            ZimpleMoney.API.setZimpleSource(id, "login", world, qsopt);
        },
        // display the login 'widget' page
        displayLoginWidget: function(id, world, login, qsopt) {
            ZimpleMoney.API.setZimpleSource(id, "loginwidget", world, qsopt);
            var el = document.getElementById(id);
            if (el && el.src) {
                if (login) {
                    el.src = el.src + "&login=" + login;
                }
            }
        },
        // display the register page
        displayRegister: function(id, world, qsopt) {
            ZimpleMoney.API.setZimpleSource(id, "register", world, qsopt);
        },
        // display the register acknowledge page
        displayRegisterAcknowledge: function(id, world, qsopt) {
            ZimpleMoney.API.setZimpleSource(id, "acknowledge", world, qsopt);
        },
        // display the contact us page
        displayContactUs: function(id, world, qsopt) {
            ZimpleMoney.API.setZimpleSource(id, "contactus", world, qsopt);
        },
        // display the forgot password page
        displayForgotPassword: function(id, world, qsopt) {
            ZimpleMoney.API.setZimpleSource(id, "forgot", world, qsopt);
        },
        // display the forgot password reset page
        displayForgotReset: function(id, world, qsopt) {
            ZimpleMoney.API.setZimpleSource(id, "reset", world, qsopt);
        },
        // set the IFrame based on supplied values
        setZimpleSource: function(id, page, world, qsopt) {
            var el = document.getElementById(id);
            if (el && el.src) {
                /* var path = "http://demo.aki.com/Zimple/Dev/Ext/"; */
                var url = "login.aspx";

                switch (page) {
                    case "login": url = "Login.aspx"; break;
                    case "loginwidget": url = "LoginWidget.aspx"; break;
                    case "register": url = "Register.aspx"; break;
                    case "acknowledge": url = "RegisterAcknowledge.aspx"; break;
                    case "forgot": url = "Forgot.aspx"; break;
                    case "reset": url = "ForgotReset.aspx"; break;
                    case "contactus": url = "ContactUs.aspx"; break;
                }

                var qs = "";
                if (world) qs = "w=" + world;
                if (ZimpleMoney.API.stylesheet) {
                    if (qs)
                        qs += "&s=" + ZimpleMoney.API.stylesheet;
                    else
                        qs = "s=" + ZimpleMoney.API.stylesheet;
                }
                var oqs = ZimpleMoney.API.queryString();
                if (oqs) {
                    if (qs)
                        qs += "&" + oqs;
                    else
                        qs = oqs;
                }
                if (qsopt) {
                    if (qs)
                        qs += "&" + qsopt;
                    else
                        qs = qsopt;
                }

                if (qs) qs = "?" + qs;

                el.src = ZimpleMoney.API.path + url + qs;
            }
        },
        // get the entire query string
        queryString: function() {
            var tmp = (location.search.substring(1));
            return tmp;
        },
        // get the specified its fro the query string.  Empty string is returned if not found.
        getQueryString: function(name) {
            var tmp = (location.search.substring(1));
            var i = tmp.toUpperCase().indexOf(name.toUpperCase() + "=");

            if (i >= 0) {
                tmp = tmp.substring(name.length + i + 1);
                i = tmp.indexOf("&");
                return unescape(tmp = tmp.substring(0, (i >= 0) ? i : tmp.length));
            }

            return ("");
        }
    }
}

