// turns the title of input fields into 'placeholder' text that disappears when the field gets focus
function activateFormPlaceholders() {
    var inputs = document.getElementsByTagName("input");
    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i].getAttribute("type") == "text") {
            if (inputs[i].getAttribute("title") && inputs[i].getAttribute("title").length > 0) {
                inputs[i].value = inputs[i].getAttribute("title");
                inputs[i].onfocus = function () {
                    if (this.value == this.getAttribute("title")) {
                        this.value = "";
                    }
                    return false;
                }
            }
        }
    }
}

    function isValidEmailAddress(emailAddress) {
	    var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	    return pattern.test(emailAddress);
	}

	$(document).ready(function () {
	    // put placeholder text in form fields
	    activateFormPlaceholders();

	    $('.smallFMA').hover(function () {
	        $(this).parent().find('p.more a').addClass('hover');
	    },
        function () {
            $(this).parent().find('p.more a').removeClass('hover');
        });

	    $('.print').click(function () {
	        window.print();
	    });

	    $('#sm-signup-form').submit(function () {
	        var dataString = 'EmailAddressSignup=' + $("input#sm-signup-email").val();
	        $("#sm-signup-msg").html("").addClass('signup-loading');

	        if (isValidEmailAddress($("input#sm-signup-email").val())) {
	            $.ajax({
	                type: "POST",
	                url: "/Account/EmailSignup",
	                dataType: 'text',
	                data: dataString,
	                success: function (data) {
	                    $("#sm-signup-msg").removeClass('signup-loading').html("Thank you for signing up for our newsletter.");
	                }
	            });
	        }
	        else {
	            $("#sm-signup-msg").removeClass('signup-loading').html("Please enter a valid email address.");
	        }
	        return false;
	    });






	    // These set the attributes of the search fields to be the deafult text
	    if ($('.wrapper-zipfinder input.searchBox').length > 0) {
	        $(window).load(function () {
	            var i = $('.wrapper-zipfinder input.searchBox');
	            i.val('Enter Post Code');
	            i.removeClass('active');
	        });
	    };

	    if ($('.wrapper-fma-zipfinder input.searchBox').length > 0) {
	        $(window).load(function () {
	            var i = $('.wrapper-fma-zipfinder input.searchBox');
	            i.val('Enter Your Post Code');
	            i.removeClass('active');
	        });
	    };

	    // Toggles the text in the Zip finder box on fucus
	    $('input.searchBox').focus(function () {
	        var inputText = $(this).attr('value');

	        if (inputText == 'Enter Post Code' || inputText == 'Enter Your Post Code') {
	            $(this).val('');
	            $(this).addClass('active');
	            $(this).attr('maxlength', '10');
	        }
	    });

	    $('input.searchBox').blur(function () {
	        var inputText = $(this).attr('value');
	        var defaultTextShort = 'Enter Post Code';
	        var defaultText = 'Enter Your Post Code';

	        if (inputText == defaultTextShort || inputText == defaultText || inputText == '') {
	            $(this).removeClass('active');
	            $(this).attr('maxlength', '25');

	            if ($(this).attr('id') == "fma-findZip") {
	                $(this).val(defaultText);
	            } else if ($(this).attr('id') == "findZip") {
	                $(this).val(defaultTextShort);
	            }
	        }
	    });



	    // Hack to add a "s" to the end of red since this is being pulled from the server
	    if ($('#ColorCategory').length > 0) {
	        var i = $('#ColorCategory option[value="Golds/Red"]');
	        i.text('Golds/Reds');
	    }


	    // Warranty comparison

	    $('#show-warranty').click(function () {
	        $('#wrapper-warranty').show();
	        return false;
	    });

	    $('#close-warranty').click(function () {
	        $('#wrapper-warranty').hide();
	        return false;
	    });

	    // ShareThis toggle 

	    $('#shareThis').click(function () {
	        $('#videocenter-wrap').hide();
	        $('#videocenter-overlay').addClass('active');
	        return false;
	    });

	    $('div.stclose').live('click', function () {
	        $('#videocenter-wrap').show();
	        $('#videocenter-overlay').removeClass('active');
	        return false;
	    });

	    if ($('#shareThis').length > 0) {
         $('body').click(function () {
             if ($('#videocenter-overlay').hasClass('active')) {
	                $('#videocenter-wrap').show();
	                $('#videocenter-overlay').removeClass('active');
                }
	        });
	    }



	});

	function overlay_satisfaction() {
	        Shadowbox.open({
	        player: 'iframe',
	        content: '/page/about-us/stainmaster-flooring-centers/overlay-swc?v=' + cachebreaker,
	        height: 400,
	        width: 700
	    });
    }


	// create closure
	(function ($) {
	    // plugin definition
	    $.fn.surveyValidation = function () {

	        $('.other_txt_input input').focus(function () {
	            $(this).parent().prev().children('input').attr('checked', true);
	        });
	        $('.other_txt input').click(function () {
	            if ($(this).attr('checked') == false) $(this).parent().next().children('input').val('');
	        });
	        $('.radios input').click(function () {
	            radio_value = $(this + ":checked").val();
	            if (radio_value != 'other') $(this).parent().parent().parent().find('.line .other_txt_input input').val('');
	        });


	    };
	    // end of closure
	})(jQuery);

