﻿
document.write("<script type='text/javascript' src='/js/FormFunctions.js'></scr" + "ipt>");

var firstnameErr = "*Firstname is required.<br />";
var lastnameErr = "*Lastname is required.<br />";
var emailErr = "*Email is required.<br />";
var emailErr2 = "*Please enter a valid Email address.<br />";
var telErr = "*Telephone number is required.<br />";
var branchErr = "*Please select a Branch.<br />";
var messageErr = "*Message is required.<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(firstname, lastname, email, tel, branch, message) {
    var Result = ""
    if (trim($('#' + firstname).attr('value')) == "") {
        $('#' + firstname).addClass("inputrequired");
        Result += firstnameErr;
    }
    if (trim($('#' + lastname).attr('value')) == "") {
        $('#' + lastname).addClass("inputrequired");
        Result += lastnameErr;
    }
   
    if (trim($('#' + email).attr('value')) == "") {
        $('#' + email).addClass("inputrequired");
        Result += emailErr;
    }
    else {
        if (!isValidEmailAddress($('#' + email).attr('value'))) {
            $('#' + email).addClass("inputrequired");
            Result += emailErr2;
        }
    }
    if (trim($('#' + tel).attr('value')) == "") {
        $('#' + tel).addClass("inputrequired");
        Result += telErr;
    }
    
    if ($('#' + branch).val() == "0") {
        $('#' + branch).addClass("inputrequired");
        Result += branchErr;
    }
    if (trim($('#' + message).attr('value')) == "") {
        $('#' + message).addClass("inputrequired");
        Result += messageErr;
    }    
    return Result;
}

$(document).ready(function() {
    $('#formerrors').empty();
    $('#' + btnSubmitId).click(function() {
        var Result = "";
        Result = ValidateContactForm(tbFirstnameId, tbLastnameId, tbEmailId, tbTelId, ddlBranchId, tbMessageId);
        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;
        }
    });
    $('#' + tbFirstnameId).blur(function() { ValidateTB(tbFirstnameId, firstnameErr); });
    $('#' + tbLastnameId).blur(function() { ValidateTB(tbLastnameId, lastnameErr); });    
    $('#' + tbEmailId).blur(function() { ValidateTB(tbEmailId, emailErr); });
    $('#' + tbEmailId).blur(function() { ValidateEmail(tbEmailId, emailErr); });
    $('#' + tbTelId).blur(function() { ValidateTB(tbTelId, telErr); });    
    $('#' + tbMessageId).blur(function() { ValidateTB(tbMessageId, messageErr); });   
    $('#' + ddlBranchId).change(function() { ValidateDDL(ddlBranchId, branchErr); });
    $('#' + chkMailingListId).addClass("checkbox");   
});     

