﻿/* Build namespace */
if (NMR == undefined) var NMR = {};
if (NMR.Common == undefined) NMR.Common = {};
if (NMR.Common.Web == undefined) NMR.Common.Web = {};
if (NMR.Common.Web.GenericControls == undefined) NMR.Common.Web.GenericControls = {};

/*
<summary>
Constructor function, passes arguments to constructor in prototype
</summary>
*/
NMR.Common.Web.GenericControls.ChangePasswordPopUp = function()
{
    // Call base constructor
    myBase(this, NMR.Common.Web.GenericControls.GenericBase.prototype.create, arguments);
    
    // Call constructor
    this.create.apply(this, arguments);
}

/*
<summary>
Defines the clientside JavaScript object for the ChangePasswordPopUp control
</summary>
*/
NMR.Common.Web.GenericControls.ChangePasswordPopUp.prototype = {

    /*
    <summary>
    </summary>
    */
    create: function()
    {
        this.attachEventHandlers(false);
    },
    
    /*
    <summary>
    </summary>
    */
    attachEventHandlers: function(detach)
    {
        myBase(this, NMR.Common.Web.GenericControls.GenericBase.prototype.attachEventHandlers, arguments);
        var me = this;
        if(!detach)
        {
            // Attach custom event handlers
            var glbEvt = NMR.Common.Web.GenericControls.GlobalEvents.getCustomEvent("onPopupClosed");
            if(!glbEvt)
            {
                glbEvt = new NMR.Common.CustomEvent("onPopupClosed");
                NMR.Common.Web.GenericControls.GlobalEvents.addCustomEvent(glbEvt);
            }
            glbEvt.addHandler(function(sender, eventArgs){me.popupClosed(sender, eventArgs);});
        
        }
        else
        {
        
        }
    },
    
    /*
    <summary>
    </summary>
    */
    showDialog: function()
    {
        var frmContent = document.getElementById("ifrCPPopup");
        frmContent.src = document.getElementById("hdnCPPopupURL").value;
        var popupCP = document.getElementById("CPDivPopup");
        var baseDivCP = document.getElementById("DivCPShim");
        var parentPopupZIndex = document.getElementById("CPDivPopup").style.zIndex;
        if (parentPopupZIndex == "") {parentPopupZIndex = 0;} else {parentPopupZIndex = parseInt(parentPopupZIndex);}
        var newZIndex = parentPopupZIndex + 100;
        popupCP.style.zIndex = newZIndex;

        // Position the popup DIV in the centre of the screen 
        popupCP.style.top = "80px";       
        popupCP.style.left = "440px";     
        popupCP.style.display = "block";

        // Position the base IFRAME underneath the DIV to hide parent window elements.  
        baseDivCP.style.width = (parseInt(popupCP.offsetWidth)) + "px";    
        baseDivCP.style.height = (parseInt(popupCP.offsetHeight) - 10) + "px";
        baseDivCP.style.top = parseInt(popupCP.style.top) + "px";
        baseDivCP.style.left = parseInt(popupCP.style.left) + "px";
        baseDivCP.style.zIndex = newZIndex - 1;
        baseDivCP.style.display = "block";   
    },
    
    /*
    <summary>
    Bubbles any child, sub-child's onPopupClosed events up the tree.
    </summary>
    <param name="sender">The instance of Generic Popup raising the event</param>
    <param name="eventArgs">Any event arguments</param>
    <remarks>
    Since window is never null, there is no easy way to tell when the top of the 'window' tree has been reached. Therefore, 
    determination is made on the window objects - these *should* always be unique.
    </remarks>
    */
    popupClosed: function(sender, eventArgs)
    {
        if(window.parent && window.parent.NMR && (window != window.parent)){window.parent.NMR.Common.Web.GenericControls.GlobalEvents.raiseCustomEvent("onPopupClosed", sender, eventArgs);}
        if(sender != this)
        {
            document.getElementById('CPDivPopup').style.display='none';
            document.getElementById('DivCPShim').style.display='none';
        }
    }
     
};
NMR.Common.Web.GenericControls.ChangePasswordPopUp.prototype = prototypeExtend(NMR.Common.Web.GenericControls.GenericBase.prototype,NMR.Common.Web.GenericControls.ChangePasswordPopUp.prototype);   

/*
<summary>
Constructor function, passes arguments to constructor in prototype
</summary>
*/
NMR.Common.Web.GenericControls.ChangePassword = function()
{
    // Call base constructor
    myBase(this, NMR.Common.Web.GenericControls.GenericBase.prototype.create, arguments);
    
    // Call constructor
    this.create.apply(this, arguments);
}

/*
<summary>
Defines the clientside JavaScript object for the ChangePassword control
</summary>
*/
NMR.Common.Web.GenericControls.ChangePassword.prototype = {

    /* Enumerators */
    resultOperation: {                          // Result of asynchronous operation on server
        Unknown:              0,
        ChangedPassword:      1,
        FailedChangePassword: 2
    },
    
    requestedOperation: {                       // Requested (a)synchronous operation on server 
        Unknown:              0,
        ChangePassword:       1
    },
    
    /* Properties */
    isUsingCallback: null,

    /*
    <summary>
    </summary>
    */
    create: function()
    {
        this.isUsingCallback = arguments[4];
        this.attachEventHandlers(false);
    },
    
    /*
    <summary>
    </summary>
    */
    attachEventHandlers: function(detach)
    {
        myBase(this, NMR.Common.Web.GenericControls.GenericBase.prototype.attachEventHandlers, arguments);
        var me = this;
        if(!detach)
        {
            // Attach DOM event handlers
            if(document.addEventListener)
            {
                // for Firefox
                window.addEventListener("load",function(){me.loadedHandler(me, null);}, false); 
            }
            else if(document.attachEvent) 
            {   
                // for IE
                window.attachEvent("onload", function(){me.loadedHandler(me, event);});
            }
        }
        else
        {
            // Detach DOM event handlers
            if(document.removeEventListener)
            {
                // for Firefox
                window.removeEventListener("load",function(){me.loadedHandler(me, null);}, false);
            }
            else if(document.detachEvent)
            {
                // for IE
                window.detachEvent("onload", function(){me.loadedHandler(me, event);});
            }
        }
    },
    
    /*
    <summary>
    Processes the result of any callback operation.
    </summary>
    <param name="result">String containing the encoded response from the server.</param>
    <param name="context">The object that raised the callback request (E.g. the JavaScript instance of the ManagementRecipients control)</param>
    */
    returnCallback: function(result, context)
    {
        // Call base method
        myBase(context, NMR.Common.Web.GenericControls.GenericBase.prototype.returnCallback, arguments);
        var rsltOp = parseInt(result.substring(0,result.indexOf(":",0)));   // The callback operation
        var rslt = result.substring(result.indexOf(":",0) + 1);             // Any additional callback return data (E.g. markup)
        switch (rsltOp)
        { 
            case context.resultOperation.ChangedPassword:
                alert(rslt);
                context.hideDialog();
                break;
                
            case context.resultOperation.FailedChangePassword:
                alert(rslt);
                var els = document.getElementById(context.clientObjectId + "_base").getElementsByTagName("input");
                for(var x=0;x<els.length;x++)
                {
                    if(els[x].attributes["type"] && (els[x].attributes["type"].nodeValue.toLowerCase() == "password"))
                    {
                        els[x].value = "";
                    }
                }
                document.getElementById(context.clientObjectId + "_txtOldPassword").focus();
                break;
                
            case context.resultOperation.Unknown:
                break;
                // Do nothing
        }
       
    },
    
    /*
    <summary>
    Handler of the onload event.
    </summary>
    <param name="sender">Object raising the event</param>
    <param name="e">Event arguments</param>
    */
    loadedHandler: function(sender, e)
    {
        // ADX Press / AM 'hack'
        var lblCont = document.getElementById("lblContact");
        if(lblCont){lblCont.style.display = "none";}
        var lgCont = document.getElementById("lgcont");
        if(lgCont){lgCont.className = "divlogincontainerNl";}
        
        // Rewire the submit button to use a callback
        if(this.isUsingCallback)
        {
            var me = this;
            var submit = document.getElementById(this.clientObjectId + "_btnSubmit");
            submit.onclick = function(){me.changePassword();}
        }
    },
    
    /*
    <summary>
    Submits the change password request
    </summary>
    */
    changePassword:function()
    {
        var me = this;
        if (typeof(Page_ClientValidate) == 'function')
        { 
            if(Page_ClientValidate(''))
            {
                var xmlChild;
                var xmlRoot;
                // Create and initialise xmlDocument
                var xmlDoc = (document.all)?new ActiveXObject("Microsoft.XMLDOM"):document.implementation.createDocument("", "", null);
                xmlDoc.async = false;
                xmlRoot = xmlDoc.createElement("ROOT");
                xmlChild = xmlDoc.createElement("pw");
                xmlChild.setAttribute("old", document.getElementById(this.clientObjectId + "_txtOldPassword").value);
                xmlChild.setAttribute("new", document.getElementById(this.clientObjectId + "_txtNewPassword").value);
                xmlRoot.appendChild(xmlChild);

                me.initialiseCallback(me.requestedOperation.ChangePassword+":"+me.serializeForTransport(xmlRoot),me);
            }
        }
    },
        
    /*
    <summary>
    </summary>
    <param name="source"></param>
    <param name="args"></param>
    */
    checkNewPasswordContent: function(source, args)
    {
    	if (args.Value.length < 6) {
		    // check minimum length requirements 
		    args.IsValid = false;
		    return;
	    } else {
		    // check complexity requirements
		    var sValidate = args.Value.split('');
		    var sFirstChar = sValidate[0];
		    var bFound = false;
		    for (var i = 0 ; i < args.Value.length; i++) {
			    // make sure at least 1 char is different from the rest of the password
			    if (sValidate[i] != sFirstChar) {
				    bFound=true;
				    break;
			    }
		    }
		    args.IsValid = bFound;
	    }
    },
    
    /*
    <summary>
    Hides the parent ChangePasswordPopup dialog (if it exists)
    </summary>
    */
    hideDialog: function()
    {
        if(window.parent && window.parent.NMR){window.parent.NMR.Common.Web.GenericControls.GlobalEvents.raiseCustomEvent("onPopupClosed", this, null);}
    }
};
NMR.Common.Web.GenericControls.ChangePassword.prototype = prototypeExtend(NMR.Common.Web.GenericControls.GenericBase.prototype,NMR.Common.Web.GenericControls.ChangePassword.prototype); 