﻿// JScript File

/*allows multipul window.onload events 
call like --- addLoadEvent(nameOfFunctionToRunOnPageLoad);   
http://www.webreference.com/programming/javascript/onloads/
*/
function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function () {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
} 

// dropDownList -
function dropDownOver(tagID, alignToTagID, addTop, addLeft){
    positionElement(tagID, alignToTagID, addTop, addLeft)
    document.getElementById(tagID).style.visibility = "visible";
}

function dropDownOut(tagID){
    document.getElementById(tagID).style.visibility = "hidden";
}



function positionElement(tagID, alignToTagID, addTop, addLeft){
    var pos = getXY(document.getElementById(alignToTagID))
    x = pos.x + addLeft
    y = pos.y + addTop
    document.getElementById(tagID).style.left = x + "px";
    document.getElementById(tagID).style.top = y + "px";
}


/**
 * Returns the absolute X and Y positions of an object.
 * @param {HTMLObject} obj HTML Object.
 * @return {Object} Returns an accessor with .x and .y values.
 */
function getXY(obj)
{
  var curleft = 0;
  var curtop = obj.offsetHeight + 5;
  var border;
  if (obj.offsetParent)
  {
    do
    {
      // XXX: If the element is position: relative we have to add borderWidth
      if (getStyle(obj, 'position') == 'relative')
      {
        if (border = getStyle(obj, 'border-top-width')) curtop += parseInt(border); //_pub.getStyle(obj, 'border-top-width')) curtop += parseInt(border);
        if (border = getStyle(obj, 'border-left-width')) curleft += parseInt(border);//_pub.getStyle(obj, 'border-left-width')) curleft += parseInt(border);
      }
      curleft += obj.offsetLeft;
      curtop += obj.offsetTop;
    }
    while (obj = obj.offsetParent)
  }
  else if (obj.x)
  {
    curleft += obj.x;
    curtop += obj.y;
  }
  return {'x': curleft, 'y': curtop};
}
/**
 * Returns the specified computed style on an object.
 * @param {HTMLObject} obj HTML Object
 * @param {String} styleProp Property name.
 * @return {Mixed} Computed style on object.
 */
function getStyle(obj, styleProp)
{
  if (obj.currentStyle)
    return obj.currentStyle[styleProp];
  else if (window.getComputedStyle)
    return document.defaultView.getComputedStyle(obj,null).getPropertyValue(styleProp);
}





//open-close window functions
//onClick="openWindow('url','','toolbar=yes,scrollbars=yes,resizable=yes,width=450,height=450')"
function openWindow(theURL,winName,features) 
{
    var popup;
    popup=window.open(theURL,winName,features);
    /*popup.moveTo(80, 80);  causes error in some browsers */
}
function closeWindow() {
    window.close();
}


function textCounter(limit, txtBox, displayTagID) {
  var maxLimit = limit;
  var rem = document.getElementById(displayTagID);
  var tb = document.getElementById(txtBox.id);
 
  if (tb.value.length > maxLimit)
      {tb.value = tb.value.substring(0, maxLimit);}
  else
      {rem.innerHTML = maxLimit - tb.value.length}
  }

  //====Start stop double button click=====
  //
  /*
  use on control : onclick="return stopDoubleClick(this)"

  Preload image on page by adding following script

  <script type="text/javascript">
  preloadImages('/App_Themes/main/images/other/progressBar.gif')
  </script>
  */
  var click = 0
  function stopDoubleClick(control) {
      click = click + 1
      document.getElementById(control.id).style.visibility = "hidden"
      if (click == 1) {
          processingImage(control)
          return true
      } else {
          return false
      }
  }

  function processingImage(control) {

      var newdiv = document.createElement('div');
      var divId = 'divProcessing';
      newdiv.setAttribute('id', divId);
      newdiv.setAttribute('style', 'position:absolute; float:left; background-color:white;text-align:center;');
      newdiv.innerHTML = 'Processing<br/><img src="/App_Themes/main/images/other/progressBar.gif" />';

      var btn = document.getElementById(control.id);     
      btn.parentNode.insertBefore(newdiv,btn); 
  }

  //====End stop double button click=====

  /*preload images
  use 
  <script type="text/javascript">
        preload('image1.jpg,image2.jpg,image3.jpg');
  </script>
  */
  function preloadImages(images) {
      if (document.images) {
          var i = 0;
          var imageArray = new Array();
          imageArray = images.split(',');
          for (i = 0; i <= imageArray.length - 1; i++) {
              //document.write('<img src="' + imageArray[i] + '" />');// Write to page (uncomment to check images)
              var imageObj = new Image();
              imageObj.src = imageArray[i];
          }
      }
  }

