// // Get browser type // var ie = (document.all && !document.getElementById) ? true : false; var mz = (document.getElementById) ? true : false; var MacIE = ((navigator.userAgent.indexOf('MSIE') > -1) && (navigator.platform != null) && (navigator.platform.indexOf('Mac') > -1)); String.format = function() { if (arguments.length == 0) return null; var str = arguments[0]; for (var i = 1; i < arguments.length; i++) { var re = new RegExp('\\{' + (i - 1) + '\\}', 'gm'); str = str.replace(re, arguments[i]); } return str; } //----------------------------------------------------------------- // Make an array of images //----------------------------------------------------------------- function makeArray(s) { this.length = s; for (var i = 0; i < s; i++) this[i] = new Image(); return this } //----------------------------------------------------------------- // Trims a string of whitespace //----------------------------------------------------------------- function TrimString() { return this.replace(/^\s+/g, '').replace(/\s+$/g, ''); } //----------------------------------------------------------------- // Trims whitespace from the left of the string //----------------------------------------------------------------- function LeftTrimString() { return this.replace(/^\s+/g, ''); } //----------------------------------------------------------------- // Trims whitespace from the right of the string //----------------------------------------------------------------- function RightTrimString() { return this.replace(/\s+$/g, ''); } // Add the methods to strings String.prototype.ltrim = LeftTrimString; String.prototype.rtrim = RightTrimString; String.prototype.trim = TrimString; //----------------------------------------------------------------- // Array methods for backward compatibility //----------------------------------------------------------------- function arrayIndexOf(searchElement, fromIndex) { if (!fromIndex) fromIndex = 0; // If fromIndex is less than zero it's an offset from the end if (fromIndex < 0) fromIndex = (this.length + fromIndex <= 0) ? 0 : this.length - 1 + fromIndex; // Make sure it's an object at the very least for (var i = fromIndex; i < this.length; i++) { if (this[i] == searchElement) return i; } return -1; } function arrayLastIndexOf(searchElement, fromIndex) { if (!fromIndex || (fromIndex >= this.length)) fromIndex = this.length - 1; if (fromIndex < 0) fromIndex = (this.length + fromIndex <= 0) ? 0 : this.length - 1 + fromIndex; for (var i = fromIndex; i >= 0; i--) { if (this[i] == searchElement) return i; } return -1; } // Add one or more elements (just add more arguments) function arrayPush(elements) { for (var i = 0; i < arguments.length; i++) this[this.length] = arguments[i]; return this.length; } // Remove the last element function arrayPop() { var element = null; if (this.length > 0) element = this[this.length - 1]; this.splice(this.length - 1, 1); return element; } // // Add elements to/removes elements from an array // howMany = number to remove. elements is a list of elements to add. // function arraySplice(index, howMany, elements) { var removed = []; var numberToAdd = arguments.length - 2; var difference = numberToAdd - howMany; var newLength = this.length + difference; if (index > this.length) index = this.length; if (index < 0) index = (this.length + index < 0) ? 0 : this.length - 1 + index; if (howMany < 0) howMany = 0; // Remove anything to be removed if ((howMany > 0) && (index < this.length)) { if (index + howMany > this.length) howMany = this.length - index; for (var i = index; i < index + howMany; i++) removed.push(this[i]); } // Move the remaning elements back and chop off the remainder if (howMany > numberToAdd) { for (i = index + numberToAdd; i < newLength; i++) this[i] = this[i - difference]; this.length = newLength; } // Make room for the new stuff if (howMany < numberToAdd) { var count = 1; for (i = this.length - 1; i >= index + howMany; i--) { this[newLength - count] = this[i]; count++; } } // Add any items that need adding for (var i = 0; i < numberToAdd; i++) this[index + i] = arguments[i + 2]; return removed; } // Add functions to arrays if they're not already supported if (!Array.indexOf) Array.prototype.indexOf = arrayIndexOf; if (!Array.lastIndexOf) Array.prototype.lastIndexOf = arrayLastIndexOf; if (!Array.push) Array.prototype.push = arrayPush; if (!Array.pop) Array.prototype.pop = arrayPop; if (!Array.splice) Array.prototype.splice = arraySplice; //----------------------------------------------------------------- // Event methods - these won't work properly if you call them before // they've been defined. Calls should always come after the file // is imported or later in this file. //----------------------------------------------------------------- EventManager = new function() { // // Adds an event handler to an object. // old = use the old way (true) or addEventListener if possible (false) // this.addEvent = function(obj, evType, fn, old) { if (obj != null) { // Use the standard way if possible if (obj.addEventListener && !old) return obj.addEventListener(evType, fn, false); // If this is the first added event, set the unload event to clean up. if (!EventManager.eventObjects) { EventManager.eventObjects = []; // We add a function to add another event handler to make sure the cleaup handler is the last thing executed. EventManager.addEvent(window, 'unload', function() { EventManager.addEvent(window, 'unload', EventManager.unloadEvents); }, true); } // Otherwise use the hacky way if (!obj.eventCache) { obj.eventCache = {}; EventManager.eventObjects.push(obj); } if (!obj.eventCache[evType]) { obj.eventCache[evType] = []; // Add any existing events to the cache if (obj['on' + evType] && (obj['on' + evType] != null)) { var existingHandler = obj['on' + evType]; if (typeof(existingHandler) != 'function') existingHandler = new Function('', existingHandler); obj.eventCache[evType].push(existingHandler); } obj['on' + evType] = EventManager.raiseEvents; } // Make sure the event is only added once if (obj.eventCache[evType].indexOf(fn) < 0) obj.eventCache[evType].push(fn); } } // // Removes an event - parameters are the same as addEvent. // this.removeEvent = function(obj, evType, fn, old) { if (obj != null) { if (obj.removeEventListener && !old) return obj.removeEventListener(evType, fn, false); if (!obj.eventCache || !obj.eventCache[evType]) return; var index = obj.eventCache[evType].indexOf(fn); if (index > -1) obj.eventCache[evType].splice(index, 1); } } // // Raise events for IE (or someone who has chosen to do things the old way) // this.raiseEvents = function(e) { e = e || window.event; e = standardiseEventArgs(e); var objEvents = this.eventCache[e.type]; var r = true; for (var i = 0; i < objEvents.length; i++) { var fr; if (objEvents[i].call) fr = objEvents[i].call(this, e); else { this.raiseEvent = objEvents[i]; fr = this.raiseEvent(e); this.raiseEvent = null; } // Default to true if the handler has no return value r = (((typeof(fr) == 'undefined') || fr) && r); } // Only return if false, otherwise it screws onbeforeunload up in IE (you have to confirm leaving the page!) if (!r) return false; } // // Unloads all the added events on unload to free memory in IE // this.unloadEvents = function(e) { for (var i = 0; i < EventManager.eventObjects.length; i++) { for (var n in EventManager.eventObjects[i].eventCache) EventManager.eventObjects[i]['on' + n] = null; EventManager.eventObjects[i].eventCache = null; } EventManager.eventObjects = null; } // // Cancels the specified event. If c is true, bubbling will also be cancelled. // this.cancelEvent = function(e, c) { e.returnValue = false; if (e.preventDefault) e.preventDefault(); if (c) { e.cancelBubble = true; if (e.stopPropagation) e.stopPropagation(); } } // // Change some IE event arg properties so we can treat everything the W3C way // standardiseEventArgs = function(e) { if (!e.target && e.srcElement) e.target = e.srcElement; if (!e.relatedTarget) { if ((e.type == 'mouseover') && e.fromElement) e.relatedTarget = e.fromElement; else if ((e.type == 'mouseout') && e.toElement) e.relatedTarget = e.toElement; } /* fix safari bug */ if ((e.target != null) && (e.target.nodeType == 3)) e.target = e.target.parentNode; return e; } }; // Encode a string for query string function enc(s) { if (window.encodeURIComponent) return encodeURIComponent(s); else if (window.escape) return escape(s).replace('+', '%2B'); return s; } //----------------------------------------------------------------- // Perform a mouseover on an image //----------------------------------------------------------------- function ImageOver(ImageArray, ImageName, n) { document.images[ImageName].src = ImageArray[n].src; } //----------------------------------------------------------------- // Perform a mouseout on an image //----------------------------------------------------------------- function ImageOut(ImageArray, ImageName, n) { document.images[ImageName].src = ImageArray[n].src; } //----------------------------------------------------------------- // Gets an element by ID //----------------------------------------------------------------- function GetObject(ObjectId, objectContext) { if (!objectContext) objectContext = document; return (ie) ? objectContext.all[ObjectId] : objectContext.getElementById(ObjectId); } //----------------------------------------------------------------- // Gets the width of space available inside the browser //----------------------------------------------------------------- function GetAvailableWidth() { if (self.innerWidth) return parseInt(self.innerWidth); // All but IE if (document.documentElement && document.documentElement.clientWidth) return parseInt(document.documentElement.clientWidth); // IE6 else return parseInt(document.body.clientWidth); // Older IE } //----------------------------------------------------------------- // Gets the height of space available inside the browser //----------------------------------------------------------------- function GetAvailableHeight() { if (self.innerHeight) return parseInt(self.innerHeight); // All but IE if (document.documentElement && document.documentElement.clientHeight) return parseInt(document.documentElement.clientHeight); // IE6 else return parseInt(document.body.clientHeight); // Older IE } //----------------------------------------------------------------- // Show something //----------------------------------------------------------------- function ShowObject(object, displayValue) { if (typeof(object) != 'object') object = GetObject(object); if (object != null) { if (!displayValue) displayValue = null; // ie actually means pre ie5.5 if (!ie && (displayValue != null)) { object.style.display = displayValue; } else { object.style.display = 'inline'; } } } //----------------------------------------------------------------- // Hide something //----------------------------------------------------------------- function HideObject(object, displayValue) { if (typeof(object) != 'object') object = GetObject(object); if (object != null) { if (!displayValue) displayValue = null; if (!ie && (displayValue != null)) object.style.display = displayValue; else object.style.display = 'none'; } } //----------------------------------------------------------------- // Shows or hides an array of rows. Set Show to true to show, or false to hide. // The 'table-row-group' stuff is so that Safari and Mozilla show/hide the form // correctly. //----------------------------------------------------------------- function ShowRows(RowGroup, Show) { if (Show) ShowObject(RowGroup, ((document.all) ? 'block' : 'table-row-group')); else HideObject(RowGroup, 'none'); } //----------------------------------------------------------------- // This jazz is for hiding selects underneath popups in IE //----------------------------------------------------------------- // Hide overlaps [IE only] // obj must be visible for this to work function hideOverlappingSelects(obj) { if (document.all) { var upperTL = new Point(getPosition(obj, 'Left'), getPosition(obj, 'Top')); var upperBR = new Point(upperTL.x + obj.offsetWidth, upperTL.y + obj.offsetHeight); var selects = document.getElementsByTagName("select"); var childSelects = obj.getElementsByTagName("select"); for (var i = 0; i < selects.length; i++) { // Make sure we can do an indexOf on the collection if (!childSelects.indexOf) childSelects.indexOf = arrayIndexOf; // Make sure the select isn't a child of the overlapping object if (childSelects.indexOf(selects[i]) < 0) { var lowerTL = new Point(getPosition(selects[i], 'Left'), getPosition(selects[i], 'Top')); var lowerBR = new Point(lowerTL.x + selects[i].offsetWidth, lowerTL.y + selects[i].offsetHeight); if (objectsIntersect(upperTL, upperBR, lowerTL, lowerBR)) { selects[i].style.visibility = 'hidden'; selects[i].overlapHide = true; } } } } } // Show overlaps function showOverlappingSelects() { if (document.all) { var selects = document.getElementsByTagName("select"); for (var i = 0; i < selects.length; i++) { if (selects[i].overlapHide) { selects[i].style.visibility = 'visible'; selects[i].overlapHide = false; } } } } // Gets the top or left position of an object (IE only) function getPosition(obj, TopOrLeft) { var offset = 0; if (obj.offsetParent && (obj.offsetParent != document.body)) offset = getPosition(obj.offsetParent, TopOrLeft); return offset + obj['offset' + TopOrLeft]; } // Checks to see if two objects intersect function objectsIntersect(aTL, aBR, bTL, bBR) { return (planesIntersect(aTL.x, aBR.x, bTL.x, bBR.x) && planesIntersect(aTL.y, aBR.y, bTL.y, bBR.y)); } // Checks to see if two planes intersect. function planesIntersect(aStart, aEnd, bStart, bEnd) { return (((bStart <= aStart) && (bEnd > aStart)) || ((bStart < aEnd) && (bEnd >= aEnd)) || ((bStart >= aStart) && (bEnd <= aEnd))) } //----------------------------------------------------------------- // Open a window with a toolbar //----------------------------------------------------------------- function OpenWindow(Url, Name, Width, Height, options) { w = window.open(Url, Name, 'width=' + Width +',height=' + Height +',status=1,resizable=1,toolbar=1,scrollbars=1,top=20,left=20,screenY=20,screenX=20' + ((options) ? options : '')); w.focus(); } //----------------------------------------------------------------- // Open a window without a toolbar //----------------------------------------------------------------- function OpenSmallWindow(url, name, width, height, status, resizable, scrollbars) { status = ((typeof(status) == 'undefined') || status) ? 1 : 0; // Default to true resizable = ((typeof(resizable) == 'undefined') || resizable) ? 1 : 0; // default to true scrollbars = (scrollbars) ? 1 : 0; // Default to false w = window.open(url, name, 'width=' + width + ',height=' + height + ',status=' + status + ',resizable=' + resizable + ',scrollbars=' + scrollbars + ',top=100,left=100,screenY=100,screenX=100'); w.focus(); } //----------------------------------------------------------------- // Opens a help window //----------------------------------------------------------------- function OpenHelp(HelpFile) { OpenHelpWH(HelpFile, 475, 350, 1); } //----------------------------------------------------------------- // Opens a calendar window for date picking! //----------------------------------------------------------------- function OpenCalendar(elementID, showTime, timeRequired) { var timeString = ''; if (showTime) { timeString = 'ShowTime&'; if (timeRequired) timeString += 'TimeRequired&'; } var CalendarUrl = ApplicationPath + '/Common/DatePicker.aspx?' + timeString + '&ElementID=' + elementID; if (GetObject(elementID).value != '') CalendarUrl += '&Date=' + escape(GetObject(elementID).value); OpenSmallWindow(CalendarUrl, 'Calendar', 245, (showTime) ? 210 : 185, false); } // Gets the ASP.NET control prefix from a full control ID function GetControlPrefix(fullID, baseID) { return (fullID.indexOf(baseID) > -1) ? fullID.substring(0, fullID.indexOf(baseID)) : ''; } // MarioM 05/10/2006 - For some reason this func was removed by TimD on 30/03/2006, but // its removal broke Admin/Commerce/OrderDetails.aspx - had to reinstate it. //----------------------------------------------------------------- // Gets the name of the form upon which the named control exists. //----------------------------------------------------------------- // Store the name so we only have to look it up once var __FormName = ''; function GetFormName(ControlName) { if (__FormName.length == 0) { if (document.forms.length == 1) __FormName = document.forms[0].name; else { for (var i = 0; i < document.forms.length; i++) { if (document.forms[i].elements[ControlName] != null) { __FormName = document.forms[i].name; break; } } } } return __FormName; } // // Cursor setting stuff // var __cursorTimeout = null; function WaitCursor() { document.body.style.cursor = 'wait'; __cursorTimeout = setTimeout('ResetCursor()', 10000); } function ResetCursor() { if (__cursorTimeout != null) { clearTimeout(__cursorTimeout); __cursorTimeout == null; } document.body.style.cursor = 'default'; } function Point(x, y) { this.x = x; this.y = y; } // // Get the position of the mouse using event args // function GetMousePosition(e) { var x = 0; var y = 0; if (e.pageX || e.pageY) { x = e.pageX; y = e.pageY; } else if ((e.clientX || e.clientY) && document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) { x = e.clientX + document.documentElement.scrollLeft; y = e.clientY + document.documentElement.scrollTop; } else if (e.clientX || e.clientY) { x = e.clientX + document.body.scrollLeft; y = e.clientY + document.body.scrollTop; } return new Point(x, y); } // // Check a bunch of ImageCheckBoxes // var _SelectCheckBoxes = null; function CheckImageCheckBoxes(ImageCheckBoxPrefixes, CheckBoxName) { if (_SelectCheckBoxes == null) _SelectCheckBoxes = new Object(); if (_SelectCheckBoxes[CheckBoxName] == null) { _SelectCheckBoxes[CheckBoxName] = true; } for (var i = 0; i < ImageCheckBoxPrefixes.length; i++) { ImageCheckBox.setChecked(GetObject(ImageCheckBoxPrefixes[i] + CheckBoxName), _SelectCheckBoxes[CheckBoxName]); } _SelectCheckBoxes[CheckBoxName] = !_SelectCheckBoxes[CheckBoxName]; } // // If more than one checkbox is checked, pops up a dialog box to confirm deletion of 'items'. // If not items are checked, returns false. // function ConfirmMultipleDelete( CheckBoxPrefixes, CheckBoxName ) { var CheckCount = 0; for (var i = 0; i < CheckBoxPrefixes.length; i++) { if (ImageCheckBox.isChecked(GetObject(CheckBoxPrefixes[i] + CheckBoxName))) CheckCount++; } if (CheckCount == 0) { alert('You need to select at least one item before clicking on delete button.'); return false; } else if (CheckCount == 1) return confirm('Are you sure you want to permanently delete this item?'); else return confirm(String.format('Are you sure you want to permanently delete these {0} items?', CheckCount)); } // // Behaves similar to ConfirmMultipleDelete() method except that is handles the special case when // the user is deleting all the items // function ConfirmAllDelete(CheckBoxPrefixes, CheckBoxName, AllMsg) { var CheckCount = 0; for (var i = 0; i < CheckBoxPrefixes.length; i++) { if (ImageCheckBox.isChecked(GetObject(CheckBoxPrefixes[i] + CheckBoxName))) CheckCount++; } if (CheckCount == 0) return false; // check all msg was not specified set a generic message if(AllMsg.length == 0) AllMsg = 'Are you sure you want to delete all the items?'; else if (CheckCount == 1) return confirm('Are you sure you want to permanently delete this item?'); else if (CheckCount == CheckBoxPrefixes.length) return confirm(AllMsg); else return confirm(String.format('Are you sure you want to permanently delete these {0} items?', CheckCount ) ); } // //Fire a confirm message box and wait for use to accept and then does a post back if user //accepts. function ConfirmDuplicateProject(elementID,buttonID) { var obj = null; obj = GetObject(elementID); if(obj.value == 'true') { if (confirm('A project with this name or job number or project type already exists. Do you want to continue?')) { //raise the event to post back on the next button obj.value = true; __doPostBack(buttonID,''); } else obj.value = false; } } // // If more than one checkbox is checked, pops up a dialog box to confirm editing of the first 'item'. // If not items are checked, returns false. // function ConfirmEdit(CheckBoxPrefixes, CheckBoxName) { var CheckCount = 0; for (var i = 0; i < CheckBoxPrefixes.length; i++) { if (ImageCheckBox.isChecked(GetObject(CheckBoxPrefixes[i] + CheckBoxName))) CheckCount++; } if (CheckCount == 0) { alert('You need to select at least one item before clicking on edit button.'); return false; } else if (CheckCount > 1) return confirm('Only the first item can be edited. Do you want to continue?'); else return true; } // // If more than one checkbox is checked, pops up a dialog box to comfirm if user wants to send message // If not items are checked, returns false. // function ConfirmNotify(CheckBoxPrefixes, CheckBoxName) { var CheckCount = 0; var IsNewAccess = false; for (var i = 0; i < CheckBoxPrefixes.length; i++) { if(ImageCheckBox.isChecked(GetObject(CheckBoxPrefixes[i] + CheckBoxName))) { CheckCount++; if(ImageCheckBox.getStateElement(GetObject(CheckBoxPrefixes[i] + CheckBoxName)).previousState != 'Checked') { IsNewAccess = true; break; } } } if (CheckCount == 0) return false; else if (CheckCount = 1 && IsNewAccess) return confirm('You will now be presented with a list of new users granted access to this project, \nfor selection of who you would like to receive a welcome message.\n\nPlease click OK to proceed, or Cancel to skip this step.'); return false; } // // Confirm if the user wants to close the project // function ConfirmCloseProject(projectName, CurrentProjectStatus) { // show prompt only for closing table if(CurrentProjectStatus == '0') return confirm(String.format('You are about to close Project {0}. Are you sure?', projectName )); return true; } // Displays a new list of Childs for the selected Parent function ShowChildList(controlPrefix, load, selectedChild, parentListID, childListID, itemCollection) { var parentList = GetObject(controlPrefix + parentListID); var childList = GetObject(controlPrefix + childListID); if ((parentList != null) && (childList != null)) { var selected = parentList.selectedIndex; // Clear the Child list (leave new item) and selected the first option. ClearChildList(childList); childList.selectedIndex = 0; // Find the position/index at which the parent items start (without a '' value) var parentOffset = 0; for (var parentOffset = 0; parentOffset < parentList.options.length; parentOffset++) { if (parentList.options[parentOffset].value != '') break; } // Display a new list if a Parent has been selected if (selected >= parentOffset) { var childOptions = itemCollection[parentList.options[selected].value]; // Check to see if we have an object with an items collection (normally it would just be an array) if ((childOptions != null) && childOptions.items) childOptions = childOptions.items; if (childOptions != null) { for (var i = 0; i < childOptions.length; i++) { childList.options[childList.options.length] = new Option(childOptions[i][1], childOptions[i][0]); if (load && (childOptions[i][0] == selectedChild)) childList.selectedIndex = childList.options.length - 1; } } } } } // Clear the Child list, but leaves any blank items at the top function ClearChildList(childList) { if (childList != null) { var blankOptions = []; for (var i = 0; i < childList.options.length; i++) { if (childList.options[i].value == '') blankOptions[i] = childList.options[i]; else break; } // Clear the old Childs ClearOptions(childList); // Add the blank options back in for (var i = 0; i < blankOptions.length; i++) childList.options[i] = blankOptions[i]; } } // Clear the specified DropDownList of options function ClearOptions(listToClear) { while (listToClear.options.length) listToClear.options[0] = null; } //----------------------------------------------------------------- // Methods for validating a dropdown/textbox combo, such as Advertiser/product //----------------------------------------------------------------- // // Shows/hides the buttons required for adding an element to a form // function ShowNewItemForm(elementID, controlPrefix, Show) { if (Show) { HideObject(controlPrefix + elementID + 'Select', null); HideObject(controlPrefix + elementID + 'Add', null); ShowObject(controlPrefix + elementID + 'Enter', null); ShowObject(controlPrefix + elementID + 'AddButtons', null); GetObject(controlPrefix + elementID + 'UseText').value = 'true'; GetObject(controlPrefix + elementID + 'New').focus(); } else { HideObject(controlPrefix + elementID + 'Enter', null); HideObject(controlPrefix + elementID + 'AddButtons', null); ShowObject(controlPrefix + elementID + 'Select', null); ShowObject(controlPrefix + elementID + 'Add', null); GetObject(controlPrefix + elementID + 'UseText').value = 'false'; GetObject(controlPrefix + elementID + 'New').value = ''; } } // // Make sure that an item has been selected or a new one entered. // For this to work, the required field validator must have an // id of rfvAdvertiser, rfvProduct or rfvCheese. // function NewItemFormFieldsRequired_Validate(source, arguments) { var controlPrefix = (source.id.indexOf('_rfv') > -1) ? source.id.substring(0, source.id.indexOf('_rfv') + 1) : ''; var elementID = source.id.substring(0, source.id.indexOf('rfv') ) + source.id.substring(source.id.indexOf('rfv') + 3, source.id.length); arguments.IsValid = NewItemFormFieldsHasValue(elementID, controlPrefix); } // // Whether or not a value has been selected/entered for advertiser or product // function NewItemFormFieldsHasValue(elementID, controlPrefix) { var dropDown = GetObject(controlPrefix + elementID); if (dropDown == null) return true; var textBox = GetObject(controlPrefix + 'New' + elementID); var useText = (GetObject(controlPrefix + elementID + 'UseText').value == 'true'); var cssName=""; if (useText) cssName = textBox.className; else cssName = dropDown.className; if (cssName != "RequiredField") return true; return ((useText) ? (textBox.value.trim() != '') : (dropDown.selectedIndex > 0)); } // This controls the disable flag of an object based on another // e.g. enable a dropdown if a checkbox is click, disable otherwise etc function toggleObject(control, object) { var control = "isWorkflowFolder"; var object = "previousStageNumber"; var obj = GetObject(object); if(ImageCheckBox.isChecked(GetObject(control))) obj.disabled = false; else obj.disabled = true; } function isDefined(variable) { return eval('(typeof(' + variable + ') != "undefined");'); }