﻿var Omega = {

    couponcookie:function() {
        if (document.cookie.length > 0 && document.cookie.indexOf("OMP.Coupon.Shown") > -1)
          return true;
        else
        {
            var exd = new Date(); exd.setDate(exd.getDate()+7);
            document.cookie = "OMP.Coupon.Shown="+exd.toUTCString()+";expires="+exd.toUTCString();
        }        
        return false;
    },

    WebService: {
        FwdResearchViaEmail: function(researchId) {
            var data = Array();
            $('#divSendResearchToForm input, #divSendResearchToForm textarea').each(function(i) { data[i] = $(this).val(); });

            if (data[1].length < 8 || data[2].length < 2 || data[1].indexOf('@') < 1 || data[2].indexOf('@') < 1) {
                if ($('#divSendResearchToForm #e').length == 0)
                    $('<span id=\"e\" style=\"color:#cc0000\">Please enter a valid email address for both you and the recipient</span>').insertBefore('#divSendResearchToForm div.research-fwd-form table');
                return;
            }

            WebService.FwdResearchViaEmail(researchId, window.location.href, data[0], data[1], data[2], data[3], Omega.WebService._fwdresearchviaemail_oncomplete, _MSAjax_OnError, _MSAjax_OnTimeOut);
        },
        _fwdresearchviaemail_oncomplete: function(data) {
            var o = eval('(' + data + ')');
            if (o.success) {
                $('#divSendResearchToForm div.research-fwd-form')
	                .html("<span style='font-size:8pt'>You have successfully forwarded the research article <b>\"" + o.title + "\"</b> to your associate. Thanks for your interest in Omega Protein animal nutrition research.</span>");
            }
        },
        AddToCart: function(lineItemId, sizeId, refId) {
            var qty = $('#txtQty-' + sizeId).val();
            if (parseInt(qty) > 0 && qty < 1000)
                WebService.AddToCart(lineItemId, sizeId, qty, refId, Omega.WebService._addtocart_oncomplete, _MSAjax_OnError, _MSAjax_OnTimeOut);
        },
        _addtocart_oncomplete: function(data) {
            var o = eval('(' + data + ')');
            if (o.success) {
                $('#' + o.refCtrl).parents('table.sizes').next('div.tq').find('input').val(o.total);
            }
        },
        CalculateFreight: function(postalcode) {
            if (postalcode.match(/([0-9]{5}|[0-9a-z]{6}|[0-9a-z ]{7})/i))
                WebService.CalculateFreight(postalcode, Omega.WebService._calculatefrieght_oncomplete, _MSAjax_OnError, _MSAjax_OnTimeOut);
        },
        _calculatefrieght_oncomplete: function(data) {
            var o = eval('(' + data + ')');
            if (o.success) { $('td#tdEF').html("<strong>$ " + o.rate + "</strong>"); $('span#spnEF').show(); }
            else { $('td#tdEF').html(''); $('span#spnEF').hide(); }
        }
    }
}

function _MSAjax_OnComplete(arg)
{
    //alert(arg);
    return true;
}
function _MSAjax_OnError(arg)
{
    alert("error has occured: " + arg.get_message());
}
function _MSAjax_OnTimeOut(arg)
{
   alert("timeout has occured");
}