/*------------------------------------------------------------------------------
List of functions:

		 fnChangeWheelData(strWheelDia, strSpokeNo, strAddRim)
		 fnAddToBasket(strDescOne, strTreatment, strPrice, strNum, strSpokeNo, strRim)
		 fnGetProductCode(strString)
		 fnViewBasket()
		 fnConsolidateArrays()

------------------------------------------------------------------------------*/
// global variables
// ********************************
// assign the basic prices
var str76cmBasic= "100.00";
var str57cmBasic= "83.00";
var str38cmBasic= "60.00";
var str76cmRubberRimAddon= "16.00", str76cmSteelRimAddon= "23.00";
var str57cmRubberRimAddon= "13.00", str57cmSteelRimAddon= "19.00";
var str38cmRubberRimAddon= "10.00", str38cmSteelRimAddon= "15.00";
var strBracketPrice= "20.00";
// define extra costs for additional spokes
var str10SpokeAddon= "6.00";
var str12SpokeAddon= "12.00";
// ********************************
// strings
var strEmptyBasket= "Your basket is empty";
var strBasketCookieName= "DW_basketcookie";
var strPayMethod= "nil";
// arrays
var strPartNo= new Array(), strDescription= new Array(), fProductPrice= new Array(), iQuantity= new Array();
// boolean
var bUpdateBasket= new Boolean(false), bConfirmAddToBasket= new Boolean(true);
// -----------------------------------------------------------------------------
// function to change the wheels products data
function fnChangeWheelData(strWheelDia, strSpokeNo, strAddRim)
{
  // define variables used in function
  var strPhotoSrc= "photos/";
  var fBasicWheelCost= 0;
  var fAddRimPrice= 0;
  var fSpokeAddon= 0;
  // find the handle of the photo
  var strPhotoID= "wheel_photo";
  var hPhoto= document.getElementById? document.getElementById(strPhotoID):
    																						document.all.strPhotoID;
  // set values according to wheels diameter
  if (strWheelDia== "76cm")
  {
    strPhotoSrc+= "80cmx";
  	fBasicWheelCost= parseFloat(str76cmBasic);
  	hPhoto.alt= "76cm wagon wheel";
  	//fAddRimPrice= (strAddRim!= "None")? parseFloat(str76cmRimAddon): 0;
		fAddRimPrice= (strAddRim== "Rubber")? parseFloat(str76cmRubberRimAddon):
									(strAddRim== "Steel")? parseFloat(str76cmSteelRimAddon): 0;
  } // end if
  else if (strWheelDia== "57cm")
  {
    strPhotoSrc+= "60cmx";
  	fBasicWheelCost= parseFloat(str57cmBasic);
  	hPhoto.alt= "57cm wagon wheel";
  	//fAddRimPrice= (strAddRim!= "None")? parseFloat(str57cmRimAddon): 0;
		fAddRimPrice= (strAddRim== "Rubber")? parseFloat(str57cmRubberRimAddon):
									(strAddRim== "Steel")? parseFloat(str57cmSteelRimAddon): 0;
		
  } // end else if
  else if (strWheelDia== "38cm")
  {
    strPhotoSrc+= "40cmx";
  	fBasicWheelCost= parseFloat(str38cmBasic);
  	hPhoto.alt= "38cm wagon wheel";
  	//fAddRimPrice= (strAddRim!= "None")? parseFloat(str38cmRimAddon): 0;
		fAddRimPrice= (strAddRim== "Rubber")? parseFloat(str38cmRubberRimAddon):
									(strAddRim== "Steel")? parseFloat(str38cmSteelRimAddon): 0;
		
  } // end else if
  
  // set values according to number of spokes
  if (strSpokeNo== "8")
  {
    strPhotoSrc+= "8_spoke";
  	fSpokeAddon= 0;
  } // end if
  else if (strSpokeNo== "10")
  {
    strPhotoSrc+= "10_spoke";
  	fSpokeAddon= parseFloat(str10SpokeAddon);
  } // end else if
  else if (strSpokeNo== "12")
  {
    strPhotoSrc+= "12_spoke";
  	fSpokeAddon= parseFloat(str12SpokeAddon);
  } // end else if
  // finsh off the photo source string
  strPhotoSrc+= "_wheel.jpg";
  // change the wheel picture
  strWheelDia!= -1? hPhoto.src= strPhotoSrc: "";
  // determine the wheel price
  document.formX.wheel_price.value= fnGetCurrency(fBasicWheelCost+ fSpokeAddon+ fAddRimPrice);
} // end fnChangeWheelData()
// -----------------------------------------------------------------------------
// process products added to basket
function fnAddToBasket(strDescOne, bTreatment, strPrice, strNum, strSpokeNo, strRim)
{
  // ********************* function argument data **************************
  // strDescOne = either wheel diameter or bracket etc
  // bTreatment = boolean true/false for preservative treatment
  // strPrice = product price
  // strNum = number of products ordered
  // [strSpokeNo] = number of spokes on wheel
  // [strRim] = None, Rubber or Steel
  // ********************* function argument data **************************
  // get quantity number from strNum and check it's > 0
  var iNum= parseInt(strNum);
  if (iNum== 0)
  {
    window.alert("It is not possible to add ZERO quantity of products to your basket");
    return;
  } // end if
  // **** if we get this far there is a product to add to the basket ****
  var iArrayIndex= (strPartNo.length> 0)? strPartNo.length: 0;
	// set bUpdateBasket to TRUE so basket will be updated
  bUpdateBasket= true;
  // ****** fill array elements with parameters passed to function
  // build temp string of product description
  strTemp= strDescOne;
  strTemp+= (strSpokeNo)?", "+ strSpokeNo+ " spokes wheel": "";
  strTemp+= (bTreatment)?", treated": ", untreated";
	strTemp+= (strRim== "Rubber")?" with rubber tyre": "";
  strTemp+= (strRim== "Steel")?" with steel tyre": "";
  // assign to description array element
  strDescription[iArrayIndex]=strTemp;
  // array price element
  fProductPrice[iArrayIndex]= parseFloat(strPrice);
  // array product number element
  iQuantity[iArrayIndex]= parseInt(strNum);
  // array part number element
  strPartNo[iArrayIndex]= fnGetProductCode(strDescription[iArrayIndex]);
  // confirm addition to customer if 'bConfirmAddToBasket' is true
  if (bConfirmAddToBasket)
  {
    bConfirmAddToBasket= window.confirm("Quantity of "+ iQuantity[iArrayIndex]+ ", "+
  			strDescription[iArrayIndex]+ " added to basket.\n\n"+
				"To prevent this message showing each time\n"+
  			"    you add to basket, click 'Cancel'.");
  } // end if
	// increment array index counter
  iArrayIndex++;
} // end fnAddToBasket()
// -----------------------------------------------------------------------------
// get product codes for each item added to basket
function fnGetProductCode(strString)
{
  // build components of product code
  // determine product type
	var strProdCode= "DW";
	strProdCode+= (strString.indexOf("76")!= -1? "76.":
                strString.indexOf("57")!= -1? "57.":
                strString.indexOf("38")!= -1? "38.":
                strString.indexOf("bracket")!= -1? "B.":"");
  // check for number of spokes
	strProdCode+= strString.indexOf("12 spoke")!= -1? "12S":
             		strString.indexOf("10 spoke")!= -1? "10S":
    					  strString.indexOf("8 spoke")!= -1? "8S": "";
  // check for steel rim
	strProdCode+= strString.indexOf("rubber")!= -1? "-RT-":
								strString.indexOf("steel")!= -1? "-ST-": "-";
  // check if treated or untreated
	strProdCode+= strString.indexOf("untreated")!= -1? "U":
    					  strString.indexOf("treated")!= -1? "T": "";
  // return the product code
  return (strProdCode);
} // end fnGetProductCode()
//------------------------------------------------------------------------------
// function to view shopping basket
function fnViewBasket()
{
  if (strPartNo.length==0)
	{
	 	alert("There are no products in your basket to view");
		return;
	} // end if
	// consolidate the arrays by gathering identical part no's
  fnConsolidateArrays();
  // make the cookie
  var strBasketCookie= fnMakeBasketCookie();
  // write the cookie
  fnWriteCookie(strBasketCookieName, strBasketCookie);
  // open shopping_basket.html
  window.open('shopping_basket.html', '_top');
} // end fnViewBasket()
//------------------------------------------------------------------------------
// function to gather like product quantities into one array element
function fnConsolidateArrays()
{
  for (var i= 0; i!= strPartNo.length- 1; i++)
  {
    var iNext= i+ 1;
    while (iNext<= strPartNo.length)
    {
     	if (strPartNo[i]== strPartNo[iNext])
			{
			 	iQuantity[i]+= iQuantity[iNext];
				iQuantity[iNext]= 0;
			} // end if
    iNext++;
		} // end while
  } // end for
} // end fnConsolidateArrays()
//------------------------------------------------------------------------------
