function checkEmail(value) {
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(value)){
        return false;
    } else {
        return true;
    }
}

function checkValidDate(dateStr) {
    // dateStr must be of format month day year with either slashes
    // or dashes separating the parts. Some minor changes would have
    // to be made to use day month year or another format.
    // This function returns True if the date is valid.
    var slash1 = dateStr.indexOf("/");
    if (slash1 == -1) { slash1 = dateStr.indexOf("-"); }
    // if no slashes or dashes, invalid date
    if (slash1 == -1) { return false; }
    var dateMonth = dateStr.substring(0, slash1)
    var dateMonthAndYear = dateStr.substring(slash1+1, dateStr.length);
    var slash2 = dateMonthAndYear.indexOf("/");
    if (slash2 == -1) { slash2 = dateMonthAndYear.indexOf("-"); }
    // if not a second slash or dash, invalid date
    if (slash2 == -1) { return false; }
    var dateDay = dateMonthAndYear.substring(0, slash2);
    var dateYear = dateMonthAndYear.substring(slash2+1, dateMonthAndYear.length);
    if ( (dateMonth == "") || (dateDay == "") || (dateYear == "") ) { return false; }
    // if any non-digits in the month, invalid date
    for (var x=0; x < dateMonth.length; x++) {
        var digit = dateMonth.substring(x, x+1);
        if ((digit < "0") || (digit > "9")) { return false; }
    }
    // convert the text month to a number
    var numMonth = 0;
    for (var x=0; x < dateMonth.length; x++) {
        digit = dateMonth.substring(x, x+1);
        numMonth *= 10;
        numMonth += parseInt(digit);
    }
    if ((numMonth <= 0) || (numMonth > 12)) { return false; }
    // if any non-digits in the day, invalid date
    for (var x=0; x < dateDay.length; x++) {
        digit = dateDay.substring(x, x+1);
        if ((digit < "0") || (digit > "9")) { return false; }
    }
    // convert the text day to a number
    var numDay = 0;
    for (var x=0; x < dateDay.length; x++) {
        digit = dateDay.substring(x, x+1);
        numDay *= 10;
        numDay += parseInt(digit);
    }
    if ((numDay <= 0) || (numDay > 31)) { return false; }
    // February can't be greater than 29 (leap year calculation comes later)
    if ((numMonth == 2) && (numDay > 29)) { return false; }
    // check for months with only 30 days
    if ((numMonth == 4) || (numMonth == 6) || (numMonth == 9) || (numMonth == 11)) { 
        if (numDay > 30) { return false; } 
    }
    // if any non-digits in the year, invalid date
    for (var x=0; x < dateYear.length; x++) {
        digit = dateYear.substring(x, x+1);
        if ((digit < "0") || (digit > "9")) { return false; }
    }
    // convert the text year to a number
    var numYear = 0;
    for (var x=0; x < dateYear.length; x++) {
        digit = dateYear.substring(x, x+1);
        numYear *= 10;
        numYear += parseInt(digit);
    }
    // Year must be a 2-digit year or a 4-digit year
    if ( (dateYear.length != 2) && (dateYear.length != 4) ) { return false; }
    // if 2-digit year, use 50 as a pivot date
    if ( (numYear < 50) && (dateYear.length == 2) ) { numYear += 2000; }
    if ( (numYear < 100) && (dateYear.length == 2) ) { numYear += 1900; }
    if ((numYear <= 0) || (numYear > 9999)) { return false; }
    // check for leap year if the month and day is Feb 29
    if ((numMonth == 2) && (numDay == 29)) {
        var div4 = numYear % 4;
        var div100 = numYear % 100;
        var div400 = numYear % 400;
        // if not divisible by 4, then not a leap year so Feb 29 is invalid
        if (div4 != 0) { return false; }
        // at this point, year is divisible by 4. So if year is divisible by
        // 100 and not 400, then it's not a leap year so Feb 29 is invalid
        if ((div100 == 0) && (div400 != 0)) { return false; }
    }
    // date is valid
    return true;
}

function deleteAccount() {
    if (confirm("Are you sure you want to delete this record?")) {      
        document.myForm.submit();
    } else {
        return false;
    }
}

function deleteEvent() {
    if (confirm("Are you sure you want to delete this event?")) {      
        document.myForm.submit();
    } else {
        return false;
    }
}

function openWin() {
    var myWin = open("", "window","width=450,height=400,status=no,toolbar=no,menubar=no,scrollbars=yes");
    myWin.focus();
}

function submitAccount(submit) {
    if (submit == "EDIT") {
        document.myForm.action = "account.asp";
    } else if (submit == "DELETE") {
        document.myForm.action = "dAccount.asp";
    }
    document.myForm.submit();
}

function validateAccount(type) {
    var username = document.myForm.username.value;
    var password1 = document.myForm.password1.value;
    var password2 = document.myForm.password2.value;
    document.myForm.action = "aAccount.asp";

    if (type.toUpperCase() == "EDIT") {
        if (username == "" || username == null || username.length < 6 || username.length > 15) {
            alert ("Must specify Username 6-15 alphanumeric characters in length");
            document.myForm.username.focus();
        } else if (password1 == password2) {
            if (password1.length != 0 && (password1.length < 6 || password1.length > 12)) {
                alert ("Must specify Password 6-12 alphanumeric characters in length");
                document.myForm.password1.value = "";
                document.myForm.password2.value = "";
                document.myForm.password1.focus();
            } else {
                document.myForm.submit();
            }
        } else if (password1 != password2) {
            alert ("Passwords must match");
            document.myForm.password1.value = "";
            document.myForm.password2.value = "";
            document.myForm.password1.focus();
        } else {
            document.myForm.submit();
        }
    } else if (type.toUpperCase() == "ADD") {
        if (username == "" || username == null || username.length < 6 || username.length > 15) {
            alert ("Must specify Username 6-15 alphanumeric characters in length");
            document.myForm.username.focus();
        } else if (password1 == "" || password1 == null || password1.length < 6 || password1.length > 12) {
            alert ("Must specify Password 6-12 alphanumeric characters in length");
            document.myForm.password1.value = "";
            document.myForm.password2.value = "";
            document.myForm.password1.focus();
        } else if (password1 != password2) {
            alert ("Passwords must match");
            document.myForm.password1.value = "";
            document.myForm.password2.value = "";
            document.myForm.password1.focus();
        } else {
            document.myForm.submit();
        }
    }
}

function validateEvent() {
    var contactEmail = document.myForm.contactEmail.value;
    var categoryID = document.myForm.categoryID.value;
    var title = document.myForm.title.value;
    var eventDesc = document.myForm.eventDesc.value;
    var numDays = document.myForm.numDays.value;
    var valid = true;    
   
    for (var i = 0; i<document.myForm.elements.length; i++) {
        if ((document.myForm.elements[i].name.indexOf('eventDate') > -1)) {
            if (valid == true && !checkValidDate(document.myForm.elements[i].value)) {
                valid = false;
                alert ("Please enter a valid date in mm/dd/yyyy format");
                document.myForm.elements[i].focus();
            }
        }
    }
    
    if (valid == true) {
        if (title == "" || title == null) {
            valid = false;
            alert("Please enter a title");
            document.myForm.title.focus();
        } else if (eventDesc == "" || eventDesc == null) {
            valid = false;
            alert("Please enter a description");
            document.myForm.eventDesc.focus();
        } else if (categoryID == "NONE") {
            valid = false;
            alert("Please select a category");
            document.myForm.categoryID.focus();
        } else if (contactEmail != "" && contactEmail != null) {
            if (checkEmail(contactEmail)) {
                valid = false;
                alert ("Email must be in correct form. Ex: username@domain.com");
                document.myForm.contactEmail.focus();
            }
        }
    }
    
    if (valid == true) {
        document.myForm.submit();   
    }
}

function validateNewEvent() {
    var numDays = document.myForm.numDays.value;
    var presenters = document.myForm.presenters.value;
    
    if (numDays == "" || numDays == null || numDays < 1 || numDays > 99) {
        alert ("Please enter a numeric value from 1-99 for the number of days this event will run");
        document.myForm.numDays.focus();
    } else if (presenters == "" || presenters == null || presenters < 0 || presenters > 99) {
        alert ("Please enter a numeric value from 0-99 for the number of presenters this event will have");
        document.myForm.presenters.focus();
    } else {
        document.myForm.submit();
    }
}