// JavaScript Document

<!-- JavaScript Follows

//
// Main function
//

// Name of fields to check
var fieldRequired = Array("TwinOxide_Solution", "Dosing_Concentration", "Process_Dose_Volume",  "Dose_Repeat_Rate", "Batch_Size");
// Accompagnying field description to appear in the dialog box
var fieldDescription = Array("TwinOxide Solution", "Dosing Concentration", "Process Dose Volume", "Dose Repeat Rate", "Batch Size");

var alertMsg;

// Get date for cookie
var now = new Date();
fixDate(now);
// Keep our cookies for a year
now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);


function doCalcTwinBatch(inFormobj) {
	alertMsg = "Please correct the following fields:\n";
	var l_Msg = alertMsg.length;

	//	Accept empty columns, however if ANY of the fields of the column is filled in ALL fields must be correct
	for (var i = 1; i <= 3; i++){
		setColumnCookie(inFormobj, i);
		if (isColumnEmpty(inFormobj, i) == false) {
			if (isColumnOK(inFormobj, i) == true) {
				calcColumn(inFormobj, i);
			}
		}
	}

	if (alertMsg.length != l_Msg){
		alert(alertMsg);
		return false;
	}

	return true;
}


//
// Display cookies (if existing)
//

function retrieveCookies(inFormobj) {
	for (var j = 1; j <= 3; j++){
		var myCookie = getCookie("Twinoxide Batch Column " + j.toString())
		if (myCookie) {
			var myCookieArray = myCookie.split("&")
			for (var i = 0; i < fieldRequired.length; i++){
				var obj = inFormobj.elements[fieldRequired[i] + j.toString()];
				if (obj){
					obj.value = myCookieArray[i];
				}
			}
		}
	}
}


//
// Set Cookies for a defined column
// !$%W$#!!!! Max 20 cookies, so concatenate per column
//

function setColumnCookie(inFormobj, inColumn) {
	myCookieValue = "";
	for (var i = 0; i < fieldRequired.length; i++){
		var obj = inFormobj.elements[fieldRequired[i] + inColumn.toString()];
		if (obj){
			myCookieValue += obj.value;
			myCookieValue += "&";
		}
	}
	setCookie("Twinoxide Batch Column " + inColumn.toString(), myCookieValue, now)
}



//
// Checks whether all rows in a column hava been filled in
//
function isColumnEmpty(inFormobj, inColumn) {
    var isEmpty = true;
	for (var i = 0; i < fieldRequired.length; i++){
		var obj = inFormobj.elements[fieldRequired[i] + inColumn.toString()];
		if (obj){
			if (obj.value != "" && obj.value != null) {
				isEmpty = false;
			}
		}
	}
	return isEmpty;
}


//
// Checks whether values are not Numeric and <> Zero
//
function isColumnOK(inFormobj, inColumn) {
    var isOK = true;
	for (var i = 0; i < fieldRequired.length; i++){
		var obj = inFormobj.elements[fieldRequired[i] + inColumn.toString()];
		if (obj){
			if (obj.value == "" || obj.value == null) {
					alertMsg += " - Column " + inColumn.toString() + ", '" + fieldDescription[i] + "' is required\n";
					isOK = false;
			} else {
				if (obj.value == 0) {
					alertMsg += " - Column " + inColumn.toString() + ", '" + fieldDescription[i] + "' must be unequal to zero\n";
					isOK = false;
				} else {
					if (isNumeric(obj.value) == false) {
						alertMsg += " - Column " + inColumn.toString() + ", '" +  fieldDescription[i] + "' must be numeric\n";
						isOK = false;
					}
				}
			}
		}
	}
	return isOK;
}


//
// Checks whether a value ia Numeric
//
function isNumeric(inString) {
   //  check for valid numeric strings
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (inString.length == 0) return false;

   //  test inString consists of valid characters listed above
   for (i = 0; i < inString.length && blnResult == true; i++) {
      strChar = inString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1) {
         blnResult = false;
         }
      }
   return blnResult;
}


//
// Calculates the results
//
function calcColumn(inFormobj, inColumn) {
	// Calc and Round values

	var objInA = inFormobj.elements["TwinOxide_Solution" + inColumn.toString()];
	var objInB = inFormobj.elements["Dosing_Concentration" + inColumn.toString()];
	var objInC = inFormobj.elements["Process_Dose_Volume" + inColumn.toString()];
	var objResOut1 = inFormobj.elements["Twinoxide_Volume_per_dose" + inColumn.toString()];

	var objInD = inFormobj.elements["Dose_Repeat_Rate" + inColumn.toString()];
	var objInE = inFormobj.elements["Batch_Size" + inColumn.toString()];
	var objResOut2 = inFormobj.elements["Batch_Duration" + inColumn.toString()];

	objResOut1.value = objInB.value / objInA.value / 10000 * objInC.value;
	objResOut1.value = Math.round(objResOut1.value * 100) / 100;

	objResOut2.value = objInE.value / objResOut1.value / objInD.value;
	objResOut2.value = Math.round(objResOut2.value * 100) / 100;
}



// * COOKIEMANIA * COOKIEMANIA * COOKIEMANIA * COOKIEMANIA * COOKIEMANIA * COOKIEMANIA *

/* SETCOOKIE
   [name] - name of the cookie
   [value] - value of the cookie
   [expires] - expiration date of the cookie (defaults to end of current session)
   [path] - path for which the cookie is valid (defaults to path of calling document)
   [domain] - domain for which the cookie is valid (defaults to domain of calling document)
   [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
   * an argument defaults when it is assigned null as a placeholder
   * a null placeholder is not required for trailing omitted arguments
*/

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}


/* GETCOOKIE
   [name] - name of the desired cookie
   [return] string containing value of specified cookie or null if cookie does not exist
*/

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}


/* DELETECOOKIE
   [name] - name of the cookie
   [path] - path of the cookie (must be same as path used to create cookie)
   [domain] - domain of the cookie (must be same as domain used to create cookie)
   path and domain default if assigned null or omitted if no explicit
     argument proceeds
*/

function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

/* FIXDATE
   [date] - any instance of the Date object
   * hand all instances of the Date object to this function for "repairs"
*/

function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}

 //JavaScript Ends -->