function toggleLoginEmail() {
    if(document.frmLogin.emailAddress.value == "Email Address") {
        document.getElementById("emailAddress").className='inteproLoginText'; 
        document.frmLogin.emailAddress.value='';
    }
}

function toggleLoginPassword() {
    document.getElementById("passwordText").style.display = "none";
    document.getElementById("password").style.display = "";
    document.frmLogin.password.focus();
}

function validateEmailAddress(email) {
    // Provides fairly simple email address validation.
    // Assumes addresses will be of the simple form bob@bob.com
    // First, validate that the email is not null
    var errorMessage = "A valid email address is required";
    var validCharList = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
    for(i=0; i < email.length; i++) {
        if(validCharList.indexOf(email.charAt(i).toLowerCase()) == -1) {
            throw errorMessage;
        }
    }
    
    
    // Next validate that we have an @ symbol and a dot and the dot for the domain is after the @
    // Also, the email address cannot start with an @
    if(email.indexOf("@") <= 0) {
        throw errorMessage;
    }
    
    if(email.indexOf(".") <= 0) {
        throw errorMessage;
    }
    
    if(email.lastIndexOf(".") < email.indexOf("@")) {
        throw errorMessage;
    }
    
    // Validate that there is only one @ symbol
    if(email.indexOf("@") != email.lastIndexOf("@")) {
        throw errorMessage;
    }
    
    // A dot cannot immediately follow the @ symbol
    if(email.lastIndexOf(".") == (email.indexOf("@")+1)) {
        throw errorMessage;
    }
    
    // Now check to make sure that the dot and the @ are not the last characters
    if(email.lastIndexOf(".") == (email.length-1)) {
        throw errorMessage;
    }
    
    if(email.lastIndexOf("@") == (email.length-1)) {
        throw errorMessage;
    }
    
    // Now validate that there are not 2 consecutive dots
    if(email.indexOf("..") != -1) {
        throw errorMessage;
    }
}

function validatePasswords(password1, password2) {
    var requiredChars = "0123456789!@#$%^&*()-+={}[]|~`?<>,.";
    var hasRequiredChar = false;
    for(i=0; i < password1.length; i++) {
        if(requiredChars.indexOf(password1.charAt(i)) >= 0) {
            hasRequiredChar = true;
            break;
        }
    }
    
    if(password1.indexOf(" ") >= 0) {
        throw "Password may not contain a space";
        
    } else if(password1.length < 6) {
        throw "Password must contain at least 6 characters";
    
    } else if(!hasRequiredChar) {
        throw "Password must contain at least one number or special character"; 
    
    } else if(password1 != password2) {
        throw "Passwords do not match";
        
    }
    
}

function validateCreateAccountForm() {
    var step = "emailAddress";
    try {
        validateEmailAddress(document.frmCreateAccount.emailAddress.value);
        step = "password1";
        validatePasswords(document.frmCreateAccount.password1.value, document.frmCreateAccount.password2.value);
        step = "securityImageText";
        if(document.frmCreateAccount.securityImageText.value.length == 0) {
            throw "You must enter the security image text exactly as it appears";
        }
        step = "termsAgree";
        if(!document.frmCreateAccount.termsAgree.checked) {
            throw "You must agree to the terms";
        }
        document.frmCreateAccount.submit();
        
    } catch (e) {
        alert(e);
        document.frmCreateAccount.elements[step].focus();
    }
}

function validateUserInfoForm() {
    var step = "emailAddress";
    try {
        validateEmailAddress(document.frmInfo.customerUserEmailAddress.value);
        step = "password1";
        if(document.frmInfo.customerUserPassword1.value.length > 0) {
            validatePasswords(document.frmInfo.customerUserPassword1.value, document.frmInfo.customerUserPassword2.value);
        }
        document.frmInfo.submit();
        
    } catch (e) {
        alert(e);
        document.frmInfo.elements[step].focus();
    }
}

function downloadAuditor() {
    alert(frames.downloadWindow.document.location);
}

function showScreenshot(url, windowName) {
    var screenshotWindow = window.open(url, windowName, 'width=730,height=470');
    screenshotWindow.focus();
}
