
/* IDs of Schema Component Representation boxes */
var scBoxes = new Array( 'b0', 'b1', 'b2','b3', 'b4', 'b5', 'b6','b7', 'b8', 'b9', 'b10', 'b11', 'b12','b13', 'b14', 'b15', 'b16','b17', 'b18', 'b19', 'b20', 'b21', 'b22','b23', 'b24', 'b25', 'b26','b27', 'b28', 'b29', 'b30', 'b31', 'b32','b33', 'b34', 'b35', 'b36','b37', 'b38', 'b39', 'b40','b41', 'b42','b43', 'b44', 'b45', 'b46','b47', 'b48', 'b49', 'b50', 'b51', 'b52','b53', 'b54', 'b55', 'b56','b57', 'b58', 'b59', 'b60','b61', 'b62','b63', 'b64', 'b65', 'b66','b67', 'b68', 'b69', 'b70', 'Asia', 'Europe', 'North America', 'Korea', 'Japan', 'France', 'Germany','Spain', 'Israel', 'Italy', 'Norway','Denmark','United-Kingdom','The Netherlands','Canada','USA');

/**
 * Can get the ID of the button controlling
 * a collapseable box by concatenating
 * this string onto the ID of the box itself.
 */
var B_SFIX = "_button";

/**
 * Counter of documentation windows
 * Used to give each window a unique name
 */
var windowCount = 0;

/**
 * Initialises the state of the HTML page.
 */
function show_hide_init() {
  
  var obj = getElementObject("globalControls");
  if (obj != null) {
    obj.style.display="block";
  }
  obj = getElementObject("printerControls");
  if (obj != null) {
    obj.style.display="inline";
  }
  //  expandAll(xiBoxes);
  collapseAll(scBoxes);
  //switchAll(scBoxes);
  //   viewControlButtons(xiBoxes);
  
 viewControlButtons(scBoxes);
 
}

/**
 * Returns an element in the current HTML document.
 * 
 * @param elementID Identifier of HTML element
 * @return					HTML element object
 */
function getElementObject(elementID) {
  var elemObj = null;
  if (document.getElementById) {
    elemObj = document.getElementById(elementID);
  }
  return elemObj;
}				 

/**
 * Closes a collapseable box.
 * 
 * @param boxObj		 Collapseable box
 * @param buttonObj Button controlling box
 */
function closeBox(boxObj, buttonObj) {
  if (boxObj == null || buttonObj == null) {
    // Box or button not found
  } else {
    // Change 'display' CSS property of box
    boxObj.style.display="none";

    // Change text of button
    if (boxObj.style.display=="none") {
      buttonObj.src="/common/logo/collapse.gif";
      buttonObj.value="+";
    }
  }
}

/**
 * Opens a collapseable box.
 * 
 * @param boxObj		 Collapseable box
 * @param buttonObj Button controlling box
 */
function openBox(boxObj, buttonObj) {
  if (boxObj == null || buttonObj == null) {
    // Box or button not found
  } else {
    // Change 'display' CSS property of box
    boxObj.style.display="block";

    // Change text of button
    if (boxObj.style.display=="block") {
      buttonObj.src="/common/logo/expand.gif";
      buttonObj.value="-";
    }
  }
}

/**
 * Switches the state of a collapseable box, e.g.
 * if it's opened, it'll be closed, and vice versa.
 * 
 * @param boxID Identifier of box
 */
function switchState(boxID) {
  var boxObj = getElementObject(boxID);
  var buttonObj = getElementObject(boxID+B_SFIX);
  if (boxObj == null || buttonObj == null) {
    // Box or button not found
  } else if (boxObj.style.display=="none") {
    // Box is closed, so open it
    openBox(boxObj, buttonObj);
  } else if (boxObj.style.display=="block") {
    // Box is opened, so close it
    closeBox(boxObj, buttonObj);
  }
}

/**
 * Closes all boxes in a given list.
 * 
 * @param boxList Array of box IDs
 */
function collapseAll(boxList) {
  var idx;
  for (idx = 0; idx < boxList.length; idx++) {
    var boxObj = getElementObject(boxList[idx]);
    var buttonObj = getElementObject(boxList[idx]+B_SFIX);
    closeBox(boxObj, buttonObj);
  }
}

/**
 * Open all boxes in a given list.
 * 
 * @param boxList Array of box IDs
 */
function expandAll(boxList) {
  var idx;
  for (idx = 0; idx	  < boxList.length; idx++) {
    var boxObj = getElementObject(boxList[idx]);
    var buttonObj = getElementObject(boxList[idx]+B_SFIX);
    openBox(boxObj, buttonObj);
  }
}
function switchAll(boxList) {
  var idx;
  for (idx = 0; idx	  < boxList.length; idx++) {
    var boxObj = getElementObject(boxList[idx]);
    switchState(boxObj);
  }
}
/**
 * Makes all the control buttons of boxes appear.
 * 
 * @param boxList Array of box IDs
 */
function viewControlButtons(boxList) {
  var idx;
  for (idx = 0; idx < boxList.length; idx++) {
    buttonObj = getElementObject(boxList[idx]+B_SFIX);
    if (buttonObj != null) {
      buttonObj.style.display = "inline";
    }
  }
}

/**
 * Makes all the control buttons of boxes disappear.
 * 
 * @param boxList Array of box IDs
 */
function hideControlButtons(boxList) {
  var idx;
  for (idx = 0; idx < boxList.length; idx++) {
    buttonObj = getElementObject(boxList[idx]+B_SFIX);
    if (buttonObj != null) {
      buttonObj.style.display = "none";
    }
  }
}

/**
 * Sets the page for either printing mode
 * or viewing mode. In printing mode, the page
 * is made to be more readable when printing it out.
 * In viewing mode, the page is more browsable.
 *
 * @param isPrinterVersion If true, display in
 * 										  printing mode; otherwise, 
 * 										  in viewing mode
 */
function displayMode(isPrinterVersion) {
  var obj;
  if (isPrinterVersion) {
    // Hide global control buttons
    obj = getElementObject("globalControls");
    if (obj != null) {
      obj.style.display = "none";
    }
    // Hide Legend
    obj = getElementObject("legend");
    if (obj != null) {
      obj.style.display = "none";
    }
    obj = getElementObject("legendTOC");
    if (obj != null) {
      obj.style.display = "none";
    }
    // Hide Glossary
    obj = getElementObject("glossary");
    if (obj != null) {
      obj.style.display = "none";
    }
    obj = getElementObject("glossaryTOC");
    if (obj != null) {
      obj.style.display = "none";
    }

    // Expand all XML Instance Representation tables
    //    expandAll(xiBoxes);
    // Expand all Schema Component Representation tables
    expandAll(scBoxes);

    // Hide Control buttons
    //    hideControlButtons(xiBoxes);
    hideControlButtons(scBoxes);
  } else {
    // View global control buttons
    obj = getElementObject("globalControls");
    if (obj != null) {
      obj.style.display = "block";
    }
    // View Legend
    obj = getElementObject("legend");
    if (obj != null) {
      obj.style.display = "block";
    }
    obj = getElementObject("legendTOC");
    if (obj != null) {
      obj.style.display = "block";
    }
    // View Glossary
    obj = getElementObject("glossary");
    if (obj != null) {
      obj.style.display = "block";
    }
    obj = getElementObject("glossaryTOC");
    if (obj != null) {
      obj.style.display = "block";
    }

    // Expand all XML Instance Representation tables
    //    expandAll(xiBoxes);
    // Collapse all Schema Component Representation tables
    collapseAll(scBoxes);

    // View Control buttons
    //    viewControlButtons(xiBoxes);
    viewControlButtons(scBoxes);
  }
}

/**
 * Opens up a window displaying the documentation
 * of a schema component in the XML Instance
 * Representation table.
 * 
 * @param compDesc		Description of schema component 
 * @param compName		Name of schema component 
 * @param docTextArray Array containing the paragraphs 
 * 								  of the new document
 */
function viewDocumentation(compDesc, compName, docTextArray) {
  var width = 400;
  var height = 200;
  var locX = 100;
  var locY = 200;

  /* Generate content */
  var actualText = "<html>";
  actualText += "<head><title>";
  actualText += compDesc;
  if (compName != '') {
    actualText += ": " + compName;
  }
  actualText += "</title></head>";
  actualText += "<body bgcolor=\"#FFFFEE\">";
  // Title
  actualText += "<p style=\"font-family: Arial, san-serif; font-size: 12pt; font-weight: bold; letter-spacing:1px;\">";
  actualText += compDesc;
  if (compName != '') {
    actualText += ": <span style=\"color:#006699\">" + compName + "</span>";
  }
  actualText += "</p>";
  // Documentation
  var idx;
  for (idx = 0; idx < docTextArray.length; idx++) {
    actualText += "<p style=\"font-family: Arial, san-serif; font-size: 10pt;\">" + docTextArray[idx] + "</p>";
  }
  // Link to close window
  actualText += "<a href=\"javascript:void(0)\" onclick=\"window.close();\" style=\"font-family: Arial, san-serif; font-size: 8pt;\">Close</a>";
  actualText += "</body></html>";

  /* Display window */
  windowCount++;
  var docWindow = window.open("", "documentation"+windowCount, "toolbar=no,location=no,status=no,menubar=no,scrollbars=auto,resizable,alwaysRaised,dependent,titlebar=no,width="+width+",height="+height+",screenX="+locX+",left="+locX+",screenY="+locY+",top="+locY);
  docWindow.document.write(actualText);
}


