//The common javascript functions used throughout the site

//global variables
var kgsflag = false;
var litresflag = false;
var m3flag = false;
var gasflag = false;
var oilflag = false;
var lpgkgsflag = false;
var lpglitresflag = false;
var oldVal = '';

//return the current element based on their id
function getElementValue(id, num) {
if (id == "") return;
var e = document.getElementById(id);
var val = e.value;
if (val == "") {
 if (num) {
  val = '0.00';
 } 
} else {
 if (num) {
  if (isNaN(val)) val = 0;
  val = parseFloat(val);
  if (val <= 0) val = 0;
  val = val.toFixed(2);
  if (val == '-0.00') val = 0;
 }
}

e.value = val;
return val; 

}

//check the percent of each process and disallow if 100%
function checkProcess(e, label) {

//calculate the current total
var elArray = new Array("filmextrusion","profileextrusion","injectionmoulding","rotationalmoulding","blowmoulding","compounding","vacuumthermoforming","compressionmoulding","expandedpolystyrene");
//set the current total
var currtotal = 0;
var pt = document.getElementById("pt");
for (var i=0; i < elArray.length; i++) {
  currtotal = parseFloat(currtotal) + parseFloat(getElementValue(elArray[i], true));
}

if (currtotal > 100) {
  var newval = e.value;
  var diff = currtotal - 100;
  var newval = parseFloat(newval - diff);
  newval = newval.toFixed(2);
  e.value = newval;
  currtotal = 100;
 } else {
 currtotal = currtotal.toFixed(2);
 }

pt.innerHTML = currtotal;
document.tool_form.processtypepercent.value = currtotal;
}

//check the percent of polymers processed and disallow if 100%
function checkPoly(e, label) {

//calculate the current total
var elArray = new Array("pvc","hdpe","pet","ldpe","pp","ps","urethanes","other");
//set the current total
var currtotal = 0;
var pt = document.getElementById("poly");
for (var i=0; i < elArray.length; i++) {
  currtotal = parseFloat(currtotal) + parseFloat(getElementValue(elArray[i], true));
}


    
if (currtotal > 100) {
  var newval = e.value;
  var diff = currtotal - 100;
  var newval = parseFloat(newval - diff);
  newval = newval.toFixed(2);
  e.value = newval;
  currtotal = 100;
 } else {
 currtotal = currtotal.toFixed(2);
 }

pt.innerHTML = currtotal;
document.tool_form.polymersprocessedpercent.value = currtotal;
}

//do the conversions
function doConvert(s, id) {

var e = document.getElementById(s);
if (e == null) {
 var switch_val = s;
} else {
 var switch_val = e.value;
}
var i = document.getElementById(id);
var val = getElementValue(id, true);

switch (switch_val) {
  
  case "gj":
   if (!gasflag) {
   var result = val * 278;
  gasflag = true;
  i.focus();
  } else {
  var result = val/278;
  gasflag = false;
  e.selectedIndex = 0;
  }
  break;
  
  case "litres":
   if (!oilflag) {
  var result = val * 11.44;
  oilflag = true;
  i.focus();
  } else {
  var result = val/11.44;
  oilflag = false;
  e.selectedIndex = 0;
  }
  
  break;
  
  default:
  var result = val;

}

result = parseFloat(result);
result = result.toFixed(2);

i.value = result;
return result;

}

function doConvertLPG(s, id) {

var e = document.getElementById(s);
if (e == null) {
 var switch_val = s;
} else {
 var switch_val = e.value;
}
var i = document.getElementById(id);
var val = getElementValue(id, true);

switch (switch_val) {
  
  case "kgs":
   if (!lpgkgsflag) {
  var result = val * 14.11;
  lpgkgsflag = true;
  i.focus();
  } else {
  var result = val/14.11;
  lpgkgsflag = false;
  e.selectedIndex = 0;
  }
  
  break;
  
  case "litres":
   if (!lpglitresflag) {
  var result = val * 7.4;
  lpglitresflag = true;
  i.focus();
  } else {
  var result = val/7.4;
  lpglitresflag = false;
  e.selectedIndex = 0;
  }
  
  break;
 
  default:
  var result = val;

}

result = parseFloat(result);
result = result.toFixed(2);

i.value = result;
return result;

}

function doConvert6(s, id) {

var e = document.getElementById(s);
if (e == null) {
 var switch_val = s;
} else {
 var switch_val = e.value;
}

var i = document.getElementById(id);
var val = getElementValue(id, true);

if (val == 0) return;

switch (switch_val) {
  
  case "kgs":
  if (!kgsflag) {
  var result = val * 1000;
  kgsflag = true;
  i.focus();
  } else {
  var result = val/1000;
  kgsflag = false;
  e.selectedIndex = 0;
  }
  break;
  
  default:
  var result = val;
  
}

result = parseFloat(result);
result = result.toFixed(2);

i.value = result;
return result;

}

function doConvert7(s, id) {

var e = document.getElementById(s);
if (e == null) {
 var switch_val = s;
} else {
 var switch_val = e.value;
}
var i = document.getElementById(id);
var val = getElementValue(id, true);

if (val == 0) return;

switch (switch_val) {
  
  case "litres":
 if (!litresflag) {
  var result = val * 1000;
  litresflag = true;
  i.focus();
  } else {
  var result = val/1000;
  litresflag = false;
  e.selectedIndex = 0;
  }
  break;
  
  default:
  var result = val;
  
}

result = parseFloat(result);
result = result.toFixed(2);

i.value = result;
return result;

}

function doConvert9(s, id) {

var e = document.getElementById(s);
if (e == null) {
 var switch_val = s;
} else {
 var switch_val = e.value;
}
var i = document.getElementById(id);
var val = getElementValue(id, true);

if (val == 0) return;

switch (switch_val) {
  
  case "m3":
  if (!m3flag) {
  var result = val * 0.15;
  m3flag = true;
  i.focus();
  } else {
  var result = val/0.15;
  m3flag = false;
  e.selectedIndex = 0;
  }
  break;
  
  default:
  var result = val;
  
}

result = parseFloat(result);
result = result.toFixed(2);

i.value = result;
return result;

}

function doTotal(flag) {

var total = 0;
  switch (flag) {
    case "k":
    var t = document.getElementById("energyused");
    var fldary = new Array("electricity","gas","oil","lpg");
        
    for (var i=0; i < fldary.length; i++) {
     total = parseFloat(total) + parseFloat(getElementValue(fldary[i], true));
    }
    total = total.toFixed(2);
    t.value = total;
    break;
    
    case "c":
    var t = document.getElementById("totalcost");
    var fldary = new Array("electricitycost","gascost","oilcost","lpgcost");
        
    for (var i=0; i < fldary.length; i++) {
     total = parseFloat(total) + parseFloat(getElementValue(fldary[i], true));
    }
    total = total.toFixed(2);
    t.value = total;
    break;
  
  }


}


function checkForm(evt) {
var frm = document.tool_form;
var msg = '';
if (frm == null) return;

var pt_val = parseFloat(frm.processtypepercent.value);
var pp_val = parseFloat(frm.polymersprocessedpercent.value);

if (isNaN(pt_val)) pt_val = 0;
if (isNaN(pp_val)) pp_val = 0;

if (pt_val < 100) {
 msg = msg + "The Process Type is not 100%. \n Please fill in the missing numbers. \n\n"
}

if (pp_val < 100) {
 msg = msg + "The Processed is not 100%. \n Please fill in the missing numbers.\n\n"
}

if (frm.ems[0].checked == false && frm.ems[1].checked == false) {
 msg = msg + "Please select an option for the environmental management system.";
}

if (msg != '') {
 alert(msg);
 if (document.all) {
  evt.returnValue = false;
 } else {
   evt.preventDefault();
  
 }
   return false;
}

}

function doRetro(url, val) {

var ans = window.confirm('Please note that clicking "OK" will refresh the screen\n to retrive data for the year if it exists.\n If you have done changes please click "Cancel"\n and save your data before proceeding.');
if (ans) {
document.location.href = url+'/'+val;
}

}

function doFocus(e, action) {

var change = false;
if (action == 'focus') {
oldVal = e.value;
e.value = '';
} else {
var newVal = e.value;

if (newVal == '') {
  change = true;
}
if (newVal == '') newVal = 0;
if (isNaN(newVal)) newVal = 0;
newVal = parseFloat(newVal);
if (change) {
e.value = oldVal;
} else {
e.value = newVal;
}
oldVal = '';
}

}
