﻿$(function() {
    $(".menu").css("display", "none");

    $("#bpaTopMenu").buildMenu({
        template: "",
        additionalData: "",
        menuSelector: ".menuContainer",
        menuWidth: 150,
        openOnRight: false,
        containment: "window",
        iconPath: "ico/",
        hasImages: false,
        fadeInTime: 100,
        fadeOutTime: 200,
        menuTop: 0,
        menuLeft: 0,
        submenuTop: 0,
        submenuLeft: 4,
        opacity: 1,
        shadow: false,
        shadowColor: "black",
        shadowOpacity: .2,
        openOnClick: false,
        closeOnMouseOut: true,
        closeAfter: 500,
        minZindex: "auto",
        hoverIntent: 0, //if you use jquery.hoverIntent.js set this to time in milliseconds; 0= false;
        submenuHoverIntent: 0 //if you use jquery.hoverIntent.js set this to time in milliseconds; 0= false;
    });

    // Scroll down to title of page, but not if it's the home page
    var cloc = location.href;
    cloc = cloc.substring(cloc.lastIndexOf("/") + 1, cloc.length);
    if ((cloc != "home.asp") && (cloc != "")) {
        window.scrollTo(0, $("#leftsidebar").offset().top);
    }

    // Sliding sub-menus
    $("#od").click(function() {
        $("#odsubmenu").slideToggle('slow');
        $("#btesubmenu").slideUp('slow');
        $("#biasubmenu").slideUp('slow');
        $("#dssubmenu").slideUp('slow');
        return false;
    });

    $("#bte").click(function() {
        $("#btesubmenu").slideToggle('slow');
        $("#odsubmenu").slideUp('slow');
        $("#utssubmenu").hide();
        $("#biasubmenu").slideUp('slow');
        $("#dssubmenu").slideUp('slow');
        return false;
    });

    $("#uts").click(function() {
        $("#utssubmenu").slideToggle('slow');
        return false;
    });

    $("#bia").click(function() {
        $("#biasubmenu").slideToggle('slow');
        $("#btesubmenu").slideUp('slow');
        $("#odsubmenu").slideUp('slow');
        $("#utsubmenu").slideUp('slow');
        $("#dssubmenu").slideUp('slow');
        return false;
    });

    $("#ds").click(function() {
        $("#dssubmenu").slideToggle('slow');
        $("#btesubmenu").slideUp('slow');
        $("#odsubmenu").slideUp('slow');
        $("#utsubmenu").slideUp('slow');
        $("#biasubmenu").slideUp('slow');
        return false;
    });


    // Handles the "Member Email" and "Password" text
    $("#member_email").focus(function() {
        if ($(this).val() == "Member Email") {
            $(this).val("");
        }
    });
    $("#member_email").blur(function() {
        if ($(this).val() < " ") {
            $(this).val("Member Email");
        }
    });

    $("#member_password").focus(function() {
        if ($(this).val() == "Password") {
            $(this).val("");
        }
    });
    $("#member_password").blur(function() {
        if ($(this).val() < " ") {
            $(this).val("Password");
        }
    });

    // Submit button hovers
    $("#bpa_join_submit").hover(function() {
        $(this).toggleClass("bpa_join_submit_hover");
    });

    $("#contact_us_submit").hover(function() {
        $(this).toggleClass("edit_submit_hover");
    });


    // Validate and handle member login
    $("#frm_memberlogin").submit(function() {
        if ($("#member_email").val() < " " || $("#member_email").val() == "Member Email") {
            alert("You must enter your Email Address before logging in.");
            $("#member_email").focus();
            return false;
        }

        if ($("#member_password").val() < " " || $("#member_password").val() == "Member Password") {
            alert("You must enter your Password before logging in.");
            $("#member_password").focus();
            return false;
        }

        $.ajax({
            type: "POST",
            url: "dologin_dbj.asp",
            data: $("#frm_memberlogin").serialize(),
            success:
			function(data) {
			    //				alert(data);
			    if (data == "BadEmail") {
			        alert("Your Email address does not appear to be valid. Please correct it before submitting this form.");
			        $("#login_email").focus();
			        return false;
			    }
			    else if (data == "BadLogin") {
			        alert("Unknown Email Address or Password");
			        $("#login_email").focus();
			        return false;
			    }
			    else {
			        $("#frm_memberlogin").slideUp("slow");
			        $("#loggedin").fadeIn("slow");
			        //			        $("#awards_link").show();
			    }
			}
        });

        return false;
    });

    // Validate and handle application for membership
    $("#bpa_join").submit(function() {

        if ($("#bpa_join_title").val() == "0") {
            alert("You must enter your Title before submitting this form.");
            $("#bpa_join_title").focus();
            return false;
        }

        if ($("#bpa_join_name").val() < " ") {
            alert("You must enter your Name before submitting this form.");
            $("#bpa_join_name").focus();
            return false;
        }

        if ($("#bpa_join_jobtitle").val() < " ") {
            alert("You must enter your Job Title before submitting this form.");
            $("#bpa_join_jobtitle").focus();
            return false;
        }

        if ($("#bpa_join_email").val() < " ") {
            alert("You must enter your Email Address before submitting this form.");
            $("#bpa_join_email").focus();
            return false;
        }

        if ($("#bpa_join_orgname").val() < " ") {
            alert("You must enter your Organisations Name before submitting this form.");
            $("#bpa_join_orgname").focus();
            return false;
        }

        if ($("#bpa_join_orgadd").val() < " ") {
            alert("You must enter your Organisations Address before submitting this form.");
            $("#bpa_join_orgadd").focus();
            return false;
        }

        if ($("#bpa_join_orgpcode").val() < " ") {
            alert("You must enter your Organisations Postcode before submitting this form.");
            $("#bpa_join_orgpcode").focus();
            return false;
        }

        if ($("#bpa_join_orgtel").val() < " ") {
            alert("You must enter your Organisations Telephone before submitting this form.");
            $("#bpa_join_orgtel").focus();
            return false;
        }


        $.ajax({
            type: "POST",
            url: "bpa_join_db.asp",
            data: $("#bpa_join").serialize(),
            success:
			function(data) {
			    if (data == "BadEmail") {
			        alert("Your Email address does not appear to be valid. Please correct it before submitting this form.");
			        $("#bpa_join_email").focus();
			    }
			    else if (data == "BadOrgEmail") {
			        alert("Your Organisations Email address does now appear to be valid. Please correct it before submitting this form.");

			        $("#bpa_join_orgemail").focus();
			    }
			    else if (data == "BadCaptcha") {
			        alert("I'm afraid that the two words do not match.");
			        $("#recaptcha_response_field").focus();
			    }
			    else {
			        window.location.href = "join_ty.asp";
			    }
			}
        });
        return false;
    });

    // Validate and handle contact us form
    $("#webform-client-form-9").submit(function() {
        if ($("#edit-submitted-name").val() < " ") {
            alert("You must enter your Name before submitting your enquiry.");
            $("#edit-submitted-name").focus();
            return false;
        }

        if ($("#edit-submitted-email").val() < " ") {
            alert("You must enter your Email Address before submitting your enquiry.");
            $("#edit-submitted-email").focus();
            return false;
        }

        if ($("#edit-submitted-message").val() < " ") {
            alert("You must enter your Message before submitting your enquiry.");
            $("#edit-submitted-message").focus();
            return false;
        }

        $.ajax({
            type: "POST",
            url: "contact_db.asp",
            data: $("#webform-client-form-9").serialize(),
            success:
			function(data) {
			    if (data == "BadEmail") {
			        alert("Your Email address does not appear to be valid. Please correct it before submitting this form.");
			        $("#edit-submitted-email").focus();
			    }
			    else if (data == "BadCaptcha") {
			        alert("I'm afraid that the two words do not match.");
			        $("#recaptcha_response_field").focus();
			    }
			    else {
			        window.location.href = "contact_ty.asp";
			    }
			}
        });
        return false;
    });

    // Validate and handle documents form
    $("#frm_Documents").submit(function() {
        if ($("#doc_name").val() < " ") {
            alert("You must enter your Name before submitting your enquiry.");
            $("#doc_name").focus();
            return false;
        }

        if ($("#doc_email").val() < " ") {
            alert("You must enter your Email Address before submitting your enquiry.");
            $("#doc_email").focus();
            return false;
        }

        var docchecked = $("input:checked").length;

        if (docchecked == 0) {
            alert("You must select at least one document before submitting your enquiry.");
        }

        $.ajax({
            type: "POST",
            url: "documents_db.asp",
            data: $("#frm_Documents").serialize(),
            success:
			function(data) {
			    if (data == "BadEmail") {
			        alert("Your Email address does not appear to be valid. Please correct it before submitting this form.");
			        $("#bpa_join_email").focus();
			    }
			    else if (data == "BadCaptcha") {
			        alert("I'm afraid that the two words do not match.");
			        $("#recaptcha_response_field").focus();
			    }
			    else {
			        window.location.href = "documents_ty.asp";
			    }
			}
        });
        return false;
    });

    // Validate and handle member edit details
    $("#frm_EditDetails").submit(function() {
        if ($("#member_forename").val() < " ") {
            alert("You must enter your Forename before submitting this form.");
            $("#member_forename").focus();
            return false;
        }

        if ($("#member_surname").val() < " ") {
            alert("You must enter your Surname before submitting this form.");
            $("#member_surname").focus();
            return false;
        }

        if ($("#member_jobtitle").val() < " ") {
            alert("You must enter your Job Title before submitting this form.");
            $("#member_jobtitle").focus();
            return false;
        }

        if ($("#member_login").val() < " ") {
            alert("You must enter your Login Email Address before submitting this form.");
            $("#member_login").focus();
            return false;
        }

        if ($("#member_pword").val() < " ") {
            alert("You must enter a Password before submitting this form.");
            $("#member_pword").focus();
            return false;
        }

        if ($("#member_address1").val() < " ") {
            alert("You must enter your Address before submitting this form.");
            $("#member_address1").focus();
            return false;
        }

        if ($("#member_postcode").val() < " ") {
            alert("You must enter your Postcode before submitting this form.");
            $("#member_postcode").focus();
            return false;
        }

        if ($("#member_tel").val() < " ") {
            alert("You must enter your Telephone before submitting this form.");
            $("#member_tel").focus();
            return false;
        }

        $.ajax({
            type: "POST",
            url: "edit_details_db.asp",
            data: $("#frm_EditDetails").serialize(),
            success:
			function(data) {
			    if (data == "BadEmail") {
			        alert("Your Email address does not appear to be valid. Please correct it before submitting this form.");
			        $("#bpa_join_email").focus();
			    }
			    else if (data == "BadCaptcha") {
			        alert("I'm afraid that the two words do not match.");
			        $("#recaptcha_response_field").focus();
			    }
			    else {
			        window.location.href = "edit_details_ty.asp";
			    }

			}
        });
        return false;
    });

    // Validate and handle member edit details
    $("#frm_EditCompany").submit(function() {
        if ($("#member_name").val() < " ") {
            alert("You must enter your Forename before submitting this form.");
            $("#member_name").focus();
            return false;
        }

        if ($("#member_address1").val() < " ") {
            alert("You must enter your Address before submitting this form.");
            $("#member_address1").focus();
            return false;
        }

        if ($("#member_postcode").val() < " ") {
            alert("You must enter your Postcode before submitting this form.");
            $("#member_postcode").focus();
            return false;
        }

        if ($("#member_tel").val() < " ") {
            alert("You must enter your Telephone before submitting this form.");
            $("#member_tel").focus();
            return false;
        }

        if ($("#member_yearbookprofile").val() > "") {
            var wordsfound = CheckWords("member_yearbookprofile");
            if (wordsfound > 50) {
                alert("You're Yearbook Company Listing can only be 50 words long.  To upgrade to an Enhanced Listing please contact Paul Turp on 0117 923 2951.");
                $("#member_yearbookprofile").focus();
                return false;
            }
        }

        if ($("#member_yearbookprofileenh").val() > "") {
            var wordsfound = CheckWords("member_yearbookprofileenh");
            if (wordsfound > 100) {
                alert("You're Yearbook Enhanced Company Listing can only be 100 words long.");
                $("#member_yearbookprofileenh").focus();
                return false;
            }
        }

        var prods = '';
        var prodDesc = '';
        var count = 0;
        $("#member_products option").each(function() {
            if (count > 0) {
                prods += ',';
                prodDesc += ', ';
            }
            prods += this.value;
            prodDesc += this.text;
            count++;
        });

        $.ajax({
            type: "POST",
            url: "edit_codetails_db.asp?prods=" + prods + "&prodDesc=" + prodDesc,
            data: $("#frm_EditCompany").serialize(),
            success:
			function(data) {
			    if (data == "BadEmail") {
			        alert("Your Email address does not appear to be valid. Please correct it before submitting this form.");
			        $("#bpa_join_email").focus();
			    }
			    else {
			        window.location.href = "edit_codetails_ty.asp";
			    }
			}
        });
        return false;
    });

    $("#member_yearbookprofile").keyup(function(obj) {
        var words = CheckWords("member_yearbookprofile");
        $("#yearbookwordcount").val(words);
        if (words > 50)
            $("#yearbookwordcount").addClass("worderror");
        else
            $("#yearbookwordcount").removeClass("worderror");
    });

    $("#member_yearbookprofileenh").keyup(function(obj) {
        var words = CheckWords("member_yearbookprofileenh");
        $("#yearbookwordcountenh").val(words);
        if (words > 100)
            $("#yearbookwordcountenh").addClass("worderror");
        else
            $("#yearbookwordcountenh").removeClass("worderror");
    });

    $("#member_yearbookprofile").trigger("keyup");
    $("#member_yearbookprofileenh").trigger("keyup");
});


function CheckWords(cntrl) {
    var value = $("#" + cntrl).val();
    if (value == "")
        return 0;
    while (value.indexOf('  ') > -1) {
        value = value.replace('  ', ' ');
    }
    var wordsfound = 0;
    var linelist = value.split('\n');
    for (var idx = 0; idx < linelist.length; idx++) {
        var line = linelist[idx];
        while (line.substr(0, 1) == ' ')
            line = line.substr(1);
        while (line.substr(line.length - 1) == ' ')
            line = line.substr(0, line.length - 1);
        var wordlist = line.split(' ');
        if (line != "")
            wordsfound += wordlist.length;
    }
    return wordsfound;
}
