
/***** Hauling Cost Section  *****/

/* Get Fuel Price */ 
var mi4;

function getUrl(url) {   
   var http = new XMLHttpRequest();
   http.open("GET",url,false);
   http.send();
   var thefile = http.responseText; 
   var parfile = new Array();
   var mifile  = new Array(); 
   var mi2     = new Array();
   var mi3     = new Array();
     
   parfile = thefile.split('<tr>')
   mifile  = parfile[22].split('<td'); 
   mi2     = mifile[3].split('>');
   mi3     = mi2[1].split('&nbsp;');
   mi4 = mi3[0] * 1;
   document.getElementById('hfp').value = mi4;
   createCookie('AvgFuelPrice',mi4,4);
}

 
  
/*   User Defined Variables   */
var  mrt;        // Miles Round Trip
var  fp;         // Fuel Price
var  mpg;        // Mile Per Gallon
var  pmc;        // Per Mile Cost that is charged to the customer 
var  fcpm;       // Fuel Cost Per Mile
var  tcpm;       // Total Cost Per Mile
var  minum = 50; // Minimum value if used
   
/*   Variables   */
var  gfu;     // Gallons of fuel used
var  rtfc;    // Round Trip Fuel Cost and Total Fuel Cost
var  mc;      // Material Cost
var  thc;     // Total Hauling Cost
var  tmc;     // Total Material Cost
var  chc;     // Customers Hauling Cost
var  cmc;     // Customers Material Cost
var  tc;      // Total Cost
var  ctc;     // Customers Total Cost
var  profit,dc,ec;

/* Solve hauling cost */
function SolveEq() {
  getValues();
  workEq();
  solve();
}

/* Get the values from the text fields */
function getValues() {
   mrt  = 2*parseFloat(document.getElementById('hmiles').value);  //document.hmiles.value Miles Round Trip
   fp   = parseFloat(document.getElementById('hfp').value);       // Fuel Price per Gallon
   mpg  = parseFloat(document.getElementById('hmpg').value);      // Miles Per Gallon
   ecpm = parseFloat(document.getElementById('hecpm').value);     // Equipment Cost Per Mile
   dcpm = parseFloat(document.getElementById('hdcpm').value);     // Driver Cost Per Mile
   pmc  = parseFloat(document.getElementById('hpmc').value);      // Per Mile Cost that is charged to the customer   
}

/* Calulates the values of the fields */
function workEq() {
   gfu  = mrt/mpg;
   rtfc = gfu*fp;
   thc  = rtfc+(mrt*ecpm)+(mrt*dcpm);
   chc  = mrt*pmc;
   fcpm = fp/mpg;            
   tcpm = parseFloat(fcpm)+parseFloat(ecpm)+parseFloat(dcpm);   
   profit = chc-thc;
   ec = ecpm*mrt;
   dc = dcpm*mrt;
   
/*   Uncomment if useing minimum vales
   if (mrt<=20){ //Minum value of 10 miles
       chc = minum;
      //document.getElementById('min').innerHTML =" <b>Min Cost</b>";
   }
   else
   {
      chc  = mrt*pmc;
      //document.getElementById('min').innerHTML =" ";
   }
*/
   
}
/* Places the values back in the text fields */
function solve() {
   document.getElementById('rtm').value   = mrt;
   document.getElementById('hfcpm').value = fcpm.toFixed(2);
   document.getElementById('htcpm').value = tcpm.toFixed(2);
   document.getElementById('hgfu').value  = gfu.toFixed(2);
   document.getElementById('hrtfc').value = rtfc.toFixed(2);
   document.getElementById('hthc').value  = thc.toFixed(2);
   
   document.getElementById('pfit').value = profit.toFixed(2);
   document.getElementById('cmrt').value = mrt;
   document.getElementById('hchc').value = chc.toFixed(2);
   
   document.getElementById('tec').value = ec;
   document.getElementById('tdc').value = dc;
                                            
}

/* Reset values for Hauling Cost */
function ResetEQ(){
   document.getElementById('hmiles').value = 10;    //document.hmiles.value Miles Round Trip
   document.getElementById('hfp').value    = readCookie('AvgFuelPrice');   // Fuel Price per Gallon
   document.getElementById('hmpg').value   = 10;    // Miles Per Gallon
   document.getElementById('hecpm').value  = 1.00;  // Equipment Cost Per Mile
   document.getElementById('hdcpm').value  = 0.50;  // Driver Cost Per Mile
   document.getElementById('hpmc').value   = 2.50;  // Per Mile Cost that is charged to the customer   
   
   document.getElementById('rtm').value   = 0;
   document.getElementById('hfcpm').value = 0;
   document.getElementById('htcpm').value = 0;
   document.getElementById('hgfu').value  = 0; 
   document.getElementById('hrtfc').value = 0;
   document.getElementById('hthc').value  = 0;
   
   document.getElementById('pfit').value = 0;
   document.getElementById('cmrt').value = 0;
   document.getElementById('hchc').value = 0;
   
   document.getElementById('tec').value = 0;
   document.getElementById('tdc').value = 0;
     
}

/*****  End of Hauling Cost Section  *****/   
   
/***** Materials Section *****/   
var length,width,height,weight;
var mweight,cpy,yards;
var loads,wloads,yloads;
var mxload, mxweight;
var price;

function SolveEqMat() {
  getValuesMat();
  workEqMat();
  solveMat();
}

function getValuesMat() {
  yards  = parseFloat(yard.value);
  weight = parseFloat(wei.value);
  cpy    = parseFloat(cost.value);
  mxload = parseFloat(mxl.value);
  mxweight = parseFloat(mxw.value);
}

function workEqMat() {
   mweight = yards*weight;
   wloads  = mweight/mxweight; 
   yloads  = yards/mxload;
   price   = cpy*yards;
}

function SolveEqYard(){
  length = parseFloat(len.value);
  width  = parseFloat(wid.value);
  height = parseFloat(hei.value);
  
  yards   = ((length*width*(height/12))/27);
  
  yard.value  = yards.toFixed(3);
}

function solveMat() {
  weigh.value = mweight.toFixed(2);
  weigh.value = mweight.toFixed(2);
  cpyard.value  = price.toFixed(2);
  
  if(wloads >= yloads)
     if(wloads >= 1)
        load.value = wloads.toFixed(2);
     else
        load.value = 1; 
  else 
     if (yloads >= 1)
        load.value = yloads.toFixed(2);
     else
        load.value = 1; 
}

/***** End of Materials Section *****/ 

/*****  Hauling + Materials *****/
function SolveAll(){
   SolveEq();
   if (yard.value<=1 ){
     SolveEqYard();
     SolveEqMat();
   }  
   else
      SolveEqMat();
   
   hauling.value   =  chc*(Math.ceil(load.value));
   materials.value =  cpyard.value;
   gtotal.value    =  parseFloat(hauling.value)+parseFloat(materials.value);
   pyard.value     =  round(gtotal.value/yards,4)   
}

/***** End of Hauling + Materials *****/



function round(number,X) {
  // rounds number to X decimal places, defaults to 2
  X = (!X ? 2 : X);
  return Math.round(number*Math.pow(10,X))/Math.pow(10,X);
}

function addit(){
   document.summing.s.value = parseInt(document.summing.a.value) + parseInt(document.summing.b.value)
}



//function myOnClickHandler(event) {
//    
//    var element = document.elementFromPoint(event.pageX, event.pageY);
//    if (element.type === "submit"){
      //alert("You've clicked on an HTML element named " + element.tagName);
//     SolveEq();
//    }  
//}
