﻿//    var phoneRegEx = new RegExp(/^\(?[\d]{3}\)?[\s-]?[\d]{3}[\s-]?[\d]{4}$/);
//    var emailRegEx = new RegExp(/^[a-zA-Z0-9_\-.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9]+$/);
//    var zipRegEx = new RegExp(/^\d{5}$|^\d{5}-\d{4}$/);

var phoneRegEx = /^\(?[\d]{3}\)?[\s-]?[\d]{3}[\s-]?[\d]{4}$/;
var emailRegEx = /^[a-zA-Z0-9_\-.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9]+$/;
var zipRegEx = /^\d{5}$|^\d{5}-\d{4}$/;

function stepOneValidation(CreateUserNameTextBox,CreateUserNameConfirmationTextBox,CreatePasswordTextBox,CreatePasswordConfirmationTextBox,Step1ErrorMessageLabel,CreateUserNameRegularExpressionValidator,CreateUserNameConfirmationRegularExpressionValidator,IsBlackBerry) {

    if (IsBlackBerry == "True") {
        return true;
    }
    
    var createUserNameTextBox = document.getElementById(CreateUserNameTextBox);
    var createUserNameConfirmationTextBox = document.getElementById(CreateUserNameConfirmationTextBox);
    var createPasswordTextBox = document.getElementById(CreatePasswordTextBox);
    var createPasswordConfirmationTextBox = document.getElementById(CreatePasswordConfirmationTextBox);
    var createUserNameRegularExpressionValidator = document.getElementById(CreateUserNameRegularExpressionValidator);
    var createUserNameConfirmationRegularExpressionValidator = document.getElementById(CreateUserNameConfirmationRegularExpressionValidator);
    var errorMessage = ""; 
    var step1ErrorMessageLabel = document.getElementById(Step1ErrorMessageLabel);

    

        if (!step1ErrorMessageLabel.style.display == 'none') {
            step1ErrorMessageLabel.style.display = 'none';
        }

        if (trim(createUserNameTextBox.value) == "") {

            errorMessage += "Please enter an Email address. <br/>";
        }

        if (trim(createUserNameConfirmationTextBox.value) == "") {

            errorMessage += "Please confirm your Email address. <br/>";
        }


        if (!emailRegEx.test(trim(createUserNameTextBox.value))) {

            errorMessage += "Please provide a valid Email. <br/>";
        }


        if (trim(createUserNameConfirmationTextBox.value) != trim(createUserNameTextBox.value)) {

            errorMessage += "Emails do not match. <br/>";
        }

        if (trim(createPasswordTextBox.value) == "") {

            errorMessage += "Please enter a password. <br/>";
        }

        if (trim(createPasswordTextBox.value).length < 7) {

            errorMessage += "Password must be at least 7 characters long. <br/>";
        }

        if (trim(createPasswordConfirmationTextBox.value) == "") {

            errorMessage += "Please confirm your password. <br/>";
        }

        if (trim(createPasswordConfirmationTextBox.value) != trim(createPasswordTextBox.value)) {

            errorMessage += "Passwords do not match. <br/>";
        }

        if (errorMessage != "" || createUserNameRegularExpressionValidator.style.visibility == 'visible' || createUserNameConfirmationRegularExpressionValidator.style.visibility == 'visible') {

            step1ErrorMessageLabel.innerHTML = errorMessage;
            step1ErrorMessageLabel.style.display = 'inline';
            step1ErrorMessageLabel.style.color = 'Red';
            return false;
        }
}

function stepTwoValidation(PrefixDropDownList, FirstNameTextBox, LastNameTextBox, CompanyNameTextBox, PhoneTextBox, MobileTextBox, FaxTextBox, Step2ErrorMessageLabel, PhoneRegularExpressionValidator, MobileRegularExpressionValidator, FaxRegularExpressionValidator, IsBlackBerry) {

    if (IsBlackBerry == "True") {
        return true;
    }
    
    var prefixDropDownList = document.getElementById(PrefixDropDownList);
    var firstNameTextBox = document.getElementById(FirstNameTextBox);
    var lastNameTextBox = document.getElementById(LastNameTextBox);
    var companyNameTextBox = document.getElementById(CompanyNameTextBox);
    var phoneTextBox = document.getElementById(PhoneTextBox);
    var mobileTextBox = document.getElementById(MobileTextBox);
    var faxTextBox = document.getElementById(FaxTextBox);
    var errorMessage = "";

    var phoneRegularExpressionValidator = document.getElementById(PhoneRegularExpressionValidator);
    var mobileRegularExpressionValidator = document.getElementById(MobileRegularExpressionValidator);
    var faxRegularExpressionValidator = document.getElementById(FaxRegularExpressionValidator);
    var step2ErrorMessageLabel = document.getElementById(Step2ErrorMessageLabel);
    

    if (!step2ErrorMessageLabel.style.display == 'none') {
        step2ErrorMessageLabel.style.display = 'none';
    }

    if (!prefixDropDownList.selectedIndex > 0) {

        errorMessage += "Please select your title. <br/>";
    }

    if (trim(firstNameTextBox.value) == "") {

        errorMessage += "Please enter your first name. <br/>";
    }

    if (trim(lastNameTextBox.value) == "") {

        errorMessage += "Please enter your last name. <br/>";
    }

    if (trim(companyNameTextBox.value) == "") {

        errorMessage += "Please enter your company name. <br/>";
    }

    if (trim(phoneTextBox.value) == "") {

        errorMessage += "Please enter your phone number. <br/>";
    }

    if (!phoneRegEx.test(trim(phoneTextBox.value))) {

        errorMessage += "Please enter a valid phone number. <br/>";
    }

    if (errorMessage != "" || phoneRegularExpressionValidator.style.visibility == 'visible' || mobileRegularExpressionValidator.style.visibility == 'visible' || faxRegularExpressionValidator.style.visibility == 'visible') {
        step2ErrorMessageLabel.innerHTML = errorMessage;
        step2ErrorMessageLabel.style.display = 'inline';
        step2ErrorMessageLabel.style.color = 'red';
        return false;
    }
    
}

function stepThreeValidation(PhysicalAddress1TextBox, PhysicalAddress2TextBox, PhysicalCityTextBox, PhysicalStateDropDownList, OtherPhysicalStateTextBox, PhysicalZipCodeTextBox, PhysicalCountryDropDownList, MailingAddress1TextBox, MailingAddress2TextBox, MailingCityTextBox, OtherMailingStateTextBox, MailingZipCodeTextBox, MailingCountryDropDownList, MailingStateDropDownList, SameAsPrimaryAddress, Step3ErrorMessagelabel, PhysicalZipCodeRegularExpressionValidator, MailingZipCodeRegularExpressionValidator, IsBlackBerry) {

    if (IsBlackBerry == "True") {
        return true;
    }
    
    var physicalAddress1TextBox = document.getElementById(PhysicalAddress1TextBox);
    var physicalAddress2TextBox = document.getElementById(PhysicalAddress2TextBox);
    var physicalCityTextBox = document.getElementById(PhysicalCityTextBox);
    var physicalStateDropDownList = document.getElementById(PhysicalStateDropDownList);
    var otherPhysicalStateTextBox = document.getElementById(OtherPhysicalStateTextBox);
    var physicalZipCodeTextBox = document.getElementById(PhysicalZipCodeTextBox);
    var physicalCountryDropDownList = document.getElementById(PhysicalCountryDropDownList);
    var mailingAddress1TextBox = document.getElementById(MailingAddress1TextBox);
    var mailingAddress2TextBox = document.getElementById(MailingAddress2TextBox);
    var mailingCityTextBox = document.getElementById(MailingCityTextBox);
    var otherMailingStateTextBox = document.getElementById(OtherMailingStateTextBox);
    var mailingZipCodeTextBox = document.getElementById(MailingZipCodeTextBox);
    var mailingCountryDropDownList = document.getElementById(MailingCountryDropDownList);
    var mailingStateDropDownList = document.getElementById(MailingStateDropDownList);
    var sameAsPrimaryAddress = document.getElementById(SameAsPrimaryAddress);
    var errorMessage = "";

    var physicalZipCodeRegularExpressionValidator = document.getElementById(PhysicalZipCodeRegularExpressionValidator);
    var mailingZipCodeRegularExpressionValidator = document.getElementById(MailingZipCodeRegularExpressionValidator);
    var step3ErrorMessageLabel = document.getElementById(Step3ErrorMessagelabel);

    if (!step3ErrorMessageLabel.style.display == 'none') {
        step3ErrorMessageLabel.style.display = 'none';
    }

    if (trim(physicalAddress1TextBox.value) == "") {

        errorMessage += "Please enter your address. <br/>";
    }

    if (trim(physicalCityTextBox.value) == "") {
    
        errorMessage += "Please enter your city. <br/>";
    }

    if (trim(physicalZipCodeTextBox.value) == "") {

        errorMessage += "Please enter your zip code. <br/>";
    }

    if (!zipRegEx.test(trim(physicalZipCodeTextBox.value))) {

        errorMessage += "Please enter a valid physical zip code. <br/>";
    }
    
    if(!sameAsPrimaryAddress.checked) {

        if (trim(mailingAddress1TextBox.value) == "") {

            errorMessage += "Please enter your mailing address. <br/>";
        }

        if (trim(mailingCityTextBox.value) == "") {

            errorMessage += "Please enter your mailing city. <br/>";
        }

        if (trim(mailingZipCodeTextBox.value) == "") {

            errorMessage += "Please enter your mailing zip code. <br/>";
        }

        if (!zipRegEx.test(trim(mailingZipCodeTextBox.value))) {

            errorMessage += "Please enter a valid mailing zip code. <br/>";
        }
    }

    if (errorMessage != "" || physicalZipCodeRegularExpressionValidator.style.visibility == 'visible' || mailingZipCodeRegularExpressionValidator.style.visibility == 'visible') {
        step3ErrorMessageLabel.innerHTML = errorMessage;
        step3ErrorMessageLabel.style.display = 'inline';
        step3ErrorMessageLabel.style.color = 'Red';
        return false;
    }

}

function ChangePassword(CurrentPassword, NewPassword, ConfirmNewPassword, ChangePasswordConfirmationLabel, IsBlackBerry) {

    if (IsBlackBerry == "True") {
        return true;
    }
    
    var currentPassword = document.getElementById(CurrentPassword);
    var newPassword = document.getElementById(NewPassword);
    var confirmNewPassword = document.getElementById(ConfirmNewPassword);
    var changePasswordConfirmationLabel = document.getElementById(ChangePasswordConfirmationLabel);
    var errorMessage = "";

    if (!changePasswordConfirmationLabel.style.display == 'none') {
        changePasswordConfirmationLabel.style.display = 'none';
    }

    if (trim(currentPassword.value) == "") {

        errorMessage += "Please enter your old password. <br/>";
    }

    if (trim(newPassword.value) != trim(confirmNewPassword.value)) {

        errorMessage += "Passwords do not match. <br/>";
    }

    if (trim(newPassword.value).length < 7) {

        errorMessage += "Password must be at least 7 characters long. <br/>";
    }

    if (errorMessage != "") {
        changePasswordConfirmationLabel.innerHTML = errorMessage;
        changePasswordConfirmationLabel.style.display = 'inline';
        changePasswordConfirmationLabel.style.color = 'Red';
        return false;
    }

}

function ChangeUserName(NewEmailTextBox, ConfirmNewEmailTextBox, PasswordTextBox, ChangeUserNameConfirmationLabel, NewEmailRegularExpressionValidator, ConfirmNewEmailRegularExpressionValidator, IsBlackBerry) {

    if (IsBlackBerry == "True") {
        return true;
    }

    var newEmailTextBox = document.getElementById(NewEmailTextBox);
    var confirmNewEmailTextBox = document.getElementById(ConfirmNewEmailTextBox);
    var passwordTextBox = document.getElementById(PasswordTextBox);
    var changeUserNameConfirmationLabel = document.getElementById(ChangeUserNameConfirmationLabel);
    var errorMessage = "";


    var newEmailRegularExpressionValidator = document.getElementById(NewEmailRegularExpressionValidator);
    var confirmNewEmailRegularExpressionValidator = document.getElementById(ConfirmNewEmailRegularExpressionValidator);
    
    
    if (!changeUserNameConfirmationLabel.style.display == 'none') {
        changeUserNameConfirmationLabel.style.display = 'none';
    }

    if (trim(newEmailTextBox.value) == "") {

        errorMessage += "Please enter your new Email. <br/>";
    }

    if (!emailRegEx.test(trim(newEmailTextBox.value))) {

        errorMessage += "Please provide a valid Email. <br/>";
    }

    if (trim(newEmailTextBox.value) != trim(confirmNewEmailTextBox.value)) {

        errorMessage += "Emails do not match. <br/>";
    }

    if (trim(passwordTextBox.value) == "") {

        errorMessage += "Please enter your password. <br/>";
    }

    if (errorMessage != "" || newEmailRegularExpressionValidator.style.visibility == 'visible' || confirmNewEmailRegularExpressionValidator.style.visibility == 'visible') {
        changeUserNameConfirmationLabel.innerHTML = errorMessage;
        changeUserNameConfirmationLabel.style.display = 'inline';
        changeUserNameConfirmationLabel.style.color = 'Red';
        return false;
    }

}

function trim(s) {
    while (s.substring(0, 1) == ' ') {
        s = s.substring(1, s.length);
    }
    while (s.substring(s.length - 1, s.length) == ' ') {
        s = s.substring(0, s.length - 1);
    }
    return s;
} 
