﻿Service = function() { }

Service.SercureBaseUrl = "/proxy/proxy.ashx";

Service.login = function(_email, _password, debug, callbackFunction) {
    var url = Service.SercureBaseUrl + encodeURI("?service=login" + "&debug=" + debug );
    $.post(url, { email: _email, password: _password }, callbackFunction, "json");
}

Service.forgotPassword = function(_email, debug, callbackFunction) {
    var url = Service.SercureBaseUrl + encodeURI("?service=forgotPassword" + "&debug=" + debug);
    $.post(url, { email: _email }, callbackFunction, "json");
}

Service.changePassword = function(_currentpassword, _newpassword1, _newpassword2, debug, callbackFunction) {
    var url = Service.SercureBaseUrl + encodeURI("?service=changePassword" + "&debug=" + debug);

    $.post(url,
        { currentpassword: _currentpassword,
          newpassword1: _newpassword1,
          newpassword2: _newpassword2
        },
        callbackFunction,
        "json");
    }

Service.updateUser = function(_firstname, _lastname, _postalcode, _country, _dobYear, _dobMonth, _dobDate, debug, callbackFunction) {
    var url = Service.SercureBaseUrl + encodeURI("?service=updateUser" + "&debug=" + debug);
    
    $.post(url,
    {
        firstname: _firstname,
        lastname: _lastname,
        postalcode: _postalcode,
        country: _country,
        dobYear: _dobYear,
        dobMonth: _dobMonth,
        dobDate: _dobDate
    },
    callbackFunction,
    "json");
}

Service.deleteUser = function(_email, debug) {
var url = Service.SercureBaseUrl + encodeURI("?service=deleteUser" + "&debug=" + debug);

$.post(url,
    {
        firstname: _firstname
    },
    callbackFunction,
    "json");
}


Service.resetPassword = function(_email, _xy, _hi, _rs, _password, debug, callbackFunction) {
    var url = Service.SercureBaseUrl + encodeURI("?service=resetPassword" + "&debug=" + debug);

    $.post(url,
        { email: _email,
            hi: _hi,
            password: _password,
            xy: _xy,
            rs: _rs
        },
        callbackFunction,
        "json");
}


Service.updateLoggedInState = function(isLoggedIn) {
    if (isLoggedIn == "true") {
        $('.loggedInState').show();
        $('.loggedOutState').hide();
        $('.defaultState').hide();
    }
    else {
        $('.loggedInState').hide();
        $('.loggedOutState').show();
        $('.defaultState').hide();
    }
}

Service.updateLoggedInStateBasedOnCookie = function() {

    var isLoggedIn = Service.isLoggedIn();
    
    if (isLoggedIn == true) {
        $('.loggedInState').show();
        $('.loggedOutState').hide();
        $('.defaultState').hide();
    }
    else {
        $('.loggedInState').hide();
        $('.loggedOutState').show();
        $('.defaultState').hide();
    }
}


Service.isLoggedIn = function() {
    var loggedIn = Service.readCookie("loggedIn");

    if (loggedIn == "true") {
        return true;
    }
    return false;
}

Service.updateStatus = function(debug) {
    var url = Service.SercureBaseUrl + encodeURI("?service=updateStatus" + "&debug=" + debug);
    $.post(url, {}, function(data) {
        Service.updateLoggedInState(data.loggedin);
    },"json");
}

Service.logout = function(debug, callbackFunction) {
    var url = Service.SercureBaseUrl + encodeURI("?service=logout" + "&debug=" + debug);
    $.post(url, callbackFunction, "json");
}

Service.register = function(_screenname, _emailaddress, _password1, _password2, _firstname, _lastname, _postalcode, _country, _agree, _yearOfBirth, _monthOfBirth, _dayOfBirth, debug, callbackFunction) {
    var url = Service.SercureBaseUrl + encodeURI("?service=register" + "&debug=" + debug + "&format=json");

    $.post(url,
            {
                screenname: _screenname,
                emailaddress: _emailaddress,
                password1: _password1,
                password2: _password2,
                firstname: _firstname,
                lastname: _lastname,
                postalcode: _postalcode,
                country: _country,
                agree: _agree,
                yearOfBirth: _yearOfBirth,
                monthOfBirth: _monthOfBirth,
                dayOfBirth: _dayOfBirth
            },
            callbackFunction,
            "json");
}


Service.VerifyFacebookToken = function(_token, debug, callbackFunction) {
            var url = Service.SercureBaseUrl + encodeURI("?service=verifyfacebooktoken" + "&debug=" + debug + "&format=json");
            $.post(url,
            {
                token: _token
            },
            callbackFunction,
            "json");
}

Service.registerExternal = function(_screenname, debug, callbackFunction) {
            var url = Service.SercureBaseUrl + encodeURI("?service=registerexternal" + "&debug=" + debug + "&format=json");            
            $.post(url,
            {
                screenname: _screenname   
            },
            callbackFunction,
            "json");
}

Service.resendActivation = function(_emailaddress, debug, callbackFunction) {
    var url = Service.SercureBaseUrl + encodeURI("?service=resendActivation" + "&debug=" + debug);
    $.post(url, { email: _emailaddress }, callbackFunction, "json");
}


Service.checkSessionVar = function(debug) {
    var url = Service.SercureBaseUrl + encodeURI("?service=checkSessionVar" + "&debug=" + debug);
    $.post(url,
        function(data) {
            alert("data.result: " + data.result);
            alert("data.sessionID: " + data.sessionID);
            alert("loggedIn: " + Service.readCookie('loggedIn'));
        });
}


Service.accountDetails = function(debug, callbackFunction) {
    var url = Service.SercureBaseUrl + encodeURI("?service=accountDetails" + "&debug=" + debug);
    $.post(url, {}, callbackFunction, "json");
}


Service.readCookie = function(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

Service.createCookie = function(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

Service.Comments = function() {

    window.status = "checking comments" + Math.random();
    var loggedIn = Service.readCookie("loggedIn");
    var name = Service.readCookie("name");

    var ob = document.getElementById('CommentsBox');

    if (ob == null) {
        document.write("<div id='CommentsBox'><h1>Comments</h1></div>");
    }

    ob = document.getElementById('CommentsBox');

    var contents = "Log in to comment";

    if (loggedIn == "true") {
        contents = "comment:<textarea width=30 height=10 name='comment'>";
    }

    ob.innerHTML = contents;

    setTimeout("Service.Comments()", 4000)
}

Service.PostComment = function( _comment, _commentType, _objectId, debug, callBackFunction) {
    var url = Service.SercureBaseUrl + encodeURI("?service=pcomment" + "&debug=" + debug);
    $.post(url,
        {
            comment: _comment,
            commentType: _commentType,
            objectId: _objectId
        },
        callBackFunction, 
        "json");
}

loginAuth = function() { }

loginAuth.loggedout = function(cls) {
        var el = $("." + cls);
        if (typeof cls =="string") {
             for (i = 0; i <= (el.length-1); i++) {
                el[i].style.display = 'none';
             }
        }
        else {
            return;
        }
}


loginAuth.loggedin = function(cls) {
    var el = $("." + cls);
    if (typeof cls =="string") {
        for (i = 0; i <= (el.length-1); i++) {
            el[i].style.display = 'block';
        }
    }
    else {
        return;
    }
}

loginAuth.checkAuthentication = function(cls, param) {
    switch(param) {
        case false:
            loginAuth.loggedout(cls);     
            break
        case true:
            loginAuth.loggedin(cls);
            break;
    }
}

getQueryParameterByName = function (name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return results[1];
}

   

