var rod_end_display_units = "INCH";
function rod_end_parse_return_inch_units( inputname ) {
	var retval = parseFloat( document.getElementById(inputname).value );
	if (rod_end_display_units == "MM") {
		retval /= 25.4;
		
	}
	return retval;
}

function rod_end_parse_return_mm_units( inputname ) {
	var retval = parseFloat( document.getElementById(inputname).value );
	if (rod_end_display_units == "INCH") {
		retval *= 25.4;
	}
	return retval;
}

function rod_end_format_inch_val( inch_val ) {
	var retval = "";
	if (rod_end_display_units == "INCH") {
		retval = inch_val.toFixed(3)
	} else {
		retval = (inch_val*25.4).toFixed(2)
	}
	return retval;
}

function rod_end_format_mm_val( mm_val ) {
	var retval = "";
	if (rod_end_display_units == "MM") {
		retval = mm_val.toFixed(2)
	} else {
		retval = (mm_val/25.4).toFixed(3)
	}
	return retval;
}

function checkNumeric( name ) {
	var oNF = document.getElementById(name);
	if (!oNF) return false;
	oNF.style.backgroundColor = "";
	// trim whitespace
	oNF.value = oNF.value.toString().replace(/^\s+/,'').replace(/\s+$/,'')
	if ("" == oNF.value) {
		return false;
	}
	if ( isFinite( oNF.value * 1.0) ) {
		return true;
	}
	rod_end_status_message("Correct Input.");
	var xpos = getPositionX(oNF)+48;
	var ypos = getPositionY(oNF);
	displayErrorMsg( "rod_"+name, "All values should be numeric.", xpos, ypos );
	return false;
}

function rod_end_status_message(message) {
	var oStatus = document.getElementById("rod_end_status");
	var oDiv = document.getElementById("MP_"+"rod");
	if (oDiv) oDiv.style.display="none";

	if (oStatus) {
		if (arguments.length > 1) {
			oStatus.innerHTML = "&nbsp;";
		} else {
			if (message == "") {
				oStatus.innerHTML = "&nbsp;";
			} else {
				oStatus.innerHTML = message;
				oStatus.style.fontWeight = "bold";
			}
		}
	} else {
		alert(message)
	}
	// CLEAR BACKGROUNDS
	var oNF = document.getElementById("A");
	if (oNF) oNF.style.backgroundColor = "";
	oNF = document.getElementById("H");
	if (oNF) oNF.style.backgroundColor = "";
	oNF = document.getElementById("R");
	if (oNF) oNF.style.backgroundColor = "";
	oNF = document.getElementById("AL");
	if (oNF) oNF.style.backgroundColor = "";
	oNF = document.getElementById("MM");
	if (oNF) oNF.style.backgroundColor = "";
	oNF = document.getElementById("DB");
	if (oNF) oNF.style.backgroundColor = "";
	oNF = document.getElementById("DA");
	if (oNF) oNF.style.backgroundColor = "";
	oNF = document.getElementById("T");
	if (oNF) oNF.style.backgroundColor = "";
	// CLEAR POPUP MESSAGES
	var oDiv = document.getElementById("MP_rod_A");
	if (oDiv) oDiv.style.display = "none";
	var oDiv = document.getElementById("MP_rod_H");
	if (oDiv) oDiv.style.display = "none";
	var oDiv = document.getElementById("MP_rod_R");
	if (oDiv) oDiv.style.display = "none";
	var oDiv = document.getElementById("MP_rod_AL");
	if (oDiv) oDiv.style.display = "none";
	var oDiv = document.getElementById("MP_rod_T");
	if (oDiv) oDiv.style.display = "none";

	if (arguments.length > 1) {
		oNF = document.getElementById(arguments[1]);
		oNF.style.backgroundColor = "#FFFFAA";
		var xpos = getPositionX(oNF)+48;
		var ypos = getPositionY(oNF);
		displayErrorMsg( "rod_"+arguments[1], message, xpos, ypos );
	}
}

function rod_end_enable_request(btf) {
	var oRequestButton = document.getElementById("request_part_number_button");
	if (oRequestButton) {
		oRequestButton.disabled = !btf;
	}
}

function rodend_flipunit( name ) {
	if (checkNumeric(name)) {
		var oNF = document.getElementById(name);
		if (oNF) {
			// assume a unit change
			if (rod_end_display_units == "INCH") {
				oNF.value = (oNF.value/25.4).toFixed(3)
			} else {
				oNF.value = (oNF.value*25.4).toFixed(2)
			}
		}
	}
}

function rodend_unit_change(oSel, check_function) {
	rod_end_display_units = oSel.options[oSel.selectedIndex].value;
	
	rodend_flipunit("A");
	rodend_flipunit("H");
	rodend_flipunit("R");
	rodend_flipunit("AL");

	check_function();
}

