﻿// Chemica Solutions Javascript Toolbox
  
  
  //Globals:
  
  var nbsp = 160;		// non-breaking space char
  var node_text = 3;	// DOM text node-type
  var emptyString = /^\s*$/ ; 
  var global_valfield;	// retain valfield for timer thread
  
  
  // ---------- Auto-validation
  
  // Given an event (like an onkeypress), gets the key code for the last pressed key.
  function getkey(e)
  {
    if (window.event)
      return window.event.keyCode;
    else if (e)
      return e.which;
    else
    return null;
  }
  
  // Pass in an onkeypress event object from a textbox,
  // and the input will be restricted neatly to numbers.
  function NumericChars(event_object){
    return GoodChars(event_object, '0123456789');
  }
  
  // Pass in an onkeypress event object from a textbox,
  // and the input will be restricted neatly to numbers
  // witho only one decimal point allowed.
  function DecimalChars(event_object, element){
    if(element.value.indexOf(".") > 0){
        return NumericChars(event_object);
    }
    else
    {
        return NumericCharsWithPoints(event_object);
    }
  }
  
  // Pass in a number, which will be limited to values 
  // between those entered, inclusive, and returned.
  function NumberBetween(number, low, high)
  {
    if(number < low) 
      return low;
    if(number > high)
      return high;
    return number;
  }
  // Pass in an onkeypress event object from a textbox,
  // and the input will be restricted neatly to numbers and decimal points.
  function NumericCharsWithPoints(event_object){
    return GoodChars(event_object, '0123456789.');
  }
  // Pass in an onkeypress event object from a textbox and a string,
  // and the input will be restricted neatly to characters in the string.
  function GoodChars(e, goods)
  {
    var key, keychar;
    key = getkey(e);
    if (key == null) return true;

    // get character
    keychar = String.fromCharCode(key);
    keychar = keychar.toLowerCase();
    goods = goods.toLowerCase();

    // check goodkeys
    if (goods.indexOf(keychar) != -1)
	  return true;

    // control keys
    if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
      return true;

    // else return false
    return false;
  }
  
  // ---------- Visual Effects
  
  // Uses Prototype.js and efects.js to make an element appear smoothly
  // without re-appearing if it already exists!
  function ChemShow(element){
    element = EnsureElement(element);
    if(!Element.visible(element)) new Effect.Appear(element);
  }
  
  // Uses Prototype.js and efects.js to make an element disappear smoothly
  // without re-disappearing if it's already invisible!
  function ChemHide(element){
    element = EnsureElement(element);
    if(Element.visible(element)) new Effect.Fade(element);
  }
  
  function QuickShow(element){
    element = EnsureElement(element);
    element.show();
  }
  
  function QuickHide(element){
    element = EnsureElement(element);
    element.hide();
  }
  
  //Shows an element if an input field has a value within the aValues array, 
  //and hides it otherwise.
  //bHideIfValues reverses the behaviour. 
  function ShowIfValues(inputElement, showElement, aValues, bHideIfValues){
    inputElement = EnsureElement(inputElement);
    showElement = EnsureElement(showElement);
    aValues = EnsureArray(aValues);
    bHideIfValues = bHideIfValues || false;
    
    if(ArrayContains(aValues, inputElement.value) == bHideIfValues){
        ChemHide(showElement);
    } else {
        ChemShow(showElement);
    }
  } 
  
  // ---------- Object Manipulation
  
  //Converts a string to an element, but leaves an element alone.
  function EnsureElement(element){
    if (typeof element == 'string')
      element = document.getElementById(element);
    return element;
  }
  
  //Checks if the passed object is an array.
  function IsArray(oArray) {
    if(oArray.constructor.toString().indexOf("Array")==-1)
      return false;
    else
      return true;
  }
  
  //If the passed object isn't an array, one will be created
  //with the original object as the only member.
  function EnsureArray(aArray){
    if(!IsArray(aArray))
      aArray = new Array(aArray);
    return aArray;
  }
  
  //Checks if an array contains a value.
  function ArrayContains(aArray, vValue){
    found = false;
    for(i=0; i<aArray.length; i++){
        if(aArray[i] == vValue)
            found = true;
    }
    return found;
  }
  
  // ---------- Validation helpers
  
  // Trim leading/trailing whitespace off string
  function Trim(str)
  {
    return str.replace(/^\s+|\s+$/g, '');
  }

  // Delayed focus setting to get around IE bug
  function SetFocusDelayed() 
  {
    global_valfield.focus();
  }

  function setfocus(valfield)
  {
    // save valfield in global variable so value retained when routine exits
    global_valfield = valfield;
    setTimeout( 'setFocusDelayed()', 100 );
  }

  function ShowIfEmpty(inputElement, validatorElement)
  {
    inputElement = EnsureElement(inputElement);
    if(emptyString.test(inputElement.value))
    {
      ChemShow(validatorElement);
    } 
    else 
    {
      ChemHide(validatorElement);
    }
  }
  
  function ShowIfAnyEmpty(inputElements, validatorElement)
  {
    var show = false;
    for(x = 0; x < inputElements.length; x++){
        elem = EnsureElement(inputElements[x]);
          
        if(emptyString.test(elem.value))
        {
            show = true;
        }
    }
    
    if(show)
    {
      ChemShow(validatorElement);
    } 
    else 
    {
      ChemHide(validatorElement);
    }
  }
  
  function QuickShowIfEmpty(inputElement, validatorElement)
  {
    inputElement = EnsureElement(inputElement);
    if(emptyString.test(inputElement.value))
    {
      QuickShow(validatorElement);
    } 
    else 
    {
      QuickHide(validatorElement);
    }
  }
  
  
  function QuickShowIfAnyEmpty(inputElements, validatorElement)
  {
    var show = false;
    for(x = 0; x < inputElements.length; x++){
        elem = EnsureElement(inputElements[x]);
          
        if(emptyString.test(elem.value))
        {
            show = true;
        }
    }
    
    if(show)
    {
      QuickShow(validatorElement);
    } 
    else 
    {
      QuickHide(validatorElement);
    }
  }
  
  function ShowIfSumNotEqual(elem1, elem2, sum, showElem)
  {
    elem1 = EnsureElement(elem1);
    elem2 = EnsureElement(elem2);
    if(parseInt(elem1.value) + parseInt(elem2.value) != sum)
    {
        ChemShow(showElem);
    }
    else
    {
        ChemHide(showElem);
    }
  }
  
  
  function QuickShowIfSumNotEqual(elem1, elem2, sum, showElem)
  {
    elem1 = EnsureElement(elem1);
    elem2 = EnsureElement(elem2);
    if(parseInt(elem1.value) + parseInt(elem2.value) != sum)
    {
        QuickShow(showElem);
    }
    else
    {
        QuickHide(showElem);
    }
  }
  
  
  function popup(url, name, width, height, scrollbars) {
    sb = 'yes';
    if (!scrollbars) { sb = 'no'; }
	  newwindow=window.open(url, name, 'height=' + height + ',width=' + width + ',scrollbars=' + sb);
	  if (window.focus) {newwindow.focus();}
	  return false;
  }