﻿// authentication.js
/*var userName;
var password;
var rememberMe;
var msgError;
*/
//var panelLogin;
//var panelRegister;

//NOTE:PAGELOAD is embeded into Login.ascx       

// This function sets and gets the default
// login completed callback functio.
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();
    SetDefaultFailedCallBack();
    userName = $get("ctl00_ctl00_ctl13_TxtUserName");
    password = $get("ctl00_ctl00_ctl13_TxtPassword");
    rememberMe = $get('ctl00_ctl00_ctl13_ChkRememberMe');
    //if (rememberMe == null)
    //{
    //    rememberMe = $get('ctl00_ctl00_ContentSite_ColumnMain_ctl04_ChkRememberMe');
    //}
    if (rememberMe == null)
    {
          rememberMe = $get('ctl00_ctl00_ctl13_ChkRememberMe');
    }
	msgError = $get('ctl00_ctl00_ctl13_LblError');
	//if (msgError == null)
	//{
	//   msgError = $get('ctl00_ctl00_ContentSite_ColumnMain_ctl04_LblError');
	//}
	if (msgError == null)
	{
	     msgError = $get('ctl00_ctl00_ctl13_LblError');
	}   
    // Call the authetication service to authenticate
    // the credentials entered by the user.
    //alert(userName.value +" "+ password.value+" "+ rememberMe.checked);
    Sys.Services.AuthenticationService.login(userName.value, password.value, rememberMe.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() 
{  
    SetDefaultLogoutCompletedCallBack();
    //alert("onclicklogout");
   // Clear the forms authentication cookie. 
   Sys.Services.AuthenticationService.logout(null, 
        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 name password.
    //userName = ""
    //password.value = "";
    
    // On success there will be a forms 
    // authentication cookie in the browser.
    if (validCredentials == true) 
    {
        // Hide modalPopup
	    $find('MPELogin').hide();
        // Clear the user name.
        userName.value = "";
        // Clear the feedback area.
        DisplayInformation(""); 
        //loadProfile(userContext);
        window.location.reload()
    }
    else 
    {
        DisplayInformation("<div class='warningLabel'>Sorry, no match for such a user.</div>"); 
        return false;
    }
}

// This is the callback function called 
// if the user logged out successfully.
function OnLogoutCompleted(result) 
{
    // Display login fields.
}                   

// This function displays feedback
// information for the user.    
function DisplayInformation(text)
{
    /*
    document.getElementById("FeedBackID").innerHTML = 
        "<br/>" + text;

    // Display authentication service information.
    
	var userLoggedIn =
	    Sys.Services.AuthenticationService.get_isLoggedIn();
	
    var authServiceTimeout =       
        Sys.Services.AuthenticationService.get_timeout();
   
    var userLoggedInfo = 
        "<br/> User logged in:                 " + userLoggedIn;
        
    var timeOutInfo = 
        "<br/> Authentication service timeout: " + authServiceTimeout;
        
    document.getElementById("FeedBackID").innerHTML = 
        userLoggedInfo + timeOutInfo; 
    */
    msgError.innerHTML = text; 
    
}

/************************************************************************************/
function checkUserNameAvailibility() 
/************************************************************************************/
{

   var elUserName = $get("ctl00_ctl00_ContentSite_ColumnMain_ctl02_ctl00_CUWizard1_CreateUserStepContainer_UserName");
   if (elUserName == null)//if from photodetail.aspx
   {
        elUserName = $get("ctl00_ctl00_ctl13_ctl00_CUWizard1_CreateUserStepContainer_UserName");
   }
    if (elUserName == null)//if from register.ascx (top)
   {
        elUserName = $get("ctl00_ctl00_ctl13_ctl00_CUWizard1_CreateUserStepContainer_UserName");
   }
   if (elUserName == null)//if from sitemaster directly
   {
        elUserName = $get("ctl00_ctl00_ctl13_CUWizard1_CreateUserStepContainer_UserName");
   }
   
   userName = elUserName.value;
   if (userName == "")
   {
      //alert('Please enter User Name!');
      return false;
   }
   requestComplexService = MontageService.CheckUserNameAvailibility(userName, onCompleteCheckUserNameAvailibility, onErrorCheckUserNameAvailibility);
   return false;
}
/************************************************************************************/
function onCompleteCheckUserNameAvailibility(result, userContext, methodName) 
/************************************************************************************/
{
   var elMsgError = $get('ctl00_ctl00_ContentSite_ColumnMain_ctl02_ctl00_CUWizard1_CreateUserStepContainer_LblError');//$get('hTxtError');
   if (elMsgError == null)//if from photodetail.aspx
   {
        elMsgError = $get("ctl00_ctl00_ContentSite_ColumnMain_ctl04_ctl00_CUWizard1_CreateUserStepContainer_LblError");
   }
    if (elMsgError == null)//if from register.ascx (top)
   {
        elMsgError = $get("ctl00_ctl00_ctl13_ctl00_CUWizard1_CreateUserStepContainer_LblError");
   }
    if (elMsgError == null)//if from sitemaster
   {
        elMsgError = $get("ctl00_ctl00_ctl13_CUWizard1_CreateUserStepContainer_LblError");
   }
   if(result)
   {
      elMsgError.innerHTML = "";  
    //$find("UserNameValidatorCalloutExtender").show();
   }
   else
   {
      elMsgError.innerHTML = "<div class='warningLabel'>This User Name is taken!</div>";
      
   }
}
/************************************************************************************/
function onErrorCheckUserNameAvailibility(result)
/************************************************************************************/
{
    alert("error in CheckUserNameAvailibility");
}
/************************************************************************************/
function loadProfile()
/************************************************************************************/
{ 
    Sys.Services.ProfileService.load(null, onProfileLoadCompleted, onProfileFailed, null); 
} 
/************************************************************************************/
function onProfileLoadCompleted(numProperties, userContext, methodName)
/************************************************************************************/
{
  alert("Sys.Services.ProfileService:"+Sys.Services.ProfileService.properties.DisplayDeleteConfirm);
} 
/************************************************************************************/
function onProfileFailed(result)
/************************************************************************************/
{
    alert("error in onProfileFailed");
}

if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
