//
function VivQty(Action)
{
var VivQtyElement = document.vivariums.viv_qty;

// these actions are for the product list
if(Action == "add")
	{
	VivQtyElement.value = parseInt(VivQtyElement.value) + 1;
	CompleteCheck();
	}
if(Action == "subtract" && VivQtyElement.value != 1)
	{
	VivQtyElement.value = parseInt(VivQtyElement.value) - 1;
	CompleteCheck();
	}
}

//
function ChangeColour(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("viv_standard.gif","viv_glass_lower_fixed.gif","viv_glass_lower.jpg","viv_mesh_upper_flap.gif","chameleon.gif");
var DirPath ="images/vivariums/";
}
SelectedOption = FormElement.selectedIndex;
document.images[PicId].src = DirPath+PicArray[SelectedOption];
}

//
function ChangeUnits(FormElement)
{
var SelectedOption,TheForm,ImperialArray,MetricArray,SelectedLength,SelectedHeight,SelectedWidth;

TheForm = document.vivariums;

SelectedOption = FormElement.options[FormElement.selectedIndex].value;

SelectedLength = TheForm.viv_length.selectedIndex;
SelectedWidth = TheForm.viv_width.selectedIndex;
SelectedHeight = TheForm.viv_height.selectedIndex;

ImperialLengthArray = new Array("Select",12,15,18,21,24,27,30,36,42,48,54,60,66,72);
MetricLengthArray = new Array("Select",305,380,460,535,610,685,760,915,1065,1220,1370,1525,1675,1830);
ImperialWidthArray = new Array("Select",12,18,24);
MetricWidthArray = new Array("Select",305,460,610);

if(SelectedOption == "metric")
{
	for (var q=TheForm.viv_length.options.length; q>=0; q--)
        {
        TheForm.viv_length.options[q]=null;
	TheForm.viv_height.options[q]=null;
        }
    for (listcount = 0;listcount<=MetricLengthArray.length-1;listcount++)
        {
        NewOption = new Option(MetricLengthArray[listcount],ImperialLengthArray[listcount]);
        TheForm.viv_length.options[listcount] = NewOption;
        NewOption = new Option(MetricLengthArray[listcount],ImperialLengthArray[listcount]);
	TheForm.viv_height.options[listcount] = NewOption;
        }
    for (var q=TheForm.viv_width.options.length; q>=0; q--)
        {
        TheForm.viv_width.options[q]=null;
        }
    for (listcount = 0;listcount<=MetricWidthArray.length-1;listcount++)
        {
        NewOption = new Option(MetricWidthArray[listcount],ImperialWidthArray[listcount]);
        TheForm.viv_width.options[listcount] = NewOption;
        }
TheForm.viv_length.options[SelectedLength].selected = true;
TheForm.viv_width.options[SelectedWidth].selected = true;
TheForm.viv_height.options[SelectedHeight].selected = true;
}
else
{
    for (var q=TheForm.viv_length.options.length; q>=0; q--)
        {
        TheForm.viv_length.options[q]=null;
	TheForm.viv_height.options[q]=null;
        }
    for (listcount = 0;listcount<=ImperialLengthArray.length-1;listcount++)
        {
        NewOption = new Option(ImperialLengthArray[listcount],ImperialLengthArray[listcount]);
        TheForm.viv_length.options[listcount] = NewOption;
        NewOption = new Option(ImperialLengthArray[listcount],ImperialLengthArray[listcount]);
	TheForm.viv_height.options[listcount] = NewOption;
        }
    for (var q=TheForm.viv_width.options.length; q>=0; q--)
        {
        TheForm.viv_width.options[q]=null;
        }
    for (listcount = 0;listcount<=ImperialWidthArray.length-1;listcount++)
        {
        NewOption = new Option(ImperialWidthArray[listcount],ImperialWidthArray[listcount]);
        TheForm.viv_width.options[listcount] = NewOption;
        }
TheForm.viv_length.options[SelectedLength].selected = true;
TheForm.viv_width.options[SelectedWidth].selected = true;
TheForm.viv_height.options[SelectedHeight].selected = true;
}
}

//check if all required elements are complete before doing calc
function CompleteCheck()
{
var TheForm=document.vivariums,LenComplete,WidthComplete,HeightComplete;
LenComplete = TheForm.viv_length.options[TheForm.viv_length.selectedIndex].text;
WidthComplete = TheForm.viv_width.options[TheForm.viv_width.selectedIndex].text;
HeightComplete = TheForm.viv_height.options[TheForm.viv_height.selectedIndex].text;

if(LenComplete != "Select" && WidthComplete != "Select" && HeightComplete != "Select")
	{
	Update("Vivariums");
	TheForm.AddToBasket.disabled = false;
	}
else
	{
	TheForm.price_viv.value = "";
	TheForm.rrp_viv.value = "";
	TheForm.viv_order.value = "";
	TheForm.weight_viv.value = "";
	TheForm.volume_viv.value = "";
	TheForm.AddToBasket.disabled = true;
	}
}

//calculate the cost as selections are made
function Update(WhichPage,VivL,VivW,VivH,VivColour,VivStyle)
{
var whitecost = 0.0042;
var colcost = 0.01;
var runner = 0.025;
var vent = 0.3;
var glass = 0.01;
var hardboard = 0.002;

var GlassDensity = 0.00642; //weight in Kg per square inch
var HardboardDensity = 0.002; //weight in Kg per square inch
var ChipboardDensity = 0.00665; //weight in Kg per square inch

if(WhichPage == "Vivariums")
	{
	var TheForm = document.vivariums;
	var length = parseInt(TheForm.viv_length.options[TheForm.viv_length.selectedIndex].value);
	var width = parseInt(TheForm.viv_width.options[TheForm.viv_width.selectedIndex].value);
	var height = parseInt(TheForm.viv_height.options[TheForm.viv_height.selectedIndex].value);
	var disp_length = parseInt(TheForm.viv_length.options[TheForm.viv_length.selectedIndex].text);
	var disp_width = parseInt(TheForm.viv_width.options[TheForm.viv_width.selectedIndex].text);
	var disp_height = parseInt(TheForm.viv_height.options[TheForm.viv_height.selectedIndex].text);
	var colour = TheForm.colour.options[TheForm.colour.selectedIndex].value;
	var style = TheForm.style.options[TheForm.style.selectedIndex].text;
	var stylecost = TheForm.style.options[TheForm.style.selectedIndex].value;
	var Quantity = parseInt(TheForm.viv_qty.value);
	}
else
	{
	var length = parseInt(VivL);
	var width = parseInt(VivW);
	var height = parseInt(VivH);
	var colour = VivColour;
	var stylecost = VivStyle;	
	var Quantity = 1;	
	}

var totlength = eval(length + width + height);

if (totlength >= 140){labour = 55}
else if (totlength >= 130){labour = 50}
else if (totlength >= 120){labour = 45}
else if (totlength>= 110){labour = 40}
else if (totlength >= 100){labour = 35}
else if (totlength >= 90){labour = 30}
else if (totlength >= 80){labour = 25}
else if (totlength >= 70){labour = 20}
else if (totlength >= 60){labour = 15}
else if (totlength >= 50){labour = 12.5}
else if (totlength >= 40){labour = 10}
else{labour = 10}

var woodarea = eval(length + height)*2*width;

var volume = length * width * height;
volume = (volume * 16.4)/1000; //conversion factor from cubic inches to cubic cm's

if (style == "Mesh/Upper Flap")
	{
	var uppervol = (15 * (length*2.54) * (width*2.54))/1000;
	volume = volume - uppervol; //remove volume of top section. Approx 15 cm high
	}

areacost = colcost;
if (colour == "White")
	{
	areacost = whitecost;
	}

var matcost = eval(woodarea*areacost);
var backcost = eval(length*height*hardboard);
var glasscost = eval(length*height*glass);
var numvents = Math.round(length/12);
var ventcost = eval(numvents*vent);
var runnercost = eval(length + height)*2*runner;

var TradeCost = eval(matcost + backcost + glasscost + ventcost + labour + runnercost)*stylecost;
var DirectCost = eval(TradeCost*1.25*1.175);
var RrpCost = eval(TradeCost*1.55*1.175);

var div = Math.pow(10,2);

	// now calculate the approximate total weight

	var GlassWeight = GlassDensity * (length*height);
	var BackWeight = HardboardDensity * (length*height);
	var ChipboardWeight = ChipboardDensity * woodarea;

if(WhichPage == "Vivariums")
	{
	TheForm.price_viv.value = Math.round(DirectCost * div)/div;
	TheForm.rrp_viv.value = Math.round(RrpCost * div)/div;
	TheForm.viv_order.value = "Vivarium (" +style+") "+disp_length+"L x "+disp_width+"W x "+disp_height+"H "+colour;

	TheForm.weight_viv.value = Math.round(((GlassWeight + BackWeight + ChipboardWeight)*Quantity) * div)/div;
	TheForm.volume_viv.value = Math.round(volume * div)/div;
	}
else
	{
	if(document.StarterKit.VivCost)
		{
		document.StarterKit.VivCost.value = Math.round(DirectCost * div)/div;
		document.StarterKit.TotVivCost.value = Math.round(DirectCost * div)/div;
		document.StarterKit.TotVivWeight.value = Math.round(((GlassWeight + BackWeight + ChipboardWeight)*Quantity) * div)/div;
		}
	var PriceWeight = DirectCost+"end"+Math.round(((GlassWeight + BackWeight + ChipboardWeight)*Quantity) * div)/div;
	return PriceWeight;
	}
}