﻿function ClearFields()
{
    ClientControl('TextBoxUserName').value = "";
    ClientControl('TextBoxPassword').value = "";
    document.getElementById("SpanError").innerHTML = "";
    document.getElementById('ImgErrorUserName').style.visibility = "hidden";
    document.getElementById('ImgErrorPassword').style.visibility = "hidden";
}

function ControlFocus(type, control)
{
    if(type == "focus")
    {
        ClientControl(control).style.backgroundColor  = "#fff";
        ClientControl(control).style.borderColor = "#990033"; 
    }
    else if(type == "blur")
    {
	ClientControl(control).style.backgroundColor  = "#fafafa";
	ClientControl(control).style.borderColor = "#999999";    
    }
    
    return false;
}

function LoginUser()
{
    var errorExists = false;
    
    document.body.style.cursor = "wait";
    document.getElementById("SpanError").innerHTML = "Validating Login: Please wait...";
    
    if(ClientControl('TextBoxUserName').value == "")
    {
        document.getElementById('ImgErrorUserName').style.visibility = "visible";
        errorExists = true;
    }

    if(ClientControl('TextBoxPassword').value == "")
    {
        document.getElementById('ImgErrorPassword').style.visibility = "visible";
        errorExists = true;
    }
       
    if(errorExists == true)
    {
        document.getElementById("SpanError").innerHTML = "Validation Error: The following errors exists.";
        
        document.body.style.cursor = "default";
    }
    else
    {
        document.getElementById("SpanError").innerHTML = "Validating Login: Please wait...";
        
        ret = UserSessionService.ValidateLogin(ClientControl('HiddenToken').value, ClientControl('TextBoxUserName').value, ClientControl('TextBoxPassword').value,
                                               ValidateLogonOnComplete, ValidateLogonOnTimeOut, ValidateLogonOnError);
        
    }
}

//function ValidateLogonOnComplete(args)
//{
//}

function ValidateLogonOnTimeOut(args)
{
    document.body.style.cursor = "default";
    document.getElementById("SpanError").innerHTML = "Validation Timeout Error.  Please try again.";
    

}

function ValidateLogonOnError(args)
{
    document.body.style.cursor = "default";
    document.getElementById("SpanError").innerHTML = "Validation Error Occured.  Please try again.";

}

function ValidateLogonOnComplete(args)
{
    var controlToken;
    var controlApplication;
    
    if(args == false)
    {
        document.getElementById("SpanError").innerHTML = "Invalid Login: Please enter your valid login information. ";
        document.getElementById("ImgErrorUserName").style.visibility  = "visible";
        document.getElementById("ImgErrorPassword").style.visibility = "visible";
        
    }
    else
    {
        document.getElementById("SpanError").innerHTML = "Validating Access Permission: Please wait...";
        controlToken = ClientControl('HiddenToken'); 
        controlApplication = ClientControl('HiddenApplication'); 

        ret = UserSessionService.RetrieveAccessRights(controlToken.value, // controlApplication.value,
                                                RetrieveAccessRightsOnComplete, RetrieveAccessRightsOnTimeOut, RetrieveAccessRightsOnError);
    }

}

function ValidateLogonOnTimeOut(args)
{
    document.body.style.cursor = "default";
    document.getElementById("SpanError").innerHTML = "Login timed out.  Please try again.";

}

function ValidateLogonOnError(args)
{
    document.body.style.cursor = "default";
    document.getElementById("SpanError").innerHTML = "Login error.  Please try again.";
}



function RetrieveAccessRightsOnComplete(args)
{
    var isEnabled = args.IsEnabled;
    //var canRead = args.CanRead;
    //var isAdmin = args.IsAdmin;
    //var isAdminWithGrant = args.IsAdminWithGrant;
    
    var ret;
    
    var controlToken;
    var controlApplication;

    page = args.DefaultPage;
    
    if(isEnabled == true)
    {
       // if(isAdmin == true || isEnabled == true) // canRead == true)
        //{
            document.getElementById("SpanError").innerHTML = "Validating Location Access: Please wait...";
            controlToken = ClientControl('HiddenToken'); 
            controlApplication = ClientControl('HiddenApplication'); 
            
            location.href = page;
            
            //ret = UserSessionService.IsAccessAllowedFromIP(controlToken.value, controlApplication.value,
                                                           //IsAccessAllowedFromIPOnComplete, IsAccessAllowedFromIPTimeOut, 
                                                           //IsAccessAllowedFromIPOnError);
            
            
        //}
        //else
        //{
          //  document.body.style.cursor = "default";
//            document.getElementById("SpanError").innerHTML = "Access Warning.  Insufficent read access.";
            
  //      }
    }
    else
    {
        document.body.style.cursor = "default";
        document.getElementById("SpanError").innerHTML = "Acess Warning.  You are current not enabled.";
        
    }    
}

function RetrieveAccessRightsOnTimeOut(args)
{
    document.body.style.cursor = "default";
    document.getElementById("SpanError").innerHTML = "Login timed out.  Please try again.";

}

function RetrieveAccessRightsOnError(args)
{
    document.body.style.cursor = "default";
    document.getElementById("SpanError").innerHTML = "Login timed out.  Please try again.";

}

function IsAccessAllowedFromIPOnComplete(args)
{
    if(args == false)
    {
        document.getElementById("SpanError").innerHTML = "Access Denied: Access has been denied from your current location.";
    }
    else
    {
        document.getElementById("SpanError").innerHTML = "Access Approved: You have access to the application.";
        
        //window.setTimeout("ExecuteCountDown('SpanError', page, 'You will be redirected in..', '')", 100);

        location.href = page;
    }
}

function IsAccessAllowedFromIPTimeOut(args)
{
    document.body.style.cursor = "default";
    document.getElementById("SpanError").innerHTML = "Login timed out.  Please try again.";

}

function IsAccessAllowedFromIPOnError(args)
{
    document.body.style.cursor = "default";
    document.getElementById("SpanError").innerHTML = "Login timed out.  Please try again.";

}
