var show = 1;

if (Browser.Engine.webkit === true && navigator.userAgent.toLowerCase().indexOf('chrome') > -1) {
    var text = "Press 'Stay on this Page' for details on how to\nFile For Grants!";
} else {
    var text = "Press 'CANCEL' for details on how to\nFile For Grants!";
}

var alertMsg = "WAIT!\nAre You Ready to Start Filing For Grants?";

function dontLeaveUs() {
    if (show == 1) {
        var box = document.getSize(), scroll = document.getScroll();
		if (Browser.Engine.trident == true) {
            var size = {x: '650', y: '490'};
        } else {
            var size = {x: '640', y: '480'};
        }
		var to = {
			width: size.x,
			height: size.y,
			left: (scroll.x + (box.x - size.x - 20) / 2).toInt(),
			top: (scroll.y + (box.y - size.y - 20) / 2).toInt()
		};
		
        document.id('onleave-wrapper').setStyle('display', '');
        document.id('sbox-overlay').setStyles({'width': document.getSize().x, 'height': document.getSize().y});
        document.id('sbox-window').setStyles(to);

        SqueezeBox.open(document.id('onleave-offer'), {
            'handler': 'adopt',
            'size': size,
            'onOpen': function(){
                document.id('onleave-wrapper').destroy();
            }
        });

        alert(alertMsg);

        show = 0;

        return text;
    } else {
        return;
    }
}

window.addEvent('domready', function(){
    if (document.id('onleave-offer')) {
        SqueezeBox.initialize({'closable': false, 'closeBtn': false});
        if (Browser.Engine.preto === true) {
            window.onunload = dontLeaveUs;
        } else {
            window.onbeforeunload = dontLeaveUs;
        }

    }

    var validation = new FormCheck('main-form', {
        display : {
            errorsLocation : 1,
            indicateErrors : 1,
            showErrors: 0,
            keepFocusOnError: 0,
            checkValueIfEmpty: 1,
            tipsPosition: 'right',
            flashTips : true,
            fadeDuration : 500
        },
        'onValidateSuccess': function(){
            showExit = 0;
        }
    });

    if (Browser.Engine.trident === true) {
        if (document.id('submit-button')) {
            document.id('submit-button').addEvent('click', function(){
                if (validation.isFormValid()) {
                    showExit = 0;
                    document.id('main-form').submit();
                } else {
                    document.id('main-form').fireEvent('submit');
                }
            });
        }
    }
});

function popUp(URL) {
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=400,height=300,left = 500,top = 500');");
}
