﻿function submitLogin() {
    //Swap between the two login type
    if ($("#email").is(":visible")) {
        var params = {
            'Email': escape($.trim($("#email").val())),
            'Password': escape($.trim($("#password").val()))
        }
        var type = { LoginMethod: 'ByEmail' };
    } else {
        var params = {
            'Password': escape($.trim($("#password").val())),
            'Firstname': escape($.trim($("#firstname").val())),
            'Lastname': escape($.trim($("#lastname").val()))
        }
        var type = { LoginMethod: 'ByName' };
    }

    //Look for empty parameters
    if (validateLogin(params)) {
        $.post("/login/LoginHandler.ashx", $.extend(type, params), callBackLogin);
    }
}

function validateLogin(params) {
    //Todo: Need to Translate those error msg.
    var errors_text = {
        'Password': "Please provide a Password.",
        'Firstname': "Please provide a Firstname.",
        'Lastname': "Please provide a Lastname.",
        'Email': "Please provide a Email."
    }

    $(".box_field input, box_field label").removeClass("login_error");

    var errorDisplay = $(".error_display");
    var error = '';

    $.each(params, function(i, n) {
        if (!n.length) {
            var id = i.toLowerCase();
            $("#" + id + ", label_" + id).addClass("login_error");
            error = eval('errors_text.' + i);
            return error;
        }
    });

    if (error != "") {
        errorDisplay.html("* " + error);
        return false;
    } else {
        errorDisplay.html("");
        return true;
    }

}

function callBackLogin(server_response) {
    var errorDisplay = $(".error_display");
    var server_response = unescape(server_response);
    var res = server_response.split("&");
    var auth = res[0].split("=")[1];


    if (auth == "False") {
        var msg = res[1].split("=")[1];
        errorDisplay.html("* " + URLDecode(msg));
    } else {
        window.location = "/Home/";
    }
}

function URLDecode(psEncodeString) {
    // Create a regular expression to search all +s in the string
    var lsRegExp = /\+/g;
    // Return the decoded string
    return unescape(String(psEncodeString).replace(lsRegExp, " "));
}


//************************************

String.formatSTR = function (text) {
    //check if there are two arguments in the arguments list
    if (arguments.length <= 1) {
        //if there are not 2 or more arguments there’s nothing to replace
        //just return the original text
        return text;
    }
    //decrement to move to the second argument in the array
    var tokenCount = arguments.length - 2;
    for (var token = 0; token <= tokenCount; token++) {
        //iterate through the tokens and replace their placeholders from the original text in order
        text = text.replace(new RegExp("\\{" + token + "\\}", "gi"), arguments[token + 1]);
    }
    return text;
};
String.prototype.asIllegalChar = function () {
    var re = new RegExp(/[^a-z'-.\séèêëàäâìòôöùûüç]|\*|\(|\)|\+|,/gi);
    if (this.match(re) == null)
        return false
    else
        return true
};

String.prototype.isLegalCharForPhone = function () {
    var re = new RegExp(/[^0-9() -.]/gi);
    if (this.match(re) == null)
        return true;
    else
        return false;
};

String.prototype.isStartingWithLetter = function () {
    var re = new RegExp(/^[a-z]/gi)
    if (this.match(re) == null)
        return false;
    else
        return true;
};

String.prototype.trim = function () {
    return this.replace(/^\s+|\s+$/g, "");
};


String.prototype.ltrim = function () {
    return this.replace(/^\s+/, "");
};
String.prototype.rtrim = function () {
    return this.replace(/\s+$/, "");
};

String.prototype.validateMinLength = function (length) {
    return this.length >= length;
};




function clearCombo(obj) {
    var l = obj[0].length;
    for (var i = 0; i < l; i++) {
        obj[0].remove(obj[0].length - 1);
    }
}

function validateEmail(email) {
    var pattern = new RegExp(/^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/i);
    return pattern.test(email)
}

function EJson(str) {

    var find = "\\'";
    var regex = new RegExp(find, "g");
    return str.replace(regex, '\\u0027')
}

function querySt(ji) {
    hu = window.location.search.substring(1);
    gy = hu.split("&");
    for (i = 0; i < gy.length; i++) {
        ft = gy[i].split("=");
        if (ft[0] == ji) {
            return ft[1];
        }
    }
}

function get_cookie(cookie_name) {
    var results = document.cookie.match('(^|;) ?' + cookie_name + '=([^;]*)(;|$)');

    if (results)
        return (unescape(results[2]));
    else
        return null;
}
