function validate() {
    if(validateFields() == true) {
        document.getElementById("costcalcform").submit();
        return true;
    }
    return false;
}
function validateFields() {
    var MAX = 2500000;
    var value;
    var sale_price = 0.0;
    var purchase_price = 0.0;
    var remortgage_price = 0.0;
	
    var range_message = "You have entered a value above 2,500,000\nPlease call our Client Liason Team on 0845 050 0083";
    var invalid_message = "Invalid price entered.";
	
    value = document.getElementById("sale_price").value;
    if( value != "" ) {
        if( !isNaN(parseFloat(value)) ) {
            if( value > MAX || value == 0.0 ) {
                alert(range_message);
                return false;
            }
            else {
                sale_price = value;
            }
        }
        else {
            alert(invalid_message);
            sale_price.focus();
            return false;
        }
    }
	
    value = document.getElementById("purchase_price").value;
    if( value != "" ) {
        if( !isNaN(parseFloat(value)) ) {
            if( value > MAX || value == 0.0 ) {
                alert(range_message);
                return false;
            }
            else {
                purchase_price = value;
            }
        }
        else {
            alert(invalid_message);
            purchase_price.focus();
            return false;
        }
    }
	
    value = document.getElementById("remortgage_price").value;
    if( value != "" ) {
        if( !isNaN(parseFloat(value)) ) {
            if( value > MAX || value == 0.0 ) {
                alert(range_message);
                return false;
            }
            else {
                remortgage_price = value;
            }
        }
        else {
            alert(invalid_message);
            remortgage_price.focus();
            return false;
        }
    }
	
    if( sale_price > 0 && purchase_price > 0 && remortgage_price == 0 ) {
        if(document.getElementById("purcftbyes").checked == true){
            alert("You cannot have a related sale if you are a First Time Buyer. Please remove the Sale Value and try again.");
            return false;
        }else{
            return true;
        }
    }
    else if( sale_price > 0 && purchase_price == 0 && remortgage_price == 0 ) {
        return true;
    }
    else if( sale_price == 0 && purchase_price > 0 && remortgage_price == 0 ) {
        return true;
    }
    else if( sale_price == 0 && purchase_price == 0 && remortgage_price > 0 ) {
        return true;
    }
    else {
        alert("Please enter values for either a Sale, Purchase, Sale & Purchase or a Remortgage.");
        return false;
    }
}

function validateftb() {
    var value = document.getElementById("purchase_price").value;

    if( isNaN(parseFloat(value)) ) {
        alert("Invalid Price Entered")
        return false;
    }
    if( value > 2500000) {
        alert("You have entered a value above 2,500,000\nPlease call our New Business Team on 0845 600 2343");
        return false;
    }
    return true;
}

function validateremo() {
    var value = document.getElementById("remortgage_price").value;


    if( isNaN(parseFloat(value)) ) {
        alert("Invalid Price Entered")
        return false;
    }
    if( value > 2500000) {
        alert("You have entered a value above 2,500,000\nPlease call our New Business Team on 0845 600 2343");
        return false;
    }
    return true;
}
