function checkEmail(em) {
    var atpos = em.indexOf('@');
    if (atpos == -1) {
        return false;
    }
    if (atpos == 0) {
        return false;
    }
    if (atpos == (em.length - 1)) {
        return false;
    }
    
    var atpos2 = em.indexOf('@', atpos+1);
    if (atpos2 != -1) {
        return false;
    }
    
    var lastDot = em.lastIndexOf('.');
    if (lastDot == -1) {
        return false;
    }
    if (lastDot < atpos + 2) {
        return false;
    }
    if (lastDot > em.length - 3) {
        return false;
    }
    
    return true;
}

function checkEmails(f, elname) {
    // alert('Checking emails for elname ' + elname);
    var el = f.elements(elname);
    var el2 = f.elements(elname + "2");
    if (el2 != null) {
        if (el2.value != el.value) {
            alert('Please ensure the email address matches in both input fields');
            return false;
        }
    }
    var ce = checkEmail(el.value);
    if (ce) {
        return ce;
    }
    alert('Please try again with a valid email address - you may have typed it wrongly');
    return false;
}

function hideShowInput(elementId , style){
    document.getElementById(elementId).style['display']=style;
}

function toggleDisplay(elementId){
    if(document.getElementById(elementId).style.display == "none" ){
        hideShowInput(elementId,"block");
    }
    else {
        hideShowInput(elementId,"none");
    }
    
}

function checkLogin(){
    
    if (document.getElementById("username").value ==""){
        alert('Please enter email address');
        return;
    }
    if (document.getElementById("password").value ==""){
        alert('Please enter password');
        return;
    }
    document.forms["vmForm"].submit();
    
}

function forgotPassword(){
    if (document.getElementById("username").value ==""){
        alert('Please enter email address');
        return;
    }
    window.location="../handlers/remindPassword.jsp?username="+document.getElementById("username").value;
}

function enableSubmit(element,buttonId){
    if(!element){
        return;
    }
    if(element.checked){
        document.getElementById(buttonId).disabled=false;
    }
    else {
        document.getElementById(buttonId).disabled=true;
    }
}

 function checkSelected(elementName) {
        var elements = document.getElementsByName(elementName);
        var unlockButton = false;
        for (i=0; i<elements.length; i++) {
            if(elements[i].checked == true) {
                unlockButton = true;
                break;
            }
        }
        if (unlockButton) {
            document.forms.vmForm.vmFormSubmitButton.disabled = false;
        } else {
            document.forms.vmForm.vmFormSubmitButton.disabled = true;
        }
    }
 function popup(url,name,width, height) {

        if (width == null) {
            width = 650;
        }
        if (height == null) {
            height = 500;
        }
        if(name==null){
            name="popup";
        }
        var win = window.open(url,name, "toolbar=no,scrollbars=yes,width=" + width + ",height=" + height + ",location=no,directories=no,status=yes,menubar=no,resizable=yes");
        return win;

  }