﻿function doAuth(authUrl)
{
    //window.Login.control.open('FileManager', {}, handler); 
    //var manager = new Telerik.Web.UI.RadWindowManager();
    //manager._registerAsPageManager();
    var oWnd = radopen(authUrl, "AuthWindow");
    if (oWnd) {
        oWnd.SetSize(330,260);
        oWnd.set_top(150);
        oWnd.SetModal(true);
        oWnd.set_visibleStatusbar(false);
        oWnd.SetTitle("Sign In");
        oWnd._enablePageScrolling(false);
        
        //set a function to be called when RadWindow is closed
        oWnd.set_clientCallBackFunction(onLoginCallBack);        
    }    
}
function FocusUserName()
{
    if ($(window.UserNameTxt))
        setTimeout("try{$(window.UserNameTxt).focus();}catch(er){}", 500);
}

function DisplayLogin(display, login, register)
{
    if (display)
    {        
        document.title = "Sign In";
        $(login).show();
    }
    else 
    {
        document.title = "Volunteer Development Scotland > Restricted Resource";
        $(register).show();            
    }       
} 

function GetRadWindow()
{
	var oWindow = null;
	if (window.radWindow) 
	    oWindow = window.radWindow;
	else {
	    if (window.frameElement != null)
	    {
	        if (window.frameElement.radWindow)
	            oWindow = window.frameElement.radWindow;
	    }
	}
	return oWindow;
}		
	
function onLoginCallBack(radWindow, returnValue)
{
    // Split the callback return value [Operation Status]:[Redirection Url]
    var temp = new Array();
    var status = 'LOGIN_FAILED';
    var redirectUrl = '#';

    if (returnValue == null)
        return;
    
    temp = returnValue.split(';');
    
    // Get the status
    status = temp[0];
    
    // Get the url to redirect to
    if (temp.length > 1)
    {
        redirectUrl = temp[1];
    }
    
    if (status == 'LOGIN_SUCCESS') {
    
        showLoader();
        
        // Redirect to the return url if present otherwise refresh page
        if (redirectUrl != '#') {
            var windowLocation = "window.location = '" + redirectUrl + "'";
            setTimeout(windowLocation, 1000);
        }
        else {
            setTimeout("window.location.reload( true )", 1000);
        }
    }
    else if (status == "REGISTER_USER")
    {
        var windowLocation = "window.location = '" + redirectUrl + "'";
        setTimeout(windowLocation, 1000);
    }
    else if (status == "LOGIN_FORGOT_PASSWORD")
    {
        // Redirect to the return url if present
        if (redirectUrl != '#') {
            alert(redirectUrl);
            window.location = redirectUrl;
        }
    }
    else if (status == "LOGIN_CANCEL")
    {
        // Do nothing
    }
    else if (status == "LOGIN_FAILED")
    {
        // Do nothing
    }
    else if (status == "ACCOUNT_LOCKED")
    {
        radalert(redirectUrl, 350, 120, 'Account Locked');
    }
    else if (status == "ACCOUNT_SYNC_FAILED")
    {
        radalert(redirectUrl, 350, 120, 'Authentication Problem');
    }
    else if (status == "PASSWORD_CHANGE_FAILED")
    {
        radalert(redirectUrl, 350, 100, 'Password Expired');
    }
}

function LoginWindowCallBack(radWindow, returnValue)
{
    // Split the callback return value [Operation Status]:[Redirection Url]
    var temp = new Array();
    var status = 'LOGIN_FAILED';
    var redirectUrl = '#';
    
    temp = returnValue.split(';');
    
    // Get the status
    status = temp[0];
    
    // Get the url to redirect to
    if (temp.length > 1)
    {
        redirectUrl = temp[1];
    }
    
    if (status == 'LOGIN_SUCCESS') {
    
        showLoader();
        
        // Redirect to the return url if present otherwise refresh page
        if (redirectUrl != '#') {
            var windowLocation = "window.location = '" + redirectUrl + "'";
            setTimeout(windowLocation, 1000);
        }
        else {
            setTimeout("window.location.reload( true )", 1000);
        }
    }
    else if (status == "REGISTER_USER")
    {
        var windowLocation = "window.location = '" + redirectUrl + "'";
        setTimeout(windowLocation, 1000);
    }
    else if (status == "LOGIN_FORGOT_PASSWORD")
    {
        // Redirect to the return url if present
        if (redirectUrl != '#') {
            alert(redirectUrl);
            window.location = redirectUrl;
        }
    }
    else if (status == "LOGIN_CANCEL")
    {
        // Do nothing
    }
    else if (status == "LOGIN_FAILED")
    {
        // Do nothing
    }
    else if (status == "ACCOUNT_LOCKED")
    {
        radalert(redirectUrl, 350, 120, 'Account Locked');
    }
    else if (status == "ACCOUNT_SYNC_FAILED")
    {
        radalert(redirectUrl, 350, 120, 'Authentication Problem');
    }
    else if (status == "PASSWORD_CHANGE_FAILED")
    {
        radalert(redirectUrl, 350, 100, 'Password Expired');
    }
}

function logoutCallBackFn(arg)
{
    if (arg) {
        showLoader();
        
        var logoutUrl;
        logoutUrl = $('dnn_vdsLOGIN_logoutUrl');
       
        var windowLocation = "window.location = '" + logoutUrl.value + "'";
        setTimeout(windowLocation, 1000);
    }
 }
 
 function showLoader() { 
    if (document.getElementById) { // DOM3 = IE5, NS6
        document.getElementById('loader').style.display = 'block';
    } 
    else {
        if (document.layers) { // Netscape 4 
            document.hideshow.display = 'block';
        } 
        else { // IE 4 
            document.all.hideshow.style.display = 'block'; 
        } 
    } 
}