﻿$(document).ready(function () {
    var $winwidth = $(window).width();
    $("background-image").attr({
        width: $winwidth
    });
    $(window).bind("resize", function () {
        var $winwidth = $(window).width();
        $("#background-image").attr({
            width: $winwidth
        });
    });

    /* this has nothing to do with it, just wanted to remove the last 'pipe' from the background links */
    $("#bg_delete").css("border-right", "none");


    $(".bgChange").click(function () {
        $(".buxa").fadeOut();
        $(".contact_form").fadeOut();
        $("#body_id").attr("style", "background-image:url(" + $(this).attr('rel') + ");background-repeat:no-repeat");

        $(this).addClass("link_active");

        var id = $(this).attr("id");
        id = id.substring(id.indexOf('_') + 1);
        var num = id;
        id = "content/" + id + ".html";
        $(".scroll-pane").each(function () {
            if ($(this).attr("id") == ("scrollMe" + num))
                $(this).fadeIn();
            else
                $(this).hide();
        })
        $(".buxa").fadeIn();

        var src = $("img", this).attr("rel");
        //$("img", this).attr("src", src.split("|")[2]);

        $(".bgChange").each(function () {
            var temp = $("img", this).attr("rel");
            if (temp != src) {
                // $("img", this).attr("src", temp.split("|")[0]);
                $(this).removeClass("link_active");
            }
        });
    });


    $(".buxa").hide();


    $("#mclose").click(function () {
        $(".buxa").fadeOut();
        $(".bgChange").removeClass("link_active");
    });
    $(".contact").click(function () {
        $("#mclose").click();
        $(".contact_form").fadeIn();
    });
    $(".close").click(function () {
        $(".contact_form").fadeOut();
    });

    $(".input_field1, .textarea2").focus(function () {
        var label = $(this).attr("rel");
        var val = $(this).val();
        if (val == label)
            $(this).val("");
    });
    $(".input_field1, .textarea2").blur(function () {
        var label = $(this).attr("rel");
        var val = $(this).val();
        if (val == "")
            $(this).val(label);
    });
    $(".submitbutton").click(function () {
        if (validate()) {

            $("#loading").fadeIn();
            $(this).attr('disabled', 'disabled');
            var firstName = $("#firstName").val();
            var lastName = $("#lastName").val();
            var email = $("#email").val();
            var phone = $("#phone").val();
            var title = $("#title").val();
            var company = $("#company").val();
            var comments = $("#comments").val();

            $.ajax({
                type: "POST",
                url: 'post.aspx',
                data: { firstName: firstName, lastName: lastName, email: email, phone: phone, title: title, company: company, comments: comments },
                success: function (data) {
                    $(".formContent").fadeOut();
                    $(".thanksContent").fadeIn();
                    $("#form1")[0].reset();
                    $("#loading").fadeOut();
                    $(".submitbutton").removeAttr("disabled");
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    alert("Error posting your request please try again later");
                    $("#loading").fadeOut();
                    $(".submitbutton").removeAttr("disabled");
                }
            });
        }
    });
    $('#backToForm').click(function () {
        $(".thanksContent").fadeOut();
        $(".formContent").fadeIn();
        $("#loading").fadeOut();
        $(".submitbutton").removeAttr("disabled");
    });

});
function validate() {
    var firstName = $("#firstName").val();
    var lastName = $("#lastName").val();
    var email = $("#email").val();
    var phone = $("#phone").val();
    var title = $("#title").val();
    var company = $("#company").val();
    var comments = $("#comments").val();

    if (firstName == '' || firstName == 'FIRST NAME') {
        alert("Please enter First Name");
        return false;
    }
    if (lastName == '' || lastName == 'LAST NAME') {
        alert("Please enter Last Name");
        return false;
    }
    if (email == '' || email == 'EMAIL') {
        alert("Please enter Email Address");
        return false;
    }
    else {
        var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
        if (!emailPattern.test(email)) {
            alert("Please enter correct Email Address");
            return false;
        }
    }
    return true;
}
