﻿jQuery(document).ready(function() {

    ShowProductGetCOAsLink(jQuery("#COA").html());
    //ShowGetCOAsLink(jQuery.query.get('Product_Code'));
});

function ShowProductGetCOAsLink(productcode) {
    ShowGetCOAsLink(productcode);
//    var xxx = {
//        type: "POST",
//        url: "/Webservices/ProductCOAs.asmx/ShowProductGetCOAsLink",
//        data: "{'productcode':'" + productcode + "'}",
//        contentType: "application/json; charset=utf-8",
//        dataType: "json",
//        success: function(msg) {
//            var showproduct = msg.d;
//            if (showproduct)
//                ShowGetCOAsLink(productcode);
//        },
//        error: function(XMLHttpRequest, textStatus, errorThrown) {
//            //do nothing
//        }
//    }
//    jQuery.ajax(xxx);

}

function ShowGetCOAsLink(productcode) {

    jQuery("#COADocsClose").unbind('click');
    jQuery("#COADocsClose").bind('click', function() {

        jQuery.unblockUI();

    });

    jQuery("#cCOALink").css({ 'display': 'block' });
    jQuery("#COALink").unbind('click');
    jQuery("#COALink").bind('click', function() {

        GetProductCOALinks(productcode);

        jQuery.blockUI({ message: $('#cCOADocs'), centerY: 0, css: { top: '10px', left: '', right: '10px', width: '270px'} });

    });

}

function GetProductCOALinks(productcode) {

    jQuery("#COADocs").html("loadings...");

    var xxx = {
        type: "POST",
        url: "/Webservices/ProductCOAs.asmx/GetProductCOALinks",
        data: "{'productcode':'" + productcode + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            GenerateProductCOALinks(productcode, msg.d);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            //do nothing
            alert("failed");
        }
    }
    jQuery.ajax(xxx);

}

function GenerateProductCOALinks(productcode, ArrayOfCOADocs) {

    jQuery("#COADocs").html("");

    var i = 0;
    var rowHtml = "";

    if (ArrayOfCOADocs.length == 0)
        jQuery("#COADocs").append("<div class='NoCOAsMessage'><span class='gTranslate'><h6>COA Documents</h6><b>" + productcode + "</b></span></div><hr class='hralt' noshade='noshade' size='1'><div class='NoCOAsMessage'><span class='gTranslate'><b>COAs for product " + productcode + " are currently unavailable.</b><br /><br />Please <a class='gTranslate' href='/Contact-Us/Default.aspx?subject=Certificate%20of%20Analysis%20" + productcode + "'>contact</a> an Aniara representative for further assistance.</span></div>");
    else {

        tableHtml = "<div class='NoCOAsMessage'><h6 class='gTranslate'>COA Documents</h6><b class='gTranslate'>" + productcode + "</b></div><hr class='hralt' noshade='noshade' size='1'>";
		tableHtml += "<table id='tableCOADocs'>";
        tableHtml += "<thead>";
        tableHtml += "<tr>";
        tableHtml += "<th class='LotHead'><span class='gTranslate'>Lot COA</span></th>";
        tableHtml += "<th class='ExpDateHead'><span class='gTranslate'>Exp. Date</span></th>";
        tableHtml += "<th class='Download'></th>";
        tableHtml += "</tr>";
        tableHtml += "</thead>";
        tableHtml += "<tbody>";
        for (i = 0; i < ArrayOfCOADocs.length; i++) {
            tableHtml += "<tr class='COADoc'>";
            tableHtml += "<td class='Lot'><span class='gTranslate'>" + ArrayOfCOADocs[i].ProductLot + "</span></td>";
            tableHtml += "<td class='ExpDate'><span class='gTranslate'>" + ArrayOfCOADocs[i].ExpYear + "-" + ArrayOfCOADocs[i].ExpMonth + "</span></td>";
            tableHtml += "<td class='Download'><span class='gTranslate'><a class='DownloadLink' target='_blank' href='/coa/" + ArrayOfCOADocs[i].FileName + "'>Download</a></span></td>";
            tableHtml += "</tr>";
        }

        tableHtml += "</tbody>";
        tableHtml += "</table>";
    
        jQuery("#COADocs").append(tableHtml);
        jQuery("#tableCOADocs").tablesorter({ sortList: [[1, 1]] });
        
    }
}
