//javascript: mystep('next');mystep('next');

function mystep(direction) {
    var currentStep = curStep;
    var validationerror;

    if (direction == 'next' && (currentStep + 1) <= maxStep) {
        currentStep = currentStep + 1;
    }
    else if (direction == 'prev' && (currentStep - 1) >= minStep) {
        currentStep = currentStep - 1;
    }

    // enable/disable divs and change background
    changeView(currentStep);
}

function changeView(step) {
    curStep = step;
    for (var i = 1; i < (maxStep + 1); i++) {
        var stepDiv = document.getElementById("step" + i);
        var stepLabelDiv = document.getElementById("step" + i + "_label");

        if (stepDiv != null && stepLabelDiv != null) {
            // if on or before the current step
            if (i <= step) {
                stepLabelDiv.style.background = "#CCCCCC";
                stepLabelDiv.style.fontWeight = "bold";

                // if on current step
                if (i == step) {
                    stepDiv.style.display = "block";
                }
                else {
                    stepDiv.style.display = "none";
                }
            }
            else {
                stepDiv.style.display = "none";
                stepLabelDiv.style.background = "#FFFFFF";
                stepLabelDiv.style.fontWeight = "normal";
            }
        }
    }
}


function checkAccountInfo(obj, focusCaptcha) {
    var captcha = document.getElementById("captcha");
    var accepttos = document.getElementById("accepttos");
    var accepttosText = document.getElementById("accepttosText");

    var buttonID = "createButton";

    if (captcha != null &&
        accepttos != null &&
        accepttosText != null) {
        makeNormal(captcha);
        makeNormal(accepttos);
        makeNormal(accepttosText);

        captcha.value = stripSpacesChars(captcha.value);
        captcha.value = captcha.value.toUpperCase();
        if (captcha.value.length > 4) {
            if (accepttos.checked == true) {
                enableButton(buttonID);
                return focusCaptcha;
            }
            else {
                highlight(accepttosText);
                disableButton(buttonID);
            }
        }
        else {
            if (focusCaptcha) {
                captcha.focus();
            }
            highlight(captcha);
            disableButton(buttonID);
        }
    }
    return false;
}

function goToCreate(event) {
    var pK = event.keyCode;
    if (pK == 13 || pK == 39) {
        var buttonID = "usernameInfoNext";
        var button = document.getElementById(buttonID);

        if (!button.disabled) {
            var registration_form = document.getElementById("registration-form");

            if (registration_form != null) {
                registration_form.submit();
            }
        }
    }

    goBackToStepThree(event);
}

function goBackToStepThree(event) {
    var pK = event.keyCode;
}

function editPassword() {
    var pass = document.getElementById("pass");
    var password = document.getElementById("password");
    var passwordverify = document.getElementById("passwordverify");

    if (pass != null &&
        password != null &&
        passwordverify != null) {
        if (password.value != "No PASS here :P" ||
            passwordverify.value != "No PASS here :P") {
            password.value = "";
            passwordverify.value = "";
            pass.value = "";
            password.onkeyup = checkUsernameInfo;
            passwordverify.onkeyup = checkUsernameInfo;
            checkUsernameInfo();
        }
    }
}

var usernameError = new Array();

// Step 3
function checkUsernameInfo(obj, focusUsername) {
    document.onkeypress = goBackToStepTwo;

    var username = document.getElementById("username");
    var pass = document.getElementById("pass");
    var password = document.getElementById("password");
    var passwordverify = document.getElementById("passwordverify");
    var passquestion = document.getElementById("passquestion");
    var passanswer = document.getElementById("passanswer");

    var buttonID = "usernameInfoNext";

    if (username != null &&
        password != null &&
        passwordverify != null &&
        passquestion != null &&
        passanswer != null) {
        makeNormal(username, "usernameError");
        makeNormal(password);
        makeNormal(passwordverify);
        makeNormal(passquestion);
        makeNormal(passanswer);

        if (pass.value.length > 0) {
            password.value = "No PASS here :P";
            passwordverify.value = "No PASS here :P";
            password.onkeyup = editPassword;
            passwordverify.onkeyup = editPassword;
        }

        username.value = stripSpacesChars(username.value);
        if (username.value.length > 3) {
            if (usernameError[username.value] == null) {
                if (pass.value.length > 0 || password.value.length > 5) {
                    if (pass.value.length > 0 || password.value == passwordverify.value) {
                        if (passquestion.options[passquestion.selectedIndex].value.length > 1) {
                            if (passanswer.value.length > 3) {

                                enableButton(buttonID);
                                document.onkeypress = goToStepFour;
                                return focusUsername;
                            }
                            else {
                                highlight(passanswer);
                                disableButton(buttonID);
                            }
                        }
                        else {
                            highlight(passquestion);
                            disableButton(buttonID);
                        }
                    }
                    else {
                        highlight(passwordverify);
                        disableButton(buttonID);
                    }
                }
                else {
                    highlight(password);
                    disableButton(buttonID);
                }
            }
            else {
                if (focusUsername) {
                    username.focus();
                }
                makeError(username, "usernameError", usernameError[username.value]);
                disableButton(buttonID);
            }
        }
        else {
            if (focusUsername) {
                username.focus();
            }
            highlight(username);
            disableButton(buttonID);
        }
    }

    return false;
}

function goToStepFour(event) {
    var pK = event.keyCode;
    if (pK == 13 || pK == 39) {
        var buttonID = "usernameInfoNext";
        var button = document.getElementById(buttonID);

        if (!button.disabled) {
            mystep('next');
            checkAccountInfo(null, true);
        }
    }

    goBackToStepTwo(event);
}

function goBackToStepTwo(event) {
    var pK = event.keyCode;
}


// Step 2
function checkBillingInfo(obj, focusBilladdr1) {
    document.onkeypress = goBackToStepOne;

    var billaddr1 = document.getElementById("billaddr1");
    var billcity = document.getElementById("billcity");
    var billstate = document.getElementById("billstate");
    var billpostalcode = document.getElementById("billpostalcode");

    var buttonID = "billingInfoNext";

    if (billaddr1 != null &&
        billcity != null &&
        billstate != null &&
        billpostalcode != null) {
        makeNormal(billaddr1);
        makeNormal(billcity);
        makeNormal(billstate);
        makeNormal(billpostalcode);

        if (billaddr1.value.length > 5) {
            if (billcity.value.length >= 3) {
                if (billstate.options[billstate.selectedIndex].value.length > 1) {
                    //billpostalcode.value = stripAlphaChars(billpostalcode.value);
                    if (billpostalcode.value.length >= 5) {
                        enableButton(buttonID);
                        document.onkeypress = goToStepThree;
                        return focusBilladdr1;
                    }
                    else {
                        highlight(billpostalcode);
                        disableButton(buttonID);
                    }
                }
                else {
                    highlight(billstate);
                    disableButton(buttonID);
                }
            }
            else {
                highlight(billcity);
                disableButton(buttonID);
            }
        }
        else {
            if (focusBilladdr1) {
                billaddr1.focus();
            }
            highlight(billaddr1);
            disableButton(buttonID);
        }
    }

    return false;
}

function goToStepThree(event) {
    var pK = event.keyCode;
    if (pK == 13 || pK == 39) {
        var buttonID = "billingInfoNext";
        var button = document.getElementById(buttonID);

        if (!button.disabled) {
            mystep('next');
            checkUsernameInfo(null, true);
        }
    }

    goBackToStepOne(event);
}

function goBackToStepOne(event) {
    var pK = event.keyCode;
}

var emailError = new Array();

// Step 1
function checkContactInfo(obj) {
    document.onkeypress = null;

    var firstname = document.getElementById("firstname");
    var lastname = document.getElementById("lastname");
    var phone1 = document.getElementById("phone1");
    var phone2 = document.getElementById("phone2");
    var phone3 = document.getElementById("phone3");
    var industry = document.getElementById("industry");
    var email = document.getElementById("email");
    var emailverify = document.getElementById("emailverify");

    var buttonID = "contactInfoNext";

    if (firstname != null &&
        lastname != null &&
        phone1 != null &&
        phone2 != null &&
        phone3 != null &&
        industry != null) {
        makeNormal(firstname);
        makeNormal(lastname);
        makeNormal(phone1);
        makeNormal(phone2);
        makeNormal(phone3);
        makeNormal(industry);
        makeNormal(email, "emailError");
        makeNormal(emailverify);

        if (firstname.value.length > 0) {
            if (lastname.value.length > 0) {
                phone1.value = stripAlphaChars(phone1.value);
                if (phone1.value.length == 3) {
                    phone2.value = stripAlphaChars(phone2.value);
                    if (phone2.value.length == 3) {
                        phone3.value = stripAlphaChars(phone3.value);
                        if (phone3.value.length == 4) {
                            validRegExp = "/^[^@]+@[^@]+.[a-z]{2,}$/i";

                            if (email.value.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) {
                                if (emailError[email.value] == null) {
                                    if (emailverify.value == email.value) {
                                        if (industry.options[industry.selectedIndex].value.length > 1) {
                                            enableButton(buttonID);
                                            if (obj == null) {
                                                firstname.focus();
                                            }
                                            document.onkeypress = goToStepTwo;
                                            return true;
                                        }
                                        else {
                                            highlight(industry);
                                            disableButton(buttonID);
                                        }
                                    }
                                    else {
                                        highlight(emailverify);
                                        disableButton(buttonID);
                                    }
                                }
                                else {
                                    makeError(email, "emailError", emailError[email.value]);
                                    disableButton(buttonID);
                                    email.focus();
                                }
                            }
                            else {
                                if (obj == phone3) {
                                    email.focus();
                                }
                                highlight(email);
                                disableButton(buttonID);
                            }
                        }
                        else {
                            if (obj == phone2) {
                                phone3.focus();
                            }
                            highlight(phone3);
                            disableButton(buttonID);
                        }
                    }
                    else {
                        if (obj == phone1) {
                            try {
                                phone2.focus();
                            }
                            catch (err) {
                            }
                        }
                        highlight(phone2);
                        disableButton(buttonID);
                    }
                }
                else {
                    highlight(phone1);
                    disableButton(buttonID);
                }
            }
            else {
                highlight(lastname);
                disableButton(buttonID);
            }

        }
        else {
            if (obj == null) {
                firstname.focus();
            }
            highlight(firstname);
            disableButton(buttonID);
        }
    }

    return false;
}

function goToStepTwo(event) {
    var pK = event.keyCode;
    if (pK == 13 || pK == 39) {
        var buttonID = "contactInfoNext";
        var button = document.getElementById(buttonID);

        if (!button.disabled) {
            mystep('next');
            checkBillingInfo(null, true);
        }
    }
}


function highlight(obj) {
    if (obj.className == "phone" || obj.className == "phoneSelected") {
        obj.className = "phoneSelected";
    }
    else if (obj.className == "checkbox_register" || obj.className == "checkbox_register_Selected") {
        obj.className = "checkbox_register_Selected";
    }
    else {
        obj.className = "registerSelect";
    }
}

function makeNormal(obj, errorDivID) {
    if (obj.className == "phoneSelected" || obj.className == "phone") {
        obj.className = "phone";
    }
    else if (obj.className == "checkbox_register" || obj.className == "checkbox_register_Selected") {
        obj.className = "checkbox_register";
    }
    else {
        obj.className = "register";
    }

    if (errorDivID != null) {
        var errorDiv = document.getElementById(errorDivID);

        if (errorDiv != null) {
            errorDiv.innerHTML = "";
        }
    }
}

function makeError(obj, errorDivID, errorString) {
    obj.className = "registerError";

    if (errorDivID != null) {
        var errorDiv = document.getElementById(errorDivID);

        if (errorDiv != null) {
            errorDiv.innerHTML = errorString;
        }
    }
}

function disableButton(id) {
    changeButtonState(id, true);
}

function enableButton(id) {
    changeButtonState(id, false);
}

function changeButtonState(id, disabled) {
    var button = document.getElementById(id);

    if (button != null) {
        button.disabled = disabled;
    }
}

function stripSpacesChars(pstrSource) {
    var m_strOut = new String(pstrSource);
    m_strOut = m_strOut.replace(' ', '');
    m_strOut = m_strOut.replace('@', '');
    m_strOut = m_strOut.replace(',', '');
    m_strOut = m_strOut.replace('_', '');
    m_strOut = m_strOut.replace('.', '');
    m_strOut = m_strOut.replace("'", '');
    m_strOut = m_strOut.replace('"', '');
    m_strOut = m_strOut.replace('!', '');
    m_strOut = m_strOut.replace('#', '');
    m_strOut = m_strOut.replace('$', '');
    m_strOut = m_strOut.replace('%', '');
    m_strOut = m_strOut.replace('^', '');
    m_strOut = m_strOut.replace('&', '');
    m_strOut = m_strOut.replace('*', '');
    m_strOut = m_strOut.replace('(', '');
    m_strOut = m_strOut.replace(')', '');
    m_strOut = m_strOut.replace('+', '');
    m_strOut = m_strOut.replace('-', '');

    return m_strOut;
}

function stripAlphaChars(pstrSource) {
    var m_strOut = new String(pstrSource);
    m_strOut = m_strOut.replace(/[^0-9]/g, '');

    return m_strOut;
}
