﻿loadGoogleTrackingJS();
window.onload = init;
var pageTracker;
var bVideoPlayed=false;
function init() {
    
    try {
        pageTracker = _gat._getTracker('UA-8299994-2');
        pageTracker._trackPageview();
        
    } catch(err) {}
}


$(document).ready(function() {
   fontResizer('1em','1.16em','1.33em');
   
   
   //reporting pdf
   $('a.reportingPDF').click(function() {
        googleEventTracker('PDF', '0-PDF-Recognizing_Flares', 'Download');
    });
   //just ask pdf
   $('a.justAskPDF').click(function() {
        googleEventTracker('PDF', '0-PDF-Start_UC_Dialogue', 'Download');
    });
   //understanding uc pdf
   $('a.understandingPDF').click(function() {
        googleEventTracker('PDF', '0-PDF-Understanding_UC', 'Download');
    });
   //resources pdf
   $('a.resourcesPDF').click(function() {
        googleEventTracker('PDF', '0-PDF-Resources_for_UC', 'Download');
    });
    
    //ccfaLink
    $('a.ccfaLink').click(function() {
        googleEventTracker('OffsiteLink', '1-CCFA-Presentation', 'Link');
    });
    //myIBD
    $('a.ibdLink').click(function() {
        googleEventTracker('OffsiteLink', '1-myibd.org', 'Link');
        
    });
   //professional site link
   $('a.lnkProfessionalSite').click(function() {
        googlePageView('/link/professional');
        googleEventTracker('OffsiteLink', '1-Lialda-Professional', 'Link');
        
		
    });
   //shire corporate site link
   $('a.lnkShireCorporate').click(function() {
        googlePageView('/link/shire.com');
        googleEventTracker('OffsiteLink', '1-Shire-Corporate', 'Link');
    });
   
   //inside story video
   $('a.healingstory').click(function() {
        googlePageView('/video/insidestory');
         //atlas tag tracker
        $.ajax({
			type: "POST",
			url: "/handlers/tag-tracker.ashx",
			data: "url=http://view.atdmt.com/action/dihshr_JUSTASK_092009_Healing_Story_Video_Watch",
			success: function(msg){
				
		    }
		});
		
		
		
    });
   
    $('a#videoSurveySubmit').click(function() {
        var pollDiv=$(this).parent().parent();
        var totalPolls=9;
        var x;
        var qDiv;
        var ansCount;
        var ansFound=false;
        for(x=1;x<=totalPolls;x++){
            if(pollDiv.find('#poll_q'+x).attr('id')==('poll_q'+x)){
                qDiv=pollDiv.find('#poll_q'+x);
                ansCount=0;
                nsFound=false;
                $(".poll_answer").each( function () {
                   if(this.checked == true && !ansFound){
			            ansFound=true;
			       }
                   if(!ansFound)ansCount++;                      
                });

                
                
                switch(x){
                    case 1:
                        googleEventTracker('Poll', 'report_symptoms', ansCount.toString());
                        break;
                    case 2:
                        googleEventTracker('Poll', 'uc_remission', ansCount.toString());
                        break;
                    case 3:
                        googleEventTracker('Poll', 'UC_Disruptions', ansCount.toString());
                        break;
                    case 4:
                        googleEventTracker('Poll', 'Overthecounter_meds', ansCount.toString());
                        break;
                    case 5:
                        googleEventTracker('Poll', 'mild_symptoms', ansCount.toString());
                        break;
                    case 6:
                        googleEventTracker('Poll', 'no_treatment', ansCount.toString());
                        break;
                    case 7:
                        googleEventTracker('Poll', 'complementary_therapy', ansCount.toString());
                        break;
                    case 8:
                        googleEventTracker('Poll', 'healthy_diet', ansCount.toString());
                        break;
                    case 9:
                        googleEventTracker('Poll', 'flare', ansCount.toString());
                        break;
                    
                }
                break;
            }
        }
        
        
        
        googlePageView('/tool/whatdoyouthink_submit');
        //atlas tag tracker
        $.ajax({
			type: "POST",
			url: "/handlers/tag-tracker.ashx",
			data: "url=http://view.atdmt.com/action/dihshr_JUSTASK_092009_What_Do_You_Think_SUBMIT",
			success: function(msg){
				
		    }
		});
        
    });
   preloadUtilHoverImgs(); //preloads the util hover images.
   
   //search functionality
   $('input#ctl00_objHeader_objHeaderLinks_searchText').click(function() {
        if($(this).attr('value')=='Find your answers here'){
            $(this).attr('value','');
        }
        
    });
    
    $('input#ctl00_objHeader_objHeaderLinks_searchText').blur(function(){	//test to see if the value of our search field is empty, then fill with our holding text
	    if($(this).attr('value') == ''){
		    $(this).attr('value', 'Find your answers here');
	    }
    });
    
    $('input#ctl00_objHeader_objHeaderLinks_searchText').keypress(function(e) {
        if(e.which==13){
            var sTerm=$('input#ctl00_objHeader_objHeaderLinks_searchText').val();
            doSearch(sTerm);
            return false;
        }
        
        
    });
    
    $('input#btnG').click(function() {
        searchFromAppliance();
        return false;
    });
    $('input#q').keypress(function(e) {
        if(e.which==13){
            searchFromAppliance();
            return false;
        }
        
    });
    
    //search button
    $('span.searchEnd').click(function() {
        var sTerm=$('input#ctl00_objHeader_objHeaderLinks_searchText').val();
        doSearch(sTerm);
        return false;
    });
    function doSearch(thisTerm){
        var searchTarget='/search.aspx?q='+thisTerm;
        window.location=searchTarget;
    }
    function searchFromAppliance(){
        var sTerm=$('input#q').val();
        doSearch(sTerm);
        
    }
    
    $('a.thickbox').click(function() {
        window.scrollTo(0,0);
    });
   
   //email a friend box
   $('img#email_submit').livequery('click',function() {
        //validate form
        var mainDiv=$(this).parent().parent().parent();
        var bValid=true;
        var emailFromFName,emailToFName,emailFromAddress,emailToAddress;
        
        //validate first name
        var fieldValue=mainDiv.find('#email_fname1').val();
        emailFromFName=fieldValue;
        if(fieldValue==''){
            mainDiv.find('#err_email_fname1').css('display','block');
            bValid=false;
        }
        else mainDiv.find('#err_email_fname1').css('display','none');
        
        //validate first name of to field
        var fieldValue=mainDiv.find('#email_fname2').val();
        emailToFName=fieldValue;
        if(fieldValue==''){
            mainDiv.find('#err_email_fname2').css('display','block');
            bValid=false;
        }
        else mainDiv.find('#err_email_fname2').css('display','none');
        
        //validate email of sender
        var fieldValue=mainDiv.find('#email_email1').val();
        emailFromAddress=fieldValue;
        if(fieldValue==''){
            mainDiv.find('#err_email_email1').css('display','block');
            mainDiv.find('#err_email_email1_invalid').css('display','none');
            bValid=false;
        }
        else if(!validateEmail(fieldValue)){
            mainDiv.find('#err_email_email1_invalid').css('display','block');
            mainDiv.find('#err_email_email1').css('display','none');
            bValid=false;
        }
        else{
            mainDiv.find('#err_email_email1').css('display','none');
            mainDiv.find('#err_email_email1_invalid').css('display','none');
        }
        
        //validate email of to field
        var fieldValue=mainDiv.find('#email_email2').val();
        emailToAddress=fieldValue;
        if(fieldValue==''){
            mainDiv.find('#err_email_email2').css('display','block');
            mainDiv.find('#err_email_email2_invalid').css('display','none');
            bValid=false;
        }
        else if(!validateEmail(fieldValue)){
            mainDiv.find('#err_email_email2_invalid').css('display','block');
            mainDiv.find('#err_email_email2').css('display','none');
            bValid=false;
        }
        else{
            mainDiv.find('#err_email_email2').css('display','none');
            mainDiv.find('#err_email_email2_invalid').css('display','none');
        }
        
        if(!bValid)return;
        mainDiv.addClass('flarehide');
        
        
         //determine what type of email to send
        var sendpage=mainDiv.find('#emailWhat_0').attr('checked');
        var sendtype=1; //sendtype=0 - send this page only; sendtype=1 - send entire site
        if(sendpage){
            sendtype=0;
        }
        
        //send the email w/ ajax call, show success div when the response is received
        $.ajax({
			type: "POST",
			url: "/handlers/email-a-friend.ashx",
			data: "sendtype="+sendtype+"&emailFromFName="+emailFromFName+"&emailToFName="+emailToFName+"&emailFromAddress="+emailFromAddress+"&emailToAddress="+emailToAddress+"&pageURL="+window.location.toString(),
			success: function(msg){
				mainDiv.parent().parent().find('div#ddg_email_confirmation').removeClass('flarehide');
                window.setTimeout('tb_remove();',5000);
		}
		});
        
        
        //$('div#ddg_emaildiv').removeClass('flareshow');
    });
    $('img#email_cancel').livequery('click',function() {
        tb_remove();
        
    });
   $('div#email_closebtn').livequery('click',function() {
        try{
            tb_remove();
        }
        catch(e){}
        
    });
    $('div#story_closebtn').livequery('click',function() {
        try{
            tb_remove();
        }
        catch(e){}
        
    });
    $('div#video_closebtn').livequery('click',function() {
        googlePageView('/video/close');
        try{
            tb_remove();
        }
        catch(e){}
        
    });
    //sidebar questions
    $('div.co_cta').click(function() {
        
        $(this).parent().find('div.co_question').addClass('hideElement');
        $(this).parent().find('div.co_answer').removeClass('hideElement');
        if($(this).parent().find('div.co_question').attr('class')=="co_question hideElement"){
            $(this).addClass('hideElement');
        }
    });
    
    
    
    //home page rollovers
    $('div#co').find('div.blue').mouseover(function() {
        swapHomeImages($('div.flare'));
        $('img#h_blue_co').attr('src','/images/ulcerative-colitis-flare-remission-over.png');
        
    });
    $('div#co').find('div.blue').mouseout(function() {
        swapHomeImages($('div.living'));
        $('img#h_blue_co').attr('src','/images/ulcerative-colitis-flare-remission.png');
        
    });
    
    $('div#co').find('div.green').mouseover(function() {
        swapHomeImages($('div.visit'));
        $('img#h_green_co').attr('src','/images/uc-doctor-visit-over.png');
    });
    $('div#co').find('div.green').mouseout(function() {
        swapHomeImages($('div.living'));
        $('img#h_green_co').attr('src','/images/uc-doctor-visit.png');
    });
    
    $('div#co').find('div.yellow').mouseover(function() {
        swapHomeImages($('div.experts'));
        $('img#h_yellow_co').attr('src','/images/uc-experts-over.png');
    });
    $('div#co').find('div.yellow').mouseout(function() {
        swapHomeImages($('div.living'));
        $('img#h_yellow_co').attr('src','/images/uc-experts.png');
    });
    
    $('div#co').find('div.pink').mouseover(function() {
        swapHomeImages($('div.affect'));
        $('img#h_pink_co').attr('src','/images/flare-ups-colon-over.png');
    });
    $('div#co').find('div.pink').mouseout(function() {
        swapHomeImages($('div.living'));
        $('img#h_pink_co').attr('src','/images/flare-ups-colon.png');
    });
    
    //clear home bkgd images and replace
    function swapHomeImages(showDiv){
        var bkgds=new Array($('div.living'),$('div.flare'),$('div.visit'),$('div.experts'),$('div.affect'));
        var x;
        for(x=0;x<bkgds.length;x++){
            if(showDiv.attr('class')!=bkgds[x].attr('class')){
                bkgds[x].css('display','none');
            }
            else{
                bkgds[x].css('display','block');
            }
        }
        
        
    }
    
    //video email div
    //email div
    $('img#video_email_btn').livequery('click',function() {
        $('div#video_divEmail').removeClass('hideElement');
        $('div#video_divEmail').addClass('showElement');
        $('div#videoContainer').hide();
        googlePageView('/video/healingstory_email');
        
    });
    $('img#video_email_cancel').livequery('click',function() {
        
        $('div#video_divEmail').removeClass('showElement');
        $('div#video_divEmail').addClass('hideElement');
        $('div#videoContainer').show();
    });
    
    
    $('div#video_email_closebtn').livequery('click',function() {
        $('div#video_divEmail').removeClass('showElement');
        $('div#video_divEmail').addClass('hideElement');
        $('div#videoContainer').show();
        
    });
    $('img#video_email_submit').livequery('click',function() {
        //validate form
        var mainDiv=$('div#video_divEmail');
        var bValid=true;
        var emailFromFName,emailToFName,emailFromAddress,emailToAddress;
        var sendPage;
        if($('#video_emailWhat_0').is(':checked')){
            sendPage=true;
        }
        else{
            sendPage=false;
        }
        
        
        //validate first name
        var fieldValue=mainDiv.find('#video_email_fname1').val();
        emailFromFName=fieldValue;
        if(fieldValue==''){
            mainDiv.find('#video_err_email_fname1').css('display','block');
            bValid=false;
        }
        else mainDiv.find('#video_err_email_fname1').css('display','none');
        
        //validate first name of to field
        var fieldValue=mainDiv.find('#video_email_fname2').val();
        emailToFName=fieldValue;
        if(fieldValue==''){
            mainDiv.find('#video_err_email_fname2').css('display','block');
            bValid=false;
        }
        else mainDiv.find('#video_err_email_fname2').css('display','none');
        
        //validate email of sender
        var fieldValue=mainDiv.find('#video_email_email1').val();
        emailFromAddress=fieldValue;
        if(fieldValue==''){
            mainDiv.find('#video_err_email_email1').css('display','block');
            mainDiv.find('#video_err_email_email1_invalid').css('display','none');
            bValid=false;
        }
        else if(!validateEmail(fieldValue)){
            mainDiv.find('#video_err_email_email1_invalid').css('display','block');
            mainDiv.find('#video_err_email_email1').css('display','none');
            bValid=false;
        }
        else{
            mainDiv.find('#video_err_email_email1').css('display','none');
            mainDiv.find('#video_err_email_email1_invalid').css('display','none');
        }
        
        //validate email of to field
        var fieldValue=mainDiv.find('#video_email_email2').val();
        emailToAddress=fieldValue;
        if(fieldValue==''){
            mainDiv.find('#video_err_email_email2').css('display','block');
            mainDiv.find('#video_err_email_email2_invalid').css('display','none');
            bValid=false;
        }
        else if(!validateEmail(fieldValue)){
            mainDiv.find('#video_err_email_email2_invalid').css('display','block');
            mainDiv.find('#video_err_email_email2').css('display','none');
            bValid=false;
        }
        else{
            mainDiv.find('#video_err_email_email2').css('display','none');
            mainDiv.find('#video_err_email_email2_invalid').css('display','none');
        }
        
        if(!bValid)return;
        var sendtype;
        if(sendPage){
            sendtype=0;
        }
        else{
            sendtype=1;
        }
        
        $.ajax({
			type: "POST",
			url: "/handlers/email-a-friend.ashx",
			data: "sendtype="+sendtype+"&emailFromFName="+emailFromFName+"&emailToFName="+emailToFName+"&emailFromAddress="+emailFromAddress+"&emailToAddress="+emailToAddress+"&pageURL="+window.location.toString(),
			success: function(msg){
				$('div#video_email_step1').addClass('hideElement');
                $('div#video_email_confirmation').removeClass('hideElement');
                $('div#video_email_confirmation').addClass('showElement');
                window.setTimeout('closeConfirmVideo()',5000);
                
		    }
		});
        
       
        
        googlePageView('/video/emailafriend');
        
        //$('div#ddg_emaildiv').removeClass('flareshow');
        
    });
    
    //registration form logic
    $('div#medications').find('input.medcheck').click(function(){
        if($(this).attr('name')=='chkLialda'){
            if($(this).is(':checked')){
            $('div#uselialda').removeClass('hideElement');
            }
            else $('div#uselialda').addClass('hideElement');
        };
    });
    $('select#condition').change(function(){

        if($(this).val()==1){
            $('div#haveuc').removeClass('hideElement');
        }
        else{
            $('div#haveuc').addClass('hideElement');
        }
    });
    //unsubscribe radio button logic
    
    $('input#ctl00_MainBodyPlaceHolder_unsub_0').click(function(){
        $('div#wrapper').css('min-height','450px');
        if($(this).attr('checked')){
            $('div#byemail').removeClass('hideElement');
            $('div#bydirect').addClass('hideElement');
            $('div#byall').addClass('hideElement');
            
        }
        else{
            $('div#byemail').addClass('hideElement');
        }
    });
    $('input#ctl00_MainBodyPlaceHolder_unsub_1').click(function(){
        $('div#wrapper').css('min-height','500px');
        if($(this).attr('checked')){
            $('div#bydirect').removeClass('hideElement');
            $('div#byemail').addClass('hideElement');
            $('div#byall').addClass('hideElement');
            
        }
        else{
            $('div#bydirect').addClass('hideElement');
        }
    });
    $('input#ctl00_MainBodyPlaceHolder_unsub_2').click(function(){
        $('div#wrapper').css('min-height','550px');
        if($(this).attr('checked')){
            $('div#byall').removeClass('hideElement');
            $('div#bydirect').addClass('hideElement');
            $('div#byemail').addClass('hideElement');
            
        }
        else{
            $('div#byall').addClass('hideElement');
        }
    });
    
    //form button rollover
    $('input.btn-submit').mouseover(function(){
        $(this).addClass('btn-submitOver');
    });
    $('input.btn-submit').mouseout(function(){
        $(this).removeClass('btn-submitOver');
    });
    
    
    
});
function closeConfirmVideo(){
    $('div#video_email_step1').removeClass('hideElement');
    $('div#video_email_confirmation').removeClass('showElement');
    $('div#video_emaildiv').removeClass('showElement');
    
    $('div#video_divEmail').removeClass('showElement');
    $('div#video_divEmail').addClass('hideElement');
    $('div#video_email_confirmation').addClass('hideElement');
    $('div#videoContainer').show();
}
//Preloads the utility toolbar hover images.
function preloadUtilHoverImgs() {
	preloadImg('/images/util-print-over.png', 92, 56);
	preloadImg('/images/util-email-over.png', 134, 56);
}

//Preloads an image.
function	preloadImg(path, w, h) {
	if (document.images) {
		objImg = new Image(w, h); 
		objImg.src = path;
	}
}

//Cancels event bubbling.
function stopPropogations(evt) {
	var e=(evt)?evt:window.event;
	if (window.event) {
		e.cancelBubble=true;
	} else {
		e.stopPropagation();
	}
}

//Shows/hides hover images on utility nav items.
function utilHover(id, mode) {
	//stopPropogations(e);
	if (id.length > 0) {
		if (document.getElementById(id)) {
			var elem = document.getElementById(id);
			elem.style.display = (mode == 1 ? 'block' : 'none');
		}
	}
}

function closeConfirm(){
    $('div#ddg_email_step1').removeClass('flarehide');
    $('div#ddg_email_confirmation').removeClass('flareshow');
    $('div#ddg_emaildiv').removeClass('flareshow');
}

//show hide unsubscribe

if (!document.getElementById)
{
if (document.all)
{
document.getElementById = function(p_id) { return document.all[p_id]; };
}
}


function hideElement()
{

for (var i = arguments.length - 1; i >= 0; --i)
{
if (document.getElementById(arguments[i]).className != "hideElement")
{
document.getElementById(arguments[i]).className = "hideElement";
}
}
}

function showElement()
{

for (var i = arguments.length - 1; i >= 0; --i)
{
if (document.getElementById(arguments[i]).className != "showElement")
{
document.getElementById(arguments[i]).className = "showElement";
}
}
}

function validateEmail(thisEmail){
    if(thisEmail.search(/^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$/) == -1){
        return false;
    }
    else return true;
}

function loadGoogleTrackingJS(){
    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
    document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
}

function googlePageTracker(id){
    //stopper for moa
    if(!bVideoPlayed)googleEventTracker('Inside Story', '0-Button Begin', 'Video');
    bVideoPlayed=true;
}
function googleEventTracker(id,sEvent,sType,sNumber){
    try {
        pageTracker._trackEvent(id,sEvent,sType,sNumber);
        
    } catch(err) {}
    
}
function flashTrackEvent(id,sEvent,sType,sNumber){
    try {
        pageTracker._trackEvent(id,sEvent,sType,sNumber);
        
    } catch(err) {}
    
}
function googlePageView(id){
    
     try {
        pageTracker._trackPageview(id);
        
    } catch(err) {}
}
function flashTrackPage(id){
    
     try {
        pageTracker._trackPageview(id);
        
    } catch(err) {}
}
function dartTagBuilder(id){
    var axel = Math.random()+"";
    var a = axel * 10000000000000;
    return id+a+'?';
}
