// Javascript code copyleft under the Creative Commons license 
// 2007
// luc.jaouen@matelys.com

function calculate() {
var sigma        = parseFloat( document.parameters.sigma.value );
var phi          = parseFloat( document.parameters.phi.value );
var alpha_infty  = parseFloat( document.parameters.alpha_infty.value );
var Lambda       = parseFloat( document.parameters.Lambda.value );
var Lambda_prime = parseFloat( document.parameters.Lambda_prime.value );
var k_0_prime    = parseFloat( document.parameters.k_0_prime.value );
var eta          = 1.84e-5;
var k_0          = eta/sigma; 

var warning = 0;
var my_comments = "";


var M, M_prime;

initialize_all();


// Compute the shape factors and update page
M = 8*eta*alpha_infty/(Lambda*Lambda*phi*sigma);
document.parameters.M.value = M.toPrecision(2);
if (!isNaN(k_0_prime)) {
  M_prime = 8*k_0_prime/(Lambda_prime*Lambda_prime*phi);
  document.parameters.M_prime.value = M_prime.toPrecision(2);
}
else { document.parameters.M_prime.value = ""; }

  if ( (phi <= 0) || (phi > 1) ) {
    warning = 1;
    my_comments = my_comments + "Open porosity should be in the range [0 1].\n";
    document.parameters.phi.focus();
    document.getElementById('id_phi').style.color="red";
  }

  if (alpha_infty < 1) {
    warning = 1;
    my_comments = my_comments + "HF limit of the tortuosity should be greater than or equal to 1.\n";	
    document.parameters.alpha_infty.focus();
    document.getElementById('id_alpha_infty').style.color="red";
  }

  if (Lambda_prime < Lambda) {
    warning = 1;
    my_comments = my_comments + "Thermal charac. length should be greater than or equal to viscous one.\n";
    document.parameters.Lambda.focus();
    document.getElementById('id_Lambda').style.color="red";
    document.parameters.Lambda_prime.focus();
    document.getElementById('id_Lambda_prime').style.color="red";
  }

  if (!isNaN(k_0_prime) && k_0 > k_0_prime) {
    warning = 1;
    my_comments = my_comments + "Thermal static permeability should be greater than or equal to viscous one.\n";
    document.parameters.sigma.focus();
    document.getElementById('id_sigma').style.color="red";
    document.parameters.k_0_prime.focus();
    document.getElementById('id_k_0_prime').style.color="red";
  }	

  // finally
  if (warning) { 
    document.parameters.comments.value = my_comments; 
  }
  else { 
    document.parameters.comments.value = "Parameters are consistent."; 
  }
}

function initialize_all() {
  document.parameters.M.value = "";
  document.parameters.M_prime.value = "";
  document.parameters.sigma.focus();
  document.getElementById('id_sigma').style.color="black";
  document.parameters.phi.focus();
  document.getElementById('id_phi').style.color="black";
  document.parameters.alpha_infty.focus();
  document.getElementById('id_alpha_infty').style.color="black";
  document.parameters.Lambda.focus();
  document.getElementById('id_Lambda').style.color="black";
  document.parameters.Lambda_prime.focus();
  document.getElementById('id_Lambda_prime').style.color="black";
  document.parameters.k_0_prime.focus();
  document.getElementById('id_k_0_prime').style.color="black";
}
