﻿/// <reference path="Jquery/jquery-1.2.6-intellisense.js" />

var isFirstResearchClick = true;
var questionHolder = {};
$(document).ready(function()
{
    var theForm = $('form');
    $('.emailTextBox').blur(verifyEmail);
    $('.newUserTextBox').blur(verifyUserName);
    questionHolder = $(theForm).find('.questionHolder')
    if (questionHolder.length > 0)
    {
        jQuery.cn.otherQuestions.init();
    }

    $('.researchInterests input').each(
        function addListeners(index, element)
        {
            $(element).click(
            function checkOtherElements()
            {
                //code is added as a quick validation fix for research interests (which usually is not a required field) 
                $("h4:contains('Research Interests')").css({ color: '#1371AA' });
                $('#reqAst').html(" *");
                $('#reqAst').css({ color: '#74CA0A' });
                //end research interest fix 
                if (isFirstResearchClick)
                {
                    $('.chkOptIn input').attr('checked', 'true');
                    isFirstResearchClick = false;
                }
            })
        })
    if (theForm.hasClass('formB'))
    {
        $($('.formRow')[0]).addClass('alternate');
    }
    if (theForm.hasClass('questions'))
    {
        $('.formRow:even').addClass('alternate');
    }
    $('.formG .imageCheckbox, .formH .imageCheckbox').click(function(event)
    {
        if (event.target.type !== 'checkbox')
        {
            $(':checkbox', this).trigger('click');
        }
    })
    $('.requestType').change(function(event)
    {
        if ($('.requestType option:selected').text() == "Get Quotation")
        {
            if (!theForm.hasClass('quotation'))
            {
                theForm.addClass('quotation');
            }
        }
        else
        {
            theForm.removeClass('quotation');
        }
    })
    $('.formG .subscriptions .main').click(function()
    {
        displaySubscriptionPreferences(false);
    })
    if ($('#aspnetForm').hasClass('loggedIn'))
    {
        $('.formG .subscriptions .main').toggleClass('closed');
    }
});

function displaySubscriptionPreferences(initial)
{
    $('.formG .subscriptions .main').toggleClass('closed');
    if (initial)
    {
        $('.subscriptions .content').slideToggle(0);
    }
    else
    {
        $('.subscriptions .content').slideToggle(600);
    }
}


function verifyEmail()
{
    //Assuming we use a static method we can use PageMethods
    //Otherwise we need a fully qualified web service call, e.g.:
    //WebSite.TestPages.WebServiceClassName.WebServiceMethodName(params);
    var emailtext = $('.emailTextBox').val();
    if (emailtext !== '')
    {
        PageMethods.CheckEmail(emailtext, OnSucceeded, OnFailed);
    }

}

function verifyUserName()
{
    //Assuming we use a static method we can use PageMethods
    //Otherwise we need a fully qualified web service call, e.g.:
    //WebSite.TestPages.WebServiceClassName.WebServiceMethodName(params);
    var userNameText = $('.newUserTextBox').val();
    if (userNameText !== '')
    {
        PageMethods.CheckUserName(userNameText, OnSucceeded, OnFailed);
    }
}

//Onsucceeded and onfailed. Potential add an OnTimeout too
function OnSucceeded(result, userContext, methodName)
{
    if (methodName == "CheckEmail")
    {
        if (result != '0')
        {
            $('#divExistingEmail').css({ display: "block" });
            jQuery.cn.switchAjaxHints(jQuery('#divExistingEmail').parents('.formRow'), true);
            jQuery.cn.switchValidRow($('#divExistingEmail').parents('.formRow'), false);
            $('.newUserTextBox').val("");
        } else
        {
            jQuery.cn.switchAjaxHints(jQuery('#divExistingEmail').parents('.formRow'), false);
            jQuery.cn.switchValidRow($('#divExistingEmail').parents('.formRow'), true);
            jQuery.cn.toggleHelper($('#divExistingEmail').parents('.formRow'), false);
            //This email does not exist in the system, so we enter it 
            //in the new user textbox below
            var emailtext = $('.emailTextBox').val();
            $('.newUserTextBox').val(emailtext + "");
        }
    }
    else if (methodName == "CheckUserName")
    {
        if (result != '0')
        {
            jQuery.cn.switchAjaxHints(jQuery('#divExistingUser').parents('.formRow'), true);
            jQuery.cn.switchValidRow($('#divExistingUser').parents('.formRow'), false);
        }
        else
        {
            jQuery.cn.switchAjaxHints(jQuery('#divExistingUser').parents('.formRow'), false);
            jQuery.cn.switchValidRow($('#divExistingUser').parents('.formRow'), true);
        }
    }
}
function OnFailed(error, userContext, methodName)
{
    if (error !== null)
    {
        displayElement.innerHTML = "An error occurred: " +
				error.get_message();
    }
}

function NoContactConfirm(chkBox)
{
    if (chkBox.checked == true)
    {
        if (!confirm("Please note that if you check this box the product information you requested may not be delivered to you."))
        {
            chkBox.checked = false;
        }
    }
}
jQuery.cn.otherQuestions =
{
    rbListWithOthers: {},



    init: function()
    {
        jQuery.cn.otherQuestions.rbListWithOther = questionHolder.find('span.other').parents('.formRow');
        jQuery.cn.otherQuestions.rbListWithOther.each(function(i, item)
        {
            $(item).data('otherClicked', false);
            $(item).find(':radio').bind('click', item, jQuery.cn.otherQuestions.onRBClick);
            $(item).find('input:text').bind('focus blur', item, jQuery.cn.otherQuestions.onInputEvent);
        })

    },
    onRBClick: function(event)
    {

        var rbList = $(event.data);
        if ($(event.currentTarget).parents('span').hasClass('other'))
        {
            rbList.data('otherSelected', true);
            rbList.find('input:text').focus();
        }
        else
        {
            rbList.data('otherSelected', false);
            rbList.find('.validationHolder span')[0].isvalid = true;
            rbList.removeClass('invalid');
        }
    },
    onInputEvent: function(event)
    {
        var rbList = $(event.data);
        if (event.type == 'focus')
        {
            rbList.find('span.other :radio').attr('checked', 'true');
            rbList.data('otherSelected', true);
        }
        if (event.type == 'blur')
        {
            if ($(event.currentTarget).val() == '' && rbList.data('otherSelected') == true)
            {
                rbList.find('.validationHolder span')[0].isvalid = false;
                rbList.find('span.other :radio').removeAttr('checked');

            }
            else if ($(event.currentTarget).val() != '' && rbList.data('otherSelected') == true)
            {
                rbList.find('.validationHolder span')[0].isvalid = true;
            }
        }
    }
}

