﻿/*
    signin
*/

onloadAdd('initialisesignin()');

function initialisesignin() {
    // Check for cookie
    var lcookie = readCookie("signin");
    var uremember = lcookie ? lcookie : "";
    
    if (uremember != "") {
        document.getElementById("signinemail").value = uremember;
        document.getElementById("signinremember").checked = true;
        // Set focus
        document.getElementById("signinpassword").focus();
    }
    else {
        // Set focus
        document.getElementById("signinemail").focus();
    }
}

function signin()
{
    // Disable the form and show loading animation
    disableform(document.getElementById("signinform"));
    document.getElementById("siajaxloader").className = "left";
    // Get details
    var email, password;
    email = document.getElementById("signinemail").value;
    password = document.getElementById("signinpassword").value;
    if (email == "" && password == "") {
        document.getElementById("signinmessage").innerHTML = "<p>Please enter your e-mail address and password to login.</p>";
        document.getElementById("signinmessage").className = "";
        enableform(document.getElementById("signinform"));
        return false;
    }
    
    // Remember
    if (document.getElementById("signinremember").checked == true) {
        createCookie("signin", document.getElementById("signinemail").value, 30) ;
    }
    else {
        createCookie("signin", "", 0) ;
    }

    var ctx = {
        divid: "signinform",
        animid: "siajaxloader"
    };

    PageMethods.Signin_User(email, password, signinsuccess, ajaxfailure, ctx);
}

function signinsuccess(value, ctx, methodName) { 
    // Get the response value
    if (value == "false") {
        alert("The e-mail address / password combination entered was incorrect");
        // Enable button and hide animation
        enableform(document.getElementById("signinform"));
        document.getElementById(ctx.animid).className = "hidden";
    }
    else if (value == "true") {
        // Set the status
        top.location.href = "tracker/projects/default.aspx";
    }
    else {
        // Show the options
        greyOut(true);
        document.getElementById("edititem").className = "";
        document.getElementById("edititem").innerHTML = value;
    }
} 

function togglepasswordform(show) {
    if (show) {
        document.getElementById("signinform").className = "hidden";
        document.getElementById("passwordform").className = "signinform";
        document.getElementById("passwordfields").className = "";
        document.getElementById("passwordmessage").innerHTML = "";
        document.getElementById("passwordmessage").className = "hidden";
    }
    else {
        document.getElementById("passwordform").className = "hidden";
        document.getElementById("signinform").className = "signinform";
        document.getElementById("signinmessage").innerHTML = "";
        document.getElementById("signinmessage").className = "hidden";
    }
}

function signinforgottenpassword()
{
    // Disable the form and show loading animation
    disableform(document.getElementById("passwordform"));
    var email;
    email = document.getElementById("passwordemail").value;
    if (email == "") {
        document.getElementById("passwordmessage").innerHTML = "<p>Please enter your e-mail address.</p>";
        document.getElementById("passwordmessage").className = "";
        enableform(document.getElementById("passwordform"));
        return false;
    }
    // Call ajax function
    //Ajax_Scripts.Signin_User(email, password, signincallback, ajaxfailure, ctx);
    PageMethods.Signin_Forgotten_Password(email, signinforgottenpasswordsuccess, ajaxfailure);
}

function signinforgottenpasswordsuccess(value, ctx, methodName)
{
    // Get the response value
    if (value != "success") {
        document.getElementById("passwordmessage").innerHTML = "<p>Your email address was not found. If you do not already have a user account please get in touch with your administrator.</p>";
        document.getElementById("passwordmessage").className = "";
        enableform(document.getElementById("passwordform"));
    }
    else {
        enableform(document.getElementById("passwordform"));
        document.getElementById("passwordfields").className = "hidden";
        document.getElementById("passwordmessage").innerHTML = "<p>Your password has been e-mailed to your registered e-mail address.</p><a href='#' onclick='togglepasswordform(false);' title='View our sign in form'>&lt; Back to our sign in form</a>";
        document.getElementById("passwordmessage").className = "";
    }
}

function selecttracker(sid, ctype) {
    PageMethods.Signin_Tracker_Select(sid, ctype, signintrackerselectsuccess, ajaxfailure);
}

function signintrackerselectsuccess(value, ctx, methodName)
{
    // Set the status
    top.location.href = "tracker/projects/default.aspx";
}

function submitsignin(objEvent)
{

    var iKeyCode, strKey;

    if (isIE) {
        iKeyCode = objEvent.keyCode;
    }
    else {
        iKeyCode = objEvent.which; 
    }

    if (iKeyCode == 13) {
        signin();
        return false;
    }
    
    return true;

}

function submitsigninforgottenpassword(objEvent)
{

    var iKeyCode, strKey;

    if (isIE) {
        iKeyCode = objEvent.keyCode;
    }
    else {
        iKeyCode = objEvent.which; 
    }

    if (iKeyCode == 13) {
        signinforgottenpassword();
        return false;
    }
    
    return true;

}


