//UC constants

var UC_OPIS = 1

var UC_TANK = 2

var UC_LPG = 4

var UC_PRESSURE = 8

var UC_LITERATURE = 16

var UC_ORDER = 32



function GetCookie(sCookieName){

	sCookieName += "="

	var i = 0

	while (i < document.cookie.length) {

		var j = i + sCookieName.length

		if (document.cookie.substring(i, j) == sCookieName){

			var lEndPos = document.cookie.indexOf (";", j)

			if (lEndPos == -1){

				lEndPos = document.cookie.length

			}

			return unescape(document.cookie.substring(j, lEndPos))

		}



		i = document.cookie.indexOf(" ", i) + 1

		if (i == 0){

			break

		}

	}

	return ""

}





function GetCookieValue(sCookieName, sKey){

	var sCookie = GetCookie(sCookieName)

	var lStartPos = sCookie.indexOf(sKey + "=")

	var sValue

	if (lStartPos < 0 ){

		return ""

	}

	else{

		lStartPos += sKey.length + 1

	}

	var lEndPos = sCookie.indexOf("&", lStartPos)

	if (lEndPos < 0 ){

		lEndPos = sCookie.length

	}

	sValue = unescape(sCookie.substring(lStartPos, lEndPos))

	return sValue.replace(/\+/gi, ' ')

}





function GetParamValue(sKey) {

	sKey += "="

	var iPos1 = window.document.URL.indexOf(sKey)

	if (iPos1 < 0){

		return ""

	}

	var iPos2 = window.document.URL.indexOf("&", iPos1)

	return unescape( iPos2 >= iPos1  ? 

	window.document.URL.substring(iPos1 + sKey.length, iPos2) :

	window.document.URL.substring(iPos1 + sKey.length))

}





function initArray(){

	this.length = initArray.arguments.length

	for (var i = 0; i < this.length; i++)

	this[i] = initArray.arguments[i]

}





function stripCommas(num){

	var temp="" + num;

	while(temp.indexOf(",") >= 0){

		temp = temp.substring(0,temp.indexOf(",")) + temp.substring(temp.indexOf(",")+1,temp.length);

	}

	return(temp);

}





function formatComma(Val){

	var OutString="";

	var valStr="" + Val



	if(Val != Math.floor(Val)){

		len=valStr.indexOf(".");

		OutString +=  valStr.substring(valStr.indexOf("."),valStr.length);

	}

	else{

		len=valStr.length;

	}



	if (len>=3){

		while (len>0){

			TempString=valStr.substring(len-3, len)

			if (TempString.length==3){

				OutString=","+TempString+OutString

				len=len-3;

			}

			else{

				OutString=TempString+OutString

				len=0

			}

		}

		if (OutString.substring(0, 1)==","){

			Val=OutString.substring (1, OutString.length)

		}

		else{

			Val=OutString

		}

	} 

	return (Val);

}





function pressureConv(){

	cval = Math.round((fval * R)*100000)/100000

	window.document.frmPressure.DESTVALUE.value = formatComma(cval);

	window.document.frmPressure.SOURCEVALUE.value = formatComma(fval);

}





function lpgCalc(){

	var mwe=30.068;

	var mwp=44.096;

	var mwi=58.123;

	var mwn=58.123;

	var mwic5=72.15;

	var mwnc5=72.15;

	var tw=0;

	var conce=0;

	var concp=0;

	var conci=0;

	var concn=0;

	var concic5=0;

	var concnc5=0;

	var t=70;

	var maxt=90;

	var pt=0;

	var tm=0;

	var pe=0;

	var pp=0;

	var pi=0;

	var pn=0;

	var pic5=0;

	var pnc5=0;

	var me=0;

	var mp=0;

	var mi=0;

	var mn=0;

	var mic5=0;

	var mnc5=0;

	var msg="";



	conce=parseFloat(window.document.frmLPG.ETHANE.value);

	concp=parseFloat(window.document.frmLPG.PROPANE.value);

	conci=parseFloat(window.document.frmLPG.ISOBUTANE.value);

	concn=parseFloat(window.document.frmLPG.NBUTANE.value);

	concic5=parseFloat(window.document.frmLPG.ISOPENTANE.value);

	concnc5=parseFloat(window.document.frmLPG.NPENTANE.value);

	t=parseFloat(window.document.frmLPG.TEMPERATURE.value);

	tw=conce+concp+conci+concn+concic5+concnc5;



	if(conce==0){

		maxt=130

	}



	if(tw==100&&t>=-40&&t<=maxt){

		tm=conce/mwe+concp/mwp+conci/mwi+concn/mwn+concic5/mwic5+concnc5/mwnc5;

		me=conce/mwe/tm;

		mp=concp/mwp/tm;

		mi=conci/mwi/tm;

		mn=concn/mwn/tm;

		mic5=concic5/mwic5/tm;

		mnc5=concnc5/mwnc5/tm;

		pe=me*(220.18+3.3343*t+0.018303*t*t+5.229E-5*t*t*t);

		pp=mp*(38.48468+0.7557638*t+5.622844E-3*t*t+1.71567E-5*t*t*t+1.51E-08*t*t*t*t);

		pi=mi*(11.96436+0.2623489*t+2.3929406E-3*t*t+1.210796E-5*t*t*t-3.5525E-9*t*t*t*t);

		pn=mn*(7.263343+0.1778697*t+1.739232E-3*t*t+8.1743E-6*t*t*t+1.09E-8*t*t*t*t);

		pic5=mic5*(2.184576+6.127169e-2*t+7.064072e-4*t*t+4.1623e-6*t*t*t+9.3e-9*t*t*t*t);

		pnc5=mnc5*(1.476642+4.349669e-2*t+5.351563e-4*t*t+3.4171e-6*t*t*t+9.5e-9*t*t*t*t);



		if(t<=60){

			de=conce/(-1.316489e-9*t*t*t*t-1.698576e-7*t*t*t-1.73593e-5*t*t-9.235216e-3*t+3.640357)

		}

		else {

			de=conce/(-1.383099E-3*t*t+1.641653e-1*t-1.900759)

		}



		dp=concp/(4.606144-5.570822e-3*t-1.050917e-5*t*t);

		di=conci/(5.021738-5.069614e-3*t-5.388712e-6*t*t);

		dn=concn/(5.168192-4.808923e-3*t-4.352434e-6*t*t);

		dic5=concic5/(5.476189-4.535617e-3*t-1.0256e-6*t*t);

		dnc5=concnc5/(5.5239-0.0044375*t-6.25e-7*t*t);

		dt=100/(de+dp+di+dn+dic5+dnc5);

		pt=pe+pp+pi+pn+pic5+pnc5;

		window.document.frmLPG.PRESSURE.value=(Math.round((pt-14.696)*10))/10;

		window.document.frmLPG.DENSITY1.value=(Math.round(dt*1000))/1000;

		window.document.frmLPG.DENSITY2.value=(Math.round(dt*119.826))/1000;

		

	}

	else {

		msg="Error";

		window.document.frmLPG.PRESSURE.value=msg;

		window.document.frmLPG.DENSITY1.value=msg;

		window.document.frmLPG.DENSITY2.value=msg;



		if(tw!=100&&(t<=-40||t>=maxt)){

			msg=msg+"! Component Compositions do not Total 100% and Temperature is not within range.";



		}

		else {

			if(tw!=100){

				msg=msg+"! Component Compositions do not Total 100%.";

			}

			else {

				msg=msg+"! Temperature out of range (-40 to "+maxt+" °F)";

			}

		}

		alert(msg);

	}

}