//
function CabinetQty(Action)
{
var CabQtyElement = document.cabinets.cabinet_qty;

// these actions are for the product list
if(Action == "add")
	{
	CabQtyElement.value = parseInt(CabQtyElement.value) + 1;
	CompleteCabCheck();
	}
if(Action == "subtract" && CabQtyElement.value != 1)
	{
	CabQtyElement.value = parseInt(CabQtyElement.value) - 1;
	CompleteCabCheck();
	}
}

//
function ChangeCabColour(FormElement,PicId)
{
if(PicId == "viv_colour_swatch")
{
PicArray = new Array("light_oak.jpg","pine_eff.jpg","beech.jpg","cherry.jpg","maple.jpg","white_satin.jpg");
var DirPath ="images/viv_colours/";
}
else
{
PicArray = new Array("standard.jpg","single_door.jpg","door_div_shelf.jpg","twin_doors.jpg","twin_door_shelf.jpg","triple_door.jpg");
var DirPath ="images/cabinets/";
}
SelectedOption = FormElement.selectedIndex;
document.images[PicId].src = DirPath+PicArray[SelectedOption];
}

//
function ChangeCabUnits(FormElement)
{
var SelectedOption,TheForm,ImperialArray,MetricArray,SelectedLength,SelectedHeight,SelectedWidth;

TheForm = document.cabinets;

SelectedOption = FormElement.options[FormElement.selectedIndex].value;

SelectedLength = TheForm.cabinet_length.selectedIndex;
SelectedWidth = TheForm.cabinet_width.selectedIndex;
SelectedHeight = TheForm.cabinet_height.selectedIndex;

ImperialLengthArray = new Array("Select",24,27,30,36,42,48,54,60,66,72);
MetricLengthArray = new Array("Select",610,685,760,915,1065,1220,1370,1525,1675,1830);

ImperialWidthArray = new Array("Select",12,18,24);
MetricWidthArray = new Array("Select",305,460,610);

ImperialHeightArray = new Array("Select",18,21,24,27,30);
MetricHeightArray = new Array("Select",460,535,610,685,760);

for (var q=TheForm.cabinet_length.options.length; q>=0; q--)
        {
        TheForm.cabinet_length.options[q]=null;
        }
for (var q=TheForm.cabinet_height.options.length; q>=0; q--)
        {
        TheForm.cabinet_height.options[q]=null;
        }
for (var q=TheForm.cabinet_width.options.length; q>=0; q--)
        {
        TheForm.cabinet_width.options[q]=null;
        }

if(SelectedOption == "metric")
	{
    	for (listcount = 0;listcount<=MetricLengthArray.length-1;listcount++)
        	{
        	NewOption = new Option(MetricLengthArray[listcount],ImperialLengthArray[listcount]);
        	TheForm.cabinet_length.options[listcount] = NewOption;
        	}
    	for (listcount = 0;listcount<=MetricWidthArray.length-1;listcount++)
        	{
        	NewOption = new Option(MetricWidthArray[listcount],ImperialWidthArray[listcount]);
        	TheForm.cabinet_width.options[listcount] = NewOption;
        	}
    	for (listcount = 0;listcount<=MetricHeightArray.length-1;listcount++)
        	{
        	NewOption = new Option(MetricHeightArray[listcount],ImperialHeightArray[listcount]);
        	TheForm.cabinet_height.options[listcount] = NewOption;
        	}
	TheForm.cabinet_length.options[SelectedLength].selected = true;
	TheForm.cabinet_width.options[SelectedWidth].selected = true;
	TheForm.cabinet_height.options[SelectedHeight].selected = true;
	}
else
	{
    	for (listcount = 0;listcount<=MetricLengthArray.length-1;listcount++)
        	{
        	NewOption = new Option(ImperialLengthArray[listcount],ImperialLengthArray[listcount]);
        	TheForm.cabinet_length.options[listcount] = NewOption;
        	}
    	for (listcount = 0;listcount<=MetricWidthArray.length-1;listcount++)
        	{
        	NewOption = new Option(ImperialWidthArray[listcount],ImperialWidthArray[listcount]);
        	TheForm.cabinet_width.options[listcount] = NewOption;
        	}
    	for (listcount = 0;listcount<=MetricHeightArray.length-1;listcount++)
        	{
        	NewOption = new Option(ImperialHeightArray[listcount],ImperialHeightArray[listcount]);
        	TheForm.cabinet_height.options[listcount] = NewOption;
        	}
	TheForm.cabinet_length.options[SelectedLength].selected = true;
	TheForm.cabinet_width.options[SelectedWidth].selected = true;
	TheForm.cabinet_height.options[SelectedHeight].selected = true;
	}
}

//check if all required elements are complete before doing calc
function CompleteCabCheck()
{
var TheForm = document.cabinets,LenComplete,WidthComplete,HeightComplete;
LenComplete = TheForm.cabinet_length.options[TheForm.cabinet_length.selectedIndex].text;
WidthComplete = TheForm.cabinet_width.options[TheForm.cabinet_width.selectedIndex].text;
HeightComplete = TheForm.cabinet_height.options[TheForm.cabinet_height.selectedIndex].text;

if(LenComplete != "Select" && WidthComplete != "Select" && HeightComplete != "Select")
	{
	UpdateCab();
	TheForm.AddToBasket.disabled = false;
	}
else
	{
	TheForm.price_cabinet.value = "";
	TheForm.rrp_cabinet.value = "";
	TheForm.cabinet_order.value = "";
	TheForm.weight_cabinet.value = "";
	TheForm.AddToBasket.disabled = true;
	}
}

//calculate the cost as selections are made
function UpdateCab()
{
var TheForm = document.cabinets;
var whitecost = 0.0042; //pounds per sq inch
var colcost = 0.01; //pounds per sq inch
var hardboard = 0.002; //pounds per sq inch
var TrimCost = 0.05; //pounds per inch
var HingeCost = 1.50; //pounds each
var HandleCost = 1.50; //pounds each

var HardboardDensity = 0.002; //weight in Kg per square inch
var ChipboardDensity = 0.00665; //weight in Kg per square inch

var length = parseInt(TheForm.cabinet_length.options[TheForm.cabinet_length.selectedIndex].value);
var width = parseInt(TheForm.cabinet_width.options[TheForm.cabinet_width.selectedIndex].value);
var height = parseInt(TheForm.cabinet_height.options[TheForm.cabinet_height.selectedIndex].value);
var disp_length = parseInt(TheForm.cabinet_length.options[TheForm.cabinet_length.selectedIndex].text);
var disp_width = parseInt(TheForm.cabinet_width.options[TheForm.cabinet_width.selectedIndex].text);
var disp_height = parseInt(TheForm.cabinet_height.options[TheForm.cabinet_height.selectedIndex].text);
var colour = TheForm.colour.options[TheForm.colour.selectedIndex].value;
var style = TheForm.style.options[TheForm.style.selectedIndex].text;
var labour = parseInt(TheForm.style.options[TheForm.style.selectedIndex].value);
var Quantity = parseInt(TheForm.cabinet_qty.value);

var CarcassArea = eval(length + height)*2*width; //common for all cabinet styles
var DividerArea = eval(height * width); //common for all cabinet styles 

var ShowAlert = "no", ShelfArea = 0, DoorArea = 0, DoorTrimLength = 0, TotalWoodArea = 0, TotalDividerArea = 0, NumDividers = 0, NumHinges = 0, NumHandles = 0;

switch (style) //calculate the style dependant costs
	{ 
	case "Standard (open)": //divider fitted for cabinets 30 inch length and over
		if(length <= 29){NumDividers = 0}
		if(length >= 30){NumDividers = 1}
		if(length >= 48){NumDividers = 2}
		TotalDividerArea = NumDividers * DividerArea;
		TotalWoodArea = TotalDividerArea + CarcassArea;
		break;
	case "Single Door" : //available in 24 inch length only
		DoorArea = length * height;
		TotalWoodArea = ShelfArea + DoorArea + CarcassArea;
		NumHinges = 2;
		NumHandles = 1;
		DoorTrimLength = (length + height) * 2;
		if(length != 24)
			{
			ShowAlert = "yes";
			TheForm.cabinet_length.options[1].selected = true;
			}
		MessageText = "This style is only available in\n24 inch / 610mm lengths";
		break;
	case "Door/Divider/Shelf": //max length 48 inches
		ShelfArea = (length/2) * width;
		DoorArea = (length/2) * height;
		NumDividers = 1;
		TotalDividerArea = NumDividers * DividerArea;
		TotalWoodArea = ShelfArea + DoorArea + TotalDividerArea + CarcassArea;
		NumHinges = 2;
		NumHandles = 1;
		DoorTrimLength = ((length/2) + height) * 2;
		if(length > 48)
			{
			ShowAlert = "yes";
			TheForm.cabinet_length.options[6].selected = true;
			}
		MessageText = "This style is only available up to\n48 inch / 1220mm maximum length";
		break;
	case "Twin Doors": //max length 48 inches
		DoorArea = length * height;
		NumDividers = 1;
		TotalDividerArea = NumDividers * DividerArea;
		TotalWoodArea = DoorArea + TotalDividerArea + CarcassArea;
		NumHinges = 4;
		NumHandles = 2;
		DoorTrimLength = (length * 2)+(height * 4);
		if(length > 48)
			{
			ShowAlert = "yes";
			TheForm.cabinet_length.options[6].selected = true;
			}
		MessageText = "This style is only available up to\n48 inch / 1220mm maximum length";
		break;
	case "Twin Doors/Shelf": //36 inch min length
		DoorArea = (length/3)* 2 * height;
		ShelfArea = (length/3)* width;
		NumDividers = 2;
		TotalDividerArea = NumDividers * DividerArea;
		TotalWoodArea = ShelfArea + DoorArea + TotalDividerArea + CarcassArea;
		NumHinges = 4;
		NumHandles = 2;
		DoorTrimLength = ((length/3)*4)+(height * 4);
		if(length < 36)
			{
			ShowAlert = "yes";
			TheForm.cabinet_length.options[4].selected = true;
			}
		MessageText = "This style is only available in lengths\ngreater than 36 inch / 915mm";
		break;
	case "Triple Doors": //36 inch min length
		DoorArea = length * height;
		NumDividers = 2;
		TotalDividerArea = NumDividers * DividerArea;
		TotalWoodArea = DoorArea + TotalDividerArea + CarcassArea;
		NumHinges = 6;
		NumHandles = 3;
		DoorTrimLength = (length * 2)+(height * 6);
		if(length < 36)
			{
			ShowAlert = "yes";
			TheForm.cabinet_length.options[4].selected = true;
			}
		MessageText = "This style is only available in lengths\ngreater than 36 inch / 915mm";
		break;
	default :
		TotalWoodArea = 1; 
	}

if(ShowAlert == "yes")
	{
	alert(MessageText);
	}

areacost = colcost;
if(colour == "White")
	{
	areacost = whitecost;
	}

var matcost = TotalWoodArea*areacost;
var backcost = length*height*hardboard;
var DoorTrimCost = DoorTrimLength * TrimCost;

TotHingeCost = HingeCost * NumHinges;
TotHandleCost = HandleCost * NumHandles;

var TradeCost = matcost + backcost + TotHingeCost + TotHandleCost + DoorTrimCost + labour;
var DirectCost = TradeCost*1.2*1.175*Quantity;
var RrpCost = TradeCost*1.5*1.175*Quantity;

var div = Math.pow(10,2);

TheForm.price_cabinet.value = Math.round(DirectCost * div)/div;
TheForm.rrp_cabinet.value = Math.round(RrpCost * div)/div;
TheForm.cabinet_order.value = "Cabinet (" +style+") "+disp_length+"L x "+disp_width+"W x "+disp_height+"H "+colour;

// now calculate the approximate total weight

var BackWeight = HardboardDensity * (length*height);
var ChipboardWeight = ChipboardDensity * TotalWoodArea;

TheForm.weight_cabinet.value = Math.round(((BackWeight + ChipboardWeight)*Quantity) * div)/div;
}