// JavaScript utilities
// Bradley Searle - 01 January 2007

// Elements
$ = function (Element) { return document.getElementById(Element); }
show = function (Element) { Element.style.display = "block"; }
hide = function (Element) { Element.style.display = "none"; }
isVisible = function (Element) { return Element.style.display == "block" || Element.style.display == ""; }
getVisibility = function (Element) { return Element.style.display; }
setVisibility = function (Element, Visibility) { Element.style.visibility = Visibility; }
getHeight = function (Element) { var OriginallyVisible = isVisible(Element); if (!isVisible(Element)) show(Element); var Height = Element.offsetHeight; if (!OriginallyVisible) hide(Element); return Height; }
setHeight = function (Element, Height) {  if (isNaN(Height)) Element.style.height = Height; else Element.style.height = Height + "px"; }
getWidth = function (Element) { var OriginallyVisible = isVisible(Element); if (!isVisible(Element)) show(Element); var Width = Element.offsetWidth; if (!OriginallyVisible) hide(Element); return Width; }
setWidth = function (Element, Width) { if (isNaN(Width)) Element.style.width = Width; else Element.style.width = Width + "px"; }
getOffsetTop = function (Element) { var Top = 0; while (Element.offsetParent) { Top += getTop(Element); Element = Element.offsetParent; } return Top; }
getTop = function (Element) { var OriginallyVisible = isVisible(Element); if (!isVisible(Element)) show(Element); var Top = Element.offsetTop; if (!OriginallyVisible) hide(Element); return Top; }
setTop = function (Element, Top) { Element.style.top = Top + "px"; }
getOffsetLeft = function (Element) { var Left = 0; while (Element.offsetParent) { Left += getLeft(Element); Element = Element.offsetParent; } return Left; }
getLeft = function (Element) { var OriginallyVisible = isVisible(Element); if (!isVisible(Element)) show(Element); var Left = Element.offsetLeft; if (!OriginallyVisible) hide(Element); return Left; }
setLeft = function (Element, Left) { Element.style.left = Left + "px"; }
getClip = function (Element) { var clips = Element.style.clip.toString().replace(/[^0-9\s]/gi, "").split(" "); return { top: clips[0], right: clips[1], bottom: clips[2], left: clips[3], width: clips[1] - clips[3], height: clips[2] - clips[0] }; }
setClip = function (Element, Top, Left, Width, Height) { Element.style.clip = "rect(" + Top + "px, " + (Left + Width) + "px, " + (Top + Height) + "px, " + Left + "px)"; }
getZindex = function (Element) { return Element.style.zIndex; }
setZindex = function (Element, Index) { Element.style.zIndex = Index; }
setHTML = function (Element, HTML) { Element.innerHTML = HTML; }

// Misc
getScrollX = function () { try { return document.documentElement.scrollLeft + document.body.scrollLeft; } catch (e) { return window.scrollX; } }
getScrollY = function () { try { return document.documentElement.scrollTop + document.body.scrollTop; } catch (e) { return window.scrollY; } }
getMouseX = function (Event) { if (!Event) Event = window.event; return Event.clientX + getScrollX(); }
getMouseY = function (Event) { if (!Event) Event = window.event; return Event.clientY + getScrollY(); }
removeItemFromArray = function (ArrayItem, OriginalArray) { var NewArray = new Array(); for (var i = 0; i < OriginalArray.length; i++) { if (OriginalArray[i] != ArrayItem) NewArray.push(OriginalArray[i]); } return NewArray; }

// Events
attachEventListener = function (Element, Method, Function, Capture) { if (Element.addEventListener) Element.addEventListener(Method, Function, Capture); else if (Element.attachEvent) Element.attachEvent("on" + Method, Function); else eval("Element.on" + Method + "=" + Function); }
detachEventListener = function (Element, Method, Function, Capture) { if (Element.removeEventListener) Element.removeEventListener(Method, Function, Capture); else if (Element.detachEvent) Element.detachEvent("on" + Method, Function); else eval("delete Element.on" + Method); }
cancelEventBubble = function (Event) { if (!Event) Event = window.event; if (Event.cancelBubble){ Event.cancelBubble = true;	Event.returnValue = false; } if (Event.preventDefault) Event.preventDefault(); if (Event.stopPropagation) Event.stopPropagation(); }
getEventTarget = function (Event) { var Target; if (!Event) var Event = window.event; if (Event.target) Target = Event.target; else if (Event.srcElement) Target = Event.srcElement; if (Target && Target.nodeType == 3) Target = Target.parentNode; return Target; }
getKeyCode = function (Event) { if (!Event) var Event = window.event; if (Event.keyCode) return Event.keyCode; else if (Event.which) return Event.which; }
isEnterKey = function (Event) { if (getKeyCode(Event) == 13) return true; else return false; }

// Document
getDocumentWidth = function () { if (window.innerWidth) return window.innerWidth; if (document.documentElement.clientWidth) return document.documentElement.clientWidth; if (document.body.clientWidth) return document.body.clientWidth; }	
getDocumentHeight = function () { if (window.innerHeight) return window.innerHeight; if (document.documentElement.clientHeight) return document.documentElement.clientHeight; if (document.body.clientHeight) return document.body.clientHeight; }	
getScrollTop = function () { if (document.documentElement && document.documentElement.scrollTop) return document.documentElement.scrollTop; else if (document.body) return document.body.scrollTop; else return 0; }
getScrollLeft = function () { if (document.documentElement && document.documentElement.scrollLeft) return document.documentElement.scrollLeft; else if (document.body) return document.body.scrollLeft; else return 0; }

// Strings
isValidEmail = function (Email) { return Email.match(/^([a-zA-Z0-9_\-\.\']+)@((\[[0-9]{1,3} \.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)/gi); }
isValidFormText = function (Text) { return !Text.match(/[\&\#\<\>]+/gi); }
isValidExtension = function (FilePath, Extension) { var Extensions = Extension.split(","); for (var i = 0; i < Extensions.length; i++) { if (FilePath.match(new RegExp(Extensions[i] + "$", "gi"))) return true; } return false; }
isEmpty = function (Text) { return !Text.toString().match(/[^\s]+/gi); }
sentenceCase = function (Text) { var Words = Text.split(" "); var Sentence = ""; for (var i = 0; i < Words.length; i++) { Sentence += Words[i].replace(/^[a-z]/gi, Words[i].substr(0,1).toUpperCase()) + (i < Words.length - 1 ? " " : ""); } return Sentence; }
trimWhiteSpace = function (Text) { return Text.replace(/^\s*/, "").replace(/\s*$/, ""); }
truncate = function(Text, Length, Suffix) { return (Text.length <= Length) ? Text : Text.substr(0, Length) + "..."; }

// Toggle select box visibility (IE only)
toggleSelectBoxes = function (State) { if (!document.all) return; var Selects = document.getElementsByTagName("select"); for (var i = 0; i < Selects.length; i++) Selects[i].style.visibility = (State == "on") ? "visible" : "hidden"; }
hideSelectBoxes = function () { toggleSelectBoxes("off"); }
showSelectBoxes = function () { toggleSelectBoxes("on"); }

// Easing functions -- Copyright (c) 2001 Robert Penner
// t: current time, b: beginning value, c: change in value, d: duration
strongEaseOut = function (t, b, c, d) { return c*((t=t/d-1)*t*t*t*t + 1) + b; }
strongEaseIn = function (t, b, c, d) { return c*(t/=d)*t*t*t*t + b; }
strongEaseInOut = function (t, b, c, d) { if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b; return c/2*((t-=2)*t*t*t*t + 2) + b; }
bounceEaseOut = function (t, b, c, d) { if ((t/=d) < (1/2.75)) { return c*(7.5625*t*t) + b; } else if (t < (2/2.75)) { return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b; } else if (t < (2.5/2.75)) { return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b; } else { return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b; } }
bounceEaseIn = function (t, b, c, d) { return c - bounceEaseOut (d-t, 0, c, d) + b; }
bounceEaseInOut = function(t, b, c, d) { if (t < d/2) return bounceEaseIn (t*2, 0, c, d) * .5 + b; else return bounceEaseOut (t*2-d, 0, c, d) * .5 + c*.5 + b; }

// Write anti-spam e-mail address
// arguments[0]: username, arguments[1-n]: domain
noSpam = function () { var EmailHtml = ""; for (var i = 0; i < arguments.length; i++) { EmailHtml += arguments[i]; if (i == 0) EmailHtml += "@"; else if (i < arguments.length - 1) EmailHtml += "."; } EmailHtml = "<a href=\"mail" + "to:" + EmailHtml + "\" title=\"Send an e-mail to " + EmailHtml + "\">" + EmailHtml + "</a>"; document.write (EmailHtml); }

// Modal dialogues
showAlert = function (Message) { alert(Message); }
submitUpload = function () { var NeatUploadId = ""; var Inputs = document.getElementsByTagName("INPUT"); for (var i = 0; i < Inputs.length; i++) { if (Inputs[i].type.toLowerCase() == "file" && Inputs[i].value.match(/[^\s]+/gi)) { NeatUploadId = Inputs[i].name.match(/^NeatUpload_[0-9A-Z]+/gi); } } showModal(""); if (!$("ModalIframe")) { var ModalIframe = document.createElement("iframe"); ModalIframe.setAttribute("id", "ModalIframe"); ModalIframe.setAttribute("width", "400"); ModalIframe.setAttribute("height", "100"); ModalIframe.setAttribute("scrolling", "no"); ModalIframe.setAttribute("frameborder", "0"); $("ModalDialogue").appendChild(ModalIframe); } $("ModalIframe").src = "/Common/Scripts/Upload/UploadProgressHtml.aspx?postBackID=" + NeatUploadId; setTimeout("document.forms[0].submit();", 1000); }
showModal = function (Html, Width) { hideSelectBoxes(); if (!$("ModalOverlay")) { var ModalOverlay = document.createElement("div"); ModalOverlay.setAttribute("id", "ModalOverlay"); document.body.appendChild(ModalOverlay); } if (!$("ModalDialogue")) { var ModalDialogue = document.createElement("div"); ModalDialogue.setAttribute("id", "ModalDialogue"); ModalDialogue.setAttribute("style", "display:none;top:-10000;left:-10000;"); document.body.appendChild(ModalDialogue); } setHTML($("ModalDialogue"), Html); if (Width) setWidth($("ModalDialogue"), Width); setModal(); if ($("ModalOverlay").StageListener) clearInterval($("ModalOverlay").StageListener); $("ModalOverlay").StageListener = setInterval("setModal()", 50); }
closeModal = function () { if ($("ModalOverlay") && $("ModalOverlay").StageListener) clearInterval($("ModalOverlay").StageListener); if($("ModalDialogue")) $("ModalDialogue").parentNode.removeChild($("ModalDialogue")); if($("ModalOverlay")) $("ModalOverlay").parentNode.removeChild($("ModalOverlay")); showSelectBoxes(); }
setModal = function () { setTop($("ModalOverlay"), getScrollTop()); setLeft($("ModalOverlay"), getScrollLeft()); setHeight($("ModalOverlay"), getDocumentHeight()); setTop($("ModalDialogue"), getScrollTop() + (getDocumentHeight() - getHeight($("ModalDialogue"))) / 2); setLeft($("ModalDialogue"), getScrollLeft() + (getDocumentWidth() - getWidth($("ModalDialogue"))) / 2); show($("ModalDialogue")); }

// Tasks
monitorTaskProgress = function (TaskId) { sendPostRequest("/Common/Scripts/Task/TaskProgressXml.aspx", "<request taskid=\"" + TaskId + "\" abort=\"false\" />", "getTaskProgress"); }
abortTask = function (TaskId) { sendPostRequest("/Common/Scripts/Task/TaskProgressXml.aspx", "<request taskid=\"" + TaskId + "\" abort=\"true\" />", "getTaskProgress"); }
getTaskProgress = function (Request) { var Xml = Request.responseXML; var Id =  Xml.documentElement.getAttribute("id"); var PercentComplete = Xml.documentElement.getAttribute("percentcomplete"); var ElapsedTime = Xml.documentElement.getAttribute("elapsedtime"); var Status = Xml.documentElement.getAttribute("status"); var Error = Xml.documentElement.firstChild.nodeValue; UpdateTaskProgress(PercentComplete, Error); if (PercentComplete < 1) sendPostRequest("/Common/Scripts/Task/TaskProgressXml.aspx", "<request taskid=\"" + Id + "\" abort=\"false\" />", "getTaskProgress"); }

// AJAX
makeHttpRequest = function (ScriptUrl, Method, PostData, SuccessCallbackFunction, FailureCallbackFunction) { var Request; var RefreshQS = "Refresh=" + (Math.random() * 999999); if (ScriptUrl.indexOf("?") > -1) ScriptUrl += "&" + RefreshQS; else ScriptUrl += "?" + RefreshQS; if (window.XMLHttpRequest) { Request = new XMLHttpRequest(); Request.onreadystatechange = function () { if(Request.readyState == 4) { if (Request.status != 200) eval(FailureCallbackFunction + "(Request);"); else eval(SuccessCallbackFunction + "(Request);"); } }; Request.open(Method, ScriptUrl, true);  if (Method.toUpperCase() == "POST") Request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");  Request.send(PostData); } else if (window.ActiveXObject) { Request = new ActiveXObject("Microsoft.XMLHTTP"); if (Request) { Request.onreadystatechange = function () { if(Request.readyState == 4) { if (Request.status != 200) eval(FailureCallbackFunction + "(Request);"); else eval(SuccessCallbackFunction + "(Request);"); } }; Request.open(Method, ScriptUrl, true); if (Method.toUpperCase() == "POST") Request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); Request.send(PostData); } } return Request; }
makeHttpRequestFailed = function (Request) { showAlert(Request.responseText); /*"<div class=\"Warning\">Unable to complete request!</div>"); */}
sendPostRequest = function (ScriptUrl, PostData, CallbackFunction) { makeHttpRequest (ScriptUrl, "POST", PostData, CallbackFunction, "makeHttpRequestFailed"); }
sendGetRequest = function (ScriptUrl, CallbackFunction) { makeHttpRequest (ScriptUrl, "GET", "", CallbackFunction, "makeHttpRequestFailed"); }