function openPIPopup(whichURL) {
    window.open('/pdf/Tamiflu-PPI.pdf', 'pi', 'width=700,height=500,top=16,left=16');
    window.open(whichURL, 'tamiflu', 'width=700,height=500,top=0,left=0');
}

function isCouponSurveyComplete() {
    var surveyCompleted = true;

    if ($('input[name="firstAnswer"]:checked').length <= 0) {
        surveyCompleted = false;
    }

    if ($('input[name="secondAnswer"]:checked').length <= 0) {
        surveyCompleted = false;
    }

    if ($('input[name="thirdAnswer"]:checked').length <= 0) {
        surveyCompleted = false;
    }

    return surveyCompleted;
}


$(document).ready(function () {

    /***** Custom Datepicker function *****/
    if ($('#datepicker').length > 0) {
        // Using jQuery UI
        $("#datepicker").datepicker({ maxDate:'0d', showAnim:'slide', showOn:'both', buttonImage:'/images/get/datepicker-calendar-icon.png',
            buttonImageOnly:true });
    }

    /***** Custom Lightbox functions *****/
    if ($("a.ajaxSpanish").length > 0) {
        $(".ajaxSpanish").hoverIntent(function () {
                $.colorbox({
                    href:"/IndicationsSafetySpanish.html",
                    title:"Información de Seguridad y Efectos Secundarios",
                    overlayClose:false
                });
                safetyRollover($(this)[0]);
            },
            function () {
            }
        );
    } else {
        $(".ajax").hoverIntent(function () {
                $.colorbox({
                    href:"/safetyOverlay.html",
                    title:"Indications and Important Safety Information",
                    overlayClose:false
                });
                safetyRollover($(this)[0]);
            },
            function () {
            }
        );

    }

    /******** Popup window for PDF downloads ********/
        // popup the prescribing information PDF on any PDF download from the site
    $('a[href$=".pdf"]').not('a[href$="pi.pdf"]').not('a[href$="ppi.pdf"]').not('a[href$="_coupon.pdf"]').click(function (e) {
        var thisUrl = $(this).attr('href');
        openPIPopup(thisUrl);
        e.preventDefault();
    });

    /******** Tracking for Google Analytics ********/
    $('a[href^="http://www.gene.com/"]').not('a[href$=".pdf"]').click(function () {
        _gaq.push(['_trackEvent', 'Exit Link', 'outbound Link', '/link/gene']);
    });

    $('a[href^="http://www.genentech.com"]').click(function () {
        _gaq.push(['_trackEvent', 'Exit Link', 'outbound Link', '/link/genentech']);
    });

    $('a[href$="/tamiflu/pdf/pi.pdf"]').click(function () {
        _gaq.push(['_trackEvent', 'Download', 'Tamiflu-dosing-pdf']);
    });

    $('a[href^="http://www.rocheusa.com/"]').click(function () {
        _gaq.push(['_trackEvent', 'Exit Link', 'outbound Link', '/link/rocheusa']);
    });


    /******** Custom Radio Buttons function ********/
    /*if ($('.optionBox').length > 0) {
     // add a class so that we know that javascript is active
     $('.optionBox').addClass('js');

     // get the current state of the form
     $('.optionBox input[type=radio]:checked').parent(".optionBox").addClass("checked");

     $('.optionBox').click(function(e) {
     $('.optionBox input[type=radio]').removeAttr("checked");
     $('.optionBox').removeClass("checked");
     $(this).children('input[type=radio]').attr("checked", "checked");
     $(this).addClass("checked");
     });
     } */

    /***** Custom Check Box function *****/
    if ($('.symptomsSelector').length > 0) {
        $('.symptomsSelector input').customInput();
    }

    if ($('.exposureSelector').length > 0) {
        $('.exposureSelector input').customInput();
        $('.exposureSelector input[type=radio]').removeAttr("checked");
        $('.exposureSelector label').removeClass("checked");
    }

    if ($('.customSurvey').length > 0) {
        $('.customSurvey input').customInput();
        $('.customSurvey input[type=radio]').removeAttr("checked");
        $('.customSurvey label').removeClass("checked");
    }

    /*****  Custom Dropdown function *****/
    if ($('div.jqtransform').length > 0) {
        $("div.jqtransform").jqTransform();
    }


    $("#search-field").focus(function () {
        if ($("#search-field").val() == "Search Tamiflu") {
            $("#search-field").val("")
        }
    });


    if ($(".mainnav").length > 0) {
        $("a.menuPrimary").hover(function () {
            //console.log('You clicked a main link.');
            //$(".activeMain").removeClass("activeMain");
            $(this).addClass("activeMain");
        });
    }

    /***** Disable the coupon survey submit button until the survey is complete *****/
    if ($('#couponform').length > 0) {
        if (isCouponSurveyComplete()) {
            $('#couponform input[type="submit"]').removeClass('disabled')
        }

        $('input[type="radio"]').change(function (e) {
            if (isCouponSurveyComplete()) {
                $('#couponform input[type="submit"]').removeClass('disabled')
            }
        });

        $('#couponform').delegate('input[type="submit"].disabled', 'click', function (e) {
            e.preventDefault();
        });


    }


    // tracking for portlets
    $('form#fluTrackerPortlet').submit(function (e) {
        var form = this;
        e.preventDefault();

        trackTool('Flu Tracker');

        setTimeout(function () {
            form.submit();
        }, 500); // in milliseconds
    });

    $('form#fluAlertsPortlet').submit(function (e) {
        var form = this;
        e.preventDefault();

        omniLinkClick('Flu Alerts Step 1', 'Bottom Bar');

        setTimeout(function () {
            form.submit();
        }, 500); // in milliseconds
    });

    $('form#fluAlertsSidebarPortlet').submit(function (e) {
        var form = this;
        e.preventDefault();

        omniLinkClick('Flu Alerts Step 1', 'Sidebar');

        setTimeout(function () {
            form.submit();
        }, 500); // in milliseconds
    });

});

$(window).bind("load", function () {

    if ($('#fluTrack').length > 0) {
        var locationVar = getUrlVars()["zipcode"];

        if (locationVar == 'Enter+ZIP+code' || locationVar == undefined) {
            $('#fluTrack .box').val('');
        } else {
            $('#fluTrack .box').val(locationVar);
            $('#fluTrack').submit();
        }
    }

    if ($('#fluAlertsForm').length > 0) {
        emailVal = $('#fluAlertsForm #email').attr('value');
        if (emailVal == "Email address") {
            $('#fluAlertsForm #email').val('');
        }
    }

    <!-- error tracking is done by looking at the formErrors div earlier in the page -->
    // wait for jquery to be loaded before firing the event
    function jqueryLoaded() {
        $(function () {
            var numberOfErrors = $("#formErrors li").length;

            if (numberOfErrors > 0) {
                var lastError = $("#formErrors li:last").text();

                trackFormError(numberOfErrors, lastError, "flualerts");
            }
        });
    }

    // we repeatedly check for jquery until it's loaded
    function checkJquery() {
        if (window.jQuery && jQuery.ui) {
            jqueryLoaded();
        } else {
            window.setTimeout(checkJquery, 100);
        }
    }

    checkJquery();

});

