// BEGIN - Get DOM elements
function $() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}
// END - Get DOM elements

// BEGIN - Event handlers
// Add event handler to given object
function AddEventHandler(obj, eventName, functionNotify) {
	if (obj.attachEvent) {
		obj.attachEvent('on' + eventName, functionNotify);
	}
	else if (obj.addEventListener) {
		obj.addEventListener(eventName, functionNotify, true);
	}
	else {
		obj['on' + eventName] = functionNotify;
	}
}

// Remove event handler from given object
function RemoveEventHandler(obj, eventName, functionNotify) {
	if (obj.detachEvent) {
		obj.detachEvent('on' + eventName, functionNotify);
	}
	else if (obj.removeEventListener) {
		obj.removeEventListener(eventName, functionNotify, true);
	}
	else {
		obj['on' + eventName] = null;
	}
}

// Return the Unicode key code associated with the key that caused the event
function GetEventKeyCode(evnt) {
	return evnt.keyCode ? evnt.keyCode : evnt.charCode ? evnt.charCode : evnt.which ? evnt.which : void 0;
}
// END - Event handler

// BEGIN - Disable button
// Disable a button after click to prevent subsequent postbacks to server
function DisableButton(buttonElem) {
	buttonElem.value = 'Please Wait...';
	buttonElem.disabled = true;
}
// END - Disable button

// BEGIN - Watermark
// Remove watermark text
function WatermarkFocus(txtElem, strWatermark) {
	if (txtElem.value == strWatermark) txtElem.value = '';
}

// Add watermark text
function WatermarkBlur(txtElem, strWatermark) {
	if (txtElem.value == '') txtElem.value = strWatermark;
}
// END - Watermark

// BEGIN - Attribute helper
function GetAttributeValue(attribList, attribName, firstDelim, secondDelim) {
	var attribNameLowerCase = attribName.toLowerCase();
	if (attribList) {
		var attribArr = attribList.split(firstDelim);
		for (var i = 0, loopCnt = attribArr.length; i < loopCnt; i++) {
			var nameValueArr = attribArr[i].split(secondDelim);
			for (var j = 0, loopCnt2 = nameValueArr.length; j < loopCnt2; j++) {
				if (nameValueArr[0].toLowerCase().replace(/\s/g, '') == attribNameLowerCase && loopCnt2 > 1) {
					return nameValueArr[1];
				}
			}
		}
	}
}
// END - Attribute helper

// BEGIN - window helper
function GetScreenWidth() {
	return window.screen.availWidth;
}

function GetScreenHeight() {
	return window.screen.availHeight;
}

function WindowOpenHelper(sURL, sName, sFeatures, centerWindow) {
	var windowLeft = '';
	var windowTop = '';
	if (centerWindow) {
		var windowWidth = GetAttributeValue(sFeatures, 'width', ',', '=');
		windowLeft = (typeof(windowWidth) != 'undefined') ? ',left=' + ((GetScreenWidth() - windowWidth) / 2) : '';

		var windowHeight = GetAttributeValue(sFeatures, 'height', ',', '=');
		windowTop = (typeof(windowHeight) != 'undefined') ? ',top=' + ((GetScreenHeight() - windowHeight) / 2) : '';
	}
	window.open(sURL, sName, sFeatures + windowLeft + windowTop);
}
// END - window helper

// BEGIN - Max char
// Validate max char length in a given string
function ValidateMaxLength(evnt, str, maxLength) {
	var evntKeyCode = GetEventKeyCode(evnt);
	// Ignore keys such as Delete, Backspace, Shift, Ctrl, Alt, Insert, Delete, Home, End, Page Up, Page Down and arrow keys
    var escChars = ",8,17,18,19,33,34,35,36,37,38,39,40,45,46,";
	if (escChars.indexOf(',' + evntKeyCode + ',') == -1) {
        if (str.length >= maxLength) {
            alert("You cannot enter more than " + maxLength + " characters.");
            return false;
        }
    }
    return true;
}
// END - Max char

// BEGIN - Format number
// Format number
function FormatNumber(num, decimalPlaces, appendZeros, insertCommas) {
	var powerOfTen = Math.pow(10, decimalPlaces);
	var num = Math.round(num * powerOfTen) / powerOfTen;
	if (!appendZeros && !insertCommas) {
		return num;
	}
	else {
		var strNum = num.toString();
		var posDecimal = strNum.indexOf(".");
        if (appendZeros) {
		    var zeroToAppendCnt = 0;
		    if (posDecimal < 0) {
			    strNum += ".";
			    zeroToAppendCnt = decimalPlaces;
		    }
		    else {
			    zeroToAppendCnt = decimalPlaces - (strNum.length - posDecimal - 1);
		    }
		    for (var i = 0; i < zeroToAppendCnt; i++) {
			    strNum += "0";
		    }
		}
	    if (insertCommas && (Math.abs(num) >= 1000)) {
		    var i = strNum.indexOf(".");
		    if (i < 0) {
			    i = strNum.length;
            }
		    i -= 3;
		    while (i >= 1) {
			    strNum = strNum.substring(0, i) + ',' + strNum.substring(i, strNum.length);
			    i -= 3;
		    }		
	    }
		return strNum;
	}
}
// END - Format number

// BEGIN - Swap table rows
function SwapRows(rowElem, dirUp, ignoreFirstRow) {
	var rowElemToSwap = (dirUp) ? rowElem.previousSibling : rowElem.nextSibling;

	// Firefox returns a blank text node for the sibling
	while (rowElemToSwap && rowElemToSwap.nodeType != 1) {
		rowElemToSwap = (dirUp) ? rowElemToSwap.previousSibling : rowElemToSwap.nextSibling;
	}

	if (rowElemToSwap && !(ignoreFirstRow && rowElemToSwap.rowIndex == 0)) {
		var rowCells = rowElem.cells;
		var colInner;
		for (var i = 0, loopCnt = rowCells.length; i < loopCnt; i++) {
			colInner = rowCells[i].innerHTML;
			rowCells[i].innerHTML = rowElemToSwap.cells[i].innerHTML;
			rowElemToSwap.cells[i].innerHTML = colInner;
		}
	}
}
// END - Swap table rows

// BEGIN - Handle unload
// Add unload event handler
function AddUnloadHandler() {
	AddEventHandler(window, 'beforeunload', HandleUnload);
}

function HandleUnload() {
	var strConfirm = 'Please make sure you saved your changes before closing the page.';
	if (typeof(window.event) != 'undefined') {
		window.event.returnValue = strConfirm;
	}
	else {
		alert(strConfirm);
		var evnt = arguments[0];
		evnt.stopPropagation();
		evnt.preventDefault();
	}
}
// END - Handle unload

