﻿//Globals
var preLogin = 'ctl00_cntLogIn_';

//Login functions
function BeginAuthenticateUser() {                              //Sets up the user login info and sends it to the AJAX auth service
    $get('AnonymousView').style.display = 'none';
    $get('LoggingIn').style.display = '';
    var uid;
    var pwd;
    var isPersistent = true;
    uid = document.getElementById(preLogin + 'UserName').value;
    pwd = document.getElementById(preLogin + 'Password').value;
    Sys.Services.AuthenticationService.login(uid, pwd, isPersistent, null, null, EndAuthenticateUser, OnError, uid);
    return false;
}
function EndAuthenticateUser(result, userContext, methodName) { //Passback function from BeginAuthenticateUser.  If user logged in, go to webservice to set cookie
    if (result) {
        var uid = document.getElementById(preLogin + 'UserName').value;
        UlsterTrader.WebService1.WriteCookie(uid, OnComplete);
    }
    else {
        alert("Your email and password do not match.  Please check your details and try again.");
        $get('LoggingIn').style.display = 'none';
        $get('AnonymousView').style.display = '';
    }
}
function OnComplete(result) {                                     //Passback function from WriteCookie webservice writes User FirstName to loggedin view and redirects if required

    var arrresult = result.split('|');
    
    $get('LoggedInView').style.display = '';
    $get(preLogin + 'spanUserName').innerHTML = arrresult[1];
    $get('LoggingIn').style.display = 'none';
    //alert(arrresult[0]);
    
    if (arrresult[0] == '1') {
        $get('LoggedInViewUser').style.display = '';
    }
    else if (arrresult[0] == '2') {
        $get('LoggedInViewDealer').style.display = '';
    }
    else if (arrresult[0] == '3') {
        $get('LoggedInViewDealer').style.display = '';
        $get('lnkPPE').style.display = '';
        //Redirect if LeadBuddy (PPE) dealer
        //window.location = sPath + '/Dashboard/LeadManagement.aspx';
    }


    //Redirect if there is a ReturnUrl parameter
    var qs = new Querystring();
    
    var RedirectUrl = qs.get("ReturnUrl")
    if (RedirectUrl != null) {
        //window.location = "http://localhost/" + RedirectUrl;
        window.location = sPath + RedirectUrl;
    }
    //Redirect if page is logout page
    var CurrentUrl = window.location.href;
    if (CurrentUrl.indexOf("Logout") > 0) {
        window.location = sPath;
    }
    //Redirect if page is register page
    var CurrentUrl = window.location.href;
    if (CurrentUrl.indexOf("Register") > 0) {
        window.location = sPath + '/Dashboard/';
    }
}

function OnError(result, userContext, methodName) {                 //Standard error handler
    alert(result.get_message());
}

//Logout functions
function BeginLogOut() {
    var redirectUrl = sPath + "/Membership/Logout.aspx";
    Sys.Services.AuthenticationService.logout(redirectUrl, EndLogout, OnError, null);
    return false;
}

function EndLogout(result) {
    //nothing here
    //alert("You have logged out");
    $get('LoggedInView').style.display = 'none';
    $get('LoggingOut').style.display = '';

}


/* Client-side access to querystring name=value pairs
Version 1.3
28 May 2008
	
License (Simplified BSD):
http://adamv.com/dev/javascript/qslicense.txt
*/
function Querystring(qs) { // optionally pass a querystring to parse
    this.params = {};

    if (qs == null) qs = location.search.substring(1, location.search.length);
    if (qs.length == 0) return;

    // Turn <plus> back to <space>
    // See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
    qs = qs.replace(/\+/g, ' ');
    var args = qs.split('&'); // parse out name/value pairs separated via &

    // split out each name=value pair
    for (var i = 0; i < args.length; i++) {
        var pair = args[i].split('=');
        var name = decodeURIComponent(pair[0]);

        var value = (pair.length == 2)
			? decodeURIComponent(pair[1])
			: name;

        this.params[name] = value;
    }
}

Querystring.prototype.get = function(key, default_) {
    var value = this.params[key];
    return (value != null) ? value : default_;
}

Querystring.prototype.contains = function(key) {
    var value = this.params[key];
    return (value != null);
}