var TCP = TCP || {};
TCP.CMS = TCP.CMS || {};



TCP.CMS.setup_contact_us = function setup_contact_us() {
    if (onContactUsPage())
    {
        // ---------- init code ---------- >>
        setPrimaryIndustryValues(); //must do this first. So we can look them up by their new value
        var primaryIndustryDropDown = {};
        var subjectDropDown = {};
        var otherIndustryDescTextArea = {};
        var otherIndustryDescDiv = {};
        if ($('select option[value="Animal Care"]').length){
            primaryIndustryDropDown = $('select option[value="Animal Care"]').parent();
        }
        if ($('select option[value="Training"]').length){
            subjectDropDown = $('select option[value="Training"]').parent();
        }
        if ($('label:contains("Other Industry Description")').length){
            otherIndustryDescTextArea = $('label:contains("Other Industry Description")').parent().find("textarea");
        }
        if ($('label:contains("Other Industry Description")').length){
            otherIndustryDescDiv = $('label:contains("Other Industry Description")').parent().parent();
        }
        setPrimaryIndustryValues();
        hideFields();
        if (subjectDropDown.length){
            subjectDropDown.change(
                    function ()
                    {
                        var webFormId = $(this).closest(".portlet-boundary_1_WAR_webformportlet_").attr("id");
                        var subjectValue = getSubjectValue(webFormId);
                        if(subjectValue == "Manufacturer Sales")
                        {
                            //show primaryIndustryDropDown
                            showPrimaryIndustry(webFormId);
                        }
                        else
                        {
                            hideFields();
                        }
                    });
        }

        if (primaryIndustryDropDown.length){
            primaryIndustryDropDown.change(
                    function ()
                    {
                        var webFormId = $(this).closest(".portlet-boundary_1_WAR_webformportlet_").attr("id");
                        var primaryValue = getPrimaryIndustryValue(webFormId);
                        var localOtherIndustryDescDiv = otherIndustryDescDiv;
                        localOtherIndustryDescDiv = getLocalattr(webFormId,localOtherIndustryDescDiv,otherIndustryDescDiv);
                        if(primaryValue == "Other")
                        {
                            localOtherIndustryDescDiv.show();
                        }
                        else
                        {
                            clearHideOtherIndustryDesc();
                        }
                    });
        }

        if ($('.aui-button-input-submit').length){
            $('.aui-button-input-submit').click(
                    function (event)
                    {
                        return validateContactUsFields(event, primaryIndustryDropDown, otherIndustryDescTextArea, otherIndustryDescDiv);
                    });
        }
        // ---------- init code ---------- >>
    }
    function showPrimaryIndustry(webFormId)
    {
        var localPrimaryIndustryDropDown = primaryIndustryDropDown;
        localPrimaryIndustryDropDown = getLocalattr(webFormId,localPrimaryIndustryDropDown, primaryIndustryDropDown);
        if  ($(localPrimaryIndustryDropDown).length)
        {
            $(localPrimaryIndustryDropDown).parent().parent().show();
        }
    }

    function getSubjectValue(webFormId)
    {
        var result = "";
        var localSubjectDropDown = subjectDropDown;
        localSubjectDropDown = getLocalattr(webFormId, localSubjectDropDown, subjectDropDown);
        if (localSubjectDropDown.length) {
            result = localSubjectDropDown.find("option:selected").val();
        }
        return result;
    }

    function getPrimaryIndustryValue(webFormId)
    {
        var result = "";
        var localPrimaryIndustryDropDown = primaryIndustryDropDown;
        localPrimaryIndustryDropDown = getLocalattr(webFormId,localPrimaryIndustryDropDown, primaryIndustryDropDown);
        if (localPrimaryIndustryDropDown.length) {
            result = localPrimaryIndustryDropDown.find("option:selected").val();
        }
        return result;
    }


    function setPrimaryIndustryValues()
    {
        if($('option[value="Animal/Pet Care"]').length)
        {
            $('option[value="Animal/Pet Care"]').val("Animal Care")
        }

        if($('option[value="Automotive"]').length)
        {
            $('option[value="Automotive"]').val("Automotive Aftermarket")
        }

        if($('option[value="Beauty"]').length)
        {
            $('option[value="Beauty"]').val("Body Care")
        }

        if($('option[value="Electronics"]').length)
        {
            $('option[value="Electronics"]').val("Electronics")
        }

        if($('option[value="Fashion"]').length)
        {
            $('option[value="Fashion"]').val("Body Care")
        }

        if($('option[value="Health/Personal Care"]').length)
        {
            $('option[value="Health/Personal Care"]').val("Body Care")
        }

        if($('option[value="Sporting Goods"]').length)
        {
            $('option[value="Sporting Goods"]').val("Sporting Goods")
        }

        if($('option[value="Tactical"]').length)
        {
            $('option[value="Tactical"]').val("Tactical / Safety")
        }

        if($('option[value="Travel/Leisure"]').length)
        {
            $('option[value="Travel/Leisure"]').val("Travel / Hobby")
        }

        if($('option[value="Other"]').length)
        {
            $('option[value="Other"]').val("Other")
        }


        if($('option[value="Get your brand on 3point5.com"]').length)
        {
            $('option[value="Get your brand on 3point5.com"]').val("Manufacturer Sales")
        }

        if($('option[value="Get your brand on 3point5.com/ProMotive.com"]').length)
        {
            $('option[value="Get your brand on 3point5.com/ProMotive.com"]').val("Manufacturer Sales")
        }

        if($('option[value="Get your brand on ProMotive.com"]').length)
        {
            $('option[value="Get your brand on ProMotive.com"]').val("Manufacturer Sales")
        }

    }

    function hideFields()
    {
        clearHidePrimaryIndustry();
        clearHideOtherIndustryDesc();
        showExperticitySalesPrimaryIndustry();
    }

    function clearHideOtherIndustryDesc()
    {
        if (otherIndustryDescDiv.length && otherIndustryDescTextArea.length) {
            otherIndustryDescDiv.hide();
            otherIndustryDescTextArea.val("");
        }
    }

    function clearHidePrimaryIndustry()
    {
        //hide primaryIndustryDropDown
        if(subjectDropDown.length && primaryIndustryDropDown.length)
        {
            primaryIndustryDropDown.parent().parent().hide();
            primaryIndustryDropDown.parent().parent().find('option[value=""]').attr("selected",true);
        }
    }
    function showExperticitySalesPrimaryIndustry()
    {
        if($('select option:contains("Animal Care")').length)
        {
            $('select option:contains("Animal Care")').parent().parent().parent().show();
        }
    }

    function onContactUsPage()
    {
        return $(".use-contact-us-validation").length;
    }

    function stopOtherEvents(event)
    {
        event.preventDefault();
        event.stopPropagation();
        event.stopImmediatePropagation();
    }

    function checkElementBelongsToForm(element, webFormId)
    {
        return $(element).closest(".portlet-boundary_1_WAR_webformportlet_").attr("id") == webFormId;
    }

    function resetValidationErrors()
    {
        $(".webFormValidationError").remove();
        $(".aui-field-label").css("color", "black");
        $(".error").removeClass("error");
    }

    function displayValidationErrorHeader(inputField)
    {
        inputField = inputField || this;
        if(!$(".webFormValidationError").length)
        {
            $(inputField).closest(".portlet-boundary_1_WAR_webformportlet_").prepend("<div class='portlet-msg-error webFormValidationError alert-message error'><span>You have entered invalid data. Please try again.</span></div>")
        }
    }

    function markValidationErrorLabel(webFormId, inputField)
    {
        inputField = inputField || this;
        var label = $(inputField).parent().prev().closest(".aui-field-label");
        if(label.length)
        {
            if(checkElementBelongsToForm(label, webFormId))
            {
                $(label).css("color", "red");
            }
        }
    }

    function validateAllWebFormRequired(event,webFormId)
    {
        var result = true;
        $(".aui-field-input").each(function(){
            var requiredFieldVal = $(this).val();
            if($.trim(requiredFieldVal) == "" && checkElementBelongsToForm(this,webFormId) && $(this).is(":visible") && !$(this).parent().parent().parent().hasClass("optional"))
            {
                displayValidationErrorHeader(this);
                $(this).addClass("error");
                markValidationErrorLabel(webFormId, this);
                if(result)
                {
                    result = false;
                }
            }
        });
        if (!result){
            stopOtherEvents(event);
        }
        return result;
    }

    function getLocalattr(webFormId, localatter, publicAttr)
    {
        if($($(publicAttr)[0]).closest(".portlet-boundary_1_WAR_webformportlet_").attr("id") == webFormId)
        {
            localatter = $($(publicAttr)[0]);
        }
        else
        {
            if($(publicAttr).length > 1)
            {
                $(publicAttr).each(function (i)
                                   {
                                       if(i > 0)
                                       {
                                           localatter = $(getLocalattr(webFormId, localatter, $(publicAttr)[i]));
                                       }
                                   });
            }
        }
        return localatter;
    }

    function validatePrimaryIndustry(event,webFormId)
    {
        var result = true;
        var localIndustryDropDown = primaryIndustryDropDown;
        localIndustryDropDown = getLocalattr(webFormId,localIndustryDropDown, primaryIndustryDropDown);
        if(getSubjectValue(webFormId) == "Manufacturer Sales" && $.trim(getPrimaryIndustryValue(webFormId)) == "")
        {
            stopOtherEvents(event);
            displayValidationErrorHeader(localIndustryDropDown);
            $(localIndustryDropDown).addClass("error");
            markValidationErrorLabel(webFormId, localIndustryDropDown);
            result = false;
        }
        return result;
    }

    function subjectNotInFormWebFormId(webFormId){
        var localSubjectDropDown = subjectDropDown;
        localSubjectDropDown = getLocalattr(webFormId,localSubjectDropDown,subjectDropDown);
        return !($(localSubjectDropDown).closest(".portlet-boundary_1_WAR_webformportlet_").attr("id") == webFormId);
    }

    function validateOtherIndustryDesc(event,webFormId)
    {
        var result = true;
        var localOtherIndustryDescTextArea = otherIndustryDescTextArea;
        localOtherIndustryDescTextArea = getLocalattr(webFormId,localOtherIndustryDescTextArea,otherIndustryDescTextArea);
        if((getSubjectValue(webFormId) == "Manufacturer Sales" || subjectNotInFormWebFormId(webFormId)) && getPrimaryIndustryValue(webFormId) == "Other" && $.trim($(localOtherIndustryDescTextArea).val()) == "")
        {
            stopOtherEvents(event);
            displayValidationErrorHeader(localOtherIndustryDescTextArea);
            localOtherIndustryDescTextArea.addClass("error");
            markValidationErrorLabel(webFormId, localOtherIndustryDescTextArea);
            result = false;
        }
        return result;
    }

    function validateContactUsFields(event)
    {
        resetValidationErrors();
        var webFormId = $(event.currentTarget).closest(".portlet-boundary_1_WAR_webformportlet_").attr("id");
        var result1 = validateAllWebFormRequired(event,webFormId);
        var result2 = validatePrimaryIndustry(event,webFormId);
        var result3 = validateOtherIndustryDesc(event,webFormId);
        return (result1 && result2 && result3);
    }
};

