//<!--
/* >-- > for small convert WebParts */
var direction12 = true;

function initSmallConvertForm(idForm,idE1,idE2,idFunction,initV)
{
	//> check & set Parameters
	if("" == idForm || "" == idE1 || "" == idE2 || "" == idFunction) return false;
	if(!idForm || !idE1 || !idE2 || !idFunction) return false;
	var oForm = document.getElementById(idForm);
	if(!oForm) return false;
	oForm.action = "javascript:return false;";
	var oE1 = document.getElementById(idE1);
	if(!oE1) return false;
	//oE1.value = 1;

	var oE2 = document.getElementById(idE2);
	if(!oE2) return false;

	var oFunction = document.getElementById(idFunction);
	if(!oFunction) return false;
	if(!oFunction.value || "" == oFunction.value) return false;
	//< check & set Parameters

	direction12 = true;
		
	oE1.value = initV;
	convertE(oForm,idForm,oE1,idE1,oE2,idE2,oFunction,idFunction);
	return false;
}

function convertE(oForm,idForm,oE1,idE1,oE2,idE2,oFunction,idFunction)
{
	maxPrecision = 10;
	//> check & set Parameters
	if(!oForm || "" == oForm){
		if(!idForm || "" == idForm) return false;
		else oForm = document.getElementById(idForm);
	}
	if(!oE1 || "" == oE1){
		if(!idE1 || "" == idE1) return false;
		else oE1 = document.getElementById(idE1);
	}
	if(!oE2 || "" == oE2){
		if(!idE2 || "" == idE2) return false;
		else oE2 = document.getElementById(idE2);
	}
	if(!oFunction || "" == oFunction){
		if(!idFunction || "" == idFunction) return false;
		else oFunction = document.getElementById(idFunction);
	}
	if(!oFunction.value || "" == oFunction.value) return false;
	//< check & set Parameters
	
	var x, strRule = oFunction.value, re = /\,/g, tmpV = "";;
	if(!isRule(strRule)) return false;
//alert(direction12 + "\n" + oE1.value + "\n" + strRule);
	if(direction12)
	{
		x = oE1.value;
		x = x.replace(re,".");
		if(1 > x.length) x = 0;
		//else x = ifNum(x,0);
		//if(0 == x) oE1.value = 0;
		if(isNaN(x))
		{
			tmpV = oE2.value;
			oE1.style.backgroundColor = "#FF0000";
			oE1.style.color = '#FFFFFF';
//			oE2.style.backgroundColor = "#FF0000";
//			oE2.style.color = '#FFFFFF';
//			oE2.value = " ! ! ! ! ! ! ! ";
			setTimeout(function(){/*oE2.value=tmpV; oE2.style.backgroundColor='';oE2.style.color='';*/oE1.style.backgroundColor='';oE1.style.color='';},100);
			//validateEValue(idForm,idE1);
			return false;
		}
		oE1.value = x;
		oE2.value = removeEndZeros(calculateValueFromRule(strRule,x,"f").toFixed(maxPrecision));
	}
	else
	{
		x = oE2.value;
		x = x.replace(re,".");
		if(1 > x.length) x = 0;
		//else x = ifNum(x,0);
		//if(0 == x) oE2.value = 0;
		if(isNaN(x))
		{
			tmpV = oE1.value;
//			oE1.value = " ! ! ! ! ! ! ! ";
//			oE1.style.backgroundColor = "#FF0000";
//			oE1.style.color = '#FFFFFF';
			oE2.style.backgroundColor = "#FF0000";
			oE2.style.color = '#FFFFFF';
			setTimeout(function(){/*oE1.value=tmpV; oE1.style.backgroundColor='';oE1.style.color='';*/oE2.style.backgroundColor='';oE2.style.color='';},100);
			//validateEValue(idForm,idE2);
			return false;
		}
		oE2.value = x;
		oE1.value = removeEndZeros(calculateValueFromRule(strRule,x,"i").toFixed(maxPrecision));
	}
}

function changeConvertDirection(direct)
{
	direction12 = direct;
	return false;
}

function cKeyCheck()
{
	var oForm = null,idForm = "",oE1 = null,idE1 = "",oE2 = null,idE2 = "",oFunction = null,idFunction = "", tmpV = 2, activeE = "";
	var k;
	try{k = event.keyCode;}
	catch(e){}

	for (var i=0;i<arguments.length;i++)
		switch(i)
		{
			case 0:	oForm = arguments[i];break;
			case 1:	idForm = arguments[i];break;
			case 2:	oE1 = arguments[i];break;
			case 3:	idE1 = arguments[i];break;
			case 4:	oE2 = arguments[i];break;
			case 5:	idE2 = arguments[i];break;
			case 6:	oFunction = arguments[i];break;
			case 7:	idFunction = arguments[i];break;
			case 8:	tmpV = arguments[i];break;
		}

	if (13 == k)
	{
		convertE(oForm,idForm,oE1,idE1,oE2,idE2,oFunction,idFunction);
		return false;
	}
	else
	{
		if(1 == tmpV) {activeE = idE2; tmpV = idE1; changeConvertDirection(false);}
		else {activeE = idE1; tmpV = idE2; changeConvertDirection(true);}
	}
	
	var needToCheck = true;
	
	//if((59 > k && k > 47) || (106 > k && k >96)) needToCheck = false;// numbers
//alert(k);
	switch(k)
	{
		//16 - shift
		//case 8://backslash
		case 9://tab
		//case 27://esc
		//case 33://pgup
		//case 34://pgdn
		case 35://end
		case 36://home
		case 37://up
		case 38://dn
		case 39://left
		case 40://right
		//case 45://insert
		//case 46://del
			needToCheck = false;
	}
	
	if(needToCheck) validateEValue(idForm,activeE,k);
	clearE(idForm,tmpV);
	return false;
}

function validateEValue(idForm,idE)
{
	if(!idForm || "" == idForm) return false;
	var oForm = document.getElementById(idForm);
	if(!oForm) return false;
	
	if(!idE || "" == idE) return false;
	oE = document.getElementById(idE);
	
	if(!oE) return false;

	var tmpV = oE.value;
	if(!tmpV) {tmpV = ""; return false;}
	if(1 > tmpV.length) return false;
	var posX, wasDot = false, deleteSymbol = false, wasMinPlus = false, rezV = "";
	for(i=0;i<=tmpV.length;i++)
	{
		deleteSymbol = false;
		//posX = tmpV.length-i;
		curSymb = tmpV.substr(i,1);
		if(isNaN(curSymb))
		{	switch(curSymb)
			{
				case ".":
				case ",":
					if(wasDot || (i==1 && wasMinPlus)) deleteSymbol = true;
					else wasDot = true;
					break;
				case "-":
					if(wasMinPlus || 0 < rezV.length) deleteSymbol = true;
					else wasMinPlus = true;
					break;
				default:
					deleteSymbol = true;
			}
		}
		if(" " == curSymb) deleteSymbol = true;
		if(!deleteSymbol) rezV = rezV + curSymb;
	}
	
	oE.value = rezV;
	return false;
}

function clearE(idForm,idE)
{
	if(!idForm || "" == idForm) return false;
	var oForm = document.getElementById(idForm);
	if(!oForm) return false;
	
	if(!idE || "" == idE) return false;
	oE = document.getElementById(idE);
	
	if(!oE) return false;
	
	oE.value = "";
	return false;
}

/* <--< for small convert WebParts*/
//-->