var power = new Array();
power[0] = "hp";
power[1] = "kW";
power[2] = "PS";

var hp = 1.3
var kW = 1
var PS = 1.4

var boost = new Array();
boost[0] = "psi";
boost[1] = "bar";
boost[2] = "kgcm";
boost[3] = "kPa";

var psi = 14.5;
var bar = 1;
var kPa = 99.9;
var kgcm = 1.02

var varToSelect = 'toSelect';
var varFromSelect = 'fromSelect';

// reminder : document.forms[0] = calculateForm

function setVariables( updateType ){
	// update type is to determine if the update is for the from field or just the to field. if its only the to field then it is not+neccesary to recreate the from field.
	if( updateType != "1" ) {
		var radioLength=document.forms[0].conversionType.length;
		for(i=0; i<radioLength; i++){
			if( document.forms[0].conversionType[i].checked){
				arrayToPopulateFrom=document.forms[0].conversionType[i].value
			}
		}
		reCreate( varFromSelect, '', arrayToPopulateFrom );
	}
	reCreate( varToSelect, varFromSelect, arrayToPopulateFrom );
}

function reCreate(thisField, fromThis, arrayToPopulateFrom ){
	// i've seperated recreate incase i want to recreate the from and the to fields seperately. if the fromThis field is empty then the add function will assume that there is no from parameters.
	deleteOption( thisField );
	addOption( thisField, fromThis, arrayToPopulateFrom );
}

function deleteOption( thisField ) {
	var numElements = eval( "document.forms[0]." + thisField +".length")
	for( i=0; i < numElements; i++) {
	    eval("document.forms[0]." + thisField + ".options[0] =null");
	}
}    

function addOption( thisField, fromThis, arrayToPopulateFrom ) {
    var defaultSelected = true;
    var selected = true;
	var arrayLength = eval( arrayToPopulateFrom + ".length" );
	var arrayValue = "";

	for( i=0; i<arrayLength ; i++ ) {
		if ( fromThis ) {
			if ( eval( "document.forms[0]." + fromThis +".value != " + arrayToPopulateFrom + "[i]" ) ){
				arrayValue = eval( arrayToPopulateFrom + "[i]" );
			   	var optionName = new Option( arrayValue, arrayValue, defaultSelected, selected );
			    var length = eval( "document.forms[0]." + thisField + ".length" );
				eval( "document.forms[0]." + thisField + ".options[length] = optionName" );
			}		
		} else {
			arrayValue = eval( arrayToPopulateFrom + "[i]");
			var optionName = new Option(arrayValue, arrayValue, defaultSelected, selected );
			var length = eval( "document.forms[0]." + thisField + ".length" );
			eval( "document.forms[0]." + thisField + ".options[length] = optionName" );
		}
	}
}

function calc() {

	var calcVar = eval(document.forms[0].toSelect.value) / eval(document.forms[0].fromSelect.value) ;
	
//	var finalValue = document.forms[0].from.value * eval(calcType);
	var finalValue = document.forms[0].from.value * calcVar;
	if ( isNaN( finalValue ) ) finalValue = 0;
	document.forms[0].to.value = finalValue;
}

/* 
function replaceOption(object) {
    var Current = object.selectName.selectedIndex;
    object.selectName.options[Current].text =
object.currentText.value;
    object.selectName.options[Current].value =
object.currentText.value;
}
*/
