﻿
var ControlToSetFocus = null;
var processingresult = false;

function isLoggedIn()
{
    var x = document.getElementById("ctl00_ContentPlaceHolder1_ucSimpleUpload_divNotLoggedin");
    if (x == null) return false;
    
    if (x.style.display == "none") return true;
    
    return false;
}

function HomePageLogin(callingcontrol)
{   
    if (processingresult) return;
    
    ControlToSetFocus = callingcontrol;
    
    if (isLoggedIn()) return;
    
    var txtUserName = document.getElementById("ctl00_ContentPlaceHolder1_ucSimpleUpload_txtUserName");
    var txtPassword = document.getElementById("ctl00_ContentPlaceHolder1_ucSimpleUpload_txtPassword");
    var chkRememberMe = document.getElementById("ctl00_ContentPlaceHolder1_ucSimpleUpload_chkRememberMe");
    
    if (txtUserName == null) txtUserName = document.getElementById("login_EmailAddress");
    if (txtPassword == null) txtPassword = document.getElementById("login_Password");
    if (chkRememberMe == null) chkRememberMe = document.getElementById("login_chkRememberMe");
    
    if (txtUserName == null) return;
    if (txtPassword == null) return;
    if (chkRememberMe == null) return;
    
    if (txtUserName.value == "") 
    {
        radalert("<span class=PopupText><b>Email Address</b> can not be blank.</span>", 450, 100);

        processingresult = false;
        return false;
    }
    
    if (txtPassword.value == "") 
    {
        radalert("<span class=PopupText><b>Password</b> can not be blank.</span>", 450, 100);
        processingresult = false;       
        return false;
    }
    
    var url = "/login/ajax.aspx?";
    
    url += "u=" + escape(txtUserName.value);
    url += "&p=" + escape(txtPassword.value);
    if (chkRememberMe.checked) url += "&r=1";
        
    loadXMLDoc(url, HomePageLoginCallBack);
}

function HomePageLoginCallBack()
{
    if (processingresult) return;
    
   // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            HomePageCheckRequest();
         } else {
            //alert("There was a problem retrieving the XML data:\n" + req.statusText);
         }
    }
}

function HomePageCheckRequest()
{  
    if (processingresult) return;
    
    processingresult = true;
    
    var txtUserName = document.getElementById("ctl00_ContentPlaceHolder1_ucSimpleUpload_txtUserName");
    var txtPassword = document.getElementById("ctl00_ContentPlaceHolder1_ucSimpleUpload_txtPassword");
    
    var Status = req.responseXML.getElementsByTagName("Status");
    if (Status.length == 0) 
    {
        processingresult = false;
        return;
    }
      
    Status = Status[0];
    
    var result = Status.childNodes[0].nodeValue;

   
    // Good to go!
    if (result == "1")
    {
       if (ControlToSetFocus != null)
       {
            if (ControlToSetFocus.id == "login_LoginButton") 
            {
                document.location = "/MyAccount/MyFiles/";
            }
       }
       
       var litEmailAddress = document.getElementById("ctl00_ContentPlaceHolder1_ucSimpleUpload_litEmailAddress");
       
       if (litEmailAddress == null) 
       {
            
            processingresult = false;
            return;
       }
       
       HideElement("ctl00_ContentPlaceHolder1_ucSimpleUpload_divNotLoggedin");
       
       litEmailAddress.innerHTML = txtUserName.value;
       ShowElement("ctl00_ContentPlaceHolder1_ucSimpleUpload_divLoggedIn");
       
        var BytesRemaining = req.responseXML.getElementsByTagName("BytesRemaining");
        if (BytesRemaining.length)
        {
            BytesRemaining = BytesRemaining[0];
            var x = Number(BytesRemaining.childNodes[0].nodeValue);
            if (isNaN(x) == false) SetSizeLimit(x);
       }

       HideElement("ctl00_ucUserNav_divUserNavNotLoggedIn");
       ShowElement("ctl00_ucUserNav_divUserNavLoggedIn");
       
       if (ControlToSetFocus != null) ControlToSetFocus.focus();
       processingresult = false;
       return;
    }
    
    // Email Not found.
    if (result == "-1")
    {
        radconfirm("<span class=PopupText>Your email address was not found in our system.<br>\n<br>\nWould you like to signup for an account?</span>", callBackResult_1, 450, 200);
        processingresult = false;
        return;
    }
    
    // Password incorrect
    if (result == "-2")
    {
        radalert("<span class=PopupText>The password you entered is incorrect.<br>\n<br>\nPlease try again.</span>", 450, 200);
        processingresult = false;
        return;
    }
    
    // Not activated
    if (result == "-3")
    {
        radconfirm("<span class=PopupText>Your account has not been activated.<br>\n<br>\nWould you like us to resend the activation email?", callBackResult_3, 450, 200);
        processingresult = false;
        return;
    }
    
    processingresult = false;
}

function callBackResult_1(arg)
{
  if (arg) window.location = "/signup/";
  processingresult = false;
}

function callBackResult_3(arg)
{
  if (arg) window.location = "/login/resendactivate.aspx?autosend=1";
  processingresult = false;
}

function login_OnClientClose(radWindow)
{
    processingresult = false;
    return;
}

function userNavLogin() {
    var UserName = document.getElementById("EmailAddress").value;
    var Password = document.getElementById("Password").value;

    if (UserName == null) return;
    if (Password == null) return;

    if (UserName == "" ) {
        radalert("<span class=PopupText><b>Email Address</b> can not be blank.</span>", 450, 100);
        return;
    }

    if (UserName == "email") {
        radalert("<span class=PopupText>Please enter valid<b> Email Address</b>.</span>", 450, 100);
        return;
    }

    if (Password == "" ) {
        radalert("<span class=PopupText><b>Password</b> can not be blank.</span>", 450, 100);
        return;
    }

    if (Password == "password") {
        radalert("<span class=PopupText>Please enter valid<b> Password</b>.</span>", 450, 100);
        return;
    }

    window.location = "/login/default.aspx?loginsource=UserNav"+"&EmailAddress="+UserName+"&Password="+Password ;
}
