// ******************************************* Length Covertor Constructor ***********************************************
function LengthCovertor(iValue, strUnit) {
	this.setDigitsCount = function (iValue) {
		if (!isNaN(iValue))
			this.digitsCount = iValue;
	}
	if (!isNaN(iValue)) {
		iValue *= 1000000000000;
		switch (strUnit.toLowerCase()) {
			case "cm" :
				this.value = iValue / this.cm_interval;
				break;
			case "m" :
				this.value = iValue / this.m_interval;
				break;
			case "km" :
				this.value = iValue / this.km_interval;
				break;
			case "inch" :
				this.value = iValue / this.inch_interval;
				break;
			case "feet" :
				this.value = iValue / this.feet_interval;
				break;
			case "yard" :
				this.value = iValue / this.yard_interval;
				break;
			case "mile" :
				this.value = iValue / this.mile_interval;
				break;
		}
	}
	else
		alert("[" + iValue + "] is not a number!");
}

LengthCovertor.prototype.digitsCount = 8;
LengthCovertor.prototype.cm_interval = 25400000000000;
LengthCovertor.prototype.m_interval = 254000000000;
LengthCovertor.prototype.km_interval = 254000000;
LengthCovertor.prototype.inch_interval = 10000000000000;
LengthCovertor.prototype.feet_interval = 833333333333.33333333333333333333;
LengthCovertor.prototype.yard_interval = 277777777777.77777777777777777778;
LengthCovertor.prototype.mile_interval = 157828282.82828282828282828282828;

LengthCovertor.prototype.trim = function(iValue) {
	if (iValue.toString().length > this.digitsCount) {
		if (iValue.toString().indexOf(".") == -1 || iValue.toString().indexOf("e") != -1)
			return "N/A";
		else {
			var iCounter = 0;
			while (iValue.toString().indexOf(".") != -1) {
				iValue *= 10;
				iCounter++;
			}
			iValue = iValue / Math.pow(10, iCounter);
			var strValue = iValue.toString();
			strValue = strValue.substring(0,this.digitsCount+1);
			var iPosition = strValue.indexOf(".");
			if (parseInt(strValue) == parseFloat(strValue))
				strValue = strValue.substring(0,iPosition);
			return strValue;
		}
	}
	else
		return iValue.toString();
}

LengthCovertor.prototype.getCM = function() {
	return this.trim((this.value * this.cm_interval) / 1000000000000);
}

LengthCovertor.prototype.getM = function() {
	return this.trim((this.value * this.m_interval) / 1000000000000);
}

LengthCovertor.prototype.getKM = function() {
	return this.trim((this.value * this.km_interval) / 1000000000000);
}

LengthCovertor.prototype.getInch = function() {
	return this.trim((this.value * this.inch_interval) / 1000000000000);
}

LengthCovertor.prototype.getFeet = function() {
	return this.trim((this.value * this.feet_interval) / 1000000000000);
}

LengthCovertor.prototype.getYard = function() {
	return this.trim((this.value * this.yard_interval) / 1000000000000);
}

LengthCovertor.prototype.getMile = function() {
	return this.trim((this.value * this.mile_interval) / 1000000000000);
}


// ******************************************* Weight Covertor Constructor ***********************************************

function WeightCovertor(iValue, strUnit) {
	this.setDigitsCount = function (iValue) {
		if (!isNaN(iValue))
			this.digitsCount = iValue;
	}
	if (!isNaN(iValue)) {
		iValue *= 1000000000000;
		switch (strUnit.toLowerCase()) {
			case "kg" :
				this.value = iValue / this.kg_interval;
				break;
			case "ston" :
				this.value = iValue / this.ston_interval;
				break;
			case "lton" :
				this.value = iValue / this.lton_interval;
				break;
			case "lb" :
				this.value = iValue / this.lb_interval;
				break;
			case "oz" :
				this.value = iValue / this.oz_interval;
				break;
		}
	}
	else
		alert("[" + iValue + "] is not a number!");
}

WeightCovertor.prototype.digitsCount = 8;
WeightCovertor.prototype.kg_interval = 453592370;
WeightCovertor.prototype.ston_interval = 446428.57142857142857142857142857;
WeightCovertor.prototype.lton_interval = 453592.370;
WeightCovertor.prototype.lb_interval = 1000000000;
WeightCovertor.prototype.oz_interval = 16000000000;

WeightCovertor.prototype.trim = function(iValue) {
	if (iValue.toString().length > this.digitsCount) {
		if (iValue.toString().indexOf(".") == -1 || iValue.toString().indexOf("e") != -1)
			return "N/A";
		else {
			var iCounter = 0;
			while (iValue.toString().indexOf(".") != -1) {
				iValue *= 10;
				iCounter++;
			}
			iValue = iValue / Math.pow(10, iCounter);
			var strValue = iValue.toString();
			strValue = strValue.substring(0,this.digitsCount+1);
			var iPosition = strValue.indexOf(".");
			if (parseInt(strValue) == parseFloat(strValue))
				strValue = strValue.substring(0,iPosition);
			return strValue;
		}
	}
	else
		return iValue.toString();
}

WeightCovertor.prototype.getKG = function() {
	return this.trim((this.value * this.kg_interval) / 1000000000000);
}

WeightCovertor.prototype.getSTon = function() {
	return this.trim((this.value * this.ston_interval) / 1000000000000);
}

WeightCovertor.prototype.getLTon = function() {
	return this.trim((this.value * this.lton_interval) / 1000000000000);
}

WeightCovertor.prototype.getLB = function() {
	return this.trim((this.value * this.lb_interval) / 1000000000000);
}

WeightCovertor.prototype.getOZ = function() {
	return this.trim((this.value * this.oz_interval) / 1000000000000);
}

// ******************************************* Area Covertor Constructor ***********************************************
function AreaCovertor(iValue, strUnit) {
	this.setDigitsCount = function (iValue) {
		if (!isNaN(iValue))
			this.digitsCount = iValue;
	}
	if (!isNaN(iValue)) {
		iValue *= 1000000000000;
		switch (strUnit.toLowerCase()) {
			case "scm" :
				this.value = iValue / this.scm_interval;
				break;
			case "sm" :
				this.value = iValue / this.sm_interval;
				break;
			case "skm" :
				this.value = iValue / this.skm_interval;
				break;
			case "sin" :
				this.value = iValue / this.sin_interval;
				break;
			case "sya" :
				this.value = iValue / this.sya_interval;
				break;
			case "smi" :
				this.value = iValue / this.smi_interval;
				break;
		}
	}
	else
		alert("[" + iValue + "] is not a number!");
}

AreaCovertor.prototype.digitsCount = 8;
AreaCovertor.prototype.scm_interval = 6451600000000//10000000000;
AreaCovertor.prototype.sm_interval = 645160000//1000000;
AreaCovertor.prototype.skm_interval = 645.16;
AreaCovertor.prototype.sin_interval = 1000000000000//6451600 ;
AreaCovertor.prototype.sya_interval = 771604938.2716049382716049382716;
AreaCovertor.prototype.smi_interval = 249.09766860524436282012039587797;

AreaCovertor.prototype.trim = function(iValue) {
	if (iValue.toString().length > this.digitsCount) {
		if (iValue.toString().indexOf(".") == -1 || iValue.toString().indexOf("e") != -1)
			return "N/A";
		else {
			var iCounter = 0;
			while (iValue.toString().indexOf(".") != -1) {
				iValue *= 10;
				iCounter++;
			}
			iValue = iValue / Math.pow(10, iCounter);
			var strValue = iValue.toString();
			strValue = strValue.substring(0,this.digitsCount+1);
			var iPosition = strValue.indexOf(".");
			if (parseInt(strValue) == parseFloat(strValue))
				strValue = strValue.substring(0,iPosition);
			return strValue;
		}
	}
	else
		return iValue.toString();
}

AreaCovertor.prototype.getSCM = function() {
	return this.trim((this.value * this.scm_interval) / 1000000000000);
}

AreaCovertor.prototype.getSM = function() {
	return this.trim((this.value * this.sm_interval) / 1000000000000);
}

AreaCovertor.prototype.getSKM = function() {
	return this.trim((this.value * this.skm_interval) / 1000000000000);
}

AreaCovertor.prototype.getSIN = function() {
	return this.trim((this.value * this.sin_interval) / 1000000000000);
}

AreaCovertor.prototype.getSYA = function() {
	return this.trim((this.value * this.sya_interval) / 1000000000000);
}

AreaCovertor.prototype.getSMI = function() {
	return this.trim((this.value * this.smi_interval) / 1000000000000);
}


// ******************************************* Volume Covertor Constructor ***********************************************
function VolumeCovertor(iValue, strUnit) {
	this.setDigitsCount = function (iValue) {
		if (!isNaN(iValue))
			this.digitsCount = iValue;
	}
	if (!isNaN(iValue)) {
		iValue *= 1000000000000;
		switch (strUnit.toLowerCase()) {
			case "ccm" :
				this.value = iValue / this.ccm_interval;
				break;
			case "cl" :
				this.value = iValue / this.cl_interval;
				break;
			case "cm" :
				this.value = iValue / this.cm_interval;
				break;
			case "cin" :
				this.value = iValue / this.cin_interval;
				break;
			case "cya" :
				this.value = iValue / this.cya_interval;
				break;
			case "cg" :
				this.value = iValue / this.cg_interval;
				break;
		}
	}
	else
		alert("[" + iValue + "] is not a number!");
}

VolumeCovertor.prototype.digitsCount = 8;
VolumeCovertor.prototype.ccm_interval = 16387064000000//10000000000;
VolumeCovertor.prototype.cl_interval = 16387064000//10000000000;
VolumeCovertor.prototype.cm_interval = 16387064 //1000000;
VolumeCovertor.prototype.cin_interval = 1000000000000;
VolumeCovertor.prototype.cya_interval = 21433470.507544581618655692729767//6451600 ;
VolumeCovertor.prototype.cg_interval = 3604650149.9090427158283271998575

VolumeCovertor.prototype.trim = function(iValue) {
	if (iValue.toString().length > this.digitsCount) {
		if (iValue.toString().indexOf(".") == -1 || iValue.toString().indexOf("e") != -1)
			return "N/A";
		else {
			var iCounter = 0;
			while (iValue.toString().indexOf(".") != -1) {
				iValue *= 10;
				iCounter++;
			}
			iValue = iValue / Math.pow(10, iCounter);
			var strValue = iValue.toString();
			strValue = strValue.substring(0,this.digitsCount+1);
			var iPosition = strValue.indexOf(".");
			if (parseInt(strValue) == parseFloat(strValue))
				strValue = strValue.substring(0,iPosition);
			return strValue;
		}
	}
	else
		return iValue.toString();
}

VolumeCovertor.prototype.getCCM = function() {
	return this.trim((this.value * this.ccm_interval) / 1000000000000);
}

VolumeCovertor.prototype.getCL = function() {
	return this.trim((this.value * this.cl_interval) / 1000000000000);
}

VolumeCovertor.prototype.getCM = function() {
	return this.trim((this.value * this.cm_interval) / 1000000000000);
}

VolumeCovertor.prototype.getCIN = function() {
	return this.trim((this.value * this.cin_interval) / 1000000000000);
}

VolumeCovertor.prototype.getCYA = function() {
	return this.trim((this.value * this.cya_interval) / 1000000000000);
}

VolumeCovertor.prototype.getCG = function() {
	return this.trim((this.value * this.cg_interval) / 1000000000000);
}

// ******************************************* General Fuctions ***********************************************

function clearForm(oInput) {
	for (i=0; i<oInput.form.length; i++) {
		if (oInput.form[i] != oInput)
			oInput.form[i].value = "";
		else
			oInput.form[i].select();
	}
}

function calculate(oForm) {
	var formName = oForm.name.toLowerCase();
	var disable = true;
	for (i=0; i<oForm.length; i++) {
		if (oForm[i].value == "") {
			disable = false;
			break;
		}
	}
	if (!disable) {
		for (i=0; i<oForm.length; i++) {
			if (!isNaN(oForm[i].value) && oForm[i].value != "") {
				switch (formName) {
					case "lengthfrm" :
						var oConvertor = new LengthCovertor(oForm[i].value, oForm[i].id.substr(1,oForm[i].id.length-1));
						break;
					case "weightfrm" :
						var oConvertor = new WeightCovertor(oForm[i].value, oForm[i].id.substr(1,oForm[i].id.length-1));
						break;
					case "areafrm" :
						var oConvertor = new AreaCovertor(oForm[i].value, oForm[i].id.substr(1,oForm[i].id.length-1));
						oConvertor.setDigitsCount(11)
						break;
					case "volumefrm" :
						var oConvertor = new VolumeCovertor(oForm[i].value, oForm[i].id.substr(1,oForm[i].id.length-1));
						oConvertor.setDigitsCount(11)
						break;
				}
				printValues(oForm, oConvertor);
				break;
			}
		}
	}
	return;
}

function printValues(oForm, oConvertor) {
	for (i=0; i<oForm.length; i++) {
		oForm[i].value = eval("oConvertor.get" + oForm[i].id.substr(1,oForm[i].id.length-1) + "()");
	}
	return;
}

function showCalc(ID) {
	for (i=1; i<5; i++) {
		if (i == ID) {
			document.all["oCalc"+i].style.display = "block";
			document.all["oTab"+i].className = "tabOn";
			document.all["oSeperator"+i].bgColor = "#D4EEB8";
		}
		else {
			document.all["oCalc"+i].style.display = "none";
			document.all["oTab"+i].className = "tabOff";
			document.all["oSeperator"+i].bgColor = "#74C41C";
		}
	}
}