var ratios = [];
var gears = [];
gears["gearbox"] = 6;
gears["transfer"] = 2;
gears["od"] = 1;

function set_ratios(box, init) {
	var blist = document.getElementById(box + "_list");
	var box_s = blist.options[blist.selectedIndex].value;
	
	var vlist = document.getElementById(box + "_variant");
	var variant_s = "";
	
	if(box != "od") {
		variant_s = vlist.options[vlist.selectedIndex].value;
		
		if(!init) {
			vlist.className = "";
		}
	}
	
	for(i = 0; i < gears[box]; i++) {
		var ratio = document.getElementById(box + "_r" + i);
		
		if(box_s == "none" || box_s == "select" || variant_s == "select") {
			ratio.value = "";
			ratio.disabled = true;
		}else if(box_s == "custom") {
			if(init == 0) {
				ratio.value = "";
			}
			
			ratio.disabled = false;
		}else{
			if(typeof(ratios[box][box_s][variant_s][i]) == "undefined") {
				ratio.value = "";
			}else{
				ratio.value = ratios[box][box_s][variant_s][i];
			}
			
			ratio.disabled = false;
		}
	}
}

function set_custom(box) {
	var list = document.getElementById(box + "_list");
	
	for(var i = 0; i < list.options.length; i++) {
		if(list.options[i].value == "custom") {
			list.selectedIndex = list.options[i].index;
			break;
		}
	}
	
	set_variants(box, 1);
}

function set_variants(box, init) {
	var list = document.getElementById(box + "_list");
	var v = list.options[list.selectedIndex].value;
	
	if(box != "od") {
		var vlist = document.getElementById(box + "_variant");
		vlist.length = 0;
		vlist.disabled = false;
		
		if(v != "select" && v != "none" && v != "custom") {
			if(typeof(ratios[box][v][""]) == "undefined") {
				vlist.options[vlist.length] = new Option("Please select...", "select");
			}
			
			for(variant in ratios[box][v]) {
				if(variant != "") {
					vlist.options[vlist.length] = new Option(variant, variant);
					
					if(init && set_variant[box] == variant) {
						vlist.selectedIndex = vlist.length-1;
					}
				}
			}
		}
		
		if(vlist.length == 0) {
			vlist.options[vlist.length] = new Option("N/A", "");
			vlist.disabled = true;
		}
		
		if(vlist.selectedIndex == -1) {
			vlist.selectedIndex = 0; // Stupid browsers...
		}
	}
	
	set_ratios(box, init);
}

function verify_box(box, req) {
	var list = document.getElementById(box + "_list");
	var ok = true;
	var need_ratios = true;
	
	if(list.options[list.selectedIndex].value == "select") {
		list.className = "inv_input";
		ok = false;
		need_ratios = false;
	}else{
		list.className = "";
	}
	
	if(list.options[list.selectedIndex].value == "none") {
		need_ratios = false;
	}
	
	if(box != "od") {
		var vlist = document.getElementById(box + "_variant");
		
		if(vlist.options[vlist.selectedIndex].value == "select") {
			vlist.className = "inv_input";
			ok = false;
			need_ratios = false;
		}else{
			vlist.className = "";
		}
		
		if(vlist.options[vlist.selectedIndex].value == "none") {
			need_ratios = false;
		}
	}
	
	for(i = 0; i < gears[box]; i++) {
		var ratio = document.getElementById(box + "_r" + i);
		
		if(ratio.value == "" && need_ratios && req > i) {
			ok = false;
			ratio.className = "inv_input";
		}else if(ratio.value != "" && !ratio.value.match(/^\d+(\.\d+)?$/)) {
			ok = false;
			ratio.className = "inv_input";
		}else{
			ratio.className = "";
		}
	}
	
	return ok;
}

function verify_num(id) {
	var field = document.getElementById(id);
	
	if(!field.value.match(/^\d+(\.\d+)?$/)) {
		field.className = "inv_input";
		return false;
	}else{
		field.className = "";
		return true;
	}
}

function form_verify() {
	var g = verify_box("gearbox", 1);
	var t = verify_box("transfer", 2);
	var o = verify_box("od", 1);
	
	var a = verify_num("max_speed");
	var b = verify_num("speed_step");
	var d = verify_num("redline");
	
	var tyre_scale = document.getElementById("tyre_scale").value;
	var tyre_size = document.getElementById("tyre_dia");
	var c = true;
	
	if(tyre_scale == "metric") {
		if(!tyre_size.value.match(/^\d+(\/\d+)?R\d+(\.\d+)?$/i)) {
			tyre_size.className = "inv_input";
			c = false;
		}else{
			tyre_size.className = "";
		}
	}else if(tyre_scale == "imperial") {
		if(!tyre_size.value.match(/^\d+(\.\d+)?x\d+(\.\d+)?$/i)) {
			tyre_size.className = "inv_input";
			c = false;
		}else{
			tyre_size.className = "";
		}
	}else{
		if(!tyre_size.value.match(/^\d+(\.\d+)?$/i)) {
			tyre_size.className = "inv_input";
			c = false;
		}else{
			tyre_size.className = "";
		}
	}
	
	if(!(g && t && o && a && b && c && d)) {
		alert("Errors detected in form.\nPlease correct the highlighted fields and try again.");
		return false;
	}else{
		return true;
	}
}
