function setCookie(c_name,value,expiredays) {
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" + escape(value) + ((expiredays==null) ? "" : ";expires="+exdate.toGMTString()) + ";path=/";
}

function getCookie(c_name) {
    if (document.cookie.length>0) {
        c_start=document.cookie.indexOf(c_name + "=");
        if (c_start!=-1) {
            c_start=c_start + c_name.length+1;
            c_end=document.cookie.indexOf(";",c_start);
            if (c_end==-1) c_end=document.cookie.length;
            return unescape(document.cookie.substring(c_start,c_end));
        }
    }
    return "";
}
        
var _HOVER_INTERVAL = null;
var _COUNTDOWN;

function loadBox(caption, url) {
    
    var cookie = getCookie('offerBox');
    if(cookie == '') {
        $(document).ready(function () {
            var interval = 1; //sec
            var max_time = 120;
            _COUNTDOWN = max_time;
            $('div.timer').html('02:00 left');
            var _interval = setInterval(function () {
                var t = _COUNTDOWN -  interval;
                if(t > 0) {
                    var minutes = Math.floor(t / 60);
                    var seconds = t - (minutes * 60);
                    $('div.timer').html('0'+minutes+':'+(seconds>9?'':'0')+seconds + ' left');
                    _COUNTDOWN = t;
                } else {
                    clearInterval(_interval);
                    TB_remove();
                }
            }
            , interval * 1000);
        }
        );

        TB_show(caption, url);
        $("#TB_ajaxContent span.dynamic-phone-number").html($('span.dynamic-phone-number:eq(0)').html());
        _HOVER_INTERVAL = setInterval(buttonHover, 100);
        setCookie('offerBox', 'true', 999)
    }
}

function buttonHover() {
    if(document.getElementById('TB_window')) {
        $("#TB_window button#offerSubmit").hover(
            function() {
                $(this).children("img").attr("src","/images/offer_submit_hover.png");
            },
            function() {
                $(this).children("img").attr("src","/images/offer_submit.png");
        });
        clearInterval(_HOVER_INTERVAL);
    }
}

function submitObForm() {
    var name = $('#TB_window #ob_name');
    var phone = $('#TB_window #ob_phone');
    var email = $('#TB_window #ob_email');
    var errorMessage = "";
    if(name.attr('value') == "" || name.attr('value') == undefined) {
        errorMessage += "Name Cannot Be Blank \n";
    }
    if(phone.attr('value') == "" || phone.attr('value') == undefined) {
        errorMessage += "Phone Cannot Be Blank \n";
    }
    if(email.attr('value') == "" || email.attr('value') == undefined) {
        errorMessage += "Email Cannot Be Blank \n";
    }
    
    if(errorMessage == "") {
        $.post('/scripts/post_lead.php?rand='+Math.random().toString(), {
            l_fname: name.attr('value'),
            email_addr: email.attr('value'),
            l_phone1: phone.attr('value'),
            key3: 'offerBox',
            auto: '1'
        }, function (oJson) {
            if(oJson.success == undefined) {
                if(oJson.errors.length > 0){
                    alert(oJson.errors);
                }
            } else {
                if(oJson.success == true) {
                    window.location = "http://www.protectamerica.com/pa/home/thank_you_lsub";
                }
            }
        }, 'json');
    } else {
        alert(errorMessage);
    }
}     

$(document).ready(function() {
    $("html").mousemove(function(e) {
        if(e.clientY < 4) {
            loadBox('Internet Special', '#TB_inline?height=470&width=610&inlineId=popUp');
        }
    })
});

setTimeout("loadBox('Internet Special', '#TB_inline?height=470&width=610&inlineId=popUp');", 45000);