var undefined = "undefined";
document.preloadedImages = new Array();
/* ==================================== General Functions ==================================== */
function getElement(elementNameOrId) {
	if (document.getElementsByName(elementNameOrId)[0]) return document.getElementsByName(elementNameOrId)[0];
	if (document.getElementById(elementNameOrId)) return document.getElementById(elementNameOrId);
}
/*---------------------------------------------------------------------------------------------*/
function getElements(elementsName) {
	var result = null;

	if (document.getElementsByName(elementsName)) result = document.getElementsByName(elementsName);

	return result;
}
/*---------------------------------------------------------------------------------------------*/
function appLock() {
	var allInput = document.getElementsByTagName("input");
	var allSelect = document.getElementsByTagName("select");
	var allTextarea = document.getElementsByTagName("textarea");
	var allControl = [];
	for (var i = 0; i < allInput.length; i++) allControl.push(allInput[i]);
	for (var i = 0; i < allSelect.length; i++) allControl.push(allSelect[i]);
	for (var i = 0; i < allTextarea.length; i++) allControl.push(allTextarea[i]);
	
	top.locked = true;
	var isException;

	for (var counter = 0; counter < allControl.length; counter++) {
		isException = false;
		for (var exceptionCounter = 0; exceptionCounter < arguments.length; exceptionCounter++) {
			if (allControl[counter] == arguments[exceptionCounter]) {isException = true; break;}
		}

		switch (allControl[counter].nodeName.toLowerCase()) {
			case "select":
				allControl[counter].style.visibility = (isException?"visible":"hidden");
			break;

			case "input": case "textarea":
				allControl[counter].disabled = !isException;
			break;
		}
	}
}
/*---------------------------------------------------------------------------------------------*/
function appUnlock() {
	var allInput = document.getElementsByTagName("input");
	var allSelect = document.getElementsByTagName("select");
	var allTextarea = document.getElementsByTagName("textarea");
	var allControl = [];
	for (var i = 0; i < allInput.length; i++) allControl.push(allInput[i]);
	for (var i = 0; i < allSelect.length; i++) allControl.push(allSelect[i]);
	for (var i = 0; i < allTextarea.length; i++) allControl.push(allTextarea[i]);
	
	top.locked = false;

	for (var counter = 0; counter < allControl.length; counter++) {
		switch (allControl[counter].nodeName.toLowerCase()) {
			case "select":
				allControl[counter].style.visibility = "visible";
			break;

			case "input": case "textarea":
				allControl[counter].disabled = false;
			break;
		}
	}
}
/*---------------------------------------------------------------------------------------------*/
function preloadImages() {
	self.location.baseURL = self.location.protocol + "//" + self.location.host;

	for (var intArgumentsCounter = 0, blnExists = false; intArgumentsCounter < arguments.length; intArgumentsCounter++) {
		for (var intImagesCounter = 0; intImagesCounter < document.preloadedImages.length; intImagesCounter++) {
			if (document.preloadedImages[intImagesCounter].src == self.location.baseURL + arguments[intArgumentsCounter]) blnExists = true;
		}

		if (!blnExists) {
			var tmpImage = new Image();
			tmpImage.src = arguments[intArgumentsCounter];
			document.preloadedImages.push(tmpImage);
		}
	}
}
/*---------------------------------------------------------------------------------------------*/
function typeofNumber(aNumber) {
	var result = NaN;
	if (String(aNumber).trim().length == 0) {
		result = "empty";
	} else {
		if (parseFloat(aNumber) > 0) result = "positive";
		if (parseFloat(aNumber) < 0) result = "negative";
		if (parseFloat(aNumber) == 0 || aNumber == "0") result = "zero";
	}

	return result;
}
/*---------------------------------------------------------------------------------------------*/
function isNumeric(aNumber) {
	if (parseFloat(aNumber)) {
		return true;
	} else {
		return false;
	}
}
/*---------------------------------------------------------------------------------------------*/
function isNumericForComparison(objInput) {
	var strValidChar = "0123456789.";
	var strInput = String(objInput);

	var startAt = (strInput.charAt(0) == "-" && strInput.length > 1)?1:0;

	for (var intCounter = startAt; intCounter < strInput.length; intCounter++) {
		if (strValidChar.indexOf(strInput.charAt(intCounter)) == -1) return false
	}

	return true;
}
/* ===================================== Date Functions ====================================== */
String.prototype.shorten = shorten;

function shorten() {
	var tempDate = this.split("/");
	var result = this;

	if (tempDate.length == 3) {
		result = tempDate[1] + "/" + tempDate[2];
	}

	return result;
}
/*---------------------------------------------------------------------------------------------*/
function isDate(inString) {
	var result = Date.parse(inString);

	if (result > 0) {
		return true;
	} else {
		return false;
	}
}
/*---------------------------------------------------------------------------------------------*/
function stringCompare(string1, string2) {
	if (string1 == string2) {
		return 0;
	} else {
		var tempArray = new Array(String(string1), String(string2))

		tempArray.sort();
		if (String(string1) == tempArray[0]) {
			return -1;
		} else {
			return 1;
		}
	}
}
/*---------------------------------------------------------------------------------------------*/
var t = 1;
function parseDate(dt) {
    var vals = dt.split("/");
    var rt = new Date();
   
    rt.setDate(vals[0]);
    rt.setMonth(vals[1] - 1);
    rt.setFullYear(vals[2]);
    rt.setHours(0, 0, 0, 0);
    
    return rt;
}
function dateCompare2(date1, date2) {
    var dt1 = parseDate(date1); //new Date(date1);
    var dt2 = parseDate(date2); //new Date(date2);
    var val1 = dt1.getYear();
    var val2 = dt2.getYear();
    var rt;
    
    if(dt1.getMonth() <= 9) {
        val1 += "0";
    }
    val1 += dt1.getMonth();
    if(dt2.getMonth() <= 9) {
        val2 += "0";
    }
    val2 += dt2.getMonth();
    
    if(dt1.getDate() <= 9) {
        val1 += "0";
    }
    val1 += dt1.getDate();
    if(dt2.getDate() <= 9) {
        val2 += "0";
    }
    val2 += dt2.getDate();
    
    rt = stringCompare(val1, val2);
    
    return rt;
}
function dateCompare(date1, date2) {
    var dt1 = Date.parse(date1);
    var dt2 = Date.parse(date2);
    var rt = dt1 - dt2;
    
/*
	var tempDate1 = date1.split("/");
	var tempDate2 = date2.split("/");

	for (var counter = 2; counter >= 0; counter--) {
		if (parseInt(tempDate1[counter]) > parseInt(tempDate2[counter])) {
			return 1;
			break;
		}
	}
*/

	return rt;
}
/*---------------------------------------------------------------------------------------------*/
function compareDateNumberString(item1, item2) {
	var item1Priority, item2Priority, rt;

	item1Priority = 0;
	if (isNumericForComparison(item1)) item1Priority = 2;
	if (isDate(item1)) item1Priority = 1;

	item2Priority = 0;
	if (isNumericForComparison(item2)) item2Priority = 2;
	if (isDate(item2)) item2Priority = 1;

	if ((item1Priority - item2Priority) != 0) {
		rt = (item1Priority - item2Priority)
	} else {
		switch (item1Priority) {
			case 2:
				rt = (Number(item1) - Number(item2))
			break;

			case 1:
				//rt = (dateCompare(item1, item2));
				rt = (dateCompare2(item1, item2));
			break;

			case 0:
				rt = (stringCompare(item1, item2));
			break;
		}
	}
	
	return rt;
}
/* ==================================== String Functions ===================================== */
var ASCIItable = new Array(38, 39, 34, 44, 60, 62, 42, 13);
var HTMLtable = new Array("&#38;", "&#39;", "&#34;", "&#44;", "&lt;", "&gt;", "&#42;", "<BR>");

String.prototype.toHTML = toHTML;
String.prototype.toASCII = toASCII;
String.prototype.reFormat = reFormat;
String.prototype.trim = trim;
String.prototype.truncate = truncate;
/*---------------------------------------------------------------------------------------------*/
function toHTML() {
	var tempString = this;

	tempString = tempString.replace(String.fromCharCode(13) + String.fromCharCode(10), "<BR> ");

	for (var counter = 0; counter < ASCIItable.length; counter++) {
		tempString = tempString.replace(String.fromCharCode(ASCIItable[counter]), HTMLtable[counter]);
	}

	return tempString;
}
/*---------------------------------------------------------------------------------------------*/
function toASCII() {
	var tempString = this;

	tempString = tempString.replace("<BR> ", String.fromCharCode(13) + String.fromCharCode(10));

	for (var counter = 0; counter < ASCIItable.length; counter++) {
		tempString = tempString.replace(HTMLtable[counter], String.fromCharCode(ASCIItable[counter]));
	}

	return tempString;
}
/*---------------------------------------------------------------------------------------------*/
function trim() {
	var startSlice = null;
	var endSlice = 0;

	for (var counter = 0; counter < this.length; counter++) {
		if (this.charAt(counter) != " ") {
			if (startSlice == null)	startSlice = counter;
			endSlice = counter + 1;
		}
	}

	if (startSlice == null) startSlice = 0;
	return this.slice(startSlice, endSlice);
}
/*---------------------------------------------------------------------------------------------*/
function truncate(where) {
	var maxLength = (Number(where))?where:40;
	var refineArray = new Array();
	var puncsLocation = new Array("0");
	var tempLine = "";
	var result = "";

	for (var counter = 0; counter < this.length; counter++) {
		if ((this.charAt(counter) == ".") || (this.charAt(counter) == ",") || (this.charAt(counter) == "-") || (this.charAt(counter) == " ")) {
			puncsLocation.push(counter + 1);
		}
	}
	puncsLocation.push(this.length);

	for (var counter = 0; counter < puncsLocation.length - 1; counter++) {
		substrStart = puncsLocation[counter];
		substrEnd = puncsLocation[counter + 1] - substrStart;
		refineArray.push(this.substr(substrStart, substrEnd));
	}

	for (var counter = 0; counter < refineArray.length; counter++) {
		if (tempLine.length + refineArray[counter].length <= maxLength) {
			tempLine+= refineArray[counter];
		} else {
			if (refineArray[counter].length <= maxLength) {
				result+= tempLine + "<BR>";
				tempLine = refineArray[counter];
			} else {
				for (var exceedCounter = 0; exceedCounter < refineArray[counter].length; exceedCounter++) {
					if (tempLine.length <= maxLength) {
						tempLine+= refineArray[counter].charAt(exceedCounter);
					} else {
						result+= tempLine + "<BR>" + refineArray[counter].charAt(exceedCounter);
						tempLine = "";
					}
				}
			}
		}
	}
	if (tempLine.length > 0) {
		result+= tempLine;
	}
	return result;
}
/* ===================================== Array Functions ===================================== */
Array.prototype.delItem = delItem;
Array.prototype.push = push;
Array.prototype.partition = partition;
Array.prototype.sortByObjectProperty = builtInSort;
/*---------------------------------------------------------------------------------------------*/
function delItem(iIndex) {
	var tempArray = new Array();

	for (var counter = 0; counter < this.length; counter++) {
		if (counter != iIndex) tempArray[tempArray.length] = this[counter];
	}

	return tempArray;
}
/*---------------------------------------------------------------------------------------------*/
function push(oObject) {
	this[this.length] = oObject;
}
/*---------------------------------------------------------------------------------------------*/
function insertionSortByObjectProperty(propertyName, _secondPropertyName) {
	var tempObject = null;
	var secondPropertyName = (_secondPropertyName)?_secondPropertyName:propertyName;

	for (var counter1 = 1; counter1 < this.length; counter1++) {
		for (var counter2 = (counter1 - 1); counter2 >= 0; counter2--) {
			switch (compareDateNumberString(eval("this[" + counter2 + "]." + propertyName), eval("this[" + (counter2 + 1) + "]." + propertyName))) {
				case (-1):
					tempObject = this[(counter2 + 1)];
					this[(counter2 + 1)] = this[counter2];
					this[counter2] = tempObject;
				break;

				case (0):
					if (compareDateNumberString(eval("this[" + counter2 + "]." + secondPropertyName), eval("this[" + (counter2 + 1) + "]." + secondPropertyName)) == -1) {
						tempObject = this[(counter2 + 1)];
						this[(counter2 + 1)] = this[counter2];
						this[counter2] = tempObject;
					}
				break;
			}
		}
	}
}
/*---------------------------------------------------------------------------------------------*/
function quickSortByObjectProperty(propertyName, _secondPropertyName, low, high) {
	var secondPropertyName = (_secondPropertyName)?_secondPropertyName:propertyName;

	var pivot;

	if (low < high) {
		pivot = this.partition(propertyName, secondPropertyName, low, high);
		this.sortByObjectProperty(propertyName, secondPropertyName, low, pivot - 1);
		this.sortByObjectProperty(propertyName, secondPropertyName, pivot + 1, high);
	}
}
/*---------------------------------------------------------------------------------------------*/
function partition(propertyName, _secondPropertyName, low, high) {
	var pivot, pivotPosition, counter, tempItem, compareResult;
	var secondPropertyName = (_secondPropertyName)?_secondPropertyName:propertyName;

	pivotPosition = low;
	pivot = eval("this[" + pivotPosition + "]." + propertyName);

	for (counter = low + 1; counter < high + 1; counter++) {
		compareResult = compareDateNumberString(eval("this[" + counter + "]." + propertyName), pivot);

		if (compareResult < 0) {
			pivotPosition++;

			tempItem = this[pivotPosition];
			this[pivotPosition] = this[counter];
			this[counter] = tempItem;
		}

		if (compareResult == 0) {
			compareResult = compareResult = compareDateNumberString(eval("this[" + counter + "]." + secondPropertyName), eval("this[" + pivotPosition + "]." + secondPropertyName));

			if (compareResult < 0) {
				pivotPosition++;

				tempItem = this[pivotPosition];
				this[pivotPosition] = this[counter];
				this[counter] = tempItem;
			}
		}
	}

	tempItem = this[pivotPosition];
	this[pivotPosition] = this[low];
	this[low] = tempItem;

	return pivotPosition;
}
/*---------------------------------------------------------------------------------------------*/
function builtInSort(strPropertyName) {
	this.sort(
		function (a,b) {
		    var rt;
			var item1Priority = 0;
			if (isNumericForComparison(a[strPropertyName])) item1Priority = 2;
			if (isDate(a[strPropertyName])) item1Priority = 1;

			var item2Priority = 0;
			if (isNumericForComparison(b[strPropertyName])) item2Priority = 2;
			if (isDate(b[strPropertyName])) item2Priority = 1;

			if ((item1Priority - item2Priority) != 0) {
				rt = (item1Priority - item2Priority)
			} else {
				switch (item1Priority) {
					case 2:
						rt = (Number(a[strPropertyName]) - Number(b[strPropertyName]))
					break;

					case 1:
						rt = (dateCompare2(a[strPropertyName], b[strPropertyName]));
					break;

					case 0:
						rt = (stringCompare(a[strPropertyName], b[strPropertyName]));
					break;
				}
			}
			
			return rt;
		}
	);
}
/* ==================================== Number Functions ===================================== */
Number.prototype.reFormat = reFormat;
Number.prototype.toHTML = function() {return this}
Number.prototype.toASCII = function() {return this}
/*---------------------------------------------------------------------------------------------*/
function reFormat(NumOfDigitsAfterDecimal, clear) {
	var tempString = String(this);
	if ((typeofNumber(this) != "empty") && (typeofNumber(this) != NaN)) {
		var negative = false;
		var result = "";
		var digitsCounter = 1;
		var numLength = 0;

		if (tempString.indexOf("-") == 0) {
			tempString = tempString.slice(1, tempString.length);
			negative = true;
		}

		if (String(tempString).indexOf(".") == -1) {
			if (Number(NumOfDigitsAfterDecimal)) {
				result = "."
				for (var counter = 0; counter < NumOfDigitsAfterDecimal; counter++) {
					result+= '0';
				}
			}
			numLength = String(tempString).length - 1;
		} else {
			result = String(tempString).substr(String(tempString).indexOf("."), NumOfDigitsAfterDecimal + 1)
			numLength = String(tempString).indexOf(".") - 1
		}

		for (var counter = numLength; counter >= 0; counter--) {
			result = String(tempString).charAt(counter) + result;
			if (digitsCounter % 3 == 0 && digitsCounter != numLength + 1) if (clear != true) result= ',' + result;
			digitsCounter++;
		}

		if (negative) {
			if (clear != true) {
				result = "(" + result + ")";
			} else {
				result = "-" + result;
			}
		}

		return result;
	} else {
		return tempString;
	}
}
