function validateLogin() {
    var login = document.getElementById("login").value;
    var password = document.getElementById("password").value;

    if (login == "" && password == "") {
        window.alert("Please enter login and password");
        return false
    } else if (login == "") {
        window.alert("Please enter login");
        return false
    } else if (password == "") {
        window.alert("Please enter password");
        return false
    } else {
        return true;
    }
}

function validateUpload() {
    var file = document.getElementById("file").value;
    var year = document.getElementById("year").value;

    if (file == "") {
        window.alert("Please browse for a file");
        return false;
    } else if (year.length != 4 || !isNumeric(year)) {
        window.alert("Please enter correct four-digit year number");
        return false;
    } else if (file.lastIndexOf(".csv") != file.length - 4) {
        window.alert("You have chosen file which extension is not '.csv'");
        return false;
    } else if (year != new Date().getFullYear()) {
        return window.confirm("Entered year number is not equal to current year number.\n" +
                              "Are you sure you want to upload data for " + year + "?");
    } else {
        return true;
    }
}

function validateCustomizedReport() {
    var church = document.getElementById("church").value;
    var city = document.getElementById("city").value;
    var description = document.getElementById("description").value;
    var attendance = document.getElementById("attendance").value;
    var membership = document.getElementById("membership").value;
    var budget = document.getElementById("budget").value;

    if (church == "" || city == "" || description == "" ||
            attendance == "" || membership == "" || budget == "") {
        window.alert("Please fill in all text fields");
        return false;
    } else if (!isNumeric(attendance) || !isNumeric(membership) || !isNumeric(budget)) {
        window.alert("Invalid entry. Please specify with a number.");
        return false;
    } else {
		var yesNo = confirm("Most reports take 30-60 seconds to run. The Summary report may take up to 2 minutes. Do you still want to run the report?");
		
		if(yesNo){
			return true;
		}else{
			return false;
		}
    }
}

function validateCustomizedReportStep2(changed) {
    if (changed) {
        window.alert("Ranges were changed. Please click Recalculate button first.");
        return false;
    }

    var attendanceLow = document.getElementById('attendanceLow').value;
    var attendanceHigh = document.getElementById('attendanceHigh').value;
    var membershipLow = document.getElementById('membershipLow').value;
    var membershipHigh = document.getElementById('membershipHigh').value;
    var budgetLow = document.getElementById('budgetLow').value;
    var budgetHigh = document.getElementById('budgetHigh').value;

    var message = "The Range entered is invalid. Please enter a Range where the" +
                  "number on the left is equal to or less than the number on the right.";

    if (attendanceLow == "" || attendanceHigh == "" ||
            membershipLow == "" || membershipHigh == "" ||
            budgetLow == "" || budgetHigh == "") {
        window.alert(message);
        return false;
    }

    if (!isNumeric(attendanceLow) || !isNumeric(attendanceHigh) ||
            !isNumeric(membershipLow) || !isNumeric(membershipHigh) ||
            !isNumeric(budgetLow) || !isNumeric(budgetHigh)) {
        window.alert(message);
        return false;
    }

    if (parseInt(attendanceLow) > parseInt(attendanceHigh) ||
            parseInt(membershipLow) > parseInt(membershipHigh) ||
            parseInt(budgetLow) > parseInt(budgetHigh)) {
        window.alert(message);
        return false;
    }

    return true;
}

function validateNPSummaryReport() {
    var attendanceLow = document.getElementById("attendanceLow").value;
    var attendanceHigh = document.getElementById("attendanceHigh").value;
    var budgetLow = document.getElementById("budgetLow").value;
    var budgetHigh = document.getElementById("budgetHigh").value;

    var message = "The Range entered is invalid. Please enter a Range where the" +
                  "number on the left is equal to or less than the number on the right.";

    if (attendanceLow == "" || attendanceHigh == "" ||
            budgetLow == "" || budgetHigh == "") {
        window.alert(message);
        return false;
    }

    if (!isNumeric(attendanceLow) || !isNumeric(attendanceHigh) ||
            !isNumeric(budgetLow) || !isNumeric(budgetHigh)) {
        window.alert(message);
        return false;
    }

    if (parseInt(attendanceLow) > parseInt(attendanceHigh) ||
            parseInt(budgetLow) > parseInt(budgetHigh)) {
        window.alert(message);
        return false;
    }

    return true;
}

function isNumeric(string)
{
	if (string.length == 0) {
        return false;
    }
	for (var i = 0; i < string.length; i++) {
		if (!(string.charAt(i) >= '0' && string.charAt(i) <= '9')) {
            return false;
        }
	}
	return true;
}
