function checkHistoryCVD(checkvalue) {
    var error = "";
    if (checkvalue) {
       error = "The risk score calculator is not valid if you have already had aheart attack, angina, stroke or TIA.\n";
    }
    return error;
}

function checkHistoryDiabetes(checkvalue) {
    var error = "";
    if (checkvalue) {
       error = "The risk score calculator is not valid if you have already have diabetes.\n";
    }
    return error;
}

function isPostcode(val) {
        var pattern = /^([A-Z]{1,2})([0-9R][0-9A-Z]{0,1})([0-9][ABD-HJLNP-UW-Z]{2})$/;
	if (pattern.test(val)) {
        	return true;
	} else {
		return false;
	}
}

function isNumeric(val) {
        var pattern = /^\d+.?\d*$/;
	if (pattern.test(val)) {
        	return true;
	} else {
		return false;
	}
}

function isInteger(val) {
        var pattern = /^\d+$/;
	if (pattern.test(val)) {
        	return true;
	} else {
		return false;
	}
}

function checkCholesterol(theValue) {
    if (theValue=="")
	return "";
    var error="Cholesterol/HDL ratio: either leave blank or enter a number between 1.0 and 12.0.\n";
    if (!isNumeric(theValue)) {
	return error;
    }
    var dbl = parseFloat(theValue);
    if (dbl < 1.0 || dbl > 12.0) {
	return error;
    }
    return "";
}

function checkBmi(theValue) {
    if (theValue=="")
	return "";
    var error="Body mass index: either leave blank or enter a number between 18.0 and 42.0.\n";
    if (!isNumeric(theValue)) {
	return error;
    }
    var dbl = parseFloat(theValue);
    if (dbl < 18.0 || dbl > 42.0) {
	return error;
    }
    return "";
}

function checkSystolicBloodPressure(theValue) {
    if (theValue=="")
	return "";
    var error="Systolic blood pressure: either leave blank or enter an integer between 70 and 210\n";
    if (!isInteger(theValue)) {
	return error;
    }
    var i = parseInt(theValue);
    if (i < 70 || i > 210) {
	return error;
    }
    return "";
}

function checkWeightAndHeight(weight, height) {
    var weightError="Weight: enter an integer between 35 and 255\n";
    var heightError="Height: enter an integer between 120 and 215\n";
    if (weight=="" || height=="") {
	return "";
    }
    if (!isInteger(weight)) {
	return weightError;
    }
    var i = parseInt(weight);
    if (i < 35 || i > 255) {
	return weightError;
    }
    if (!isInteger(height)) {
	return heightError;
    }
    var i = parseInt(height);
    if (i < 120 || i > 215) {
	return heightError;
    }
    return "";
}

function checkAge(theValue) {
    var error="Age: enter an integer between 30 and 84\n";
    if (!isInteger(theValue)) {
	return error;
    }
    var i = parseInt(theValue);
    if (i < 30 || i > 84) {
	return error;
    }
    return "";
}

function checkPostcode(theValue) {
    if (theValue=="")
	return "";
    var error="Postcode: the postcode is not recognised.  Please re-enter or leave blank.\n";
    theValue = theValue.replace(/\s/g, "");
    if (!isPostcode(theValue)) {
	return error;
    }
    return "";
}

function checkCalculatorForm(theForm) {
    theForm.postcode.value=theForm.postcode.value.toUpperCase();
    var why = "";
    why += checkAge(theForm.age.value);
    why += checkPostcode(theForm.postcode.value);
    why += checkWeightAndHeight(theForm.weight.value, theForm.height.value);
    if (why != "") {
       alert(why);
       return false;
    }
    return true;
}

