
// Authentication.js

var qsParm = new Array();

function qs() {
    var query = window.location.search.substring(1);
    var parms = query.split('&');
    for (var i=0; i<parms.length; i++) {
        var pos = parms[i].indexOf('=');
        if (pos > 0) {
            var key = parms[i].substring(0,pos);
            var val = parms[i].substring(pos+1);
            qsParm[key] = val;
        }
    }
} 

var username;
var password;
var remember;
var textLoggedIn;
var textNotLoggedIn;

function pageLoad()
{
	username = $get("ctl00_ctl00_txtUserName");
	password = $get("ctl00_ctl00_txtPassword");
	textNotLoggedIn = $get("ctl00_ctl00_notloggedin");
    if (username == null)
    {
	    username = $get("ctl00_txtUserName");
	    password = $get("ctl00_txtPassword");
	    textNotLoggedIn = $get("ctl00_notloggedin");
    }

	remember = $get("remember");
	textLoggedIn = $get("loggedin");
	DisplayInformation("");
}            

// This function sets and gets the default
// login completed callback function.
function SetDefaultLoginCompletedCallBack()
{
    // Set the default callback function.
    Sys.Services.AuthenticationService.set_defaultLoginCompletedCallback(OnLoginCompleted);
   
    // Get the default callback function.
    var callBack =     
        Sys.Services.AuthenticationService.get_defaultLoginCompletedCallback();
}

// This function sets and gets the default
// logout completed callback function.
function SetDefaultLogoutCompletedCallBack()
{
    // Set the default callback function.
    Sys.Services.AuthenticationService.set_defaultLogoutCompletedCallback(OnLogoutCompleted);
   
    // Get the default callback function.
    var callBack =     
        Sys.Services.AuthenticationService.get_defaultLogoutCompletedCallback();
}

// This function sets and gets the default
// failed callback function.
function SetDefaultFailedCallBack()
{
    // Set the default callback function.
    Sys.Services.AuthenticationService.set_defaultFailedCallback(OnFailed);
   
    // Get the default callback function.
    var callBack =     
        Sys.Services.AuthenticationService.get_defaultFailedCallback();
}

// This function calls the login method of the
// authentication service to verify 
// the credentials entered by the user.
// If the credentials are authenticated, the
// authentication service issues a forms 
// authentication cookie. 
function OnClickLogin() 
{  
    // Set the default callback functions.
    SetDefaultLoginCompletedCallBack();
    SetDefaultLogoutCompletedCallBack();
    SetDefaultFailedCallBack();
   
    // Call the authetication service to authenticate
    // the credentials entered by the user.
    Sys.Services.AuthenticationService.login(username.value, 
        password.value, remember.checked,null,null,null,null,"User Context");
}

// This function calls the logout method of the
// authentication service to clear the forms 
// authentication cookie.
function OnClickLogout() 
{  
   // Clear the forms authentication cookie. 
   Sys.Services.AuthenticationService.logout("LoggedOut.aspx", 
        null, null, null); 
} 

// This is the callback function called 
// if the authentication fails.      
function OnFailed(error, 
    userContext, methodName)
{			
    // Display feedback message.
	DisplayInformation("error:message = " + 
	    error.get_message());
	DisplayInformation("error:timedOut = " + 
	    error.get_timedOut());
	DisplayInformation("error:statusCode = " + 
	    error.get_statusCode());			
}


// The callback function called 
// if the authentication completed successfully.
function OnLoginCompleted(validCredentials, 
    userContext, methodName)
{
	
    // Clear the user password.
    password.value = "";
    
    // On success there will be a forms 
    // authentication cookie in the browser.
    if (validCredentials == true) 
    {
        
        // Clear the user name.
        username.value = "";
     
        // Clear the feedback area.
        DisplayInformation(""); 


        //redirect if there is a ReturnUrl in the querystring
        qsParm['ReturnUrl'] = null;
        qs(); 
        if(qsParm['ReturnUrl'] != null)
        {
            //decode slashes in ReturnUrl
            var newLocation = unescape(qsParm['ReturnUrl']);
            window.location = newLocation;
        } else {
            window.location = "LoggedIn.aspx";
        }

    }
    else 
    {
        DisplayInformation("Username or password incorrect. Could not log in."); 
    }
}

// This is the callback function called 
// if the user logged out successfully.
function OnLogoutCompleted(result) 
{
}                   

// This function displays feedback
// information for the user.    
function DisplayInformation(text)
{
	
    document.getElementById("FeedBackID").innerHTML = text;

    // Display authentication service information.
    
	var userLoggedIn = Sys.Services.AuthenticationService.get_isLoggedIn();
	    
    textNotLoggedIn.style.display = (userLoggedIn == false) ? "block" : "none";  
    textLoggedIn.style.display = (userLoggedIn == true) ? "block" : "none";  
}




//Sys.Application.add_init(appl_init);

//function appl_init() {
//    var pgRegMgr = Sys.WebForms.PageRequestManager.getInstance();
//    pgRegMgr.add_beginRequest(BeginHandler);
//}

//function BeginHandler(sender, args) {
//    var myRequest = args.get_request();
//    var myBody = myRequest.get_body();
////    alert(myBody.substr(myBody.indexOf("test")));
//    //    myBody = myBody.replace("wibble", "flibble");
////    myRequest.set_body(myBody);
//}



if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();


