﻿///
/// Copyright ¸ 2009, ayondo GmbH.
/// All rights reserved.
/// http://www.ayondo.com
///
/// Redistribution and use in source and binary forms, with or without
/// modification, is strictly prohibited.
///

// registration process functions go here!

//===================================================================================
/// register functions
//  TODO create js class for this functionalities
//===================================================================================
var RegData = [];  // global object contains user input in registry form


function initRegister() {
    RegData = [];
    clearRegisterFields();
    addCallbacks();

}
/// <summary>
/// delete content of error fields
/// </summary>
function clearErrors() {

    TOOL.hideError("ErrLabelTitle");
    TOOL.hideError("ErrLabelFirstname");
    TOOL.hideError("ErrLabelLastname");
    TOOL.hideError("BirthdayError");
    TOOL.hideError("ErrLabelUsername");
    TOOL.hideError("ErrLabelEmail");
    TOOL.hideError("ErrLabelPassword");
    TOOL.hideError("ErrLabelDisclaimer");

    TOOL.hideError("ErrLabelEmailRep");
    TOOL.hideError("ErrLabelPasswordRep");
}
/// <summary>
/// delete user input
/// </summary>
function clearRegisterFields() {
    //clearErrors();
    $get("RegTitle1").value = -1;

    var yearTextBox = $get("RegBYear");
    yearTextBox.value = Loc.Dat.yearInit;

    $get("RegPreName").value = "";
    $get("RegName").value = "";
    $get("RegCountry").value = "DE";

    $get("RegEmail").value = "";
    $get("RegEmailRep").value = "";

    $get("RegBDay").value = "";
    $get("RegBMonth").value = "";

    $get("RegUserName").value = "";
    $get("RegPW1").value = "";
    $get("RegPW2").value = "";

    $get("CheckBox1").checked = false;
    $get("CheckBox2").checked = false;
    $get("CheckBox3").checked = false;
}

function addCallbacks() {

    $get("RegEmail").onpaste = PasteAction;
    $get("RegEmailRep").onpaste = PasteAction;

    $get("RegPW1").onpaste = PasteAction;
    $get("RegPW2").onpaste = PasteAction;
    $addHandler($get("RegisterNowBtn"), "click", registerUser);

    $get("RegBDay").onchange = function() {
        if ($get("RegBDay").value != "" && $get("RegBMonth").value != "" && $get("RegBYear").value != Loc.Dat.yearInit) {
            TOOL.hideError("BirthdayError");
        }
    }
    $get("RegBMonth").onchange = function() {
        if ($get("RegBDay").value != "" && $get("RegBMonth").value != "" && $get("RegBYear").value != Loc.Dat.yearInit) {
            TOOL.hideError("BirthdayError");
        }
    }
    $get("RegBYear").onchange = function() {
        if ($get("RegBDay").value != "" && $get("RegBMonth").value != "" && $get("RegBYear").value != Loc.Dat.yearInit) {
            TOOL.hideError("BirthdayError");
        }
    }
    $get("RegBYear").onfocus = function() {
        if ($get("RegBYear").value == Loc.Dat.yearInit) {
            $get("RegBYear").value = "";
        }
    }

    $get("RegTitle1").onchange = function() {
        if (this.value != "-1") {
            TOOL.hideError("ErrLabelTitle");
        }
    }
    $get("RegPreName").onblur = function() { TOOL.checkField(this.value, "ErrLabelFirstname", "name", 2, Loc.Dat.login.Errors.Prename); }

    $get("RegName").onblur = function() { TOOL.checkField(this.value, "ErrLabelLastname", "name", 2, Loc.Dat.login.Errors.Name); }

    $get("RegEmail").onblur = function() {
        clearPasteAction("RegEmail");
        if (!TOOL.checkEMail(this.value)) {
            TOOL.showError("ErrLabelEmail", Loc.Dat.login.Errors.Email);
        } else {
            TOOL.hideError("ErrLabelEmail");
        }
    }

    $get("RegEmailRep").onblur = function() {
        clearPasteAction("RegEmailRep");
        if (!TOOL.checkEMail(this.value)) {
            TOOL.showError("ErrLabelEmailRep", Loc.Dat.login.Errors.Email2);
        } else if (this.value != $get("RegEmail").value) {
            TOOL.showError("ErrLabelEmailRep", Loc.Dat.login.Errors.Email2);
        } else {
            TOOL.hideError("ErrLabelEmailRep");
        }
    }

    $get("RegUserName").onblur = function() { TOOL.checkField(this.value, "ErrLabelUsername", "credential", 5, Loc.Dat.login.Errors.Username); }

    $get("RegPW1").onblur = function() {
        clearPasteAction("RegPW1");
        TOOL.checkField(this.value, "ErrLabelPassword", "credential", 5, Loc.Dat.login.Errors.Password2);
    }
    $get("RegPW2").onblur = function() {
        clearPasteAction("RegPW2");
        if ($get("RegPW1").value != this.value)
            TOOL.showError("ErrLabelPasswordRep", Loc.Dat.login.Errors.Password);
        else
            TOOL.hideError("ErrLabelPasswordRep");
    }
    // setup tooltips for the info bullets
    var bullet = TOOL.getDomElement("FirstNameInfoBullet_RegNameSubtitle");
    $addHandler(bullet, "mouseover", TOOLTIP.show);
    $addHandler(bullet, "mouseout", TOOLTIP.hide);

    bullet = TOOL.getDomElement("BirthdayInfoBullet_RegBirthdaySubtitle");
    $addHandler(bullet, "mouseover", TOOLTIP.show);
    $addHandler(bullet, "mouseout", TOOLTIP.hide);

    bullet = TOOL.getDomElement("UserNameInfoBullet_RegUserNameInfo");
    $addHandler(bullet, "mouseover", TOOLTIP.show);
    $addHandler(bullet, "mouseout", TOOLTIP.hide);

    bullet = TOOL.getDomElement("PasswordInfoBullet_RegPasswordInfo");
    $addHandler(bullet, "mouseover", TOOLTIP.show);
    $addHandler(bullet, "mouseout", TOOLTIP.hide);

}

/// <summary>
/// put server side html(registry form) in container and creates handler
/// </summary>
function createRegisterPage(content) {

    RegData = [];
    $get("LoginContent").innerHTML = content;
    clearRegisterFields();

    addCallbacks();
}
/// <summary>
/// validate user input of all fields
/// </summary>
function checkRegisterFields() {

    var success = true;
    readRegisterForm();
    if (RegData.RegTitle1 == "-1") {
        TOOL.showError("ErrLabelTitle", Loc.Dat.login.Errors.Gender);
        success = false;
    }
    if (!TOOL.checkField(RegData.RegPreName, "ErrLabelFirstname", "name", 2, Loc.Dat.login.Errors.Prename)) success = false;
    if (!TOOL.checkField(RegData.RegName, "ErrLabelLastname", "name", 2, Loc.Dat.login.Errors.Name)) success = false;

    if (!TOOL.checkEMail(RegData.RegEmail)) {
        TOOL.showError("ErrLabelEmail", Loc.Dat.login.Errors.Email);
        success = false;
    }

    // year of birth check
    var currentDate = new Date();
    var yearInt = parseInt(RegData.RegBYear);

    if (yearInt < 1900 || yearInt >= currentDate.getFullYear()) {
        TOOL.showError("BirthdayError", Loc.Dat.login.Errors.Birthday);
        success = false;
    }

    if (!RegData.RegBDay || !RegData.RegBMonth || !RegData.RegBYear) {
        TOOL.showError("BirthdayError", Loc.Dat.login.Errors.Birthday);
        success = false;
    }
    if (!TOOL.checkField(RegData.RegUserName, "ErrLabelUsername", "credential", 5, Loc.Dat.login.Errors.Username)) success = false;
    if (!TOOL.checkField(RegData.RegPW1, "ErrLabelPassword", "credential", 5, Loc.Dat.login.Errors.Password2)) success = false;

    if (!RegData.RegDisclaimer) {
        TOOL.showError("ErrLabelDisclaimer", Loc.Dat.login.Errors.Disclaimer);
        success = false;
    }
    if (!RegData.RegAGB) {
        TOOL.showError("ErrLabelDisclaimer", Loc.Dat.login.Errors.AGB);
        success = false;
    }
    if (success == true && RegData.RegUserName == RegData.RegPW1) {
        TOOL.showError("ErrLabelPassword", Loc.Dat.login.Errors.UserEqualPW);
        success = false;
    }
    if (RegData.RegEmailRep != RegData.RegEmail) {
        TOOL.showError("ErrLabelEmailRep", Loc.Dat.login.Errors.Email2);
        success = false;
    }

    if (RegData.RegPW1 != RegData.RegPW2) {
        TOOL.showError("ErrLabelPasswordRep", Loc.Dat.login.Errors.Password);
        success = false;
    }    
    
    return success;
}
/// <summary>
/// reads user input of all fields to RegData object
/// </summary>
function readRegisterForm() {

    if ($get("RegTitle1"))
        RegData.RegTitle1 = $get("RegTitle1").value;
    RegData.RegPreName = $get("RegPreName").value;
    RegData.RegName = $get("RegName").value;
    RegData.RegCountry = $get("RegCountry").value;
    RegData.RegEmail = $get("RegEmail").value;
    RegData.RegEmailRep = $get("RegEmailRep").value;

    RegData.RegBDay = $get("RegBDay").value;
    RegData.RegBMonth = $get("RegBMonth").value;
    RegData.RegBYear = $get("RegBYear").value;

    RegData.RegUserName = $get("RegUserName").value;
    RegData.RegPW1 = $get("RegPW1").value;
    RegData.RegPW2 = $get("RegPW2").value;

    RegData.RegNewsletter = $get("CheckBox1").checked;
    RegData.RegDisclaimer = $get("CheckBox2").checked;
    RegData.RegAGB = $get("CheckBox3").checked;
}
/// <summary>
/// refill registry form after viewing some other content
/// </summary>
function fillRegisterForm() {

    $get("RegTitle1").value = RegData.RegTitle1;
    $get("RegPreName").value = RegData.RegPreName;
    $get("RegName").value = RegData.RegName;
    $get("RegCountry").value = RegData.RegCountry;
    $get("RegEmail").value = RegData.RegEmail;

    $get("RegBDay").value = RegData.RegBDay;
    $get("RegBMonth").value = RegData.RegBMonth;
    $get("RegBYear").value = RegData.RegBYear;

    $get("RegUserName").value = RegData.RegUserName;
    $get("RegPW1").value = RegData.RegPW1;

    $get("CheckBox1").checked = RegData.RegNewsletter;
    $get("CheckBox2").checked = RegData.RegDisclaimer;
    $get("CheckBox3").checked = RegData.RegAGB;
}
/// <summary>
/// final client side validation of user input
/// call webservice 
/// </summary>
function registerUser() {

    clearErrors();
    if (checkRegisterFields()) {
        LoginService.CreateUser(RegData.RegTitle1, " ", RegData.RegPreName, RegData.RegName, " ", " ", RegData.RegCountry, RegData.RegEmail, RegData.RegBDay, RegData.RegBMonth, RegData.RegBYear, RegData.RegUserName, RegData.RegPW1, RegData.RegNewsletter, showRegistrationResult, callError);
    }
}
/// <summary>
/// result of registration attempt
/// </summary>
function showRegistrationResult(result) {
    Sys.Debug.trace("showRegistrationResult " + result);
    var LoginError = false;
    if (result == "E-Mail") {
        TOOL.showError("ErrLabelEmail", Loc.Dat.login.Errors.EmailRegistered);
        LoginError = true;
    }
    if (result == "UserName") {
        TOOL.showError("ErrLabelUsername", Loc.Dat.login.Errors.UserNameRegistered);
        LoginError = true;
    }
    if (result == "TooManyAttempts") {
        TOOL.showError("ErrLabelDisclaimer", Loc.Dat.login.Errors.ToManyAttempts);
        LoginError = true;
    }
    if (!LoginError) {
        if (result == "OKOK") {
            var pageId = Loc.Dat.login.page_ids.registered.label;

            var url = "http://" + window.location.host + "/Default.aspx?lang=" + Language + "&id=" + pageId;

            if (ServerEnvironment == "Local")
                location.href = url;

            if (ServerEnvironment == "Development")
                location.href = url;

            if (ServerEnvironment == "Production")
                location.href = "https://www.ayondo.com/Default.aspx?lang=" + Language + "&id=" + pageId;

        }
    }
}
function showRegisterPage(c) {
    $get("LoginContent").innerHTML = c;
}

/*
/// <summary>
/// call registry form from backend
/// </summary>
function showRegisterPage() {
var currentUrl = location.href.toString().substring(0, 5);



$get("LoginContent").innerHTML = "";
createLoader($get("LoginContent"));
LandingPageService.CreateRegisterPage(createRegisterPage, callError);
setPageTitle(Loc.Dat.pageTitles.registration);

}
*/
if (typeof (Sys) != "undefined") { Sys.Application.notifyScriptLoaded(); }