// =============================================================================
// Project   : DWIS 170 - Drinking Water Information System (O.Reg. 170/03)
// Filename  : utility.js
// Standards : JavaScript version 1.3
// History   : November-20-2003 Huy Tran Created
// Note(s)   : 
// =============================================================================

//
// Function:        rtrim
// Description:     This function remove trailing space from a string
// Input arguments: str (String)
// Return value:    the trimmed string.
//
function rtrim(str) {
  var i = 0;
  for (i = str.length - 1; i > -1; i--) {
    if (str.charAt(i) != ' ') break;
  }
  return str.substring(0, i+1);
}

//
// Function:        ltrim
// Description:     This function remove leading spaces from a string
// Input arguments: str (String)
// Return value:    the trimmed string.
//
function ltrim(str) {
  var trimStr = "";
  var i = 0;
  for (i = 0; i < str.length; i++) {
    if (str.charAt(i) != ' ') break;
  }
  if (i < str.length) trimStr = str.substring(i, str.length);
  return trimStr;
}

//
// Function:        trim
// Description:     This function remove leading and trailing spaces from a string
// Input arguments: str (String)
// Return value:    the trimmed string.
//
function trim(str) {
  return ltrim(rtrim(str));
}

//
// Function:        selectAll
// Description:     This function selects/deselected all options in the specified select element.
// Input arguments: select (element name), select/deselect (boolean)
// Return value:    the trimmed string.
//
function selectAll(elementName, b) {
  var theSel = getElement(elementName);
  var i;
  if (theSel != null) { 
    var numOptions = theSel.length; 
    for (i=0; i<numOptions; i++) {
      theSel[i].selected = b;
    }
  }
}

//
// Function:        findOption
// Description:     This function returns the option with the specified value or null.
// Input arguments: options (element), optionValue
// Return value:    None.
//
function findOption(options, optionValue) {
  var numOptions = options.length;
  var i;
  for (i=0; i<numOptions; i++) {
    if (options[i].value == optionValue) {
      return options[i];
    }
  }
  return null;
}

//
// Function:        moveSelected
// Description:     This function move the selected options from the src to the dest select element.
// Input arguments: dest select (element name), src select (element name)
// Return value:    None.
//
function moveSelected(destElementName, srcElementName) {
  var destSel = getElement(destElementName);
  var srcSel = getElement(srcElementName);

  if (destSel == null || srcSel == null) {
    return;
  }

  var numOptions = srcSel.length;
  var lastIndex = destSel.length;
  var unselectedOptions = new Array();
  var option;
  var selectedCount = 0;
  var i;

  for (i=0; i<numOptions; i++) {
    if (srcSel[i].selected) {
      selectedCount++;
      if (!findOption(destSel, srcSel[i].value)) {
        option = new Option(srcSel[i].label, srcSel[i].value, true, true);
        option.label = srcSel[i].label;
        destSel[lastIndex] = option;
        lastIndex++;
      }
    } else {
      unselectedOptions[unselectedOptions.length] = srcSel[i];
    }
  }

  if (selectedCount > 0) {
    // Leave only options that where not selected.
    // Must clear from the last to the first.
    for (i=srcSel.length-1; i>=0; i--) {
      srcSel[i] = null;
    }
    //srcSel.options = null; -- unimplemented in ie 5.x, do long way above.
    
    for (i=0; i<unselectedOptions.length; i++) {
      srcSel[i] = unselectedOptions[i];
    }
  }
}

function getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i;
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
}

function getSelectedRadioValue(buttonGroup) {
   // returns the value of the selected radio button or "" if no button is selected
   var i = getSelectedRadio(buttonGroup);
   if (i == -1) {
      return "";
   } else {
      if (buttonGroup[i]) { // Make sure the button group is an array (not just one button)
         return buttonGroup[i].value;
      } else { // The button group is just the one button, and it is checked
         return buttonGroup.value;
      }
   }
}

function getCookie(cookieName) {
  var search = cookieName + "="
  var returnValue = "";
  if (document.cookie.length > 0) {
    offset = document.cookie.indexOf(search)
    if (offset != -1) { 
      offset += search.length
      end = document.cookie.indexOf(";", offset);
      if (end == -1) {
        end = document.cookie.length;
      }
      returnValue = unescape(document.cookie.substring(offset, end));
    }
  }
  return returnValue;
}

function setCookie(cookieName, cookieValue) {
  document.cookie=cookieName+"="+cookieValue;
}

var autoTabFieldLength=0;
var previousTabField=null;
function autoTabNext(currentField, keyDir, fieldLength, nextField) {
  if (keyDir == "down") {
    autoTabFieldLength=currentField.value.length;
    previousTabField = currentField;
  } else if (keyDir == "up") {
    if (currentField.value.length != autoTabFieldLength) {
      autoTabFieldLength=currentField.value.length;
      if (previousTabField == currentField && autoTabFieldLength == fieldLength) {
        nextField.focus();
      }
    }
  }
}

var disabledElement=null;
function disableElement(element) {
  disabledElement = element;
  element.disabled = true;
}
function enableElement(element) {
  if (element) {
    element.disabled = false;
    if (disabledElement == element) {
      disabledElement = null;
    }
  }
}

var disabledLink=null;
function cancelLink() {
  return false;
}
function disableLink(link) {
  disabledLink = link;
  if (link.onclick) {
    link.oldOnClick = link.onclick;
  }
  link.onclick = cancelLink;
  if (link.style) {
    link.style.cursor = 'default';
  }
}
function enableLink(link) {
  if (link) {
    link.onclick = link.oldOnClick ? link.oldOnClick : null;
    if (link.style) {
      link.style.cursor = document.all ? 'hand' : 'pointer';
    }
    if (disabledLink == link) {
      disabledLink = null;
    }
  }
}

function swopDiv(whichLayer, showOption) {
	if (showOption == 'show') {
		whichLayerPicked = whichLayer + "Expanded";
		whichLayerHidden = whichLayer + "Collapsed";
	} else {
		whichLayerPicked = whichLayer + "Collapsed";
	whichLayerHidden = whichLayer + "Expanded";
	}
	if (document.getElementById) {
		// this is the way the standards work
		var style = document.getElementById(whichLayerPicked).style;
		style.display = "block";
		var style2 = document.getElementById(whichLayerHidden).style;
		style2.display = "none";
	} else if (document.all) {
		// this is the way old msie versions work
		var style = document.all[whichLayerPicked].style;
		style.display = "block";
		var style2 = document.all[whichLayerHidden].style;
		style2.display = "none";
	} else if (document.layers) {
		// this is the way nn4 works
		var style = document.layers[whichLayerPicked].style;
		style.display = "block";
		var style2 = document.layers[whichLayerHidden].style;
		style2.display = "none";
	}
}

function showSubmenu() {
	if (cleansingProcess != "") swopDiv('cleansingProcess', 'show');
}

// Declare 'Page level' variables

var status = true;
var errors = "";
var warnings = "Warnings:\n";
var firstErrorField = "";
var warningField;
var osubmit = true;

// Function:        Validate
// Description:     Turns form validation on/off
// Input arguments: none
// Return value:    true / false
// Note:            
// 

function validate() {
  return true;
}

// Function:        buttonSubmit
// Description:     Eliminates double-click submissions
// Input arguments: form (form name)
// Return value:    true / false
// Note:            
// 

function buttonSubmit(form) {
  // ensure we don't double-submit if user double-clicks button
  if (!osubmit) { 
    return false; 
  }
  osubmit = false;

  form.submit();
  return true;
}

   // ------------------------------------------------------------------------------------------------------------------------------------------------
   // date functions
   // ------------------------------------------------------------------------------------------------------------------------------------------------
   
    var arrWeekdays_de = new Array ( "Montag", "Dienstag", "Mittwoch",  "Donnerstag", "Freitag", "Samstag",  "Sonntag" );
    var arrWeekdays_en = new Array ( "Monday", "Tuesday",  "Wednesday", "Thursday",   "Friday",  "Saturday", "Sunday"  );

    var arrMonth_de = new Array ( "Januar",  "Februar",  "M&auml;rz", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember" );
    var arrMonth_en = new Array ( "January", "February", "March",     "April", "May", "June", "July", "August", "September", "October", "November", "December" );
   
   
    function getWeekdayText ( day, language ) {
        if ( day >= 1 && day <= 7 ) {
            if ( "de" == language ) {
                return arrWeekdays_de [ day-1 ];
            }
            return arrWeekdays_en [ day-1 ];
        }
        return "{invalid day}";
    }
    
    function getMonthText ( month, language ) {
        if ( month >= 1 && month <= 12 ) {
            if ( "de" == language ) {
                return arrMonth_de [ month ];
            }
            return arrMonth_en [ month ];
        }
        return "{invalid month}";
    }



