﻿/* Signin scripting
--------------------------  */
var ctrlp;
function signin(o)
{
    // get the control prefix
    ctrlp = getID(o);

    // Disable the form and show loading animation
    disableform(document.getElementById(ctrlp + "signinform"));
    document.getElementById(ctrlp + "siajaxloader").className = "right";
    
    var email, password, remember;
    email = document.getElementById(ctrlp + "signinemail").value;
    password = document.getElementById(ctrlp + "signinpassword").value;
    remember = document.getElementById(ctrlp + "cbxRemember").checked;
    if (email == "" && password == "") {
        alert("Please enter your e-mail address and password to login");
        enableform(document.getElementById(ctrlp + "signinform"));
        document.getElementById(ctrlp + "siajaxloader").className = "hidden";
        return false;
    }
    // Call ajax function
    TheSniffa.Signin_User(email, password, remember, signincallback);
}

function signincallback(response)
{
    // Check for errors
    if(response.error != null) {
    	alert("A system error occurred. Please try again. Error type: " + response.error.Message);
        // Enable button and hide animation
        enableform(document.getElementById(ctrlp + "signinform"));
        document.getElementById(ctrlp + "siajaxloader").className = "hidden";
		return;
	}
    // Get the response value
    var lStatus = response.value;

    if (lStatus == "inactive") {
        enableform(document.getElementById(ctrlp + "signinform"));
        document.getElementById(ctrlp + "siajaxloader").className = "hidden";
        alert("Sign in unsuccessful\r\n\r\nYour account is currently inactive. If you would like to reactivate your account get in touch through our contact page")
    }
    else if (lStatus == "false") {
        enableform(document.getElementById(ctrlp + "signinform"));
        document.getElementById(ctrlp + "siajaxloader").className = "hidden";
        alert("Sign in unsuccessful\r\n\r\nYour email address and password combination could not be found. If you have not already signed up for a Sniffa Site please visit our sign up page")
    }
    else {
        if (retURL) {
            window.location.href = retURL;
        }
        else {
            window.location.href = lStatus;
        }
    }
}

function submitsignin(objEvent)
{

    var iKeyCode, strKey, o;

    if (isIE) {
        iKeyCode = objEvent.keyCode;
        o = objEvent.srcElement;
    }
    else {
        iKeyCode = objEvent.which; 
        o = objEvent.target;
    }

    if (iKeyCode == 13) {
        signin(o);
        return false;
    }
    
    return true;

}

function togglepasswordform(show, o) {
    // get the control prefix
    ctrlp = getID(o);

    if (show) {
        document.getElementById(ctrlp + "signinform").className = "hidden";
        document.getElementById(ctrlp + "passwordform").className = "signinform";
        document.getElementById(ctrlp + "passwordfields").className = "";
        document.getElementById(ctrlp + "passwordmessage").innerHTML = "";
        document.getElementById(ctrlp + "passwordmessage").className = "hidden";
    }
    else {
        document.getElementById(ctrlp + "passwordform").className = "hidden";
        document.getElementById(ctrlp + "signinform").className = "signinform";
        document.getElementById(ctrlp + "signinmessage").innerHTML = "";
        document.getElementById(ctrlp + "signinmessage").className = "hidden";
    }
}

function signinforgottenpassword(o)
{
    // get the control prefix
    ctrlp = getID(o);

    // Disable the form and show loading animation
    disableform(document.getElementById(ctrlp + "passwordform"));
    document.getElementById(ctrlp + "sifpajaxloader").className = "right";
    var email;
    email = document.getElementById(ctrlp + "passwordemail").value;
    if (email == "") {
        alert("Please enter your e-mail address");
        enableform(document.getElementById(ctrlp + "passwordform"));
        document.getElementById(ctrlp + "sifpajaxloader").className = "hidden";
        return false;
    }
    // Call ajax function
    TheSniffa.Signin_Forgotten_Password(email, signinforgottenpasswordcallback);
}

function signinforgottenpasswordcallback(response)
{
    // Check for errors
    if(response.error != null) {
    	alert("A system error occurred. Please try again. Error type: " + response.error.Message);
        // Enable button and hide animation
        enableform(document.getElementById(ctrlp + "passwordform"));
        document.getElementById(ctrlp + "sifpajaxloader").className = "hidden";
		return;
	}
    // Get the response value
    var lStatus = response.value;

    if (lStatus != "true") {
        alert("Your email address was not recognised. If you have not already signed up for a Sniffa Site please visit our sign up page");
        enableform(document.getElementById(ctrlp + "passwordform"));
        document.getElementById(ctrlp + "sifpajaxloader").className = "hidden";
    }
    else {
        document.getElementById(ctrlp + "passwordfields").className = "hidden";
        document.getElementById(ctrlp + "passwordmessage").innerHTML = "<p>Your password has been e-mailed to your registered e-mail address.</p><p>Your password has been e-mailed from helpdesk@thesniffa.com. Please ensure that your spam filters will allow e-mails from this address.</p><a href='#' id='" + ctrlp + "lnkback' onclick='togglepasswordform(false, this);' title='View our sign in form'>&lt; Back to our sign in form</a>";
        document.getElementById(ctrlp + "passwordmessage").className = "";
        enableform(document.getElementById(ctrlp + "passwordform"));
        document.getElementById(ctrlp + "sifpajaxloader").className = "hidden";
    }
}

function submitsigninforgottenpassword(objEvent)
{

    var iKeyCode, strKey, o;

    if (isIE) {
        iKeyCode = objEvent.keyCode;
        o = objEvent.srcElement;
    }
    else {
        iKeyCode = objEvent.which; 
        o = objEvent.target;
    }

    if (iKeyCode == 13) {
        signinforgottenpassword(o);
        return false;
    }
    
    return true;

}
