﻿$(document).ready(function() {
    initTargetEnquiry();
});

function initTargetEnquiry() {
    $("#targetPriceEnquiry").submit(function() { return (ValidateTargetPriceEnquiry()); });
}

function ValidateTargetPriceEnquiry() {

    if ($('#makeId :selected').text() == 'Choose a make' || $('#modelVersionId :selected').text() == 'and choose a model' || $('#makeId :selected').text() == 'loading....' || $('#modelVersionId :selected').text() == 'loading....') {
        alert('Please select a make, model and version combination');
        return false;
    }
    if ($('#editionId :selected').text() == 'and choose a version' || $('#editionId :selected').text() == 'loading....') {
        alert('Please select a make, model and version combination');
        return false;
    }

    if ($("#bestDealerPrice").val() == '') {
        alert("You must enter Dearl best price you have found?");
        $("#bestDealerPrice").focus();
        return (false);
    }

    if ($('#bestDealerPrice').length) {
        if ($('#bestDealerPrice').val() != "") {

            var theValue = $('#bestDealerPrice').val();


            if (isNaN(theValue) || theValue < 0 || theValue > 999999) {
                alert("Best price must be a whole number containing no commas or decimal points");
                $('#bestDealerPrice').focus();
                return false;
            }
        }
    }
    
    if ($("#dealerName").val() == '') {
        alert("You must enter Dealer name");
        $("#dealerName").focus();
        return (false);
    }
    if ($("#fullName").val() == '') {
        alert("You must enter your Name");
        $("#fullName").focus();
        return (false);
    }
    if ($("#telephone").val() == '') {
        alert("You must enter your Telephone");
        $("#telephone").focus();
        return (false);
    }
    if ($("#emailAddress").val() == '') {
        alert("You must enter your email address");
        $("#emailAddress").focus();
        return(false);
    }

    if (!isEmailAddress($("#emailAddress").val())) {
        alert("You must enter a valid email address");
        $("#emailAddress").focus();
        return(false);
    }

}


function isEmailAddress(strEmailAddress) {

    // Check for value not entered
    if (strEmailAddress.length == 0) {
        return (false);
    }

    if (strEmailAddress.indexOf(" ") != -1) {
        return (false);
    }

    // Check that address is in format anyname@domain.level
    var oRegExp2 = /\w+\@(\w[\w-]*\w)+\.\w+/;
    if (!oRegExp2.test(strEmailAddress)) {
        return (false);
    }

    // Check that there is one and only one @ sign
    var oRegExp = new RegExp("@", "g");
    var oMatches = strEmailAddress.match(oRegExp);
    if (oMatches.length != 1) {
        return (false);
    }
    // No errors found
    return(true);
}
