
var oktohide;
var currentmenu;

function isValidEmail(str) {
    var at="@";
    var dot=".";
    var lat=str.indexOf(at);
    var lstr=str.length;
    var ldot=str.indexOf(dot);
    var msg = "The email address entered is not valid!";
    
    if (str.indexOf(at)==-1){
       alert(msg);
       return false;
    }
    
    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
       alert(msg);
       return false;
    }

    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
        alert(msg);
        return false;
    }

     if (str.indexOf(at,(lat+1))!=-1){
        alert(msg);
        return false;
     }

     if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
        alert(msg);
        return false;
     }

     if (str.indexOf(dot,(lat+2))==-1){
        alert(msg);
        return false;
     }
	
     if (str.indexOf(" ")!=-1){
        alert(msg);
        return false;
     }

     return true;				
}

function showImage(strImagePath){
    picWindow = window.open("photo.php?photo=" + strImagePath , 'picWindow' , "toolbar=no, directories=no, location=no, status=no, menubar=no, resizable=yes, scrollbars=yes, width=600, height=400");
    
    if (window.focus){ picWindow.focus()}
}

function showVideo(strVideoId){
    videoWindow = window.open("videoviewer.aspx?vid=" + strVideoId , 'vidWindow' , "toolbar=no, directories=no, location=no, status=no, menubar=no, resizable=no, scrollbars=no, width=600, height=410");
    
    if (window.focus){ videoWindow.focus()}
}

function showsubmenu(lnkName){
    var lnk = document.getElementById("lnk" + lnkName);
    var submenu = document.getElementById('submenu'+lnkName);
    
    //If not current submenu then show it
    if (submenu != currentmenu || currentmenu==undefined){
        
        //If there is a current menu displayed then hide it before showing new menu
        if (currentmenu != undefined) {
            clearTimeout;
            currentmenu.style.display="none";
        }                
        
        var pos = findPos(lnk);
        
        submenu.style.left = (parseInt(pos[0]) - 2) + 'px';
        submenu.style.top = (parseInt(pos[1]) + 22) + 'px';
        submenu.style.display = "block";
        
        //Set current menu to the one we are opening 
        currentmenu = submenu;
    } 
    
    //Clear hide
    oktohide = 0;
}

function hidesubmenu(submenu){
    oktohide = 1;
    setTimeout("dohide('"+submenu+"')", 750);
}

function hideallsubmenus(){
    document.getElementById('submenuClubs').style.display="none";
    document.getElementById('submenuResults').style.display="none";
    document.getElementById('submenuSunbowl').style.display="none";
    document.getElementById('submenuState').style.display="none";
    document.getElementById('submenuSales').style.display="none";
}

function dohide(submenu){
    if (oktohide){
        //document.getElementById('submenu'+submenu).style.display="none";
        hideallsubmenus();
        currentmenu=undefined;
    }
}

function findPos(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        curleft = obj.offsetLeft
        curtop = obj.offsetTop
        while (obj = obj.offsetParent) {
	        curleft += obj.offsetLeft
	        curtop += obj.offsetTop
        }
    }
    return [curleft,curtop];
}        

function doLogin(){
    var username = document.getElementById('username').value;
    var password = document.getElementById('password').value;
    
    if (username == '' || password == ''){
        alert("Please ensure you enter a username and password.");
        return false;
    }
    
    return true;
}

function changePassword(){
    var current_password = document.getElementById("current_password").value;
    var new_password = document.getElementById("new_password").value;
    var confirm_password = document.getElementById("confirm_password").value;
    
    if (current_password == ""){
        alert("You must enter the current password before proceeding.");
        return false;
    }
    
    if (new_password == ""){
        alert("You must enter a new password before proceeding.");
        return false;
    }    
    
    if (new_password != confirm_password){
        alert("The 'New Password' field must match the 'Confirm New Password' field before proceeding.");
        return false;
    }    
    
    return true;
}

function confirmSendNotice(){
    var notice = document.getElementById('notice').value;
    
    if (notice.length == 0){
        alert("Please enter a notice to send out.");
        return false;
    }
    
    // check for too long notice
    if (notice.length > 3000){
        alert("Please ensure the notice is no longer than 3,000 characters.\n\nIt is currently " + notice.length + "characters.");
        return false;
    }
    
    // confirm send
    if (confirm("Are you sure you wish to send this notice?\n\nThis will email every user for the currently selected list.")){
        return true;
    } else {
        return false;
    }
}

function confirmDeleteResource(file_name){
    if (confirm("Are you sure you wish to delete this resource?")){
        window.location = "resources.php?did=" + file_name;
        return;
    } 
}

function removeSubscriber(id){
    if (confirm("Are you sure you wish to remove this email address?\n\nOnce it has been removed it cannot be recovered.")){
        document.getElementById('remove').value = id;
        document.getElementById('frmMain').submit();
    }
    return;
}