
function wholeNumber (f) {
	var notdigit = /\D/;
	return f.match(notdigit);
}

function isInteger (f) {
	var digit = /^-?\d+$/;
	return !(f.match(digit));
}

function validForm(f) {

        if (f.alt_min.value) {
                var result = isInteger(f.alt_min.value);
                if (result) {
                        alert("To specify a minimum altitude, you must supply an integer value.");
                        return false;
                }
        }

        if (f.alt_max.value) {
                var result = isInteger(f.alt_max.value);
                if (result) {
                        alert("To specify a maximum altitude, you must supply an integer value.");
                        return false;
                }
        }

        f.lon_min.value = "";
        f.lon_max.value = "";
        f.lat_min.value = "";
        f.lat_max.value = "";

        var deg_sign = unescape("%b0");

	if (f.lon_min_deg.value) {
		f.lon_min.value = f.lon_min_deg.value;
		var result = wholeNumber(f.lon_min.value);
		if (result) {
			alert("To specify the westerly boundary of your search, you must supply a positive integer.");
                        f.lon_min_deg.value = null;
                        return false;
                }
                else if (f.lon_min_sign.value == '' || f.lon_min_sign.value == null) {
			alert("You must specify the westerly boundary of your search as " + f.lon_min.value + deg_sign + " east (E) or " + f.lon_min.value + deg_sign + " west (W).");
                        return false;
                }
                else if (f.lon_min_deg.value > 180) {
                        alert("Your westerly boundary cannot exceed 180" + deg_sign + " " + f.lon_min_sign.value + ".");
                        return false;
                }
                else if (f.lon_min_sign.value == 'W') {
                        f.lon_min.value = (f.lon_min.value * -1);
                }
        }
        if (f.lon_max_deg.value) {
                f.lon_max.value = f.lon_max_deg.value;
                var result = wholeNumber(f.lon_max.value);
                if (result) {
                        alert("To specify the easterly boundary of your search, you must supply a positive integer.");
                        f.lon_max_deg.value = null;
                        return false;
                }
                else if (f.lon_max_sign.value == '' || f.lon_max_sign.value == null) {
                        alert("You must specify the easterly boundary of your search as " + f.lon_max.value + deg_sign + " east (E) or " + f.lon_max.value + deg_sign + " west (W).");
                        return false;
                }
                else if (f.lon_max_deg.value > 180) {
                        alert("Your easterly boundary cannot exceed 180" + deg_sign + " " + f.lon_max_sign.value + ".");
                        return false;
                }
                else if (f.lon_max_sign.value == 'W') {
                        f.lon_max.value = (f.lon_max.value * -1);
                }
        }

        // latitude validation

        if (f.lat_min_deg.value) {
                f.lat_min.value = f.lat_min_deg.value;
                var result = wholeNumber(f.lat_min.value);
                if (result) {
                        alert("To specify the southerly boundary of your search, you must supply a positive integer.");
                        f.lat_min_deg.value = null;
                        return false;
                }
                else if (f.lat_min_sign.value == '' || f.lat_min_sign.value == null) {
                        alert("You must specify the southerly boundary of your search as " + f.lat_min.value + deg_sign + " north (N) or " + f.lat_min.value + deg_sign + " south (S).");
                        return false;
                }
                else if (f.lat_min_deg.value > 90) {
                        alert("Your southerly boundary cannot exceed 90" + deg_sign + " " + f.lat_min_sign.value + ".");
                        return false;
                }
                else if (f.lat_min_sign.value == 'S') {
                        f.lat_min.value = (f.lat_min.value * -1);
                }
        }
        if (f.lat_max_deg.value) {
                f.lat_max.value = f.lat_max_deg.value;
                var result = wholeNumber(f.lat_max.value);
                if (result) {
                        alert("To specify the northerly boundary of your search, you must supply a positive integer.");
                        f.lat_max_deg.value = null;
                        return false;
                }
                else if (f.lat_max_sign.value == '' || f.lat_max_sign.value == null) {
                        alert("You must specify the northerly boundary of your search as " + f.lat_max.value + deg_sign + " north (N) or " + f.lat_max.value + deg_sign + " south (S).");
                        return false;
                }
                else if (f.lat_max_deg.value > 90) {
                        alert("Your northerly boundary cannot exceed 90" + deg_sign + " " + f.lat_max_sign.value + ".");
                        return false;
                }
                else if (f.lat_max_sign.value == 'S') {
                        f.lat_max.value = (f.lat_max.value * -1);
                }
        }

        // check if the query makes sense

        if (parseInt(f.lon_min.value) > parseInt(f.lon_max.value)) {
                alert("Your search is invalid, because its easterly boundary is west of its westerly boundary.");
                return false;
        }

        if (parseInt(f.lat_min.value) > parseInt(f.lat_max.value)) {
                alert("Your search is invalid, because its northerly boundary is south of its southerly boundary.");
                return false;
        }

        return true;
}

