﻿// JScript File

var doc = document;
var nav = top.navigator;
var position = {};
var global = {};
var util = {};

var dialog;
var background;

// Used to work around some browser bugs where we can't use feature testing.
global.isIE = /msie/.test(nav.userAgent.toLowerCase());
global.isIE_5or6 = /msie 6/.test(nav.userAgent.toLowerCase()) || /msie 5/.test(nav.userAgent.toLowerCase());
global.isIE_7plus = global.isIE && !global.isIE_5or6;
global.isOpera = /opera/.test(nav.userAgent.toLowerCase());
global.isKonqueror = /konqueror/.test(nav.userAgent.toLowerCase());

function ToggleElement(id)
{
  var element = document.getElementById(id);
  if (element.style.display == "none")
  {
    element.style.display = "block";
  }
  else
  {
    element.style.display = "none";
  }
}

function ToggleElementDisplay(id, displayMode)
{
  var element = document.getElementById(id);
  if (element.style.display == "none")
  {
    element.style.display = displayMode;
  }
  else
  {
    element.style.display = "none";
  }
}

function IsVisible(element)
{
  if (typeof (element.parentNode) != 'undefined')
    return element.parentNode.style.display != "none";
  else
    return true;
}

function Focus(id)
{
  var element = document.getElementById(id);

  if (element != null && typeof (element) != 'undefined' && IsVisible(element))
    element.focus();
}

function UpdateAnimate(element, fromColor, toColor)
{
  Animate(element, fromColor, toColor, 1000);
}

function Animate(element, fromColor, toColor, speed)
{
  element.style.backgroundColor = fromColor;
  jQuery(element).animate({ backgroundColor: toColor }, speed);
}

function AddScriptBlock(strScript)
{
  $('<script type=text/javascript>' + strScript + '</sc' + 'ript>').appendTo(document.body);
}

function GetSelectedIds(attributeName)
{
  var ids = '';

  $(".GridSelectCheckbox INPUT").each(function(index)
  {
    if ($(this)[0].checked == true)
    {
      ids += $(this).attr(attributeName) + '|';
    }
  });

  return ids.substring(0, ids.length - 1);
}

function SelectAllGridItems(sender)
{
  $(".GridSelectCheckbox INPUT").each(function(index)
  {
    $(this)[0].checked = sender.checked;
  });
}

function PromptPopup(elem, width, height)
{
	CreateBackground();
  CreateDialog(elem, width, height);
}

function CreateDialog(elem, width, height)
{
  // The main dialog box.
  dialog = elem;
  dialog.className = "prompt-dialog";
  dialog.style.display = "block";
  dialog.style.padding = "10px;";
  dialog.style.position = "fixed";
  dialog.style.width = width + "px";
  dialog.style.height = height + "px";
  dialog.style.zIndex = "1001";

  util.addEvent(doc.body, "keydown", CheckEscapeCloseDialog);
  
  dialog.style.top = "50%";
  dialog.style.left = "50%";
  dialog.style.display = "block";
  
  if (global.isIE_5or6)
  {
    dialog.style.position = "absolute";
    dialog.style.top = doc.documentElement.scrollTop + 200 + "px";
    dialog.style.left = "50%";
  }

  // This has to be done AFTER adding the dialog to the form if you
  // want it to be centered.
  dialog.style.marginTop = -(position.getHeight(dialog) / 2) + "px";
  dialog.style.marginLeft = -(position.getWidth(dialog) / 2) + "px";  
}

// Creates the background behind the hyperlink text entry box.
// Most of this has been moved to CSS but the div creation and
// browser-specific hacks remain here.
function CreateBackground()
{
  background = document.createElement("div");
  background.className = "prompt-background";
  style = background.style;
  style.position = "absolute";
  style.top = "0";

  style.zIndex = "1000";

  // Some versions of Konqueror don't support transparent colors
  // so we make the whole window transparent.
  //
  // Is this necessary on modern konqueror browsers?
  if (global.isKonqueror)
  {
    style.backgroundColor = "transparent";
  }
  else if (global.isIE)
  {
    style.filter = "alpha(opacity=50)";
  }
  else
  {
    style.opacity = "0.5";
  }

  var pageSize = position.getPageSize();
  style.height = pageSize[1] + "px";

  if (global.isIE)
  {
    style.left = document.documentElement.scrollLeft;
    style.width = document.documentElement.clientWidth;
  }
  else
  {
    style.left = "0";
    style.width = "100%";
  }

  document.body.appendChild(background);
}

// Used as a keydown event handler. Esc dismisses the prompt.
// Key code 27 is ESC.
function CheckEscapeCloseDialog(key)
{
  var code = (key.charCode || key.keyCode);
  if (code === 27)
  {
    CloseDialog(true);
  }
};

// Dismisses the hyperlink input box.
// isCancel is true if we don't care about the input text.
// isCancel is false if we are going to keep the text.
function CloseDialog(isCancel)
{
  util.removeEvent(doc.body, "keydown", CheckEscapeCloseDialog);

  ToggleElement(dialog.id);
  background.parentNode.removeChild(background);

  background = null;
  dialog = null;
  
  return false;
};

// UNFINISHED
// The assignment in the while loop makes jslint cranky.
// I'll change it to a better loop later.
position.getTop = function(elem, isInner)
{
  var result = elem.offsetTop;
  if (!isInner)
  {
    while (elem = elem.offsetParent)
    {
      result += elem.offsetTop;
    }
  }
  return result;
};

position.getHeight = function(elem)
{
  return elem.offsetHeight || elem.scrollHeight;
};

position.getWidth = function(elem)
{
  return elem.offsetWidth || elem.scrollWidth;
};

position.getPageSize = function()
{

  var scrollWidth, scrollHeight;
  var innerWidth, innerHeight;

  // It's not very clear which blocks work with which browsers.
  if (self.innerHeight && self.scrollMaxY)
  {
    scrollWidth = doc.body.scrollWidth;
    scrollHeight = self.innerHeight + self.scrollMaxY;
  }
  else if (doc.body.scrollHeight > doc.body.offsetHeight)
  {
    scrollWidth = doc.body.scrollWidth;
    scrollHeight = doc.body.scrollHeight;
  }
  else
  {
    scrollWidth = doc.body.offsetWidth;
    scrollHeight = doc.body.offsetHeight;
  }

  if (self.innerHeight)
  {
    // Non-IE browser
    innerWidth = self.innerWidth;
    innerHeight = self.innerHeight;
  }
  else if (doc.documentElement && doc.documentElement.clientHeight)
  {
    // Some versions of IE (IE 6 w/ a DOCTYPE declaration)
    innerWidth = doc.documentElement.clientWidth;
    innerHeight = doc.documentElement.clientHeight;
  }
  else if (doc.body)
  {
    // Other versions of IE
    innerWidth = doc.body.clientWidth;
    innerHeight = doc.body.clientHeight;
  }

  var maxWidth = Math.max(scrollWidth, innerWidth);
  var maxHeight = Math.max(scrollHeight, innerHeight);
  return [maxWidth, maxHeight, innerWidth, innerHeight];
};

// Returns true if the DOM element is visible, false if it's hidden.
// Checks if display is anything other than none.
util.isVisible = function(elem)
{

  if (window.getComputedStyle)
  {
    // Most browsers
    return window.getComputedStyle(elem, null).getPropertyValue("display") !== "none";
  }
  else if (elem.currentStyle)
  {
    // IE
    return elem.currentStyle["display"] !== "none";
  }
};


// Adds a listener callback to a DOM element which is fired on a specified
// event.
util.addEvent = function(elem, event, listener)
{
  if (elem.attachEvent)
  {
    // IE only.  The "on" is mandatory.
    elem.attachEvent("on" + event, listener);
  }
  else
  {
    // Other browsers.
    elem.addEventListener(event, listener, false);
  }
};


// Removes a listener callback from a DOM element which is fired on a specified
// event.
util.removeEvent = function(elem, event, listener)
{
  if (elem.detachEvent)
  {
    // IE only.  The "on" is mandatory.
    elem.detachEvent("on" + event, listener);
  }
  else
  {
    // Other browsers.
    elem.removeEventListener(event, listener, false);
  }
};

var mouseX = 0;
var mouseY = 0;

$(document).ready(function()
{
	$().mousemove(function(e)
	{
		mouseX = e.pageX;
		mouseY = e.pageY;
	});
});

function GetMouseX()
{
	return mouseX;
}

function GetMouseY()
{
	return mouseY;
}

function FindPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (1)
		{
			curleft += obj.offsetLeft;
			if (!obj.offsetParent)
				break;
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
		
	return curleft;
}

function FindPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (1)
		{
			curtop += obj.offsetTop;
			if (!obj.offsetParent)
				break;
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function AjaxHandlerCallback(postData, callbackFunction)
{
	//alert(postData);
	var xml;

	var url = location.protocol + '//' + location.host + '/Ajax.handler';

	if (postData == null)
		postData = "";

	var ie = (window.navigator.appName.toLowerCase().indexOf('explorer') != -1);

	if (ie)
		xml = new ActiveXObject("Microsoft.XMLHTTP")
	else
		xml = new XMLHttpRequest();

	if (callbackFunction != null)
	{
		xml.onreadystatechange = function()
		{
			if (xml.readyState == 4)
			{
				var result = xml.responseText;

				callbackFunction(result);
			}
		}
	}
	try
	{
		xml.open("POST", url, callbackFunction != null);
	}
	catch (e)
	{
		alert(e);
	}

	xml.send(postData);

	if (callbackFunction == null)
	{
		return xml.responseText;
	}}

function AjaxPageCallback(postData, callbackFunction)
{
  //alert(postData);
  var xml;

  var url = location.protocol + '//' + location.host + '/Ajax.aspx?__AjaxCallback=AjaxCallback';

  if (postData == null)
    postData = "";

  var ie = (window.navigator.appName.toLowerCase().indexOf('explorer') != -1);

  if (ie)
    xml = new ActiveXObject("Microsoft.XMLHTTP")
  else
    xml = new XMLHttpRequest();

  if (callbackFunction != null)
  {
    xml.onreadystatechange = function()
    {
      if (xml.readyState == 4)
      {
        var result = xml.responseText;

        callbackFunction(result);
      }
    }
  }
  try
  {
    xml.open("POST", url, callbackFunction != null);
  }
  catch (e)
  {
    alert(e);
  }

  xml.send(postData);

  if (callbackFunction == null)
  {
    return xml.responseText;
  }
}

function AddNewsletterMember(name, email, callbackFunction)
{
	var strXml = '<callback><action>AddNewsletterMember</action><name>' + name + '</name><email>' + email + '</email></callback>';
	AjaxHandlerCallback(strXml, callbackFunction);
}

function ValidateEmailAddress(email)
{
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	
	if (reg.test(email) == false)
	{
		return false;
	}

	return true;}

function escapeHTML(str)
{
	var div = document.createElement('div');
	var text = document.createTextNode(str);
	div.appendChild(text);
	return div.innerHTML;
}

function StripHtmlTags(oldString)
{
	var newString = "";
	var inTag = false;

	for (var i = 0; i < oldString.length; i++)
	{
		if (oldString.charAt(i) == '<') inTag = true;
		if (oldString.charAt(i) == '>')
		{
			if (oldString.charAt(i + 1) == "<")
			{
				//dont do anything
			}
			else
			{
				inTag = false;
				i++;
			}
		}

		if (!inTag)
			newString += oldString.charAt(i);
	}

	return newString;
}

function ShowLoadingMessage(message, elemToInsertAfter)
{
	$("<div class='loading'>" + message + "</div>").insertAfter(elemToInsertAfter);
}

function HideLoadingMessages()
{
	$(".loading").remove();
}