﻿// JScript File
//Changes Password 
//Uses yahoo-min.js and event-min for Event Handling

var _objValuePwd = 'objValuePwd';
var _objChgPwd = 'objChgPwd';
var _objTagUser = 'objTagUser';
var _objTagUserH = 'objTagUserH';
var _objValueUser = 'objValueUser';
var _objTagPwd = 'objTagPwd';
var _objTagChkPwd = 'objTagChkPwd';
var _objValueChkPwd = 'objValueChkPwd';
var _objbtnOk = 'objbtnOk';
var _objbtnCancel = 'objbtnCancel';
var _messagesetTimeOut = 0;
var _AllOk = false;
var _setTimeOut = 0;

function OpenWindowChangePwd(id , options , optionsFuncs , optionsParams , optionsTips , Type , ContentAreawidth , ContentAreaheight , WithScroll , singleTon , WithMessage , WithResize)
{   
    if (typeof ChangePwdWindow == 'undefined')
    {
        ChangePwdWindow = new AJAXWindow('WindowChgPwd' , '' , '' , '' , '' , 'Control' , '0px' , '0px' , "False" , "False" , "True" , "True");
        ChangePwdWindow.SetLocalValues(id , options , optionsFuncs , optionsParams , optionsTips , Type , ContentAreawidth , ContentAreaheight , WithScroll , WithMessage , WithResize);
        ChangePwdWindow.overrideSize('520px', '200px');
        //ChangePwdWindow.AJAXWindowinitOut();
    }
    else
    {
        //ChangePwdWindow.AJAXWindowinitOut();
    }
    return ChangePwdWindow;
}

function ChangePwd(id , options , optionsFuncs , optionsParams , optionsTips , Type , ContentAreawidth , ContentAreaheight , WithScroll , singleTon , WithMessage , WithResize , elemUser , valueUser , elemPwd , elemChkPwd , elemOk , elemCancel , reqFldTxtPwd , regExpTxtUserLogin , LengthUser , reqFldTxtChkPwd , compFldTxtChkPwd , challengeURL , ChgPwdURL , Scripts , challenge , encValue)
{      
    this.id = id;
    this.options = options;
    this.optionsFuncs = optionsFuncs;
    this.optionsParams = optionsParams;
    this.optionsTips = optionsTips;
    this.ContentAreawidth = ContentAreawidth;
    this.ContentAreaheight = ContentAreaheight;
    this.WithScroll = WithScroll;
    this.singleTon = singleTon;
    this.elemUser = elemUser;
    this.valueUser = valueUser;
    this.elemPwd = elemPwd;
    this.elemChkPwd = elemChkPwd;
    this.elemOk = elemOk;
    this.elemCancel = elemCancel;
    this.WindowRef = null;
    this.reqFldTxtPwd = reqFldTxtPwd;
    this.regExpTxtUserLogin = regExpTxtUserLogin;
    this.LengthUser = LengthUser;
    this.reqFldTxtChkPwd = reqFldTxtChkPwd;
    this.compFldTxtChkPwd = compFldTxtChkPwd;
    this.challengeURL = challengeURL;
    this.ChgPwdURL = ChgPwdURL;
    this.Scripts = Scripts;
    this.fieldInError = _objValuePwd;
    this.value = '';
    this.getRSA_E = '';
    this.getRSA_M = '';
    this.challenge = challenge;
    this.challengevalue = '';
    this.encValue = encValue;
}

ChangePwd.prototype.openWindowChangePwd = function (params)
{   
    this.WindowRef = OpenWindowChangePwd(this.id , this.options , this.optionsFuncs , this.optionsParams , this.optionsTips , this.Type , this.ContentAreawidth , this.ContentAreaheight , this.WithScroll , this.singleTon , this.WithMessage , this.WithResize);
    if (! this.WindowRef.WindowIsOpen)
    {
        this.WindowRef.AJAXWindowinitOut();
        this.InitWindow(this.WindowRef , this);
    }
    else
    {        
        AJAXModalMessageobj = new AJAXModalMessage('' , 400 , 400 , this , '' , 'Modal');	// We only create one object of this class
        AJAXModalMessageobj.displayStaticMessage();
    }
};

ChangePwd.prototype.InitWindow = function(WindowRef, outer) {
    var objChgPwd = document.createElement('div');
    objChgPwd.id = _objChgPwd;
    document.body.appendChild(objChgPwd);
    var objTagUser = document.createElement('div');
    objTagUser.id = _objTagUser;
    objTagUser.innerHTML = this.elemUser;
    objChgPwd.appendChild(objTagUser);
    var objValueUser = document.createElement('input');
    objValueUser.id = _objValueUser;
    objValueUser.value = this.valueUser;
    objValueUser.setAttribute("disabled", "disabled");
    objChgPwd.appendChild(objValueUser);
    objChgPwd.appendChild(document.createElement('br'));
    objChgPwd.appendChild(document.createElement('br'));
    var objTagPwd = document.createElement('div');
    objTagPwd.id = _objTagPwd;
    objTagPwd.innerHTML = this.elemPwd;
    objChgPwd.appendChild(objTagPwd);
    var objValuePwd = document.createElement('input');
    objValuePwd.id = _objValuePwd;
    objValuePwd.setAttribute("type", "password");
    objChgPwd.appendChild(objValuePwd);
    objChgPwd.appendChild(document.createElement('br'));
    objChgPwd.appendChild(document.createElement('br'));
    var objTagChkPwd = document.createElement('div');
    objTagChkPwd.id = _objTagChkPwd;
    objTagChkPwd.innerHTML = this.elemChkPwd;
    objChgPwd.appendChild(objTagChkPwd);
    var objValueChkPwd = document.createElement('input');
    objValueChkPwd.id = _objValueChkPwd;
    objValueChkPwd.setAttribute("type", "password");
    objChgPwd.appendChild(objValueChkPwd);
    objChgPwd.appendChild(document.createElement('br'));
    var objbtnOk = document.createElement('div');
    objbtnOk.id = _objbtnOk;
    objbtnOk.className = "btnAction";
    var objbtnOka = document.createElement('a');
    objbtnOka.href = "javascript:void(0);";
    objbtnOka.innerHTML = this.elemOk;
    objbtnOk.appendChild(objbtnOka);
    objChgPwd.appendChild(objbtnOk);
    objbtnOk.onclick = (function(obj) {
        return function() {
            obj.ValidateClientChgPwd();
        };
    })(outer);
    var objbtnCancel = document.createElement('div');
    objbtnCancel.id = _objbtnCancel;
    objbtnCancel.className = "btnAction";
    var objbtnCancela = document.createElement('a');
    objbtnCancela.href = "javascript:void(0);";
    objbtnCancela.innerHTML = this.elemCancel;
    objbtnCancel.appendChild(objbtnCancela);
    objChgPwd.appendChild(objbtnCancel);
    objbtnCancel.onclick = (function(obj) {
        return function() {
            obj.CloseWindow();
        };
    })(outer);
    WindowRef.setDOMContent(objChgPwd);
    WindowRef.OverrideMessage("", null);
};

ChangePwd.prototype.ValidateClientChgPwd = function ()
{
    clearTimeout(_setTimeOut);
    clearTimeout(_messagesetTimeOut);
    document.getElementById(this.fieldInError).style.backgroundColor = "#FFFFFF";
    this.WindowRef.OverrideMessage("" , null);  
    setOpacity(this.WindowRef.AJAXMessage , 10);
    if (document.getElementById(_objValuePwd).value == "")
    {   
        this.fieldInError = _objValuePwd;
        document.getElementById(_objValuePwd).style.backgroundColor = "#D1B4B0";
        _setTimeOut = setTimeout(function ()
        {
            document.getElementById(_objValuePwd).style.backgroundColor = "#FFFFFF";
        }, 5000);
        this.WindowRef.OverrideMessage(this.reqFldTxtPwd, "default");
        _messagesetTimeOut = FadeMessage(this.WindowRef.AJAXMessage, "True", 5);
        return false;
    }
    if (document.getElementById(_objValueChkPwd).value == "") 
    {
        this.fieldInError = _objValueChkPwd;
        document.getElementById(_objValueChkPwd).style.backgroundColor = "#D1B4B0";
        _setTimeOut = setTimeout(function ()
        {
            document.getElementById(_objValueChkPwd).style.backgroundColor = "#FFFFFF";
        }, 5000);
        this.WindowRef.OverrideMessage(this.reqFldTxtChkPwd, "default");
        _messagesetTimeOut = FadeMessage(this.WindowRef.AJAXMessage, "True", 5);
        return false;
    }
    var value = this.LengthUser;
    var re = new RegExp(this.LengthUser);
    if ((document.getElementById(_objValuePwd).value.match(re)))
    {
    } 
    else
    {
        this.fieldInError = _objValuePwd;
        document.getElementById(_objValuePwd).style.backgroundColor = "#D1B4B0";
        _setTimeOut = setTimeout(function ()
        {
            document.getElementById(_objValuePwd).style.backgroundColor = "#FFFFFF";
        }, 5000);
        this.WindowRef.OverrideMessage(this.regExpTxtUserLogin, "default");
        _messagesetTimeOut = FadeMessage(this.WindowRef.AJAXMessage, "True", 5);
        return false;
    } 
    if ((document.getElementById(_objValueChkPwd).value.match(re)))
    {
    } 
    else
    {
        this.fieldInError = _objValueChkPwd;
        document.getElementById(_objValueChkPwd).style.backgroundColor = "#D1B4B0";
        _setTimeOut = setTimeout(function ()
        {
            document.getElementById(_objValueChkPwd).style.backgroundColor = "#FFFFFF";
        }, 5000);
        this.WindowRef.OverrideMessage(this.regExpTxtUserLogin, "default");
        _messagesetTimeOut = FadeMessage(this.WindowRef.AJAXMessage, "True", 5);
        return false;
    } 
    if (document.getElementById(_objValuePwd).value == document.getElementById(_objValueChkPwd).value)
    {
    } 
    else    
    {
        this.fieldInError = _objValuePwd;
        document.getElementById(_objValuePwd).style.backgroundColor = "#D1B4B0";
        _setTimeOut = setTimeout(function ()
        {
            document.getElementById(_objValuePwd).style.backgroundColor = "#FFFFFF";
        } , 5000);
        this.WindowRef.OverrideMessage(this.compFldTxtChkPwd, "default");
        _messagesetTimeOut = FadeMessage(this.WindowRef.AJAXMessage, "True", 5);
        return false;
    }        
    Disable(_objbtnCancel);
    Disable(_objbtnOk);
    this.GetChallenge();
};

ChangePwd.prototype.CloseWindow = function ()
{   
    this.WindowRef.fadeOutMyPopup(this.WindowRef);
    return false;
};

ChangePwd.prototype.GetChallenge = function ()
{   
    var outer = this;
    this.returnedCallback = 
    {
        success: function(t) 
        {
            outer.completeChallenge(t);
        },
        failure: function(t) 
        {
            outer.ShowCallBackErrorChallenge(t);
        }
    }
    var curDateTime = new Date()
    var curHour = curDateTime.getHours()
    var curMin = curDateTime.getMinutes()
    var curSec = curDateTime.getSeconds()
    var curTime = ((curHour < 10) ? "0" : "") + curHour + ":" + ((curMin < 10) ? "0" : "") + curMin + ":" + ((curSec < 10) ? "0" : "") + curSec 
    //YAHOO.util.Connect.setDefaultPostHeader(false);
    //YAHOO.util.Connect.initHeader("Content-Type", "text/xml; charset=utf-8"); 
    this.getXML = YAHOO.util.Connect.asyncRequest("GET",this.challengeURL + "?" + curHour + curMin + curSec , this.returnedCallback); 
};

ChangePwd.prototype.completeChallenge = function(response) {
    var x = response.responseXML.getElementsByTagName("ajax-response");
    var success = false;
    var error = -1;
    var message = '';
    var data = '';
    for (var i = 0; i < x.length; i++) {
        successcol = x[i].getElementsByTagName("Success");
        errorcol = x[i].getElementsByTagName("Error");
        messagecol = x[i].getElementsByTagName("Message");
        datacol = x[i].getElementsByTagName("Data");
        try {
            for (var count = 0; count < successcol.length; count++) {
                success = GetInnerText(successcol[count]);
            }
            for (var count1 = 0; count1 < errorcol.length; count1++) {
                error = GetInnerText(errorcol[count1]);
            }
            for (var count2 = 0; count2 < messagecol.length; count2++) {
                message = GetInnerText(messagecol[count2]);
            }
            for (var count3 = 0; count3 < datacol.length; count3++) {
                data = datacol[count3];
                valuecol = data.getElementsByTagName("Value");
                getRSA_Ecol = data.getElementsByTagName("GetRSA_E");
                getRSA_Mcol = data.getElementsByTagName("GetRSA_M");
                challengecol = data.getElementsByTagName("Challenge");
                for (var count_inner = 0; count_inner < valuecol.length; count_inner++) {
                    this.value = GetInnerText(valuecol[count_inner]);
                }
                for (var count_inner1 = 0; count_inner1 < getRSA_Ecol.length; count_inner1++) {
                    this.getRSA_E = GetInnerText(getRSA_Ecol[count_inner1]);
                }
                for (var count_inner2 = 0; count_inner2 < getRSA_Mcol.length; count_inner2++) {
                    this.getRSA_M = GetInnerText(getRSA_Mcol[count_inner2]);
                }
                for (var count_inner3 = 0; count_inner3 < challengecol.length; count_inner3++) {
                    this.challengevalue = GetInnerText(challengecol[count_inner3]);
                }
            }
        }
        catch (er) {
        }
    }
    if (success == "True") {
        this.AddScripts();
    }
    else {
        this.WindowRef.OverrideMessage(message, MyAJAXMessage.CSSImgOk);
        FadeEnable(_objbtnCancel, 0);
        FadeEnable(_objbtnOk, 0);
    }
};

ChangePwd.prototype.ShowCallBackErrorChallenge = function (t)	
{   
    this.WindowRef.OverrideMessage("ERROR GENERATING CHALLENGE" , "default");
    _messagesetTimeOut = FadeMessage(this.WindowRef.AJAXMessage , "True" , 5);
    FadeEnable(_objbtnCancel , 0);
    FadeEnable(_objbtnOk , 0);
};

ChangePwd.prototype.SetNewPassword = function (encvalue)
{   
    var GetParams = '';
    GetParams = this.encValue + "=" + encvalue + "&";
    GetParams += this.challenge + "=" + this.challengevalue;
    var outer = this;
    this.returnedCallback = 
    {
        success: function(t) 
        {
            outer.completeNewPassword(t);
        },
        failure: function(t) 
        {
            outer.ShowCallBackErrorNewPassword(t);
        }
    }
    //YAHOO.util.Connect.setDefaultPostHeader(false);
    //YAHOO.util.Connect.initHeader("Content-Type", "text/xml; charset=utf-8"); 
    this.getXML = YAHOO.util.Connect.asyncRequest("GET",this.ChgPwdURL + "?" + GetParams,this.returnedCallback); 
};

ChangePwd.prototype.completeNewPassword = function (response)	
{   
    var x = response.responseXML.getElementsByTagName("ajax-response");
    var success = false;
    var error = -1;
    var message = '';
    for (var i = 0 ; i < x.length ; i++)
    {
        successcol = x[i].getElementsByTagName("Success");
        errorcol = x[i].getElementsByTagName("Error");
        messagecol = x[i].getElementsByTagName("Message");
        try
        {
	        for (var count = 0 ; count < successcol.length ; count++)
            {
   		        success = GetInnerText(successcol[count]);
            } 
	        for (var count1 = 0; count1 < errorcol.length; count1++)
            {
   		        error = GetInnerText(errorcol[count1]);
            } 
	        for (var count2 = 0; count2 < messagecol.length; count2++)
            {
   		        message = GetInnerText(messagecol[count2]);
            } 
        } 
        catch (er)
        {
        }        
    }
    if (success == "True")
    {
        this.WindowRef.OverrideMessage(message , MyAJAXMessage.CSSImgOk);             
        refPass = this;
        //setTimeout(function() {refPass.fadeOutMyPopup(refPass.WindowRef)}, 5000);
        setTimeout(function () 
        {
            refPass.CloseWindow();
        }, 5000);           
    }
    else
    {
        this.WindowRef.OverrideMessage(message , MyAJAXMessage.CSSImgOk);  
        FadeEnable(_objbtnCancel , 0);
        FadeEnable(_objbtnOk , 0);
    }
};

ChangePwd.prototype.ShowCallBackErrorNewPassword = function (t)	
{   
    this.WindowRef.OverrideMessage("ERROR CHANGING CHALLENGE" , "default");
    _messagesetTimeOut = FadeMessage(this.WindowRef.AJAXMessage , "True", 5);
    FadeEnable(_objbtnCancel , 0);
    FadeEnable(_objbtnOk , 0);
};

ChangePwd.prototype.AddScripts = function() {
    if (_AllOk == true) {
        this.encData();
    }
    else {
        //this.WindowRef.OverrideMessage("Challenge Received from Server, getting files for client encryption. . .", MyAJAXMessage.ImgOk);
        //var options = this.Scripts.split('~');
        //var script;
        //for (var i = 0; i < options.length; i++) {
            //this.AddScript(options[i]);
        //}
        this.encData();
    }
};

ChangePwd.prototype.AddScript = function(scriptName) {
    var script;
    script = document.createElement('script');
    script.setAttribute('language', 'javascript');
    script.setAttribute('type', 'text/javascript');
    script.setAttribute('defer', 'true');
    document['ChangePwdID'] = this;
    script.onreadystatechange = function() {
        if (script.readyState == 'complete') {
            checkChangePwdScriptLoaded();
        }
    };
    script.onload = checkChangePwdScriptLoaded;
    script.setAttribute('src', scriptName);
    document.getElementsByTagName('head')[0].appendChild(script);
};

ChangePwd.prototype.InitEvents = function (objSubscribe) 
{
    objSubscribe.ResponseBackScriptsLoaded.subscribe(this.fnSubscriberonResponseBackScriptsLoaded , this);      
};

ChangePwd.prototype.fnSubscriberonResponseBackScriptsLoaded = function (type , args , me) 
{   
    me.encData();
};

ChangePwd.prototype.encData = function () 
{   
    this.WindowRef.OverrideMessage("Encrypting Data . . ." , MyAJAXMessage.ImgOk);  
    var g_AJAXChwPwdCrypto =  new Crypto(this.value , this.getRSA_E , this.getRSA_M , this.challengevalue , _objValueUser , _objValuePwd, _objValueChkPwd, _objbtnOk, "False");
    var encValue = g_AJAXChwPwdCrypto.cmdEncrypt();
    this.SetNewPassword(encValue);
};

function AreChangePwdScriptsLoaded() 
{
    if ((typeof (CryptoexternalScriptLoaded) == 'undefined') ||  (typeof (BigIntexternalScriptLoaded) || 'undefined') ||  (typeof (BarretexternalScriptLoaded) || 'undefined') ||  (typeof (RSAexternalScriptLoaded) == 'undefined'))
    {
        return false;
    }
    else
    {
        return ((CryptoexternalScriptLoaded) && (BigIntexternalScriptLoaded) && (BarretexternalScriptLoaded) & (RSAexternalScriptLoaded));
    }  
}

function checkChangePwdScriptLoaded(outer) 
{
    if (typeof (this.ResponseBackScriptsLoaded == 'undefined'))
    {
        this.ResponseBackScriptsLoaded = new YAHOO.util.CustomEvent("ResponseBackScriptsLoaded" , this); 
        document['ChangePwdID'].InitEvents(this);
    }
    if ((typeof (CryptoexternalScriptLoaded) == 'undefined') || (typeof (BigIntexternalScriptLoaded) == 'undefined')  ||  (typeof (BarretexternalScriptLoaded) == 'undefined') ||  (typeof (RSAexternalScriptLoaded) == 'undefined')) 
    {
        _setTimeOut = setTimeout('checkChangePwdScriptLoaded' , 500);
    } 
    else
    {
        if ((CryptoexternalScriptLoaded) && (BigIntexternalScriptLoaded) && (BarretexternalScriptLoaded) & (RSAexternalScriptLoaded))
        {
            if (_AllOk == true)
            {
            }
            else
            {
                _AllOk = true;
                this.ResponseBackScriptsLoaded.fire();
            }                   
        }
        else
        {
            _setTimeOut = setTimeout('checkChangePwdScriptLoaded' , 500);
        }
    }        
}

function GetInnerText(node)
{
    if (typeof (node.textContent || node.innerText || node.text) != 'undefined')
    {
	    return (node.textContent || node.innerText || node.text);
    }
	else
	{
        return '';
    }
}


