//
// 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 showDiv(whichLayer) {
	if (document.getElementById) {
		// this is the way the standards work
		var style = document.getElementById(whichLayer).style;
		style.display = "block";
	} else if (document.all) {
		// this is the way old msie versions work
		var style = document.all[whichLayer].style;
		style.display = "block";
	} else if (document.layers) {
		// this is the way nn4 works
		var style = document.layers[whichLayer].style;
		style.display = "block";
	}
}

function highlight(whichLayer) {
	if (document.getElementById) {
		// this is the way the standards work
		var style = document.getElementById(whichLayer).style;
		// style.background = "#FFF";
		style.color = "#CC6600";
	} else if (document.all) {
		// this is the way old msie versions work
		var style = document.all[whichLayer].style;
		// style.background = "#FFF";
		style.color = "#CC6600";
	} else if (document.layers) {
		// this is the way nn4 works
		var style = document.layers[whichLayer].style;
		// style.background = "#FFF";
		style.color = "#CC6600";
	}
}

function showSubmenu(target) {
	if (target != "") eval("showDiv('" + target + "')");
}

function activeNav(target) {
	if (target != "") eval("highlight('" + target + "')");
}

function showNestedMenu(target) {
	if (target != "") eval("swopDiv('" + target + "', '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;
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

if(document.images) {
	var pics = new Array(); 
	pics[1] = new Image();
	pics[1].src = "../images/download_button_over.gif"; 
	pics[2] = new Image();
	pics[2].src = "../images/download_button.gif";
}

function changer(from,to) {
	if(document.images) {
		document.images[from].src = pics[to].src;
	}
}

