﻿function ValidateEmail() {
    // txtPassword.SetEnabled(false); btnSignIn.SetEnabled(false); pnlContact.SetVisible(true);lblLoginFormValidationMessage.SetText('');$('#anchor1').click(); parent.loginPopup.SetSize(425, 450);txtFirstName.GetInputElement().focus(); }
    lblLoginFormValidationMessage.SetText('');
    if (txtEmail.GetText() == '') {
        txtEmail.GetInputElement().focus();
        lblLoginFormValidationMessage.SetText("Please enter your email.");
        return false;
    }
    if (!LogWindow_IsValidEmailAddress(txtEmail.GetText())) {
        txtEmail.GetInputElement().focus();
        lblLoginFormValidationMessage.SetText("Please enter a valid email.");
        return false;
    }
    
    //check email format
    MembershipService_IsUserAvailable(txtEmail.GetText(), LogWindow_ValidateEmailSuccess, LogWindow_ValidateEmailFailed);
}

function LogWindow_ValidateEmailSuccess(isAvailable, userContext, methodName) {
    if(isAvailable) {
        txtPassword.SetEnabled(false);
        btnSignIn.SetEnabled(false);
        pnlContact.SetVisible(true);
        lblLoginFormValidationMessage.SetText('');
        $('#anchor1').click();
        parent.loginPopup.SetSize(425, 450);
        txtFirstName.GetInputElement().focus(); 
    }
    else {
        lblLoginFormValidationMessage.SetText("The email address you entered is associated with a Blacoh.com account. Please enter your password. If you need help with your password, click the link below.");
    }
}
function LogWindow_ValidateEmailFailed(error, userContext, methodName) {
    lblContactFormValidationMessage.SetText(error.get_message());
}


function RegisterMe() {

    if (txtEmail.GetText() == '') {
        txtEmail.GetInputElement().focus();
        lblContactFormValidationMessage.SetText("Please enter your email.");
        return false;
    }
    if (txtFirstName.GetText() == '') {
        txtFirstName.GetInputElement().focus();
        lblContactFormValidationMessage.SetText("Please enter your first name.");
        return false;
    }
    if (txtLastName.GetText() == '') {
        txtLastName.GetInputElement().focus();
        lblContactFormValidationMessage.SetText("Please enter your last name.");
        return false;
    }
    if (txtCompany.GetText() == '') {
        txtCompany.GetInputElement().focus();
        lblContactFormValidationMessage.SetText("Please enter your company name.");
        return false;
    }
    if (txtPhone.GetText() == '') {
        txtPhone.GetInputElement().focus();
        lblContactFormValidationMessage.SetText("Please enter your phone.");
        return false;
    }
    if (!chkTerms.GetChecked()) {
        chkTerms.Focus();
        lblContactFormValidationMessage.SetText("Please read and agree to the Terms of Use.");
        return false;
    }
    //
    if (chkSignInGuest.GetChecked() == false) {
        if (txtEnterPassword.GetText() == '') {
            txtEnterPassword.GetInputElement().focus();
            lblContactFormValidationMessage.SetText("Please enter your password.");
            return false;
        }
        if (txtConfirmPassword.GetText() != txtEnterPassword.GetText()) {
            txtEnterPassword.GetInputElement().focus();
            lblContactFormValidationMessage.SetText("Passwords do not match");
            return false;
        }
        lblContactFormValidationMessage.SetText("");
        MembershipService_CreateUserWithCallback(txtFirstName.GetText(),
                    txtLastName.GetText(),
                    txtCompany.GetText(),
                    txtPhone.GetText(),
                    txtEmail.GetText(),
                    txtEnterPassword.GetText(),
                    txtEmail.GetText(),
                    LogWindow_RegistrationSuccess,
                    LogWindow_RegistrationFailed);
    }
    else {
        lblContactFormValidationMessage.SetText("");
        //sign in as 'Guest'
        MembershipService_CreateGuestWithCallback(txtFirstName.GetText(),
                    txtLastName.GetText(),
                    txtCompany.GetText(),
                    txtPhone.GetText(),
                    txtEmail.GetText(),
                    LogWindow_GuestSuccess,
                    LogWindow_GuestFailed);
    }
}
function LoginMeIn() {
    if (txtEmail.GetText() == '') {
        txtEmail.GetInputElement().focus();
        lblLoginFormValidationMessage.SetText("Please enter your email.");
        return false;
    }
    if (txtPassword.GetText() == '') {
        txtPassword.GetInputElement().focus();
        lblLoginFormValidationMessage.SetText("Please enter your password or select the button indicating you are new to Blacoh.com. If you need help with your password, click the link below.");
        return false;
    }
    lblLoginFormValidationMessage.SetText("");
    MembershipService_IsUserAccountActive(txtEmail.GetText(), LogWindow_UserAccountActiveSuccess, LogWindow_LoginFailed);
}

function LogWindow_UserAccountActiveSuccess(accountActive, userContext, methodName) {
    AuthenticationService_LoginWithCallback(txtEmail.GetText(), txtPassword.GetText(), LogWindow_LoginSuccess, LogWindow_LoginFailed);
}

function LogWindow_LoginSuccess(validCredentials, userContext, methodName) {
    if (validCredentials) {
        parent.LogWindowShowPanel(2);
        if (parent.logWindowSource == 'top') {
            parent.location.href = parent.location.href;
            parent.loginPopup.Hide();
        } else
            parent.LogWindowSignInSuccess();  //switch
    }
    else
        lblLoginFormValidationMessage.SetText('The email address and password do not match. Please try again. If you need help with your password, click the link below.');
}

function LogWindow_LoginFailed(error, userContext, methodName) {
    lblLoginFormValidationMessage.SetText(error.get_message());
}
function LogWindow_RegistrationSuccess(validCredentials, userContext, methodName) {
    if (validCredentials) {
        parent.LogWindowShowPanel(2);
        if (parent.logWindowSource == 'top') {
            //show thank you
            pnlSignIn.SetVisible(false); pnlContact.SetVisible(false); pnlGuestThanks.SetVisible(false); pnlRegistrationThanks.SetVisible(true);
            parent.loginPopup.SetHeaderText("Welcome");
        } else {
            parent.LogWindowRegistrationSuccess(); //switch
        }
    } else
        lblContactFormValidationMessage.SetText('Registration for ' + userContext + ' failed.');
}
function LogWindow_RegistrationFailed(error, userContext, methodName) {
    lblContactFormValidationMessage.SetText(error.get_message());
}
function LogWindow_GuestSuccess(validCredentials, userContext, methodName) {
    if (validCredentials) {
        parent.LogWindowShowPanel(1);
        if (parent.logWindowSource == 'top') {
            //show thank you
            pnlSignIn.SetVisible(false); pnlContact.SetVisible(false); pnlGuestThanks.SetVisible(true); pnlRegistrationThanks.SetVisible(false);
            parent.loginPopup.SetHeaderText("Welcome");
        } else {
            parent.LogWindowGuestRegistrationSuccess(); //switch
        }
    } else
        lblContactFormValidationMessage.SetText('Guest Registration for ' + userContext + ' failed.');
}
function LogWindow_GuestFailed(error, userContext, methodName) {
    lblContactFormValidationMessage.SetText(error.get_message());
}

function LogWindow_IsValidEmailAddress(emailAddress) {
    if ((emailAddress.indexOf('@') < 0) || ((emailAddress.charAt(emailAddress.length - 4) != '.') && (emailAddress.charAt(emailAddress.length - 3) != '.'))) {
        return false;
    }
    else
        return true;
}