//pipes
//
//
//round pipes must start with word round for formula detection

var types =[ 
"Please choose a pipe type",
"round 9.5 mm diameter",
"round 12.7 mm diameter",
"round 15.9 mm diameter",
"round 19.1 mm diameter",
"round 22.3 mm diameter",
"round 26.4 mm diameter",
"round 31.8 mm diameter",
"round 38.1 mm diameter",
"round 42.7 mm diameter",
"round 50.8 mm diameter",
"round 63.5 mm diameter",
"round 76.3 mm diameter",
"round 89.1 mm diameter",
"round 101 mm diameter",
"round 114 mm diameter",
"square 10x10 mm",
"square 12.7x12.7 mm",
"square 15x15 mm",
"square 20x20 mm",
"square 25x25 mm",
"square 30x30 mm",
"square 40x40 mm",
"square 50x50 mm",
"rectangular 10x20 mm",
"rectangular 13x26 mm",
"rectangular 20x40 mm",
"rectangular 25x50 mm",
"rectangular 30x60 mm",
"rectangular 30x90 mm"];
var weights = [
1.34,
1.73,
2.2,
2.67,
3.13,
3.6,
4.55,
5.48,
6.16,
7.36,
9.24,
11.1,
13,
14.6,
16.6,
1.88,
2.39,
2.83,
3.77,
4.71,
5.65,
7.54,
9.42,
2.83,
3.67,
5.65,
7.07,
8.48,
10.5
];
var minthick=[
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.5,
0.7,
0.7,
0.8,
1,
1.2,
1.2,
0.6,
0.6,
0.6,
0.6,
0.6,
0.6,
0.6,
0.6,
0.6,
0.6,
0.6,
0.6,
0.6,
1
];
var maxthick =[
0.9,
1.2,
1.5,
1.5,
1.5,
1.5,
1.5,
1.5,
1.5,
1.5,
1.5,
1.5,
1.5,
1.5,
1.5,
1.2,
1.2,
1.2,
1.2,
1.2,
1.2,
1.2,
1.2,
1.2,
1.2,
1.2,
1.2,
1.2,
1.2
];
var thicknesses =[0.5,0.6,0.7,0.8,0.9,1,1.1,1.2,1.5];
var diameter =[ 
"9.5",
"12.7",
"15.9",
"19.1",
"22.3",
"26.4",
"31.8",
"38.1",
"42.7",
"50.8",
"63.5",
"76.3",
"89.1",
"101",
"114"];
function emptythicklist(){
	var thelist=document.getElementById('thickness');
		var optionsize=thelist.options.length;
	for (var j =optionsize; j>=0 ;j--){
	thelist.options[j]=null;
	}
}
function emptyweight(){
var thefield=document.getElementById('weight');
thefield.value="";
}
function emptyprice(){
var thefield=document.getElementById('price');
thefield.value="";
}
function select() {
	var thelist=document.getElementById('thickness');
	var index =document.getElementById('typeslist').options.selectedIndex -1 ;

emptythicklist();
emptyweight();
emptyprice();
	if (index == -1)
		return;
	var theminthick=minthick[index];
	var themaxthick=maxthick[index];
	var tempOpt = new Option("Please choose a thickness",0);
	thelist.options.add(tempOpt);
	for (var i=0 ;i<=thicknesses.length;i++){
		if (thicknesses[i]>=theminthick && thicknesses[i] <=themaxthick){
		var tempOpt = new Option(thicknesses[i],i);
		 thelist.options.add(tempOpt);
		 }
	}
}
function selectthick() {
var indextype =document.getElementById('typeslist').options.selectedIndex -1 ;
var indexthick =document.getElementById('thickness').value ;
var theweiht=0;
if (types[indextype].toLowerCase().substr(0,5)=="round") {
theweiht=roundNumber((diameter[indextype]-thicknesses[indexthick])*thicknesses[indexthick]*Math.PI*7.85*6/1000,2);
}else{
theweiht=roundNumber(weights[indextype]*thicknesses[indexthick],2);
}
document.getElementById('weight').value=theweiht;
document.getElementById('price').value=roundNumber(theweiht*10,0);

}
	
function roundNumber(rnum, rlength) { // Arguments: number to round, number of decimal places
  var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
  return newnumber; // Output the result to the form field (change for your purposes)
}
function populate() {

	var thelist=document.getElementById('typeslist');


	for (var i =0;i<types.length;i++){
	var tempOpt = new Option(types[i],i);
	 thelist.options.add(tempOpt);
	}


}
//shipping
//
//
//
function populateshipping() {

	var thelist=document.getElementById('destination');


	for (var i =0;i<destination.length;i++){
	var tempOpt = new Option(destination[i],i);
	 thelist.options.add(tempOpt);
	}


}
function selectdest() {
	var maxlength=document.getElementById('length');
	var maxperim=document.getElementById('perim');
	var price = document.getElementById('price');
	var transpo = document.getElementById('transpoair');
	var index =document.getElementById('destination').options.selectedIndex -1 ;
var weight=document.getElementById('weight').value;

	if (index == -1){
		price.value="";
		maxlength.value="";
		maxperim.value="";
		return;
	}

	if (transpo.checked){
			var  ntranches =Math.ceil((weight-0.5)*2);
	if (ntranches<=0)
	ntranches=0;
	ntranches++;
		price.value=Math.ceil((ntranches*priceair[index]+initpriceair[index])*1.1);
		maxlength.value=maxlengthair[index];
		maxperim.value=maxperimeterair[index];
		
	}else{
			var  ntranches =Math.ceil(weight-1);
	if (ntranches<=0)
	ntranches=0;
	ntranches++;
		price.value=Math.ceil((ntranches*pricesea[index]+initpricesea[index])*1.1);
		maxlength.value=maxlengthsea[index];
		maxperim.value=maxperimetersea[index];
	}

}
var destination=[
	"Please choose a destination",
	"Australia",
	"Canada",
"France",
"New Zealand",
"South Korea",
	"UK",
	"USA"
];
var maxlengthsea=[
	1.05,
	2,
2,
1.5,
1.05,
	1.5,
	1.05
];
var maxlengthair=[
	1.05,
	1.5,
1.5,
1.5,
1.05,
	1.05,
	1.05
];
var maxperimetersea=[
	2,
	3,
3,
3,
2,
	3,
	2
];
var maxperimeterair=[
	2,
	3,
3,
3,
2,
	3,
	2
];
var initpricesea=[		  
	11.45,
	10.93,
	
17.06,
14.38,
11.63,
	14.33,
	10.75
];
var initpriceair=[
	12.36,
	12.87,
18.52,
15.80,
11.53,
	15.75,
	14.42
];
var pricesea=[
	1.91,
	1.82,
1.39,
2.09,
1.12,
	2.38,
	3.47
];
var priceair=[
	4.06,
	5,
	
4.08,
4.75,
1.85,
	5.03,
	6.07
];
//
//
//shippin calculator
function maxweight(){
var height=document.getElementById('heightkg').value;
var width=document.getElementById('widthkg').value;
var length=document.getElementById('lengthkg').value;
var maxkg=document.getElementById('allowedkg');
maxkg.value = Math.floor(height*width*length/6000);

}
