/*
==============================================================
 IE script to cover <select> elements with <iframe>s
==============================================================
*/
function selectHide(menuID)
{
  var j;
  var ieULs = document.getElementById(menuID).getElementsByTagName('ul');
 
  for (j=0; j<ieULs.length; j++)
  {
    selectHideElement(ieULs[j]);
  }
}

function selectHideElement(element)
{
  var existingHider = document.getElementById(element.ID + '_selectHide');
  if (existingHider == null)
  {
    //insert iframe to hide select dropdowns
    element.innerHTML = (element.innerHTML + '<div class="iFrameHiderDiv" id="' + element.ID + '_selectHide"></div><iframe name="IEBugHider" title="IEBugHider" style="filter:progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)" class="iFrameHider" src="about:blank" scrolling="no" frameborder="0"></iframe>');
    var ieMat = element.lastChild; //select iFrame for element
    ieMat.style.height = element.offsetHeight + "px";
  }
}

function showErrorBox(id, show)
{
  var errorBox = document.getElementById(id);
  errorBox.style.display = (show ? 'block' : 'none');  

  //Make sure the errorbox is above windowed controls in IE
  var isIE = (window.event ? true : false);
  if (isIE)
    selectHideElement(errorBox);
}

/* displays the "System is working" message on postback */
function ShowSystemWorking() {
  if (typeof(Page_IsValid) == "undefined" || Page_IsValid == null || Page_IsValid == true) {
    var systemWorking = document.getElementById('SystemWorkingBox');
    var systemWorkingContent = document.getElementById('SystemWorkingContent');
    if (systemWorkingContent != null)
      systemWorkingContent.style.display = "none";
    if (systemWorking != null)
      systemWorking.style.display = "";
  }
}

function test()
{
  alert('test');
}

function checkInputValue(e, controlToValidateID, inputValidatorID, regularExpression, imageUrl, invalidImageUrl, nextControlID) {
  var isValid = false;
  var show = false;
  //Gets the controls from their ID
  var controlToValidate = document.getElementById(controlToValidateID);
  var nextControl = document.getElementById(nextControlID);
  var inputValidator = document.getElementById(inputValidatorID);

  if (invalidImageUrl == "") invalidImageUrl = null;
  
  if (controlToValidate != null && inputValidator != null) {
    //Validate the controls value from regular expression
    var valueToValidate = controlToValidate.value;
    if (valueToValidate != null)
    {
      regEx = new RegExp(regularExpression);
      isValid = (regEx.exec(valueToValidate) != null);
      show = valueToValidate != "" && (isValid || invalidImageUrl != null) && (inputValidator.tagName == "IMG" || inputValidator.tagName == "SPAN");
    }
    //Set the visibility or change the image
    inputValidator.style.visibility = (show ? "" : "hidden");
    if (inputValidator.tagName == "SPAN")
    {
    // This is to fix a problem showing PNG in IE6
      inputValidator.outerHTML = "<span id=" + inputValidatorID + " style=\"width:12px; height:12px; display:inline-block;"
			   + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
               + "(src='" + (isValid || invalidImageUrl == null ? imageUrl : invalidImageUrl) + "', sizingMethod='scale');\"></span>";
    }
    else
    {
      inputValidator.src = (isValid || invalidImageUrl == null ? imageUrl : invalidImageUrl);
    }
    
    //If the value is valid, and we are running in an event, 
    //we move to the next control (if a correct key is pressed)
    if (e != null && nextControl != null && isValid) {
      var isIE = (window.event ? true : false);
      var key = (isIE ? e.keyCode : e.which);
      if (key != null && key != 0) {
        var filter = [0,8,9,16,17,18,37,38,39,40,46];
        if (containsElement(filter, key) == false)
          nextControl.focus();
      }
    }
  }
}

function containsElement(arr, ele)
{
  var found = false;
  for (var i = 0; i < arr.length; i++) {
    if (arr[i] == ele) {
      found = true;
      break;
    }
  }
  return found;
}

/* ****************************************************
  BROWSER DETECTION 
****************************************************** */
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	isIE: function () {
		return (this.browser == "Explorer");
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

/* ----------------------------------------------
  Close warning for all browsers
  See http://www.sitepoint.com/forums/printthread.php?t=273341
---------------------------------------------- */
// default warning message
var closeWarningMessage = null;

function closeWarningSetMessage(message) {
  closeWarningMessage = message;
}

// Cross browser event handling for IE 5+, NS6+ and Gecko
function closeWarningAddEvent(elm, evType, fn, useCapture)
{
  if (elm.addEventListener)
  {
    // Gecko
    elm.addEventListener(evType, fn, useCapture);
    return true;
  }
  else if (elm.attachEvent)
  {
    // Internet Explorer
    var r = elm.attachEvent('on' + evType, fn);
    return r;
  }
  else
  {
    // nutscrape?
    elm['on' + evType] = fn;
  }
}

// Add Listeners
function closeWarningAddListeners(e)
{
  // Before unload listener
  closeWarningAddEvent(window, 'beforeunload', closeWarningExitAlert, false);
}

// Exit Alert
function closeWarningExitAlert(e)
{
  //No warning set, just unload
  if (closeWarningMessage == null)
    return;
    
  // set event
  if (!e) { e = window.event; }
  if (e) { e.returnValue = closeWarningMessage; }

  // return warning message
  return closeWarningMessage;
}

// Initialise
closeWarningAddEvent(window, 'load', closeWarningAddListeners, false);

