﻿var baseUrl = "/";

//addOnLoad(initForm);
addOnLoad(pngFix);
addOnLoad(sfBorder);
addOnLoad(sfHover);

function addOnLoad(newFunction)
{
	var oldOnLoad = window.onload;
	
	if (typeof oldOnLoad == "function")
	{
		window.onload = function()
		{
			if (oldOnLoad)
			{
				oldOnLoad();
			}

			newFunction();
		}
	}
	else
	{
		window.onload = newFunction;
	} 
}

function areasOfInterest(areas)
{
  if ($(areas))
  {
    var areasOfInterest = $(areas).getElements("input[type=checkbox]");
    
    if (areasOfInterest.length > 0)
    {
      var areasOfInterestAll = areasOfInterest[areasOfInterest.length - 1];
      
      // OnClick
      areasOfInterestAll.onclick = function()
      {
        for (var i = 0; i < (areasOfInterest.length - 1); i++)
        {
          areasOfInterest[i].checked = areasOfInterestAll.checked;
          areasOfInterest[i].disabled = areasOfInterestAll.checked;
        }
      }

      // Init
      if (areasOfInterestAll.checked)
      {
        for (var i = 0; i < (areasOfInterest.length - 1); i++)
        {
          areasOfInterest[i].checked = areasOfInterestAll.checked;
          areasOfInterest[i].disabled = areasOfInterestAll.checked;
        }
      }
    }
  }
}

function check_all(checkbox, gridview)
{
  var gv = document.getElementById(gridview);

  if (gv)
  {
    var controls = gv.getElementsByTagName("input")
    
    if (controls.length > 0)
    {
      for (var i = 0; i < controls.length; i++)
      {
        if (controls[i].type == "checkbox")
        {
          controls[i].checked = (checkbox.checked ? "checked" : "");
        }
      }
    }
  }
}

function default_button(btn_id)
{
  var form = document.forms[0];
  var btn = $(btn_id);
  
  if (form && btn)
  {
	  form.onkeypress = function(e)
	  {
      // get event object (Firefox : IE)
      e = (e) ? e : window.event;

      // get key code (IE : FF)
	    var keyCode = (e.keyCode) ? e.keyCode : e.which;
	    
	    // get target (IE : FF)
	    var srcElement = (e.srcElement) ? e.srcElement : e.target;
	    
	    if (keyCode == 13 && srcElement.type != "textarea")
	    {
		    if (btn.type == "submit")
		    {
		      btn.click();
		    }
		    else
		    {
		      eval(unescape(btn.href));
		    }
	    }
	  }
  }
}

function help(title, msg, e)
{
  // remove if msg is empty
  if (msg != "" && msg != null)
  {
    var helpMsg = document.createElement("div");
    helpMsg.setAttribute("id", "help-msg");
    
    // display name of option
    if (title != "" && title != null)
    {
      var helpTitle = document.createElement("h1");
      helpTitle.appendChild(document.createTextNode(title));
      helpMsg.appendChild(helpTitle);
    }
    
    var msgDiv = document.createElement("div");
    msgDiv.innerHTML = msg;
    helpMsg.appendChild(msgDiv);
    
    // get event object (Firefox : IE)
    e = (e) ? e : window.event;
    
    helpMsg.style.top = (e.clientY + 8) + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop) + "px";
    helpMsg.style.left = (e.clientX + 8) + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft) + "px";
    
    // insert help-msg DIV
    document.body.appendChild(helpMsg);
  }
  else
  {
    var helpMsg = document.getElementById("help-msg");

    helpMsg.innerHTML = "";
    helpMsg.style.display = "none";
	  
    // remove help-msg DIV
    document.body.removeChild(helpMsg);
  }
}

function initForm()
{
  $$("table.form").each(function(item, index){
    $$("input.text", "select", "textarea").each(function(item, index){
      item.onfocus = function()
      {
        this.effect("background-color").start("#eaeaea", "#ffc");
      },
      item.onblur = function()
      {
        this.effect("background-color").start("#ffc", "#eaeaea");
      }
      });
      $$("span.option", "table.option-list td").each(function(item, index){
        item.onfocus = function()
        {
          this.effect("background-color").start("#fff", "#ffc");
        },
        item.onblur = function()
        {
          this.effect("background-color").start("#ffc", "#fff");
        }
      });
  });
}

function pngFix()
{
  var arVersion = navigator.appVersion.split("MSIE");
  var version = parseFloat(arVersion[1]);

  if ((version >= 5.5 && version <= 6) && (document.body.filters)) 
  {
     for(var i=0; i<document.images.length; i++)
     {
        var img = document.images[i];
        var imgName = img.src.toUpperCase();
        if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
        {
           var imgID = (img.id) ? "id='" + img.id + "' " : "";
           var imgClass = (img.className) ? "class='" + img.className + "' " : "";
           var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
           var imgStyle = "display:inline-block;" + img.style.cssText ;
           if (img.align == "left") imgStyle = "float:left;" + imgStyle;
           if (img.align == "right") imgStyle = "float:right;" + imgStyle;
           if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle;
           var strNewHTML = "<span " + imgID + imgClass + imgTitle
           + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
           + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
           + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>";
           img.outerHTML = strNewHTML;
           i = i-1;
        }
     }
  }
}

function sfBorder()
{
  var arVersion = navigator.appVersion.split("MSIE");
  var version = parseFloat(arVersion[1]);

  if (window.attachEvent && document.getElementById("nav-main") && version <= 6)
  {
	  var sfEls = document.getElementById("nav-main").getElementsByTagName("IMG");
  	
	  for (var i = 0; i < sfEls.length; i++)
	  {
		  sfEls[i].onmouseover = function()
		  {
			  this.className += " sfborder";
		  };

		  sfEls[i].onmouseout = function()
		  {
			  this.className = this.className.replace(new RegExp(" sfborder\\b"), "");
		  };
	  }
  }
}

function sfHover()
{
  var arVersion = navigator.appVersion.split("MSIE");
  var version = parseFloat(arVersion[1]);

  if (window.attachEvent && document.getElementById("nav-main") && version <= 6)
  {
	  var sfEls = document.getElementById("nav-main").getElementsByTagName("LI");

	  for (var i = 0; i < sfEls.length; i++)
	  {
		  sfEls[i].onmouseover = function()
		  {
		    toggleSelects(true);
			  this.className += " sfhover";
		  };

		  sfEls[i].onmouseout = function()
		  {
		    toggleSelects(false);
			  this.className = this.className.replace(new RegExp(" sfhover\\b"), "");
		  };
	  }
  }
}

function toggleSelects(hide)
{
  $$("table.form").each(function(item, index){
    $$("select").each(function(item, index){
      item.style.visibility = (hide ? "hidden" : "");
      });
  });
}

function openWindow(url, n, w, h, sb, r, mb, tb, s, loc, dir)
{
  var screenWidth = window.screen.availWidth;
  var screenHeight = window.screen.availHeight;

  var offsetXWinXP = 5;
  var offsetYWinXP = 18;

  var halfScreenWidth = Number(window.screen.availWidth / 2);
  var halfScreenHeight = Number(window.screen.availHeight / 2);

  var halfWindowWidth = Number(w / 2);
  var halfWindowHeight = Number(h / 2);

  var diffWidth = Number(halfScreenWidth-halfWindowWidth);
  var diffHeight = Number(halfScreenHeight-halfWindowHeight);

  diffWidth -= offsetXWinXP;
  diffHeight -= offsetYWinXP;
  
  var windowName = n;
  
  if (windowName.length == 0)
  {
    //The windowName is a zero-based integer corresponding to the number of milliseconds
    //since January 1, 1970, to the date specified by the instance of the Date object;
    //therefore, the windowName will almost always be unique.
    var dtm = new Date();
    windowName = dtm.getTime();
  }
  
  //if (url == null) { url = "" };
  if (w == null) { w = 640 };
  if (h == null) { h = 480 };
  if (sb == null) { sb = "no" };
  if (r == null) { r = "no" };
  if (mb == null) { mb = "no" };
  if (tb == null) { tb = "no" };
  if (s == null) { s = "no" };
  if (loc == null) { loc = "no" };
  if (dir == null) { dir = "no" };

  var features = new StringBuilder();
  features.Append("directories=" + dir + ",");
  features.Append("location=" + loc + ",");
  features.Append("menubar=" + mb + ",");
  features.Append("resizable=" + r + ",");
  features.Append("scrollbars=" + sb + ",");
  features.Append("status=" + s + ",");
  features.Append("toolbar=" + tb +",");
  features.Append("width=" + w + ",");
  features.Append("height=" + h + ",");
  features.Append("top=" + diffHeight + ",");
  features.Append("left=" + diffWidth);
  
  return window.open(url, windowName, features.ToString());
}

// Initializes a new instance of the StringBuilder class
// and appends the given value if supplied
function StringBuilder(value)
{
  this.strings = new Array("");
  this.Append(value);
}

// Appends the given value to the end of this instance.
StringBuilder.prototype.Append = function (value)
{
  if (value)
  {
    this.strings.push(value);
  }
}

// Appends the given value to the end of this instance with a line break.
StringBuilder.prototype.AppendLine = function (value)
{
  if (value)
  {
    this.strings.push(value + "\n");
  }
}

// Clears the string buffer
StringBuilder.prototype.Clear = function ()
{
  this.strings.length = 1;
}

// Converts this instance to a String.
StringBuilder.prototype.ToString = function ()
{
  return this.strings.join("");
}

function WebForm_FireDefaultButton(event, target) 
{
  // event.srcElement doesn't work in FF so we check whether
  // it or event.target exists, using whichever is returned
  var element = event.target || event.srcElement;
  
  if (event.keyCode == 13 && !(element && element.tagName.toLowerCase() == "textarea")) 
  {
    var defaultButton;
    if (__nonMSDOMBrowser) 
    {
      defaultButton = document.getElementById(target);
    } 
    else 
    {
      defaultButton = document.all[target];
    }
    if (defaultButton && typeof defaultButton.click != "undefined") 
    {
      defaultButton.click();
      event.cancelBubble = true;
      if (event.stopPropagation) 
      {
        event.stopPropagation();
      }
      return false;
    }
  }
  return true;
}

