<?php
use Application\Session;
if(Session::get('steps.current.pageType')=='leadPage-abc' && !Session::has('ev_pixel_check.leadPage'))
{
?>
<script>
var fb_id = '<?php echo Session::get('queryParams.c4') ?>';
if(fb_id!='')
{
    !function(f,b,e,v,n,t,s)
    {if(f.fbq)return;n=f.fbq=function(){n.callMethod?
    n.callMethod.apply(n,arguments):n.queue.push(arguments)};
    if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
    n.queue=[];t=b.createElement(e);t.async=!0;
    t.src=v;s=b.getElementsByTagName(e)[0];
    s.parentNode.insertBefore(t,s)}(window, document,'script',
    'https://connect.facebook.net/en_US/fbevents.js');
    fbq('init', '<?php echo Session::get('queryParams.c5') ?>');
    fbq.disablePushState = true;
    window.fbq('track', 'ViewContent');
}
</script>

<?php
    Session::set('ev_pixel_check.leadPage',1);
}
if(Session::get('steps.current.pageType')=='checkoutPage-abc' && !Session::has('ev_pixel_check.checkoutPage'))
{
?>
<script>
var fb_id = '<?php echo Session::get('queryParams.c4') ?>';
if(fb_id!='')
{
    !function(f,b,e,v,n,t,s)
    {if(f.fbq)return;n=f.fbq=function(){n.callMethod?
    n.callMethod.apply(n,arguments):n.queue.push(arguments)};
    if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
    n.queue=[];t=b.createElement(e);t.async=!0;
    t.src=v;s=b.getElementsByTagName(e)[0];
    s.parentNode.insertBefore(t,s)}(window, document,'script',
    'https://connect.facebook.net/en_US/fbevents.js');
    fbq('init', '<?php echo Session::get('queryParams.c5') ?>');
    fbq.disablePushState = true;
    window.fbq('track', 'InitiateCheckout');
}
</script>

<?php
    Session::set('ev_pixel_check.checkoutPage',1);
}
if(Session::get('steps.current.pageType')=='thankyouPage' && !Session::has('ev_pixel_check.thankyouPage') && Session::has('steps.1.orderId'))
// if(Session::get('steps.current.pageType')=='thankyouPage')
{
?>
<script>
    var ev_pixel_check;
    var ev_gen_click = EF.getTransactionId(EF.urlParameter('oid'));
    setTimeout(function(){
        const xmlhttp = new XMLHttpRequest();
        xmlhttp.onload = function () {
            ev_pixel_check = JSON.parse(this.responseText);
        }
        xmlhttp.open("POST", "https://www.exclusiverightamericans.com/ev/check.php", true);
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlhttp.send("click_id="+ev_gen_click);
    }, 3000);
    var check_time = setInterval(function(){
        if (ev_pixel_check !== undefined) {
            if (ev_pixel_check.resonse.status) {
                //facebook
                var fb_id = '<?php echo Session::get('queryParams.c4') ?>';
                if(fb_id!='' && (fb_id!='6322356061152152' || fb_id!='164632293141926'))
                {
                sessionStorage.setItem('fb_fire',1);
                    !function(f,b,e,v,n,t,s)
                    {if(f.fbq)return;n=f.fbq=function(){n.callMethod?
                    n.callMethod.apply(n,arguments):n.queue.push(arguments)};
                    if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
                    n.queue=[];t=b.createElement(e);t.async=!0;
                    t.src=v;s=b.getElementsByTagName(e)[0];
                    s.parentNode.insertBefore(t,s)}(window, document,'script',
                    'https://connect.facebook.net/en_US/fbevents.js');
                    fbq('init', '<?php echo Session::get('queryParams.c4') ?>');
                    fbq.disablePushState = true;
                    window.fbq('track', 'Purchase',{value: '<?php echo Session::get('steps.1.products.0.productPrice') + Session::get('steps.1.products.0.shippingPrice') ?>', currency: 'USD'});
                }
            }
            clearInterval(check_time)
        }
    }, 1000);
</script>
<?php
    Session::set('ev_pixel_check.thankyouPage',1);
}
?>