﻿var cs = 0;

function changeVar(variations) {
    if (document.getElementById('prevprice' + cs)) {
        document.getElementById('prevprice' + cs).style.display = 'none'; 
    }
    document.getElementById('curprice' + cs).style.display = 'none';
    if (document.getElementById('availability' + cs)) {
        document.getElementById('availability' + cs).style.display = 'none'; 
    }
    v = variations.value;
    if (document.getElementById('prevprice' + v)) {
        document.getElementById('prevprice' + v).style.display = ''; 
    }
    document.getElementById('curprice' + v).style.display = '';
    if (document.getElementById('availability' + v)) {
        document.getElementById('availability' + v).style.display = ''; 
    }
    cs = v;
}

function validateNonNumber(evt, o) {
    var keyCode = evt.which ? evt.which : evt.keyCode;
    return keyCode < '0'.charCodeAt() || keyCode > '9'.charCodeAt(); 
}

function validateNonLetter(evt, o) {
    var keyCode = evt.which ? evt.which : evt.keyCode;
    return keyCode < '9'.charCodeAt() || keyCode == '9'.charCodeAt(); 
}

function renderImage(pid, imgName, setname, configname, v_width, v_height, moviename) {
    // Version check based upon the values entered above in "Globals"
    var hasReqestedVersion = DetectFlashVer(8, 0, 0);

    // Check to see if the version meets the requirements for playback
    if (hasReqestedVersion) {
        // if we've detected an acceptable version
        // embed the Flash Content SWF when all tests are passed
        document.write("<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0'");
        document.write(" ID='KitbagViewer' width='" + v_width + "' height='" + v_height + "'>");
        document.write(" <param name='movie' value='http://findel.scene7.com/is-viewers/flash/" + moviename + ".swf?serverUrl=http://findel.scene7.com/is/image/&image=Findel/" + setname + "&modifier=resMode%3dsharp%26op_usm%3d1.0,1.0,8&initialFrame=0&config=Findel/" + configname + "&contentRoot=http://findel.scene7.com/skins/&instancename=KitbagViewer'\>");
        document.write(" <param name='menu' value='false'\><param name='wmode' value='transparent'><param name='quality' value='high'\><param name='scale' value='noscale'\><param name='salign' value='LT'\><param name='bgcolor' value='#ffffff'\><param name='allowScriptAccess' value='sameDomain' />");
        document.write(" <embed src='http://findel.scene7.com/is-viewers/flash/" + moviename + ".swf?serverUrl=http://findel.scene7.com/is/image/&image=Findel/" + setname + "&modifier=resMode%3dsharp%26op_usm%3d0.5,2&initialFrame=0&config=Findel/" + configname + "&contentRoot=http://findel.scene7.com/skins/&instancename=KitbagViewer'");
        document.write(" name='KitbagViewer' wmode='transparent' quality='high' bgcolor='#ffffff' width='" + v_width + "' height='" + v_height + "' swliveconnect='true' allowScriptAccess='sameDomain'");
        document.write(" type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer'>");
        document.write(" </embed></object>");
    } else {  // flash is too old or we can't detect the plugin
        document.write("<img border='0' alt='' src='http://findel.scene7.com/is/image/Findel/" + imgName + "?layer=comp&wid=" + v_width + "&hei=" + v_height + "&fmt=jpeg&qlt=90,0&op_sharpen=0&resMode=sharp&op_usm=1.0,1.0,8,0&iccEmbed=0' />");
        var alternateContent = "<div class='FeedbackInformation'>To receive an enhanced version of this content please download the Adobe Flash Player by <a href=http://www.adobe.com/go/getflash/>clicking here</a></div>";
        document.write(alternateContent);  // insert non-flash content
    }
}

function sctrackATB(qtydd_id, brand, wasprice, sizedd_id, onesizeonly) {

    // This is a tracking function for Site Catalyst
    // It is called when the add to basket button is clicked, the onclick attribute is added from the code behind
    // * IMPORTANT - this relies on  'productdetail' and 'printingChoices' JSON classes being outputted from code behind into the page

    var quantity = parseInt(document.getElementById(qtydd_id).value, 10);
    if (quantity === 0) { return; }

    var variationcost;
    var counter = 0;
    var subtotal, availability, size, pid, vid;

    if (sizedd_id == '') {
        variationcost = productdetail.variations[0].PriceActual;
        availability = productdetail.variations[0].Availability;
        size = onesizeonly;
    } else {
        vid = parseInt(document.getElementById(sizedd_id).value, 10);
        if (vid === 0) { return; }
        while (counter <= productdetail.variations.length - 1) {
            if (productdetail.variations[counter].VariationId == vid) {
                variationcost = productdetail.variations[counter].PriceActual;
                availability = productdetail.variations[counter].Availability;
                size = productdetail.variations[counter].Description;
                break;
            }
            counter++;
        }
    }

    if (wasprice === 0) { wasprice = 'N/A'; }
    if (brand == '') { brand = 'N/A'; }
    if (availability == 'In Stock Today') {
        availability = 'NOW';
    } else {
        availability = 'PRE';
    }

    pid = productdetail.ProductID;
    if (printingChoices.HeroPid !== 0) {
        var hero = window["product" + printingChoices.HeroPid];
        description = hero.Description;
        pid = hero.ProductID;
    }
    if (printingChoices.Badges != "blank") {
        pid = pid + "-Badges";
    }
    if (printingChoices.Name != "" || printingChoices.Number != "") {
        pid = pid + "-Personalised";
    }

    subtotal = variationcost * quantity;

    //alert('pid = ' + pid + ', name = ' + productdetail.Description + ', qty = ' + quantity + ', cost = ' + subtotal + ', brand = ' + brand + ', was price = ' + wasprice + ', availability = ' + availability + ', size = ' + size);
    //sc_trackATB(PID,PNAME,QTY,COST,MERCH_CAT,BRAND,WAS_PRICE,AVAIL,SIZE,COLOUR);")
    sc_trackATB(pid, productdetail.Description, quantity, subtotal, 'UNKNOWN', brand, wasprice, availability, size, 'N/A');

}