// USITC common javascript library
// This file contains JavaScript methods that can be used on any page of the USITC website.

// void: displayContactLabel
// Description: Writes obfuscated JS to create mail to link.
// INPUTS:
//    label: the label of the link
//     contactPrefix: the name of the account
//     host: the smtp host
function  displayContactLabel(label, contactPrefix, host)
{
  if ((label != null && label != "") &&
       (contactPrefix != null && contactPrefix != "") &&
       (host != null && host != "")) {
    // We have all values
    document.write("<a  href=\"" + "&#109;&#97;&#105;&#108;" + "&#116;&#111;:" + contactPrefix + "&#64;" + host+ "\">" + label + "</a>");
  } else if ((label != null && label != "") &&
       (contactPrefix == null || contactPrefix == "") &&
       (host == null || host == "")) {
    // We only have a label
    document.write(label);
  } else {
    // do nothing
  }
}
// void: clearField
// Description: Clears the value of a field if the value in the field is the same as the default field value.
// INPUTS:
//     thisField: the field to be affected
//     defValue: the expected default value of "thisField"
function clearField(thisField, defValue, color) {
  if (thisField.value == defValue) {
    thisField.value = "";
    alterFontColor(thisField, color);
  }
}
// void: restoreField
// Description: Restores the value of an empty field to a default value, when the curser focus leaves the field. 
// INPUTS:
//     thisField: the field to be affected
//     defValue: the value assigned to "thisField"
function restoreField(thisField, defValue, color) {
  if (thisField.value == "") {
    alterFontColor(thisField, color);
    thisField.value = defValue;
  }
}
// void: alterFontColor
// Description: Alters the font color of a given object
// INPUTS:
//     obj: the target DOM object
//     color: the target color
function alterFontColor(obj, color) {
	obj.style.color = color;
}
// This function hides an element if it is showing and displays it, if it is hidden.	
// Input: ElementId - the id value of the element to be toggled.
function ToggleDivDisplay(ElementId) {
	try {
    var el;
    // get element from the passed id
    el = document.getElementById(ElementId);
    
    // If it is showing...
    if (el.style.display=="block") {
      // hide it
      el.style.display="none";
    } else {
      // show it
      el.style.display="block";
    }
  } catch(e) {
    // do nothing
  }
}
// This function displays all elements with a given elementPrefix in the ID value.	The pattern of the
// elements should match the "elementPrefixX_Y" pattern.
// Input: elementPrefix - the prefix of the id value. 
//        maxItems - upperbound for element iterator
// For example, if "foo" is passed, this method will recursively expand elements (if they exist)
// named "fooX", where X is a value between 0 and "maxItems". It then looks for "fooX_Y". 
function ExpandAll(elementPrefix, maxItems) {
  try {
    if (elementPrefix){ 
      for (var i=0; i<maxItems; i++) {
        var el;
        var elementId = elementPrefix+i;
        el = document.getElementById(elementId);
        if (el) {
          // Show parent
          el.style.display="block";
          // Show each child of this parent
          ExpandAll(elementId+"_", maxItems);
        }
      }
    }
  } catch(e) {
    // do nothing
  }
}
// This function hides all elements with a given elementPrefix in the ID value. The pattern of the
// elements should match the "elementPrefixX_Y" pattern.	
// Input: elementPrefix - the prefix of the id value.
//        maxItems - upperbound for element iterator
// For example, if "foo" is passed, this method will recursively collapse elements (if they exist)
// named "fooX", where X is a value between 0 and "maxItems". It then looks for "fooX_Y". 
function CollapseAll(elementPrefix, maxItems) {
  try {
    if (elementPrefix) {
      for (var i=0; i<maxItems; i++) {
        var el;
        var elementId = elementPrefix+i;
        el = document.getElementById(elementId);
        if (el) {
          // Hide parent
          el.style.display="none";
          // Collapse each child of this parent
          CollapseAll(elementId+"_",maxItems);
        }
      }
    }
  } catch(e) {
    // do nothing
  }
}
// This method is used to highlight a selected item in a page that is part of sub navigation.
// The target highlighted item is the "element" (passed as "this"). Any element with an "elementPrefix"
// is de-highlighted before the selected item is highlighted. The pattern of the elements to be de-highlighted
// should match the "elementPrefixX_Y" pattern. The "backgroundColor" is the color to be applied to the selected element.
// Input: element - the element to be highlighted
//        elementPrefix - the prefix of the id value of elements that are highlighted by this method.
//        backgroundColor - The color to use for an item that has been highlighted.
function highlight(element,elementPrefix,backgroundColor) {
  try {
    // First dehighlight all
    whtBackground(elementPrefix);			
    var el = document.getElementById(element.id);
    el.style.backgroundColor=backgroundColor; 
  } catch(e) {
    // do nothing
  }
}
// This function deselects (clears backgroundColor) all elements with a given elementPrefix in the ID value.
//  The pattern of the elements should match the "elementPrefixX_Y" pattern.	
// Input: elementPrefix - the prefix of the id value.
function clearBackground(elementPrefix) {
  try {
    if (elementPrefix) {
      for (var i=0; i<50; i++) {
        var el;
        var elementId = elementPrefix+i;
        el = document.getElementById(elementId);
        if (el) {
          // default color
          el.style.backgroundColor="";  
        }
      }
    }
  } catch(e) {
    // do nothing
  }
}

