// JavaScript Document


// new units can be added at the end of the list

// if 2 units have the same [number], the first one is ignored

// if 2 units have the same symbol, the second one is ignored

// fundamental SI units : meter, kilogram, second, Ampere, Kelvin, mol, candela

// ex: hr = time = s =>  m^0.kg^0.s^1.A^0.K^0.mol^0.cd^0

// ex: g = acceleration = m / s^2 =>  m^1.kg^0.s^-2.A^0.K^0.mol^0.cd^0

// ex: V = tension = kg.m²/s³/A =>  m^2.kg^1.s^-3.A^-1.K^0.mol^0.cd^0



all_units = new Array();

all_units[0] = new Array("Unit","Symbol","Value","m","kg","s","A","K","mol","cd");


all_units[1] = new Array("mm","mm","0.001",1,0,0,0,0,0,0);

all_units[2] = new Array("cm","cm","0.01",1,0,0,0,0,0,0);

all_units[3] = new Array("inch","inch","0.0254",1,0,0,0,0,0,0);

all_units[4] = new Array("inches","inches","0.0254",1,0,0,0,0,0,0);

all_units[5] = new Array("feet","feet","0.3048",1,0,0,0,0,0,0);

all_units[6] = new Array("m","m",1,1,0,0,0,0,0,0);

all_units[7] = new Array("meter","meter",1,1,0,0,0,0,0,0);

all_units[8] = new Array("meters","meters",1,1,0,0,0,0,0,0);





function min(a,b) {if (a<b) return(a); else return(b);}

function max(a,b) {if (a>b) return(a); else return(b);}



function analyse(coef,str)  //============================ splits units into fundamental SI units

	{

	var pos_dot;

	var pos_slash;

	var pos;

	var unit;

	var exponent;

	var list_units_in = new Array;

	var table_base = new Array(coef,0,0,0,0,0,0,0);

	str = str + ".";

	var i = 0;

	while(str.length > 1) // for every unit in the unit input string

		{

		pos_dot = str.indexOf(".");

		pos_slash = str.indexOf("/");

		if (pos_dot == -1 || pos_slash == -1)

			pos = max(pos_dot,pos_slash);

		else

			pos = min(max(pos_dot,0),max(pos_slash,0));


		list_units_in[i] = str.substring(0,pos);

		str=str.substring(pos+1,str.length);



		unit = list_units_in[i];
					
		table_base_unit = fetch_unit(unit); // get the SI equivalent of the selected unit

		if (table_base_unit) // if found in the list

			{

			table_base[0] = table_base[0] * Math.pow(table_base_unit[2],1);

			for (j=1; j < 8; j++) // on incrémente les exposants des unités de base

				{

				table_base[j] = table_base[j] + ( 1 * table_base_unit[j+2] );

				}

			}

		else return(0); // there is a problem

		i = i + 1;

		}

	return(table_base);

}



function fetch_unit(symbol) //============================ fetches SI equivalents of a unit

	{

	k = 1;

	while (all_units[k])

		{

		if (all_units[k][1] == symbol) return(all_units[k]); // found !

		k = k + 1;

		}

	return(0); // not found

	}



function exp_nice(string) //============================ replaces ^2 with ²

	{

	string = string.replace("^2","²");

	string = string.replace("^2","²");

	string = string.replace("^2","²");

	string = string.replace("^2","²");

	string = string.replace("^3","³");

	string = string.replace("^3","³");

	string = string.replace("^3","³");

	return(string);

	}


function display_unit(base_values,base){  //============================ Display results
	
	var dec = 4;

			
	str_display = document.forms[0].jq_unit_out.value

	str_display = exp_nice(str_display);

	document.forms[0].jq_unit_out.value = str_display;
	
	val = base_values[0];
	
	final_out = Math.round(val*Math.pow(10,dec))/Math.pow(10,dec);
	
	if(isNaN(final_out)){
		final_out = "???";	
	}
	
	document.forms[0].jq_quantity_out.value = final_out;


}


function convert(quantity_in,unit_in,quantity_out,unit_out){ //============================ main function

	var units_base_in;

	var units_base_out;
	
	if(quantity_in == "") quantity_in=1;
		
	units_base_in = analyse(quantity_in,unit_in);
	
	units_base_out = analyse(1, unit_out);


	if (units_base_out){ // if the units are found in the database

		if (units_base_out[1] != units_base_in[1]

			|| units_base_out[2] != units_base_in[2]

			|| units_base_out[3] != units_base_in[3]

			|| units_base_out[4] != units_base_in[4]

			|| units_base_out[5] != units_base_in[5]

			|| units_base_out[6] != units_base_in[6]

			|| units_base_out[7] != units_base_in[7]){ // if units are not compatible

				display_unit(units_base_in,1);
		}else{
			units_base_in[0] = units_base_in[0] / units_base_out[0];

			display_unit(units_base_in,0);
		}

	}else{ // all is OK

		display_unit(units_base_in,0);
	}

}

/*
$(document).ready(function() {
	
	$.fn.convert = function() { 
	
	
		var units_base_in;

		var units_base_out;
		
		if($("#jq-quantity-in").val() == "") $("#jq-quantity-in").val(1);

		if(document.forms[0].units_in.value == "") {document.forms[0].units_in.value = "???"; exit;}
		
		
		units_base_in = analyse($("#jq-quantity-in").val(), $("#jq-unit-in").val());

		

		units_base_out = analyse(1, $("#jq-unit-out").val());

		if (units_base_out){ // if the units are found in the database


			if (units_base_out[1] != units_base_in[1]

				|| units_base_out[2] != units_base_in[2]

				|| units_base_out[3] != units_base_in[3]

				|| units_base_out[4] != units_base_in[4]

				|| units_base_out[5] != units_base_in[5]

				|| units_base_out[6] != units_base_in[6]

				|| units_base_out[7] != units_base_in[7]){ // if units are not compatible

					display_unit(units_base_in,1);
				}

		}else{ // all is OK

			units_base_in[0] = units_base_in[0] / units_base_out[0];

			display_unit(units_base_in,0);
		}



		
	}
});
*/
