﻿function PrintCoupon()
{
    var img = new Image();
    var index = document.getElementById('ctl00_pageContent_selectedIndex').value;
                       
    ShowHideDiv('#printer' + index);               
    ShowHideDiv('#loader' + index);

    $(img)
    // once the image has loaded, execute this code
    .load(function () {
        // then insert our image
        $('#couponDiv').append(this);
        
        ShowHideDiv('#loader' + index);
        ShowHideDiv('#printer' + index);               
        
        setTimeout("window.print()" , 500);
    })

    .error(function () {
        ShowHideDiv('#loader' + index);
        ShowHideDiv('#printer' + index); 
        
        setTimeout("alert('An error has occurred with the printing of your coupon. Please try again later.')", 500); 
    })

    // *finally*, set the src attribute of the new image to our image
    .attr('src', 'coupon_render.aspx?id=' + Math.floor(Math.random()*9999));
}

function ShowHideDiv(id)
{
    if ($(id).css('display') == 'none') {
        $(id).css('display', 'block');
    } else {
        $(id).css('display', 'none');
    }
}