﻿
document.write("<script type='text/javascript' src='js/FormFunctions.js'></scr" + "ipt>");

var titleErr = "*Title is required.<br />";
var firstnameErr = "*Firstname is required.<br />";
var lastnameErr = "*Lastname is required.<br />";
var telErr = "*Telephone number is required.<br />";
var emailErr = "*Email is required.<br />";
var emailErr2 = "*Please enter a valid Email address.<br />";
var houseErr = "*House name/no is required.<br />";
var streetErr = "*Street is required.<br />";
var cityErr = "*City/Town is required.<br />";
var postcodeErr = "*Postcode is required.<br />";
var branchErr = "*Please select a Branch.<br />";


function reset() {
    $('.row input').attr('value', '');
    $('.row input').removeClass("inputrequired");
    $('.row select').removeClass("inputrequired");
    $('.row textarea').removeClass("inputrequired");   
    document.getElementById(ddlBranchId).selectedIndex = 0;
    $('.formerrors').empty();
}

function ValidateContactForm(title, firstname, lastname, tel, email, house, street, city, postcode, branch) {
    var Result = ""

    if (trim($('#' + title).attr('value')) == "") {
        $('#' + title).addClass("inputrequired");
        Result += titleErr;
    }    
    if (trim($('#' + firstname).attr('value')) == "") {
        $('#' + firstname).addClass("inputrequired");
        Result += firstnameErr;
    }
    if (trim($('#' + lastname).attr('value')) == "") {
        $('#' + lastname).addClass("inputrequired");
        Result += lastnameErr;
    }
    if (trim($('#' + tel).attr('value')) == "") {
        $('#' + tel).addClass("inputrequired");
        Result += telErr;
    }
    if (trim($('#' + email).attr('value')) == "") {
        $('#' + email).addClass("inputrequired");
        Result += emailErr;
    }
    else {
        if (!isValidEmailAddress($('#' + email).attr('value'))) {
            $('#' + email).addClass("inputrequired");
            Result += emailErr2;
        }
    }
    if (trim($('#' + house).attr('value')) == "") {
        $('#' + house).addClass("inputrequired");
        Result += houseErr;
    }
    if (trim($('#' + street).attr('value')) == "") {
        $('#' + street).addClass("inputrequired");
        Result += streetErr;
    }
    if (trim($('#' + city).attr('value')) == "") {
        $('#' + city).addClass("inputrequired");
        Result += cityErr;
    }
    if (trim($('#' + postcode).attr('value')) == "") {
        $('#' + postcode).addClass("inputrequired");
        Result += postcodeErr;
    }    
    if ($('#' + branch).val() == "0") {
        $('#' + branch).addClass("inputrequired");
        Result += branchErr;
    }
    
    return Result;
}

$(document).ready(function() {
    $('#formerrors').empty();
    $('#' + btnSubmitId).click(function() {
        var Result = "";
        Result = ValidateContactForm(tbTitleId, tbFirstnameId, tbLastnameId, tbTelId, tbEmailId, tbHouseId, tbStreetId, tbCityTownId, tbPostcodeId, ddlBranchId);
        if (Result != "") {
            Result = "<p><strong>Please fill out the required fields before sending the details.</strong></p>" + Result;
            $('.formerrors').html(Result);
            $('html, body').animate({ scrollTop: 0 }, 'slow');
            return false;
        }
        else {
            $('#' + btnSubmitId).hide();
            $('#btnReset').hide();
            $('#sending').attr("style", "display:block;");
            return true;
        }
    }); 
    $('#' + tbTitleId).blur(function() { ValidateTB(tbTitleId, titleErr); });
    $('#' + tbFirstnameId).blur(function() { ValidateTB(tbFirstnameId, firstnameErr); });
    $('#' + tbLastnameId).blur(function() { ValidateTB(tbLastnameId, lastnameErr); });
    $('#' + tbTelId).blur(function() { ValidateTB(tbTelId, telErr); });
    $('#' + tbEmailId).blur(function() { ValidateEmail(tbEmailId, emailErr); });
    $('#' + tbEmailId).blur(function() { ValidateTB(tbEmailId, emailErr); });
    $('#' + tbHouseId).blur(function() { ValidateTB(tbHouseId, houseErr); });
    $('#' + tbStreetId).blur(function() { ValidateTB(tbStreetId, streetErr); });
    $('#' + tbCityTownId).blur(function() { ValidateTB(tbCityTownId, cityErr); }); 
    $('#' + tbPostcodeId).blur(function() { ValidateTB(tbPostcodeId, postcodeErr); });
    $('#' + ddlBranchId).change(function() { ValidateDDL(ddlBranchId, branchErr); });
    $('#' + chkMailingListId).addClass("checkbox");    
});     

