﻿var mySplitResult;
getVariants();

function getVariants(){
    if(strVariants.length != 0) {
        mySplitResult = strVariants.split(",");
    }
    else{
        mySplitResult = ''
    }
}

var lastImageChange;
function changeMainImage(Number){   
    var arrThumbs = strThumbs.split(";");
    var arrLarge = strLarge.split(";");

    if(lastImageChange==Number){
        Number = 0
    }

    // change large image 
    var imgObj = document.getElementById('productLargePic');
    imgObj.src = 'images/products/' + arrLarge[Number];
    
    // reset thumbnails 
    imgObj = document.getElementById('productThumbPic1');
    imgObj.src = 'images/products/' + arrThumbs[1];
    imgObj = document.getElementById('productThumbPic2');
    
    if(imgObj!=null){
        imgObj.src = 'images/products/' + arrThumbs[2];
         }

    // change thumbnail
    if(Number>0){
        imgObj = document.getElementById('productThumbPic' + Number);
        imgObj.src = 'images/products/' + arrThumbs[0];
    }
    
    lastImageChange = Number
    
}

function chooseColour(colour_id,img_ext,price_type,colour_name,price_now,price_type_id,filter_id){
    var selObj

    //update chosen colour & price type
    document.getElementById('colourid').value = colour_id; 
    document.getElementById('priceid').value = price_type_id;
    document.getElementById('filterid').value = filter_id;

    selObj = document.getElementById('selectedColour');
    selObj.src = 'images/colours/colour_'+colour_id+'.'+img_ext;

    selObj = document.getElementById('selectedPriceType');
    selObj.innerHTML  = price_type;

    selObj = document.getElementById('selectedColourName');
    selObj.innerHTML  = colour_name;

    selObj = document.getElementById('selectedPrice');
    selObj.innerHTML  = '£' + FormatNumber(price_now,2,false,false,false);;

    document.getElementById("hiddenSelectedPrice").value = price_now;
    
    updateCostOfSelection()
}

function updateCostOfSelection(){
    var thePrice
    var arrPrice
    var selObj
    
    selObj = document.getElementById('hiddenSelectedPrice');
    thePrice = selObj.value * 1;
    
    if(thePrice>0){
        if(mySplitResult.length != 0){
            for(i = 0; i < mySplitResult.length; i++){
        	    
	            selObj = document.getElementById(mySplitResult[i].toString());
	            arrPrice = selObj.options[selObj.selectedIndex].value
	            arrPrice = arrPrice.split("_");
	            intPrice = arrPrice[0].toString() * 1;
	            thePrice = thePrice + intPrice;
	            
	            document.getElementById('txt' + mySplitResult[i].toString()).value = arrPrice[1];
	            //alert('txt' + mySplitResult[i].toString() +'='+arrPrice[1])
                }
            }
        
        selObj = document.getElementById('QuantityRight');
        if(selObj != null){
            thePrice = thePrice * (selObj.options[selObj.selectedIndex].value)
        }
        
        selObj = document.getElementById('costOfSelection');
        selObj.innerHTML  = '£' + FormatNumber(thePrice,2,false,false,false);
        
        selObj = document.getElementById('costOfSelectionRight');
        if(selObj != null){ 
            selObj.innerHTML  = '£' + FormatNumber(thePrice,2,false,false,false);
        }
    }
}
    
function updateQuantityDD(strDDUsed){
    var selectedindex;
    
    selObj = document.getElementById(strDDUsed);
    selectedindex = selObj.selectedIndex;
    
    selObj = document.getElementById('QuantityRight');
    selObj.options[selectedindex].selected = true;
    
    selObj = document.getElementById('Quantity');
    selObj.options[selectedindex].selected = true;
    
    updateCostOfSelection()
}
    
function submitProductForm(){
    selObj = document.getElementById('hiddenSelectedPrice');
    thePrice = selObj.value * 1;
    
    if(thePrice>0){
        selObj = document.getElementById('frmProduct');
        selObj.submit();
    }
    else{
        alert('Please choose an option before adding to basket');
    }
}
    
function convertDimensions(){
    var selObj = document.getElementById('dimensionsMeasurements');
    var convertTo = selObj.options[selObj.selectedIndex].value;

    var widthObj = document.getElementById('dimensionsWidth');
    var widthInt = widthObj.innerHTML * 1

    var lengthObj = document.getElementById('dimensionsLength');
    var lengthInt = lengthObj.innerHTML * 1

    var depthObj = document.getElementById('dimensionsDepth');
    var depthInt = depthObj.innerHTML * 1

    if(convertTo!='in'){
        widthObj.innerHTML = FormatNumber(widthInt * 2.54,0,false,false,false);
        lengthObj.innerHTML = FormatNumber(lengthInt * 2.54,0,false,false,false);
        depthObj.innerHTML = FormatNumber(depthInt * 2.54,0,false,false,false);    
    }
    else{
        widthObj.innerHTML = FormatNumber(widthInt * 0.3937008,1,false,false,false);
        lengthObj.innerHTML = FormatNumber(lengthInt * 0.3937008,1,false,false,false);
        depthObj.innerHTML = FormatNumber(depthInt * 0.3937008,1,false,false,false);
    }
}

function FormatNumber(num,decimalNum,bolLeadingZero,bolParens,bolCommas)
/**********************************************************************
	IN:
		NUM - the number to format
		decimalNum - the number of decimal places to format the number to
		bolLeadingZero - true / false - display a leading zero for
										numbers between -1 and 1
		bolParens - true / false - use parenthesis around negative numbers
		bolCommas - put commas as number separators.
 
	RETVAL:
		The formatted number!
 **********************************************************************/
    { 
        if (isNaN(parseInt(num))) return "NaN";

	var tmpNum = num;
	var iSign = num < 0 ? -1 : 1;		// Get sign of number

	// Adjust number so only the specified number of numbers after
	// the decimal point are shown.
	tmpNum *= Math.pow(10,decimalNum);
	tmpNum = Math.round(Math.abs(tmpNum))

	var tmpLengthStr = new String(tmpNum);
	var ilength = tmpLengthStr.length + 1;

	tmpNum /= Math.pow(10,decimalNum);
	tmpNum *= iSign;					// Readjust for sign

	// Create a string object to do our formatting on
	var tmpNumStr = new String(tmpNum);

	if(ilength!=tmpNumStr.length){	    
	    // we are missing zeroes on the end... add them
	    var iDecCheck = tmpNumStr.indexOf(".");

	    if (iDecCheck<0){
		    tmpNumStr = tmpNumStr + '.00';
		    }
		else{
		    tmpNumStr = tmpNumStr + '0';
			}
	}

	// See if we need to strip out the leading zero or not.
	if (!bolLeadingZero && num < 1 && num > -1 && num != 0)
		if (num > 0)
			tmpNumStr = tmpNumStr.substring(1,tmpNumStr.length);
		else
			tmpNumStr = "-" + tmpNumStr.substring(2,tmpNumStr.length);

	// See if we need to put in the commas
	if (bolCommas && (num >= 1000 || num <= -1000)) {
		var iStart = tmpNumStr.indexOf(".");
		if (iStart < 0)
			iStart = tmpNumStr.length;

		iStart -= 3;
		while (iStart >= 1) {
			tmpNumStr = tmpNumStr.substring(0,iStart) + "," + tmpNumStr.substring(iStart,tmpNumStr.length)
			iStart -= 3;
		}		
	}

	// See if we need to use parenthesis
	if (bolParens && num < 0)
		tmpNumStr = "(" + tmpNumStr.substring(1,tmpNumStr.length) + ")";

	return tmpNumStr;		// Return our formatted string!
}
