﻿// common.js
// 
// 2008 - Presteligence


// vars
var is_ie  = (navigator.userAgent.indexOf('MSIE') >= 0);
var is_ie5 = (navigator.appVersion.indexOf("MSIE 5.5") !=-1);
var _acro = null;


// detect acrobat
if (_acro == null)
{
   _acro = { Installed: false, Version: 0.0 };

   if (is_ie)
   {
      if (checkActiveXObject("AcroPDF.PDF.1"))
      {
         _acro.Version = 7.0;
      }
      else if (checkActiveXObject("PDF.PdfCtrl.1"))
      {
         _acro.Version = 4.0;
      }
      else
      {
         for (var i = 2; i < 10; i++) 
         {
            if (checkActiveXObject('PDF.PdfCtrl.' + i)) 
            {
               version = parseFloat(i + '.0');
            }
         }
      }
   }
   else
   {
      var plugin = navigator.plugins['Adobe Acrobat'];
      if (plugin != null)
      {
         if (plugin.description == "Adobe PDF Plug-In For Firefox and Netscape")
            _acro.Version = 8.0;
         else
            _acro.Version = parseFloat(plugin.description.split('Version ')[1] + ".0");
      }
   }
}


// common functions
function $(id) {
   return document.getElementById(id);
}

function $name(name)
{
   var list = [];
   var all = document.all;
   if (all == null)
      all = document.getElementsByTagName('*');
      
   var attr = null;
   for (var i = 0; i < all.length; i++)
   {
      if (all[i].attributes == null)
         continue;

      attr = all[i].attributes.getNamedItem("name");
      if (attr == null)
         continue;

      if (attr.value == name)
         list[list.length] = all[i];
   }
   
   return list;
}

function clearDropdown(obj) {
   if (obj == null)
      return;
   
   while (obj.options.length > 0)   
      obj.remove(0);
}

function resetDropdownSelection(obj) {
   for (var i = 0; i < obj.options.length; i++)
   {
      obj.options[i].selected = false;
   }
}

function getTopWnd()
{
   var o = window.parent;
   var p = null;
   while (o != null && o != p)
   {
      p = o;
      o = p.parent;
   }

   return p;
}

function checkActiveXObject(name) {
  try 
  {
    var o = new ActiveXObject(name);
    if (o != null)
      return true;
  } 
  catch (e) { }
  
  return false;
}

function _findOffsetX(obj, stopId)
{
   var lft = 0;
   if (!obj.offsetParent)
      return 0;
   
   while (obj.offsetParent)   
   {
      lft += obj.offsetLeft;
      obj = obj.offsetParent;
      
      if (obj.id == stopId)
         break;
   }
   return lft;
}

function _findOffsetY(obj, stopId)
{
   var top = 0;
   if (!obj.offsetParent)
      return 0;
   
   while (obj.offsetParent)   
   {
      top += obj.offsetTop;
      obj = obj.offsetParent;
      
      if (obj.id == stopId)
         break;
   }
   return top;
}

function _getYScroll()
{
   if (typeof(window.pageYOffset) == 'number')
      return window.pageYOffset;
   else if (document.body && document.body.scrollTop)
      return document.body.scrollTop;
   else if(document.documentElement && document.documentElement.scrollTop)
      return document.documentElement.scrollTop;
   else
      return 0;
}

function _getXScroll()
{
   if (typeof(window.pageXOffset) == 'number')
      return window.pageXOffset;
   else if (document.body && document.body.scrollLeft)
      return document.body.scrollLeft;
   else if(document.documentElement && document.documentElement.scrollLeft)
      return document.documentElement.scrollLeft;
   else
      return 0;
}

function _totalDocHeight()
{
   var scroll = 0;
   if (window.innerHeight && window.scrollMaxY) // firefox
   {
      scroll = window.innerHeight + window.scrollMaxY;
   } 
   else if (document.body.scrollHeight > document.body.offsetHeight) // all but Explorer Mac
   { 
	   scroll = document.body.scrollHeight;
   } 
   else // works in Explorer 6 Strict, Mozilla (not FF) and Safari
   { 
      scroll = document.body.offsetHeight;
  	}
  	
  	return scroll;
}

function _totalDocWidth()
{
   var scroll = 0;
   if (window.innerWidth && window.scrollMaxX) // firefox
   {
      scroll = window.innerWidth + window.scrollMaxX;
   }
   else if (document.body.scrollWidth > document.body.offsetWidth) // all but Explorer Mac
   { 
	   scroll = document.body.scrollWidth;
   } 
   else // works in Explorer 6 Strict, Mozilla (not FF) and Safari
   { 
      scroll = document.body.offsetWidth;
  	}
  	
  	return scroll;
}

function setCropPanel(id, x, y, w, h)
{
   $(id).style.top = y + "px";
   $(id).style.left = x + "px";
   $(id).style.width = w + "px";
   $(id).style.height = h + "px";
   $(id).style.display = "";
}

function setObjLoc(id, x, y)
{
   $(id).style.top = y + "px";
   $(id).style.left = x + "px";
   $(id).style.display = "";
}

function windowSize() {
   var w = 0;
   var h = 0;

   if (!window.innerWidth) {     //IE
      if (!(document.documentElement.clientWidth == 0)) {   //strict mode
         w = document.documentElement.clientWidth;
         h = document.documentElement.clientHeight;
      } else { // quirks
         w = document.body.clientWidth;
         h = document.body.clientHeight;
      }
   } else { //w3c
      w = window.innerWidth;
      h = window.innerHeight;
   }
   
   return { width: w, height: h };
}

function IsNumeric(val)
{
   if (isNaN(parseFloat(val)))
      return false;

   return true;
}
