/* -------------------------------------------------------- */
/* TIPBOX */

function fncShowTipBox(oField,strTipText) {
  var intYOffset = 0;
  if (typeof(arguments[2]) != "undefined") {
    if (!isNaN(arguments[2])) {
      intYOffset = arguments[2];
    }
  }
  
  if (document.getElementById('idTipBox') && document.getElementById('idTopBoxContent') && typeof(oField) != "undefined") {
    var intFieldY = fncGetAbsoluteY(oField);
    if (!isNaN(intFieldY)) {
      if (strTipText.length > 0) { 
        document.getElementById('idTopBoxContent').innerHTML = strTipText;
      } 
      document.getElementById('idTipBox').style.top = (intFieldY+intYOffset) + "px";
      document.getElementById('idTipBox').style.display = "block";
    }
  }
}

function fncHideTipBox() {
  if (document.getElementById('idTipBox')) {
    document.getElementById('idTipBox').style.display = "none";
  }
}


/* -------------------------------------------------------- */
/* SIDEBAR TOGGLE */

function fncSideToggle(oTrigger) {
  if (typeof(oTrigger) == "string") {
    if (document.getElementById(oTrigger)) {
      oTrigger = document.getElementById(oTrigger);
    }
  } 
    
  var strTriggerID = oTrigger.id;
  var strContainerBoxID = strTriggerID + "Container";
  if (document.getElementById(strContainerBoxID)) {
    if (document.getElementById(strContainerBoxID).style.display != "none") {
      document.getElementById(strContainerBoxID).style.display = "none";
      fncAddClass(oTrigger,"clsPlus");
      // A cookie could be set here to automatically close this box onload (ex: fncSideToggle('idSideMyRentalCredits')
    }
    else {
      document.getElementById(strContainerBoxID).style.display = "block";
      fncRemoveClass(oTrigger,"clsPlus");
    }
  }
}


/* -------------------------------------------------------- */
/* UTILITY -- CLASS FUNCTIONS */

function fncAddClass(oElement, strClassName) {
  if (typeof(oElement) == "object" && strClassName.length > 0) {
    if (typeof(oElement.className) == "string") {  
      var strCurrentClassName = oElement.className;
      if (strCurrentClassName.indexOf(strClassName) < 0) {
        if (strCurrentClassName.length > 1) {
          strClassName = strCurrentClassName + " " + strClassName;
        }        
        oElement.className = strClassName;
      }
    }
  }
}

function fncRemoveClass(oElement, strClassName) {
  if (typeof(oElement) == "object" && strClassName.length > 0) {
    if (typeof(oElement.className) == "string") {  
      var strCurrentClassName = oElement.className;
      if (strCurrentClassName.indexOf(strClassName) >= 0) {
        strNewClassName = fncRemoveClassRecursive(" " + strCurrentClassName + " ", " " + strClassName + " ");
        if (strNewClassName.indexOf(" ") == 0) { strNewClassName = strNewClassName.substring(1); }
        if (strNewClassName.lastIndexOf(" ") == strNewClassName.length-1) { strNewClassName = strNewClassName.substring(0,strNewClassName.length-1); }
        oElement.className = strNewClassName;
      }
    }
  }
}

function fncRemoveClassRecursive(strSource, strSubstrToRemove) {
  var intIndex = strSource.indexOf(strSubstrToRemove);
  var strReturn = "";
  if (intIndex == -1) return strSource;
  strReturn += strSource.substring(0,intIndex) + " " + fncRemoveClassRecursive(strSource.substring(intIndex + strSubstrToRemove.length), strSubstrToRemove);
  return strReturn;
}


/* -------------------------------------------------------- */
/* UTILITY -- POSITIONING FUNCTIONS */

function fncGetAbsoluteX(oElement) {
  // Utility function to get the absolute X-coordinate of an object on the page
  if (typeof(oElement) == "string") {
    oElement = document.getElementById(oElement);
  }
  if (typeof(oElement) == "object") {
    var intCoords = {x: 0};
    while (oElement) {
      intCoords.x += oElement.offsetLeft;
      oElement = oElement.offsetParent;
    }
    return intCoords.x;
  }
}
function fncGetAbsoluteY(oElement) {
  // Utility function to get the absolute Y-coordinate of an object on the page
  if (typeof(oElement) == "string") {
    oElement = document.getElementById(oElement);
  }
  if (typeof(oElement) == "object") {
    var intCoords = {y: 0};
    while (oElement) {
      intCoords.y += oElement.offsetTop;
      oElement = oElement.offsetParent;
    }
    return intCoords.y;
  }
}


/* -------------------------------------------------------- */
/* UTILITY -- ONLOAD */

if (typeof window.addEventListener != 'undefined') {
	window.addEventListener('load', fncPageOnload, false);
}
else if (typeof document.addEventListener != 'undefined') {
	document.addEventListener('load', fncPageOnload, false);
}
else if (typeof window.attachEvent != 'undefined') {
	window.attachEvent('onload', fncPageOnload);
}
else {
	if(typeof window.onload == 'function') {
		var onload_existing = onload;
		window.onload = function() {
			onload_existing();
			fncPageOnload();
		};
	}
	else {
		window.onload = fncPageOnload;
	}
}

function fncPageOnload() {
  if (typeof(fncThisPageOnload) == "function") {
    fncThisPageOnload();
  }
}

/* -------------------------------------------------------- */
/* EDIT_AVAILABILITY PAGE -- ENABLE/DISABLE TIME SELECTORS  */
/* IF CHECKBOX IS CHECKED                                   */
function fncDisableTimeBoxes(bool, index) {
    document.getElementById("availability_" + index + "_from_time").disabled = bool;
    document.getElementById("availability_" + index + "_to_time").disabled = bool;
}
