function Get_Cookie(name) {
   var start = document.cookie.indexOf(name+"=");
   var len = start+name.length+1;
   if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
   if (start == -1) return null;
   var end = document.cookie.indexOf(";",len);
   if (end == -1) end = document.cookie.length;
   return unescape(document.cookie.substring(len,end));
}

function Set_Cookie(name, value) {
    var domain = "";
    if (top.location.toString().toLowerCase().indexOf('testout.com') > -1)
        domain = "; domain=testout.com";
    if (top.location.toString().toLowerCase().indexOf('labsimonline.com') > -1)
        domain = "; domain=labsimonline.com";
    var cookieString = name + "=" + escape(value) + domain + "; path=/";
    document.cookie = cookieString;
}

function S4() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}
function guid() {
    return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}

function AddToCart(productId) {
    Set_Cookie('mercatusContinuePage', top.location);
    Set_Cookie('mercatusAddProductId', guid() + '|' + productId);
    var mercatusStatusFrame = document.getElementById('frameMercatusStatus');
    var url = top.location;
    if (url.toString().toLowerCase().indexOf('http://testout.com') == 0 || url.toString().toLowerCase().indexOf('http://www.testout.com') == 0)
        top.location = 'https://www.testout.com/viewcart.htm';
    else if (url.toString().toLowerCase().indexOf('http://labsimonline.com') == 0 || url.toString().toLowerCase().indexOf('http://www.labsimonline.com') == 0)
        top.location = 'https://www.labsimonline.com/viewcart.htm';
    else top.location = '/viewcart.htm';
}

function AddToCartWithKeyCode(productId, keycode) {
    Set_Cookie('mercatusContinuePage', top.location);
    Set_Cookie('mercatusAddProductId', guid() + '|' + productId);
    Set_Cookie('mercatusAddKeyCode', keycode);
    var mercatusStatusFrame = document.getElementById('frameMercatusStatus');
    var url = top.location;
    if (url.toString().toLowerCase().indexOf('http://testout.com') == 0 || url.toString().toLowerCase().indexOf('http://www.testout.com') == 0)
        top.location = 'https://www.testout.com/viewcart.htm';
    else if (url.toString().toLowerCase().indexOf('http://labsimonline.com') == 0 || url.toString().toLowerCase().indexOf('http://www.labsimonline.com') == 0)
        top.location = 'https://www.labsimonline.com/viewcart.htm';
    else top.location = '/viewcart.htm';
}

var iframeSrc = '/Mercatus/CartStatus.aspx';
var seperator = '?';
var priceCount = 1;
var priceSpan = document.getElementById('price' + priceCount);
while (priceSpan) {
    iframeSrc = iframeSrc + seperator + 'price' + priceCount + "=" + priceSpan.innerHTML;
    seperator = '&';
    priceCount++;
    priceSpan = document.getElementById('price' + priceCount);
}

var cartStatus = document.getElementById('cartStatus');
if (cartStatus != null)
    cartStatus.innerHTML = '<iframe id="frameMercatusStatus" src="' + iframeSrc + '" width="112" height="36" frameborder="0" allowtransparency="true", scrolling="no"></iframe>';

var cartView = document.getElementById('cartView');
if (cartView != null) {
    cartView.innerHTML = '<iframe id="frameMercatusCart" width="750" height="825" frameborder="0" allowtransparency="true", scrolling="auto"></iframe>';
    var src;
    var charPos = window.location.href.indexOf('?')
    if (charPos == -1)
        src = '/Mercatus/Cart.aspx';
    else {
        var qry = window.location.href.substring(charPos);
        switch (qry) {
            case '?getUpdates':
                src = '/Mercatus/GetUpdates.aspx';
                break;
            case '?acctInfo':
                src = '/Mercatus/AcctInfo.aspx';
                break;
            case '?addrInfo':
                src = '/Mercatus/AddrInfo.aspx';
                break;
            case '?shipMethod':
                src = '/Mercatus/ShipMethod.aspx';
                break;
            case '?miscInfo':
                src = '/Mercatus/MiscInfo.aspx';
                break;
            case '?paymentInfo':
                src = '/Mercatus/PaymentInfo.aspx';
                break;
            case '?orderComplete':
                src = '/Mercatus/OrderComplete.aspx';
                break;
            case '?onlineFulfillInfo':
                src = '/Mercatus/OnlineFulfillInfo.aspx';
                break;
        }
    }
    var mercatusCartFrame = document.getElementById('frameMercatusCart');
    mercatusCartFrame.src = src;
}

var cartBuyNow = document.getElementById('cartBuyNow');
if (cartBuyNow != null) {
    var productId = cartBuyNow.innerText;
    if (productId == undefined)
        productId = cartBuyNow.textContent;
    cartBuyNow.innerHTML = '<iframe id="frameMercatusBuyNow" src="/Mercatus/BuyNow.aspx?productId=' + productId + '" width="150" height="248" frameborder="0" allowtransparency="true", scrolling="no"></iframe>';
}

var cartPromoKey = document.getElementById('cartPromoKey');
if (cartPromoKey != null) {
    cartPromoKey.innerHTML = '<iframe id="frameMercatusPromoKey" src="/Mercatus/PromoKey.aspx" width="145" height="60" frameborder="0" allowtransparency="true", scrolling="no"></iframe>';
}

var cartFreeTrial = document.getElementById('requestFreeTrial');
if (cartFreeTrial != null) {
    cartFreeTrial.innerHTML = '<iframe id="frameMercatusFreeTrial" src="/Mercatus/RequestFreeTrial.aspx" width="300" height="190" frameborder="0" allowtransparency="true", scrolling="no"></iframe>';
}

var examFreeTrial = document.getElementById('requestExamTrial');
if (examFreeTrial != null) {
    examFreeTrial.innerHTML = '<iframe id="frameMercatusExamTrial" src="/Mercatus/RequestFreeTrial.aspx?examTrial=true" width="300" height="190" frameborder="0" allowtransparency="true", scrolling="no"></iframe>';
}

var scormDownload = document.getElementById('scormDownload');
if (scormDownload != null) {
    scormDownload.innerHTML = '<iframe id="frameSCORMDownload" src="/Mercatus/SCORMLogin.aspx" width="750" height="635" frameborder="0" allowtransparency="true", scrolling="no"></iframe>';
}

function SetPriceOnPage(tagId, productId, newValue) {
    var tag = document.getElementById(tagId);
    tag.innerHTML = tag.innerHTML.replace(productId, newValue);
    tag.style.visibility = "visible";
}

var loginLabSim = document.getElementById('loginLabSim');
if (loginLabSim != null) {
    loginLabSim.innerHTML = '<iframe id="frameLoginLabSim" src="/Mercatus/LoginLabSim.aspx" width="202" height="78" frameborder="0" allowtransparency="true", scrolling="no"></iframe>';
}

var loginLabSim = document.getElementById('loginLabSimWide');
if (loginLabSim != null) {
    loginLabSim.innerHTML = '<iframe id="frameLoginLabSim" src="/Mercatus/LoginLabSimW.aspx" width="292" height="78" frameborder="0" allowtransparency="true", scrolling="no"></iframe>';
}