﻿$(document).ready(function() {   
    $('.submit').mouseover(function() { $(this).attr('src', '/images/microsite/button-submit_roll.gif'); });
    $('.submit').mouseout(function() { $(this).attr('src', '/images/microsite/button-submit.gif'); });

    $('a.register').click(function() { window.open($(this).attr('href'), 'JHOLogin', 'location=no,menubar=no,resizable=yes,scrollbars=yes', false); return false; });

    //reset the form action to be the current page.  This is an issue with .net pre 3.5sp1 where aliases are not put into the form action.
    // this may cause conversion tracking issues
    $("form").attr("action", window.location);
    
    //This makes sure the functions inside pageLoad() get bound every time even after postbacks from updatepanels.
    //Sys.Application.add_load(pageLoad());
});

function pageLoad() {
    //This section is only valid during the time when CCH is down. Redirects users to a coming soon page.
    var $comingSoonOffice = $('#jho-comingsoon-office'), $comingSoonSignature = $('#jho-comingsoon-signature'),
    $comingSoonOnline = $('#jho-comingsoon-online'), $comingSoonHome = $('#jho-comingsoon-home'), $jhoThanks = $('#jho-thankYou');

//    $('.jhoRegComingSoon').click(function() {
//        window.location = '/coming-soon/';
//    });

    $('#commingsoonnav_office').click(function() {
        $comingSoonOffice.show();
        $comingSoonHome.hide();
    });

    $('#commingsoonnav_signature').click(function() {
        $comingSoonSignature.show();
        $comingSoonHome.hide();
    });

    $('#commingsoonnav_online').click(function() {
        $comingSoonOnline.show();
        $comingSoonHome.hide();
    });

    $('.commingsoonnav_home').click(function() {
        $comingSoonOffice.hide();
        $comingSoonOnline.hide();
        $comingSoonSignature.hide();
        $jhoThanks.hide();
        $comingSoonHome.show();
    });        
    //End coming soon
}

function comingSoonThankYou() {
    $('#jho-thankYou').show();
    $('#jho-comingsoon-online').hide();
}

function openShadowbox(elem, id) {   
    Shadowbox.open({
        content: $(elem).attr("href"),
        player: "iframe",
        width: 800
    });
}

function Validate() {
    
        Page_ClientValidate();
        if (Page_IsValid) {
            js_gaaw_track(); 
            return true;
        }
        else {
            return false;
        }
    }

    function pause(milliseconds) {
        var dt = new Date();
        while ((new Date()) - dt <= milliseconds) { /* Do nothing */ }
    }

    function get_array_key(target, path) {
        for (i = 0; i < target.length; i++) {
            if (target[i].indexOf(path) != -1) {
                return i;
            }
        }
    }

    function build_tracking() {

        var current_path = document.location.pathname;

        var adwords_registrations = new Array();
        adwords_registrations["main_registration"] = new Array('1029466651', 'dO80COfeugEQm9Tx6gM');
        adwords_registrations["landing_registration"] = new Array('1029466651', 'xSLuCMHfugEQm9Tx6gM');

        var destinations = new Array(
		'/start-for-free/ : main_registration : yahoo : fb',
		'/quick-and-easy/start-for-free/ : main_registration : yahoo : fb',
		'/life-events/start-for-free/ : main_registration : yahoo : fb',
		'/job-specific/start-for-free/ : main_registration : yahoo : fb',
		'/landing-life-events/ : landing_registration : yahoo : NONE',
		'/landing-job-specific/ : landing_registration : yahoo : NONE',
		'/landing-quick-and-easy/ : landing_registration : yahoo : NONE',
		'/landing-discount/ : landing_registration : yahoo : NONE',
		'/landing-efile/ : landing_registration : yahoo : NONE',
		'/landing-file-now/ : landing_registration : yahoo : NONE',
		'/walmart/start-for-free/ : NONE : NONE : NONE',
		'/home-depot/start-for-free/ : NONE : NONE : NONE',
		'/florida-education-association/start-for-free/ : NONE : NONE : NONE'
	);

        var current_tracking_info = destinations[get_array_key(destinations, current_path)].split(" : ");

        if (current_tracking_info[1] != 'NONE') {
            adwords_info = adwords_registrations[current_tracking_info[1]];
            aw_id = adwords_info[0];
            aw_label = adwords_info[1];
        } else {
            adwords_info = false;
        }

        if (current_tracking_info[2] != 'NONE') {
            window.ysm_customData = new Object();
            window.ysm_customData.conversion = "transId=" + current_path + ",currency=,amount=";
            ysm_accountid = "1120NJSDOGTBF924UH2O40R0B4G";
            yahoo_conversion = true;
        } else {
            yahoo_conversion = false;
        }

        if (current_tracking_info[3] != 'NONE') {
            fb_conversion = true;
        } else {
            fb_conversion = false;
        }

        ga_category = 'form';
        ga_action = 'submit';
        ga_label = current_path;

    }

    function js_gaaw_track() {
        build_tracking();
        pageTracker._trackEvent(ga_category, ga_action, ga_label);
        if (adwords_info != false) {
            image = new Image(1, 1);
            image.src = "http://www.googleadservices.com/pagead/conversion/" + aw_id + "/?label=" + aw_label + "&script=0";
        }
        if (yahoo_conversion != false) {
            $.getScript('http://srv2.wa.marketingsolutions.yahoo.com/script/ScriptServlet?aid=' + ysm_accountid);
        }
        if (fb_conversion != false) {
            $.getScript('http://ah8.facebook.com/js/conversions/tracking.js');
            try {
                FB.Insights.impression({
                    'id': 6002472049197,
                    'h': '73e4cbfe7a'
                });
            } catch (e) { }
        }
        pause(1000);
    }



    function initPopups() {
        initPopup({
            openEvent: 'click'
        });
        initPopup({
            popupHolderClass: 'popup-hover'
        });
    }
    
    if (window.addEventListener)
        window.addEventListener("load", initPopups, false);
    else if (window.attachEvent)
        window.attachEvent("onload", initPopups);

    function initPopup(_popup) {
        if (!_popup.popupHolderTag) _popup.popupHolderTag = 'li';
        if (!_popup.popupTag) _popup.popupTag = 'div';
        if (!_popup.popupHolderClass) _popup.popupHolderClass = 'popup-holder';
        if (!_popup.popupClass) _popup.popupClass = 'popup';
        if (!_popup.linkOpenClass) _popup.linkOpenClass = 'open';
        if (!_popup.linkCloseClass) _popup.linkCloseClass = 'close';
        if (!_popup.openClass) _popup.openClass = 'active';
        if (!_popup.openEvent) _popup.openEvent = 'hover';

        var timer = [];
        var _popupHolderTag = document.getElementsByTagName(_popup.popupHolderTag);
        if (_popupHolderTag) {
            for (var i = 0; i < _popupHolderTag.length; i++) {
                if (_popupHolderTag[i].className.indexOf(_popup.popupHolderClass) != -1) {
                    var _popupLink = _popupHolderTag[i].getElementsByTagName('a');
                    for (var j = 0; j < _popupLink.length; j++) {
                        _popupLink[j].parent = _popupHolderTag[i];
                        if (_popupLink[j].className.indexOf(_popup.linkOpenClass) != -1) {
                            if (_popup.openEvent == 'click') {
                                _popupLink[j].onclick = function() {
                                    if (this.parent.className.indexOf(_popup.openClass) != -1) {
                                        this.parent.className = this.parent.className.replace(_popup.openClass, '');
                                    } else {
                                        this.parent.className += ' ' + _popup.openClass;
                                    }
                                    return false;
                                }
                            } else {
                                var _popupTag = _popupHolderTag[i].getElementsByTagName(_popup.popupTag);
                                for (var k = 0; k < _popupTag.length; k++) {
                                    if (_popupTag[k].className.indexOf(_popup.popupClass) != -1) {
                                        _popupTag[k].parent = _popupHolderTag[i];
                                        _popupTag[k].onmouseover = function() {
                                            if (timer[j]) clearTimeout(timer[j]);
                                            if (this.parent.className.indexOf(_popup.openClass) == -1) {
                                                this.parent.className += ' ' + _popup.openClass;
                                            }
                                        }
                                        _popupTag[k].onmouseout = function() {
                                            var _this = this;
                                            timer[j] = setTimeout(function() {
                                                _this.parent.className = _this.parent.className.replace(_popup.openClass, '');
                                            }, 2);
                                        }
                                    }
                                }
                                _popupLink[j].onmouseover = function() {
                                    if (timer[j]) clearTimeout(timer[j]);
                                    if (this.parent.className.indexOf(_popup.openClass) == -1) {
                                        this.parent.className += ' ' + _popup.openClass;
                                    }
                                }
                                _popupLink[j].onmouseout = function() {
                                    var _this = this;
                                    timer[j] = setTimeout(function() {
                                        _this.parent.className = _this.parent.className.replace(_popup.openClass, '');
                                    }, 2);
                                }
                            }
                        } else if (_popupLink[j].className.indexOf(_popup.linkCloseClass) != -1) {
                            _popupLink[j].onclick = function() {
                                if (this.parent.className.indexOf(_popup.openClass) != -1) {
                                    this.parent.className = this.parent.className.replace(_popup.openClass, '');
                                } else {
                                    this.parent.className += ' ' + _popup.openClass;
                                }
                                return false;
                            }
                        }
                    }
                }
            }
        }
    }
