// ---------------------------------------------------------------------
// This unit contains general, shared functions
// ---------------------------------------------------------------------

/*
function createStandardDateString(dtValue) {
  var days = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
  var months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
  var wday = days[dtValue.getDay() + 1];
  var lmonth = months[dtValue.getMonth() + 1];
  var date = dtValue.getDate();
  var fyear = dtValue.getYear();
  if (fyear < 2000) {
    fyear = fyear + 1900;
  }
  return wday + ' ' + date + ' ' + lmonth + ' ' + fyear;
}

function padNumber(iNumber) { 
  return (iNumber < 10) ? '0' + iNumber : iNumber; 
}
*/

function obj(sName, oDoc) { // second parameter is optional
  if (!oDoc) {
	oDoc = document;
  }
  return oDoc.getElementById(sName);
}

/*
function sleep(iMS) {
  setTimeout("", iMS);
}

function getElementWidth(oElement) {
  var iWidth;
  if (oElement.style.width) {
    iWidth = oElement.style.width;
  }
  else if (oElement.style.pixelWidth) {
    iWidth = oElement.style.pixelWidth;
  }
  else if (oElement.offsetWidth) {
    iWidth = oElement.offsetWidth;
  } 
  else if (document.defaultView && document.defaultView.getComputedStyle) {
    iWidth = document.defaultView.getComputedStyle(oElement ,'').getPropertyValue('width');
  }
  if(typeof iWidth == "string") {
	return parseInt(iWidth);
  }
  else {	
    return iWidth;
  }
}
*/

function getElementHeight(oElement) {
  var iHeight;
  if (oElement.style.height) {
    iHeight = oElement.style.height;
  }
  else if (oElement.style.pixelHeight) {
    iHeight = oElement.style.pixelHeight;
  }
  else if (oElement.offsetHeight) {
    iHeight = oElement.offsetHeight;
  } 
  else if (document.defaultView && document.defaultView.getComputedStyle) {
    iHeight = document.defaultView.getComputedStyle(oElement ,'').getPropertyValue('height');
  }
  if(typeof iHeight == "string") {
	return parseInt(iHeight);
  }
  else {	
    return iHeight;
  }
}

/*
function trim(sInString) {
  sInString = sInString.replace( /^\s+/g, "" ); // strip leading
  return sInString.replace( /\s+$/g, "" );      // strip trailing
}
*/

// The following original code is from Dynamic Web Coding at http://www.dyn-web.com/
// but has been modified by Leigh Harrison leigh.harrison@else.co.nz
// See Terms of Use at http://www.dyn-web.com/bus/terms.html regarding conditions 
// under which you may use this code. This notice must be retained in the code as is!

function setOuterHeight(oWindow, sName) {
  if (oWindow.parent == oWindow) {
    setFrameHeight(sName);
  }
  else {
    parent.setFrameHeight(sName);
  }
}

function getDocHeight(doc) {
  var docHt = 0, sh, oh;
  if (doc.height) docHt = doc.height;
  else if (doc.body) {
    if (doc.body.scrollHeight) docHt = sh = doc.body.scrollHeight;
    if (doc.body.offsetHeight) docHt = oh = doc.body.offsetHeight;
    if (sh && oh) docHt = Math.max(sh, oh);
  }
  return docHt;
}

function setFrameHeight(sName) {
  var oFrame = document.getElementById? document.getElementById(sName): document.all? document.all[sName]: null;
  if (oFrame && oFrame != null) {
    var oDoc = getDocumentFromIframe(oFrame);
    if (oDoc) {
      oFrame.style.height = "auto"; // helps resize (for some) if new doc shorter than previous  
      var docHt = getDocHeight(oDoc);
      // need to add to height to be sure it will all show
      if (docHt) {
  	    oFrame.style.height = docHt + 10 + "px";
	  }
    }  	
  }
}

// Dynamic Web Coding functions end here

/*
function verify() { 
  // 0 Object is not initialized 
  // 1 Loading object is loading data 
  // 2 Loaded object has loaded data 
  // 3 Data from object can be worked with 
  // 4 Object completely initialized 
  if (parent.xmlDoc.readyState != 4) { 
    return false; 
  } 
}

function getStyleClass(sName, oDoc) { // second parameter optional
  if (!oDoc || oDoc == null) {
	oDoc = document;
  }
  if (oDoc.styleSheets.length <  1) {
	return null;
  }
  var cssRules;
  if (oDoc.styleSheets[0].cssRules) {
	cssRules = "cssRules";
  }
  else {
	cssRules = "rules";
  }
  for (var iSheet = 0; iSheet < oDoc.styleSheets.length; iSheet++) {
	for (var iRule = 0; iRule < oDoc.styleSheets[iSheet][cssRules].length; iRule++) {
      if (oDoc.styleSheets[iSheet][cssRules][iRule].selectorText.toLowerCase() == sName.toLowerCase()) {
        return oDoc.styleSheets[iSheet][cssRules][iRule];
      }
    }
  }
  return null;
}
*/

function getDocumentFromIframe(oFrame) {
  if (oFrame.contentDocument) { 
    return oFrame.contentDocument;
  }		
  else if (oFrame.contentWindow) {
    return oFrame.contentWindow.document;
  }		
  else if (oFrame.document) {
    return oFrame.document;
  }		
  else {
  	return null;
  }
}

/*
function setCookie(sName, sValue, iExpire) {
  var exdate = new Date();
  exdate.setDate(exdate.getDate() + iExpire);
  document.cookie = sName + "=" + escape(sValue) + ";expires=" + exdate.toUTCString();
}

function getCookie(sName) {
  if (document.cookie.length > 0) {
    var iStart = document.cookie.indexOf(sName + "=");
    if (iStart != -1) { 
      iStart = iStart + sName.length + 1;
      iEnd = document.cookie.indexOf(";", iStart);
      if (iEnd == -1) {
        iEnd = document.cookie.length;
      }
      return unescape(document.cookie.substring(iStart, iEnd));
    } 
  }
  return "";
}

function getCookieAsInt(sParam) {
  var iTemp = 0;
  var sTemp = getCookie(sParam);
  if (sTemp!= null && sTemp != "" && !isNaN(parseInt(sTemp))) {
    iTemp = parseInt(sTemp);
  }
  return iTemp;
}

function compareFSODates(sOne, sTwo) {
  // Returns -1 if sOne is less than sTwo
  // Returns  0 if the dates are the same
  // Returns  1 if sOne is greater than sTwo
  //alert(sOne + ' | ' + sTwo);
  var dtOne = Date.parse(sOne);
  var dtTwo = Date.parse(sTwo);
  //alert(sOne + ' | ' + sTwo + '\n' + dtOne + " | " + dtTwo);
  if (dtOne < dtTwo) {
    return -1;
  }
  else if (dtOne > dtTwo) {
    return 1;
  }
  else {
    return 0;
  }
}

function getMostRecentFileInFolder(sFolder, sExt) { //Second parameter is optional (format eg ".htm.html"
  var sFilename = null;
  var sFiledate = null;
  var sTemp;
  var fso = new ActiveXObject("Scripting.FileSystemObject");
  if (fso.folderExists(sFolder)) {
    var oFolder = fso.GetFolder(sFolder);
    var enumFolder = new Enumerator(oFolder.files);
	for ( ; !enumFolder.atEnd(); enumFolder.moveNext()) {
      sTemp = enumFolder.item().Name;
      if (!sExt || sExt.indexOf(sTemp.substring(sTemp.lastIndexOf("."), sTemp.length)) > -1) {
        if (sFilename == null || compareFSODates(enumFolder.item().DateCreated, sFiledate) > 0) {
          sFilename = enumFolder.item().Path; 
          sFiledate = enumFolder.item().DateCreated;
        }
      }
    }
  }
  if (sFilename == null) {
    return "";
  }
  else {
    return getDOSPathFromFSOPath(sFolder) + "\\" + sFilename;
  }
}

function getFSOPathFromDOSPath(sPath) {
  // Double the backslashes
  iPos = 0;
  while (iPos  < sPath.length) {
    if (sPath.substring(iPos, iPos + 1) == "\\") {
      sPath = sPath.substring(0, iPos + 1) + sPath.substring(iPos, sPath.length);
      iPos++;
    }
    iPos++;		
  }
  return sPath;	
}

function getDOSPathFromFSOPath(sPath) {
  // Removed the doubled backslashes
  iPos = sPath.lastIndexOf("\\\\");
  while(iPos > -1) {
    sPath = sPath.substring(0, iPos + 1) + sPath.substring(iPos + 2, sPath.length);
    iPos = sPath.lastIndexOf("\\\\");
  }
  return sPath;
}

function getFSOFolderFromFilename(sName) {
  var iPos = sName.lastIndexOf("\\");
  sName = sName.substring(0, iPos);
  if (sName.indexOf("file://") == 0) {
    sName = sName.substring(7, sName.length);
  }
  return getFSOPathFromDOSPath(sName);	
}

function setVisibility(sElement, sState){
  var oSection = document.getElementById(sElement);
  if (oSection) {
    oSection.style.visibility = sState;
  }
}

function setDisplay(sElement, sState) {
  var oSection = document.getElementById(sElement);
  if (oSection) {
    oSection.style.display = sState;
  }
}

function OpenPopup(sPage, iWidth, iHeight) {
  posX = (screen.width - iWidth) / 2;
  posY = (screen.height - iHeight) / 2;
  
  sFeatures = 'toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=no,width=' + 
    iWidth + ',height=' + iHeight + ',left=' + posX + ',top=' + posY + ',screenX=' + 
    posX + ',screenY=' + posY

  window.open(sPage, 'Popup', sFeatures);
}

function toggleDisplay(sClass, sTag, sState) {
  var oList = document.getElementsByTagName(sTag);
  if (oList) {
    for (var iPos = 0; iPos < oList.length; iPos++) {
      if (oList[iPos].className == sClass) {
        oList[iPos].style.display = (oList[iPos].style.display == 'none') ? sState : 'none';
      }
    }
  }
}

function toggleSpanDisplay(sClass) {
  toggleDisplay(sClass, 'SPAN', 'inline');
  setFrameHeight('pageContent');
}

function toggleDivDisplay(sClass) {
  toggleDisplay(sClass, 'DIV', 'block');
  setFrameHeight('pageContent');
}

function toggleSpanDivDisplay(sClass) {
  toggleDisplay(sClass, 'SPAN', 'inline');
  toggleDisplay(sClass, 'DIV', 'block'); 
  setFrameHeight('pageContent');
}

function reverseSpanDivDisplay(sClass) {
  toggleDisplay(sClass, 'SPAN', 'inline');
  toggleDisplay(sClass, 'DIV', 'block'); 
}

function getMousePos(e) {
  var posx = 0;
  var posy = 0;
  if (!e) {
    var e = window.event;
  }
  if (e.pageX || e.pageY) {
    posx = e.pageX;
    posy = e.pageY;
  }
  else if (e.clientX || e.clientY) {
    posx = e.clientX + document.body.scrollLeft
      + document.documentElement.scrollLeft;
    posy = e.clientY + document.body.scrollTop
      + document.documentElement.scrollTop;
  }
  return {
    left: posx, 
    top: posy
  };
}


function getElementOffset(oElement) {
  var offset = {left: 0, top: 0};
  while (oElement)
  {
      offset.left += oElement.offsetLeft;
      offset.top += oElement.offsetTop;
      oElement = oElement.offsetParent;
  }
  return {
    left: offset.left,
    top: offset.top
  };
}

function includeFile(sFilename, sType) {
  if (!sType || sType == '') {
    sType = 'text/javascript';
  }
	var oHead = document.getElementsByTagName('head')[0];
	var oScript = document.createElement('script');
	oScript.src = sFilename;
	oScript.type = sType;
	
	oHead.appendChild(oScript);
}
*/
