﻿var gServReq = 'SignIn_Req.aspx';
var gUserID = 0;

function devLogin()
{
    attemptSignIn('wunderleyg','gw');
}

function loginClick()
{
    if($F('inpUserName') != "" && $F('inpPassword') != "")
    {  
        attemptSignIn($F('inpUserName'),$F('inpPassword'));
    }
}

function attemptSignIn(userName,password)
{
	new Ajax.Request(
		gServReq,
		{ 
			method:'post', 
			onCreate: function(){
				//Turn Load Indicator On
				$('statusIcon').src = '../../images/key_go.gif';
			}, 
			onComplete: function(){
				//Turn Load Indicator Off
			},
			onSuccess: function(transport){
				//Handle Successful Response
				var obj = transport.responseText.evalJSON();
				if(obj.SignedIn == true){
				//	$('statusIcon').src = 'images/accept.gif';
				//	signInMsg.className = 'bold green rpad5 tpad5 right';
				//	signInMsg.update('Welcome to CCTS');
				//	$('loginBlock').appendChild(signInMsg);
				    if (obj.redirectUrl == 'ResetPassword')
				    {
					    gUserId = obj.userID;
					    $('loginBlock').hide();
					    $('divResetPassword').show();
				    }
				    else if (obj.redirectUrl == 'UpdateInfo')
				    {
					    if (confirm('You have been requested to update the information for your company.  Do you wish to do this now?'))
					    {
						    window.location = './../../VendorRegistration/Registration_Page1.aspx?companyID=' + obj.companyID;
					    }
					    else
					    {
				            window.location = './../Inbox/Inbox.aspx';
					    }
				    }
				    else if (obj.redirectUrl == 'RegisterInfo')
				    {
					    alert('You must complete Supplier Registration before logging into the application.')
					    window.location = './../../VendorRegistration/Registration_Page1.aspx?companyID=' + obj.companyID;
				    }
				    else
				    {
				        window.location = obj.redirectUrl;
					}
				}else{
					$('statusIcon').src = '../../images/cancel.gif';
					$('signInMsg').className = 'bold red rpad10 right lpad5';
					$('signInMsg').update('Login Failed');
					$('loginBlock').appendChild(signInMsg);
					$('inpPassword').clear();
					$('inpUserName').clear();
					$('inpUserName').activate();								
				}
			},
			parameters: {
				requestType:"attemptSignIn",
				username:userName,
				password:password
			},
			onException: function(transport,ex){
				//Turn Error Indicator On
				//Javascript Exception
			},
			on406: function(transport){
				//Turn Error Indicator On
				//Server Error
			}
		}
	);			
}

//This function checks to make sure an old password is not being used before saving the User's information.
function checkUserPassword()
{
    if ($F('inpNewPassword') == "")
    {
        alert('You must enter a new password.');
    }
    else if ($F('inpNewPassword') != $F('inpVerifyPassword'))
    {
        alert('The passwords you entered do not match.  Please reenter them and try again.');
    }
    else
    {
        new Ajax.Request(
            gServReq,
            { 
                method:'post', 
                onCreate: function(){
                }, 
                onComplete: function(){
                },
                onSuccess: function(transport){
                //Handle Successful Response
                    if (transport.responseText > 0)
                    {
                        alert('This password has recently been used.  Please enter a new password and try again.');
                    }
                    else
                    { 
                        saveUserPassword();
                    }
                },
                parameters: {
	                requestType:"checkUserPassword",
	                userID:gUserId,
                    password:$F('inpNewPassword')
                },
                onException: function(transport,ex){
                },
                on406: function(transport){
                }
            }
        );
    }        
}

//This function updates the information for the selected user in the database.
function saveUserPassword()
{
    new Ajax.Request(
        gServReq,
        { 
            method:'post', 
            onCreate: function(){
            }, 
            onComplete: function(){
            },
            onSuccess: function(transport){
            //Handle Successful Response
                if (transport.responseText == 1)
                {
                    window.location = './../Inbox/Inbox.aspx';
                }
            },
            parameters: {
	            requestType:"saveUserPassword",
	            userID:gUserId,
                password:$F('inpNewPassword')
            },
            onException: function(transport,ex){
            },
            on406: function(transport){
            }
        }
    );
}

function CheckKeyLogin(e)
{
 	var code;
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	if(code == 13){
		loginClick();
	}
}

function CheckKeyPassword(e)
{
 	var code;
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	if(code == 13){
		checkUserPassword();
	}
}