﻿
document.write("<script type='text/javascript' src='js/FormFunctions.js'></scr" + "ipt>");

function ContactTypeSwitch(selection) {
    switch (selection) {
        case "email":
            $('#address').attr('style', 'display:none;');
            break;
        case "tel":
            $('#address').attr('style', 'display:none;');
            break;
        case "post":
            $('#address').attr('style', 'display:block;');
            break;
        default:
            $('#address').attr('style', 'display:none;');
            break;
    }
}

var firstnameErr = "*Firstname is required.<br />";
var lastnameErr = "*Lastname is required.<br />";
var positionErr = "*Please select a Position.<br />";
var industryErr = "*Please select an Industry.<br />";
var enquiryErr = "*Please select an Enquiry type.<br />";
var companyErr = "*Company is required.<br />";
var postcodeErr = "*Postcode 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 contacttypeErr = "*Please select how you would like us to contact you.<br />";
var addressErr = "*Address is required.<br />";
var cityErr = "*City/Town is required.<br />";
var customertypeErr = "*Please select a Customer Type.<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(ddlPositionId).selectedIndex = 0;
    document.getElementById(ddlIndustryId).selectedIndex = 0;
    document.getElementById(ddlEnquiryTypeId).selectedIndex = 0;
    document.getElementById(ddlCustomerTypeId).selectedIndex = 0;
    document.getElementById(ddlContactTypeId).selectedIndex = 0;
    document.getElementById(ddlBranchId).selectedIndex = 0;
    $('.formerrors').empty();    
} 

function ValidateContactForm(firstname, lastname, position, industry, enquiry, company, 
                              postcode, email, tel, contacttype, address, city, customertype, 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 ($('#' + position).val() == "0") {
       // $('#' + position).addClass("inputrequired");
       // Result += positionErr;
   // }
    if ($('#' + industry).val() == "0") {
        $('#' + industry).addClass("inputrequired");
        Result += industryErr;
    }
    if ($('#' + enquiry).val() == "0") {
        $('#' + enquiry).addClass("inputrequired");
        Result += enquiryErr;
    }
    if (trim($('#' + company).attr('value')) == "") {
        $('#' + company).addClass("inputrequired");
        Result += companyErr;
    }
    if (trim($('#' + postcode).attr('value')) == "") {
        $('#' + postcode).addClass("inputrequired");
        Result += postcodeErr;
    }
    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 ($('#' + contacttype).val() == "0") {
        $('#' + contacttype).addClass("inputrequired");
        Result += contacttypeErr;
    }
    if ($('#' + contacttype).val() == "post") {
        if (trim($('#' + address).attr('value')) == "") {
            $('#' + address).addClass("inputrequired");
            Result += addressErr;
        }
        if (trim($('#' + city).attr('value')) == "") {
            $('#' + city).addClass("inputrequired");
            Result += cityErr;
        }
    }
    if ($('#' + customertype).val() == "0") {
        $('#' + customertype).addClass("inputrequired");
        Result += customertypeErr;
    }
    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, ddlPositionId, ddlIndustryId, ddlEnquiryTypeId, tbCompanyId, tbPostcodeId, tbEmailId, tbTelId, ddlContactTypeId, tbAd1Id, tbCityTownId, ddlCustomerTypeId, 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); });
    $('#' + tbCompanyId).blur(function() { ValidateTB(tbCompanyId, companyErr); });
    $('#' + tbPostcodeId).blur(function() { ValidateTB(tbPostcodeId, postcodeErr); });
    $('#' + tbEmailId).blur(function() { ValidateTB(tbEmailId, emailErr); });
    $('#' + tbEmailId).blur(function() { ValidateEmail(tbEmailId, emailErr); });
    $('#' + tbTelId).blur(function() { ValidateTB(tbTelId, telErr); });
    $('#' + tbAd1Id).blur(function() { ValidateTB(tbAd1Id, addressErr); });
    $('#' + tbCityTownId).blur(function() { ValidateTB(tbCityTownId, cityErr); });
    $('#' + tbMessageId).blur(function() { ValidateTB(tbMessageId, messageErr); });
    $('#' + ddlPositionId).change(function() { ValidateDDL(ddlPositionId, positionErr); });
    $('#' + ddlIndustryId).change(function() { ValidateDDL(ddlIndustryId, industryErr); });
    $('#' + ddlEnquiryTypeId).change(function() { ValidateDDL(ddlEnquiryTypeId, enquiryErr); });
    $('#' + ddlContactTypeId).change(function() {
        ValidateDDL(ddlContactTypeId, contacttypeErr);
        var selection = $('#' + ddlContactTypeId).val();
        ContactTypeSwitch(selection);
    });
    $('#' + ddlCustomerTypeId).change(function() { ValidateDDL(ddlCustomerTypeId, customertypeErr); });
    $('#' + ddlBranchId).change(function() { ValidateDDL(ddlBranchId, branchErr); });
    $('#' + chkMailingListId).addClass("checkbox");
    $('#' + chk3rdPartyId).addClass("checkbox");
});     

