﻿//-------------------------------------------
// JQUERY EVENTS
//-------------------------------------------

$(document).ready(function() {

    $("#Qty").click(function() {
        this.focus();
        this.select();
    });

});

//----------------------------------------------
// FUNCTIONS
//----------------------------------------------

//Matrix Change Options
function getPageValues(data) {

    var bits = data.split("~");

    var MatrixID = bits[0];
    var ProductImage1 = bits[1];
    var ProductImage2 = bits[2];
    var ProductImage3 = bits[3];
    var ThumbPhotoFile1 = bits[4];
    var ThumbPhotoFile2 = bits[5];
    var ThumbPhotoFile3 = bits[6];
    var xlPhotoFile1 = bits[7];
    var xlPhotoFile2 = bits[8];
    var xlPhotoFile3 = bits[9];
    var PriceIncVAT = bits[10];
    var PriceExcVAT = bits[11];
    var WasPriceExcVAT = bits[12];
    var WasPriceIncVAT = bits[13];
    var SaveIncVAT = bits[14];
    var SaveExcVAT = bits[15];
    var strStock = bits[16];
    var qis = bits[17];

    //alert("MatrixID = " + MatrixID +
         //"\nProductImage1 = " + ProductImage1 +
         //"\nProductImage2 = " + ProductImage2 +
         //"\nProductImage3 = " + ProductImage3 +
         //"\nThumbPhotoFile1 = " + ThumbPhotoFile1 +
         //"\nThumbPhotoFile2 = " + ThumbPhotoFile2 +
         //"\nThumbPhotoFile3 = " + ThumbPhotoFile3 +
         //"\nxlPhotoFile1 = " + xlPhotoFile1 +
         //"\nxlPhotoFile2 = " + xlPhotoFile2 +
         //"\nxlPhotoFile3 = " + xlPhotoFile3 +
         //"\nPriceIncVAT = " + PriceIncVAT +
         //"\nPriceExcVAT = " + PriceExcVAT +
         //"\nWasPriceExcVAT = " + WasPriceExcVAT +
         //"\nWasPriceIncVAT = " + WasPriceIncVAT +
         //"\nSaveIncVAT = " + SaveIncVAT +
         //"\nSaveExcVAT = " + SaveExcVAT +
         //"\nstrStock = " + strStock +
         //"\nqis = " + qis);

    $("#MatrixID").val(MatrixID);
    if (ProductImage1 != '') {
        $("#mainPhoto").attr('src', ProductImage1);
        $("#thumbl1").val(ProductImage1);
    }
    if (ProductImage2 != '') { $("#thumbl2").val(ProductImage2); }
    if (ProductImage3 != '') { $("#thumbl3").val(ProductImage3); }
    if (ThumbPhotoFile1 != '') { $("#img1").attr('src', ThumbPhotoFile1); }
    if (ThumbPhotoFile2 != '') { $("#img2").attr('src', ThumbPhotoFile2); }
    if (ThumbPhotoFile3 != '') { $("#img3").attr('src', ThumbPhotoFile3); }
    if (xlPhotoFile1 != '') { $("#thumbxl1").val(xlPhotoFile1); }
    if (xlPhotoFile1 != '') { $("#xlPhoto").attr('href', xlPhotoFile1); }
    if (xlPhotoFile2 != '') { $("#thumbxl2").val(xlPhotoFile2); }
    if (xlPhotoFile3 != '') { $("#thumbxl3").val(xlPhotoFile3); }
   
    $("#pinc").html(PriceIncVAT);
    $("#pexc").html(PriceExcVAT);
    $("#wpexc").html(WasPriceExcVAT);
    $("#wpinc").html(WasPriceIncVAT);
    $("#sinc").html(SaveIncVAT);
    $("#sexc").html(SaveExcVAT);
    $("#psm").html(strStock);
  
    if (parseInt(qis) == 0) {
        $("#psm").removeClass('ins oos').addClass('oos');
    }
    else {
        $("#psm").removeClass('ins oos').addClass('ins');
    }
}
function getSelectValues(data, axisCount, index) {

    //alert(data);
    var bits = data.split("|");
    var dbits = bits[0].split("~");
    var MatrixID = dbits[0];
    var AxisCount = dbits[1];
    var GroupIndex = dbits[2];
    
    var bitsIndex = 0;
    var currentIndex = index += 1

    for (var i = 1; i < bits.length; i++) {
        var options = bits[i].split("~");
        $('#Matrix' + currentIndex)[0].options.length = 0;

        for (var x = 0; x < options.length; x++) {
            $('#Matrix' + currentIndex).append(options[x]);
        }
        currentIndex += 1
    }

    $("#MatrixID").val(MatrixID);
    return MatrixID
}

function selectionsChanged(index) {
    $('#matrixWaiting').show();

    var url = "/ajax/matrix.asp";
    var productid = $("#id").val();
    var matrixdata = $("#Matrix" + index).val();
    var axisCount = $("#AxisCount").val();

    //alert("matrixdata: " + matrixdata +
          //"\naxisCount: " + axisCount +
          //"\nindex: " + index);

    var defaultImage1 = $("#default1").val();
    var defaultImage2 = $("#default2").val();
    var defaultImage3 = $("#default3").val();

    var ProductThumb1 = $("#defaultThumb1").val();
    var ProductThumb2 = $("#defaultThumb2").val();
    var ProductThumb3 = $("#defaultThumb3").val();

    if (parseInt(index) != parseInt(axisCount)) {
        //Do Change of DropDowns
        $.post(url,
        {
            EventType: 'ChangeAxis',
            ProductID: productid,
            AxisCount: axisCount,
            GroupIndex: index,
            AxisValue: matrixdata
        },
        function(data) {
            matrixdata = getSelectValues(data, axisCount, index);
            $.post(url,
            {
                ProductID: productid,
                EventType: 'ChangeLastAxis',
                AxisCount: axisCount,
                GroupIndex: index,
                MatrixID: matrixdata,
                ProductImage1: defaultImage1,
                ProductImage2: defaultImage2,
                ProductImage3: defaultImage3,
                ProductThumb1: ProductThumb1,
                ProductThumb2: ProductThumb2,
                ProductThumb3: ProductThumb3
            },
            function(data) {
                getPageValues(data);
            });
        });
    }
    else {
        //Do Change of Matrix ID, Prices, stock and images
        $.post(url,
        {
            ProductID: productid,
            EventType: 'ChangeLastAxis',
            AxisCount: axisCount,
            GroupIndex: index,
            MatrixID: matrixdata,
            ProductImage1: defaultImage1,
            ProductImage2: defaultImage2,
            ProductImage3: defaultImage3,
            ProductThumb1: ProductThumb1,
            ProductThumb2: ProductThumb2,
            ProductThumb3: ProductThumb3
        },
        function(data) {
            getPageValues(data);
        });
    
    }
    $('#matrixWaiting').hide();
}


//Quantity Selector Functions
function minusone() {
    var minQty = $("#minQty").val();
    var Qty = $("#Qty").val();

    if (Qty == "") { Qty = 0 }
    Qty = parseInt(Qty) - 1;
    if (parseInt(Qty) < parseInt(minQty)) { Qty = minQty }
    $("#Qty").val(Qty);
}
function addone() {
    var maxQty = $("#maxQty").val();
    var Qty = $("#Qty").val();

    if (Qty == "") { Qty = 0 }
    Qty = parseInt(Qty) + 1;
    if (parseInt(Qty) > parseInt(maxQty)) { Qty = maxQty }
    $("#Qty").val(Qty);
}

function checkQty() {

    var i;
    var s = $("#Qty").val();
    var bag = '1234567890';
    var Qty = "";
    var minQty = $("#minQty").val();
    var maxQty = $("#maxQty").val();

    for (i = 0; i < s.length; i++) {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) != -1) Qty += c;
    }
    if (Qty == "") { Qty = 1 }
    if (parseInt(Qty) < parseInt(minQty)) { Qty = minQty }
    if (parseInt(Qty) > parseInt(maxQty)) { Qty = maxQty }

    $("#Qty").val(Qty);
} 

//Images on product page
function mouseOverImage(index) {

    var xlImage;
    var lImage;
  
    switch (index) {
        case 1:
            xlImage = $("#thumbxl1").val();
            lImage = $("#thumbl1").val();
            break;
        case 2:
            xlImage = $("#thumbxl2").val();
            lImage = $("#thumbl2").val();
            break;
        case 3:
            xlImage = $("#thumbxl3").val();
            lImage = $("#thumbl3").val();
            break;
        default:
    }

     document.mainPhoto.src = lImage;
     document.getElementById("xlPhoto").href = xlImage;

 }


