var FINE_AGG = 52.0;
var CEMENT_SACK = 94.0;
var LB_CU_FT = 96.76;
var ONE_PERC_AIR = 0.27;
var calcDone = false;
var density = 0;
var ratio = 0;

/*function MixCalc() {
	
};
var mc = new MixCalc();

mc.Print = {
		date: ""
	}*/
	
Print = {
		date: ""
	}	

var SP_GR = {
	CEMENT: 3.15,
	SAND: 2.65,
	LW: 1.72,
	F: 2.2,
	C: 2.70,
	TIMES: 62.43,
	SP_GR: 1.55
}

var SP_GR_PCF = {
	CEMENT: SP_GR.CEMENT*SP_GR.TIMES,
	SAND: SP_GR.SAND*SP_GR.TIMES,
	LW: SP_GR.LW*SP_GR.TIMES,
	F: SP_GR.F*SP_GR.TIMES,
	C: SP_GR.C*SP_GR.TIMES
}

var Loose = {
	C_ASH: 76,
	F_ASH: 72,
	cement: 0,
	SAND: 100,
	LW_WET: 62.0,
	LW_DRY: 50.0,
	water: 36.0,
	ashC: 0,
	ashF: 0,
	agg: 0,
	struc: 0,
	sand: 0
}

var Weight = {
	C_ASH: 76,
	F_ASH: 72,
	cement: 0,
	ashC: 0,
	ashF: 0,
	sand: 0,
	struc: 0,
	agg: 0,
	WATER: 8.34,
	water: 300.0,
	projected: 0,
	mid: 0,
	air: 0
}

var Absolute = {
	cement: 2.87,
	ashC: 0.00,
	ashF: 0.73,
	sand: 7.86,
	struc: 9.18,
	agg: 0.00,
	water: 4.81,
	air: 1.62,
	yield: 0,
	totCuFt: 0,
	dry: 0,
	basis: 27.0
}

var Calc = {
	c1: 0,
	c2: 0,
	c3: 0,
	c4: 0,
	c5: 0,
	c6: 0,
	c7: 0,
	c8: 0
}

var logMsgInit = 'DEBUG LOG <a href="#" onclick="clearLog(); return false;">Clear Log</a><br/>';
var logMsg = logMsgInit;

function logIt(msg) {
    logMsg += msg += "<br />";
    if($('debugLog') != undefined){
        $('debugLog').update(logMsg);
    }
}
  
function clearLog() {
    logMsg = logMsgInit;
    if($('debugLog') != undefined){
        $('debugLog').update(logMsg);
    }
}

function calcs(){
	Print.date = $F('sDate');
	
	//logIt('calcs');
	Loose.cement = Number($F('cementTypeWt'))/CEMENT_SACK;
	Loose.ashC = Number($F('ashClassCwt'))/Weight.C_ASH;
	Loose.ashF = Number($F('ashClassFwt'))/Weight.F_ASH;
	Weight.sand = Number($F('sandLs'))*Loose.SAND;
	Weight.struc = Number($F('strucLs'))*Loose.LW_WET;
	Weight.agg = Number($F('aggLs'))*FINE_AGG;
	Loose.water = Number($F('waterWt'))/Weight.WATER;
	Weight.cement = Number($F('cementTypeWt'));
	Weight.ashC = Number($F('ashClassCwt'));
	Weight.ashF = Number($F('ashClassFwt'));
	//Weight.water = $F('waterWt');
	Weight.projected = Weight.cement+Weight.ashC+Weight.ashF+Weight.sand+Weight.struc+Weight.agg+Weight.water;
	//Number(Weight.cement+Weight.ashC+Weight.ashF+Weight.sand+Weight.struc+Weight.agg+Weight.water);
	//Weight.projected = setPrecision(proj,1);
	//logIt('Loose.cement:'+Loose.cement);
	updateDiv('cementTypeLs',setPrecision(Loose.cement,2));
	updateDiv('ashClassCls',setPrecision(Loose.ashC,1));
	updateDiv('ashClassFls',setPrecision(Loose.ashF,1));
	updateDiv('sandWt',setPrecision(Weight.sand,1));
	updateDiv('strucWt',setPrecision(Weight.struc,1));
	updateDiv('aggWt',setPrecision(Weight.agg,1));
	updateDiv('waterLs',setPrecision(Loose.water,1));
	updateDiv('projectedWt',setPrecision(Weight.projected,1));
	
	Absolute.cement = Weight.cement/SP_GR_PCF.CEMENT;
	Absolute.ashC = Weight.ashC/SP_GR_PCF.C;
	Absolute.ashF = Weight.ashF/SP_GR_PCF.F;
	Absolute.sand = Weight.sand/SP_GR_PCF.SAND;
	Absolute.struc = Weight.struc/SP_GR_PCF.LW;
	Absolute.agg = Weight.agg/LB_CU_FT;
	Absolute.water = Weight.water/SP_GR.TIMES;

	updateDiv('cementTypeAb',setPrecision(Absolute.cement,2));
	updateDiv('ashClassCab',setPrecision(Absolute.ashC,2));
	updateDiv('ashClassFab',setPrecision(Absolute.ashF,2));
	updateDiv('sandAb',setPrecision(Absolute.sand,2));
	updateDiv('strucAb',setPrecision(Absolute.struc,2));
	updateDiv('aggAb',setPrecision(Absolute.agg,2));
	updateDiv('waterAb',setPrecision(Absolute.water,2));
	
	Weight.mid = (((Weight.cement+Weight.ashC+Weight.ashF)/100)*Number($F('mid')));
	Weight.air = (Number($F('air'))*(Loose.cement+Loose.ashC+Loose.ashF));
	updateDiv('midWt',setPrecision(Weight.mid,1));
	updateDiv('airWt',setPrecision(Weight.air,1));
	
	Loose.air = Number($F('airLs'));
	Absolute.air = (Loose.air*ONE_PERC_AIR);
	updateDiv('airAb',setPrecision(Absolute.air,2));
	
	Absolute.totCuFt = 
	(Absolute.cement+Absolute.ashC+Absolute.ashF+Absolute.sand+Absolute.struc+Absolute.agg+Absolute.water+Absolute.air);
	updateDiv('totCuFtAb',setPrecision(Absolute.totCuFt,2));
	
	Absolute.yield = Absolute.totCuFt - 27;
	updateDiv('yieldAb',setPrecision(Absolute.yield,2));
	
	Loose.agg = Number($F('aggLs'));
	Loose.struc = Number($F('strucLs'));
	var struc = Number($F('struc'));
	var percMoist = Number($F('percMoist'));
	Absolute.dry = (struc/(1+percMoist/100));
	updateDiv('strucDry',Absolute.dry);
	//little green box
	Calc.c1 = Weight.cement * 1.2;
	Calc.c2 = Weight.ashC * 1.2;
	Calc.c3 = Weight.ashF * 1.2;
	Calc.c4 = Weight.sand / 1.01;
	Calc.c5 = Loose.struc*Absolute.dry;
	Calc.c6 = (Loose.agg * FINE_AGG)/1.1;
	Calc.c7 = 0;
	Calc.c8 = (Calc.c1+Calc.c2+Calc.c3+Calc.c4+Calc.c5+Calc.c6)/Absolute.totCuFt+3;
	updateDiv('calc1',setPrecision(Calc.c1,0));
	updateDiv('calc2',setPrecision(Calc.c2,0));
	updateDiv('calc3',setPrecision(Calc.c3,0));
	updateDiv('calc4',setPrecision(Calc.c4,0));
	updateDiv('calc5',setPrecision(Calc.c5,0));
	updateDiv('calc6',setPrecision(Calc.c6,0));
	updateDiv('calc7',setPrecision(Calc.c7,0));
	updateDiv('calc8',setPrecision(Calc.c8,2));
	
	density = Weight.projected/Absolute.totCuFt;
	ratio = Weight.water/(Weight.cement+Weight.ashC+Weight.ashF);
	updateDiv('density',setPrecision(density,1));
	updateDiv('ratio',setPrecision(ratio,2));
	
	updateDiv('uRatio',setPrecision(ratio,2));
	var sacks = (Weight.cement+Weight.ashC+Weight.ashF)/94;
	updateDiv('sacks',setPrecision(sacks,2));
	
	Loose.sand = $F('sandLs');
	calcDone = true;
}

function updateDiv(divName,divVal){
	$(divName).update("<div id='"+divName+"' name='"+divName+"'>"+divVal+"</div>");
}

function setPrecision(numIn,dec){
	var f = 1;
	for(var i = 0; i < dec; i++){
		f *= 10;
	}
	var numS = numIn.toString();
	var pre = "";
	var post = "";
	var decPos = numS.indexOf(".");
	
	if(decPos > 0){
		var numOut = (Math.round(numIn*f)/f);
		numS = numOut.toString();
		post = numS.substr(decPos+1,numS.length);
		for(var i = post.length; i < dec; i++){
			post = post.concat('0');
		}
		pre = numS.substr(0,decPos);
		if(dec > 0) {
			numS = pre.concat('.');
			numS = numS.concat(post);
		} else numS = pre;
	}else if((decPos == -1) && dec > 0){
		numS = numS.concat(".");
		for(var i = 0; i < dec; i++){
			numS = numS.concat('0');
		}
	}
	//logIt('numIn:'+numIn+' f:'+f+' numS:'+numS+' decPos:'+decPos+' pre:'+pre+' post:'+post);
	
	return numS;
}

function myOnload(){
	$('divPrint').toggle();
	//var e = (event || window.event);
	//Event.observe(window, 'keypress', isEnter, true);
	
	//first, tell the browsers to react to the event
if( document.captureEvents && Event.KEYUP ) {
  //remove this part if you do not need Netscape 4 to work
  document.captureEvents( Event.KEYUP );
}
/* this next line tells the browser to detect a keyup
event over the whole document and when it detects it,
it should run the event handler function 'alertkey' */
document.onkeyup = alertkey;

	
	
	Print.date = formatDate();
	$('sDate').value = Print.date;
	$('setSand').value = SP_GR.SAND;
	$('setF').value = SP_GR.F;
	$('setC').value = SP_GR.C;
	$('setSandLs').value = Loose.SAND;
	$('setGr').value = SP_GR.SP_GR;
	calcs();
}




//now create the event handler function to process the event
function alertkey(e) {
  if( !e ) {
    //if the browser did not pass the event information to the
    //function, we will have to obtain it from the event register
    if( window.event ) {
      //Internet Explorer
      e = window.event;
    } else {
      //total failure, we have no way of referencing the event
      return;
    }
  }
  if( typeof( e.keyCode ) == 'number'  ) {
    //DOM
    e = e.keyCode;
  } else if( typeof( e.which ) == 'number' ) {
    //NS 4 compatible
    e = e.which;
  } else if( typeof( e.charCode ) == 'number'  ) {
    //also NS 6+, Mozilla 0.9+
    e = e.charCode;
  } else {
    //total failure, we have no way of obtaining the key code
    return;
  }
  /*window.alert('The key pressed has keycode ' + e +
    ' and is key ' + String.fromCharCode( e ) );*/
  /*logIt('The key pressed has keycode ' + e +
    ' and is key ' + String.fromCharCode( e ) );*/
  
  if(e == Event.KEY_RETURN) calcs();
  //logIt('ENTER');
}




function isEnter(a){
	logIt('a:'+a);
	var e = (window.Event);
	var isIt = false;
	var iCode = ( e.keyCode || e.charCode || e.which) ;
	if(iCode == Event.KEY_RETURN) isIt = true;
	//window.key == Event.KEY_RETURN;
	//(((window.Event.which) && (window.Event.which == Event.KEY_RETURN)) ||            ((window.Event.button) && (window.Event.button == Event.KEY_RETURN)));
	clearLog();
	logIt('isEnter'+isIt+' '+iCode+' callee:'+a);
	for(i = 0; i < a.length; i++){
		logIt('window.Event:'+a[i]);
	}
	//if(window.Event == Event.KEY_RETURN) logIt('RETURN');
	//if(Event.element(e) == Event.KEY_RETURN) logIt('RETURN');
}

var MONTH_NAMES=new Array('January','February','March','April','May','June','July','August','September','October','November','December','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');

function formatDate(){
	var date = new Date();
	var sDate = "";
	var y=date.getYear()+"";
	var M=date.getMonth()+12;
	if(y.length < 4){
		y=""+(y-0+1900);
	}
	sDate = MONTH_NAMES[M]+". "+y;
	return sDate;
	//logIt('sDate:'+MONTH_NAMES[M+11]+". "+y);
}

function onLoadPrint(){
	updateDiv('sDate',Print.date);
}

function showPrint(){
	if(!calcDone) calcs();
	
	$('divPrint').toggle();
	$('divCalc').toggle();
	
	if($('divPrint').visible()){
			updateDiv('pDate',Print.date);
			var dia = "FOR USE IN A CONCRETE PUMP WITH A MINIMUM "+$F('dia')+'" LINE';
			updateDiv('pDia',dia);
			updateDiv('pMixID',$F('designNo'));
			updateDiv('pProject',$F('project'));
			updateDiv('pContractor',$F('contractor'));
			updateDiv('pConcrete',$F('util'));
			updateDiv('pStrength',$F('strength'));
			
			updateDiv('pGravCement',setPrecision(SP_GR.CEMENT,2));
			updateDiv('pGravF',setPrecision(SP_GR.F,2));
			updateDiv('pGravC',setPrecision(SP_GR.C,2));
			updateDiv('pGravAgg',setPrecision(SP_GR.SP_GR,2));
			updateDiv('pGravAggCrs',setPrecision(SP_GR.LW,2));
			updateDiv('pGravSand',setPrecision(SP_GR.SAND,2));
			updateDiv('pGravTotAir',setPrecision(Loose.air,1));
			
			updateDiv('pWtCement',setPrecision(Weight.cement,0));
			updateDiv('pWtF',setPrecision(Weight.ashF,0));
			updateDiv('pWtC',setPrecision(Weight.ashC,0));
			updateDiv('pWtAgg',setPrecision(Weight.agg,0));
			updateDiv('pWtAggCrs',setPrecision(Weight.struc,0));
			updateDiv('pWtSand',setPrecision(Weight.sand,0));
			updateDiv('pWtWater',setPrecision(Weight.water,0));
			updateDiv('pWtTot',setPrecision(Weight.projected,0));
			
			updateDiv('pAbCement',setPrecision(Absolute.cement,2));
			updateDiv('pAbF',setPrecision(Absolute.ashF,2));
			updateDiv('pAbC',setPrecision(Absolute.ashC,2));
			updateDiv('pAbAgg',setPrecision(Absolute.agg,2));
			updateDiv('pAbAggCrs',setPrecision(Absolute.struc,2));
			updateDiv('pAbSand',setPrecision(Absolute.sand,2));
			updateDiv('pAbWater',setPrecision(Absolute.water,2));	
			updateDiv('pAbTotAir',setPrecision(Absolute.air,2));	
			updateDiv('pAbTot',setPrecision(Absolute.totCuFt,2));
			
			updateDiv('pLsCement',setPrecision(Loose.cement,1));
			updateDiv('pLsF',setPrecision(Loose.ashF,1));
			updateDiv('pLsC',setPrecision(Loose.ashC,1));
			updateDiv('pLsAgg',setPrecision(Loose.agg,1));
			updateDiv('pLsAggCrs',setPrecision(Loose.struc,1));
			updateDiv('pLsSand',setPrecision(Loose.sand,1));
			updateDiv('pLsWater',setPrecision(Loose.water,1));
			
			updateDiv('pAdMid',setPrecision(Weight.mid,1));
			updateDiv('pAdAir',setPrecision(Weight.air,1));
			
			updateDiv('pPlastic',setPrecision(Calc.c3,1));
			updateDiv('pEquil',setPrecision(Calc.c8,1));
			updateDiv('pSlump',$F('slump'));
			updateDiv('pLb',setPrecision(ratio,2));
			
			var pSack = setPrecision(Loose.water,0)/((Weight.cement+Weight.ashF+Weight.ashC)/94);
			updateDiv('pSack',setPrecision(pSack,2));
			
			updateDiv('pCopy1',$F('copy1'));
			updateDiv('pCopy2',$F('copy2'));
			updateDiv('pCopy3',$F('copy3'));
			updateDiv('pCopy4',$F('copy4'));
			
			//logIt('Loose.water:'+Loose.water+' / Weight.cement:'+Weight.cement+' Weight.ashF:'+Weight.ashF+' Weight.ashC:'+Weight.ashC+' / 94');
	}
}



