﻿/// <reference path="jquery-1.4.1-vsdoc.js" />

(function() {
    Website = {
        InitFlash: function(url) {
        var flashUrl = url || 'Client/Flash/index5.swf';
        swfobject.embedSWF(Elevator.Utility.ResolveUrl(flashUrl), 'flashContainer', '650', '400', '8.0.0', false, false, { loop: 'true', allowScriptAccess: 'always', wmode: 'opaque' });
        }
    }; // Website

    ContactForm = {
        InitForm: function() {
            alert('Form init goes here');
        }
        , SubmitForm: function() {
            alert('form submitted');
            return true;
        }                
    };

    try {
        document.execCommand("BackgroundImageCache", false, true);
    }
    catch (e) { }

})();

var isOpen = false;
$(document).ready(function() {

    $('.contactus').click(function() {
        ShowContactForm();
    });

    $("#CancelButon").click(function() {
        var validator = $("#form1").validate();
        validator.resetForm();
        resetform();
    });

    $(".closebtn").click(function() {
        isOpen = false;
        $("#serverMsg").html("");
        $('.contactForm').hide("slow");
        
        //call flash function to start the movie
        document.getElementById('flashContainer').restartMovie();
    });

    // Cancel form submition, return false
    $("#form1").submit(function() {
        if ($("#form1").valid())
            FormAjaxStuff();

        return false;
    });

    $.validator.addMethod("requiredName",
        function(value, element) {
            if (element.value == "Name") {
                return false;
            } else
                return true;
        },
        "Please enter name."
    );

    $.validator.addMethod("requiredEmail",
        function(value, element) {
            if (element.value == "Email Address") {
                return false;
            } else
                return true;
        },
        "Please enter Email Address."
    );

    $("#form1").validate({
        errorElement: "div"
        , invalidHandler: function(e, validator) {
            var errors = validator.numberOfInvalids();
            if (errors) {
                $('div.error').hide();
            }
        },
        rules: {
            NameTextBox: {
                requiredName: true
            },
            EmailTextBox: {
                requiredEmail: true,
                email: true
            }
        }
    });
    
    $('.default-value').each(function() {
        var default_value = this.value;
        $(this).focus(function() {
            if (this.value == default_value) {
                this.value = '';
            }
        });
        $(this).blur(function() {
            if (this.value == '') {
                this.value = default_value;
            }
        });
    });
});              // End of $(document).ready

function FormAjaxStuff() {        
    var name = $("#NameTextBox").attr("value");
    var email = $("#EmailTextBox").attr("value");
    var position = $("#PositionTextBox").attr("value");
    var company = $("#CompanyTextBox").attr("value");
    var telephone = $("#TelephoneTextBox").attr("value");
    var comments = $("#CommentsTextBox").attr("value");
    $("#serverMsg").html("");    
    $.ajax({
        type: "GET",
        url: "submitcontact.aspx", //http://www.elevatordigital.co.uk/saatchi/
        dataType: "html",
        data: "name=" + name + "&email=" + email + "&position=" + position + "&company=" + company + "&telephone=" + telephone + "&comments=" + comments,
        success: function(response) {
            // if sucessful; response will contain some stuff echo-ed from .php
            // quick tect: alert("Here is your response: " + response);
            // Append this response to some <div> => let's append it to div with id "serverMsg"
            $("#serverMsg").html(response);
            $('#contactFormWrapper').hide(); 
        },
        error: function() {
            $("#serverMsg").val("Error occured, please try again later.");
        }
    });
} // END OF FormAjaxStuff()

function ShowContactForm() {
    if (isOpen == true)
        return;
    
    $("#serverMsg").html("");
    resetform();
    var validator = $("#form1").validate();
    validator.resetForm();
    $('#contactFormWrapper').show();
    $('.contactForm').show("slow");
    
    //call flash function to stop the movie
    document.getElementById('flashContainer').stopMovie();
    isOpen = true;
}

function resetform() {
    $("#NameTextBox").val('Name');
    $("#EmailTextBox").val('Email Address');
    $("#PositionTextBox").val('Position');;
    $("#CompanyTextBox").val('Company');
    $("#TelephoneTextBox").val('Telephone Number');
    $("#CommentsTextBox").val('Comments / Requests');
}
