
var all_models_array = new Array();
function handle_fork_man() {
	try {
		if($('existing-fork-info').visible()) {
			Effect.Fade('existing-fork-info');
		}
		new Ajax.Request('includes/asp/ajax_responses/get_fork_models.asp', {
			method:'get',
			parameters: $('de-rating-form').serialize(true), 
			onSuccess: function(transport) {
				var response = transport.responseText || "No models were found for this manufacturer";
				
				// remove existing options
				var existing_options_array = $('fork_model_slct').childElements();
				for(i = 0; i < existing_options_array.length; i++) {
					$(existing_options_array[i]).remove();
				}
				
				// create new options
				var models_array = response.split("^");
				for(i = 0; i < models_array.length; i++) {
					models_array[i] = models_array[i].split("$");;
					
					all_models_array[models_array[i][0]] = new Array();
					all_models_array[models_array[i][0]][0] = models_array[i][1]; //type
					all_models_array[models_array[i][0]][1] = models_array[i][2]; //capacity
					all_models_array[models_array[i][0]][2] = models_array[i][3]; //load_centre
					all_models_array[models_array[i][0]][3] = models_array[i][4]; //axle_to_fork
					all_models_array[models_array[i][0]][4] = models_array[i][5]; //existing_fork_class
					all_models_array[models_array[i][0]][5] = models_array[i][6]; //backleg height
					all_models_array[models_array[i][0]][6] = models_array[i][7]; //existing_fork_w
					all_models_array[models_array[i][0]][7] = models_array[i][8]; //existing_fork_t
					all_models_array[models_array[i][0]][8] = models_array[i][9]; //existing_fork_e
					
					var new_option = Builder.node('option', {
						value: models_array[i][0]
					}, models_array[i][1]);
					$('fork_model_slct').appendChild(new_option);
				}
				handle_fork_model();
			},
			onFailure: function() { 
				alert('Something went wrong...')
			}
		});
	} catch(err) {
		alert(err);
	}
}

function handle_fork_model() {
		if(!$('existing-fork-info').visible()) {
			Effect.Appear('existing-fork-info');
		}else {
			Effect.Pulsate('existing-fork-info');
		}
	
	var form = $('de-rating-form');
	var input = form['fork_model_slct'];
	var selected_model_id = $F(input);
	
	$('fork_capacity').update(all_models_array[selected_model_id][1]); //capacity
	$('fork_load_centre').update(all_models_array[selected_model_id][2]); //load_centre
	$('fork_axle_to_fork').update(all_models_array[selected_model_id][3]); //axle_to_fork
	$('fork_class').update(all_models_array[selected_model_id][4]); //existing_fork_class
	$('fork_h').update(all_models_array[selected_model_id][5]); //backleg height
	$('fork_w').update(all_models_array[selected_model_id][6]); //existing_fork_w
	$('fork_t').update(all_models_array[selected_model_id][7]); //existing_fork_t
	$('fork_e').update(all_models_array[selected_model_id][8]); //existing_fork_e
	
	var tfq = (0.00001572 * parseFloat($('fork_w').innerHTML) * parseFloat($('fork_t').innerHTML) *(parseFloat($('fork_h').innerHTML) + 3 * parseFloat($('fork_e').innerHTML) / 4));
	
	if (parseFloat($('fork_h').innerHTML) > 0) {
		tfq = tfq * 1;
	}
	$('fork_tfq').update(Math.round(tfq));
	
	var tfg = (parseFloat($('fork_t').innerHTML)*parseFloat($('fork_h').innerHTML)+(3*parseFloat($('fork_t').innerHTML)*parseFloat($('fork_e').innerHTML)+7*parseFloat($('fork_e').innerHTML)*parseFloat($('fork_e').innerHTML)/6)/2)/(2*(parseFloat($('fork_h').innerHTML)+3*parseFloat($('fork_e').innerHTML)/4));
	
	if (parseFloat($('fork_h').innerHTML) > 0) {
		tfg = tfg * 1;
	}
	$('fork_tfg').update(Math.round(tfg));
	
	update_residual_capacity();
}

function handle_att_change() {
	var form = $('de-rating-form');

	var att_h = parseFloat($('att_h').innerHTML);

	var input = form['att_w'];
	var att_w = $F(input);
	input = form['att_t'];
	var att_t = $F(input);
	input = form['att_e'];
	var att_e = $F(input);
	
	var fq = (0.00001572* att_w * att_t * (att_h + 3 * att_e / 4));
	if (att_h > 0) {
		fq = fq * 1;
	}
	
	var fg = (att_t * att_h + (3 * att_t * att_e + 7 * att_e * att_e / 6) / 2) / (2 * (att_h + 3 * att_e / 4));
	if (att_h > 0) {
		fg = fg * 1;
	}
	
	$('att_fq').update(Math.round(fq));
	$('att_fg').update(Math.round(fg));
	
	update_residual_capacity();
}


function update_residual_capacity() {
	var form = $('de-rating-form');
	
	var E10 = parseFloat($('fork_capacity').innerHTML);
	var E12 = parseFloat($('fork_load_centre').innerHTML);
	var E14 = parseFloat($('fork_axle_to_fork').innerHTML);
	var E26 = parseFloat($('fork_t').innerHTML);
	var E30 = parseFloat($('fork_tfq').innerHTML);
	var E32 = parseFloat($('fork_tfg').innerHTML);

	var M8 = parseFloat($('att_capacity').innerHTML);
	var M10 = parseFloat($('att_load_centre').innerHTML);
	var M12 = parseFloat($('att_mass').innerHTML);
	var M14 = parseFloat($('att_cofg').innerHTML);
	var M16 = parseFloat($('att_llc').innerHTML);
	input = form['total_forks'];
	var M18 = parseFloat($F(input));
	input = form['att_w'];
	var M24 = parseFloat($F(input));
	input = form['att_t'];
	var M26 = parseFloat($F(input));
	input = form['att_e'];
	var M28 = parseFloat($F(input));
	var M30 = parseFloat($('att_fq').innerHTML);
	var M32 = parseFloat($('att_fg').innerHTML);

	var G42 = (E10*(E14+E12)+E30*(E14-E26+E32)-M12*(E14-E26+M14))/(E14-E26+M16+M10);
	var K42 = M10;
	
	/*alert("G42: " + G42 + "\r\n" +
		  "K42: " + K42 + "\r\n" +
		  "E26: " + E26 + "\r\n" +
		  "M16: " + M16 + "\r\n" +
		  "M18: " + M18 + "\r\n" +
		  "M30: " + M30 + "\r\n" +
		  "E14: " + E14 + "\r\n" +
		  "M32: " + M32 + "\r\n" +
		  "M26: " + M26 + "\r\n" +
		  "M28: " + M28);*/
	
	var mass_qf = M12+(M18*M30);
	var cofg_gf = ((M12*M14)+(M18*M30*(M32+M16)))/(M12+(M18*M30));
	var llc_sf = parseFloat(M16) + parseFloat(M26);
	
	$('att_qf').update(Math.round(mass_qf)); // M36
	$('att_gf').update(Math.round(cofg_gf)); // M38
	$('att_sf').update(Math.round(llc_sf)); // M40
	
	//alert(G42 + " " + K42 + " " + E14 + " " + E26 + " " + M16);
	//alert(M18 + " " + M30 + " " + M32);
	//alert(M26 + " " + M28);
	
	var G46 = ((G42*(K42+E14-E26+M16))-(M18*M30*(E14-E26+M16+M32)))/(E14-E26+M16+M26+(M28/2));
	
	/*alert("(G42*(K42+E14-E26+M16)): " + (G42*(K42+E14-E26+M16)) +  "\r\n\r\n" +
		  "(2*M18*M30*(E14-E26+M16+M32)): " + (2*M18*M30*(E14-E26+M16+M32)) + "\r\n\r\n" +
		  "((G42*(K42+E14-E26+M16))-(2*M18*M30*(E14-E26+M16+M32))): " + ((G42*(K42+E14-E26+M16))-(2*M18*M30*(E14-E26+M16+M32))) + "\r\n\r\n" +
		  "E14: " + E14 + "\r\n" +
		  "E26: " + E26 + "\r\n" +
		  "M16: " + M16 + "\r\n" +
		  "M26: " + M26 + "\r\n" +
		  "M28: " + M28 + "\r\n" +
		  "G42: " + G42 + "\r\n" +
		  "K42: " + K42 + "\r\n" +
		  "E26: " + E26 + "\r\n" +
		  "M16: " + M16 + "\r\n" +
		  "M18: " + M18 + "\r\n" +
		  "M30: " + M30 + "\r\n" +
		  "E14: " + E14 + "\r\n" +
		  "M32: " + M32 + "\r\n" +
		  "M26: " + M26 + "\r\n" +
		  "M28: " + M28 + "\r\n" +
		  "(E14-E26+M16+M26+(M28/2)): " + (E14-E26+M16+M26+(M28/2)) + "\r\n\r\n" +
		  "((G42*(K42+E14-E26+M16))-(2*M18*M30*(E14-E26+M16+M32)))/(E14-E26+M16+M26+(M28/2)): " + ((G42*(K42+E14-E26+M16))-(2*M18*M30*(E14-E26+M16+M32)))/(E14-E26+M16+M26+(M28/2)));*/
	
	$('derated_residual_capacity_with_forks').update(Math.round(G46)); // G46
	$('derated_load_centre_with_forks').update(Math.round(M28 / 2)); // K46
	
	$('derated_residual_capacity').update(Math.round(G42));
	$('de-rating-info').addClassName("on");
}

function truck_fork_capacity_check() {
	//alert("truck_fork_capacity_check");
	var M8 = parseFloat($('att_capacity').innerHTML);
	var G42 = parseFloat($('derated_residual_capacity').innerHTML);
	
	var form = $('de-rating-form');
	
	input = form['total_forks'];
	var M18 = parseFloat($F(input));
	input = form['att_w'];
	var M24 = parseFloat($F(input));
	input = form['att_t'];
	var M26 = parseFloat($F(input));
	input = form['att_e'];
	var M28 = parseFloat($F(input));
	
	//alert(M8 + " " + M18 + " " + M24 + " " + M26 + " "+ M28 + " " + G42);
	
	if((G42 > 0)&&(M8 > 0)&&(M18 > 0)&&(M24 > 0)&&(M26 > 0)&&(M28 > 0)) {
		if(M8 < G42) {
			alert("Important!, with this truck/attachment combination, the truck capacity exceeds the attachments and therefore attachment capacity should be used as the maximum.");	
		}else if (M8 > G42) {
			alert("With this truck/attachment combination, the attachment capacity exceeds the trucks and therefore if the full attachments capacity is required a different truck/attachment selection will be necessary.");
		}
	}	
	return false;
}

//Event.observe(window, 'load', handle_fork_man);
